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