OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [bfd/] [elf32-mips.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.