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