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