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