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