]> Git Repo - binutils.git/blob - bfd/elf32-mips.c
* opncls.c (bfd_alloc_by_size_t): Set bfd_error_no_memory if
[binutils.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3
4    Most of the information added by Ian Lance Taylor, Cygnus Support,
5    <[email protected]>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "genlink.h"
28 #include "elf-bfd.h"
29 #include "elf/mips.h"
30
31 /* Get the ECOFF swapping routines.  */
32 #include "coff/sym.h"
33 #include "coff/symconst.h"
34 #include "coff/internal.h"
35 #include "coff/ecoff.h"
36 #include "coff/mips.h"
37 #define ECOFF_32
38 #include "ecoffswap.h"
39
40 static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd,
41                                                           arelent *reloc,
42                                                           asymbol *symbol,
43                                                           PTR data,
44                                                           asection *section,
45                                                           bfd *output_bfd,
46                                                           char **error));
47 static bfd_reloc_status_type mips_elf_got16_reloc PARAMS ((bfd *abfd,
48                                                            arelent *reloc,
49                                                            asymbol *symbol,
50                                                            PTR data,
51                                                            asection *section,
52                                                            bfd *output_bfd,
53                                                            char **error));
54 static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd,
55                                                           arelent *reloc,
56                                                           asymbol *symbol,
57                                                           PTR data,
58                                                           asection *section,
59                                                           bfd *output_bfd,
60                                                           char **error));
61 static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd,
62                                                              arelent *reloc,
63                                                              asymbol *symbol,
64                                                              PTR data,
65                                                              asection *section,
66                                                              bfd *output_bfd,
67                                                              char **error));
68 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
69   PARAMS ((bfd *, bfd_reloc_code_real_type));
70 static void mips_info_to_howto_rel
71   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
72 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
73 static boolean mips_elf_object_p PARAMS ((bfd *));
74 static void mips_elf_final_write_processing
75   PARAMS ((bfd *, boolean));
76 static boolean mips_elf_section_from_shdr
77   PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
78 static boolean mips_elf_fake_sections
79   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
80 static boolean mips_elf_section_from_bfd_section
81   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
82 static boolean mips_elf_section_processing
83   PARAMS ((bfd *, Elf32_Internal_Shdr *));
84 static void mips_elf_symbol_processing PARAMS ((bfd *, asymbol *));
85 static boolean mips_elf_read_ecoff_info
86   PARAMS ((bfd *, asection *, struct ecoff_debug_info *));
87 static boolean mips_elf_is_local_label
88   PARAMS ((bfd *, asymbol *));
89 static boolean mips_elf_find_nearest_line
90   PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
91            const char **, unsigned int *));
92 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
93   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
94 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
95   PARAMS ((bfd *));
96 static int gptab_compare PARAMS ((const void *, const void *));
97 static boolean mips_elf_final_link
98   PARAMS ((bfd *, struct bfd_link_info *));
99 static void mips_elf_relocate_hi16
100   PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
101            bfd_vma));
102 static boolean mips_elf_relocate_section
103   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
104            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
105 static boolean mips_elf_add_symbol_hook
106   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
107            const char **, flagword *, asection **, bfd_vma *));
108
109 #define USE_REL 1               /* MIPS uses REL relocations instead of RELA */
110
111 enum reloc_type
112 {
113   R_MIPS_NONE = 0,
114   R_MIPS_16,            R_MIPS_32,
115   R_MIPS_REL32,         R_MIPS_26,
116   R_MIPS_HI16,          R_MIPS_LO16,
117   R_MIPS_GPREL16,       R_MIPS_LITERAL,
118   R_MIPS_GOT16,         R_MIPS_PC16,
119   R_MIPS_CALL16,        R_MIPS_GPREL32,
120   /* The remaining relocs are defined on Irix, although they are not
121      in the MIPS ELF ABI.  */
122   R_MIPS_UNUSED1,       R_MIPS_UNUSED2,
123   R_MIPS_UNUSED3,
124   R_MIPS_SHIFT5,        R_MIPS_SHIFT6,
125   R_MIPS_64,            R_MIPS_GOT_DISP,
126   R_MIPS_GOT_PAGE,      R_MIPS_GOT_OFST,
127   R_MIPS_GOT_HI16,      R_MIPS_GOT_LO16,
128   R_MIPS_SUB,           R_MIPS_INSERT_A,
129   R_MIPS_INSERT_B,      R_MIPS_DELETE,
130   R_MIPS_HIGHER,        R_MIPS_HIGHEST,
131   R_MIPS_CALL_HI16,     R_MIPS_CALL_LO16,
132   R_MIPS_max
133 };
134
135 static reloc_howto_type elf_mips_howto_table[] =
136 {
137   /* No relocation.  */
138   HOWTO (R_MIPS_NONE,           /* type */
139          0,                     /* rightshift */
140          0,                     /* size (0 = byte, 1 = short, 2 = long) */
141          0,                     /* bitsize */
142          false,                 /* pc_relative */
143          0,                     /* bitpos */
144          complain_overflow_dont, /* complain_on_overflow */
145          bfd_elf_generic_reloc, /* special_function */
146          "R_MIPS_NONE",         /* name */
147          false,                 /* partial_inplace */
148          0,                     /* src_mask */
149          0,                     /* dst_mask */
150          false),                /* pcrel_offset */
151
152   /* 16 bit relocation.  */
153   HOWTO (R_MIPS_16,             /* type */
154          0,                     /* rightshift */
155          1,                     /* size (0 = byte, 1 = short, 2 = long) */
156          16,                    /* bitsize */
157          false,                 /* pc_relative */
158          0,                     /* bitpos */
159          complain_overflow_bitfield, /* complain_on_overflow */
160          bfd_elf_generic_reloc, /* special_function */
161          "R_MIPS_16",           /* name */
162          true,                  /* partial_inplace */
163          0xffff,                /* src_mask */
164          0xffff,                /* dst_mask */
165          false),                /* pcrel_offset */
166
167   /* 32 bit relocation.  */
168   HOWTO (R_MIPS_32,             /* type */
169          0,                     /* rightshift */
170          2,                     /* size (0 = byte, 1 = short, 2 = long) */
171          32,                    /* bitsize */
172          false,                 /* pc_relative */
173          0,                     /* bitpos */
174          complain_overflow_bitfield, /* complain_on_overflow */
175          bfd_elf_generic_reloc, /* special_function */
176          "R_MIPS_32",           /* name */
177          true,                  /* partial_inplace */
178          0xffffffff,            /* src_mask */
179          0xffffffff,            /* dst_mask */
180          false),                /* pcrel_offset */
181
182   /* 32 bit symbol relative relocation.  */
183   HOWTO (R_MIPS_REL32,          /* type */
184          0,                     /* rightshift */
185          2,                     /* size (0 = byte, 1 = short, 2 = long) */
186          32,                    /* bitsize */
187          false,                 /* pc_relative */
188          0,                     /* bitpos */
189          complain_overflow_bitfield, /* complain_on_overflow */
190          bfd_elf_generic_reloc, /* special_function */
191          "R_MIPS_REL32",        /* name */
192          true,                  /* partial_inplace */
193          0xffffffff,            /* src_mask */
194          0xffffffff,            /* dst_mask */
195          false),                /* pcrel_offset */
196
197   /* 26 bit branch address.  */
198   HOWTO (R_MIPS_26,             /* type */
199          2,                     /* rightshift */
200          2,                     /* size (0 = byte, 1 = short, 2 = long) */
201          26,                    /* bitsize */
202          false,                 /* pc_relative */
203          0,                     /* bitpos */
204          complain_overflow_dont, /* complain_on_overflow */
205                                 /* This needs complex overflow
206                                    detection, because the upper four
207                                    bits must match the PC.  */
208          bfd_elf_generic_reloc, /* special_function */
209          "R_MIPS_26",           /* name */
210          true,                  /* partial_inplace */
211          0x3ffffff,             /* src_mask */
212          0x3ffffff,             /* dst_mask */
213          false),                /* pcrel_offset */
214
215   /* High 16 bits of symbol value.  */
216   HOWTO (R_MIPS_HI16,           /* type */
217          0,                     /* rightshift */
218          2,                     /* size (0 = byte, 1 = short, 2 = long) */
219          16,                    /* bitsize */
220          false,                 /* pc_relative */
221          0,                     /* bitpos */
222          complain_overflow_dont, /* complain_on_overflow */
223          mips_elf_hi16_reloc,   /* special_function */
224          "R_MIPS_HI16",         /* name */
225          true,                  /* partial_inplace */
226          0xffff,                /* src_mask */
227          0xffff,                /* dst_mask */
228          false),                /* pcrel_offset */
229
230   /* Low 16 bits of symbol value.  */
231   HOWTO (R_MIPS_LO16,           /* type */
232          0,                     /* rightshift */
233          2,                     /* size (0 = byte, 1 = short, 2 = long) */
234          16,                    /* bitsize */
235          false,                 /* pc_relative */
236          0,                     /* bitpos */
237          complain_overflow_dont, /* complain_on_overflow */
238          mips_elf_lo16_reloc,   /* special_function */
239          "R_MIPS_LO16",         /* name */
240          true,                  /* partial_inplace */
241          0xffff,                /* src_mask */
242          0xffff,                /* dst_mask */
243          false),                /* pcrel_offset */
244
245   /* GP relative reference.  */
246   HOWTO (R_MIPS_GPREL16,        /* type */
247          0,                     /* rightshift */
248          2,                     /* size (0 = byte, 1 = short, 2 = long) */
249          16,                    /* bitsize */
250          false,                 /* pc_relative */
251          0,                     /* bitpos */
252          complain_overflow_signed, /* complain_on_overflow */
253          mips_elf_gprel16_reloc, /* special_function */
254          "R_MIPS_GPREL16",      /* name */
255          true,                  /* partial_inplace */
256          0xffff,                /* src_mask */
257          0xffff,                /* dst_mask */
258          false),                /* pcrel_offset */
259
260   /* Reference to literal section.  */
261   HOWTO (R_MIPS_LITERAL,        /* type */
262          0,                     /* rightshift */
263          2,                     /* size (0 = byte, 1 = short, 2 = long) */
264          16,                    /* bitsize */
265          false,                 /* pc_relative */
266          0,                     /* bitpos */
267          complain_overflow_signed, /* complain_on_overflow */
268          mips_elf_gprel16_reloc, /* special_function */
269          "R_MIPS_LITERAL",      /* name */
270          true,                  /* partial_inplace */
271          0xffff,                /* src_mask */
272          0xffff,                /* dst_mask */
273          false),                /* pcrel_offset */
274
275   /* Reference to global offset table.  */
276   /* FIXME: This is not handled correctly.  */
277   HOWTO (R_MIPS_GOT16,          /* type */
278          0,                     /* rightshift */
279          2,                     /* size (0 = byte, 1 = short, 2 = long) */
280          16,                    /* bitsize */
281          false,                 /* pc_relative */
282          0,                     /* bitpos */
283          complain_overflow_signed, /* complain_on_overflow */
284          mips_elf_got16_reloc,  /* special_function */
285          "R_MIPS_GOT16",        /* name */
286          false,                 /* partial_inplace */
287          0,                     /* src_mask */
288          0xffff,                /* dst_mask */
289          false),                /* pcrel_offset */
290
291   /* 16 bit PC relative reference.  */
292   HOWTO (R_MIPS_PC16,           /* type */
293          0,                     /* rightshift */
294          2,                     /* size (0 = byte, 1 = short, 2 = long) */
295          16,                    /* bitsize */
296          true,                  /* pc_relative */
297          0,                     /* bitpos */
298          complain_overflow_signed, /* complain_on_overflow */
299          bfd_elf_generic_reloc, /* special_function */
300          "R_MIPS_PC16",         /* name */
301          true,                  /* partial_inplace */
302          0xffff,                /* src_mask */
303          0xffff,                /* dst_mask */
304          false),                /* pcrel_offset */
305
306   /* 16 bit call through global offset table.  */
307   /* FIXME: This is not handled correctly.  */
308   HOWTO (R_MIPS_CALL16,         /* type */
309          0,                     /* rightshift */
310          2,                     /* size (0 = byte, 1 = short, 2 = long) */
311          16,                    /* bitsize */
312          false,                 /* pc_relative */
313          0,                     /* bitpos */
314          complain_overflow_signed, /* complain_on_overflow */
315          bfd_elf_generic_reloc, /* special_function */
316          "R_MIPS_CALL16",       /* name */
317          false,                 /* partial_inplace */
318          0,                     /* src_mask */
319          0xffff,                /* dst_mask */
320          false),                /* pcrel_offset */
321
322   /* 32 bit GP relative reference.  */
323   /* FIXME: This is not handled correctly.  */
324   HOWTO (R_MIPS_GPREL32,        /* type */
325          0,                     /* rightshift */
326          2,                     /* size (0 = byte, 1 = short, 2 = long) */
327          32,                    /* bitsize */
328          false,                 /* pc_relative */
329          0,                     /* bitpos */
330          complain_overflow_bitfield, /* complain_on_overflow */
331          bfd_elf_generic_reloc, /* special_function */
332          "R_MIPS_GPREL32",      /* name */
333          true,                  /* partial_inplace */
334          0xffffffff,            /* src_mask */
335          0xffffffff,            /* dst_mask */
336          false),                /* pcrel_offset */
337
338     /* The remaining relocs are defined on Irix 5, although they are
339        not defined by the ABI.  */
340     { 13 },
341     { 14 },
342     { 15 },
343
344   /* A 5 bit shift field.  */
345   HOWTO (R_MIPS_SHIFT5,         /* type */
346          0,                     /* rightshift */
347          2,                     /* size (0 = byte, 1 = short, 2 = long) */
348          5,                     /* bitsize */
349          false,                 /* pc_relative */
350          6,                     /* bitpos */
351          complain_overflow_bitfield, /* complain_on_overflow */
352          bfd_elf_generic_reloc, /* special_function */
353          "R_MIPS_SHIFT5",       /* name */
354          true,                  /* partial_inplace */
355          0x000007c0,            /* src_mask */
356          0x000007c0,            /* dst_mask */
357          false),                /* pcrel_offset */
358
359   /* A 6 bit shift field.  */
360   /* FIXME: This is not handled correctly; a special function is
361      needed to put the most significant bit in the right place.  */
362   HOWTO (R_MIPS_SHIFT6,         /* type */
363          0,                     /* rightshift */
364          2,                     /* size (0 = byte, 1 = short, 2 = long) */
365          6,                     /* bitsize */
366          false,                 /* pc_relative */
367          6,                     /* bitpos */
368          complain_overflow_bitfield, /* complain_on_overflow */
369          bfd_elf_generic_reloc, /* special_function */
370          "R_MIPS_SHIFT6",       /* name */
371          true,                  /* partial_inplace */
372          0x000007c4,            /* src_mask */
373          0x000007c4,            /* dst_mask */
374          false),                /* pcrel_offset */
375
376   /* A 64 bit relocation.  Presumably not used in 32 bit ELF.  */
377   { R_MIPS_64 },
378
379   /* Displacement in the global offset table.  */
380   /* FIXME: Not handled correctly.  */
381   HOWTO (R_MIPS_GOT_DISP,       /* type */
382          0,                     /* rightshift */
383          2,                     /* size (0 = byte, 1 = short, 2 = long) */
384          16,                    /* bitsize */
385          false,                 /* pc_relative */
386          0,                     /* bitpos */
387          complain_overflow_bitfield, /* complain_on_overflow */
388          bfd_elf_generic_reloc, /* special_function */
389          "R_MIPS_GOT_DISP",     /* name */
390          true,                  /* partial_inplace */
391          0x0000ffff,            /* src_mask */
392          0x0000ffff,            /* dst_mask */
393          false),                /* pcrel_offset */
394
395   /* Displacement to page pointer in the global offset table.  */
396   /* FIXME: Not handled correctly.  */
397   HOWTO (R_MIPS_GOT_PAGE,       /* type */
398          0,                     /* rightshift */
399          2,                     /* size (0 = byte, 1 = short, 2 = long) */
400          16,                    /* bitsize */
401          false,                 /* pc_relative */
402          0,                     /* bitpos */
403          complain_overflow_bitfield, /* complain_on_overflow */
404          bfd_elf_generic_reloc, /* special_function */
405          "R_MIPS_GOT_PAGE",     /* name */
406          true,                  /* partial_inplace */
407          0x0000ffff,            /* src_mask */
408          0x0000ffff,            /* dst_mask */
409          false),                /* pcrel_offset */
410
411   /* Offset from page pointer in the global offset table.  */
412   /* FIXME: Not handled correctly.  */
413   HOWTO (R_MIPS_GOT_OFST,       /* type */
414          0,                     /* rightshift */
415          2,                     /* size (0 = byte, 1 = short, 2 = long) */
416          16,                    /* bitsize */
417          false,                 /* pc_relative */
418          0,                     /* bitpos */
419          complain_overflow_bitfield, /* complain_on_overflow */
420          bfd_elf_generic_reloc, /* special_function */
421          "R_MIPS_GOT_OFST",     /* name */
422          true,                  /* partial_inplace */
423          0x0000ffff,            /* src_mask */
424          0x0000ffff,            /* dst_mask */
425          false),                /* pcrel_offset */
426
427   /* High 16 bits of displacement in global offset table.  */
428   /* FIXME: Not handled correctly.  */
429   HOWTO (R_MIPS_GOT_HI16,       /* type */
430          0,                     /* rightshift */
431          2,                     /* size (0 = byte, 1 = short, 2 = long) */
432          16,                    /* bitsize */
433          false,                 /* pc_relative */
434          0,                     /* bitpos */
435          complain_overflow_dont, /* complain_on_overflow */
436          bfd_elf_generic_reloc, /* special_function */
437          "R_MIPS_GOT_HI16",     /* name */
438          true,                  /* partial_inplace */
439          0x0000ffff,            /* src_mask */
440          0x0000ffff,            /* dst_mask */
441          false),                /* pcrel_offset */
442
443   /* Low 16 bits of displacement in global offset table.  */
444   /* FIXME: Not handled correctly.  */
445   HOWTO (R_MIPS_GOT_LO16,       /* 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_dont, /* complain_on_overflow */
452          bfd_elf_generic_reloc, /* special_function */
453          "R_MIPS_GOT_LO16",     /* name */
454          true,                  /* partial_inplace */
455          0x0000ffff,            /* src_mask */
456          0x0000ffff,            /* dst_mask */
457          false),                /* pcrel_offset */
458
459   /* 64 bit subtraction.  Presumably not used in 32 bit ELF.  */
460   { R_MIPS_SUB },
461
462   /* Used to cause the linker to insert and delete instructions?  */
463   { R_MIPS_INSERT_A },
464   { R_MIPS_INSERT_B },
465   { R_MIPS_DELETE },
466
467   /* Get the higher values of a 64 bit addend.  Presumably not used in
468      32 bit ELF.  */
469   { R_MIPS_HIGHER },
470   { R_MIPS_HIGHEST },
471
472   /* High 16 bits of displacement in global offset table.  */
473   /* FIXME: Not handled correctly.  */
474   HOWTO (R_MIPS_CALL_HI16,      /* type */
475          0,                     /* rightshift */
476          2,                     /* size (0 = byte, 1 = short, 2 = long) */
477          16,                    /* bitsize */
478          false,                 /* pc_relative */
479          0,                     /* bitpos */
480          complain_overflow_dont, /* complain_on_overflow */
481          bfd_elf_generic_reloc, /* special_function */
482          "R_MIPS_CALL_HI16",    /* name */
483          true,                  /* partial_inplace */
484          0x0000ffff,            /* src_mask */
485          0x0000ffff,            /* dst_mask */
486          false),                /* pcrel_offset */
487
488   /* Low 16 bits of displacement in global offset table.  */
489   /* FIXME: Not handled correctly.  */
490   HOWTO (R_MIPS_CALL_LO16,      /* type */
491          0,                     /* rightshift */
492          2,                     /* size (0 = byte, 1 = short, 2 = long) */
493          16,                    /* bitsize */
494          false,                 /* pc_relative */
495          0,                     /* bitpos */
496          complain_overflow_dont, /* complain_on_overflow */
497          bfd_elf_generic_reloc, /* special_function */
498          "R_MIPS_CALL_LO16",    /* name */
499          true,                  /* partial_inplace */
500          0x0000ffff,            /* src_mask */
501          0x0000ffff,            /* dst_mask */
502          false)                 /* pcrel_offset */
503 };
504
505 /* Do a R_MIPS_HI16 relocation.  This has to be done in combination
506    with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
507    the HI16.  Here we just save the information we need; we do the
508    actual relocation when we see the LO16.  MIPS ELF requires that the
509    LO16 immediately follow the HI16, so this ought to work.  */
510
511 static bfd_byte *mips_hi16_addr;
512 static bfd_vma mips_hi16_addend;
513
514 static bfd_reloc_status_type
515 mips_elf_hi16_reloc (abfd,
516                      reloc_entry,
517                      symbol,
518                      data,
519                      input_section,
520                      output_bfd,
521                      error_message)
522      bfd *abfd;
523      arelent *reloc_entry;
524      asymbol *symbol;
525      PTR data;
526      asection *input_section;
527      bfd *output_bfd;
528      char **error_message;
529 {
530   bfd_reloc_status_type ret;
531   bfd_vma relocation;
532
533   /* If we're relocating, and this an external symbol, we don't want
534      to change anything.  */
535   if (output_bfd != (bfd *) NULL
536       && (symbol->flags & BSF_SECTION_SYM) == 0
537       && reloc_entry->addend == 0)
538     {
539       reloc_entry->address += input_section->output_offset;
540       return bfd_reloc_ok;
541     }
542
543   /* FIXME: The symbol _gp_disp requires special handling, which we do
544      not do.  */
545   if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
546     abort ();
547
548   ret = bfd_reloc_ok;
549   if (bfd_is_und_section (symbol->section)
550       && output_bfd == (bfd *) NULL)
551     ret = bfd_reloc_undefined;
552
553   if (bfd_is_com_section (symbol->section))
554     relocation = 0;
555   else
556     relocation = symbol->value;
557
558   relocation += symbol->section->output_section->vma;
559   relocation += symbol->section->output_offset;
560   relocation += reloc_entry->addend;
561
562   if (reloc_entry->address > input_section->_cooked_size)
563     return bfd_reloc_outofrange;
564
565   /* Save the information, and let LO16 do the actual relocation.  */
566   mips_hi16_addr = (bfd_byte *) data + reloc_entry->address;
567   mips_hi16_addend = relocation;
568
569   if (output_bfd != (bfd *) NULL)
570     reloc_entry->address += input_section->output_offset;
571
572   return ret;
573 }
574
575 /* Do a R_MIPS_LO16 relocation.  This is a straightforward 16 bit
576    inplace relocation; this function exists in order to do the
577    R_MIPS_HI16 relocation described above.  */
578
579 static bfd_reloc_status_type
580 mips_elf_lo16_reloc (abfd,
581                      reloc_entry,
582                      symbol,
583                      data,
584                      input_section,
585                      output_bfd,
586                      error_message)
587      bfd *abfd;
588      arelent *reloc_entry;
589      asymbol *symbol;
590      PTR data;
591      asection *input_section;
592      bfd *output_bfd;
593      char **error_message;
594 {
595   /* FIXME: The symbol _gp_disp requires special handling, which we do
596      not do.  */
597   if (output_bfd == (bfd *) NULL
598       && strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
599     abort ();
600
601   if (mips_hi16_addr != (bfd_byte *) NULL)
602     {
603       unsigned long insn;
604       unsigned long val;
605       unsigned long vallo;
606
607       /* Do the HI16 relocation.  Note that we actually don't need to
608          know anything about the LO16 itself, except where to find the
609          low 16 bits of the addend needed by the LO16.  */
610       insn = bfd_get_32 (abfd, mips_hi16_addr);
611       vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
612                & 0xffff);
613       val = ((insn & 0xffff) << 16) + vallo;
614       val += mips_hi16_addend;
615
616       /* The low order 16 bits are always treated as a signed value.
617          Therefore, a negative value in the low order bits requires an
618          adjustment in the high order bits.  We need to make this
619          adjustment in two ways: once for the bits we took from the
620          data, and once for the bits we are putting back in to the
621          data.  */
622       if ((vallo & 0x8000) != 0)
623         val -= 0x10000;
624       if ((val & 0x8000) != 0)
625         val += 0x10000;
626
627       insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
628       bfd_put_32 (abfd, insn, mips_hi16_addr);
629
630       mips_hi16_addr = (bfd_byte *) NULL;
631     }
632
633   /* Now do the LO16 reloc in the usual way.  */
634   return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
635                                 input_section, output_bfd, error_message);
636 }
637
638 /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
639    table used for PIC code.  If the symbol is an external symbol, the
640    instruction is modified to contain the offset of the appropriate
641    entry in the global offset table.  If the symbol is a section
642    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
643    addends are combined to form the real addend against the section
644    symbol; the GOT16 is modified to contain the offset of an entry in
645    the global offset table, and the LO16 is modified to offset it
646    appropriately.  Thus an offset larger than 16 bits requires a
647    modified value in the global offset table.
648
649    This implementation suffices for the assembler, but the linker does
650    not yet know how to create global offset tables.  */
651
652 static bfd_reloc_status_type
653 mips_elf_got16_reloc (abfd,
654                       reloc_entry,
655                       symbol,
656                       data,
657                       input_section,
658                       output_bfd,
659                       error_message)
660      bfd *abfd;
661      arelent *reloc_entry;
662      asymbol *symbol;
663      PTR data;
664      asection *input_section;
665      bfd *output_bfd;
666      char **error_message;
667 {
668   /* If we're relocating, and this an external symbol, we don't want
669      to change anything.  */
670   if (output_bfd != (bfd *) NULL
671       && (symbol->flags & BSF_SECTION_SYM) == 0
672       && reloc_entry->addend == 0)
673     {
674       reloc_entry->address += input_section->output_offset;
675       return bfd_reloc_ok;
676     }
677
678   /* If we're relocating, and this is a local symbol, we can handle it
679      just like HI16.  */
680   if (output_bfd != (bfd *) NULL
681       && (symbol->flags & BSF_SECTION_SYM) != 0)
682     return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
683                                 input_section, output_bfd, error_message);
684
685   abort ();
686 }
687
688 /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
689    become the offset from the gp register.  This function also handles
690    R_MIPS_LITERAL relocations, although those can be handled more
691    cleverly because the entries in the .lit8 and .lit4 sections can be
692    merged.  */
693
694 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
695                                                       arelent *, asection *,
696                                                       boolean, PTR, bfd_vma));
697
698 static bfd_reloc_status_type
699 mips_elf_gprel16_reloc (abfd,
700                         reloc_entry,
701                         symbol,
702                         data,
703                         input_section,
704                         output_bfd,
705                         error_message)
706      bfd *abfd;
707      arelent *reloc_entry;
708      asymbol *symbol;
709      PTR data;
710      asection *input_section;
711      bfd *output_bfd;
712      char **error_message;
713 {
714   boolean relocateable;
715
716   /* If we're relocating, and this is an external symbol with no
717      addend, we don't want to change anything.  We will only have an
718      addend if this is a newly created reloc, not read from an ELF
719      file.  */
720   if (output_bfd != (bfd *) NULL
721       && (symbol->flags & BSF_SECTION_SYM) == 0
722       && reloc_entry->addend == 0)
723     {
724       reloc_entry->address += input_section->output_offset;
725       return bfd_reloc_ok;
726     }
727
728   if (output_bfd != (bfd *) NULL)
729     relocateable = true;
730   else
731     {
732       relocateable = false;
733       output_bfd = symbol->section->output_section->owner;
734     }
735
736   if (bfd_is_und_section (symbol->section)
737       && relocateable == false)
738     return bfd_reloc_undefined;
739
740   /* Some of the code below assumes the output bfd is ELF too.  */
741   if (output_bfd->xvec->flavour != bfd_target_elf_flavour)
742     abort ();
743
744   /* We have to figure out the gp value, so that we can adjust the
745      symbol value correctly.  We look up the symbol _gp in the output
746      BFD.  If we can't find it, we're stuck.  We cache it in the ELF
747      target data.  We don't need to adjust the symbol value for an
748      external symbol if we are producing relocateable output.  */
749   if (elf_gp (output_bfd) == 0
750       && (relocateable == false
751           || (symbol->flags & BSF_SECTION_SYM) != 0))
752     {
753       if (relocateable != false)
754         {
755           /* Make up a value.  */
756           elf_gp (output_bfd) =
757             symbol->section->output_section->vma + 0x4000;
758         }
759       else
760         {
761           unsigned int count;
762           asymbol **sym;
763           unsigned int i;
764
765           count = bfd_get_symcount (output_bfd);
766           sym = bfd_get_outsymbols (output_bfd);
767
768           if (sym == (asymbol **) NULL)
769             i = count;
770           else
771             {
772               for (i = 0; i < count; i++, sym++)
773                 {
774                   register CONST char *name;
775
776                   name = bfd_asymbol_name (*sym);
777                   if (*name == '_' && strcmp (name, "_gp") == 0)
778                     {
779                       elf_gp (output_bfd) = bfd_asymbol_value (*sym);
780                       break;
781                     }
782                 }
783             }
784
785           if (i >= count)
786             {
787               /* Only get the error once.  */
788               elf_gp (output_bfd) = 4;
789               *error_message =
790                 (char *) "GP relative relocation when _gp not defined";
791               return bfd_reloc_dangerous;
792             }
793         }
794     }
795
796   return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
797                           relocateable, data, elf_gp (output_bfd));
798 }
799
800 static bfd_reloc_status_type
801 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
802                  gp)
803      bfd *abfd;
804      asymbol *symbol;
805      arelent *reloc_entry;
806      asection *input_section;
807      boolean relocateable;
808      PTR data;
809      bfd_vma gp;
810 {
811   bfd_vma relocation;
812   unsigned long insn;
813   unsigned long val;
814
815   if (bfd_is_com_section (symbol->section))
816     relocation = 0;
817   else
818     relocation = symbol->value;
819
820   relocation += symbol->section->output_section->vma;
821   relocation += symbol->section->output_offset;
822
823   if (reloc_entry->address > input_section->_cooked_size)
824     return bfd_reloc_outofrange;
825
826   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
827
828   /* Set val to the offset into the section or symbol.  */
829   val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
830   if (val & 0x8000)
831     val -= 0x10000;
832
833   /* Adjust val for the final section location and GP value.  If we
834      are producing relocateable output, we don't want to do this for
835      an external symbol.  */
836   if (relocateable == false
837       || (symbol->flags & BSF_SECTION_SYM) != 0)
838     val += relocation - gp;
839
840   insn = (insn &~ 0xffff) | (val & 0xffff);
841   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
842
843   if (relocateable != false)
844     reloc_entry->address += input_section->output_offset;
845
846   /* Make sure it fit in 16 bits.  */
847   if (val >= 0x8000 && val < 0xffff8000)
848     return bfd_reloc_overflow;
849
850   return bfd_reloc_ok;
851 }
852
853 /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
854
855 struct elf_reloc_map {
856   bfd_reloc_code_real_type bfd_reloc_val;
857   enum reloc_type elf_reloc_val;
858 };
859
860 static CONST struct elf_reloc_map mips_reloc_map[] =
861 {
862   { BFD_RELOC_NONE, R_MIPS_NONE, },
863   { BFD_RELOC_16, R_MIPS_16 },
864   { BFD_RELOC_32, R_MIPS_32 },
865   { BFD_RELOC_CTOR, R_MIPS_32 },
866   { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
867   { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
868   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
869   { BFD_RELOC_LO16, R_MIPS_LO16 },
870   { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
871   { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
872   { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
873   { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
874   { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
875   { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }
876 };
877
878 /* Given a BFD reloc type, return a howto structure.  */
879
880 static reloc_howto_type *
881 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
882      bfd *abfd;
883      bfd_reloc_code_real_type code;
884 {
885   unsigned int i;
886
887   for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
888     {
889       if (mips_reloc_map[i].bfd_reloc_val == code)
890         return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
891     }
892   return NULL;
893 }
894
895 /* Given a MIPS reloc type, fill in an arelent structure.  */
896
897 static void
898 mips_info_to_howto_rel (abfd, cache_ptr, dst)
899      bfd *abfd;
900      arelent *cache_ptr;
901      Elf32_Internal_Rel *dst;
902 {
903   unsigned int r_type;
904
905   r_type = ELF32_R_TYPE (dst->r_info);
906   BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
907   cache_ptr->howto = &elf_mips_howto_table[r_type];
908
909   /* The addend for a GPREL16 or LITERAL relocation comes from the GP
910      value for the object file.  We get the addend now, rather than
911      when we do the relocation, because the symbol manipulations done
912      by the linker may cause us to lose track of the input BFD.  */
913   if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
914       && (r_type == (unsigned int) R_MIPS_GPREL16
915           || r_type == (unsigned int) R_MIPS_LITERAL))
916     cache_ptr->addend = elf_gp (abfd);
917 }
918 \f
919 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
920    routines swap this structure in and out.  They are used outside of
921    BFD, so they are globally visible.  */
922
923 void
924 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
925      bfd *abfd;
926      const Elf32_External_RegInfo *ex;
927      Elf32_RegInfo *in;
928 {
929   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
930   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
931   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
932   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
933   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
934   in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
935 }
936
937 void
938 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
939      bfd *abfd;
940      const Elf32_RegInfo *in;
941      Elf32_External_RegInfo *ex;
942 {
943   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
944                 (bfd_byte *) ex->ri_gprmask);
945   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
946                 (bfd_byte *) ex->ri_cprmask[0]);
947   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
948                 (bfd_byte *) ex->ri_cprmask[1]);
949   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
950                 (bfd_byte *) ex->ri_cprmask[2]);
951   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
952                 (bfd_byte *) ex->ri_cprmask[3]);
953   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
954                 (bfd_byte *) ex->ri_gp_value);
955 }
956
957 /* Swap an entry in a .gptab section.  Note that these routines rely
958    on the equivalence of the two elements of the union.  */
959
960 static void
961 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
962      bfd *abfd;
963      const Elf32_External_gptab *ex;
964      Elf32_gptab *in;
965 {
966   in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
967   in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
968 }
969
970 static void
971 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
972      bfd *abfd;
973      const Elf32_gptab *in;
974      Elf32_External_gptab *ex;
975 {
976   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
977                 ex->gt_entry.gt_g_value);
978   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
979                 ex->gt_entry.gt_bytes);
980 }
981 \f
982 /* Determine whether a symbol is global for the purposes of splitting
983    the symbol table into global symbols and local symbols.  At least
984    on Irix 5, this split must be between section symbols and all other
985    symbols.  On most ELF targets the split is between static symbols
986    and externally visible symbols.  */
987
988 /*ARGSUSED*/
989 static boolean
990 mips_elf_sym_is_global (abfd, sym)
991      bfd *abfd;
992      asymbol *sym;
993 {
994   return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
995 }
996 \f
997 /* Set the right machine number for a MIPS ELF file.  */
998
999 static boolean
1000 mips_elf_object_p (abfd)
1001      bfd *abfd;
1002 {
1003   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
1004     {
1005     default:
1006     case E_MIPS_ARCH_1:
1007       /* Just use the default, which was set in elfcode.h.  */
1008       break;
1009
1010     case E_MIPS_ARCH_2:
1011       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
1012       break;
1013
1014     case E_MIPS_ARCH_3:
1015       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
1016       break;
1017     }
1018
1019   /* Irix 5 is broken.  Object file symbol tables are not always
1020      sorted correctly such that local symbols precede global symbols,
1021      and the sh_info field in the symbol table is not always right.  */
1022   elf_bad_symtab (abfd) = true;
1023
1024   return true;
1025 }
1026
1027 /* The final processing done just before writing out a MIPS ELF object
1028    file.  This gets the MIPS architecture right based on the machine
1029    number.  */
1030
1031 /*ARGSUSED*/
1032 static void
1033 mips_elf_final_write_processing (abfd, linker)
1034      bfd *abfd;
1035      boolean linker;
1036 {
1037   unsigned long val;
1038   unsigned int i;
1039   Elf_Internal_Shdr **hdrpp;
1040
1041   switch (bfd_get_mach (abfd))
1042     {
1043     case 3000:
1044       val = E_MIPS_ARCH_1;
1045       break;
1046
1047     case 6000:
1048       val = E_MIPS_ARCH_2;
1049       break;
1050
1051     case 4000:
1052       val = E_MIPS_ARCH_3;
1053       break;
1054
1055     default:
1056       return;
1057     }
1058
1059   elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1060   elf_elfheader (abfd)->e_flags |= val;
1061
1062   /* Set the sh_info field for .gptab sections.  */
1063   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1064        i < elf_elfheader (abfd)->e_shnum;
1065        i++, hdrpp++)
1066     {
1067       if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
1068         {
1069           const char *name;
1070           asection *sec;
1071
1072           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1073           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1074           BFD_ASSERT (name != NULL
1075                       && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1076           sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1077           BFD_ASSERT (sec != NULL);
1078           (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1079         }
1080     }
1081 }
1082 \f
1083 /* Handle a MIPS specific section when reading an object file.  This
1084    is called when elfcode.h finds a section with an unknown type.
1085    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1086    how to.  */
1087
1088 static boolean
1089 mips_elf_section_from_shdr (abfd, hdr, name)
1090      bfd *abfd;
1091      Elf32_Internal_Shdr *hdr;
1092      char *name;
1093 {
1094   asection *newsect;
1095
1096   /* There ought to be a place to keep ELF backend specific flags, but
1097      at the moment there isn't one.  We just keep track of the
1098      sections by their name, instead.  Fortunately, the ABI gives
1099      suggested names for all the MIPS specific sections, so we will
1100      probably get away with this.  */
1101   switch (hdr->sh_type)
1102     {
1103     case SHT_MIPS_LIBLIST:
1104       if (strcmp (name, ".liblist") != 0)
1105         return false;
1106       break;
1107     case SHT_MIPS_MSYM:
1108       if (strcmp (name, ".msym") != 0)
1109         return false;
1110       break;
1111     case SHT_MIPS_CONFLICT:
1112       if (strcmp (name, ".conflict") != 0)
1113         return false;
1114       break;
1115     case SHT_MIPS_GPTAB:
1116       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
1117         return false;
1118       break;
1119     case SHT_MIPS_UCODE:
1120       if (strcmp (name, ".ucode") != 0)
1121         return false;
1122       break;
1123     case SHT_MIPS_DEBUG:
1124       if (strcmp (name, ".mdebug") != 0)
1125         return false;
1126       break;
1127     case SHT_MIPS_REGINFO:
1128       if (strcmp (name, ".reginfo") != 0
1129           || hdr->sh_size != sizeof (Elf32_External_RegInfo))
1130         return false;
1131       break;
1132     case SHT_MIPS_OPTIONS:
1133       if (strcmp (name, ".options") != 0)
1134         return false;
1135       break;
1136     case SHT_MIPS_DWARF:
1137       if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
1138         return false;
1139       break;
1140     case SHT_MIPS_EVENTS:
1141       if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1142         return false;
1143       break;
1144     default:
1145       return false;
1146     }
1147
1148   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1149     return false;
1150   newsect = hdr->bfd_section;
1151
1152   if (hdr->sh_type == SHT_MIPS_DEBUG)
1153     {
1154       if (! bfd_set_section_flags (abfd, newsect,
1155                                    (bfd_get_section_flags (abfd, newsect)
1156                                     | SEC_DEBUGGING)))
1157         return false;
1158     }
1159
1160   /* FIXME: We should record sh_info for a .gptab section.  */
1161
1162   /* For a .reginfo section, set the gp value in the tdata information
1163      from the contents of this section.  We need the gp value while
1164      processing relocs, so we just get it now.  */
1165   if (hdr->sh_type == SHT_MIPS_REGINFO)
1166     {
1167       Elf32_External_RegInfo ext;
1168       Elf32_RegInfo s;
1169
1170       if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1171                                       (file_ptr) 0, sizeof ext))
1172         return false;
1173       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
1174       elf_gp (abfd) = s.ri_gp_value;
1175     }
1176
1177   return true;
1178 }
1179
1180 /* Set the correct type for a MIPS ELF section.  We do this by the
1181    section name, which is a hack, but ought to work.  */
1182
1183 static boolean
1184 mips_elf_fake_sections (abfd, hdr, sec)
1185      bfd *abfd;
1186      Elf32_Internal_Shdr *hdr;
1187      asection *sec;
1188 {
1189   register const char *name;
1190
1191   name = bfd_get_section_name (abfd, sec);
1192
1193   if (strcmp (name, ".liblist") == 0)
1194     {
1195       hdr->sh_type = SHT_MIPS_LIBLIST;
1196       hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
1197       /* FIXME: Set the sh_link field.  */
1198     }
1199   else if (strcmp (name, ".msym") == 0)
1200     {
1201       hdr->sh_type = SHT_MIPS_MSYM;
1202       hdr->sh_entsize = 8;
1203       /* FIXME: Set the sh_info field.  */
1204     }
1205   else if (strcmp (name, ".conflict") == 0)
1206     hdr->sh_type = SHT_MIPS_CONFLICT;
1207   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
1208     {
1209       hdr->sh_type = SHT_MIPS_GPTAB;
1210       hdr->sh_entsize = sizeof (Elf32_External_gptab);
1211       /* The sh_info field is set in mips_elf_final_write_processing.  */
1212     }
1213   else if (strcmp (name, ".ucode") == 0)
1214     hdr->sh_type = SHT_MIPS_UCODE;
1215   else if (strcmp (name, ".mdebug") == 0)
1216     {
1217       hdr->sh_type = SHT_MIPS_DEBUG;
1218       hdr->sh_entsize = 1;
1219     }
1220   else if (strcmp (name, ".reginfo") == 0)
1221     {
1222       hdr->sh_type = SHT_MIPS_REGINFO;
1223       hdr->sh_entsize = 1;
1224
1225       /* Force the section size to the correct value, even if the
1226          linker thinks it is larger.  The link routine below will only
1227          write out this much data for .reginfo.  */
1228       hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
1229     }
1230   else if (strcmp (name, ".options") == 0)
1231     {
1232       hdr->sh_type = SHT_MIPS_OPTIONS;
1233       hdr->sh_entsize = 1;
1234     }
1235   else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
1236     hdr->sh_type = SHT_MIPS_DWARF;
1237   else if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1238     hdr->sh_type = SHT_MIPS_EVENTS;
1239
1240   return true;
1241 }
1242
1243 /* Given a BFD section, try to locate the corresponding ELF section
1244    index.  */
1245
1246 static boolean
1247 mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
1248      bfd *abfd;
1249      Elf32_Internal_Shdr *hdr;
1250      asection *sec;
1251      int *retval;
1252 {
1253   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1254     {
1255       *retval = SHN_MIPS_SCOMMON;
1256       return true;
1257     }
1258   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
1259     {
1260       *retval = SHN_MIPS_ACOMMON;
1261       return true;
1262     }
1263   return false;
1264 }
1265
1266 /* Work over a section just before writing it out.  We update the GP
1267    value in the .reginfo section based on the value we are using.
1268    FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1269    name; there has to be a better way.  */
1270
1271 static boolean
1272 mips_elf_section_processing (abfd, hdr)
1273      bfd *abfd;
1274      Elf32_Internal_Shdr *hdr;
1275 {
1276   if (hdr->sh_type == SHT_MIPS_REGINFO)
1277     {
1278       bfd_byte buf[4];
1279
1280       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
1281       BFD_ASSERT (hdr->contents == NULL);
1282
1283       if (bfd_seek (abfd,
1284                     hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
1285                     SEEK_SET) == -1)
1286         return false;
1287       bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
1288       if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
1289         return false;
1290     }
1291
1292   if (hdr->bfd_section != NULL)
1293     {
1294       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
1295
1296       if (strcmp (name, ".sdata") == 0)
1297         {
1298           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1299           hdr->sh_type = SHT_PROGBITS;
1300         }
1301       else if (strcmp (name, ".sbss") == 0)
1302         {
1303           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1304           hdr->sh_type = SHT_NOBITS;
1305         }
1306       else if (strcmp (name, ".lit8") == 0
1307                || strcmp (name, ".lit4") == 0)
1308         {
1309           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1310           hdr->sh_type = SHT_PROGBITS;
1311         }
1312     }
1313
1314   return true;
1315 }
1316 \f
1317 /* MIPS ELF uses two common sections.  One is the usual one, and the
1318    other is for small objects.  All the small objects are kept
1319    together, and then referenced via the gp pointer, which yields
1320    faster assembler code.  This is what we use for the small common
1321    section.  This approach is copied from ecoff.c.  */
1322 static asection mips_elf_scom_section;
1323 static asymbol mips_elf_scom_symbol;
1324 static asymbol *mips_elf_scom_symbol_ptr;
1325
1326 /* MIPS ELF also uses an acommon section, which represents an
1327    allocated common symbol which may be overridden by a         
1328    definition in a shared library.  */
1329 static asection mips_elf_acom_section;
1330 static asymbol mips_elf_acom_symbol;
1331 static asymbol *mips_elf_acom_symbol_ptr;
1332
1333 /* Handle the special MIPS section numbers that a symbol may use.  */
1334
1335 static void
1336 mips_elf_symbol_processing (abfd, asym)
1337      bfd *abfd;
1338      asymbol *asym;
1339 {
1340   elf_symbol_type *elfsym;
1341
1342   elfsym = (elf_symbol_type *) asym;
1343   switch (elfsym->internal_elf_sym.st_shndx)
1344     {
1345     case SHN_MIPS_ACOMMON:
1346       /* This section is used in a dynamically linked executable file.
1347          It is an allocated common section.  The dynamic linker can
1348          either resolve these symbols to something in a shared
1349          library, or it can just leave them here.  For our purposes,
1350          we can consider these symbols to be in a new section.  */
1351       if (mips_elf_acom_section.name == NULL)
1352         {
1353           /* Initialize the acommon section.  */
1354           mips_elf_acom_section.name = ".acommon";
1355           mips_elf_acom_section.flags = SEC_ALLOC;
1356           mips_elf_acom_section.output_section = &mips_elf_acom_section;
1357           mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
1358           mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
1359           mips_elf_acom_symbol.name = ".acommon";
1360           mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
1361           mips_elf_acom_symbol.section = &mips_elf_acom_section;
1362           mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
1363         }
1364       asym->section = &mips_elf_acom_section;
1365       break;
1366
1367     case SHN_COMMON:
1368       /* Common symbols less than the GP size are automatically
1369          treated as SHN_MIPS_SCOMMON symbols.  */
1370       if (asym->value > elf_gp_size (abfd))
1371         break;
1372       /* Fall through.  */
1373     case SHN_MIPS_SCOMMON:
1374       if (mips_elf_scom_section.name == NULL)
1375         {
1376           /* Initialize the small common section.  */
1377           mips_elf_scom_section.name = ".scommon";
1378           mips_elf_scom_section.flags = SEC_IS_COMMON;
1379           mips_elf_scom_section.output_section = &mips_elf_scom_section;
1380           mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
1381           mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
1382           mips_elf_scom_symbol.name = ".scommon";
1383           mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
1384           mips_elf_scom_symbol.section = &mips_elf_scom_section;
1385           mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
1386         }
1387       asym->section = &mips_elf_scom_section;
1388       asym->value = elfsym->internal_elf_sym.st_size;
1389       break;
1390
1391     case SHN_MIPS_SUNDEFINED:
1392       asym->section = bfd_und_section_ptr;
1393       break;
1394     }
1395 }
1396 \f
1397 /* Read ECOFF debugging information from a .mdebug section into a
1398    ecoff_debug_info structure.  */
1399
1400 static boolean
1401 mips_elf_read_ecoff_info (abfd, section, debug)
1402      bfd *abfd;
1403      asection *section;
1404      struct ecoff_debug_info *debug;
1405 {
1406   HDRR *symhdr;
1407   const struct ecoff_debug_swap *swap;
1408   char *ext_hdr = NULL;
1409
1410   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1411
1412   ext_hdr = (char *) malloc ((size_t) swap->external_hdr_size);
1413   if (ext_hdr == NULL && swap->external_hdr_size != 0)
1414     {
1415       bfd_set_error (bfd_error_no_memory);
1416       goto error_return;
1417     }
1418
1419   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1420                                 swap->external_hdr_size)
1421       == false)
1422     goto error_return;
1423
1424   symhdr = &debug->symbolic_header;
1425   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1426
1427   /* The symbolic header contains absolute file offsets and sizes to
1428      read.  */
1429 #define READ(ptr, offset, count, size, type)                            \
1430   if (symhdr->count == 0)                                               \
1431     debug->ptr = NULL;                                                  \
1432   else                                                                  \
1433     {                                                                   \
1434       debug->ptr = (type) malloc ((size_t) (size * symhdr->count));     \
1435       if (debug->ptr == NULL)                                           \
1436         {                                                               \
1437           bfd_set_error (bfd_error_no_memory);                          \
1438           goto error_return;                                            \
1439         }                                                               \
1440       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0     \
1441           || (bfd_read (debug->ptr, size, symhdr->count,                \
1442                         abfd) != size * symhdr->count))                 \
1443         goto error_return;                                              \
1444     }
1445
1446   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1447   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1448   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1449   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1450   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1451   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1452         union aux_ext *);
1453   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1454   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1455   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1456   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1457   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1458 #undef READ
1459
1460   debug->fdr = NULL;
1461   debug->adjust = NULL;
1462
1463   return true;
1464
1465  error_return:
1466   if (ext_hdr != NULL)
1467     free (ext_hdr);
1468   if (debug->line != NULL)
1469     free (debug->line);
1470   if (debug->external_dnr != NULL)
1471     free (debug->external_dnr);
1472   if (debug->external_pdr != NULL)
1473     free (debug->external_pdr);
1474   if (debug->external_sym != NULL)
1475     free (debug->external_sym);
1476   if (debug->external_opt != NULL)
1477     free (debug->external_opt);
1478   if (debug->external_aux != NULL)
1479     free (debug->external_aux);
1480   if (debug->ss != NULL)
1481     free (debug->ss);
1482   if (debug->ssext != NULL)
1483     free (debug->ssext);
1484   if (debug->external_fdr != NULL)
1485     free (debug->external_fdr);
1486   if (debug->external_rfd != NULL)
1487     free (debug->external_rfd);
1488   if (debug->external_ext != NULL)
1489     free (debug->external_ext);
1490   return false;
1491 }
1492 \f
1493 /* MIPS ELF local labels start with '$', not 'L'.  */
1494
1495 /*ARGSUSED*/
1496 static boolean
1497 mips_elf_is_local_label (abfd, symbol)
1498      bfd *abfd;
1499      asymbol *symbol;
1500 {
1501   return symbol->name[0] == '$';
1502 }
1503
1504 /* MIPS ELF uses a special find_nearest_line routine in order the
1505    handle the ECOFF debugging information.  */
1506
1507 struct mips_elf_find_line
1508 {
1509   struct ecoff_debug_info d;
1510   struct ecoff_find_line i;
1511 };
1512
1513 static boolean
1514 mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
1515                             functionname_ptr, line_ptr)
1516      bfd *abfd;
1517      asection *section;
1518      asymbol **symbols;
1519      bfd_vma offset;
1520      const char **filename_ptr;
1521      const char **functionname_ptr;
1522      unsigned int *line_ptr;
1523 {
1524   asection *msec;
1525
1526   msec = bfd_get_section_by_name (abfd, ".mdebug");
1527   if (msec != NULL)
1528     {
1529       flagword origflags;
1530       struct mips_elf_find_line *fi;
1531       const struct ecoff_debug_swap * const swap =
1532         get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1533
1534       /* If we are called during a link, mips_elf_final_link may have
1535          cleared the SEC_HAS_CONTENTS field.  We force it back on here
1536          if appropriate (which it normally will be).  */
1537       origflags = msec->flags;
1538       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
1539         msec->flags |= SEC_HAS_CONTENTS;
1540
1541       fi = elf_tdata (abfd)->find_line_info;
1542       if (fi == NULL)
1543         {
1544           bfd_size_type external_fdr_size;
1545           char *fraw_src;
1546           char *fraw_end;
1547           struct fdr *fdr_ptr;
1548
1549           fi = ((struct mips_elf_find_line *)
1550                 bfd_alloc (abfd, sizeof (struct mips_elf_find_line)));
1551           if (fi == NULL)
1552             {
1553               msec->flags = origflags;
1554               return false;
1555             }
1556
1557           memset (fi, 0, sizeof (struct mips_elf_find_line));
1558
1559           if (! mips_elf_read_ecoff_info (abfd, msec, &fi->d))
1560             {
1561               msec->flags = origflags;
1562               return false;
1563             }
1564
1565           /* Swap in the FDR information.  */
1566           fi->d.fdr = ((struct fdr *)
1567                        bfd_alloc (abfd,
1568                                   (fi->d.symbolic_header.ifdMax *
1569                                    sizeof (struct fdr))));
1570           if (fi->d.fdr == NULL)
1571             {
1572               msec->flags = origflags;
1573               return false;
1574             }
1575           external_fdr_size = swap->external_fdr_size;
1576           fdr_ptr = fi->d.fdr;
1577           fraw_src = (char *) fi->d.external_fdr;
1578           fraw_end = (fraw_src
1579                       + fi->d.symbolic_header.ifdMax * external_fdr_size);
1580           for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
1581             (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
1582
1583           elf_tdata (abfd)->find_line_info = fi;
1584
1585           /* Note that we don't bother to ever free this information.
1586              find_nearest_line is either called all the time, as in
1587              objdump -l, so the information should be saved, or it is
1588              rarely called, as in ld error messages, so the memory
1589              wasted is unimportant.  Still, it would probably be a
1590              good idea for free_cached_info to throw it away.  */
1591         }
1592
1593       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
1594                                   &fi->i, filename_ptr, functionname_ptr,
1595                                   line_ptr))
1596         {
1597           msec->flags = origflags;
1598           return true;
1599         }
1600
1601       msec->flags = origflags;
1602     }
1603
1604   /* Fall back on the generic ELF find_nearest_line routine.  */
1605
1606   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
1607                                      filename_ptr, functionname_ptr,
1608                                      line_ptr);
1609 }
1610 \f
1611 /* The MIPS ELF linker needs additional information for each symbol in
1612    the global hash table.  */
1613
1614 struct mips_elf_link_hash_entry
1615 {
1616   struct elf_link_hash_entry root;
1617
1618   /* External symbol information.  */
1619   EXTR esym;
1620 };
1621
1622 /* MIPS ELF linker hash table.  */
1623
1624 struct mips_elf_link_hash_table
1625 {
1626   struct elf_link_hash_table root;
1627 };
1628
1629 /* Look up an entry in a MIPS ELF linker hash table.  */
1630
1631 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
1632   ((struct mips_elf_link_hash_entry *)                                  \
1633    elf_link_hash_lookup (&(table)->root, (string), (create),            \
1634                          (copy), (follow)))
1635
1636 /* Traverse a MIPS ELF linker hash table.  */
1637
1638 #define mips_elf_link_hash_traverse(table, func, info)                  \
1639   (elf_link_hash_traverse                                               \
1640    (&(table)->root,                                                     \
1641     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
1642     (info)))
1643
1644 /* Get the MIPS ELF linker hash table from a link_info structure.  */
1645
1646 #define mips_elf_hash_table(p) \
1647   ((struct mips_elf_link_hash_table *) ((p)->hash))
1648
1649 static boolean mips_elf_output_extsym
1650   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
1651
1652 /* Create an entry in a MIPS ELF linker hash table.  */
1653
1654 static struct bfd_hash_entry *
1655 mips_elf_link_hash_newfunc (entry, table, string)
1656      struct bfd_hash_entry *entry;
1657      struct bfd_hash_table *table;
1658      const char *string;
1659 {
1660   struct mips_elf_link_hash_entry *ret =
1661     (struct mips_elf_link_hash_entry *) entry;
1662
1663   /* Allocate the structure if it has not already been allocated by a
1664      subclass.  */
1665   if (ret == (struct mips_elf_link_hash_entry *) NULL)
1666     ret = ((struct mips_elf_link_hash_entry *)
1667            bfd_hash_allocate (table,
1668                               sizeof (struct mips_elf_link_hash_entry)));
1669   if (ret == (struct mips_elf_link_hash_entry *) NULL)
1670     return (struct bfd_hash_entry *) ret;
1671
1672   /* Call the allocation method of the superclass.  */
1673   ret = ((struct mips_elf_link_hash_entry *)
1674          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1675                                      table, string));
1676   if (ret != (struct mips_elf_link_hash_entry *) NULL)
1677     {
1678       /* Set local fields.  */
1679       memset (&ret->esym, 0, sizeof (EXTR));
1680       /* We use -2 as a marker to indicate that the information has
1681          not been set.  -1 means there is no associated ifd.  */
1682       ret->esym.ifd = -2;
1683     }
1684
1685   return (struct bfd_hash_entry *) ret;
1686 }
1687
1688 /* Create a MIPS ELF linker hash table.  */
1689
1690 static struct bfd_link_hash_table *
1691 mips_elf_link_hash_table_create (abfd)
1692      bfd *abfd;
1693 {
1694   struct mips_elf_link_hash_table *ret;
1695
1696   ret = ((struct mips_elf_link_hash_table *)
1697          bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
1698   if (ret == (struct mips_elf_link_hash_table *) NULL)
1699     return NULL;
1700
1701   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1702                                        mips_elf_link_hash_newfunc))
1703     {
1704       bfd_release (abfd, ret);
1705       return NULL;
1706     }
1707
1708   return &ret->root.root;
1709 }
1710
1711 /* Hook called by the linker routine which adds symbols from an object
1712    file.  We must handle the special MIPS section numbers here.  */
1713
1714 /*ARGSUSED*/
1715 static boolean
1716 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1717      bfd *abfd;
1718      struct bfd_link_info *info;
1719      const Elf_Internal_Sym *sym;
1720      const char **namep;
1721      flagword *flagsp;
1722      asection **secp;
1723      bfd_vma *valp;
1724 {
1725   switch (sym->st_shndx)
1726     {
1727     case SHN_COMMON:
1728       /* Common symbols less than the GP size are automatically
1729          treated as SHN_MIPS_SCOMMON symbols.  */
1730       if (sym->st_size > elf_gp_size (abfd))
1731         break;
1732       /* Fall through.  */
1733     case SHN_MIPS_SCOMMON:
1734       *secp = bfd_make_section_old_way (abfd, ".scommon");
1735       (*secp)->flags |= SEC_IS_COMMON;
1736       *valp = sym->st_size;
1737       break;
1738
1739     case SHN_MIPS_SUNDEFINED:
1740       *secp = bfd_und_section_ptr;
1741       break;
1742     }
1743
1744   return true;
1745 }
1746
1747 /* Structure used to pass information to mips_elf_output_extsym.  */
1748
1749 struct extsym_info
1750 {
1751   bfd *abfd;
1752   struct bfd_link_info *info;
1753   struct ecoff_debug_info *debug;
1754   const struct ecoff_debug_swap *swap;
1755   boolean failed;
1756 };
1757
1758 /* This routine is used to write out ECOFF debugging external symbol
1759    information.  It is called via mips_elf_link_hash_traverse.  The
1760    ECOFF external symbol information must match the ELF external
1761    symbol information.  Unfortunately, at this point we don't know
1762    whether a symbol is required by reloc information, so the two
1763    tables may wind up being different.  We must sort out the external
1764    symbol information before we can set the final size of the .mdebug
1765    section, and we must set the size of the .mdebug section before we
1766    can relocate any sections, and we can't know which symbols are
1767    required by relocation until we relocate the sections.
1768    Fortunately, it is relatively unlikely that any symbol will be
1769    stripped but required by a reloc.  In particular, it can not happen
1770    when generating a final executable.  */
1771
1772 static boolean
1773 mips_elf_output_extsym (h, data)
1774      struct mips_elf_link_hash_entry *h;
1775      PTR data;
1776 {
1777   struct extsym_info *einfo = (struct extsym_info *) data;
1778   boolean strip;
1779
1780   if (h->root.indx == -2)
1781     strip = false;
1782   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1783             || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1784            && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1785            && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1786     strip = true;
1787   else if (einfo->info->strip == strip_all
1788            || (einfo->info->strip == strip_some
1789                && bfd_hash_lookup (einfo->info->keep_hash,
1790                                    h->root.root.root.string,
1791                                    false, false) == NULL))
1792     strip = true;
1793   else
1794     strip = false;
1795
1796   if (strip)
1797     return true;
1798
1799   if (h->esym.ifd == -2)
1800     {
1801       h->esym.jmptbl = 0;
1802       h->esym.cobol_main = 0;
1803       h->esym.weakext = 0;
1804       h->esym.reserved = 0;
1805       h->esym.ifd = ifdNil;
1806       h->esym.asym.value = 0;
1807       h->esym.asym.st = stGlobal;
1808
1809       if (h->root.root.type != bfd_link_hash_defined
1810           && h->root.root.type != bfd_link_hash_defweak)
1811         h->esym.asym.sc = scAbs;
1812       else
1813         {
1814           asection *output_section;
1815           const char *name;
1816
1817           output_section = h->root.root.u.def.section->output_section;
1818           name = bfd_section_name (output_section->owner, output_section);
1819         
1820           if (strcmp (name, ".text") == 0)
1821             h->esym.asym.sc = scText;
1822           else if (strcmp (name, ".data") == 0)
1823             h->esym.asym.sc = scData;
1824           else if (strcmp (name, ".sdata") == 0)
1825             h->esym.asym.sc = scSData;
1826           else if (strcmp (name, ".rodata") == 0
1827                    || strcmp (name, ".rdata") == 0)
1828             h->esym.asym.sc = scRData;
1829           else if (strcmp (name, ".bss") == 0)
1830             h->esym.asym.sc = scBss;
1831           else if (strcmp (name, ".sbss") == 0)
1832             h->esym.asym.sc = scSBss;
1833           else if (strcmp (name, ".init") == 0)
1834             h->esym.asym.sc = scInit;
1835           else if (strcmp (name, ".fini") == 0)
1836             h->esym.asym.sc = scFini;
1837           else
1838             h->esym.asym.sc = scAbs;
1839         }
1840
1841       h->esym.asym.reserved = 0;
1842       h->esym.asym.index = indexNil;
1843     }
1844
1845   if (h->root.root.type == bfd_link_hash_common)
1846     h->esym.asym.value = h->root.root.u.c.size;
1847   else if (h->root.root.type == bfd_link_hash_defined
1848            || h->root.root.type == bfd_link_hash_defweak)
1849     {
1850       asection *sec;
1851
1852       if (h->esym.asym.sc == scCommon)
1853         h->esym.asym.sc = scBss;
1854       else if (h->esym.asym.sc == scSCommon)
1855         h->esym.asym.sc = scSBss;
1856
1857       sec = h->root.root.u.def.section;
1858       h->esym.asym.value = (h->root.root.u.def.value
1859                             + sec->output_offset
1860                             + sec->output_section->vma);
1861     }
1862
1863   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1864                                       h->root.root.root.string,
1865                                       &h->esym))
1866     {
1867       einfo->failed = true;
1868       return false;
1869     }
1870
1871   return true;
1872 }
1873
1874 /* A comparison routine used to sort .gptab entries.  */
1875
1876 static int
1877 gptab_compare (p1, p2)
1878      const PTR p1;
1879      const PTR p2;
1880 {
1881   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
1882   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
1883
1884   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1885 }
1886
1887 /* We need to use a special link routine to handle the .reginfo and
1888    the .mdebug sections.  We need to merge all instances of these
1889    sections together, not write them all out sequentially.  */
1890
1891 static boolean
1892 mips_elf_final_link (abfd, info)
1893      bfd *abfd;
1894      struct bfd_link_info *info;
1895 {
1896   asection **secpp;
1897   asection *o;
1898   struct bfd_link_order *p;
1899   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
1900   Elf32_RegInfo reginfo;
1901   struct ecoff_debug_info debug;
1902   const struct ecoff_debug_swap *swap
1903     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1904   HDRR *symhdr = &debug.symbolic_header;
1905   PTR mdebug_handle = NULL;
1906
1907   /* Drop the .options section, since it has special semantics which I
1908      haven't bothered to figure out.  */
1909   for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
1910     {
1911       if (strcmp ((*secpp)->name, ".options") == 0)
1912         {
1913           for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
1914             if (p->type == bfd_indirect_link_order)
1915               p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
1916           (*secpp)->link_order_head = NULL;
1917           *secpp = (*secpp)->next;
1918           --abfd->section_count;
1919           break;
1920         }
1921     }
1922
1923   /* Go through the sections and collect the .reginfo and .mdebug
1924      information.  */
1925   reginfo_sec = NULL;
1926   mdebug_sec = NULL;
1927   gptab_data_sec = NULL;
1928   gptab_bss_sec = NULL;
1929   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
1930     {
1931       if (strcmp (o->name, ".reginfo") == 0)
1932         {
1933           memset (&reginfo, 0, sizeof reginfo);
1934
1935           /* We have found the .reginfo section in the output file.
1936              Look through all the link_orders comprising it and merge
1937              the information together.  */
1938           for (p = o->link_order_head;
1939                p != (struct bfd_link_order *) NULL;
1940                p = p->next)
1941             {
1942               asection *input_section;
1943               bfd *input_bfd;
1944               Elf32_External_RegInfo ext;
1945               Elf32_RegInfo sub;
1946
1947               if (p->type != bfd_indirect_link_order)
1948                 {
1949                   if (p->type == bfd_fill_link_order)
1950                     continue;
1951                   abort ();
1952                 }
1953
1954               input_section = p->u.indirect.section;
1955               input_bfd = input_section->owner;
1956
1957               /* The linker emulation code has probably clobbered the
1958                  size to be zero bytes.  */
1959               if (input_section->_raw_size == 0)
1960                 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
1961
1962               if (! bfd_get_section_contents (input_bfd, input_section,
1963                                               (PTR) &ext,
1964                                               (file_ptr) 0,
1965                                               sizeof ext))
1966                 return false;
1967
1968               bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
1969
1970               reginfo.ri_gprmask |= sub.ri_gprmask;
1971               reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
1972               reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
1973               reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
1974               reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
1975
1976               /* ri_gp_value is set by the function
1977                  mips_elf_section_processing when the section is
1978                  finally written out.  */
1979
1980               /* Hack: reset the SEC_HAS_CONTENTS flag so that
1981                  elf_link_input_bfd ignores this section.  */
1982               input_section->flags &=~ SEC_HAS_CONTENTS;
1983             }
1984
1985           /* Force the section size to the value we want.  */
1986           o->_raw_size = sizeof (Elf32_External_RegInfo);
1987
1988           /* Skip this section later on (I don't think this currently
1989              matters, but someday it might).  */
1990           o->link_order_head = (struct bfd_link_order *) NULL;
1991
1992           reginfo_sec = o;
1993         }
1994
1995       if (strcmp (o->name, ".mdebug") == 0)
1996         {
1997           struct extsym_info einfo;
1998
1999           /* We have found the .mdebug section in the output file.
2000              Look through all the link_orders comprising it and merge
2001              the information together.  */
2002           symhdr->magic = swap->sym_magic;
2003           /* FIXME: What should the version stamp be?  */
2004           symhdr->vstamp = 0;
2005           symhdr->ilineMax = 0;
2006           symhdr->cbLine = 0;
2007           symhdr->idnMax = 0;
2008           symhdr->ipdMax = 0;
2009           symhdr->isymMax = 0;
2010           symhdr->ioptMax = 0;
2011           symhdr->iauxMax = 0;
2012           symhdr->issMax = 0;
2013           symhdr->issExtMax = 0;
2014           symhdr->ifdMax = 0;
2015           symhdr->crfd = 0;
2016           symhdr->iextMax = 0;
2017
2018           /* We accumulate the debugging information itself in the
2019              debug_info structure.  */
2020           debug.line = NULL;
2021           debug.external_dnr = NULL;
2022           debug.external_pdr = NULL;
2023           debug.external_sym = NULL;
2024           debug.external_opt = NULL;
2025           debug.external_aux = NULL;
2026           debug.ss = NULL;
2027           debug.ssext = debug.ssext_end = NULL;
2028           debug.external_fdr = NULL;
2029           debug.external_rfd = NULL;
2030           debug.external_ext = debug.external_ext_end = NULL;
2031
2032           mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
2033           if (mdebug_handle == (PTR) NULL)
2034             return false;
2035
2036           for (p = o->link_order_head;
2037                p != (struct bfd_link_order *) NULL;
2038                p = p->next)
2039             {
2040               asection *input_section;
2041               bfd *input_bfd;
2042               const struct ecoff_debug_swap *input_swap;
2043               struct ecoff_debug_info input_debug;
2044               char *eraw_src;
2045               char *eraw_end;
2046
2047               if (p->type != bfd_indirect_link_order)
2048                 {
2049                   if (p->type == bfd_fill_link_order)
2050                     continue;
2051                   abort ();
2052                 }
2053
2054               input_section = p->u.indirect.section;
2055               input_bfd = input_section->owner;
2056
2057               if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
2058                   || (get_elf_backend_data (input_bfd)
2059                       ->elf_backend_ecoff_debug_swap) == NULL)
2060                 {
2061                   /* I don't know what a non MIPS ELF bfd would be
2062                      doing with a .mdebug section, but I don't really
2063                      want to deal with it.  */
2064                   continue;
2065                 }
2066
2067               input_swap = (get_elf_backend_data (input_bfd)
2068                             ->elf_backend_ecoff_debug_swap);
2069
2070               BFD_ASSERT (p->size == input_section->_raw_size);
2071
2072               /* The ECOFF linking code expects that we have already
2073                  read in the debugging information and set up an
2074                  ecoff_debug_info structure, so we do that now.  */
2075               if (! mips_elf_read_ecoff_info (input_bfd, input_section,
2076                                               &input_debug))
2077                 return false;
2078
2079               if (! (bfd_ecoff_debug_accumulate
2080                      (mdebug_handle, abfd, &debug, swap, input_bfd,
2081                       &input_debug, input_swap, info)))
2082                 return false;
2083
2084               /* Loop through the external symbols.  For each one with
2085                  interesting information, try to find the symbol in
2086                  the linker global hash table and save the information
2087                  for the output external symbols.  */
2088               eraw_src = input_debug.external_ext;
2089               eraw_end = (eraw_src
2090                           + (input_debug.symbolic_header.iextMax
2091                              * input_swap->external_ext_size));
2092               for (;
2093                    eraw_src < eraw_end;
2094                    eraw_src += input_swap->external_ext_size)
2095                 {
2096                   EXTR ext;
2097                   const char *name;
2098                   struct mips_elf_link_hash_entry *h;
2099
2100                   (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
2101                   if (ext.asym.sc == scNil
2102                       || ext.asym.sc == scUndefined
2103                       || ext.asym.sc == scSUndefined)
2104                     continue;
2105
2106                   name = input_debug.ssext + ext.asym.iss;
2107                   h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
2108                                                  name, false, false, true);
2109                   if (h == NULL || h->esym.ifd != -2)
2110                     continue;
2111
2112                   if (ext.ifd != -1)
2113                     {
2114                       BFD_ASSERT (ext.ifd
2115                                   < input_debug.symbolic_header.ifdMax);
2116                       ext.ifd = input_debug.ifdmap[ext.ifd];
2117                     }
2118
2119                   h->esym = ext;
2120                 }
2121
2122               /* Free up the information we just read.  */
2123               free (input_debug.line);
2124               free (input_debug.external_dnr);
2125               free (input_debug.external_pdr);
2126               free (input_debug.external_sym);
2127               free (input_debug.external_opt);
2128               free (input_debug.external_aux);
2129               free (input_debug.ss);
2130               free (input_debug.ssext);
2131               free (input_debug.external_fdr);
2132               free (input_debug.external_rfd);
2133               free (input_debug.external_ext);
2134
2135               /* Hack: reset the SEC_HAS_CONTENTS flag so that
2136                  elf_link_input_bfd ignores this section.  */
2137               input_section->flags &=~ SEC_HAS_CONTENTS;
2138             }
2139
2140           /* Build the external symbol information.  */
2141           einfo.abfd = abfd;
2142           einfo.info = info;
2143           einfo.debug = &debug;
2144           einfo.swap = swap;
2145           einfo.failed = false;
2146           mips_elf_link_hash_traverse (mips_elf_hash_table (info),
2147                                        mips_elf_output_extsym,
2148                                        (PTR) &einfo);
2149           if (einfo.failed)
2150             return false;
2151
2152           /* Set the size of the .mdebug section.  */
2153           o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
2154
2155           /* Skip this section later on (I don't think this currently
2156              matters, but someday it might).  */
2157           o->link_order_head = (struct bfd_link_order *) NULL;
2158
2159           mdebug_sec = o;
2160         }
2161
2162       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
2163         {
2164           const char *subname;
2165           unsigned int c;
2166           Elf32_gptab *tab;
2167           Elf32_External_gptab *ext_tab;
2168           unsigned int i;
2169
2170           /* The .gptab.sdata and .gptab.sbss sections hold
2171              information describing how the small data area would
2172              change depending upon the -G switch.  These sections
2173              not used in executables files.  */
2174           if (! info->relocateable)
2175             {
2176               asection **secpp;
2177
2178               for (p = o->link_order_head;
2179                    p != (struct bfd_link_order *) NULL;
2180                    p = p->next)
2181                 {
2182                   asection *input_section;
2183
2184                   if (p->type != bfd_indirect_link_order)
2185                     {
2186                       if (p->type == bfd_fill_link_order)
2187                         continue;
2188                       abort ();
2189                     }
2190
2191                   input_section = p->u.indirect.section;
2192
2193                   /* Hack: reset the SEC_HAS_CONTENTS flag so that
2194                      elf_link_input_bfd ignores this section.  */
2195                   input_section->flags &=~ SEC_HAS_CONTENTS;
2196                 }
2197
2198               /* Skip this section later on (I don't think this
2199                  currently matters, but someday it might).  */
2200               o->link_order_head = (struct bfd_link_order *) NULL;
2201
2202               /* Really remove the section.  */
2203               for (secpp = &abfd->sections;
2204                    *secpp != o;
2205                    secpp = &(*secpp)->next)
2206                 ;
2207               *secpp = (*secpp)->next;
2208               --abfd->section_count;
2209
2210               continue;
2211             }
2212
2213           /* There is one gptab for initialized data, and one for
2214              uninitialized data.  */
2215           if (strcmp (o->name, ".gptab.sdata") == 0)
2216             gptab_data_sec = o;
2217           else if (strcmp (o->name, ".gptab.sbss") == 0)
2218             gptab_bss_sec = o;
2219           else
2220             {
2221               (*_bfd_error_handler)
2222                 ("%s: illegal section name `%s'",
2223                  bfd_get_filename (abfd), o->name);
2224               bfd_set_error (bfd_error_nonrepresentable_section);
2225               return false;
2226             }
2227
2228           /* The linker script always combines .gptab.data and
2229              .gptab.sdata into .gptab.sdata, and likewise for
2230              .gptab.bss and .gptab.sbss.  It is possible that there is
2231              no .sdata or .sbss section in the output file, in which
2232              case we must change the name of the output section.  */
2233           subname = o->name + sizeof ".gptab" - 1;
2234           if (bfd_get_section_by_name (abfd, subname) == NULL)
2235             {
2236               if (o == gptab_data_sec)
2237                 o->name = ".gptab.data";
2238               else
2239                 o->name = ".gptab.bss";
2240               subname = o->name + sizeof ".gptab" - 1;
2241               BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
2242             }
2243
2244           /* Set up the first entry.  */
2245           c = 1;
2246           tab = (Elf32_gptab *) malloc (c * sizeof (Elf32_gptab));
2247           if (tab == NULL)
2248             {
2249               bfd_set_error (bfd_error_no_memory);
2250               return false;
2251             }
2252           tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
2253           tab[0].gt_header.gt_unused = 0;
2254
2255           /* Combine the input sections.  */
2256           for (p = o->link_order_head;
2257                p != (struct bfd_link_order *) NULL;
2258                p = p->next)
2259             {
2260               asection *input_section;
2261               bfd *input_bfd;
2262               bfd_size_type size;
2263               unsigned long last;
2264               bfd_size_type gpentry;
2265
2266               if (p->type != bfd_indirect_link_order)
2267                 {
2268                   if (p->type == bfd_fill_link_order)
2269                     continue;
2270                   abort ();
2271                 }
2272
2273               input_section = p->u.indirect.section;
2274               input_bfd = input_section->owner;
2275
2276               /* Combine the gptab entries for this input section one
2277                  by one.  We know that the input gptab entries are
2278                  sorted by ascending -G value.  */
2279               size = bfd_section_size (input_bfd, input_section);
2280               last = 0;
2281               for (gpentry = sizeof (Elf32_External_gptab);
2282                    gpentry < size;
2283                    gpentry += sizeof (Elf32_External_gptab))
2284                 {
2285                   Elf32_External_gptab ext_gptab;
2286                   Elf32_gptab int_gptab;
2287                   unsigned long val;
2288                   unsigned long add;
2289                   boolean exact;
2290                   unsigned int look;
2291
2292                   if (! (bfd_get_section_contents
2293                          (input_bfd, input_section, (PTR) &ext_gptab,
2294                           gpentry, sizeof (Elf32_External_gptab))))
2295                     {
2296                       free (tab);
2297                       return false;
2298                     }
2299
2300                   bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
2301                                                 &int_gptab);
2302                   val = int_gptab.gt_entry.gt_g_value;
2303                   add = int_gptab.gt_entry.gt_bytes - last;
2304
2305                   exact = false;
2306                   for (look = 1; look < c; look++)
2307                     {
2308                       if (tab[look].gt_entry.gt_g_value >= val)
2309                         tab[look].gt_entry.gt_bytes += add;
2310
2311                       if (tab[look].gt_entry.gt_g_value == val)
2312                         exact = true;
2313                     }
2314
2315                   if (! exact)
2316                     {
2317                       Elf32_gptab *new_tab;
2318                       unsigned int max;
2319
2320                       /* We need a new table entry.  */
2321                       new_tab = ((Elf32_gptab *)
2322                                   realloc ((PTR) tab,
2323                                            (c + 1) * sizeof (Elf32_gptab)));
2324                       if (new_tab == NULL)
2325                         {
2326                           bfd_set_error (bfd_error_no_memory);
2327                           free (tab);
2328                           return false;
2329                         }
2330                       tab = new_tab;
2331                       tab[c].gt_entry.gt_g_value = val;
2332                       tab[c].gt_entry.gt_bytes = add;
2333
2334                       /* Merge in the size for the next smallest -G
2335                          value, since that will be implied by this new
2336                          value.  */
2337                       max = 0;
2338                       for (look = 1; look < c; look++)
2339                         {
2340                           if (tab[look].gt_entry.gt_g_value < val
2341                               && (max == 0
2342                                   || (tab[look].gt_entry.gt_g_value
2343                                       > tab[max].gt_entry.gt_g_value)))
2344                             max = look;
2345                         }
2346                       if (max != 0)
2347                         tab[c].gt_entry.gt_bytes +=
2348                           tab[max].gt_entry.gt_bytes;
2349
2350                       ++c;
2351                     }
2352
2353                   last = int_gptab.gt_entry.gt_bytes;
2354                 }
2355
2356               /* Hack: reset the SEC_HAS_CONTENTS flag so that
2357                  elf_link_input_bfd ignores this section.  */
2358               input_section->flags &=~ SEC_HAS_CONTENTS;
2359             }
2360
2361           /* The table must be sorted by -G value.  */
2362           if (c > 2)
2363             qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
2364
2365           /* Swap out the table.  */
2366           ext_tab = ((Elf32_External_gptab *)
2367                      bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
2368           if (ext_tab == NULL)
2369             {
2370               free (tab);
2371               return false;
2372             }
2373
2374           for (i = 0; i < c; i++)
2375             bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
2376           free (tab);
2377
2378           o->_raw_size = c * sizeof (Elf32_External_gptab);
2379           o->contents = (bfd_byte *) ext_tab;
2380
2381           /* Skip this section later on (I don't think this currently
2382              matters, but someday it might).  */
2383           o->link_order_head = (struct bfd_link_order *) NULL;
2384         }
2385     }
2386
2387   /* Get a value for the GP register.  */
2388   if (elf_gp (abfd) == 0)
2389     {
2390       struct bfd_link_hash_entry *h;
2391
2392       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2393       if (h != (struct bfd_link_hash_entry *) NULL
2394           && h->type == bfd_link_hash_defined)
2395         elf_gp (abfd) = (h->u.def.value
2396                          + h->u.def.section->output_section->vma
2397                          + h->u.def.section->output_offset);
2398       else if (info->relocateable)
2399         {
2400           bfd_vma lo;
2401
2402           /* Make up a value.  */
2403           lo = (bfd_vma) -1;
2404           for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2405             {
2406               if (o->vma < lo
2407                   && (strcmp (o->name, ".sbss") == 0
2408                       || strcmp (o->name, ".sdata") == 0
2409                       || strcmp (o->name, ".lit4") == 0
2410                       || strcmp (o->name, ".lit8") == 0))
2411                 lo = o->vma;
2412             }
2413           elf_gp (abfd) = lo + 0x8000;
2414         }
2415       else
2416         {
2417           /* If the relocate_section function needs to do a reloc
2418              involving the GP value, it should make a reloc_dangerous
2419              callback to warn that GP is not defined.  */
2420         }
2421     }
2422
2423   /* Invoke the regular ELF backend linker to do all the work.  */
2424   if (! bfd_elf32_bfd_final_link (abfd, info))
2425     return false;
2426
2427   /* Now write out the computed sections.  */
2428
2429   if (reginfo_sec != (asection *) NULL)
2430     {
2431       Elf32_External_RegInfo ext;
2432
2433       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
2434       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
2435                                       (file_ptr) 0, sizeof ext))
2436         return false;
2437     }
2438
2439   if (mdebug_sec != (asection *) NULL)
2440     {
2441       BFD_ASSERT (abfd->output_has_begun);
2442       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
2443                                                swap, info,
2444                                                mdebug_sec->filepos))
2445         return false;
2446
2447       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
2448     }
2449
2450   if (gptab_data_sec != (asection *) NULL)
2451     {
2452       if (! bfd_set_section_contents (abfd, gptab_data_sec,
2453                                       gptab_data_sec->contents,
2454                                       (file_ptr) 0,
2455                                       gptab_data_sec->_raw_size))
2456         return false;
2457     }
2458
2459   if (gptab_bss_sec != (asection *) NULL)
2460     {
2461       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
2462                                       gptab_bss_sec->contents,
2463                                       (file_ptr) 0,
2464                                       gptab_bss_sec->_raw_size))
2465         return false;
2466     }
2467
2468   return true;
2469 }
2470
2471 /* Handle a MIPS ELF HI16 reloc.  */
2472
2473 static void
2474 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
2475      bfd *input_bfd;
2476      Elf_Internal_Rela *relhi;
2477      Elf_Internal_Rela *rello;
2478      bfd_byte *contents;
2479      bfd_vma addend;
2480 {
2481   bfd_vma insn;
2482   bfd_vma addlo;
2483
2484   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2485
2486   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
2487   addlo &= 0xffff;
2488
2489   addend += ((insn & 0xffff) << 16) + addlo;
2490
2491   if ((addlo & 0x8000) != 0)
2492     addend -= 0x10000;
2493   if ((addend & 0x8000) != 0)
2494     addend += 0x10000;
2495
2496   bfd_put_32 (input_bfd,
2497               (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
2498               contents + relhi->r_offset);
2499 }
2500
2501 /* Relocate a MIPS ELF section.  */
2502
2503 static boolean
2504 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2505                            contents, relocs, local_syms, local_sections)
2506      bfd *output_bfd;
2507      struct bfd_link_info *info;
2508      bfd *input_bfd;
2509      asection *input_section;
2510      bfd_byte *contents;
2511      Elf_Internal_Rela *relocs;
2512      Elf_Internal_Sym *local_syms;
2513      asection **local_sections;
2514 {
2515   Elf_Internal_Shdr *symtab_hdr;
2516   size_t locsymcount;
2517   size_t extsymoff;
2518   Elf_Internal_Rela *rel;
2519   Elf_Internal_Rela *relend;
2520
2521   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2522
2523   if (elf_bad_symtab (input_bfd))
2524     {
2525       locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2526       extsymoff = 0;
2527     }
2528   else
2529     {
2530       locsymcount = symtab_hdr->sh_info;
2531       extsymoff = symtab_hdr->sh_info;
2532     }
2533
2534   rel = relocs;
2535   relend = relocs + input_section->reloc_count;
2536   for (; rel < relend; rel++)
2537     {
2538       int r_type;
2539       reloc_howto_type *howto;
2540       unsigned long r_symndx;
2541       bfd_vma addend;
2542       struct elf_link_hash_entry *h;
2543       asection *sec;
2544       Elf_Internal_Sym *sym;
2545       bfd_reloc_status_type r;
2546
2547       r_type = ELF32_R_TYPE (rel->r_info);
2548       if (r_type < 0 || r_type >= (int) R_MIPS_max)
2549         {
2550           bfd_set_error (bfd_error_bad_value);
2551           return false;
2552         }
2553       howto = elf_mips_howto_table + r_type;
2554
2555       r_symndx = ELF32_R_SYM (rel->r_info);
2556
2557       /* Mix in the change in GP address for a GP relative reloc.  */
2558       if (r_type != R_MIPS_GPREL16
2559           && r_type != R_MIPS_LITERAL
2560           && r_type != R_MIPS_GPREL32)
2561         addend = 0;
2562       else
2563         {
2564           if (elf_gp (output_bfd) == 0)
2565             {
2566               if (! ((*info->callbacks->reloc_dangerous)
2567                      (info,
2568                       "GP relative relocation when GP not defined",
2569                       input_bfd, input_section,
2570                       rel->r_offset)))
2571                 return false;
2572               /* Only give the error once per link.  */
2573               elf_gp (output_bfd) = 4;
2574             }
2575
2576           if (r_symndx < extsymoff
2577               || (elf_bad_symtab (input_bfd)
2578                   && local_sections[r_symndx] != NULL))
2579             {
2580               /* This is a relocation against a section.  The current
2581                  addend in the instruction is the difference between
2582                  INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
2583                  must change this to be the difference between the
2584                  final definition (which will end up in RELOCATION)
2585                  and the GP value of OUTPUT_BFD (which is in GP).  */
2586               addend = elf_gp (input_bfd) - elf_gp (output_bfd);
2587             }
2588           else if (! info->relocateable)
2589             {
2590               /* We are doing a final link.  The current addend in the
2591                  instruction is simply the desired offset into the
2592                  symbol (normally zero).  We want the instruction to
2593                  hold the difference between the final definition of
2594                  the symbol (which will end up in RELOCATION) and the
2595                  GP value of OUTPUT_BFD (which is in GP).  */
2596               addend = - elf_gp (output_bfd);
2597             }
2598           else
2599             {
2600               /* We are generating relocateable output, and we aren't
2601                  going to define this symbol, so we just leave the
2602                  instruction alone.  */
2603               addend = 0;
2604             }
2605         }
2606
2607       h = NULL;
2608       sym = NULL;
2609       sec = NULL;
2610       if (info->relocateable)
2611         {
2612           /* This is a relocateable link.  We don't have to change
2613              anything, unless the reloc is against a section symbol,
2614              in which case we have to adjust according to where the
2615              section symbol winds up in the output section.  */
2616           if (r_symndx >= locsymcount
2617               || (elf_bad_symtab (input_bfd)
2618                   && local_sections[r_symndx] == NULL))
2619             r = bfd_reloc_ok;
2620           else
2621             {
2622               sym = local_syms + r_symndx;
2623               if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2624                 r = bfd_reloc_ok;
2625               else
2626                 {
2627                   sec = local_sections[r_symndx];
2628
2629                   /* It would be logical to add sym->st_value here,
2630                      but Irix 5 sometimes generates a garbage symbol
2631                      value.  */
2632                   addend += sec->output_offset;
2633
2634                   /* If this is HI16 with an associated LO16, adjust
2635                      the addend accordingly.  Otherwise, just
2636                      relocate.  */
2637                   if (r_type != R_MIPS_HI16
2638                       || (rel + 1) >= relend
2639                       || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
2640                     r = _bfd_relocate_contents (howto, input_bfd,
2641                                                 addend,
2642                                                 contents + rel->r_offset);
2643                   else
2644                     {
2645                       mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
2646                                               contents, addend);
2647                       r = bfd_reloc_ok;
2648                     }
2649                 }
2650             }
2651         }
2652       else
2653         {
2654           bfd_vma relocation;
2655
2656           /* This is a final link.  */
2657           sym = NULL;
2658           if (r_symndx < extsymoff
2659               || (elf_bad_symtab (input_bfd)
2660                   && local_sections[r_symndx] != NULL))
2661             {
2662               sym = local_syms + r_symndx;
2663               sec = local_sections[r_symndx];
2664               relocation = (sec->output_section->vma
2665                             + sec->output_offset);
2666
2667               /* It would be logical to always add sym->st_value here,
2668                  but Irix 5 sometimes generates a garbage symbol
2669                  value.  */
2670               if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2671                 relocation += sym->st_value;
2672             }
2673           else
2674             {
2675               long indx;
2676
2677               indx = r_symndx - extsymoff;
2678               h = elf_sym_hashes (input_bfd)[indx];
2679               if (h->root.type == bfd_link_hash_defined
2680                   || h->root.type == bfd_link_hash_defweak)
2681                 {
2682                   sec = h->root.u.def.section;
2683                   relocation = (h->root.u.def.value
2684                                 + sec->output_section->vma
2685                                 + sec->output_offset);
2686                 }
2687               else if (h->root.type == bfd_link_hash_undefweak)
2688                 relocation = 0;
2689               else
2690                 {
2691                   if (! ((*info->callbacks->undefined_symbol)
2692                          (info, h->root.root.string, input_bfd,
2693                           input_section, rel->r_offset)))
2694                     return false;
2695                   relocation = 0;
2696                 }
2697             }
2698
2699           if (r_type != R_MIPS_HI16
2700               || (rel + 1) >= relend
2701               || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
2702             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2703                                           contents, rel->r_offset,
2704                                           relocation, addend);
2705           else
2706             {
2707               mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
2708                                       contents, relocation + addend);
2709               r = bfd_reloc_ok;
2710             }
2711         }
2712
2713       if (r != bfd_reloc_ok)
2714         {
2715           switch (r)
2716             {
2717             default:
2718             case bfd_reloc_outofrange:
2719               abort ();
2720             case bfd_reloc_overflow:
2721               {
2722                 const char *name;
2723
2724                 if (h != NULL)
2725                   name = h->root.root.string;
2726                 else
2727                   {
2728                     name = bfd_elf_string_from_elf_section (input_bfd,
2729                                                             symtab_hdr->sh_link,
2730                                                             sym->st_name);
2731                     if (name == NULL)
2732                       return false;
2733                     if (*name == '\0')
2734                       name = bfd_section_name (input_bfd, sec);
2735                   }
2736                 if (! ((*info->callbacks->reloc_overflow)
2737                        (info, name, howto->name, (bfd_vma) 0,
2738                         input_bfd, input_section, rel->r_offset)))
2739                   return false;
2740               }
2741               break;
2742             }
2743         }
2744     }
2745
2746   return true;
2747 }
2748 \f
2749 /* This is almost identical to bfd_generic_get_... except that some
2750    MIPS relocations need to be handled specially.  Sigh.  */
2751 static bfd_byte *
2752 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
2753                                            relocateable, symbols)
2754      bfd *abfd;
2755      struct bfd_link_info *link_info;
2756      struct bfd_link_order *link_order;
2757      bfd_byte *data;
2758      boolean relocateable;
2759      asymbol **symbols;
2760 {
2761   /* Get enough memory to hold the stuff */
2762   bfd *input_bfd = link_order->u.indirect.section->owner;
2763   asection *input_section = link_order->u.indirect.section;
2764
2765   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
2766   arelent **reloc_vector = NULL;
2767   long reloc_count;
2768
2769   if (reloc_size < 0)
2770     goto error_return;
2771
2772   reloc_vector = (arelent **) malloc (reloc_size);
2773   if (reloc_vector == NULL && reloc_size != 0)
2774     {
2775       bfd_set_error (bfd_error_no_memory);
2776       goto error_return;
2777     }
2778
2779   /* read in the section */
2780   if (!bfd_get_section_contents (input_bfd,
2781                                  input_section,
2782                                  (PTR) data,
2783                                  0,
2784                                  input_section->_raw_size))
2785     goto error_return;
2786
2787   /* We're not relaxing the section, so just copy the size info */
2788   input_section->_cooked_size = input_section->_raw_size;
2789   input_section->reloc_done = true;
2790
2791   reloc_count = bfd_canonicalize_reloc (input_bfd,
2792                                         input_section,
2793                                         reloc_vector,
2794                                         symbols);
2795   if (reloc_count < 0)
2796     goto error_return;
2797
2798   if (reloc_count > 0)
2799     {
2800       arelent **parent;
2801       /* for mips */
2802       int gp_found;
2803       bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
2804
2805       {
2806         struct bfd_hash_entry *h;
2807         struct bfd_link_hash_entry *lh;
2808         /* Skip all this stuff if we aren't mixing formats.  */
2809         if (abfd && input_bfd
2810             && abfd->xvec == input_bfd->xvec)
2811           lh = 0;
2812         else
2813           {
2814             h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
2815             lh = (struct bfd_link_hash_entry *) h;
2816           }
2817       lookup:
2818         if (lh)
2819           {
2820             switch (lh->type)
2821               {
2822               case bfd_link_hash_undefined:
2823               case bfd_link_hash_undefweak:
2824               case bfd_link_hash_common:
2825                 gp_found = 0;
2826                 break;
2827               case bfd_link_hash_defined:
2828               case bfd_link_hash_defweak:
2829                 gp_found = 1;
2830                 gp = lh->u.def.value;
2831                 break;
2832               case bfd_link_hash_indirect:
2833               case bfd_link_hash_warning:
2834                 lh = lh->u.i.link;
2835                 /* @@FIXME  ignoring warning for now */
2836                 goto lookup;
2837               case bfd_link_hash_new:
2838               default:
2839                 abort ();
2840               }
2841           }
2842         else
2843           gp_found = 0;
2844       }
2845       /* end mips */
2846       for (parent = reloc_vector; *parent != (arelent *) NULL;
2847            parent++)
2848         {
2849           char *error_message = (char *) NULL;
2850           bfd_reloc_status_type r;
2851
2852           /* Specific to MIPS: Deal with relocation types that require
2853              knowing the gp of the output bfd.  */
2854           asymbol *sym = *(*parent)->sym_ptr_ptr;
2855           if (bfd_is_abs_section (sym->section) && abfd)
2856             {
2857               /* The special_function wouldn't get called anyways.  */
2858             }
2859           else if (!gp_found)
2860             {
2861               /* The gp isn't there; let the special function code
2862                  fall over on its own.  */
2863             }
2864           else if ((*parent)->howto->special_function == mips_elf_gprel16_reloc)
2865             {
2866               /* bypass special_function call */
2867               r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
2868                                    relocateable, (PTR) data, gp);
2869               goto skip_bfd_perform_relocation;
2870             }
2871           /* end mips specific stuff */
2872
2873           r = bfd_perform_relocation (input_bfd,
2874                                       *parent,
2875                                       (PTR) data,
2876                                       input_section,
2877                                       relocateable ? abfd : (bfd *) NULL,
2878                                       &error_message);
2879         skip_bfd_perform_relocation:
2880
2881           if (relocateable)
2882             {
2883               asection *os = input_section->output_section;
2884
2885               /* A partial link, so keep the relocs */
2886               os->orelocation[os->reloc_count] = *parent;
2887               os->reloc_count++;
2888             }
2889
2890           if (r != bfd_reloc_ok)
2891             {
2892               switch (r)
2893                 {
2894                 case bfd_reloc_undefined:
2895                   if (!((*link_info->callbacks->undefined_symbol)
2896                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2897                          input_bfd, input_section, (*parent)->address)))
2898                     goto error_return;
2899                   break;
2900                 case bfd_reloc_dangerous:
2901                   BFD_ASSERT (error_message != (char *) NULL);
2902                   if (!((*link_info->callbacks->reloc_dangerous)
2903                         (link_info, error_message, input_bfd, input_section,
2904                          (*parent)->address)))
2905                     goto error_return;
2906                   break;
2907                 case bfd_reloc_overflow:
2908                   if (!((*link_info->callbacks->reloc_overflow)
2909                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2910                          (*parent)->howto->name, (*parent)->addend,
2911                          input_bfd, input_section, (*parent)->address)))
2912                     goto error_return;
2913                   break;
2914                 case bfd_reloc_outofrange:
2915                 default:
2916                   abort ();
2917                   break;
2918                 }
2919
2920             }
2921         }
2922     }
2923   if (reloc_vector != NULL)
2924     free (reloc_vector);
2925   return data;
2926
2927 error_return:
2928   if (reloc_vector != NULL)
2929     free (reloc_vector);
2930   return NULL;
2931 }
2932 #define bfd_elf32_bfd_get_relocated_section_contents elf32_mips_get_relocated_section_contents
2933 \f
2934 /* ECOFF swapping routines.  These are used when dealing with the
2935    .mdebug section, which is in the ECOFF debugging format.  */
2936 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
2937 {
2938   /* Symbol table magic number.  */
2939   magicSym,
2940   /* Alignment of debugging information.  E.g., 4.  */
2941   4,
2942   /* Sizes of external symbolic information.  */
2943   sizeof (struct hdr_ext),
2944   sizeof (struct dnr_ext),
2945   sizeof (struct pdr_ext),
2946   sizeof (struct sym_ext),
2947   sizeof (struct opt_ext),
2948   sizeof (struct fdr_ext),
2949   sizeof (struct rfd_ext),
2950   sizeof (struct ext_ext),
2951   /* Functions to swap in external symbolic data.  */
2952   ecoff_swap_hdr_in,
2953   ecoff_swap_dnr_in,
2954   ecoff_swap_pdr_in,
2955   ecoff_swap_sym_in,
2956   ecoff_swap_opt_in,
2957   ecoff_swap_fdr_in,
2958   ecoff_swap_rfd_in,
2959   ecoff_swap_ext_in,
2960   _bfd_ecoff_swap_tir_in,
2961   _bfd_ecoff_swap_rndx_in,
2962   /* Functions to swap out external symbolic data.  */
2963   ecoff_swap_hdr_out,
2964   ecoff_swap_dnr_out,
2965   ecoff_swap_pdr_out,
2966   ecoff_swap_sym_out,
2967   ecoff_swap_opt_out,
2968   ecoff_swap_fdr_out,
2969   ecoff_swap_rfd_out,
2970   ecoff_swap_ext_out,
2971   _bfd_ecoff_swap_tir_out,
2972   _bfd_ecoff_swap_rndx_out,
2973   /* Function to read in symbolic data.  */
2974   mips_elf_read_ecoff_info
2975 };
2976 \f
2977 #define TARGET_LITTLE_SYM               bfd_elf32_littlemips_vec
2978 #define TARGET_LITTLE_NAME              "elf32-littlemips"
2979 #define TARGET_BIG_SYM                  bfd_elf32_bigmips_vec
2980 #define TARGET_BIG_NAME                 "elf32-bigmips"
2981 #define ELF_ARCH                        bfd_arch_mips
2982 #define ELF_MACHINE_CODE                EM_MIPS
2983 #define ELF_MAXPAGESIZE                 0x10000
2984 #define elf_backend_collect             true
2985 #define elf_info_to_howto               0
2986 #define elf_info_to_howto_rel           mips_info_to_howto_rel
2987 #define elf_backend_sym_is_global       mips_elf_sym_is_global
2988 #define elf_backend_object_p            mips_elf_object_p
2989 #define elf_backend_section_from_shdr   mips_elf_section_from_shdr
2990 #define elf_backend_fake_sections       mips_elf_fake_sections
2991 #define elf_backend_section_from_bfd_section \
2992                                         mips_elf_section_from_bfd_section
2993 #define elf_backend_section_processing  mips_elf_section_processing
2994 #define elf_backend_symbol_processing   mips_elf_symbol_processing
2995 #define elf_backend_final_write_processing \
2996                                         mips_elf_final_write_processing
2997 #define elf_backend_ecoff_debug_swap    &mips_elf_ecoff_debug_swap
2998
2999 #define bfd_elf32_bfd_is_local_label    mips_elf_is_local_label
3000 #define bfd_elf32_find_nearest_line     mips_elf_find_nearest_line
3001
3002 #define bfd_elf32_bfd_link_hash_table_create \
3003                                         mips_elf_link_hash_table_create
3004 #define bfd_elf32_bfd_final_link        mips_elf_final_link
3005 #define elf_backend_relocate_section    mips_elf_relocate_section
3006 #define elf_backend_add_symbol_hook     mips_elf_add_symbol_hook
3007
3008 #include "elf32-target.h"
This page took 0.205648 seconds and 4 git commands to generate.