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