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