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