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