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