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