* elf32-mips.c (mips_elf_obtain_contents): Swap 16-bit halves of
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8
9 This file is part of BFD, the Binary File Descriptor library.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24
25 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
26 different MIPS ELF from other targets. This matters when linking.
27 This file supports both, switching at runtime. */
28
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libbfd.h"
32 #include "bfdlink.h"
33 #include "genlink.h"
34 #include "elf-bfd.h"
35 #include "elf/mips.h"
36
37 /* Get the ECOFF swapping routines. */
38 #include "coff/sym.h"
39 #include "coff/symconst.h"
40 #include "coff/internal.h"
41 #include "coff/ecoff.h"
42 #include "coff/mips.h"
43 #define ECOFF_32
44 #include "ecoffswap.h"
45
46 /* This structure is used to hold .got information when linking. It
47 is stored in the tdata field of the bfd_elf_section_data structure. */
48
49 struct mips_got_info
50 {
51 /* The global symbol in the GOT with the lowest index in the dynamic
52 symbol table. */
53 struct elf_link_hash_entry *global_gotsym;
54 /* The number of global .got entries. */
55 unsigned int global_gotno;
56 /* The number of local .got entries. */
57 unsigned int local_gotno;
58 /* The number of local .got entries we have used. */
59 unsigned int assigned_gotno;
60 };
61
62 /* The MIPS ELF linker needs additional information for each symbol in
63 the global hash table. */
64
65 struct mips_elf_link_hash_entry
66 {
67 struct elf_link_hash_entry root;
68
69 /* External symbol information. */
70 EXTR esym;
71
72 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
73 this symbol. */
74 unsigned int possibly_dynamic_relocs;
75
76 /* The index of the first dynamic relocation (in the .rel.dyn
77 section) against this symbol. */
78 unsigned int min_dyn_reloc_index;
79
80 /* If there is a stub that 32 bit functions should use to call this
81 16 bit function, this points to the section containing the stub. */
82 asection *fn_stub;
83
84 /* Whether we need the fn_stub; this is set if this symbol appears
85 in any relocs other than a 16 bit call. */
86 boolean need_fn_stub;
87
88 /* If there is a stub that 16 bit functions should use to call this
89 32 bit function, this points to the section containing the stub. */
90 asection *call_stub;
91
92 /* This is like the call_stub field, but it is used if the function
93 being called returns a floating point value. */
94 asection *call_fp_stub;
95 };
96
97 static bfd_reloc_status_type mips32_64bit_reloc
98 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
99 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
100 PARAMS ((bfd *, bfd_reloc_code_real_type));
101 static void mips_info_to_howto_rel
102 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
103 static void mips_info_to_howto_rela
104 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
105 static void bfd_mips_elf32_swap_gptab_in
106 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
107 static void bfd_mips_elf32_swap_gptab_out
108 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
109 static void bfd_mips_elf_swap_msym_in
110 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
111 static void bfd_mips_elf_swap_msym_out
112 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
113 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
114 static boolean mips_elf_create_procedure_table
115 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
116 struct ecoff_debug_info *));
117 static INLINE int elf_mips_isa PARAMS ((flagword));
118 static INLINE int elf_mips_mach PARAMS ((flagword));
119 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
120 static boolean mips_elf_is_local_label_name
121 PARAMS ((bfd *, const char *));
122 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
123 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
124 static int gptab_compare PARAMS ((const void *, const void *));
125 static void mips_elf_relocate_hi16
126 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
127 bfd_vma));
128 static boolean mips_elf_relocate_got_local
129 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
130 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
131 static void mips_elf_relocate_global_got
132 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
133 static bfd_reloc_status_type mips16_jump_reloc
134 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
135 static bfd_reloc_status_type mips16_gprel_reloc
136 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
137 static boolean mips_elf_create_compact_rel_section
138 PARAMS ((bfd *, struct bfd_link_info *));
139 static boolean mips_elf_create_got_section
140 PARAMS ((bfd *, struct bfd_link_info *));
141 static bfd_reloc_status_type mips_elf_final_gp
142 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
143 static bfd_byte *elf32_mips_get_relocated_section_contents
144 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
145 bfd_byte *, boolean, asymbol **));
146 static asection *mips_elf_create_msym_section
147 PARAMS ((bfd *));
148 static void mips_elf_irix6_finish_dynamic_symbol
149 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
150 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
151 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
152 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
153 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
154 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
155 static bfd_vma mips_elf_global_got_index
156 PARAMS ((bfd *, struct elf_link_hash_entry *));
157 static bfd_vma mips_elf_local_got_index
158 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
159 static bfd_vma mips_elf_got_offset_from_index
160 PARAMS ((bfd *, bfd *, bfd_vma));
161 static boolean mips_elf_record_global_got_symbol
162 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
163 struct mips_got_info *));
164 static bfd_vma mips_elf_got_page
165 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
166 static boolean mips_elf_next_lo16_addend
167 PARAMS ((const Elf_Internal_Rela *, const Elf_Internal_Rela *, bfd_vma *));
168 static bfd_reloc_status_type mips_elf_calculate_relocation
169 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
170 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
171 Elf_Internal_Sym *, asection **, bfd_vma *, const char **));
172 static bfd_vma mips_elf_obtain_contents
173 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
174 static void mips_elf_perform_relocation
175 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
176 const Elf_Internal_Rela *, bfd_vma,
177 bfd *, bfd_byte *));
178 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
179 static boolean mips_elf_sort_hash_table_f
180 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
181 static boolean mips_elf_sort_hash_table
182 PARAMS ((struct bfd_link_info *, unsigned long));
183 static asection * mips_elf_got_section PARAMS ((bfd *));
184 static struct mips_got_info *mips_elf_got_info
185 PARAMS ((bfd *, asection **));
186 static bfd_vma mips_elf_create_local_got_entry
187 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
188 static bfd_vma mips_elf_got16_entry
189 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
190 static unsigned int mips_elf_create_dynamic_relocation
191 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
192 long, bfd_vma, asection *));
193 static void mips_elf_allocate_dynamic_relocations
194 PARAMS ((bfd *, unsigned int));
195
196 /* The level of IRIX compatibility we're striving for. */
197
198 typedef enum {
199 ict_none,
200 ict_irix5,
201 ict_irix6
202 } irix_compat_t;
203
204 /* Nonzero if ABFD is using the N32 ABI. */
205
206 #define ABI_N32_P(abfd) \
207 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
208
209 /* Nonzero if ABFD is using the 64-bit ABI. FIXME: This is never
210 true, yet. */
211 #define ABI_64_P(abfd) \
212 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
213
214 /* What version of Irix we are trying to be compatible with. FIXME:
215 At the moment, we never generate "normal" MIPS ELF ABI executables;
216 we always use some version of Irix. */
217
218 #define IRIX_COMPAT(abfd) \
219 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5)
220
221 /* Whether we are trying to be compatible with IRIX at all. */
222
223 #define SGI_COMPAT(abfd) \
224 (IRIX_COMPAT (abfd) != ict_none)
225
226 /* The name of the msym section. */
227 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
228
229 /* The name of the srdata section. */
230 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
231
232 /* The name of the options section. */
233 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
234 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
235
236 /* The name of the stub section. */
237 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
238 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
239
240 /* The name of the dynamic relocation section. */
241 #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
242
243 /* The size of an external REL relocation. */
244 #define MIPS_ELF_REL_SIZE(abfd) \
245 (get_elf_backend_data (abfd)->s->sizeof_rel)
246
247 /* The size of an external dynamic table entry. */
248 #define MIPS_ELF_DYN_SIZE(abfd) \
249 (get_elf_backend_data (abfd)->s->sizeof_dyn)
250
251 /* The size of a GOT entry. */
252 #define MIPS_ELF_GOT_SIZE(abfd) \
253 (get_elf_backend_data (abfd)->s->arch_size / 8)
254
255 /* The size of a symbol-table entry. */
256 #define MIPS_ELF_SYM_SIZE(abfd) \
257 (get_elf_backend_data (abfd)->s->sizeof_sym)
258
259 /* The default alignment for sections, as a power of two. */
260 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
261 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
262
263 /* Get word-sized data. */
264 #define MIPS_ELF_GET_WORD(abfd, ptr) \
265 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
266
267 /* Put out word-sized data. */
268 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
269 (ABI_64_P (abfd) \
270 ? bfd_put_64 (abfd, val, ptr) \
271 : bfd_put_32 (abfd, val, ptr))
272
273 /* Add a dynamic symbol table-entry. */
274 #ifdef BFD64
275 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
276 (ABI_64_P (elf_hash_table (info)->dynobj) \
277 ? bfd_elf64_add_dynamic_entry (info, tag, val) \
278 : bfd_elf32_add_dynamic_entry (info, tag, val))
279 #else
280 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
281 (ABI_64_P (elf_hash_table (info)->dynobj) \
282 ? (abort (), false) \
283 : bfd_elf32_add_dynamic_entry (info, tag, val))
284 #endif
285
286 /* The number of local .got entries we reserve. */
287 #define MIPS_RESERVED_GOTNO (2)
288
289 /* Instructions which appear in a stub. For some reason the stub is
290 slightly different on an SGI system. */
291 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
292 #define STUB_LW(abfd) \
293 (SGI_COMPAT (abfd) \
294 ? (ABI_64_P (abfd) \
295 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
296 : 0x8f998010) /* lw t9,0x8010(gp) */ \
297 : 0x8f998000) /* lw t9,0x8000(gp) */
298 #define STUB_MOVE 0x03e07825 /* move t7,ra */
299 #define STUB_JALR 0x0320f809 /* jal t9 */
300 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
301 #define MIPS_FUNCTION_STUB_SIZE (16)
302
303 #if 0
304 /* We no longer try to identify particular sections for the .dynsym
305 section. When we do, we wind up crashing if there are other random
306 sections with relocations. */
307
308 /* Names of sections which appear in the .dynsym section in an Irix 5
309 executable. */
310
311 static const char * const mips_elf_dynsym_sec_names[] =
312 {
313 ".text",
314 ".init",
315 ".fini",
316 ".data",
317 ".rodata",
318 ".sdata",
319 ".sbss",
320 ".bss",
321 NULL
322 };
323
324 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
325 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
326
327 /* The number of entries in mips_elf_dynsym_sec_names which go in the
328 text segment. */
329
330 #define MIPS_TEXT_DYNSYM_SECNO (3)
331
332 #endif /* 0 */
333
334 /* The names of the runtime procedure table symbols used on Irix 5. */
335
336 static const char * const mips_elf_dynsym_rtproc_names[] =
337 {
338 "_procedure_table",
339 "_procedure_string_table",
340 "_procedure_table_size",
341 NULL
342 };
343
344 /* These structures are used to generate the .compact_rel section on
345 Irix 5. */
346
347 typedef struct
348 {
349 unsigned long id1; /* Always one? */
350 unsigned long num; /* Number of compact relocation entries. */
351 unsigned long id2; /* Always two? */
352 unsigned long offset; /* The file offset of the first relocation. */
353 unsigned long reserved0; /* Zero? */
354 unsigned long reserved1; /* Zero? */
355 } Elf32_compact_rel;
356
357 typedef struct
358 {
359 bfd_byte id1[4];
360 bfd_byte num[4];
361 bfd_byte id2[4];
362 bfd_byte offset[4];
363 bfd_byte reserved0[4];
364 bfd_byte reserved1[4];
365 } Elf32_External_compact_rel;
366
367 typedef struct
368 {
369 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
370 unsigned int rtype : 4; /* Relocation types. See below. */
371 unsigned int dist2to : 8;
372 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
373 unsigned long konst; /* KONST field. See below. */
374 unsigned long vaddr; /* VADDR to be relocated. */
375 } Elf32_crinfo;
376
377 typedef struct
378 {
379 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
380 unsigned int rtype : 4; /* Relocation types. See below. */
381 unsigned int dist2to : 8;
382 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
383 unsigned long konst; /* KONST field. See below. */
384 } Elf32_crinfo2;
385
386 typedef struct
387 {
388 bfd_byte info[4];
389 bfd_byte konst[4];
390 bfd_byte vaddr[4];
391 } Elf32_External_crinfo;
392
393 typedef struct
394 {
395 bfd_byte info[4];
396 bfd_byte konst[4];
397 } Elf32_External_crinfo2;
398
399 /* These are the constants used to swap the bitfields in a crinfo. */
400
401 #define CRINFO_CTYPE (0x1)
402 #define CRINFO_CTYPE_SH (31)
403 #define CRINFO_RTYPE (0xf)
404 #define CRINFO_RTYPE_SH (27)
405 #define CRINFO_DIST2TO (0xff)
406 #define CRINFO_DIST2TO_SH (19)
407 #define CRINFO_RELVADDR (0x7ffff)
408 #define CRINFO_RELVADDR_SH (0)
409
410 /* A compact relocation info has long (3 words) or short (2 words)
411 formats. A short format doesn't have VADDR field and relvaddr
412 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
413 #define CRF_MIPS_LONG 1
414 #define CRF_MIPS_SHORT 0
415
416 /* There are 4 types of compact relocation at least. The value KONST
417 has different meaning for each type:
418
419 (type) (konst)
420 CT_MIPS_REL32 Address in data
421 CT_MIPS_WORD Address in word (XXX)
422 CT_MIPS_GPHI_LO GP - vaddr
423 CT_MIPS_JMPAD Address to jump
424 */
425
426 #define CRT_MIPS_REL32 0xa
427 #define CRT_MIPS_WORD 0xb
428 #define CRT_MIPS_GPHI_LO 0xc
429 #define CRT_MIPS_JMPAD 0xd
430
431 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
432 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
433 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
434 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
435
436 static void bfd_elf32_swap_compact_rel_out
437 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
438 static void bfd_elf32_swap_crinfo_out
439 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
440
441 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
442
443 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
444 from smaller values. Start with zero, widen, *then* decrement. */
445 #define MINUS_ONE (((bfd_vma)0) - 1)
446
447 static reloc_howto_type elf_mips_howto_table[] =
448 {
449 /* No relocation. */
450 HOWTO (R_MIPS_NONE, /* type */
451 0, /* rightshift */
452 0, /* size (0 = byte, 1 = short, 2 = long) */
453 0, /* bitsize */
454 false, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_dont, /* complain_on_overflow */
457 bfd_elf_generic_reloc, /* special_function */
458 "R_MIPS_NONE", /* name */
459 false, /* partial_inplace */
460 0, /* src_mask */
461 0, /* dst_mask */
462 false), /* pcrel_offset */
463
464 /* 16 bit relocation. */
465 HOWTO (R_MIPS_16, /* type */
466 0, /* rightshift */
467 1, /* size (0 = byte, 1 = short, 2 = long) */
468 16, /* bitsize */
469 false, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_bitfield, /* complain_on_overflow */
472 bfd_elf_generic_reloc, /* special_function */
473 "R_MIPS_16", /* name */
474 true, /* partial_inplace */
475 0xffff, /* src_mask */
476 0xffff, /* dst_mask */
477 false), /* pcrel_offset */
478
479 /* 32 bit relocation. */
480 HOWTO (R_MIPS_32, /* type */
481 0, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 32, /* bitsize */
484 false, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_bitfield, /* complain_on_overflow */
487 bfd_elf_generic_reloc, /* special_function */
488 "R_MIPS_32", /* name */
489 true, /* partial_inplace */
490 0xffffffff, /* src_mask */
491 0xffffffff, /* dst_mask */
492 false), /* pcrel_offset */
493
494 /* 32 bit symbol relative relocation. */
495 HOWTO (R_MIPS_REL32, /* type */
496 0, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 32, /* bitsize */
499 false, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_bitfield, /* complain_on_overflow */
502 bfd_elf_generic_reloc, /* special_function */
503 "R_MIPS_REL32", /* name */
504 true, /* partial_inplace */
505 0xffffffff, /* src_mask */
506 0xffffffff, /* dst_mask */
507 false), /* pcrel_offset */
508
509 /* 26 bit branch address. */
510 HOWTO (R_MIPS_26, /* type */
511 2, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 26, /* bitsize */
514 false, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_dont, /* complain_on_overflow */
517 /* This needs complex overflow
518 detection, because the upper four
519 bits must match the PC. */
520 bfd_elf_generic_reloc, /* special_function */
521 "R_MIPS_26", /* name */
522 true, /* partial_inplace */
523 0x3ffffff, /* src_mask */
524 0x3ffffff, /* dst_mask */
525 false), /* pcrel_offset */
526
527 /* High 16 bits of symbol value. */
528 HOWTO (R_MIPS_HI16, /* type */
529 0, /* rightshift */
530 2, /* size (0 = byte, 1 = short, 2 = long) */
531 16, /* bitsize */
532 false, /* pc_relative */
533 0, /* bitpos */
534 complain_overflow_dont, /* complain_on_overflow */
535 _bfd_mips_elf_hi16_reloc, /* special_function */
536 "R_MIPS_HI16", /* name */
537 true, /* partial_inplace */
538 0xffff, /* src_mask */
539 0xffff, /* dst_mask */
540 false), /* pcrel_offset */
541
542 /* Low 16 bits of symbol value. */
543 HOWTO (R_MIPS_LO16, /* type */
544 0, /* rightshift */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
546 16, /* bitsize */
547 false, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_dont, /* complain_on_overflow */
550 _bfd_mips_elf_lo16_reloc, /* special_function */
551 "R_MIPS_LO16", /* name */
552 true, /* partial_inplace */
553 0xffff, /* src_mask */
554 0xffff, /* dst_mask */
555 false), /* pcrel_offset */
556
557 /* GP relative reference. */
558 HOWTO (R_MIPS_GPREL16, /* type */
559 0, /* rightshift */
560 2, /* size (0 = byte, 1 = short, 2 = long) */
561 16, /* bitsize */
562 false, /* pc_relative */
563 0, /* bitpos */
564 complain_overflow_signed, /* complain_on_overflow */
565 _bfd_mips_elf_gprel16_reloc, /* special_function */
566 "R_MIPS_GPREL16", /* name */
567 true, /* partial_inplace */
568 0xffff, /* src_mask */
569 0xffff, /* dst_mask */
570 false), /* pcrel_offset */
571
572 /* Reference to literal section. */
573 HOWTO (R_MIPS_LITERAL, /* type */
574 0, /* rightshift */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
576 16, /* bitsize */
577 false, /* pc_relative */
578 0, /* bitpos */
579 complain_overflow_signed, /* complain_on_overflow */
580 _bfd_mips_elf_gprel16_reloc, /* special_function */
581 "R_MIPS_LITERAL", /* name */
582 true, /* partial_inplace */
583 0xffff, /* src_mask */
584 0xffff, /* dst_mask */
585 false), /* pcrel_offset */
586
587 /* Reference to global offset table. */
588 HOWTO (R_MIPS_GOT16, /* type */
589 0, /* rightshift */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
591 16, /* bitsize */
592 false, /* pc_relative */
593 0, /* bitpos */
594 complain_overflow_signed, /* complain_on_overflow */
595 _bfd_mips_elf_got16_reloc, /* special_function */
596 "R_MIPS_GOT16", /* name */
597 false, /* partial_inplace */
598 0, /* src_mask */
599 0xffff, /* dst_mask */
600 false), /* pcrel_offset */
601
602 /* 16 bit PC relative reference. */
603 HOWTO (R_MIPS_PC16, /* type */
604 0, /* rightshift */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
606 16, /* bitsize */
607 true, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_signed, /* complain_on_overflow */
610 bfd_elf_generic_reloc, /* special_function */
611 "R_MIPS_PC16", /* name */
612 true, /* partial_inplace */
613 0xffff, /* src_mask */
614 0xffff, /* dst_mask */
615 false), /* pcrel_offset */
616
617 /* 16 bit call through global offset table. */
618 HOWTO (R_MIPS_CALL16, /* type */
619 0, /* rightshift */
620 2, /* size (0 = byte, 1 = short, 2 = long) */
621 16, /* bitsize */
622 false, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_signed, /* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_MIPS_CALL16", /* name */
627 false, /* partial_inplace */
628 0, /* src_mask */
629 0xffff, /* dst_mask */
630 false), /* pcrel_offset */
631
632 /* 32 bit GP relative reference. */
633 HOWTO (R_MIPS_GPREL32, /* type */
634 0, /* rightshift */
635 2, /* size (0 = byte, 1 = short, 2 = long) */
636 32, /* bitsize */
637 false, /* pc_relative */
638 0, /* bitpos */
639 complain_overflow_bitfield, /* complain_on_overflow */
640 _bfd_mips_elf_gprel32_reloc, /* special_function */
641 "R_MIPS_GPREL32", /* name */
642 true, /* partial_inplace */
643 0xffffffff, /* src_mask */
644 0xffffffff, /* dst_mask */
645 false), /* pcrel_offset */
646
647 /* The remaining relocs are defined on Irix 5, although they are
648 not defined by the ABI. */
649 EMPTY_HOWTO (13),
650 EMPTY_HOWTO (14),
651 EMPTY_HOWTO (15),
652
653 /* A 5 bit shift field. */
654 HOWTO (R_MIPS_SHIFT5, /* type */
655 0, /* rightshift */
656 2, /* size (0 = byte, 1 = short, 2 = long) */
657 5, /* bitsize */
658 false, /* pc_relative */
659 6, /* bitpos */
660 complain_overflow_bitfield, /* complain_on_overflow */
661 bfd_elf_generic_reloc, /* special_function */
662 "R_MIPS_SHIFT5", /* name */
663 true, /* partial_inplace */
664 0x000007c0, /* src_mask */
665 0x000007c0, /* dst_mask */
666 false), /* pcrel_offset */
667
668 /* A 6 bit shift field. */
669 /* FIXME: This is not handled correctly; a special function is
670 needed to put the most significant bit in the right place. */
671 HOWTO (R_MIPS_SHIFT6, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 6, /* bitsize */
675 false, /* pc_relative */
676 6, /* bitpos */
677 complain_overflow_bitfield, /* complain_on_overflow */
678 bfd_elf_generic_reloc, /* special_function */
679 "R_MIPS_SHIFT6", /* name */
680 true, /* partial_inplace */
681 0x000007c4, /* src_mask */
682 0x000007c4, /* dst_mask */
683 false), /* pcrel_offset */
684
685 /* A 64 bit relocation. */
686 HOWTO (R_MIPS_64, /* type */
687 0, /* rightshift */
688 4, /* size (0 = byte, 1 = short, 2 = long) */
689 64, /* bitsize */
690 false, /* pc_relative */
691 0, /* bitpos */
692 complain_overflow_bitfield, /* complain_on_overflow */
693 mips32_64bit_reloc, /* special_function */
694 "R_MIPS_64", /* name */
695 true, /* partial_inplace */
696 MINUS_ONE, /* src_mask */
697 MINUS_ONE, /* dst_mask */
698 false), /* pcrel_offset */
699
700 /* Displacement in the global offset table. */
701 HOWTO (R_MIPS_GOT_DISP, /* type */
702 0, /* rightshift */
703 2, /* size (0 = byte, 1 = short, 2 = long) */
704 16, /* bitsize */
705 false, /* pc_relative */
706 0, /* bitpos */
707 complain_overflow_bitfield, /* complain_on_overflow */
708 bfd_elf_generic_reloc, /* special_function */
709 "R_MIPS_GOT_DISP", /* name */
710 true, /* partial_inplace */
711 0x0000ffff, /* src_mask */
712 0x0000ffff, /* dst_mask */
713 false), /* pcrel_offset */
714
715 /* Displacement to page pointer in the global offset table. */
716 HOWTO (R_MIPS_GOT_PAGE, /* type */
717 0, /* rightshift */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
720 false, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_bitfield, /* complain_on_overflow */
723 bfd_elf_generic_reloc, /* special_function */
724 "R_MIPS_GOT_PAGE", /* name */
725 true, /* partial_inplace */
726 0x0000ffff, /* src_mask */
727 0x0000ffff, /* dst_mask */
728 false), /* pcrel_offset */
729
730 /* Offset from page pointer in the global offset table. */
731 HOWTO (R_MIPS_GOT_OFST, /* type */
732 0, /* rightshift */
733 2, /* size (0 = byte, 1 = short, 2 = long) */
734 16, /* bitsize */
735 false, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_bitfield, /* complain_on_overflow */
738 bfd_elf_generic_reloc, /* special_function */
739 "R_MIPS_GOT_OFST", /* name */
740 true, /* partial_inplace */
741 0x0000ffff, /* src_mask */
742 0x0000ffff, /* dst_mask */
743 false), /* pcrel_offset */
744
745 /* High 16 bits of displacement in global offset table. */
746 HOWTO (R_MIPS_GOT_HI16, /* type */
747 0, /* rightshift */
748 2, /* size (0 = byte, 1 = short, 2 = long) */
749 16, /* bitsize */
750 false, /* pc_relative */
751 0, /* bitpos */
752 complain_overflow_dont, /* complain_on_overflow */
753 bfd_elf_generic_reloc, /* special_function */
754 "R_MIPS_GOT_HI16", /* name */
755 true, /* partial_inplace */
756 0x0000ffff, /* src_mask */
757 0x0000ffff, /* dst_mask */
758 false), /* pcrel_offset */
759
760 /* Low 16 bits of displacement in global offset table. */
761 HOWTO (R_MIPS_GOT_LO16, /* type */
762 0, /* rightshift */
763 2, /* size (0 = byte, 1 = short, 2 = long) */
764 16, /* bitsize */
765 false, /* pc_relative */
766 0, /* bitpos */
767 complain_overflow_dont, /* complain_on_overflow */
768 bfd_elf_generic_reloc, /* special_function */
769 "R_MIPS_GOT_LO16", /* name */
770 true, /* partial_inplace */
771 0x0000ffff, /* src_mask */
772 0x0000ffff, /* dst_mask */
773 false), /* pcrel_offset */
774
775 /* 64 bit subtraction. Used in the N32 ABI. */
776 HOWTO (R_MIPS_SUB, /* type */
777 0, /* rightshift */
778 4, /* size (0 = byte, 1 = short, 2 = long) */
779 64, /* bitsize */
780 false, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_bitfield, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_MIPS_SUB", /* name */
785 true, /* partial_inplace */
786 MINUS_ONE, /* src_mask */
787 MINUS_ONE, /* dst_mask */
788 false), /* pcrel_offset */
789
790 /* Used to cause the linker to insert and delete instructions? */
791 EMPTY_HOWTO (R_MIPS_INSERT_A),
792 EMPTY_HOWTO (R_MIPS_INSERT_B),
793 EMPTY_HOWTO (R_MIPS_DELETE),
794
795 /* Get the higher value of a 64 bit addend. */
796 HOWTO (R_MIPS_HIGHER, /* type */
797 0, /* rightshift */
798 2, /* size (0 = byte, 1 = short, 2 = long) */
799 16, /* bitsize */
800 false, /* pc_relative */
801 0, /* bitpos */
802 complain_overflow_dont, /* complain_on_overflow */
803 bfd_elf_generic_reloc, /* special_function */
804 "R_MIPS_HIGHER", /* name */
805 true, /* partial_inplace */
806 0, /* src_mask */
807 0xffff, /* dst_mask */
808 false), /* pcrel_offset */
809
810 /* Get the highest value of a 64 bit addend. */
811 HOWTO (R_MIPS_HIGHEST, /* type */
812 0, /* rightshift */
813 2, /* size (0 = byte, 1 = short, 2 = long) */
814 16, /* bitsize */
815 false, /* pc_relative */
816 0, /* bitpos */
817 complain_overflow_dont, /* complain_on_overflow */
818 bfd_elf_generic_reloc, /* special_function */
819 "R_MIPS_HIGHEST", /* name */
820 true, /* partial_inplace */
821 0, /* src_mask */
822 0xffff, /* dst_mask */
823 false), /* pcrel_offset */
824
825 /* High 16 bits of displacement in global offset table. */
826 HOWTO (R_MIPS_CALL_HI16, /* type */
827 0, /* rightshift */
828 2, /* size (0 = byte, 1 = short, 2 = long) */
829 16, /* bitsize */
830 false, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_dont, /* complain_on_overflow */
833 bfd_elf_generic_reloc, /* special_function */
834 "R_MIPS_CALL_HI16", /* name */
835 true, /* partial_inplace */
836 0x0000ffff, /* src_mask */
837 0x0000ffff, /* dst_mask */
838 false), /* pcrel_offset */
839
840 /* Low 16 bits of displacement in global offset table. */
841 HOWTO (R_MIPS_CALL_LO16, /* type */
842 0, /* rightshift */
843 2, /* size (0 = byte, 1 = short, 2 = long) */
844 16, /* bitsize */
845 false, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_dont, /* complain_on_overflow */
848 bfd_elf_generic_reloc, /* special_function */
849 "R_MIPS_CALL_LO16", /* name */
850 true, /* partial_inplace */
851 0x0000ffff, /* src_mask */
852 0x0000ffff, /* dst_mask */
853 false), /* pcrel_offset */
854
855 /* Section displacement. */
856 HOWTO (R_MIPS_SCN_DISP, /* type */
857 0, /* rightshift */
858 2, /* size (0 = byte, 1 = short, 2 = long) */
859 32, /* bitsize */
860 false, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_dont, /* complain_on_overflow */
863 bfd_elf_generic_reloc, /* special_function */
864 "R_MIPS_SCN_DISP", /* name */
865 false, /* partial_inplace */
866 0xffffffff, /* src_mask */
867 0xffffffff, /* dst_mask */
868 false), /* pcrel_offset */
869
870 EMPTY_HOWTO (R_MIPS_REL16),
871 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
872 EMPTY_HOWTO (R_MIPS_PJUMP),
873 EMPTY_HOWTO (R_MIPS_RELGOT),
874
875 /* Protected jump conversion. This is an optimization hint. No
876 relocation is required for correctness. */
877 HOWTO (R_MIPS_JALR, /* type */
878 0, /* rightshift */
879 0, /* size (0 = byte, 1 = short, 2 = long) */
880 0, /* bitsize */
881 false, /* pc_relative */
882 0, /* bitpos */
883 complain_overflow_dont, /* complain_on_overflow */
884 bfd_elf_generic_reloc, /* special_function */
885 "R_MIPS_JALR", /* name */
886 false, /* partial_inplace */
887 0x00000000, /* src_mask */
888 0x00000000, /* dst_mask */
889 false), /* pcrel_offset */
890 };
891
892 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
893 is a hack to make the linker think that we need 64 bit values. */
894 static reloc_howto_type elf_mips_ctor64_howto =
895 HOWTO (R_MIPS_64, /* type */
896 0, /* rightshift */
897 4, /* size (0 = byte, 1 = short, 2 = long) */
898 32, /* bitsize */
899 false, /* pc_relative */
900 0, /* bitpos */
901 complain_overflow_signed, /* complain_on_overflow */
902 mips32_64bit_reloc, /* special_function */
903 "R_MIPS_64", /* name */
904 true, /* partial_inplace */
905 0xffffffff, /* src_mask */
906 0xffffffff, /* dst_mask */
907 false); /* pcrel_offset */
908
909 /* The reloc used for the mips16 jump instruction. */
910 static reloc_howto_type elf_mips16_jump_howto =
911 HOWTO (R_MIPS16_26, /* type */
912 2, /* rightshift */
913 2, /* size (0 = byte, 1 = short, 2 = long) */
914 26, /* bitsize */
915 false, /* pc_relative */
916 0, /* bitpos */
917 complain_overflow_dont, /* complain_on_overflow */
918 /* This needs complex overflow
919 detection, because the upper four
920 bits must match the PC. */
921 mips16_jump_reloc, /* special_function */
922 "R_MIPS16_26", /* name */
923 true, /* partial_inplace */
924 0x3ffffff, /* src_mask */
925 0x3ffffff, /* dst_mask */
926 false); /* pcrel_offset */
927
928 /* The reloc used for the mips16 gprel instruction. */
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 0x07ff001f, /* src_mask */
941 0x07ff001f, /* 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 /* We'd carefully arranged the dynamic symbol indices, and then the
4323 generic size_dynamic_sections renumbered them out from under us.
4324 Rather than trying somehow to prevent the renumbering, just do
4325 the sort again. */
4326
4327 if (elf_hash_table (info)->dynobj)
4328 {
4329 bfd *dynobj;
4330 asection *got;
4331 struct mips_got_info *g;
4332
4333 if (!mips_elf_sort_hash_table (info, bfd_count_sections (abfd) + 1))
4334 return false;
4335
4336 /* Make sure we didn't grow the global .got region. */
4337 dynobj = elf_hash_table (info)->dynobj;
4338 got = bfd_get_section_by_name (dynobj, ".got");
4339 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4340
4341 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
4342 - g->global_gotsym->dynindx)
4343 <= g->global_gotno);
4344 }
4345
4346 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4347 include it, even though we don't process it quite right. (Some
4348 entries are supposed to be merged.) Empirically, we seem to be
4349 better off including it then not. */
4350 if (IRIX_COMPAT (abfd) == ict_irix5)
4351 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4352 {
4353 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4354 {
4355 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4356 if (p->type == bfd_indirect_link_order)
4357 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4358 (*secpp)->link_order_head = NULL;
4359 *secpp = (*secpp)->next;
4360 --abfd->section_count;
4361
4362 break;
4363 }
4364 }
4365
4366 /* Get a value for the GP register. */
4367 if (elf_gp (abfd) == 0)
4368 {
4369 struct bfd_link_hash_entry *h;
4370
4371 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4372 if (h != (struct bfd_link_hash_entry *) NULL
4373 && h->type == bfd_link_hash_defined)
4374 elf_gp (abfd) = (h->u.def.value
4375 + h->u.def.section->output_section->vma
4376 + h->u.def.section->output_offset);
4377 else if (info->relocateable)
4378 {
4379 bfd_vma lo;
4380
4381 /* Find the GP-relative section with the lowest offset. */
4382 lo = (bfd_vma) -1;
4383 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4384 if (o->vma < lo
4385 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4386 lo = o->vma;
4387
4388 /* And calculate GP relative to that. */
4389 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4390 }
4391 else
4392 {
4393 /* If the relocate_section function needs to do a reloc
4394 involving the GP value, it should make a reloc_dangerous
4395 callback to warn that GP is not defined. */
4396 }
4397 }
4398
4399 /* Go through the sections and collect the .reginfo and .mdebug
4400 information. */
4401 reginfo_sec = NULL;
4402 mdebug_sec = NULL;
4403 gptab_data_sec = NULL;
4404 gptab_bss_sec = NULL;
4405 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4406 {
4407 if (strcmp (o->name, ".reginfo") == 0)
4408 {
4409 memset (&reginfo, 0, sizeof reginfo);
4410
4411 /* We have found the .reginfo section in the output file.
4412 Look through all the link_orders comprising it and merge
4413 the information together. */
4414 for (p = o->link_order_head;
4415 p != (struct bfd_link_order *) NULL;
4416 p = p->next)
4417 {
4418 asection *input_section;
4419 bfd *input_bfd;
4420 Elf32_External_RegInfo ext;
4421 Elf32_RegInfo sub;
4422
4423 if (p->type != bfd_indirect_link_order)
4424 {
4425 if (p->type == bfd_fill_link_order)
4426 continue;
4427 abort ();
4428 }
4429
4430 input_section = p->u.indirect.section;
4431 input_bfd = input_section->owner;
4432
4433 /* The linker emulation code has probably clobbered the
4434 size to be zero bytes. */
4435 if (input_section->_raw_size == 0)
4436 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4437
4438 if (! bfd_get_section_contents (input_bfd, input_section,
4439 (PTR) &ext,
4440 (file_ptr) 0,
4441 sizeof ext))
4442 return false;
4443
4444 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4445
4446 reginfo.ri_gprmask |= sub.ri_gprmask;
4447 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4448 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4449 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4450 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4451
4452 /* ri_gp_value is set by the function
4453 mips_elf32_section_processing when the section is
4454 finally written out. */
4455
4456 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4457 elf_link_input_bfd ignores this section. */
4458 input_section->flags &=~ SEC_HAS_CONTENTS;
4459 }
4460
4461 /* Size has been set in mips_elf_always_size_sections */
4462 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4463
4464 /* Skip this section later on (I don't think this currently
4465 matters, but someday it might). */
4466 o->link_order_head = (struct bfd_link_order *) NULL;
4467
4468 reginfo_sec = o;
4469 }
4470
4471 if (strcmp (o->name, ".mdebug") == 0)
4472 {
4473 struct extsym_info einfo;
4474
4475 /* We have found the .mdebug section in the output file.
4476 Look through all the link_orders comprising it and merge
4477 the information together. */
4478 symhdr->magic = swap->sym_magic;
4479 /* FIXME: What should the version stamp be? */
4480 symhdr->vstamp = 0;
4481 symhdr->ilineMax = 0;
4482 symhdr->cbLine = 0;
4483 symhdr->idnMax = 0;
4484 symhdr->ipdMax = 0;
4485 symhdr->isymMax = 0;
4486 symhdr->ioptMax = 0;
4487 symhdr->iauxMax = 0;
4488 symhdr->issMax = 0;
4489 symhdr->issExtMax = 0;
4490 symhdr->ifdMax = 0;
4491 symhdr->crfd = 0;
4492 symhdr->iextMax = 0;
4493
4494 /* We accumulate the debugging information itself in the
4495 debug_info structure. */
4496 debug.line = NULL;
4497 debug.external_dnr = NULL;
4498 debug.external_pdr = NULL;
4499 debug.external_sym = NULL;
4500 debug.external_opt = NULL;
4501 debug.external_aux = NULL;
4502 debug.ss = NULL;
4503 debug.ssext = debug.ssext_end = NULL;
4504 debug.external_fdr = NULL;
4505 debug.external_rfd = NULL;
4506 debug.external_ext = debug.external_ext_end = NULL;
4507
4508 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4509 if (mdebug_handle == (PTR) NULL)
4510 return false;
4511
4512 if (SGI_COMPAT (abfd))
4513 {
4514 asection *s;
4515 EXTR esym;
4516 bfd_vma last;
4517 unsigned int i;
4518 static const char * const name[] =
4519 { ".text", ".init", ".fini", ".data",
4520 ".rodata", ".sdata", ".sbss", ".bss" };
4521 static const int sc[] = { scText, scInit, scFini, scData,
4522 scRData, scSData, scSBss, scBss };
4523
4524 esym.jmptbl = 0;
4525 esym.cobol_main = 0;
4526 esym.weakext = 0;
4527 esym.reserved = 0;
4528 esym.ifd = ifdNil;
4529 esym.asym.iss = issNil;
4530 esym.asym.st = stLocal;
4531 esym.asym.reserved = 0;
4532 esym.asym.index = indexNil;
4533 last = 0;
4534 for (i = 0; i < 8; i++)
4535 {
4536 esym.asym.sc = sc[i];
4537 s = bfd_get_section_by_name (abfd, name[i]);
4538 if (s != NULL)
4539 {
4540 esym.asym.value = s->vma;
4541 last = s->vma + s->_raw_size;
4542 }
4543 else
4544 esym.asym.value = last;
4545
4546 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4547 name[i], &esym))
4548 return false;
4549 }
4550 }
4551
4552 for (p = o->link_order_head;
4553 p != (struct bfd_link_order *) NULL;
4554 p = p->next)
4555 {
4556 asection *input_section;
4557 bfd *input_bfd;
4558 const struct ecoff_debug_swap *input_swap;
4559 struct ecoff_debug_info input_debug;
4560 char *eraw_src;
4561 char *eraw_end;
4562
4563 if (p->type != bfd_indirect_link_order)
4564 {
4565 if (p->type == bfd_fill_link_order)
4566 continue;
4567 abort ();
4568 }
4569
4570 input_section = p->u.indirect.section;
4571 input_bfd = input_section->owner;
4572
4573 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4574 || (get_elf_backend_data (input_bfd)
4575 ->elf_backend_ecoff_debug_swap) == NULL)
4576 {
4577 /* I don't know what a non MIPS ELF bfd would be
4578 doing with a .mdebug section, but I don't really
4579 want to deal with it. */
4580 continue;
4581 }
4582
4583 input_swap = (get_elf_backend_data (input_bfd)
4584 ->elf_backend_ecoff_debug_swap);
4585
4586 BFD_ASSERT (p->size == input_section->_raw_size);
4587
4588 /* The ECOFF linking code expects that we have already
4589 read in the debugging information and set up an
4590 ecoff_debug_info structure, so we do that now. */
4591 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4592 &input_debug))
4593 return false;
4594
4595 if (! (bfd_ecoff_debug_accumulate
4596 (mdebug_handle, abfd, &debug, swap, input_bfd,
4597 &input_debug, input_swap, info)))
4598 return false;
4599
4600 /* Loop through the external symbols. For each one with
4601 interesting information, try to find the symbol in
4602 the linker global hash table and save the information
4603 for the output external symbols. */
4604 eraw_src = input_debug.external_ext;
4605 eraw_end = (eraw_src
4606 + (input_debug.symbolic_header.iextMax
4607 * input_swap->external_ext_size));
4608 for (;
4609 eraw_src < eraw_end;
4610 eraw_src += input_swap->external_ext_size)
4611 {
4612 EXTR ext;
4613 const char *name;
4614 struct mips_elf_link_hash_entry *h;
4615
4616 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4617 if (ext.asym.sc == scNil
4618 || ext.asym.sc == scUndefined
4619 || ext.asym.sc == scSUndefined)
4620 continue;
4621
4622 name = input_debug.ssext + ext.asym.iss;
4623 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4624 name, false, false, true);
4625 if (h == NULL || h->esym.ifd != -2)
4626 continue;
4627
4628 if (ext.ifd != -1)
4629 {
4630 BFD_ASSERT (ext.ifd
4631 < input_debug.symbolic_header.ifdMax);
4632 ext.ifd = input_debug.ifdmap[ext.ifd];
4633 }
4634
4635 h->esym = ext;
4636 }
4637
4638 /* Free up the information we just read. */
4639 free (input_debug.line);
4640 free (input_debug.external_dnr);
4641 free (input_debug.external_pdr);
4642 free (input_debug.external_sym);
4643 free (input_debug.external_opt);
4644 free (input_debug.external_aux);
4645 free (input_debug.ss);
4646 free (input_debug.ssext);
4647 free (input_debug.external_fdr);
4648 free (input_debug.external_rfd);
4649 free (input_debug.external_ext);
4650
4651 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4652 elf_link_input_bfd ignores this section. */
4653 input_section->flags &=~ SEC_HAS_CONTENTS;
4654 }
4655
4656 if (SGI_COMPAT (abfd) && info->shared)
4657 {
4658 /* Create .rtproc section. */
4659 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4660 if (rtproc_sec == NULL)
4661 {
4662 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4663 | SEC_LINKER_CREATED | SEC_READONLY);
4664
4665 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4666 if (rtproc_sec == NULL
4667 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4668 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4669 return false;
4670 }
4671
4672 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4673 info, rtproc_sec, &debug))
4674 return false;
4675 }
4676
4677 /* Build the external symbol information. */
4678 einfo.abfd = abfd;
4679 einfo.info = info;
4680 einfo.debug = &debug;
4681 einfo.swap = swap;
4682 einfo.failed = false;
4683 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4684 mips_elf_output_extsym,
4685 (PTR) &einfo);
4686 if (einfo.failed)
4687 return false;
4688
4689 /* Set the size of the .mdebug section. */
4690 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4691
4692 /* Skip this section later on (I don't think this currently
4693 matters, but someday it might). */
4694 o->link_order_head = (struct bfd_link_order *) NULL;
4695
4696 mdebug_sec = o;
4697 }
4698
4699 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4700 {
4701 const char *subname;
4702 unsigned int c;
4703 Elf32_gptab *tab;
4704 Elf32_External_gptab *ext_tab;
4705 unsigned int i;
4706
4707 /* The .gptab.sdata and .gptab.sbss sections hold
4708 information describing how the small data area would
4709 change depending upon the -G switch. These sections
4710 not used in executables files. */
4711 if (! info->relocateable)
4712 {
4713 asection **secpp;
4714
4715 for (p = o->link_order_head;
4716 p != (struct bfd_link_order *) NULL;
4717 p = p->next)
4718 {
4719 asection *input_section;
4720
4721 if (p->type != bfd_indirect_link_order)
4722 {
4723 if (p->type == bfd_fill_link_order)
4724 continue;
4725 abort ();
4726 }
4727
4728 input_section = p->u.indirect.section;
4729
4730 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4731 elf_link_input_bfd ignores this section. */
4732 input_section->flags &=~ SEC_HAS_CONTENTS;
4733 }
4734
4735 /* Skip this section later on (I don't think this
4736 currently matters, but someday it might). */
4737 o->link_order_head = (struct bfd_link_order *) NULL;
4738
4739 /* Really remove the section. */
4740 for (secpp = &abfd->sections;
4741 *secpp != o;
4742 secpp = &(*secpp)->next)
4743 ;
4744 *secpp = (*secpp)->next;
4745 --abfd->section_count;
4746
4747 continue;
4748 }
4749
4750 /* There is one gptab for initialized data, and one for
4751 uninitialized data. */
4752 if (strcmp (o->name, ".gptab.sdata") == 0)
4753 gptab_data_sec = o;
4754 else if (strcmp (o->name, ".gptab.sbss") == 0)
4755 gptab_bss_sec = o;
4756 else
4757 {
4758 (*_bfd_error_handler)
4759 (_("%s: illegal section name `%s'"),
4760 bfd_get_filename (abfd), o->name);
4761 bfd_set_error (bfd_error_nonrepresentable_section);
4762 return false;
4763 }
4764
4765 /* The linker script always combines .gptab.data and
4766 .gptab.sdata into .gptab.sdata, and likewise for
4767 .gptab.bss and .gptab.sbss. It is possible that there is
4768 no .sdata or .sbss section in the output file, in which
4769 case we must change the name of the output section. */
4770 subname = o->name + sizeof ".gptab" - 1;
4771 if (bfd_get_section_by_name (abfd, subname) == NULL)
4772 {
4773 if (o == gptab_data_sec)
4774 o->name = ".gptab.data";
4775 else
4776 o->name = ".gptab.bss";
4777 subname = o->name + sizeof ".gptab" - 1;
4778 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4779 }
4780
4781 /* Set up the first entry. */
4782 c = 1;
4783 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4784 if (tab == NULL)
4785 return false;
4786 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4787 tab[0].gt_header.gt_unused = 0;
4788
4789 /* Combine the input sections. */
4790 for (p = o->link_order_head;
4791 p != (struct bfd_link_order *) NULL;
4792 p = p->next)
4793 {
4794 asection *input_section;
4795 bfd *input_bfd;
4796 bfd_size_type size;
4797 unsigned long last;
4798 bfd_size_type gpentry;
4799
4800 if (p->type != bfd_indirect_link_order)
4801 {
4802 if (p->type == bfd_fill_link_order)
4803 continue;
4804 abort ();
4805 }
4806
4807 input_section = p->u.indirect.section;
4808 input_bfd = input_section->owner;
4809
4810 /* Combine the gptab entries for this input section one
4811 by one. We know that the input gptab entries are
4812 sorted by ascending -G value. */
4813 size = bfd_section_size (input_bfd, input_section);
4814 last = 0;
4815 for (gpentry = sizeof (Elf32_External_gptab);
4816 gpentry < size;
4817 gpentry += sizeof (Elf32_External_gptab))
4818 {
4819 Elf32_External_gptab ext_gptab;
4820 Elf32_gptab int_gptab;
4821 unsigned long val;
4822 unsigned long add;
4823 boolean exact;
4824 unsigned int look;
4825
4826 if (! (bfd_get_section_contents
4827 (input_bfd, input_section, (PTR) &ext_gptab,
4828 gpentry, sizeof (Elf32_External_gptab))))
4829 {
4830 free (tab);
4831 return false;
4832 }
4833
4834 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4835 &int_gptab);
4836 val = int_gptab.gt_entry.gt_g_value;
4837 add = int_gptab.gt_entry.gt_bytes - last;
4838
4839 exact = false;
4840 for (look = 1; look < c; look++)
4841 {
4842 if (tab[look].gt_entry.gt_g_value >= val)
4843 tab[look].gt_entry.gt_bytes += add;
4844
4845 if (tab[look].gt_entry.gt_g_value == val)
4846 exact = true;
4847 }
4848
4849 if (! exact)
4850 {
4851 Elf32_gptab *new_tab;
4852 unsigned int max;
4853
4854 /* We need a new table entry. */
4855 new_tab = ((Elf32_gptab *)
4856 bfd_realloc ((PTR) tab,
4857 (c + 1) * sizeof (Elf32_gptab)));
4858 if (new_tab == NULL)
4859 {
4860 free (tab);
4861 return false;
4862 }
4863 tab = new_tab;
4864 tab[c].gt_entry.gt_g_value = val;
4865 tab[c].gt_entry.gt_bytes = add;
4866
4867 /* Merge in the size for the next smallest -G
4868 value, since that will be implied by this new
4869 value. */
4870 max = 0;
4871 for (look = 1; look < c; look++)
4872 {
4873 if (tab[look].gt_entry.gt_g_value < val
4874 && (max == 0
4875 || (tab[look].gt_entry.gt_g_value
4876 > tab[max].gt_entry.gt_g_value)))
4877 max = look;
4878 }
4879 if (max != 0)
4880 tab[c].gt_entry.gt_bytes +=
4881 tab[max].gt_entry.gt_bytes;
4882
4883 ++c;
4884 }
4885
4886 last = int_gptab.gt_entry.gt_bytes;
4887 }
4888
4889 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4890 elf_link_input_bfd ignores this section. */
4891 input_section->flags &=~ SEC_HAS_CONTENTS;
4892 }
4893
4894 /* The table must be sorted by -G value. */
4895 if (c > 2)
4896 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4897
4898 /* Swap out the table. */
4899 ext_tab = ((Elf32_External_gptab *)
4900 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4901 if (ext_tab == NULL)
4902 {
4903 free (tab);
4904 return false;
4905 }
4906
4907 for (i = 0; i < c; i++)
4908 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4909 free (tab);
4910
4911 o->_raw_size = c * sizeof (Elf32_External_gptab);
4912 o->contents = (bfd_byte *) ext_tab;
4913
4914 /* Skip this section later on (I don't think this currently
4915 matters, but someday it might). */
4916 o->link_order_head = (struct bfd_link_order *) NULL;
4917 }
4918 }
4919
4920 /* Invoke the regular ELF backend linker to do all the work. */
4921 if (ABI_64_P (abfd))
4922 {
4923 #ifdef BFD64
4924 if (!bfd_elf64_bfd_final_link (abfd, info))
4925 return false;
4926 #else
4927 abort ();
4928 return false;
4929 #endif /* BFD64 */
4930 }
4931 else if (!bfd_elf32_bfd_final_link (abfd, info))
4932 return false;
4933
4934 /* Now write out the computed sections. */
4935
4936 if (reginfo_sec != (asection *) NULL)
4937 {
4938 Elf32_External_RegInfo ext;
4939
4940 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4941 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4942 (file_ptr) 0, sizeof ext))
4943 return false;
4944 }
4945
4946 if (mdebug_sec != (asection *) NULL)
4947 {
4948 BFD_ASSERT (abfd->output_has_begun);
4949 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4950 swap, info,
4951 mdebug_sec->filepos))
4952 return false;
4953
4954 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4955 }
4956
4957 if (gptab_data_sec != (asection *) NULL)
4958 {
4959 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4960 gptab_data_sec->contents,
4961 (file_ptr) 0,
4962 gptab_data_sec->_raw_size))
4963 return false;
4964 }
4965
4966 if (gptab_bss_sec != (asection *) NULL)
4967 {
4968 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4969 gptab_bss_sec->contents,
4970 (file_ptr) 0,
4971 gptab_bss_sec->_raw_size))
4972 return false;
4973 }
4974
4975 if (SGI_COMPAT (abfd))
4976 {
4977 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4978 if (rtproc_sec != NULL)
4979 {
4980 if (! bfd_set_section_contents (abfd, rtproc_sec,
4981 rtproc_sec->contents,
4982 (file_ptr) 0,
4983 rtproc_sec->_raw_size))
4984 return false;
4985 }
4986 }
4987
4988 return true;
4989 }
4990
4991 /* Handle a MIPS ELF HI16 reloc. */
4992
4993 static void
4994 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4995 bfd *input_bfd;
4996 Elf_Internal_Rela *relhi;
4997 Elf_Internal_Rela *rello;
4998 bfd_byte *contents;
4999 bfd_vma addend;
5000 {
5001 bfd_vma insn;
5002 bfd_vma addlo;
5003
5004 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5005
5006 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5007 addlo &= 0xffff;
5008
5009 addend += ((insn & 0xffff) << 16) + addlo;
5010
5011 if ((addlo & 0x8000) != 0)
5012 addend -= 0x10000;
5013 if ((addend & 0x8000) != 0)
5014 addend += 0x10000;
5015
5016 bfd_put_32 (input_bfd,
5017 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
5018 contents + relhi->r_offset);
5019 }
5020
5021 /* Handle a MIPS ELF local GOT16 reloc. */
5022
5023 static boolean
5024 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
5025 contents, addend)
5026 bfd *output_bfd;
5027 bfd *input_bfd;
5028 asection *sgot;
5029 Elf_Internal_Rela *relhi;
5030 Elf_Internal_Rela *rello;
5031 bfd_byte *contents;
5032 bfd_vma addend;
5033 {
5034 unsigned int assigned_gotno;
5035 unsigned int i;
5036 bfd_vma insn;
5037 bfd_vma addlo;
5038 bfd_vma address;
5039 bfd_vma hipage;
5040 bfd_byte *got_contents;
5041 struct mips_got_info *g;
5042
5043 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5044
5045 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5046 addlo &= 0xffff;
5047
5048 addend += ((insn & 0xffff) << 16) + addlo;
5049
5050 if ((addlo & 0x8000) != 0)
5051 addend -= 0x10000;
5052 if ((addend & 0x8000) != 0)
5053 addend += 0x10000;
5054
5055 /* Get a got entry representing requested hipage. */
5056 BFD_ASSERT (elf_section_data (sgot) != NULL);
5057 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5058 BFD_ASSERT (g != NULL);
5059
5060 assigned_gotno = g->assigned_gotno;
5061 got_contents = sgot->contents;
5062 hipage = addend & 0xffff0000;
5063
5064 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
5065 {
5066 address = bfd_get_32 (input_bfd, got_contents + i * 4);
5067 if (hipage == (address & 0xffff0000))
5068 break;
5069 }
5070
5071 if (i == assigned_gotno)
5072 {
5073 if (assigned_gotno >= g->local_gotno)
5074 {
5075 (*_bfd_error_handler)
5076 (_("more got entries are needed for hipage relocations"));
5077 bfd_set_error (bfd_error_bad_value);
5078 return false;
5079 }
5080
5081 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
5082 ++g->assigned_gotno;
5083 }
5084
5085 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
5086 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
5087 contents + relhi->r_offset);
5088
5089 return true;
5090 }
5091
5092 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
5093
5094 static void
5095 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
5096 bfd *input_bfd;
5097 Elf_Internal_Rela *rel;
5098 bfd_byte *contents;
5099 bfd_vma offset;
5100 {
5101 bfd_vma insn;
5102
5103 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5104 bfd_put_32 (input_bfd,
5105 (insn & 0xffff0000) | (offset & 0xffff),
5106 contents + rel->r_offset);
5107 }
5108
5109 /* Returns the GOT section for ABFD. */
5110
5111 static asection *
5112 mips_elf_got_section (abfd)
5113 bfd *abfd;
5114 {
5115 return bfd_get_section_by_name (abfd, ".got");
5116 }
5117
5118 /* Returns the GOT information associated with the link indicated by
5119 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5120 section. */
5121
5122 static struct mips_got_info *
5123 mips_elf_got_info (abfd, sgotp)
5124 bfd *abfd;
5125 asection **sgotp;
5126 {
5127 asection *sgot;
5128 struct mips_got_info *g;
5129
5130 sgot = mips_elf_got_section (abfd);
5131 BFD_ASSERT (sgot != NULL);
5132 BFD_ASSERT (elf_section_data (sgot) != NULL);
5133 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5134 BFD_ASSERT (g != NULL);
5135
5136 if (sgotp)
5137 *sgotp = sgot;
5138 return g;
5139 }
5140
5141 /* Sign-extend VALUE, which has the indicated number of BITS. */
5142
5143 static bfd_vma
5144 mips_elf_sign_extend (value, bits)
5145 bfd_vma value;
5146 int bits;
5147 {
5148 if (value & (1 << (bits - 1)))
5149 /* VALUE is negative. */
5150 value |= ((bfd_vma) - 1) << bits;
5151
5152 return value;
5153 }
5154
5155 /* Return non-zero if the indicated VALUE has overflowed the maximum
5156 range expressable by a signed number with the indicated number of
5157 BITS. */
5158
5159 static boolean
5160 mips_elf_overflow_p (value, bits)
5161 bfd_vma value;
5162 int bits;
5163 {
5164 bfd_signed_vma svalue = (bfd_signed_vma) value;
5165
5166 if (svalue > (1 << (bits - 1)) - 1)
5167 /* The value is too big. */
5168 return true;
5169 else if (svalue < -(1 << (bits - 1)))
5170 /* The value is too small. */
5171 return true;
5172
5173 /* All is well. */
5174 return false;
5175 }
5176
5177 /* Calculate the %high function. */
5178
5179 static bfd_vma
5180 mips_elf_high (value)
5181 bfd_vma value;
5182 {
5183 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5184 }
5185
5186 /* Calculate the %higher function. */
5187
5188 static bfd_vma
5189 mips_elf_higher (value)
5190 bfd_vma value ATTRIBUTE_UNUSED;
5191 {
5192 #ifdef BFD64
5193 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5194 #else
5195 abort ();
5196 return (bfd_vma) -1;
5197 #endif
5198 }
5199
5200 /* Calculate the %highest function. */
5201
5202 static bfd_vma
5203 mips_elf_highest (value)
5204 bfd_vma value ATTRIBUTE_UNUSED;
5205 {
5206 #ifdef BFD64
5207 return ((value + (bfd_vma) 0x800080008000) > 48) & 0xffff;
5208 #else
5209 abort ();
5210 return (bfd_vma) -1;
5211 #endif
5212 }
5213
5214 /* Returns the GOT index for the global symbol indicated by H. */
5215
5216 static bfd_vma
5217 mips_elf_global_got_index (abfd, h)
5218 bfd *abfd;
5219 struct elf_link_hash_entry *h;
5220 {
5221 bfd_vma index;
5222 asection *sgot;
5223 struct mips_got_info *g;
5224
5225 g = mips_elf_got_info (abfd, &sgot);
5226
5227 /* Once we determine the global GOT entry with the lowest dynamic
5228 symbol table index, we must put all dynamic symbols with greater
5229 indices into the GOT. That makes it easy to calculate the GOT
5230 offset. */
5231 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5232 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5233 * MIPS_ELF_GOT_SIZE (abfd));
5234 BFD_ASSERT (index < sgot->_raw_size);
5235
5236 return index;
5237 }
5238
5239 /* Returns the offset for the entry at the INDEXth position
5240 in the GOT. */
5241
5242 static bfd_vma
5243 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5244 bfd *dynobj;
5245 bfd *output_bfd;
5246 bfd_vma index;
5247 {
5248 asection *sgot;
5249 bfd_vma gp;
5250
5251 sgot = mips_elf_got_section (dynobj);
5252 gp = _bfd_get_gp_value (output_bfd);
5253 return (sgot->output_section->vma + sgot->output_offset + index -
5254 gp);
5255 }
5256
5257 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5258 symbol table index lower than any we've seen to date, record it for
5259 posterity. */
5260
5261 static boolean
5262 mips_elf_record_global_got_symbol (h, info, g)
5263 struct elf_link_hash_entry *h;
5264 struct bfd_link_info *info;
5265 struct mips_got_info *g ATTRIBUTE_UNUSED;
5266 {
5267 /* A global symbol in the GOT must also be in the dynamic symbol
5268 table. */
5269 if (h->dynindx == -1
5270 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5271 return false;
5272
5273 /* If we've already marked this entry as need GOT space, we don't
5274 need to do it again. */
5275 if (h->got.offset != (bfd_vma) - 1)
5276 return true;
5277
5278 /* By setting this to a value other than -1, we are indicating that
5279 there needs to be a GOT entry for H. */
5280 h->got.offset = 0;
5281
5282 return true;
5283 }
5284
5285 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5286 the dynamic symbols. */
5287
5288 struct mips_elf_hash_sort_data
5289 {
5290 /* The symbol in the global GOT with the lowest dynamic symbol table
5291 index. */
5292 struct elf_link_hash_entry *low;
5293 /* The least dynamic symbol table index corresponding to a symbol
5294 with a GOT entry. */
5295 long min_got_dynindx;
5296 /* The greatest dynamic symbol table index not corresponding to a
5297 symbol without a GOT entry. */
5298 long max_non_got_dynindx;
5299 };
5300
5301 /* If H needs a GOT entry, assign it the highest available dynamic
5302 index. Otherwise, assign it the lowest available dynamic
5303 index. */
5304
5305 static boolean
5306 mips_elf_sort_hash_table_f (h, data)
5307 struct mips_elf_link_hash_entry *h;
5308 PTR data;
5309 {
5310 struct mips_elf_hash_sort_data *hsd
5311 = (struct mips_elf_hash_sort_data *) data;
5312
5313 /* Symbols without dynamic symbol table entries aren't interesting
5314 at all. */
5315 if (h->root.dynindx == -1)
5316 return true;
5317
5318 if (h->root.got.offset != 0)
5319 h->root.dynindx = hsd->max_non_got_dynindx++;
5320 else
5321 {
5322 h->root.dynindx = --hsd->min_got_dynindx;
5323 hsd->low = (struct elf_link_hash_entry *) h;
5324 }
5325
5326 return true;
5327 }
5328
5329 /* Sort the dynamic symbol table so that symbols that need GOT entries
5330 appear towards the end. This reduces the amount of GOT space
5331 required. MAX_LOCAL is used to set the number of local symbols
5332 known to be in the dynamic symbol table. During
5333 mips_elf_size_dynamic_sections, this value is 1. Afterward, the
5334 section symbols are added and the count is higher. */
5335
5336 static boolean
5337 mips_elf_sort_hash_table (info, max_local)
5338 struct bfd_link_info *info;
5339 unsigned long max_local;
5340 {
5341 struct mips_elf_hash_sort_data hsd;
5342 struct mips_got_info *g;
5343 bfd *dynobj;
5344
5345 dynobj = elf_hash_table (info)->dynobj;
5346
5347 hsd.low = NULL;
5348 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5349 hsd.max_non_got_dynindx = max_local;
5350 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5351 elf_hash_table (info)),
5352 mips_elf_sort_hash_table_f,
5353 &hsd);
5354
5355 /* There shoud have been enough room in the symbol table to
5356 accomodate both the GOT and non-GOT symbols. */
5357 BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5358
5359 /* Now we know which dynamic symbol has the lowest dynamic symbol
5360 table index in the GOT. */
5361 g = mips_elf_got_info (dynobj, NULL);
5362 g->global_gotsym = hsd.low;
5363
5364 return true;
5365 }
5366
5367 /* Create a local GOT entry for VALUE. Return the index of the entry,
5368 or -1 if it could not be created. */
5369
5370 static bfd_vma
5371 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5372 bfd *abfd;
5373 struct mips_got_info *g;
5374 asection *sgot;
5375 bfd_vma value;
5376 {
5377 if (g->assigned_gotno >= g->local_gotno)
5378 {
5379 /* We didn't allocate enough space in the GOT. */
5380 (*_bfd_error_handler)
5381 (_("not enough GOT space for local GOT entries"));
5382 bfd_set_error (bfd_error_bad_value);
5383 return (bfd_vma) -1;
5384 }
5385
5386 MIPS_ELF_PUT_WORD (abfd, value,
5387 (sgot->contents
5388 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5389 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5390 }
5391
5392 /* Returns the GOT offset at which the indicated address can be found.
5393 If there is not yet a GOT entry for this value, create one. Returns
5394 -1 if no satisfactory GOT offset can be found. */
5395
5396 static bfd_vma
5397 mips_elf_local_got_index (abfd, info, value)
5398 bfd *abfd;
5399 struct bfd_link_info *info;
5400 bfd_vma value;
5401 {
5402 asection *sgot;
5403 struct mips_got_info *g;
5404 bfd_byte *entry;
5405
5406 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5407
5408 /* Look to see if we already have an appropriate entry. */
5409 for (entry = (sgot->contents
5410 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5411 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5412 entry += MIPS_ELF_GOT_SIZE (abfd))
5413 {
5414 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5415 if (address == value)
5416 return entry - sgot->contents;
5417 }
5418
5419 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5420 }
5421
5422 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5423 are supposed to be placed at small offsets in the GOT, i.e.,
5424 within 32KB of GP. Return the index into the GOT for this page,
5425 and store the offset from this entry to the desired address in
5426 OFFSETP, if it is non-NULL. */
5427
5428 static bfd_vma
5429 mips_elf_got_page (abfd, info, value, offsetp)
5430 bfd *abfd;
5431 struct bfd_link_info *info;
5432 bfd_vma value;
5433 bfd_vma *offsetp;
5434 {
5435 asection *sgot;
5436 struct mips_got_info *g;
5437 bfd_byte *entry;
5438 bfd_byte *last_entry;
5439 bfd_vma index;
5440 bfd_vma address;
5441
5442 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5443
5444 /* Look to see if we aleady have an appropriate entry. */
5445 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5446 for (entry = (sgot->contents
5447 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5448 entry != last_entry;
5449 entry += MIPS_ELF_GOT_SIZE (abfd))
5450 {
5451 address = MIPS_ELF_GET_WORD (abfd, entry);
5452
5453 if (!mips_elf_overflow_p (value - address, 16))
5454 {
5455 /* This entry will serve as the page pointer. We can add a
5456 16-bit number to it to get the actual address. */
5457 index = entry - sgot->contents;
5458 break;
5459 }
5460 }
5461
5462 /* If we didn't have an appropriate entry, we create one now. */
5463 if (entry == last_entry)
5464 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5465
5466 if (offsetp)
5467 {
5468 address = MIPS_ELF_GET_WORD (abfd, entry);
5469 *offsetp = value - address;
5470 }
5471
5472 return index;
5473 }
5474
5475 /* Find a GOT entry whose higher-order 16 bits are the same as those
5476 for value. Return the index into the GOT for this entry. */
5477
5478 static bfd_vma
5479 mips_elf_got16_entry (abfd, info, value)
5480 bfd *abfd;
5481 struct bfd_link_info *info;
5482 bfd_vma value;
5483 {
5484 asection *sgot;
5485 struct mips_got_info *g;
5486 bfd_byte *entry;
5487 bfd_byte *last_entry;
5488 bfd_vma index;
5489 bfd_vma address;
5490
5491 value &= 0xffff0000;
5492 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5493
5494 /* Look to see if we already have an appropriate entry. */
5495 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5496 for (entry = (sgot->contents
5497 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5498 entry != last_entry;
5499 entry += MIPS_ELF_GOT_SIZE (abfd))
5500 {
5501 address = MIPS_ELF_GET_WORD (abfd, entry);
5502 if ((address & 0xffff0000) == value)
5503 {
5504 /* This entry has the right high-order 16 bits. */
5505 index = MIPS_ELF_GOT_SIZE (abfd) * (entry - sgot->contents);
5506 break;
5507 }
5508 }
5509
5510 /* If we didn't have an appropriate entry, we create one now. */
5511 if (entry == last_entry)
5512 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5513
5514 return index;
5515 }
5516
5517 /* Sets *ADDENDP to the addend for the first R_MIPS_LO16 relocation
5518 found, beginning with RELOCATION. RELEND is one-past-the-end of
5519 the relocation table. */
5520
5521 static boolean
5522 mips_elf_next_lo16_addend (relocation, relend, addendp)
5523 const Elf_Internal_Rela *relocation;
5524 const Elf_Internal_Rela *relend;
5525 bfd_vma *addendp;
5526 {
5527 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5528 immediately following. However, for the IRIX6 ABI, the next
5529 relocation may be a composed relocation consisting of several
5530 relocations for the same address. In that case, the R_MIPS_LO16
5531 relo!scation may occur as one of these. We permit a similar
5532 extension in general, as that is useful for GCC. */
5533 while (relocation < relend)
5534 {
5535 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS_LO16)
5536 {
5537 *addendp = relocation->r_addend;
5538 return true;
5539 }
5540
5541 ++relocation;
5542 }
5543
5544 /* We didn't find it. */
5545 return false;
5546 }
5547
5548 /* Create a rel.dyn relocation for the dynamic linker to resolve. The
5549 relocatin is against the symbol with the dynamic symbol table index
5550 DYNINDX. REL is the original relocation, which is now being made
5551 dynamic. */
5552
5553 static unsigned int
5554 mips_elf_create_dynamic_relocation (output_bfd, info, rel, dynindx,
5555 addend, input_section)
5556 bfd *output_bfd;
5557 struct bfd_link_info *info;
5558 const Elf_Internal_Rela *rel;
5559 long dynindx;
5560 bfd_vma addend;
5561 asection *input_section;
5562 {
5563 Elf_Internal_Rel outrel;
5564 boolean skip;
5565 asection *sreloc;
5566 bfd *dynobj;
5567 int r_type;
5568
5569 r_type = ELF32_R_TYPE (rel->r_info);
5570 dynobj = elf_hash_table (info)->dynobj;
5571 sreloc
5572 = bfd_get_section_by_name (dynobj,
5573 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5574 BFD_ASSERT (sreloc != NULL);
5575
5576 skip = false;
5577
5578 /* The symbol for the relocation is the same as it was for the
5579 original relocation. */
5580 outrel.r_info = ELF32_R_INFO (dynindx, R_MIPS_REL32);
5581
5582 /* The offset for the dynamic relocation is the same as for the
5583 original relocation, adjusted by the offset at which the original
5584 section is output. */
5585 if (elf_section_data (input_section)->stab_info == NULL)
5586 outrel.r_offset = rel->r_offset;
5587 else
5588 {
5589 bfd_vma off;
5590
5591 off = (_bfd_stab_section_offset
5592 (output_bfd, &elf_hash_table (info)->stab_info,
5593 input_section,
5594 &elf_section_data (input_section)->stab_info,
5595 rel->r_offset));
5596 if (off == (bfd_vma) -1)
5597 skip = true;
5598 outrel.r_offset = off;
5599 }
5600 outrel.r_offset += (input_section->output_section->vma
5601 + input_section->output_offset);
5602
5603 /* If we've decided to skip this relocation, just output an emtpy
5604 record. */
5605 if (skip)
5606 memset (&outrel, 0, sizeof (outrel));
5607
5608 if (ABI_64_P (output_bfd))
5609 {
5610 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5611 (output_bfd, &outrel,
5612 (sreloc->contents
5613 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5614 }
5615 else
5616 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5617 (((Elf32_External_Rel *)
5618 sreloc->contents)
5619 + sreloc->reloc_count));
5620 ++sreloc->reloc_count;
5621
5622 /* Make sure the output section is writable. The dynamic linker
5623 will be writing to it. */
5624 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5625 |= SHF_WRITE;
5626
5627 /* On IRIX5, make an entry of compact relocation info. */
5628 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5629 {
5630 asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5631 bfd_byte *cr;
5632
5633 if (scpt)
5634 {
5635 Elf32_crinfo cptrel;
5636
5637 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5638 cptrel.vaddr = (rel->r_offset
5639 + input_section->output_section->vma
5640 + input_section->output_offset);
5641 if (r_type == R_MIPS_REL32)
5642 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5643 else
5644 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5645 mips_elf_set_cr_dist2to (cptrel, 0);
5646 cptrel.konst = addend;
5647
5648 cr = (scpt->contents
5649 + sizeof (Elf32_External_compact_rel));
5650 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5651 ((Elf32_External_crinfo *) cr
5652 + scpt->reloc_count));
5653 ++scpt->reloc_count;
5654 }
5655 }
5656
5657 return sreloc->reloc_count - 1;
5658 }
5659
5660 /* Calculate the value produced by the RELOCATION (which comes from
5661 the INPUT_BFD). The ADDEND is the addend to use for this
5662 RELOCATION; RELOCATION->R_ADDEND is ignored.
5663
5664 The result of the relocation calculation is stored in VALUEP.
5665
5666 This function returns bfd_reloc_continue if the caller need take no
5667 further action regarding this relocation, bfd_reloc_notsupported if
5668 something goes dramatically wrong, bfd_reloc_overflow if an
5669 overflow occurs, and bfd_reloc_ok to indicate success. */
5670
5671 static bfd_reloc_status_type
5672 mips_elf_calculate_relocation (abfd,
5673 input_bfd,
5674 input_section,
5675 info,
5676 relocation,
5677 addend,
5678 howto,
5679 local_syms,
5680 local_sections,
5681 valuep,
5682 namep)
5683 bfd *abfd;
5684 bfd *input_bfd;
5685 asection *input_section;
5686 struct bfd_link_info *info;
5687 const Elf_Internal_Rela *relocation;
5688 bfd_vma addend;
5689 reloc_howto_type *howto;
5690 Elf_Internal_Sym *local_syms;
5691 asection **local_sections;
5692 bfd_vma *valuep;
5693 const char **namep;
5694 {
5695 /* The eventual value we will return. */
5696 bfd_vma value;
5697 /* The address of the symbol against which the relocation is
5698 occurring. */
5699 bfd_vma symbol = 0;
5700 /* The final GP value to be used for the relocatable, executable, or
5701 shared object file being produced. */
5702 bfd_vma gp = (bfd_vma) - 1;
5703 /* The place (section offset or address) of the storage unit being
5704 relocated. */
5705 bfd_vma p;
5706 /* The value of GP used to create the relocatable object. */
5707 bfd_vma gp0 = (bfd_vma) - 1;
5708 /* The offset into the global offset table at which the address of
5709 the relocation entry symbol, adjusted by the addend, resides
5710 during execution. */
5711 bfd_vma g = (bfd_vma) - 1;
5712 /* The section in which the symbol referenced by the relocation is
5713 located. */
5714 asection *sec = NULL;
5715 struct mips_elf_link_hash_entry* h = NULL;
5716 /* True if the symbol referred to by this relocation is a local
5717 symbol. */
5718 boolean local_p;
5719 /* True if the symbol referred to by this relocation is "_gp_disp". */
5720 boolean gp_disp_p = false;
5721 Elf_Internal_Shdr *symtab_hdr;
5722 size_t extsymoff;
5723 unsigned long r_symndx;
5724 int r_type;
5725 /* True if overflow occurred during the calculation of the
5726 relocation value. */
5727 boolean overflowed_p;
5728
5729 /* Parse the relocation. */
5730 r_symndx = ELF32_R_SYM (relocation->r_info);
5731 r_type = ELF32_R_TYPE (relocation->r_info);
5732 p = (input_section->output_section->vma
5733 + input_section->output_offset
5734 + relocation->r_offset);
5735
5736 /* Assume that there will be no overflow. */
5737 overflowed_p = false;
5738
5739 /* Figure out whether or not the symbol is local. */
5740 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5741 if (elf_bad_symtab (input_bfd))
5742 {
5743 /* The symbol table does not follow the rule that local symbols
5744 must come before globals. */
5745 extsymoff = 0;
5746 local_p = local_sections[r_symndx] != NULL;
5747 }
5748 else
5749 {
5750 extsymoff = symtab_hdr->sh_info;
5751 local_p = r_symndx < extsymoff;
5752 }
5753
5754 /* Figure out the value of the symbol. */
5755 if (local_p)
5756 {
5757 Elf_Internal_Sym *sym;
5758
5759 sym = local_syms + r_symndx;
5760 sec = local_sections[r_symndx];
5761
5762 symbol = sec->output_section->vma + sec->output_offset;
5763 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5764 symbol += sym->st_value;
5765
5766 /* MIPS16 text labels should be treated as odd. */
5767 if (sym->st_other == STO_MIPS16)
5768 ++symbol;
5769
5770 /* Record the name of this symbol, for our caller. */
5771 *namep = bfd_elf_string_from_elf_section (input_bfd,
5772 symtab_hdr->sh_link,
5773 sym->st_name);
5774 if (*namep == '\0')
5775 *namep = bfd_section_name (input_bfd, sec);
5776 }
5777 else
5778 {
5779 /* For global symbols we look up the symbol in the hash-table. */
5780 h = ((struct mips_elf_link_hash_entry *)
5781 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5782 /* Find the real hash-table entry for this symbol. */
5783 while (h->root.type == bfd_link_hash_indirect
5784 || h->root.type == bfd_link_hash_warning)
5785 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5786
5787 /* Record the name of this symbol, for our caller. */
5788 *namep = h->root.root.root.string;
5789
5790 /* See if this is the special _gp_disp symbol. Note that such a
5791 symbol must always be a global symbol. */
5792 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5793 {
5794 /* Relocations against _gp_disp are permitted only with
5795 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5796 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5797 return bfd_reloc_notsupported;
5798
5799 gp_disp_p = true;
5800 }
5801 /* If this symbol is defined, calculate its address. Note that
5802 _gp_disp is a magic symbol, always implicitly defined by the
5803 linker, so it's inappropriate to check to see whether or not
5804 its defined. */
5805 else if ((h->root.root.type == bfd_link_hash_defined
5806 || h->root.root.type == bfd_link_hash_defweak)
5807 && h->root.root.u.def.section)
5808 {
5809 sec = h->root.root.u.def.section;
5810 if (sec->output_section)
5811 symbol = (h->root.root.u.def.value
5812 + sec->output_section->vma
5813 + sec->output_offset);
5814 else
5815 symbol = h->root.root.u.def.value;
5816 }
5817 else
5818 {
5819 (*info->callbacks->undefined_symbol)
5820 (info, h->root.root.root.string, input_bfd,
5821 input_section, relocation->r_offset);
5822 return bfd_reloc_undefined;
5823 }
5824 }
5825
5826 /* If we haven't already determined the GOT offset, or the GP value,
5827 and we're going to need it, get it now. */
5828 switch (r_type)
5829 {
5830 case R_MIPS_CALL16:
5831 case R_MIPS_GOT_DISP:
5832 case R_MIPS_GOT_HI16:
5833 case R_MIPS_CALL_HI16:
5834 case R_MIPS_GOT_LO16:
5835 case R_MIPS_CALL_LO16:
5836 /* Find the index into the GOT where this value is located. */
5837 if (h)
5838 {
5839 BFD_ASSERT (addend == 0);
5840 g = mips_elf_global_got_index
5841 (elf_hash_table (info)->dynobj,
5842 (struct elf_link_hash_entry*) h);
5843 }
5844 else
5845 {
5846 g = mips_elf_local_got_index (abfd, info, symbol + addend);
5847 if (g == (bfd_vma) -1)
5848 return false;
5849 }
5850
5851 /* Convert GOT indices to actual offsets. */
5852 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5853 abfd, g);
5854 break;
5855
5856 case R_MIPS_HI16:
5857 case R_MIPS_LO16:
5858 case R_MIPS_GPREL16:
5859 case R_MIPS_GPREL32:
5860 gp0 = _bfd_get_gp_value (input_bfd);
5861 gp = _bfd_get_gp_value (abfd);
5862 break;
5863
5864 default:
5865 break;
5866 }
5867
5868 /* Figure out what kind of relocation is being performed. */
5869 switch (r_type)
5870 {
5871 case R_MIPS_NONE:
5872 return bfd_reloc_continue;
5873
5874 case R_MIPS_16:
5875 value = symbol + mips_elf_sign_extend (addend, 16);
5876 overflowed_p = mips_elf_overflow_p (value, 16);
5877 break;
5878
5879 case R_MIPS_32:
5880 case R_MIPS_REL32:
5881 case R_MIPS_64:
5882 /* If we're creating a shared library, or this relocation is
5883 against a symbol in a shared library, then we can't know
5884 where the symbol will end up. So, we create a relocation
5885 record in the output, and leave the job up to the dynamic
5886 linker. */
5887 if (info->shared || !sec->output_section)
5888 {
5889 unsigned int reloc_index;
5890
5891 BFD_ASSERT (h != NULL);
5892 reloc_index
5893 = mips_elf_create_dynamic_relocation (abfd,
5894 info,
5895 relocation,
5896 h->root.dynindx,
5897 addend,
5898 input_section);
5899 if (h->min_dyn_reloc_index == 0
5900 || reloc_index < h->min_dyn_reloc_index)
5901 h->min_dyn_reloc_index = reloc_index;
5902 value = symbol + addend;
5903 }
5904 else
5905 {
5906 if (r_type != R_MIPS_REL32)
5907 value = symbol + addend;
5908 else
5909 value = addend;
5910 }
5911 value &= howto->dst_mask;
5912 break;
5913
5914 case R_MIPS16_26:
5915 /* The calculation for R_MIPS_26 is just the same as for an
5916 R_MIPS_26. It's only the storage of the relocated field into
5917 the output file that's different. That's handled in
5918 mips_elf_perform_relocation. So, we just fall through to the
5919 R_MIPS_26 case here. */
5920 case R_MIPS_26:
5921 if (local_p)
5922 value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
5923 else
5924 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
5925 value &= howto->dst_mask;
5926 break;
5927
5928 case R_MIPS_HI16:
5929 if (!gp_disp_p)
5930 {
5931 value = mips_elf_high (addend + symbol);
5932 value &= howto->dst_mask;
5933 }
5934 else
5935 {
5936 value = mips_elf_high (addend + gp - p);
5937 overflowed_p = mips_elf_overflow_p (value, 16);
5938 }
5939 break;
5940
5941 case R_MIPS_LO16:
5942 if (!gp_disp_p)
5943 value = (symbol + addend) & howto->dst_mask;
5944 else
5945 {
5946 value = addend + gp - p + 4;
5947 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5948 for overflow. But, on, say, Irix 5, relocations against
5949 _gp_disp are normally generated from the .cpload
5950 pseudo-op. It generates code that normally looks like
5951 this:
5952
5953 lui $gp,%hi(_gp_disp)
5954 addiu $gp,$gp,%lo(_gp_disp)
5955 addu $gp,$gp,$t9
5956
5957 Here $t9 holds the address of the function being called,
5958 as required by the MIPS ELF ABI. The R_MIPS_LO16
5959 relocation can easily overflow in this situation, but the
5960 R_MIPS_HI16 relocation will handle the overflow.
5961 Therefore, we consider this a bug in the MIPS ABI, and do
5962 not check for overflow here. */
5963 }
5964 break;
5965
5966 case R_MIPS_LITERAL:
5967 /* Because we don't merge literal sections, we can handle this
5968 just like R_MIPS_GPREL16. In the long run, we should merge
5969 shared literals, and then we will need to additional work
5970 here. */
5971
5972 /* Fall through. */
5973
5974 case R_MIPS16_GPREL:
5975 /* The R_MIPS16_GPREL performs the same calculation as
5976 R_MIPS_GPREL16, but stores the relocated bits in a different
5977 order. We don't need to do anything special here; the
5978 differences are handled in mips_elf_perform_relocation. */
5979 case R_MIPS_GPREL16:
5980 if (local_p)
5981 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
5982 else
5983 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
5984 overflowed_p = mips_elf_overflow_p (value, 16);
5985 break;
5986
5987 case R_MIPS_GOT16:
5988 if (local_p)
5989 {
5990 value = mips_elf_got16_entry (abfd, info, symbol + addend);
5991 if (value == (bfd_vma) -1)
5992 return false;
5993 value
5994 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5995 abfd,
5996 value);
5997 overflowed_p = mips_elf_overflow_p (value, 16);
5998 break;
5999 }
6000
6001 /* Fall through. */
6002
6003 case R_MIPS_CALL16:
6004 case R_MIPS_GOT_DISP:
6005 value = g;
6006 overflowed_p = mips_elf_overflow_p (value, 16);
6007 break;
6008
6009 case R_MIPS_GPREL32:
6010 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6011 break;
6012
6013 case R_MIPS_PC16:
6014 value = mips_elf_sign_extend (addend, 16) + symbol - p;
6015 overflowed_p = mips_elf_overflow_p (value, 16);
6016 break;
6017
6018 case R_MIPS_GOT_HI16:
6019 case R_MIPS_CALL_HI16:
6020 /* We're allowed to handle these two relocations identically.
6021 The dynamic linker is allowed to handle the CALL relocations
6022 differently by creating a lazy evaluation stub. */
6023 value = g;
6024 value = mips_elf_high (value);
6025 value &= howto->dst_mask;
6026 break;
6027
6028 case R_MIPS_GOT_LO16:
6029 case R_MIPS_CALL_LO16:
6030 value = g & howto->dst_mask;
6031 break;
6032
6033 case R_MIPS_GOT_PAGE:
6034 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
6035 if (value == (bfd_vma) -1)
6036 return false;
6037 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6038 abfd,
6039 value);
6040 overflowed_p = mips_elf_overflow_p (value, 16);
6041 break;
6042
6043 case R_MIPS_GOT_OFST:
6044 mips_elf_got_page (abfd, info, symbol + addend, &value);
6045 overflowed_p = mips_elf_overflow_p (value, 16);
6046 break;
6047
6048 case R_MIPS_SUB:
6049 value = symbol - addend;
6050 value &= howto->dst_mask;
6051 break;
6052
6053 case R_MIPS_HIGHER:
6054 value = mips_elf_higher (addend + symbol);
6055 value &= howto->dst_mask;
6056 break;
6057
6058 case R_MIPS_HIGHEST:
6059 value = mips_elf_highest (addend + symbol);
6060 value &= howto->dst_mask;
6061 break;
6062
6063 case R_MIPS_SCN_DISP:
6064 value = symbol + addend - sec->output_offset;
6065 value &= howto->dst_mask;
6066 break;
6067
6068 case R_MIPS_PJUMP:
6069 case R_MIPS_JALR:
6070 /* Both of these may be ignored. R_MIPS_JALR is an optimization
6071 hint; we could improve performance by honoring that hint. */
6072 return bfd_reloc_continue;
6073
6074 case R_MIPS_GNU_VTINHERIT:
6075 case R_MIPS_GNU_VTENTRY:
6076 /* We don't do anything with these at present. */
6077 return bfd_reloc_continue;
6078
6079 default:
6080 /* An unrecognized relocation type. */
6081 return bfd_reloc_notsupported;
6082 }
6083
6084 /* Store the VALUE for our caller. */
6085 *valuep = value;
6086 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6087 }
6088
6089 /* Obtain the field relocated by RELOCATION. */
6090
6091 static bfd_vma
6092 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6093 reloc_howto_type *howto;
6094 const Elf_Internal_Rela *relocation;
6095 bfd *input_bfd;
6096 bfd_byte *contents;
6097 {
6098 bfd_vma x;
6099 bfd_byte *location = contents + relocation->r_offset;
6100
6101 /* Obtain the bytes. */
6102 x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
6103
6104 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
6105 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6106 && bfd_little_endian (input_bfd))
6107 /* The two 16-bit words will be reversed on a little-endian
6108 system. See mips_elf_perform_relocation for more details. */
6109 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6110
6111 return x;
6112 }
6113
6114 /* It has been determined that the result of the RELOCATION is the
6115 VALUE. Use HOWTO to place VALUE into the output file at the
6116 appropriate position. The SECTION is the section to which the
6117 relocatin applies.
6118
6119 Returns false if anything goes wrong. */
6120
6121 static void
6122 mips_elf_perform_relocation (info, howto, relocation, value,
6123 input_bfd, contents)
6124 struct bfd_link_info *info;
6125 reloc_howto_type *howto;
6126 const Elf_Internal_Rela *relocation;
6127 bfd_vma value;
6128 bfd *input_bfd;
6129 bfd_byte *contents;
6130 {
6131 bfd_vma x;
6132 bfd_byte *location;
6133
6134 /* Figure out where the relocation is occurring. */
6135 location = contents + relocation->r_offset;
6136
6137 /* Obtain the current value. */
6138 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6139
6140 /* Clear the field we are setting. */
6141 x &= ~howto->dst_mask;
6142
6143 /* If this is the R_MIPS16_26 relocation, we must store the
6144 value in a funny way. */
6145 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26)
6146 {
6147 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
6148 Most mips16 instructions are 16 bits, but these instructions
6149 are 32 bits.
6150
6151 The format of these instructions is:
6152
6153 +--------------+--------------------------------+
6154 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
6155 +--------------+--------------------------------+
6156 ! Immediate 15:0 !
6157 +-----------------------------------------------+
6158
6159 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
6160 Note that the immediate value in the first word is swapped.
6161
6162 When producing a relocateable object file, R_MIPS16_26 is
6163 handled mostly like R_MIPS_26. In particular, the addend is
6164 stored as a straight 26-bit value in a 32-bit instruction.
6165 (gas makes life simpler for itself by never adjusting a
6166 R_MIPS16_26 reloc to be against a section, so the addend is
6167 always zero). However, the 32 bit instruction is stored as 2
6168 16-bit values, rather than a single 32-bit value. In a
6169 big-endian file, the result is the same; in a little-endian
6170 file, the two 16-bit halves of the 32 bit value are swapped.
6171 This is so that a disassembler can recognize the jal
6172 instruction.
6173
6174 When doing a final link, R_MIPS16_26 is treated as a 32 bit
6175 instruction stored as two 16-bit values. The addend A is the
6176 contents of the targ26 field. The calculation is the same as
6177 R_MIPS_26. When storing the calculated value, reorder the
6178 immediate value as shown above, and don't forget to store the
6179 value as two 16-bit values.
6180
6181 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
6182 defined as
6183
6184 big-endian:
6185 +--------+----------------------+
6186 | | |
6187 | | targ26-16 |
6188 |31 26|25 0|
6189 +--------+----------------------+
6190
6191 little-endian:
6192 +----------+------+-------------+
6193 | | | |
6194 | sub1 | | sub2 |
6195 |0 9|10 15|16 31|
6196 +----------+--------------------+
6197 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
6198 ((sub1 << 16) | sub2)).
6199
6200 When producing a relocateable object file, the calculation is
6201 (((A < 2) | (P & 0xf0000000) + S) >> 2)
6202 When producing a fully linked file, the calculation is
6203 let R = (((A < 2) | (P & 0xf0000000) + S) >> 2)
6204 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
6205
6206 if (!info->relocateable)
6207 /* Shuffle the bits according to the formula above. */
6208 value = (((value & 0x1f0000) << 5)
6209 | ((value & 0x3e00000) >> 5)
6210 | (value & 0xffff));
6211
6212 }
6213 else if (ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6214 {
6215 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
6216 mode. A typical instruction will have a format like this:
6217
6218 +--------------+--------------------------------+
6219 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
6220 +--------------+--------------------------------+
6221 ! Major ! rx ! ry ! Imm 4:0 !
6222 +--------------+--------------------------------+
6223
6224 EXTEND is the five bit value 11110. Major is the instruction
6225 opcode.
6226
6227 This is handled exactly like R_MIPS_GPREL16, except that the
6228 addend is retrieved and stored as shown in this diagram; that
6229 is, the Imm fields above replace the V-rel16 field.
6230
6231 All we need to do here is shuffle the bits appropriately. As
6232 above, the two 16-bit halves must be swapped on a
6233 little-endian system. */
6234 value = (((value & 0x7e0) << 16)
6235 | ((value & 0xf800) << 5)
6236 | (value & 0x1f));
6237 }
6238
6239 /* Set the field. */
6240 x |= (value & howto->dst_mask);
6241
6242 /* Swap the high- and low-order 16 bits on little-endian systems
6243 when doing a MIPS16 relocation. */
6244 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL
6245 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26)
6246 && bfd_little_endian (input_bfd))
6247 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6248
6249 /* Put the value into the output. */
6250 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6251 }
6252
6253 /* Relocate a MIPS ELF section. */
6254
6255 boolean
6256 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6257 contents, relocs, local_syms, local_sections)
6258 bfd *output_bfd;
6259 struct bfd_link_info *info;
6260 bfd *input_bfd;
6261 asection *input_section;
6262 bfd_byte *contents;
6263 Elf_Internal_Rela *relocs;
6264 Elf_Internal_Sym *local_syms;
6265 asection **local_sections;
6266 {
6267 const Elf_Internal_Rela *rel;
6268 const Elf_Internal_Rela *relend;
6269 bfd_vma addend;
6270 bfd_vma last_hi16_addend;
6271 boolean use_saved_addend_p = false;
6272 boolean last_hi16_addend_valid_p = false;
6273 struct elf_backend_data *bed;
6274
6275 bed = get_elf_backend_data (output_bfd);
6276 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6277 for (rel = relocs; rel < relend; ++rel)
6278 {
6279 const char *name;
6280 bfd_vma value;
6281 reloc_howto_type *howto;
6282
6283 /* Find the relocation howto for this relocation. */
6284 if (ELF32_R_TYPE (rel->r_info) == R_MIPS_64
6285 && !ABI_64_P (output_bfd))
6286 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6287 64-bit code, but make sure all their addresses are in the
6288 lowermost or uppermost 32-bit section of the 64-bit address
6289 space. Thus, when they use an R_MIPS_64 they mean what is
6290 usually meant by R_MIPS_32, with the exception that the
6291 stored value is sign-extended to 64 bits. */
6292 howto = elf_mips_howto_table + R_MIPS_32;
6293 else
6294 howto = elf_mips_howto_table + ELF32_R_TYPE (rel->r_info);
6295
6296 if (!use_saved_addend_p)
6297 {
6298 Elf_Internal_Shdr *rel_hdr;
6299
6300 /* If these relocations were originally of the REL variety,
6301 we must pull the addend out of the field that will be
6302 relocated. Otherwise, we simply use the contents of the
6303 RELA relocation. To determine which flavor or relocation
6304 this is, we depend on the fact that the INPUT_SECTION's
6305 REL_HDR is read before its REL_HDR2. */
6306 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6307 if ((size_t) (rel - relocs)
6308 >= (rel_hdr->sh_size / rel_hdr->sh_entsize
6309 * bed->s->int_rels_per_ext_rel))
6310 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6311 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6312 {
6313 int r_type = ELF32_R_TYPE (rel->r_info);
6314
6315 addend = mips_elf_obtain_contents (howto,
6316 rel,
6317 input_bfd,
6318 contents);
6319 addend &= howto->src_mask;
6320
6321 /* For some kinds of relocations, the ADDEND is a
6322 combination of the addend stored in two different
6323 relocations. */
6324 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16)
6325 {
6326 /* Scan ahead to find a matching R_MIPS_LO16
6327 relocation. */
6328 bfd_vma l;
6329
6330 if (!mips_elf_next_lo16_addend (rel, relend, &l))
6331 return false;
6332
6333 /* Save the high-order bit for later. When we
6334 encounter the R_MIPS_LO16 relocation we will need
6335 them again. */
6336 addend <<= 16;
6337 last_hi16_addend = addend;
6338 last_hi16_addend_valid_p = true;
6339
6340 /* Compute the combined addend. */
6341 addend |= l;
6342 }
6343 else if (r_type == R_MIPS_LO16)
6344 {
6345 /* Used the saved HI16 addend. */
6346 if (!last_hi16_addend_valid_p)
6347 return false;
6348 addend |= last_hi16_addend;
6349 }
6350 else if (r_type == R_MIPS16_GPREL)
6351 {
6352 /* The addend is scrambled in the object file. See
6353 mips_elf_perform_relocation for details on the
6354 format. */
6355 addend = (((addend & 0x1f0000) >> 5)
6356 | ((addend & 0x7e00000) >> 16)
6357 | (addend & 0x1f));
6358 }
6359 }
6360 else
6361 addend = rel->r_addend;
6362 }
6363
6364 /* In the N32 and 64-bit ABIs there may be multiple consecutive
6365 relocations for the same offset. In that case we are
6366 supposed to treat the output of each relocation as the addend
6367 for the next. */
6368 if (rel + 1 < relend
6369 && rel->r_offset == rel[1].r_offset
6370 && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
6371 use_saved_addend_p = true;
6372 else
6373 use_saved_addend_p = false;
6374
6375 /* Figure out what value we are supposed to relocate. */
6376 switch (mips_elf_calculate_relocation (output_bfd,
6377 input_bfd,
6378 input_section,
6379 info,
6380 rel,
6381 addend,
6382 howto,
6383 local_syms,
6384 local_sections,
6385 &value,
6386 &name))
6387 {
6388 case bfd_reloc_continue:
6389 /* There's nothing to do. */
6390 continue;
6391
6392 case bfd_reloc_undefined:
6393 return false;
6394
6395 case bfd_reloc_notsupported:
6396 abort ();
6397 break;
6398
6399 case bfd_reloc_overflow:
6400 if (use_saved_addend_p)
6401 /* Ignore overflow until we reach the last relocation for
6402 a given location. */
6403 ;
6404 else if (!name
6405 || ! ((*info->callbacks->reloc_overflow)
6406 (info, name, howto->name, (bfd_vma) 0,
6407 input_bfd, input_section, rel->r_offset)))
6408 return false;
6409
6410 break;
6411
6412 case bfd_reloc_ok:
6413 break;
6414
6415 default:
6416 abort ();
6417 break;
6418 }
6419
6420 /* If we've got another relocation for the address, keep going
6421 until we reach the last one. */
6422 if (use_saved_addend_p)
6423 {
6424 addend = value;
6425 continue;
6426 }
6427
6428 if (ELF32_R_TYPE (rel->r_info) == R_MIPS_64
6429 && !ABI_64_P (output_bfd))
6430 /* See the comment above about using R_MIPS_64 in the 32-bit
6431 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
6432 that calculated the right value. Now, however, we
6433 sign-extend the 32-bit result to 64-bits, and store it as a
6434 64-bit value. We are especially generous here in that we
6435 go to extreme lengths to support this usage on systems with
6436 only a 32-bit VMA. */
6437 {
6438 #ifdef BFD64
6439 /* Just sign-extend the value, and then fall through to the
6440 normal case, using the R_MIPS_64 howto. That will store
6441 the 64-bit value into a 64-bit area. */
6442 value = mips_elf_sign_extend (value, 64);
6443 howto = elf_mips_howto_table + R_MIPS_64;
6444 #else /* !BFD64 */
6445 /* In the 32-bit VMA case, we must handle sign-extension and
6446 endianness manually. */
6447 bfd_vma sign_bits;
6448 bfd_vma low_bits;
6449 bfd_vma high_bits;
6450
6451 if (value & 0x80000000)
6452 sign_bits = 0xffffffff;
6453 else
6454 sign_bits = 0;
6455
6456 /* If only a 32-bit VMA is available do two separate
6457 stores. */
6458 if (bfd_big_endian (input_bfd))
6459 {
6460 /* Store the sign-bits (which are most significant)
6461 first. */
6462 low_bits = sign_bits;
6463 high_bits = value;
6464 }
6465 else
6466 {
6467 low_bits = value;
6468 high_bits = sign_bits;
6469 }
6470 bfd_put_32 (input_bfd, low_bits,
6471 contents + rel->r_offset);
6472 bfd_put_32 (input_bfd, high_bits,
6473 contents + rel->r_offset + 4);
6474 continue;
6475 #endif /* !BFD64 */
6476 }
6477
6478 /* Actually perform the relocation. */
6479 mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
6480 contents);
6481 }
6482
6483 return true;
6484 }
6485
6486 /* This hook function is called before the linker writes out a global
6487 symbol. We mark symbols as small common if appropriate. This is
6488 also where we undo the increment of the value for a mips16 symbol. */
6489
6490 /*ARGSIGNORED*/
6491 boolean
6492 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6493 bfd *abfd ATTRIBUTE_UNUSED;
6494 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6495 const char *name ATTRIBUTE_UNUSED;
6496 Elf_Internal_Sym *sym;
6497 asection *input_sec;
6498 {
6499 /* If we see a common symbol, which implies a relocatable link, then
6500 if a symbol was small common in an input file, mark it as small
6501 common in the output file. */
6502 if (sym->st_shndx == SHN_COMMON
6503 && strcmp (input_sec->name, ".scommon") == 0)
6504 sym->st_shndx = SHN_MIPS_SCOMMON;
6505
6506 if (sym->st_other == STO_MIPS16
6507 && (sym->st_value & 1) != 0)
6508 --sym->st_value;
6509
6510 return true;
6511 }
6512 \f
6513 /* Functions for the dynamic linker. */
6514
6515 /* The name of the dynamic interpreter. This is put in the .interp
6516 section. */
6517
6518 #define ELF_DYNAMIC_INTERPRETER(abfd) \
6519 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
6520 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
6521 : "/usr/lib/libc.so.1")
6522
6523 /* Create dynamic sections when linking against a dynamic object. */
6524
6525 boolean
6526 _bfd_mips_elf_create_dynamic_sections (abfd, info)
6527 bfd *abfd;
6528 struct bfd_link_info *info;
6529 {
6530 struct elf_link_hash_entry *h;
6531 flagword flags;
6532 register asection *s;
6533 const char * const *namep;
6534
6535 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6536 | SEC_LINKER_CREATED | SEC_READONLY);
6537
6538 /* Mips ABI requests the .dynamic section to be read only. */
6539 s = bfd_get_section_by_name (abfd, ".dynamic");
6540 if (s != NULL)
6541 {
6542 if (! bfd_set_section_flags (abfd, s, flags))
6543 return false;
6544 }
6545
6546 /* We need to create .got section. */
6547 if (! mips_elf_create_got_section (abfd, info))
6548 return false;
6549
6550 /* Create the .msym section on IRIX6. It is used by the dynamic
6551 linker to speed up dynamic relocations, and to avoid computing
6552 the ELF hash for symbols. */
6553 if (IRIX_COMPAT (abfd) == ict_irix6
6554 && !mips_elf_create_msym_section (abfd))
6555 return false;
6556
6557 /* Create .stub section. */
6558 if (bfd_get_section_by_name (abfd,
6559 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6560 {
6561 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6562 if (s == NULL
6563 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6564 || ! bfd_set_section_alignment (abfd, s,
6565 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6566 return false;
6567 }
6568
6569 if (IRIX_COMPAT (abfd) == ict_irix5
6570 && !info->shared
6571 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6572 {
6573 s = bfd_make_section (abfd, ".rld_map");
6574 if (s == NULL
6575 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6576 || ! bfd_set_section_alignment (abfd, s,
6577 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6578 return false;
6579 }
6580
6581 /* On IRIX5, we adjust add some additional symbols and change the
6582 alignments of several sections. There is no ABI documentation
6583 indicating that this is necessary on IRIX6, nor any evidence that
6584 the linker takes such action. */
6585 if (IRIX_COMPAT (abfd) == ict_irix5)
6586 {
6587 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6588 {
6589 h = NULL;
6590 if (! (_bfd_generic_link_add_one_symbol
6591 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
6592 (bfd_vma) 0, (const char *) NULL, false,
6593 get_elf_backend_data (abfd)->collect,
6594 (struct bfd_link_hash_entry **) &h)))
6595 return false;
6596 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6597 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6598 h->type = STT_SECTION;
6599
6600 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6601 return false;
6602 }
6603
6604 /* We need to create a .compact_rel section. */
6605 if (! mips_elf_create_compact_rel_section (abfd, info))
6606 return false;
6607
6608 /* Change aligments of some sections. */
6609 s = bfd_get_section_by_name (abfd, ".hash");
6610 if (s != NULL)
6611 bfd_set_section_alignment (abfd, s, 4);
6612 s = bfd_get_section_by_name (abfd, ".dynsym");
6613 if (s != NULL)
6614 bfd_set_section_alignment (abfd, s, 4);
6615 s = bfd_get_section_by_name (abfd, ".dynstr");
6616 if (s != NULL)
6617 bfd_set_section_alignment (abfd, s, 4);
6618 s = bfd_get_section_by_name (abfd, ".reginfo");
6619 if (s != NULL)
6620 bfd_set_section_alignment (abfd, s, 4);
6621 s = bfd_get_section_by_name (abfd, ".dynamic");
6622 if (s != NULL)
6623 bfd_set_section_alignment (abfd, s, 4);
6624 }
6625
6626 if (!info->shared)
6627 {
6628 h = NULL;
6629 if (! (_bfd_generic_link_add_one_symbol
6630 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
6631 (bfd_vma) 0, (const char *) NULL, false,
6632 get_elf_backend_data (abfd)->collect,
6633 (struct bfd_link_hash_entry **) &h)))
6634 return false;
6635 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6636 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6637 h->type = STT_SECTION;
6638
6639 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6640 return false;
6641
6642 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6643 {
6644 /* __rld_map is a four byte word located in the .data section
6645 and is filled in by the rtld to contain a pointer to
6646 the _r_debug structure. Its symbol value will be set in
6647 mips_elf_finish_dynamic_symbol. */
6648 s = bfd_get_section_by_name (abfd, ".rld_map");
6649 BFD_ASSERT (s != NULL);
6650
6651 h = NULL;
6652 if (! (_bfd_generic_link_add_one_symbol
6653 (info, abfd, "__rld_map", BSF_GLOBAL, s,
6654 (bfd_vma) 0, (const char *) NULL, false,
6655 get_elf_backend_data (abfd)->collect,
6656 (struct bfd_link_hash_entry **) &h)))
6657 return false;
6658 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6659 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6660 h->type = STT_OBJECT;
6661
6662 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6663 return false;
6664 }
6665 }
6666
6667 return true;
6668 }
6669
6670 /* Create the .compact_rel section. */
6671
6672 static boolean
6673 mips_elf_create_compact_rel_section (abfd, info)
6674 bfd *abfd;
6675 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6676 {
6677 flagword flags;
6678 register asection *s;
6679
6680 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
6681 {
6682 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
6683 | SEC_READONLY);
6684
6685 s = bfd_make_section (abfd, ".compact_rel");
6686 if (s == NULL
6687 || ! bfd_set_section_flags (abfd, s, flags)
6688 || ! bfd_set_section_alignment (abfd, s,
6689 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6690 return false;
6691
6692 s->_raw_size = sizeof (Elf32_External_compact_rel);
6693 }
6694
6695 return true;
6696 }
6697
6698 /* Create the .got section to hold the global offset table. */
6699
6700 static boolean
6701 mips_elf_create_got_section (abfd, info)
6702 bfd *abfd;
6703 struct bfd_link_info *info;
6704 {
6705 flagword flags;
6706 register asection *s;
6707 struct elf_link_hash_entry *h;
6708 struct mips_got_info *g;
6709
6710 /* This function may be called more than once. */
6711 if (mips_elf_got_section (abfd))
6712 return true;
6713
6714 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6715 | SEC_LINKER_CREATED);
6716
6717 s = bfd_make_section (abfd, ".got");
6718 if (s == NULL
6719 || ! bfd_set_section_flags (abfd, s, flags)
6720 || ! bfd_set_section_alignment (abfd, s, 4))
6721 return false;
6722
6723 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
6724 linker script because we don't want to define the symbol if we
6725 are not creating a global offset table. */
6726 h = NULL;
6727 if (! (_bfd_generic_link_add_one_symbol
6728 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
6729 (bfd_vma) 0, (const char *) NULL, false,
6730 get_elf_backend_data (abfd)->collect,
6731 (struct bfd_link_hash_entry **) &h)))
6732 return false;
6733 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6734 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6735 h->type = STT_OBJECT;
6736
6737 if (info->shared
6738 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
6739 return false;
6740
6741 /* The first several global offset table entries are reserved. */
6742 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
6743
6744 g = (struct mips_got_info *) bfd_alloc (abfd,
6745 sizeof (struct mips_got_info));
6746 if (g == NULL)
6747 return false;
6748 g->global_gotsym = NULL;
6749 g->local_gotno = MIPS_RESERVED_GOTNO;
6750 g->assigned_gotno = MIPS_RESERVED_GOTNO;
6751 if (elf_section_data (s) == NULL)
6752 {
6753 s->used_by_bfd =
6754 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
6755 if (elf_section_data (s) == NULL)
6756 return false;
6757 }
6758 elf_section_data (s)->tdata = (PTR) g;
6759 elf_section_data (s)->this_hdr.sh_flags
6760 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6761
6762 return true;
6763 }
6764
6765 /* Returns the .msym section for ABFD, creating it if it does not
6766 already exist. Returns NULL to indicate error. */
6767
6768 static asection *
6769 mips_elf_create_msym_section (abfd)
6770 bfd *abfd;
6771 {
6772 asection *s;
6773
6774 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6775 if (!s)
6776 {
6777 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6778 if (!s
6779 || !bfd_set_section_flags (abfd, s,
6780 SEC_ALLOC
6781 | SEC_LOAD
6782 | SEC_HAS_CONTENTS
6783 | SEC_LINKER_CREATED
6784 | SEC_READONLY)
6785 || !bfd_set_section_alignment (abfd, s,
6786 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6787 return NULL;
6788 }
6789
6790 return s;
6791 }
6792
6793 /* Add room for N relocations to the .rel.dyn section in ABFD. */
6794
6795 static void
6796 mips_elf_allocate_dynamic_relocations (abfd, n)
6797 bfd *abfd;
6798 unsigned int n;
6799 {
6800 asection *s;
6801
6802 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
6803 BFD_ASSERT (s != NULL);
6804
6805 if (s->_raw_size == 0)
6806 {
6807 /* Make room for a null element. */
6808 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
6809 ++s->reloc_count;
6810 }
6811 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
6812 }
6813
6814 /* Look through the relocs for a section during the first phase, and
6815 allocate space in the global offset table. */
6816
6817 boolean
6818 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
6819 bfd *abfd;
6820 struct bfd_link_info *info;
6821 asection *sec;
6822 const Elf_Internal_Rela *relocs;
6823 {
6824 const char *name;
6825 bfd *dynobj;
6826 Elf_Internal_Shdr *symtab_hdr;
6827 struct elf_link_hash_entry **sym_hashes;
6828 struct mips_got_info *g;
6829 size_t extsymoff;
6830 const Elf_Internal_Rela *rel;
6831 const Elf_Internal_Rela *rel_end;
6832 asection *sgot;
6833 asection *sreloc;
6834 struct elf_backend_data *bed;
6835
6836 if (info->relocateable)
6837 return true;
6838
6839 dynobj = elf_hash_table (info)->dynobj;
6840 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6841 sym_hashes = elf_sym_hashes (abfd);
6842 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6843
6844 /* Check for the mips16 stub sections. */
6845
6846 name = bfd_get_section_name (abfd, sec);
6847 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6848 {
6849 unsigned long r_symndx;
6850
6851 /* Look at the relocation information to figure out which symbol
6852 this is for. */
6853
6854 r_symndx = ELF32_R_SYM (relocs->r_info);
6855
6856 if (r_symndx < extsymoff
6857 || sym_hashes[r_symndx - extsymoff] == NULL)
6858 {
6859 asection *o;
6860
6861 /* This stub is for a local symbol. This stub will only be
6862 needed if there is some relocation in this BFD, other
6863 than a 16 bit function call, which refers to this symbol. */
6864 for (o = abfd->sections; o != NULL; o = o->next)
6865 {
6866 Elf_Internal_Rela *sec_relocs;
6867 const Elf_Internal_Rela *r, *rend;
6868
6869 /* We can ignore stub sections when looking for relocs. */
6870 if ((o->flags & SEC_RELOC) == 0
6871 || o->reloc_count == 0
6872 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6873 sizeof FN_STUB - 1) == 0
6874 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6875 sizeof CALL_STUB - 1) == 0
6876 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6877 sizeof CALL_FP_STUB - 1) == 0)
6878 continue;
6879
6880 sec_relocs = (_bfd_elf32_link_read_relocs
6881 (abfd, o, (PTR) NULL,
6882 (Elf_Internal_Rela *) NULL,
6883 info->keep_memory));
6884 if (sec_relocs == NULL)
6885 return false;
6886
6887 rend = sec_relocs + o->reloc_count;
6888 for (r = sec_relocs; r < rend; r++)
6889 if (ELF32_R_SYM (r->r_info) == r_symndx
6890 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6891 break;
6892
6893 if (! info->keep_memory)
6894 free (sec_relocs);
6895
6896 if (r < rend)
6897 break;
6898 }
6899
6900 if (o == NULL)
6901 {
6902 /* There is no non-call reloc for this stub, so we do
6903 not need it. Since this function is called before
6904 the linker maps input sections to output sections, we
6905 can easily discard it by setting the SEC_EXCLUDE
6906 flag. */
6907 sec->flags |= SEC_EXCLUDE;
6908 return true;
6909 }
6910
6911 /* Record this stub in an array of local symbol stubs for
6912 this BFD. */
6913 if (elf_tdata (abfd)->local_stubs == NULL)
6914 {
6915 unsigned long symcount;
6916 asection **n;
6917
6918 if (elf_bad_symtab (abfd))
6919 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
6920 else
6921 symcount = symtab_hdr->sh_info;
6922 n = (asection **) bfd_zalloc (abfd,
6923 symcount * sizeof (asection *));
6924 if (n == NULL)
6925 return false;
6926 elf_tdata (abfd)->local_stubs = n;
6927 }
6928
6929 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6930
6931 /* We don't need to set mips16_stubs_seen in this case.
6932 That flag is used to see whether we need to look through
6933 the global symbol table for stubs. We don't need to set
6934 it here, because we just have a local stub. */
6935 }
6936 else
6937 {
6938 struct mips_elf_link_hash_entry *h;
6939
6940 h = ((struct mips_elf_link_hash_entry *)
6941 sym_hashes[r_symndx - extsymoff]);
6942
6943 /* H is the symbol this stub is for. */
6944
6945 h->fn_stub = sec;
6946 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6947 }
6948 }
6949 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6950 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6951 {
6952 unsigned long r_symndx;
6953 struct mips_elf_link_hash_entry *h;
6954 asection **loc;
6955
6956 /* Look at the relocation information to figure out which symbol
6957 this is for. */
6958
6959 r_symndx = ELF32_R_SYM (relocs->r_info);
6960
6961 if (r_symndx < extsymoff
6962 || sym_hashes[r_symndx - extsymoff] == NULL)
6963 {
6964 /* This stub was actually built for a static symbol defined
6965 in the same file. We assume that all static symbols in
6966 mips16 code are themselves mips16, so we can simply
6967 discard this stub. Since this function is called before
6968 the linker maps input sections to output sections, we can
6969 easily discard it by setting the SEC_EXCLUDE flag. */
6970 sec->flags |= SEC_EXCLUDE;
6971 return true;
6972 }
6973
6974 h = ((struct mips_elf_link_hash_entry *)
6975 sym_hashes[r_symndx - extsymoff]);
6976
6977 /* H is the symbol this stub is for. */
6978
6979 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6980 loc = &h->call_fp_stub;
6981 else
6982 loc = &h->call_stub;
6983
6984 /* If we already have an appropriate stub for this function, we
6985 don't need another one, so we can discard this one. Since
6986 this function is called before the linker maps input sections
6987 to output sections, we can easily discard it by setting the
6988 SEC_EXCLUDE flag. We can also discard this section if we
6989 happen to already know that this is a mips16 function; it is
6990 not necessary to check this here, as it is checked later, but
6991 it is slightly faster to check now. */
6992 if (*loc != NULL || h->root.other == STO_MIPS16)
6993 {
6994 sec->flags |= SEC_EXCLUDE;
6995 return true;
6996 }
6997
6998 *loc = sec;
6999 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7000 }
7001
7002 if (dynobj == NULL)
7003 {
7004 sgot = NULL;
7005 g = NULL;
7006 }
7007 else
7008 {
7009 sgot = mips_elf_got_section (dynobj);
7010 if (sgot == NULL)
7011 g = NULL;
7012 else
7013 {
7014 BFD_ASSERT (elf_section_data (sgot) != NULL);
7015 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7016 BFD_ASSERT (g != NULL);
7017 }
7018 }
7019
7020 sreloc = NULL;
7021 bed = get_elf_backend_data (abfd);
7022 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7023 for (rel = relocs; rel < rel_end; ++rel)
7024 {
7025 unsigned long r_symndx;
7026 int r_type;
7027 struct elf_link_hash_entry *h;
7028
7029 r_symndx = ELF32_R_SYM (rel->r_info);
7030 r_type = ELF32_R_TYPE (rel->r_info);
7031
7032 if (r_symndx < extsymoff)
7033 h = NULL;
7034 else
7035 {
7036 h = sym_hashes[r_symndx - extsymoff];
7037
7038 /* This may be an indirect symbol created because of a version. */
7039 if (h != NULL)
7040 {
7041 while (h->root.type == bfd_link_hash_indirect)
7042 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7043 }
7044 }
7045
7046 /* Some relocs require a global offset table. */
7047 if (dynobj == NULL || sgot == NULL)
7048 {
7049 switch (r_type)
7050 {
7051 case R_MIPS_GOT16:
7052 case R_MIPS_CALL16:
7053 case R_MIPS_CALL_HI16:
7054 case R_MIPS_CALL_LO16:
7055 case R_MIPS_GOT_HI16:
7056 case R_MIPS_GOT_LO16:
7057 if (dynobj == NULL)
7058 elf_hash_table (info)->dynobj = dynobj = abfd;
7059 if (! mips_elf_create_got_section (dynobj, info))
7060 return false;
7061 g = mips_elf_got_info (dynobj, &sgot);
7062 break;
7063
7064 case R_MIPS_32:
7065 case R_MIPS_REL32:
7066 case R_MIPS_64:
7067 if (dynobj == NULL
7068 && (info->shared || h != NULL)
7069 && (sec->flags & SEC_ALLOC) != 0)
7070 elf_hash_table (info)->dynobj = dynobj = abfd;
7071 break;
7072
7073 default:
7074 break;
7075 }
7076 }
7077
7078 if (!h && (r_type == R_MIPS_CALL_LO16
7079 || r_type == R_MIPS_GOT_LO16
7080 || r_type == R_MIPS_GOT_DISP))
7081 {
7082 /* We may need a local GOT entry for this relocation. We
7083 don't count R_MIPS_HI16 or R_MIPS_GOT16 relocations
7084 because they are always followed by a R_MIPS_LO16
7085 relocation for the value. We don't R_MIPS_GOT_PAGE
7086 because we can estimate the maximum number of pages
7087 needed by looking at the size of the segment.
7088
7089 This estimation is very conservative since we can merge
7090 duplicate entries in the GOT. In order to be less
7091 conservative, we could actually build the GOT here,
7092 rather than in relocate_section. */
7093 g->local_gotno++;
7094 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7095 }
7096
7097 switch (r_type)
7098 {
7099 case R_MIPS_CALL16:
7100 if (h == NULL)
7101 {
7102 (*_bfd_error_handler)
7103 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
7104 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
7105 bfd_set_error (bfd_error_bad_value);
7106 return false;
7107 }
7108 /* Fall through. */
7109
7110 case R_MIPS_CALL_HI16:
7111 case R_MIPS_CALL_LO16:
7112 /* This symbol requires a global offset table entry. */
7113 if (!mips_elf_record_global_got_symbol (h, info, g))
7114 return false;
7115
7116 /* We need a stub, not a plt entry for the undefined
7117 function. But we record it as if it needs plt. See
7118 elf_adjust_dynamic_symbol in elflink.h. */
7119 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
7120 h->type = STT_FUNC;
7121
7122 break;
7123
7124 case R_MIPS_GOT16:
7125 case R_MIPS_GOT_HI16:
7126 case R_MIPS_GOT_LO16:
7127 case R_MIPS_GOT_DISP:
7128 /* This symbol requires a global offset table entry. */
7129 if (h && !mips_elf_record_global_got_symbol (h, info, g))
7130 return false;
7131 break;
7132
7133 case R_MIPS_32:
7134 case R_MIPS_REL32:
7135 case R_MIPS_64:
7136 if ((info->shared || h != NULL)
7137 && (sec->flags & SEC_ALLOC) != 0)
7138 {
7139 if (sreloc == NULL)
7140 {
7141 const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
7142
7143 sreloc = bfd_get_section_by_name (dynobj, name);
7144 if (sreloc == NULL)
7145 {
7146 sreloc = bfd_make_section (dynobj, name);
7147 if (sreloc == NULL
7148 || ! bfd_set_section_flags (dynobj, sreloc,
7149 (SEC_ALLOC
7150 | SEC_LOAD
7151 | SEC_HAS_CONTENTS
7152 | SEC_IN_MEMORY
7153 | SEC_LINKER_CREATED
7154 | SEC_READONLY))
7155 || ! bfd_set_section_alignment (dynobj, sreloc,
7156 4))
7157 return false;
7158 }
7159 }
7160 if (info->shared)
7161 /* When creating a shared object, we must copy these
7162 reloc types into the output file as R_MIPS_REL32
7163 relocs. We make room for this reloc in the
7164 .rel.dyn reloc section. */
7165 mips_elf_allocate_dynamic_relocations (dynobj, 1);
7166 else
7167 {
7168 struct mips_elf_link_hash_entry *hmips;
7169
7170 /* We only need to copy this reloc if the symbol is
7171 defined in a dynamic object. */
7172 hmips = (struct mips_elf_link_hash_entry *) h;
7173 ++hmips->possibly_dynamic_relocs;
7174 }
7175
7176 /* Even though we don't directly need a GOT entry for
7177 this symbol, a symbol must have a dynamic symbol
7178 table index greater that DT_GOTSYM if there are
7179 dynamic relocations against it. */
7180 if (!mips_elf_record_global_got_symbol (h, info, g))
7181 return false;
7182 }
7183
7184 if (SGI_COMPAT (dynobj))
7185 mips_elf_hash_table (info)->compact_rel_size +=
7186 sizeof (Elf32_External_crinfo);
7187 break;
7188
7189 case R_MIPS_26:
7190 case R_MIPS_GPREL16:
7191 case R_MIPS_LITERAL:
7192 case R_MIPS_GPREL32:
7193 if (SGI_COMPAT (dynobj))
7194 mips_elf_hash_table (info)->compact_rel_size +=
7195 sizeof (Elf32_External_crinfo);
7196 break;
7197
7198 /* This relocation describes the C++ object vtable hierarchy.
7199 Reconstruct it for later use during GC. */
7200 case R_MIPS_GNU_VTINHERIT:
7201 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7202 return false;
7203 break;
7204
7205 /* This relocation describes which C++ vtable entries are actually
7206 used. Record for later use during GC. */
7207 case R_MIPS_GNU_VTENTRY:
7208 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7209 return false;
7210 break;
7211
7212 default:
7213 break;
7214 }
7215
7216 /* If this reloc is not a 16 bit call, and it has a global
7217 symbol, then we will need the fn_stub if there is one.
7218 References from a stub section do not count. */
7219 if (h != NULL
7220 && r_type != R_MIPS16_26
7221 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7222 sizeof FN_STUB - 1) != 0
7223 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7224 sizeof CALL_STUB - 1) != 0
7225 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7226 sizeof CALL_FP_STUB - 1) != 0)
7227 {
7228 struct mips_elf_link_hash_entry *mh;
7229
7230 mh = (struct mips_elf_link_hash_entry *) h;
7231 mh->need_fn_stub = true;
7232 }
7233 }
7234
7235 return true;
7236 }
7237
7238 /* Return the section that should be marked against GC for a given
7239 relocation. */
7240
7241 asection *
7242 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7243 bfd *abfd;
7244 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7245 Elf_Internal_Rela *rel;
7246 struct elf_link_hash_entry *h;
7247 Elf_Internal_Sym *sym;
7248 {
7249 /* ??? Do mips16 stub sections need to be handled special? */
7250
7251 if (h != NULL)
7252 {
7253 switch (ELF32_R_TYPE (rel->r_info))
7254 {
7255 case R_MIPS_GNU_VTINHERIT:
7256 case R_MIPS_GNU_VTENTRY:
7257 break;
7258
7259 default:
7260 switch (h->root.type)
7261 {
7262 case bfd_link_hash_defined:
7263 case bfd_link_hash_defweak:
7264 return h->root.u.def.section;
7265
7266 case bfd_link_hash_common:
7267 return h->root.u.c.p->section;
7268
7269 default:
7270 break;
7271 }
7272 }
7273 }
7274 else
7275 {
7276 if (!(elf_bad_symtab (abfd)
7277 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7278 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7279 && sym->st_shndx != SHN_COMMON))
7280 {
7281 return bfd_section_from_elf_index (abfd, sym->st_shndx);
7282 }
7283 }
7284
7285 return NULL;
7286 }
7287
7288 /* Update the got entry reference counts for the section being removed. */
7289
7290 boolean
7291 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7292 bfd *abfd ATTRIBUTE_UNUSED;
7293 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7294 asection *sec ATTRIBUTE_UNUSED;
7295 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
7296 {
7297 #if 0
7298 Elf_Internal_Shdr *symtab_hdr;
7299 struct elf_link_hash_entry **sym_hashes;
7300 bfd_signed_vma *local_got_refcounts;
7301 const Elf_Internal_Rela *rel, *relend;
7302 unsigned long r_symndx;
7303 struct elf_link_hash_entry *h;
7304
7305 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7306 sym_hashes = elf_sym_hashes (abfd);
7307 local_got_refcounts = elf_local_got_refcounts (abfd);
7308
7309 relend = relocs + sec->reloc_count;
7310 for (rel = relocs; rel < relend; rel++)
7311 switch (ELF32_R_TYPE (rel->r_info))
7312 {
7313 case R_MIPS_GOT16:
7314 case R_MIPS_CALL16:
7315 case R_MIPS_CALL_HI16:
7316 case R_MIPS_CALL_LO16:
7317 case R_MIPS_GOT_HI16:
7318 case R_MIPS_GOT_LO16:
7319 /* ??? It would seem that the existing MIPS code does no sort
7320 of reference counting or whatnot on its GOT and PLT entries,
7321 so it is not possible to garbage collect them at this time. */
7322 break;
7323
7324 default:
7325 break;
7326 }
7327 #endif
7328
7329 return true;
7330 }
7331
7332
7333 /* Adjust a symbol defined by a dynamic object and referenced by a
7334 regular object. The current definition is in some section of the
7335 dynamic object, but we're not including those sections. We have to
7336 change the definition to something the rest of the link can
7337 understand. */
7338
7339 boolean
7340 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
7341 struct bfd_link_info *info;
7342 struct elf_link_hash_entry *h;
7343 {
7344 bfd *dynobj;
7345 struct mips_elf_link_hash_entry *hmips;
7346 asection *s;
7347
7348 dynobj = elf_hash_table (info)->dynobj;
7349
7350 /* Make sure we know what is going on here. */
7351 BFD_ASSERT (dynobj != NULL
7352 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7353 || h->weakdef != NULL
7354 || ((h->elf_link_hash_flags
7355 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7356 && (h->elf_link_hash_flags
7357 & ELF_LINK_HASH_REF_REGULAR) != 0
7358 && (h->elf_link_hash_flags
7359 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7360
7361 /* If this symbol is defined in a dynamic object, we need to copy
7362 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7363 file. */
7364 hmips = (struct mips_elf_link_hash_entry *) h;
7365 if (! info->relocateable
7366 && hmips->possibly_dynamic_relocs != 0
7367 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7368 mips_elf_allocate_dynamic_relocations (dynobj,
7369 hmips->possibly_dynamic_relocs);
7370
7371 /* For a function, create a stub, if needed. */
7372 if (h->type == STT_FUNC
7373 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7374 {
7375 if (! elf_hash_table (info)->dynamic_sections_created)
7376 return true;
7377
7378 /* If this symbol is not defined in a regular file, then set
7379 the symbol to the stub location. This is required to make
7380 function pointers compare as equal between the normal
7381 executable and the shared library. */
7382 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7383 {
7384 /* We need .stub section. */
7385 s = bfd_get_section_by_name (dynobj,
7386 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7387 BFD_ASSERT (s != NULL);
7388
7389 h->root.u.def.section = s;
7390 h->root.u.def.value = s->_raw_size;
7391
7392 /* XXX Write this stub address somewhere. */
7393 h->plt.offset = s->_raw_size;
7394
7395 /* Make room for this stub code. */
7396 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7397
7398 /* The last half word of the stub will be filled with the index
7399 of this symbol in .dynsym section. */
7400 return true;
7401 }
7402 }
7403
7404 /* If this is a weak symbol, and there is a real definition, the
7405 processor independent code will have arranged for us to see the
7406 real definition first, and we can just use the same value. */
7407 if (h->weakdef != NULL)
7408 {
7409 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7410 || h->weakdef->root.type == bfd_link_hash_defweak);
7411 h->root.u.def.section = h->weakdef->root.u.def.section;
7412 h->root.u.def.value = h->weakdef->root.u.def.value;
7413 return true;
7414 }
7415
7416 /* This is a reference to a symbol defined by a dynamic object which
7417 is not a function. */
7418
7419 return true;
7420 }
7421
7422 /* This function is called after all the input files have been read,
7423 and the input sections have been assigned to output sections. We
7424 check for any mips16 stub sections that we can discard. */
7425
7426 static boolean mips_elf_check_mips16_stubs
7427 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7428
7429 boolean
7430 _bfd_mips_elf_always_size_sections (output_bfd, info)
7431 bfd *output_bfd;
7432 struct bfd_link_info *info;
7433 {
7434 asection *ri;
7435
7436 /* The .reginfo section has a fixed size. */
7437 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7438 if (ri != NULL)
7439 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7440
7441 if (info->relocateable
7442 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7443 return true;
7444
7445 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7446 mips_elf_check_mips16_stubs,
7447 (PTR) NULL);
7448
7449 return true;
7450 }
7451
7452 /* Check the mips16 stubs for a particular symbol, and see if we can
7453 discard them. */
7454
7455 /*ARGSUSED*/
7456 static boolean
7457 mips_elf_check_mips16_stubs (h, data)
7458 struct mips_elf_link_hash_entry *h;
7459 PTR data ATTRIBUTE_UNUSED;
7460 {
7461 if (h->fn_stub != NULL
7462 && ! h->need_fn_stub)
7463 {
7464 /* We don't need the fn_stub; the only references to this symbol
7465 are 16 bit calls. Clobber the size to 0 to prevent it from
7466 being included in the link. */
7467 h->fn_stub->_raw_size = 0;
7468 h->fn_stub->_cooked_size = 0;
7469 h->fn_stub->flags &= ~ SEC_RELOC;
7470 h->fn_stub->reloc_count = 0;
7471 h->fn_stub->flags |= SEC_EXCLUDE;
7472 }
7473
7474 if (h->call_stub != NULL
7475 && h->root.other == STO_MIPS16)
7476 {
7477 /* We don't need the call_stub; this is a 16 bit function, so
7478 calls from other 16 bit functions are OK. Clobber the size
7479 to 0 to prevent it from being included in the link. */
7480 h->call_stub->_raw_size = 0;
7481 h->call_stub->_cooked_size = 0;
7482 h->call_stub->flags &= ~ SEC_RELOC;
7483 h->call_stub->reloc_count = 0;
7484 h->call_stub->flags |= SEC_EXCLUDE;
7485 }
7486
7487 if (h->call_fp_stub != NULL
7488 && h->root.other == STO_MIPS16)
7489 {
7490 /* We don't need the call_stub; this is a 16 bit function, so
7491 calls from other 16 bit functions are OK. Clobber the size
7492 to 0 to prevent it from being included in the link. */
7493 h->call_fp_stub->_raw_size = 0;
7494 h->call_fp_stub->_cooked_size = 0;
7495 h->call_fp_stub->flags &= ~ SEC_RELOC;
7496 h->call_fp_stub->reloc_count = 0;
7497 h->call_fp_stub->flags |= SEC_EXCLUDE;
7498 }
7499
7500 return true;
7501 }
7502
7503 /* Set the sizes of the dynamic sections. */
7504
7505 boolean
7506 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
7507 bfd *output_bfd;
7508 struct bfd_link_info *info;
7509 {
7510 bfd *dynobj;
7511 asection *s;
7512 boolean reltext;
7513 struct mips_got_info *g;
7514
7515 dynobj = elf_hash_table (info)->dynobj;
7516 BFD_ASSERT (dynobj != NULL);
7517
7518 if (elf_hash_table (info)->dynamic_sections_created)
7519 {
7520 /* Set the contents of the .interp section to the interpreter. */
7521 if (! info->shared)
7522 {
7523 s = bfd_get_section_by_name (dynobj, ".interp");
7524 BFD_ASSERT (s != NULL);
7525 s->_raw_size
7526 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7527 s->contents
7528 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7529 }
7530 }
7531
7532 /* The check_relocs and adjust_dynamic_symbol entry points have
7533 determined the sizes of the various dynamic sections. Allocate
7534 memory for them. */
7535 reltext = false;
7536 for (s = dynobj->sections; s != NULL; s = s->next)
7537 {
7538 const char *name;
7539 boolean strip;
7540
7541 /* It's OK to base decisions on the section name, because none
7542 of the dynobj section names depend upon the input files. */
7543 name = bfd_get_section_name (dynobj, s);
7544
7545 if ((s->flags & SEC_LINKER_CREATED) == 0)
7546 continue;
7547
7548 strip = false;
7549
7550 if (strncmp (name, ".rel", 4) == 0)
7551 {
7552 if (s->_raw_size == 0)
7553 {
7554 /* We only strip the section if the output section name
7555 has the same name. Otherwise, there might be several
7556 input sections for this output section. FIXME: This
7557 code is probably not needed these days anyhow, since
7558 the linker now does not create empty output sections. */
7559 if (s->output_section != NULL
7560 && strcmp (name,
7561 bfd_get_section_name (s->output_section->owner,
7562 s->output_section)) == 0)
7563 strip = true;
7564 }
7565 else
7566 {
7567 const char *outname;
7568 asection *target;
7569
7570 /* If this relocation section applies to a read only
7571 section, then we probably need a DT_TEXTREL entry.
7572 If the relocation section is .rel.dyn, we always
7573 assert a DT_TEXTREL entry rather than testing whether
7574 there exists a relocation to a read only section or
7575 not. */
7576 outname = bfd_get_section_name (output_bfd,
7577 s->output_section);
7578 target = bfd_get_section_by_name (output_bfd, outname + 4);
7579 if ((target != NULL
7580 && (target->flags & SEC_READONLY) != 0
7581 && (target->flags & SEC_ALLOC) != 0)
7582 || strcmp (outname,
7583 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
7584 reltext = true;
7585
7586 /* We use the reloc_count field as a counter if we need
7587 to copy relocs into the output file. */
7588 if (strcmp (name,
7589 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
7590 s->reloc_count = 0;
7591 }
7592 }
7593 else if (strncmp (name, ".got", 4) == 0)
7594 {
7595 int i;
7596 bfd_size_type loadable_size = 0;
7597 bfd_size_type local_gotno;
7598 struct _bfd *sub;
7599
7600 BFD_ASSERT (elf_section_data (s) != NULL);
7601 g = (struct mips_got_info *) elf_section_data (s)->tdata;
7602 BFD_ASSERT (g != NULL);
7603
7604 /* Calculate the total loadable size of the output. That
7605 will give us the maximum number of GOT_PAGE entries
7606 required. */
7607 for (sub = info->input_bfds; sub; sub = sub->link_next)
7608 {
7609 asection *subsection;
7610
7611 for (subsection = sub->sections;
7612 subsection;
7613 subsection = subsection->next)
7614 {
7615 if ((subsection->flags & SEC_ALLOC) == 0)
7616 continue;
7617 loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
7618 }
7619 }
7620 loadable_size += MIPS_FUNCTION_STUB_SIZE;
7621
7622 /* Assume there are two loadable segments consisting of
7623 contiguous sections. Is 5 enough? */
7624 local_gotno = (loadable_size >> 16) + 5;
7625 g->local_gotno += local_gotno;
7626 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
7627
7628 /* There has to be a global GOT entry for every symbol with
7629 a dynamic symbol table index of DT_MIPS_GOTSYM or
7630 higher. Therefore, it make sense to put those symbols
7631 that need GOT entries at the end of the symbol table. We
7632 do that here. */
7633 if (!mips_elf_sort_hash_table (info, 1))
7634 return false;
7635
7636 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7637 g->global_gotno = i;
7638 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
7639 }
7640 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
7641 {
7642 /* Irix rld assumes that the function stub isn't at the end
7643 of .text section. So put a dummy. XXX */
7644 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7645 }
7646 else if (! info->shared
7647 && ! mips_elf_hash_table (info)->use_rld_obj_head
7648 && strncmp (name, ".rld_map", 8) == 0)
7649 {
7650 /* We add a room for __rld_map. It will be filled in by the
7651 rtld to contain a pointer to the _r_debug structure. */
7652 s->_raw_size += 4;
7653 }
7654 else if (SGI_COMPAT (output_bfd)
7655 && strncmp (name, ".compact_rel", 12) == 0)
7656 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
7657 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
7658 == 0)
7659 s->_raw_size = (sizeof (Elf32_External_Msym)
7660 * (elf_hash_table (info)->dynsymcount
7661 + bfd_count_sections (output_bfd)));
7662 else if (strncmp (name, ".init", 5) != 0)
7663 {
7664 /* It's not one of our sections, so don't allocate space. */
7665 continue;
7666 }
7667
7668 if (strip)
7669 {
7670 _bfd_strip_section_from_output (s);
7671 continue;
7672 }
7673
7674 /* Allocate memory for the section contents. */
7675 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
7676 if (s->contents == NULL && s->_raw_size != 0)
7677 {
7678 bfd_set_error (bfd_error_no_memory);
7679 return false;
7680 }
7681 }
7682
7683 if (elf_hash_table (info)->dynamic_sections_created)
7684 {
7685 /* Add some entries to the .dynamic section. We fill in the
7686 values later, in elf_mips_finish_dynamic_sections, but we
7687 must add the entries now so that we get the correct size for
7688 the .dynamic section. The DT_DEBUG entry is filled in by the
7689 dynamic linker and used by the debugger. */
7690 if (! info->shared)
7691 {
7692 if (SGI_COMPAT (output_bfd))
7693 {
7694 /* SGI object has the equivalence of DT_DEBUG in the
7695 DT_MIPS_RLD_MAP entry. */
7696 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
7697 return false;
7698 }
7699 else
7700 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
7701 return false;
7702 }
7703
7704 if (reltext)
7705 {
7706 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
7707 return false;
7708 }
7709
7710 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
7711 return false;
7712
7713 if (bfd_get_section_by_name (dynobj,
7714 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
7715 {
7716 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
7717 return false;
7718
7719 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
7720 return false;
7721
7722 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
7723 return false;
7724 }
7725
7726 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
7727 return false;
7728
7729 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
7730 return false;
7731
7732 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
7733 {
7734 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
7735 return false;
7736
7737 s = bfd_get_section_by_name (dynobj, ".liblist");
7738 BFD_ASSERT (s != NULL);
7739
7740 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
7741 return false;
7742 }
7743
7744 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
7745 return false;
7746
7747 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
7748 return false;
7749
7750 #if 0
7751 /* Time stamps in executable files are a bad idea. */
7752 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
7753 return false;
7754 #endif
7755
7756 #if 0 /* FIXME */
7757 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
7758 return false;
7759 #endif
7760
7761 #if 0 /* FIXME */
7762 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
7763 return false;
7764 #endif
7765
7766 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
7767 return false;
7768
7769 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
7770 return false;
7771
7772 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
7773 return false;
7774
7775 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
7776 return false;
7777
7778 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
7779 return false;
7780
7781 if (IRIX_COMPAT (dynobj) == ict_irix5
7782 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
7783 return false;
7784
7785 if (IRIX_COMPAT (dynobj) == ict_irix6
7786 && (bfd_get_section_by_name
7787 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
7788 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
7789 return false;
7790
7791 if (bfd_get_section_by_name (dynobj,
7792 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
7793 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
7794 return false;
7795 }
7796
7797 return true;
7798 }
7799
7800 /* If NAME is one of the special IRIX6 symbols defined by the linker,
7801 adjust it appropriately now. */
7802
7803 static void
7804 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
7805 bfd *abfd ATTRIBUTE_UNUSED;
7806 const char *name;
7807 Elf_Internal_Sym *sym;
7808 {
7809 /* The linker script takes care of providing names and values for
7810 these, but we must place them into the right sections. */
7811 static const char* const text_section_symbols[] = {
7812 "_ftext",
7813 "_etext",
7814 "__dso_displacement",
7815 "__elf_header",
7816 "__program_header_table",
7817 NULL
7818 };
7819
7820 static const char* const data_section_symbols[] = {
7821 "_fdata",
7822 "_edata",
7823 "_end",
7824 "_fbss",
7825 NULL
7826 };
7827
7828 const char* const *p;
7829 int i;
7830
7831 for (i = 0; i < 2; ++i)
7832 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7833 *p;
7834 ++p)
7835 if (strcmp (*p, name) == 0)
7836 {
7837 /* All of these symbols are given type STT_SECTION by the
7838 IRIX6 linker. */
7839 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7840
7841 /* The IRIX linker puts these symbols in special sections. */
7842 if (i == 0)
7843 sym->st_shndx = SHN_MIPS_TEXT;
7844 else
7845 sym->st_shndx = SHN_MIPS_DATA;
7846
7847 break;
7848 }
7849 }
7850
7851 /* Finish up dynamic symbol handling. We set the contents of various
7852 dynamic sections here. */
7853
7854 boolean
7855 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7856 bfd *output_bfd;
7857 struct bfd_link_info *info;
7858 struct elf_link_hash_entry *h;
7859 Elf_Internal_Sym *sym;
7860 {
7861 bfd *dynobj;
7862 bfd_vma gval;
7863 asection *sgot;
7864 asection *smsym;
7865 struct mips_got_info *g;
7866 const char *name;
7867 struct mips_elf_link_hash_entry *mh;
7868
7869 dynobj = elf_hash_table (info)->dynobj;
7870 gval = sym->st_value;
7871 mh = (struct mips_elf_link_hash_entry *) h;
7872
7873 if (h->plt.offset != (bfd_vma) -1)
7874 {
7875 asection *s;
7876 bfd_byte *p;
7877 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7878
7879 /* This symbol has a stub. Set it up. */
7880
7881 BFD_ASSERT (h->dynindx != -1);
7882
7883 s = bfd_get_section_by_name (dynobj,
7884 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7885 BFD_ASSERT (s != NULL);
7886
7887 /* Fill the stub. */
7888 p = stub;
7889 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7890 p += 4;
7891 bfd_put_32 (output_bfd, STUB_MOVE, p);
7892 p += 4;
7893
7894 /* FIXME: Can h->dynindex be more than 64K? */
7895 if (h->dynindx & 0xffff0000)
7896 return false;
7897
7898 bfd_put_32 (output_bfd, STUB_JALR, p);
7899 p += 4;
7900 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7901
7902 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7903 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7904
7905 /* Mark the symbol as undefined. plt.offset != -1 occurs
7906 only for the referenced symbol. */
7907 sym->st_shndx = SHN_UNDEF;
7908
7909 /* The run-time linker uses the st_value field of the symbol
7910 to reset the global offset table entry for this external
7911 to its stub address when unlinking a shared object. */
7912 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7913 sym->st_value = gval;
7914 }
7915
7916 BFD_ASSERT (h->dynindx != -1);
7917
7918 sgot = mips_elf_got_section (dynobj);
7919 BFD_ASSERT (sgot != NULL);
7920 BFD_ASSERT (elf_section_data (sgot) != NULL);
7921 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7922 BFD_ASSERT (g != NULL);
7923
7924 /* Run through the global symbol table, creating GOT entries for all
7925 the symbols that need them. */
7926 if (h->dynindx >= g->global_gotsym->dynindx)
7927 {
7928 bfd_vma offset;
7929 bfd_vma value;
7930
7931 if (sym->st_value)
7932 value = sym->st_value;
7933 else
7934 /* For an entity defined in a shared object, this will be
7935 NULL. (For functions in shared objects for
7936 which we have created stubs, ST_VALUE will be non-NULL.
7937 That's because such the functions are now no longer defined
7938 in a shared object.) */
7939 value = h->root.u.def.value;
7940
7941 offset = mips_elf_global_got_index (dynobj, h);
7942 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
7943 }
7944
7945 /* Create a .msym entry, if appropriate. */
7946 smsym = bfd_get_section_by_name (dynobj,
7947 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
7948 if (smsym)
7949 {
7950 Elf32_Internal_Msym msym;
7951
7952 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
7953 /* It is undocumented what the `1' indicates, but IRIX6 uses
7954 this value. */
7955 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
7956 bfd_mips_elf_swap_msym_out
7957 (dynobj, &msym,
7958 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
7959 }
7960
7961 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7962 name = h->root.root.string;
7963 if (strcmp (name, "_DYNAMIC") == 0
7964 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7965 sym->st_shndx = SHN_ABS;
7966 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7967 {
7968 sym->st_shndx = SHN_ABS;
7969 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7970 sym->st_value = 1;
7971 }
7972 else if (SGI_COMPAT (output_bfd))
7973 {
7974 if (strcmp (name, "_gp_disp") == 0)
7975 {
7976 sym->st_shndx = SHN_ABS;
7977 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7978 sym->st_value = elf_gp (output_bfd);
7979 }
7980 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7981 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7982 {
7983 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7984 sym->st_other = STO_PROTECTED;
7985 sym->st_value = 0;
7986 sym->st_shndx = SHN_MIPS_DATA;
7987 }
7988 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7989 {
7990 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7991 sym->st_other = STO_PROTECTED;
7992 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7993 sym->st_shndx = SHN_ABS;
7994 }
7995 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7996 {
7997 if (h->type == STT_FUNC)
7998 sym->st_shndx = SHN_MIPS_TEXT;
7999 else if (h->type == STT_OBJECT)
8000 sym->st_shndx = SHN_MIPS_DATA;
8001 }
8002 }
8003
8004 /* Handle the IRIX6-specific symbols. */
8005 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8006 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8007
8008 if (SGI_COMPAT (output_bfd)
8009 && ! info->shared)
8010 {
8011 if (! mips_elf_hash_table (info)->use_rld_obj_head
8012 && strcmp (name, "__rld_map") == 0)
8013 {
8014 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8015 BFD_ASSERT (s != NULL);
8016 sym->st_value = s->output_section->vma + s->output_offset;
8017 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
8018 if (mips_elf_hash_table (info)->rld_value == 0)
8019 mips_elf_hash_table (info)->rld_value = sym->st_value;
8020 }
8021 else if (mips_elf_hash_table (info)->use_rld_obj_head
8022 && strcmp (name, "__rld_obj_head") == 0)
8023 {
8024 /* IRIX6 does not use a .rld_map section. */
8025 if (IRIX_COMPAT (output_bfd) == ict_irix5)
8026 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8027 != NULL);
8028 mips_elf_hash_table (info)->rld_value = sym->st_value;
8029 }
8030 }
8031
8032 /* If this is a mips16 symbol, force the value to be even. */
8033 if (sym->st_other == STO_MIPS16
8034 && (sym->st_value & 1) != 0)
8035 --sym->st_value;
8036
8037 return true;
8038 }
8039
8040 /* Finish up the dynamic sections. */
8041
8042 boolean
8043 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
8044 bfd *output_bfd;
8045 struct bfd_link_info *info;
8046 {
8047 bfd *dynobj;
8048 asection *sdyn;
8049 asection *sgot;
8050 struct mips_got_info *g;
8051
8052 dynobj = elf_hash_table (info)->dynobj;
8053
8054 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8055
8056 sgot = mips_elf_got_section (dynobj);
8057 if (sgot == NULL)
8058 g = NULL;
8059 else
8060 {
8061 BFD_ASSERT (elf_section_data (sgot) != NULL);
8062 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8063 BFD_ASSERT (g != NULL);
8064 }
8065
8066 if (elf_hash_table (info)->dynamic_sections_created)
8067 {
8068 bfd_byte *b;
8069
8070 BFD_ASSERT (sdyn != NULL);
8071 BFD_ASSERT (g != NULL);
8072
8073 for (b = sdyn->contents;
8074 b < sdyn->contents + sdyn->_raw_size;
8075 b += MIPS_ELF_DYN_SIZE (dynobj))
8076 {
8077 Elf_Internal_Dyn dyn;
8078 const char *name;
8079 size_t elemsize;
8080 asection *s;
8081 boolean swap_out_p;
8082
8083 /* Read in the current dynamic entry. */
8084 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8085
8086 /* Assume that we're going to modify it and write it out. */
8087 swap_out_p = true;
8088
8089 switch (dyn.d_tag)
8090 {
8091 case DT_RELENT:
8092 s = (bfd_get_section_by_name
8093 (dynobj,
8094 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
8095 BFD_ASSERT (s != NULL);
8096 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8097 break;
8098
8099 case DT_STRSZ:
8100 /* Rewrite DT_STRSZ. */
8101 dyn.d_un.d_val =
8102 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
8103 break;
8104
8105 case DT_PLTGOT:
8106 name = ".got";
8107 goto get_vma;
8108 case DT_MIPS_CONFLICT:
8109 name = ".conflict";
8110 goto get_vma;
8111 case DT_MIPS_LIBLIST:
8112 name = ".liblist";
8113 get_vma:
8114 s = bfd_get_section_by_name (output_bfd, name);
8115 BFD_ASSERT (s != NULL);
8116 dyn.d_un.d_ptr = s->vma;
8117 break;
8118
8119 case DT_MIPS_RLD_VERSION:
8120 dyn.d_un.d_val = 1; /* XXX */
8121 break;
8122
8123 case DT_MIPS_FLAGS:
8124 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8125 break;
8126
8127 case DT_MIPS_CONFLICTNO:
8128 name = ".conflict";
8129 elemsize = sizeof (Elf32_Conflict);
8130 goto set_elemno;
8131
8132 case DT_MIPS_LIBLISTNO:
8133 name = ".liblist";
8134 elemsize = sizeof (Elf32_Lib);
8135 set_elemno:
8136 s = bfd_get_section_by_name (output_bfd, name);
8137 if (s != NULL)
8138 {
8139 if (s->_cooked_size != 0)
8140 dyn.d_un.d_val = s->_cooked_size / elemsize;
8141 else
8142 dyn.d_un.d_val = s->_raw_size / elemsize;
8143 }
8144 else
8145 dyn.d_un.d_val = 0;
8146 break;
8147
8148 case DT_MIPS_TIME_STAMP:
8149 time ((time_t *) &dyn.d_un.d_val);
8150 break;
8151
8152 case DT_MIPS_ICHECKSUM:
8153 /* XXX FIXME: */
8154 swap_out_p = false;
8155 break;
8156
8157 case DT_MIPS_IVERSION:
8158 /* XXX FIXME: */
8159 swap_out_p = false;
8160 break;
8161
8162 case DT_MIPS_BASE_ADDRESS:
8163 s = output_bfd->sections;
8164 BFD_ASSERT (s != NULL);
8165 dyn.d_un.d_ptr = s->vma & ~(0xffff);
8166 break;
8167
8168 case DT_MIPS_LOCAL_GOTNO:
8169 dyn.d_un.d_val = g->local_gotno;
8170 break;
8171
8172 case DT_MIPS_SYMTABNO:
8173 name = ".dynsym";
8174 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8175 s = bfd_get_section_by_name (output_bfd, name);
8176 BFD_ASSERT (s != NULL);
8177
8178 if (s->_cooked_size != 0)
8179 dyn.d_un.d_val = s->_cooked_size / elemsize;
8180 else
8181 dyn.d_un.d_val = s->_raw_size / elemsize;
8182 break;
8183
8184 case DT_MIPS_UNREFEXTNO:
8185 /* The index into the dynamic symbol table which is the
8186 entry of the first external symbol that is not
8187 referenced within the same object. */
8188 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8189 break;
8190
8191 case DT_MIPS_GOTSYM:
8192 dyn.d_un.d_val = g->global_gotsym->dynindx;
8193 break;
8194
8195 case DT_MIPS_HIPAGENO:
8196 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8197 break;
8198
8199 case DT_MIPS_RLD_MAP:
8200 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8201 break;
8202
8203 case DT_MIPS_OPTIONS:
8204 s = (bfd_get_section_by_name
8205 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8206 dyn.d_un.d_ptr = s->vma;
8207 break;
8208
8209 case DT_MIPS_MSYM:
8210 s = (bfd_get_section_by_name
8211 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8212 dyn.d_un.d_ptr = s->vma;
8213 break;
8214
8215 default:
8216 swap_out_p = false;
8217 break;
8218 }
8219
8220 if (swap_out_p)
8221 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8222 (dynobj, &dyn, b);
8223 }
8224 }
8225
8226 /* The first entry of the global offset table will be filled at
8227 runtime. The second entry will be used by some runtime loaders.
8228 This isn't the case of Irix rld. */
8229 if (sgot != NULL && sgot->_raw_size > 0)
8230 {
8231 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8232 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8233 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8234 }
8235
8236 if (sgot != NULL)
8237 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8238 = MIPS_ELF_GOT_SIZE (output_bfd);
8239
8240 {
8241 asection *smsym;
8242 asection *s;
8243 Elf32_compact_rel cpt;
8244
8245 /* ??? The section symbols for the output sections were set up in
8246 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
8247 symbols. Should we do so? */
8248
8249 smsym = bfd_get_section_by_name (dynobj,
8250 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8251 if (smsym != NULL)
8252 {
8253 Elf32_Internal_Msym msym;
8254
8255 msym.ms_hash_value = 0;
8256 msym.ms_info = ELF32_MS_INFO (0, 1);
8257
8258 for (s = output_bfd->sections; s != NULL; s = s->next)
8259 {
8260 long dynindx = elf_section_data (s)->dynindx;
8261
8262 bfd_mips_elf_swap_msym_out
8263 (output_bfd, &msym,
8264 (((Elf32_External_Msym *) smsym->contents)
8265 + dynindx));
8266 }
8267 }
8268
8269 if (SGI_COMPAT (output_bfd))
8270 {
8271 /* Write .compact_rel section out. */
8272 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8273 if (s != NULL)
8274 {
8275 cpt.id1 = 1;
8276 cpt.num = s->reloc_count;
8277 cpt.id2 = 2;
8278 cpt.offset = (s->output_section->filepos
8279 + sizeof (Elf32_External_compact_rel));
8280 cpt.reserved0 = 0;
8281 cpt.reserved1 = 0;
8282 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8283 ((Elf32_External_compact_rel *)
8284 s->contents));
8285
8286 /* Clean up a dummy stub function entry in .text. */
8287 s = bfd_get_section_by_name (dynobj,
8288 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8289 if (s != NULL)
8290 {
8291 file_ptr dummy_offset;
8292
8293 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8294 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8295 memset (s->contents + dummy_offset, 0,
8296 MIPS_FUNCTION_STUB_SIZE);
8297 }
8298 }
8299 }
8300
8301 /* Clean up a first relocation in .rel.dyn. */
8302 s = bfd_get_section_by_name (dynobj,
8303 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
8304 if (s != NULL && s->_raw_size > 0)
8305 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
8306 }
8307
8308 return true;
8309 }
8310 \f
8311 /* This is almost identical to bfd_generic_get_... except that some
8312 MIPS relocations need to be handled specially. Sigh. */
8313
8314 static bfd_byte *
8315 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8316 relocateable, symbols)
8317 bfd *abfd;
8318 struct bfd_link_info *link_info;
8319 struct bfd_link_order *link_order;
8320 bfd_byte *data;
8321 boolean relocateable;
8322 asymbol **symbols;
8323 {
8324 /* Get enough memory to hold the stuff */
8325 bfd *input_bfd = link_order->u.indirect.section->owner;
8326 asection *input_section = link_order->u.indirect.section;
8327
8328 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8329 arelent **reloc_vector = NULL;
8330 long reloc_count;
8331
8332 if (reloc_size < 0)
8333 goto error_return;
8334
8335 reloc_vector = (arelent **) bfd_malloc (reloc_size);
8336 if (reloc_vector == NULL && reloc_size != 0)
8337 goto error_return;
8338
8339 /* read in the section */
8340 if (!bfd_get_section_contents (input_bfd,
8341 input_section,
8342 (PTR) data,
8343 0,
8344 input_section->_raw_size))
8345 goto error_return;
8346
8347 /* We're not relaxing the section, so just copy the size info */
8348 input_section->_cooked_size = input_section->_raw_size;
8349 input_section->reloc_done = true;
8350
8351 reloc_count = bfd_canonicalize_reloc (input_bfd,
8352 input_section,
8353 reloc_vector,
8354 symbols);
8355 if (reloc_count < 0)
8356 goto error_return;
8357
8358 if (reloc_count > 0)
8359 {
8360 arelent **parent;
8361 /* for mips */
8362 int gp_found;
8363 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8364
8365 {
8366 struct bfd_hash_entry *h;
8367 struct bfd_link_hash_entry *lh;
8368 /* Skip all this stuff if we aren't mixing formats. */
8369 if (abfd && input_bfd
8370 && abfd->xvec == input_bfd->xvec)
8371 lh = 0;
8372 else
8373 {
8374 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8375 lh = (struct bfd_link_hash_entry *) h;
8376 }
8377 lookup:
8378 if (lh)
8379 {
8380 switch (lh->type)
8381 {
8382 case bfd_link_hash_undefined:
8383 case bfd_link_hash_undefweak:
8384 case bfd_link_hash_common:
8385 gp_found = 0;
8386 break;
8387 case bfd_link_hash_defined:
8388 case bfd_link_hash_defweak:
8389 gp_found = 1;
8390 gp = lh->u.def.value;
8391 break;
8392 case bfd_link_hash_indirect:
8393 case bfd_link_hash_warning:
8394 lh = lh->u.i.link;
8395 /* @@FIXME ignoring warning for now */
8396 goto lookup;
8397 case bfd_link_hash_new:
8398 default:
8399 abort ();
8400 }
8401 }
8402 else
8403 gp_found = 0;
8404 }
8405 /* end mips */
8406 for (parent = reloc_vector; *parent != (arelent *) NULL;
8407 parent++)
8408 {
8409 char *error_message = (char *) NULL;
8410 bfd_reloc_status_type r;
8411
8412 /* Specific to MIPS: Deal with relocation types that require
8413 knowing the gp of the output bfd. */
8414 asymbol *sym = *(*parent)->sym_ptr_ptr;
8415 if (bfd_is_abs_section (sym->section) && abfd)
8416 {
8417 /* The special_function wouldn't get called anyways. */
8418 }
8419 else if (!gp_found)
8420 {
8421 /* The gp isn't there; let the special function code
8422 fall over on its own. */
8423 }
8424 else if ((*parent)->howto->special_function
8425 == _bfd_mips_elf_gprel16_reloc)
8426 {
8427 /* bypass special_function call */
8428 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8429 relocateable, (PTR) data, gp);
8430 goto skip_bfd_perform_relocation;
8431 }
8432 /* end mips specific stuff */
8433
8434 r = bfd_perform_relocation (input_bfd,
8435 *parent,
8436 (PTR) data,
8437 input_section,
8438 relocateable ? abfd : (bfd *) NULL,
8439 &error_message);
8440 skip_bfd_perform_relocation:
8441
8442 if (relocateable)
8443 {
8444 asection *os = input_section->output_section;
8445
8446 /* A partial link, so keep the relocs */
8447 os->orelocation[os->reloc_count] = *parent;
8448 os->reloc_count++;
8449 }
8450
8451 if (r != bfd_reloc_ok)
8452 {
8453 switch (r)
8454 {
8455 case bfd_reloc_undefined:
8456 if (!((*link_info->callbacks->undefined_symbol)
8457 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8458 input_bfd, input_section, (*parent)->address)))
8459 goto error_return;
8460 break;
8461 case bfd_reloc_dangerous:
8462 BFD_ASSERT (error_message != (char *) NULL);
8463 if (!((*link_info->callbacks->reloc_dangerous)
8464 (link_info, error_message, input_bfd, input_section,
8465 (*parent)->address)))
8466 goto error_return;
8467 break;
8468 case bfd_reloc_overflow:
8469 if (!((*link_info->callbacks->reloc_overflow)
8470 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8471 (*parent)->howto->name, (*parent)->addend,
8472 input_bfd, input_section, (*parent)->address)))
8473 goto error_return;
8474 break;
8475 case bfd_reloc_outofrange:
8476 default:
8477 abort ();
8478 break;
8479 }
8480
8481 }
8482 }
8483 }
8484 if (reloc_vector != NULL)
8485 free (reloc_vector);
8486 return data;
8487
8488 error_return:
8489 if (reloc_vector != NULL)
8490 free (reloc_vector);
8491 return NULL;
8492 }
8493 #define bfd_elf32_bfd_get_relocated_section_contents \
8494 elf32_mips_get_relocated_section_contents
8495 \f
8496 /* ECOFF swapping routines. These are used when dealing with the
8497 .mdebug section, which is in the ECOFF debugging format. */
8498 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8499 {
8500 /* Symbol table magic number. */
8501 magicSym,
8502 /* Alignment of debugging information. E.g., 4. */
8503 4,
8504 /* Sizes of external symbolic information. */
8505 sizeof (struct hdr_ext),
8506 sizeof (struct dnr_ext),
8507 sizeof (struct pdr_ext),
8508 sizeof (struct sym_ext),
8509 sizeof (struct opt_ext),
8510 sizeof (struct fdr_ext),
8511 sizeof (struct rfd_ext),
8512 sizeof (struct ext_ext),
8513 /* Functions to swap in external symbolic data. */
8514 ecoff_swap_hdr_in,
8515 ecoff_swap_dnr_in,
8516 ecoff_swap_pdr_in,
8517 ecoff_swap_sym_in,
8518 ecoff_swap_opt_in,
8519 ecoff_swap_fdr_in,
8520 ecoff_swap_rfd_in,
8521 ecoff_swap_ext_in,
8522 _bfd_ecoff_swap_tir_in,
8523 _bfd_ecoff_swap_rndx_in,
8524 /* Functions to swap out external symbolic data. */
8525 ecoff_swap_hdr_out,
8526 ecoff_swap_dnr_out,
8527 ecoff_swap_pdr_out,
8528 ecoff_swap_sym_out,
8529 ecoff_swap_opt_out,
8530 ecoff_swap_fdr_out,
8531 ecoff_swap_rfd_out,
8532 ecoff_swap_ext_out,
8533 _bfd_ecoff_swap_tir_out,
8534 _bfd_ecoff_swap_rndx_out,
8535 /* Function to read in symbolic data. */
8536 _bfd_mips_elf_read_ecoff_info
8537 };
8538 \f
8539 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8540 #define TARGET_LITTLE_NAME "elf32-littlemips"
8541 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8542 #define TARGET_BIG_NAME "elf32-bigmips"
8543 #define ELF_ARCH bfd_arch_mips
8544 #define ELF_MACHINE_CODE EM_MIPS
8545
8546 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8547 a value of 0x1000, and we are compatible. */
8548 #define ELF_MAXPAGESIZE 0x1000
8549
8550 #define elf_backend_collect true
8551 #define elf_backend_type_change_ok true
8552 #define elf_backend_can_gc_sections true
8553 #define elf_info_to_howto mips_info_to_howto_rela
8554 #define elf_info_to_howto_rel mips_info_to_howto_rel
8555 #define elf_backend_sym_is_global mips_elf_sym_is_global
8556 #define elf_backend_object_p _bfd_mips_elf_object_p
8557 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
8558 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8559 #define elf_backend_section_from_bfd_section \
8560 _bfd_mips_elf_section_from_bfd_section
8561 #define elf_backend_section_processing _bfd_mips_elf_section_processing
8562 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8563 #define elf_backend_additional_program_headers \
8564 _bfd_mips_elf_additional_program_headers
8565 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
8566 #define elf_backend_final_write_processing \
8567 _bfd_mips_elf_final_write_processing
8568 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8569 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
8570 #define elf_backend_create_dynamic_sections \
8571 _bfd_mips_elf_create_dynamic_sections
8572 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
8573 #define elf_backend_adjust_dynamic_symbol \
8574 _bfd_mips_elf_adjust_dynamic_symbol
8575 #define elf_backend_always_size_sections \
8576 _bfd_mips_elf_always_size_sections
8577 #define elf_backend_size_dynamic_sections \
8578 _bfd_mips_elf_size_dynamic_sections
8579 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
8580 #define elf_backend_link_output_symbol_hook \
8581 _bfd_mips_elf_link_output_symbol_hook
8582 #define elf_backend_finish_dynamic_symbol \
8583 _bfd_mips_elf_finish_dynamic_symbol
8584 #define elf_backend_finish_dynamic_sections \
8585 _bfd_mips_elf_finish_dynamic_sections
8586 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
8587 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8588
8589 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8590 #define elf_backend_plt_header_size 0
8591
8592 #define bfd_elf32_bfd_is_local_label_name \
8593 mips_elf_is_local_label_name
8594 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8595 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8596 #define bfd_elf32_bfd_link_hash_table_create \
8597 _bfd_mips_elf_link_hash_table_create
8598 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
8599 #define bfd_elf32_bfd_copy_private_bfd_data \
8600 _bfd_mips_elf_copy_private_bfd_data
8601 #define bfd_elf32_bfd_merge_private_bfd_data \
8602 _bfd_mips_elf_merge_private_bfd_data
8603 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
8604 #define bfd_elf32_bfd_print_private_bfd_data \
8605 _bfd_mips_elf_print_private_bfd_data
8606 #include "elf32-target.h"
This page took 0.226425 seconds and 4 git commands to generate.