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