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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [elfxx-mips.c] - Blame information for rev 53

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

Line No. Rev Author Line
1 14 khays
/* MIPS-specific support for ELF
2
   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3
   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4
   Free Software Foundation, Inc.
5
 
6
   Most of the information added by Ian Lance Taylor, Cygnus Support,
7
   <ian@cygnus.com>.
8
   N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9
   <mark@codesourcery.com>
10
   Traditional MIPS targets support added by Koundinya.K, Dansk Data
11
   Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
12
 
13
   This file is part of BFD, the Binary File Descriptor library.
14
 
15
   This program is free software; you can redistribute it and/or modify
16
   it under the terms of the GNU General Public License as published by
17
   the Free Software Foundation; either version 3 of the License, or
18
   (at your option) any later version.
19
 
20
   This program is distributed in the hope that it will be useful,
21
   but WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
   GNU General Public License for more details.
24
 
25
   You should have received a copy of the GNU General Public License
26
   along with this program; if not, write to the Free Software
27
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28
   MA 02110-1301, USA.  */
29
 
30
 
31
/* This file handles functionality common to the different MIPS ABI's.  */
32
 
33
#include "sysdep.h"
34
#include "bfd.h"
35
#include "libbfd.h"
36
#include "libiberty.h"
37
#include "elf-bfd.h"
38
#include "elfxx-mips.h"
39
#include "elf/mips.h"
40
#include "elf-vxworks.h"
41
 
42
/* Get the ECOFF swapping routines.  */
43
#include "coff/sym.h"
44
#include "coff/symconst.h"
45
#include "coff/ecoff.h"
46
#include "coff/mips.h"
47
 
48
#include "hashtab.h"
49
 
50
/* This structure is used to hold information about one GOT entry.
51
   There are three types of entry:
52
 
53
      (1) absolute addresses
54
            (abfd == NULL)
55
      (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
56
            (abfd != NULL, symndx >= 0)
57
      (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
58
            (abfd != NULL, symndx == -1)
59
 
60
   Type (3) entries are treated differently for different types of GOT.
61
   In the "master" GOT -- i.e.  the one that describes every GOT
62
   reference needed in the link -- the mips_got_entry is keyed on both
63
   the symbol and the input bfd that references it.  If it turns out
64
   that we need multiple GOTs, we can then use this information to
65
   create separate GOTs for each input bfd.
66
 
67
   However, we want each of these separate GOTs to have at most one
68
   entry for a given symbol, so their type (3) entries are keyed only
69
   on the symbol.  The input bfd given by the "abfd" field is somewhat
70
   arbitrary in this case.
71
 
72
   This means that when there are multiple GOTs, each GOT has a unique
73
   mips_got_entry for every symbol within it.  We can therefore use the
74
   mips_got_entry fields (tls_type and gotidx) to track the symbol's
75
   GOT index.
76
 
77
   However, if it turns out that we need only a single GOT, we continue
78
   to use the master GOT to describe it.  There may therefore be several
79
   mips_got_entries for the same symbol, each with a different input bfd.
80
   We want to make sure that each symbol gets a unique GOT entry, so when
81
   there's a single GOT, we use the symbol's hash entry, not the
82
   mips_got_entry fields, to track a symbol's GOT index.  */
83
struct mips_got_entry
84
{
85
  /* The input bfd in which the symbol is defined.  */
86
  bfd *abfd;
87
  /* The index of the symbol, as stored in the relocation r_info, if
88
     we have a local symbol; -1 otherwise.  */
89
  long symndx;
90
  union
91
  {
92
    /* If abfd == NULL, an address that must be stored in the got.  */
93
    bfd_vma address;
94
    /* If abfd != NULL && symndx != -1, the addend of the relocation
95
       that should be added to the symbol value.  */
96
    bfd_vma addend;
97
    /* If abfd != NULL && symndx == -1, the hash table entry
98
       corresponding to symbol in the GOT.  The symbol's entry
99
       is in the local area if h->global_got_area is GGA_NONE,
100
       otherwise it is in the global area.  */
101
    struct mips_elf_link_hash_entry *h;
102
  } d;
103
 
104
  /* The TLS types included in this GOT entry (specifically, GD and
105
     IE).  The GD and IE flags can be added as we encounter new
106
     relocations.  LDM can also be set; it will always be alone, not
107
     combined with any GD or IE flags.  An LDM GOT entry will be
108
     a local symbol entry with r_symndx == 0.  */
109
  unsigned char tls_type;
110
 
111
  /* The offset from the beginning of the .got section to the entry
112
     corresponding to this symbol+addend.  If it's a global symbol
113
     whose offset is yet to be decided, it's going to be -1.  */
114
  long gotidx;
115
};
116
 
117
/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
118
   The structures form a non-overlapping list that is sorted by increasing
119
   MIN_ADDEND.  */
120
struct mips_got_page_range
121
{
122
  struct mips_got_page_range *next;
123
  bfd_signed_vma min_addend;
124
  bfd_signed_vma max_addend;
125
};
126
 
127
/* This structure describes the range of addends that are applied to page
128
   relocations against a given symbol.  */
129
struct mips_got_page_entry
130
{
131
  /* The input bfd in which the symbol is defined.  */
132
  bfd *abfd;
133
  /* The index of the symbol, as stored in the relocation r_info.  */
134
  long symndx;
135
  /* The ranges for this page entry.  */
136
  struct mips_got_page_range *ranges;
137
  /* The maximum number of page entries needed for RANGES.  */
138
  bfd_vma num_pages;
139
};
140
 
141
/* This structure is used to hold .got information when linking.  */
142
 
143
struct mips_got_info
144
{
145
  /* The global symbol in the GOT with the lowest index in the dynamic
146
     symbol table.  */
147
  struct elf_link_hash_entry *global_gotsym;
148
  /* The number of global .got entries.  */
149
  unsigned int global_gotno;
150
  /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
151
  unsigned int reloc_only_gotno;
152
  /* The number of .got slots used for TLS.  */
153
  unsigned int tls_gotno;
154
  /* The first unused TLS .got entry.  Used only during
155
     mips_elf_initialize_tls_index.  */
156
  unsigned int tls_assigned_gotno;
157
  /* The number of local .got entries, eventually including page entries.  */
158
  unsigned int local_gotno;
159
  /* The maximum number of page entries needed.  */
160
  unsigned int page_gotno;
161
  /* The number of local .got entries we have used.  */
162
  unsigned int assigned_gotno;
163
  /* A hash table holding members of the got.  */
164
  struct htab *got_entries;
165
  /* A hash table of mips_got_page_entry structures.  */
166
  struct htab *got_page_entries;
167
  /* A hash table mapping input bfds to other mips_got_info.  NULL
168
     unless multi-got was necessary.  */
169
  struct htab *bfd2got;
170
  /* In multi-got links, a pointer to the next got (err, rather, most
171
     of the time, it points to the previous got).  */
172
  struct mips_got_info *next;
173
  /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
174
     for none, or MINUS_TWO for not yet assigned.  This is needed
175
     because a single-GOT link may have multiple hash table entries
176
     for the LDM.  It does not get initialized in multi-GOT mode.  */
177
  bfd_vma tls_ldm_offset;
178
};
179
 
180
/* Map an input bfd to a got in a multi-got link.  */
181
 
182
struct mips_elf_bfd2got_hash
183
{
184
  bfd *bfd;
185
  struct mips_got_info *g;
186
};
187
 
188
/* Structure passed when traversing the bfd2got hash table, used to
189
   create and merge bfd's gots.  */
190
 
191
struct mips_elf_got_per_bfd_arg
192
{
193
  /* A hashtable that maps bfds to gots.  */
194
  htab_t bfd2got;
195
  /* The output bfd.  */
196
  bfd *obfd;
197
  /* The link information.  */
198
  struct bfd_link_info *info;
199
  /* A pointer to the primary got, i.e., the one that's going to get
200
     the implicit relocations from DT_MIPS_LOCAL_GOTNO and
201
     DT_MIPS_GOTSYM.  */
202
  struct mips_got_info *primary;
203
  /* A non-primary got we're trying to merge with other input bfd's
204
     gots.  */
205
  struct mips_got_info *current;
206
  /* The maximum number of got entries that can be addressed with a
207
     16-bit offset.  */
208
  unsigned int max_count;
209
  /* The maximum number of page entries needed by each got.  */
210
  unsigned int max_pages;
211
  /* The total number of global entries which will live in the
212
     primary got and be automatically relocated.  This includes
213
     those not referenced by the primary GOT but included in
214
     the "master" GOT.  */
215
  unsigned int global_count;
216
};
217
 
218
/* Another structure used to pass arguments for got entries traversal.  */
219
 
220
struct mips_elf_set_global_got_offset_arg
221
{
222
  struct mips_got_info *g;
223
  int value;
224
  unsigned int needed_relocs;
225
  struct bfd_link_info *info;
226
};
227
 
228
/* A structure used to count TLS relocations or GOT entries, for GOT
229
   entry or ELF symbol table traversal.  */
230
 
231
struct mips_elf_count_tls_arg
232
{
233
  struct bfd_link_info *info;
234
  unsigned int needed;
235
};
236
 
237
struct _mips_elf_section_data
238
{
239
  struct bfd_elf_section_data elf;
240
  union
241
  {
242
    bfd_byte *tdata;
243
  } u;
244
};
245
 
246
#define mips_elf_section_data(sec) \
247
  ((struct _mips_elf_section_data *) elf_section_data (sec))
248
 
249
#define is_mips_elf(bfd)                                \
250
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
251
   && elf_tdata (bfd) != NULL                           \
252
   && elf_object_id (bfd) == MIPS_ELF_DATA)
253
 
254
/* The ABI says that every symbol used by dynamic relocations must have
255
   a global GOT entry.  Among other things, this provides the dynamic
256
   linker with a free, directly-indexed cache.  The GOT can therefore
257
   contain symbols that are not referenced by GOT relocations themselves
258
   (in other words, it may have symbols that are not referenced by things
259
   like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
260
 
261
   GOT relocations are less likely to overflow if we put the associated
262
   GOT entries towards the beginning.  We therefore divide the global
263
   GOT entries into two areas: "normal" and "reloc-only".  Entries in
264
   the first area can be used for both dynamic relocations and GP-relative
265
   accesses, while those in the "reloc-only" area are for dynamic
266
   relocations only.
267
 
268
   These GGA_* ("Global GOT Area") values are organised so that lower
269
   values are more general than higher values.  Also, non-GGA_NONE
270
   values are ordered by the position of the area in the GOT.  */
271
#define GGA_NORMAL 0
272
#define GGA_RELOC_ONLY 1
273
#define GGA_NONE 2
274
 
275
/* Information about a non-PIC interface to a PIC function.  There are
276
   two ways of creating these interfaces.  The first is to add:
277
 
278
        lui     $25,%hi(func)
279
        addiu   $25,$25,%lo(func)
280
 
281
   immediately before a PIC function "func".  The second is to add:
282
 
283
        lui     $25,%hi(func)
284
        j       func
285
        addiu   $25,$25,%lo(func)
286
 
287
   to a separate trampoline section.
288
 
289
   Stubs of the first kind go in a new section immediately before the
290
   target function.  Stubs of the second kind go in a single section
291
   pointed to by the hash table's "strampoline" field.  */
292
struct mips_elf_la25_stub {
293
  /* The generated section that contains this stub.  */
294
  asection *stub_section;
295
 
296
  /* The offset of the stub from the start of STUB_SECTION.  */
297
  bfd_vma offset;
298
 
299
  /* One symbol for the original function.  Its location is available
300
     in H->root.root.u.def.  */
301
  struct mips_elf_link_hash_entry *h;
302
};
303
 
304
/* Macros for populating a mips_elf_la25_stub.  */
305
 
306
#define LA25_LUI(VAL) (0x3c190000 | (VAL))      /* lui t9,VAL */
307
#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
308
#define LA25_ADDIU(VAL) (0x27390000 | (VAL))    /* addiu t9,t9,VAL */
309
 
310
/* This structure is passed to mips_elf_sort_hash_table_f when sorting
311
   the dynamic symbols.  */
312
 
313
struct mips_elf_hash_sort_data
314
{
315
  /* The symbol in the global GOT with the lowest dynamic symbol table
316
     index.  */
317
  struct elf_link_hash_entry *low;
318
  /* The least dynamic symbol table index corresponding to a non-TLS
319
     symbol with a GOT entry.  */
320
  long min_got_dynindx;
321
  /* The greatest dynamic symbol table index corresponding to a symbol
322
     with a GOT entry that is not referenced (e.g., a dynamic symbol
323
     with dynamic relocations pointing to it from non-primary GOTs).  */
324
  long max_unref_got_dynindx;
325
  /* The greatest dynamic symbol table index not corresponding to a
326
     symbol without a GOT entry.  */
327
  long max_non_got_dynindx;
328
};
329
 
330
/* The MIPS ELF linker needs additional information for each symbol in
331
   the global hash table.  */
332
 
333
struct mips_elf_link_hash_entry
334
{
335
  struct elf_link_hash_entry root;
336
 
337
  /* External symbol information.  */
338
  EXTR esym;
339
 
340
  /* The la25 stub we have created for ths symbol, if any.  */
341
  struct mips_elf_la25_stub *la25_stub;
342
 
343
  /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
344
     this symbol.  */
345
  unsigned int possibly_dynamic_relocs;
346
 
347
  /* If there is a stub that 32 bit functions should use to call this
348
     16 bit function, this points to the section containing the stub.  */
349
  asection *fn_stub;
350
 
351
  /* If there is a stub that 16 bit functions should use to call this
352
     32 bit function, this points to the section containing the stub.  */
353
  asection *call_stub;
354
 
355
  /* This is like the call_stub field, but it is used if the function
356
     being called returns a floating point value.  */
357
  asection *call_fp_stub;
358
 
359
#define GOT_NORMAL      0
360
#define GOT_TLS_GD      1
361
#define GOT_TLS_LDM     2
362
#define GOT_TLS_IE      4
363
#define GOT_TLS_OFFSET_DONE    0x40
364
#define GOT_TLS_DONE    0x80
365
  unsigned char tls_type;
366
 
367
  /* This is only used in single-GOT mode; in multi-GOT mode there
368
     is one mips_got_entry per GOT entry, so the offset is stored
369
     there.  In single-GOT mode there may be many mips_got_entry
370
     structures all referring to the same GOT slot.  It might be
371
     possible to use root.got.offset instead, but that field is
372
     overloaded already.  */
373
  bfd_vma tls_got_offset;
374
 
375
  /* The highest GGA_* value that satisfies all references to this symbol.  */
376
  unsigned int global_got_area : 2;
377
 
378
  /* True if all GOT relocations against this symbol are for calls.  This is
379
     a looser condition than no_fn_stub below, because there may be other
380
     non-call non-GOT relocations against the symbol.  */
381
  unsigned int got_only_for_calls : 1;
382
 
383
  /* True if one of the relocations described by possibly_dynamic_relocs
384
     is against a readonly section.  */
385
  unsigned int readonly_reloc : 1;
386
 
387
  /* True if there is a relocation against this symbol that must be
388
     resolved by the static linker (in other words, if the relocation
389
     cannot possibly be made dynamic).  */
390
  unsigned int has_static_relocs : 1;
391
 
392
  /* True if we must not create a .MIPS.stubs entry for this symbol.
393
     This is set, for example, if there are relocations related to
394
     taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
395
     See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
396
  unsigned int no_fn_stub : 1;
397
 
398
  /* Whether we need the fn_stub; this is true if this symbol appears
399
     in any relocs other than a 16 bit call.  */
400
  unsigned int need_fn_stub : 1;
401
 
402
  /* True if this symbol is referenced by branch relocations from
403
     any non-PIC input file.  This is used to determine whether an
404
     la25 stub is required.  */
405
  unsigned int has_nonpic_branches : 1;
406
 
407
  /* Does this symbol need a traditional MIPS lazy-binding stub
408
     (as opposed to a PLT entry)?  */
409
  unsigned int needs_lazy_stub : 1;
410
};
411
 
412
/* MIPS ELF linker hash table.  */
413
 
414
struct mips_elf_link_hash_table
415
{
416
  struct elf_link_hash_table root;
417
#if 0
418
  /* We no longer use this.  */
419
  /* String section indices for the dynamic section symbols.  */
420
  bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
421
#endif
422
 
423
  /* The number of .rtproc entries.  */
424
  bfd_size_type procedure_count;
425
 
426
  /* The size of the .compact_rel section (if SGI_COMPAT).  */
427
  bfd_size_type compact_rel_size;
428
 
429
  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
430
     entry is set to the address of __rld_obj_head as in IRIX5.  */
431
  bfd_boolean use_rld_obj_head;
432
 
433
  /* This is the value of the __rld_map or __rld_obj_head symbol.  */
434
  bfd_vma rld_value;
435
 
436
  /* This is set if we see any mips16 stub sections.  */
437
  bfd_boolean mips16_stubs_seen;
438
 
439
  /* True if we can generate copy relocs and PLTs.  */
440
  bfd_boolean use_plts_and_copy_relocs;
441
 
442
  /* True if we're generating code for VxWorks.  */
443
  bfd_boolean is_vxworks;
444
 
445
  /* True if we already reported the small-data section overflow.  */
446
  bfd_boolean small_data_overflow_reported;
447
 
448
  /* Shortcuts to some dynamic sections, or NULL if they are not
449
     being used.  */
450
  asection *srelbss;
451
  asection *sdynbss;
452
  asection *srelplt;
453
  asection *srelplt2;
454
  asection *sgotplt;
455
  asection *splt;
456
  asection *sstubs;
457
  asection *sgot;
458
 
459
  /* The master GOT information.  */
460
  struct mips_got_info *got_info;
461
 
462
  /* The size of the PLT header in bytes.  */
463
  bfd_vma plt_header_size;
464
 
465
  /* The size of a PLT entry in bytes.  */
466
  bfd_vma plt_entry_size;
467
 
468
  /* The number of functions that need a lazy-binding stub.  */
469
  bfd_vma lazy_stub_count;
470
 
471
  /* The size of a function stub entry in bytes.  */
472
  bfd_vma function_stub_size;
473
 
474
  /* The number of reserved entries at the beginning of the GOT.  */
475
  unsigned int reserved_gotno;
476
 
477
  /* The section used for mips_elf_la25_stub trampolines.
478
     See the comment above that structure for details.  */
479
  asection *strampoline;
480
 
481
  /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
482
     pairs.  */
483
  htab_t la25_stubs;
484
 
485
  /* A function FN (NAME, IS, OS) that creates a new input section
486
     called NAME and links it to output section OS.  If IS is nonnull,
487
     the new section should go immediately before it, otherwise it
488
     should go at the (current) beginning of OS.
489
 
490
     The function returns the new section on success, otherwise it
491
     returns null.  */
492
  asection *(*add_stub_section) (const char *, asection *, asection *);
493
};
494
 
495
/* Get the MIPS ELF linker hash table from a link_info structure.  */
496
 
497
#define mips_elf_hash_table(p) \
498
  (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
499
  == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
500
 
501
/* A structure used to communicate with htab_traverse callbacks.  */
502
struct mips_htab_traverse_info
503
{
504
  /* The usual link-wide information.  */
505
  struct bfd_link_info *info;
506
  bfd *output_bfd;
507
 
508
  /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
509
  bfd_boolean error;
510
};
511
 
512
#define TLS_RELOC_P(r_type) \
513
  (r_type == R_MIPS_TLS_DTPMOD32                \
514
   || r_type == R_MIPS_TLS_DTPMOD64             \
515
   || r_type == R_MIPS_TLS_DTPREL32             \
516
   || r_type == R_MIPS_TLS_DTPREL64             \
517
   || r_type == R_MIPS_TLS_GD                   \
518
   || r_type == R_MIPS_TLS_LDM                  \
519
   || r_type == R_MIPS_TLS_DTPREL_HI16          \
520
   || r_type == R_MIPS_TLS_DTPREL_LO16          \
521
   || r_type == R_MIPS_TLS_GOTTPREL             \
522
   || r_type == R_MIPS_TLS_TPREL32              \
523
   || r_type == R_MIPS_TLS_TPREL64              \
524
   || r_type == R_MIPS_TLS_TPREL_HI16           \
525
   || r_type == R_MIPS_TLS_TPREL_LO16)
526
 
527
/* Structure used to pass information to mips_elf_output_extsym.  */
528
 
529
struct extsym_info
530
{
531
  bfd *abfd;
532
  struct bfd_link_info *info;
533
  struct ecoff_debug_info *debug;
534
  const struct ecoff_debug_swap *swap;
535
  bfd_boolean failed;
536
};
537
 
538
/* The names of the runtime procedure table symbols used on IRIX5.  */
539
 
540
static const char * const mips_elf_dynsym_rtproc_names[] =
541
{
542
  "_procedure_table",
543
  "_procedure_string_table",
544
  "_procedure_table_size",
545
  NULL
546
};
547
 
548
/* These structures are used to generate the .compact_rel section on
549
   IRIX5.  */
550
 
551
typedef struct
552
{
553
  unsigned long id1;            /* Always one?  */
554
  unsigned long num;            /* Number of compact relocation entries.  */
555
  unsigned long id2;            /* Always two?  */
556
  unsigned long offset;         /* The file offset of the first relocation.  */
557
  unsigned long reserved0;      /* Zero?  */
558
  unsigned long reserved1;      /* Zero?  */
559
} Elf32_compact_rel;
560
 
561
typedef struct
562
{
563
  bfd_byte id1[4];
564
  bfd_byte num[4];
565
  bfd_byte id2[4];
566
  bfd_byte offset[4];
567
  bfd_byte reserved0[4];
568
  bfd_byte reserved1[4];
569
} Elf32_External_compact_rel;
570
 
571
typedef struct
572
{
573
  unsigned int ctype : 1;       /* 1: long 0: short format. See below.  */
574
  unsigned int rtype : 4;       /* Relocation types. See below.  */
575
  unsigned int dist2to : 8;
576
  unsigned int relvaddr : 19;   /* (VADDR - vaddr of the previous entry)/ 4 */
577
  unsigned long konst;          /* KONST field. See below.  */
578
  unsigned long vaddr;          /* VADDR to be relocated.  */
579
} Elf32_crinfo;
580
 
581
typedef struct
582
{
583
  unsigned int ctype : 1;       /* 1: long 0: short format. See below.  */
584
  unsigned int rtype : 4;       /* Relocation types. See below.  */
585
  unsigned int dist2to : 8;
586
  unsigned int relvaddr : 19;   /* (VADDR - vaddr of the previous entry)/ 4 */
587
  unsigned long konst;          /* KONST field. See below.  */
588
} Elf32_crinfo2;
589
 
590
typedef struct
591
{
592
  bfd_byte info[4];
593
  bfd_byte konst[4];
594
  bfd_byte vaddr[4];
595
} Elf32_External_crinfo;
596
 
597
typedef struct
598
{
599
  bfd_byte info[4];
600
  bfd_byte konst[4];
601
} Elf32_External_crinfo2;
602
 
603
/* These are the constants used to swap the bitfields in a crinfo.  */
604
 
605
#define CRINFO_CTYPE (0x1)
606
#define CRINFO_CTYPE_SH (31)
607
#define CRINFO_RTYPE (0xf)
608
#define CRINFO_RTYPE_SH (27)
609
#define CRINFO_DIST2TO (0xff)
610
#define CRINFO_DIST2TO_SH (19)
611
#define CRINFO_RELVADDR (0x7ffff)
612
#define CRINFO_RELVADDR_SH (0)
613
 
614
/* A compact relocation info has long (3 words) or short (2 words)
615
   formats.  A short format doesn't have VADDR field and relvaddr
616
   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
617
#define CRF_MIPS_LONG                   1
618
#define CRF_MIPS_SHORT                  0
619
 
620
/* There are 4 types of compact relocation at least. The value KONST
621
   has different meaning for each type:
622
 
623
   (type)               (konst)
624
   CT_MIPS_REL32        Address in data
625
   CT_MIPS_WORD         Address in word (XXX)
626
   CT_MIPS_GPHI_LO      GP - vaddr
627
   CT_MIPS_JMPAD        Address to jump
628
   */
629
 
630
#define CRT_MIPS_REL32                  0xa
631
#define CRT_MIPS_WORD                   0xb
632
#define CRT_MIPS_GPHI_LO                0xc
633
#define CRT_MIPS_JMPAD                  0xd
634
 
635
#define mips_elf_set_cr_format(x,format)        ((x).ctype = (format))
636
#define mips_elf_set_cr_type(x,type)            ((x).rtype = (type))
637
#define mips_elf_set_cr_dist2to(x,v)            ((x).dist2to = (v))
638
#define mips_elf_set_cr_relvaddr(x,d)           ((x).relvaddr = (d)<<2)
639
 
640
/* The structure of the runtime procedure descriptor created by the
641
   loader for use by the static exception system.  */
642
 
643
typedef struct runtime_pdr {
644
        bfd_vma adr;            /* Memory address of start of procedure.  */
645
        long    regmask;        /* Save register mask.  */
646
        long    regoffset;      /* Save register offset.  */
647
        long    fregmask;       /* Save floating point register mask.  */
648
        long    fregoffset;     /* Save floating point register offset.  */
649
        long    frameoffset;    /* Frame size.  */
650
        short   framereg;       /* Frame pointer register.  */
651
        short   pcreg;          /* Offset or reg of return pc.  */
652
        long    irpss;          /* Index into the runtime string table.  */
653
        long    reserved;
654
        struct exception_info *exception_info;/* Pointer to exception array.  */
655
} RPDR, *pRPDR;
656
#define cbRPDR sizeof (RPDR)
657
#define rpdNil ((pRPDR) 0)
658
 
659
static struct mips_got_entry *mips_elf_create_local_got_entry
660
  (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
661
   struct mips_elf_link_hash_entry *, int);
662
static bfd_boolean mips_elf_sort_hash_table_f
663
  (struct mips_elf_link_hash_entry *, void *);
664
static bfd_vma mips_elf_high
665
  (bfd_vma);
666
static bfd_boolean mips_elf_create_dynamic_relocation
667
  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
668
   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
669
   bfd_vma *, asection *);
670
static hashval_t mips_elf_got_entry_hash
671
  (const void *);
672
static bfd_vma mips_elf_adjust_gp
673
  (bfd *, struct mips_got_info *, bfd *);
674
static struct mips_got_info *mips_elf_got_for_ibfd
675
  (struct mips_got_info *, bfd *);
676
 
677
/* This will be used when we sort the dynamic relocation records.  */
678
static bfd *reldyn_sorting_bfd;
679
 
680
/* True if ABFD is for CPUs with load interlocking that include
681
   non-MIPS1 CPUs and R3900.  */
682
#define LOAD_INTERLOCKS_P(abfd) \
683
  (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
684
   || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
685
 
686
/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
687
   This should be safe for all architectures.  We enable this predicate
688
   for RM9000 for now.  */
689
#define JAL_TO_BAL_P(abfd) \
690
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
691
 
692
/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
693
   This should be safe for all architectures.  We enable this predicate for
694
   all CPUs.  */
695
#define JALR_TO_BAL_P(abfd) 1
696
 
697
/* True if ABFD is for CPUs that are faster if JR is converted to B.
698
   This should be safe for all architectures.  We enable this predicate for
699
   all CPUs.  */
700
#define JR_TO_B_P(abfd) 1
701
 
702
/* True if ABFD is a PIC object.  */
703
#define PIC_OBJECT_P(abfd) \
704
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
705
 
706
/* Nonzero if ABFD is using the N32 ABI.  */
707
#define ABI_N32_P(abfd) \
708
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
709
 
710
/* Nonzero if ABFD is using the N64 ABI.  */
711
#define ABI_64_P(abfd) \
712
  (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
713
 
714
/* Nonzero if ABFD is using NewABI conventions.  */
715
#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
716
 
717
/* The IRIX compatibility level we are striving for.  */
718
#define IRIX_COMPAT(abfd) \
719
  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
720
 
721
/* Whether we are trying to be compatible with IRIX at all.  */
722
#define SGI_COMPAT(abfd) \
723
  (IRIX_COMPAT (abfd) != ict_none)
724
 
725
/* The name of the options section.  */
726
#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
727
  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
728
 
729
/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
730
   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
731
#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
732
  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
733
 
734
/* Whether the section is readonly.  */
735
#define MIPS_ELF_READONLY_SECTION(sec) \
736
  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))         \
737
   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
738
 
739
/* The name of the stub section.  */
740
#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
741
 
742
/* The size of an external REL relocation.  */
743
#define MIPS_ELF_REL_SIZE(abfd) \
744
  (get_elf_backend_data (abfd)->s->sizeof_rel)
745
 
746
/* The size of an external RELA relocation.  */
747
#define MIPS_ELF_RELA_SIZE(abfd) \
748
  (get_elf_backend_data (abfd)->s->sizeof_rela)
749
 
750
/* The size of an external dynamic table entry.  */
751
#define MIPS_ELF_DYN_SIZE(abfd) \
752
  (get_elf_backend_data (abfd)->s->sizeof_dyn)
753
 
754
/* The size of a GOT entry.  */
755
#define MIPS_ELF_GOT_SIZE(abfd) \
756
  (get_elf_backend_data (abfd)->s->arch_size / 8)
757
 
758
/* The size of a symbol-table entry.  */
759
#define MIPS_ELF_SYM_SIZE(abfd) \
760
  (get_elf_backend_data (abfd)->s->sizeof_sym)
761
 
762
/* The default alignment for sections, as a power of two.  */
763
#define MIPS_ELF_LOG_FILE_ALIGN(abfd)                           \
764
  (get_elf_backend_data (abfd)->s->log_file_align)
765
 
766
/* Get word-sized data.  */
767
#define MIPS_ELF_GET_WORD(abfd, ptr) \
768
  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
769
 
770
/* Put out word-sized data.  */
771
#define MIPS_ELF_PUT_WORD(abfd, val, ptr)       \
772
  (ABI_64_P (abfd)                              \
773
   ? bfd_put_64 (abfd, val, ptr)                \
774
   : bfd_put_32 (abfd, val, ptr))
775
 
776
/* The opcode for word-sized loads (LW or LD).  */
777
#define MIPS_ELF_LOAD_WORD(abfd) \
778
  (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
779
 
780
/* Add a dynamic symbol table-entry.  */
781
#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)      \
782
  _bfd_elf_add_dynamic_entry (info, tag, val)
783
 
784
#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)                      \
785
  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
786
 
787
/* The name of the dynamic relocation section.  */
788
#define MIPS_ELF_REL_DYN_NAME(INFO) \
789
  (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
790
 
791
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
792
   from smaller values.  Start with zero, widen, *then* decrement.  */
793
#define MINUS_ONE       (((bfd_vma)0) - 1)
794
#define MINUS_TWO       (((bfd_vma)0) - 2)
795
 
796
/* The value to write into got[1] for SVR4 targets, to identify it is
797
   a GNU object.  The dynamic linker can then use got[1] to store the
798
   module pointer.  */
799
#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
800
  ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
801
 
802
/* The offset of $gp from the beginning of the .got section.  */
803
#define ELF_MIPS_GP_OFFSET(INFO) \
804
  (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
805
 
806
/* The maximum size of the GOT for it to be addressable using 16-bit
807
   offsets from $gp.  */
808
#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
809
 
810
/* Instructions which appear in a stub.  */
811
#define STUB_LW(abfd)                                                   \
812
  ((ABI_64_P (abfd)                                                     \
813
    ? 0xdf998010                                /* ld t9,0x8010(gp) */  \
814
    : 0x8f998010))                              /* lw t9,0x8010(gp) */
815
#define STUB_MOVE(abfd)                                                 \
816
   ((ABI_64_P (abfd)                                                    \
817
     ? 0x03e0782d                               /* daddu t7,ra */       \
818
     : 0x03e07821))                             /* addu t7,ra */
819
#define STUB_LUI(VAL) (0x3c180000 + (VAL))      /* lui t8,VAL */
820
#define STUB_JALR 0x0320f809                    /* jalr t9,ra */
821
#define STUB_ORI(VAL) (0x37180000 + (VAL))      /* ori t8,t8,VAL */
822
#define STUB_LI16U(VAL) (0x34180000 + (VAL))    /* ori t8,zero,VAL unsigned */
823
#define STUB_LI16S(abfd, VAL)                                           \
824
   ((ABI_64_P (abfd)                                                    \
825
    ? (0x64180000 + (VAL))      /* daddiu t8,zero,VAL sign extended */  \
826
    : (0x24180000 + (VAL))))    /* addiu t8,zero,VAL sign extended */
827
 
828
#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
829
#define MIPS_FUNCTION_STUB_BIG_SIZE 20
830
 
831
/* The name of the dynamic interpreter.  This is put in the .interp
832
   section.  */
833
 
834
#define ELF_DYNAMIC_INTERPRETER(abfd)           \
835
   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"   \
836
    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
837
    : "/usr/lib/libc.so.1")
838
 
839
#ifdef BFD64
840
#define MNAME(bfd,pre,pos) \
841
  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
842
#define ELF_R_SYM(bfd, i)                                       \
843
  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
844
#define ELF_R_TYPE(bfd, i)                                      \
845
  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
846
#define ELF_R_INFO(bfd, s, t)                                   \
847
  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
848
#else
849
#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
850
#define ELF_R_SYM(bfd, i)                                       \
851
  (ELF32_R_SYM (i))
852
#define ELF_R_TYPE(bfd, i)                                      \
853
  (ELF32_R_TYPE (i))
854
#define ELF_R_INFO(bfd, s, t)                                   \
855
  (ELF32_R_INFO (s, t))
856
#endif
857
 
858
  /* The mips16 compiler uses a couple of special sections to handle
859
     floating point arguments.
860
 
861
     Section names that look like .mips16.fn.FNNAME contain stubs that
862
     copy floating point arguments from the fp regs to the gp regs and
863
     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
864
     call should be redirected to the stub instead.  If no 32 bit
865
     function calls FNNAME, the stub should be discarded.  We need to
866
     consider any reference to the function, not just a call, because
867
     if the address of the function is taken we will need the stub,
868
     since the address might be passed to a 32 bit function.
869
 
870
     Section names that look like .mips16.call.FNNAME contain stubs
871
     that copy floating point arguments from the gp regs to the fp
872
     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
873
     then any 16 bit function that calls FNNAME should be redirected
874
     to the stub instead.  If FNNAME is not a 32 bit function, the
875
     stub should be discarded.
876
 
877
     .mips16.call.fp.FNNAME sections are similar, but contain stubs
878
     which call FNNAME and then copy the return value from the fp regs
879
     to the gp regs.  These stubs store the return value in $18 while
880
     calling FNNAME; any function which might call one of these stubs
881
     must arrange to save $18 around the call.  (This case is not
882
     needed for 32 bit functions that call 16 bit functions, because
883
     16 bit functions always return floating point values in both
884
     $f0/$f1 and $2/$3.)
885
 
886
     Note that in all cases FNNAME might be defined statically.
887
     Therefore, FNNAME is not used literally.  Instead, the relocation
888
     information will indicate which symbol the section is for.
889
 
890
     We record any stubs that we find in the symbol table.  */
891
 
892
#define FN_STUB ".mips16.fn."
893
#define CALL_STUB ".mips16.call."
894
#define CALL_FP_STUB ".mips16.call.fp."
895
 
896
#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
897
#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
898
#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
899
 
900
/* The format of the first PLT entry in an O32 executable.  */
901
static const bfd_vma mips_o32_exec_plt0_entry[] =
902
{
903
  0x3c1c0000,   /* lui $28, %hi(&GOTPLT[0])                             */
904
  0x8f990000,   /* lw $25, %lo(&GOTPLT[0])($28)                         */
905
  0x279c0000,   /* addiu $28, $28, %lo(&GOTPLT[0])                      */
906
  0x031cc023,   /* subu $24, $24, $28                                   */
907
  0x03e07821,   /* move $15, $31                                        */
908
  0x0018c082,   /* srl $24, $24, 2                                      */
909
  0x0320f809,   /* jalr $25                                             */
910
  0x2718fffe    /* subu $24, $24, 2                                     */
911
};
912
 
913
/* The format of the first PLT entry in an N32 executable.  Different
914
   because gp ($28) is not available; we use t2 ($14) instead.  */
915
static const bfd_vma mips_n32_exec_plt0_entry[] =
916
{
917
  0x3c0e0000,   /* lui $14, %hi(&GOTPLT[0])                             */
918
  0x8dd90000,   /* lw $25, %lo(&GOTPLT[0])($14)                         */
919
  0x25ce0000,   /* addiu $14, $14, %lo(&GOTPLT[0])                      */
920
  0x030ec023,   /* subu $24, $24, $14                                   */
921
  0x03e07821,   /* move $15, $31                                        */
922
  0x0018c082,   /* srl $24, $24, 2                                      */
923
  0x0320f809,   /* jalr $25                                             */
924
  0x2718fffe    /* subu $24, $24, 2                                     */
925
};
926
 
927
/* The format of the first PLT entry in an N64 executable.  Different
928
   from N32 because of the increased size of GOT entries.  */
929
static const bfd_vma mips_n64_exec_plt0_entry[] =
930
{
931
  0x3c0e0000,   /* lui $14, %hi(&GOTPLT[0])                             */
932
  0xddd90000,   /* ld $25, %lo(&GOTPLT[0])($14)                         */
933
  0x25ce0000,   /* addiu $14, $14, %lo(&GOTPLT[0])                      */
934
  0x030ec023,   /* subu $24, $24, $14                                   */
935
  0x03e07821,   /* move $15, $31                                        */
936
  0x0018c0c2,   /* srl $24, $24, 3                                      */
937
  0x0320f809,   /* jalr $25                                             */
938
  0x2718fffe    /* subu $24, $24, 2                                     */
939
};
940
 
941
/* The format of subsequent PLT entries.  */
942
static const bfd_vma mips_exec_plt_entry[] =
943
{
944
  0x3c0f0000,   /* lui $15, %hi(.got.plt entry)                 */
945
  0x01f90000,   /* l[wd] $25, %lo(.got.plt entry)($15)          */
946
  0x25f80000,   /* addiu $24, $15, %lo(.got.plt entry)          */
947
  0x03200008    /* jr $25                                       */
948
};
949
 
950
/* The format of the first PLT entry in a VxWorks executable.  */
951
static const bfd_vma mips_vxworks_exec_plt0_entry[] =
952
{
953
  0x3c190000,   /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)           */
954
  0x27390000,   /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)     */
955
  0x8f390008,   /* lw t9, 8(t9)                                 */
956
  0x00000000,   /* nop                                          */
957
  0x03200008,   /* jr t9                                        */
958
  0x00000000    /* nop                                          */
959
};
960
 
961
/* The format of subsequent PLT entries.  */
962
static const bfd_vma mips_vxworks_exec_plt_entry[] =
963
{
964
  0x10000000,   /* b .PLT_resolver                      */
965
  0x24180000,   /* li t8, <pltindex>                    */
966
  0x3c190000,   /* lui t9, %hi(<.got.plt slot>)         */
967
  0x27390000,   /* addiu t9, t9, %lo(<.got.plt slot>)   */
968
  0x8f390000,   /* lw t9, 0(t9)                         */
969
  0x00000000,   /* nop                                  */
970
  0x03200008,   /* jr t9                                */
971
  0x00000000    /* nop                                  */
972
};
973
 
974
/* The format of the first PLT entry in a VxWorks shared object.  */
975
static const bfd_vma mips_vxworks_shared_plt0_entry[] =
976
{
977
  0x8f990008,   /* lw t9, 8(gp)         */
978
  0x00000000,   /* nop                  */
979
  0x03200008,   /* jr t9                */
980
  0x00000000,   /* nop                  */
981
  0x00000000,   /* nop                  */
982
  0x00000000    /* nop                  */
983
};
984
 
985
/* The format of subsequent PLT entries.  */
986
static const bfd_vma mips_vxworks_shared_plt_entry[] =
987
{
988
  0x10000000,   /* b .PLT_resolver      */
989
  0x24180000    /* li t8, <pltindex>    */
990
};
991
 
992
/* Look up an entry in a MIPS ELF linker hash table.  */
993
 
994
#define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
995
  ((struct mips_elf_link_hash_entry *)                                  \
996
   elf_link_hash_lookup (&(table)->root, (string), (create),            \
997
                         (copy), (follow)))
998
 
999
/* Traverse a MIPS ELF linker hash table.  */
1000
 
1001
#define mips_elf_link_hash_traverse(table, func, info)                  \
1002
  (elf_link_hash_traverse                                               \
1003
   (&(table)->root,                                                     \
1004
    (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
1005
    (info)))
1006
 
1007
/* Find the base offsets for thread-local storage in this object,
1008
   for GD/LD and IE/LE respectively.  */
1009
 
1010
#define TP_OFFSET 0x7000
1011
#define DTP_OFFSET 0x8000
1012
 
1013
static bfd_vma
1014
dtprel_base (struct bfd_link_info *info)
1015
{
1016
  /* If tls_sec is NULL, we should have signalled an error already.  */
1017
  if (elf_hash_table (info)->tls_sec == NULL)
1018
    return 0;
1019
  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1020
}
1021
 
1022
static bfd_vma
1023
tprel_base (struct bfd_link_info *info)
1024
{
1025
  /* If tls_sec is NULL, we should have signalled an error already.  */
1026
  if (elf_hash_table (info)->tls_sec == NULL)
1027
    return 0;
1028
  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1029
}
1030
 
1031
/* Create an entry in a MIPS ELF linker hash table.  */
1032
 
1033
static struct bfd_hash_entry *
1034
mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1035
                            struct bfd_hash_table *table, const char *string)
1036
{
1037
  struct mips_elf_link_hash_entry *ret =
1038
    (struct mips_elf_link_hash_entry *) entry;
1039
 
1040
  /* Allocate the structure if it has not already been allocated by a
1041
     subclass.  */
1042
  if (ret == NULL)
1043
    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1044
  if (ret == NULL)
1045
    return (struct bfd_hash_entry *) ret;
1046
 
1047
  /* Call the allocation method of the superclass.  */
1048
  ret = ((struct mips_elf_link_hash_entry *)
1049
         _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1050
                                     table, string));
1051
  if (ret != NULL)
1052
    {
1053
      /* Set local fields.  */
1054
      memset (&ret->esym, 0, sizeof (EXTR));
1055
      /* We use -2 as a marker to indicate that the information has
1056
         not been set.  -1 means there is no associated ifd.  */
1057
      ret->esym.ifd = -2;
1058
      ret->la25_stub = 0;
1059
      ret->possibly_dynamic_relocs = 0;
1060
      ret->fn_stub = NULL;
1061
      ret->call_stub = NULL;
1062
      ret->call_fp_stub = NULL;
1063
      ret->tls_type = GOT_NORMAL;
1064
      ret->global_got_area = GGA_NONE;
1065
      ret->got_only_for_calls = TRUE;
1066
      ret->readonly_reloc = FALSE;
1067
      ret->has_static_relocs = FALSE;
1068
      ret->no_fn_stub = FALSE;
1069
      ret->need_fn_stub = FALSE;
1070
      ret->has_nonpic_branches = FALSE;
1071
      ret->needs_lazy_stub = FALSE;
1072
    }
1073
 
1074
  return (struct bfd_hash_entry *) ret;
1075
}
1076
 
1077
bfd_boolean
1078
_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1079
{
1080
  if (!sec->used_by_bfd)
1081
    {
1082
      struct _mips_elf_section_data *sdata;
1083
      bfd_size_type amt = sizeof (*sdata);
1084
 
1085
      sdata = bfd_zalloc (abfd, amt);
1086
      if (sdata == NULL)
1087
        return FALSE;
1088
      sec->used_by_bfd = sdata;
1089
    }
1090
 
1091
  return _bfd_elf_new_section_hook (abfd, sec);
1092
}
1093
 
1094
/* Read ECOFF debugging information from a .mdebug section into a
1095
   ecoff_debug_info structure.  */
1096
 
1097
bfd_boolean
1098
_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1099
                               struct ecoff_debug_info *debug)
1100
{
1101
  HDRR *symhdr;
1102
  const struct ecoff_debug_swap *swap;
1103
  char *ext_hdr;
1104
 
1105
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1106
  memset (debug, 0, sizeof (*debug));
1107
 
1108
  ext_hdr = bfd_malloc (swap->external_hdr_size);
1109
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1110
    goto error_return;
1111
 
1112
  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1113
                                  swap->external_hdr_size))
1114
    goto error_return;
1115
 
1116
  symhdr = &debug->symbolic_header;
1117
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1118
 
1119
  /* The symbolic header contains absolute file offsets and sizes to
1120
     read.  */
1121
#define READ(ptr, offset, count, size, type)                            \
1122
  if (symhdr->count == 0)                                                \
1123
    debug->ptr = NULL;                                                  \
1124
  else                                                                  \
1125
    {                                                                   \
1126
      bfd_size_type amt = (bfd_size_type) size * symhdr->count;         \
1127
      debug->ptr = bfd_malloc (amt);                                    \
1128
      if (debug->ptr == NULL)                                           \
1129
        goto error_return;                                              \
1130
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0         \
1131
          || bfd_bread (debug->ptr, amt, abfd) != amt)                  \
1132
        goto error_return;                                              \
1133
    }
1134
 
1135
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1136
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1137
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1138
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1139
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1140
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1141
        union aux_ext *);
1142
  READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1143
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1144
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1145
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1146
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1147
#undef READ
1148
 
1149
  debug->fdr = NULL;
1150
 
1151
  return TRUE;
1152
 
1153
 error_return:
1154
  if (ext_hdr != NULL)
1155
    free (ext_hdr);
1156
  if (debug->line != NULL)
1157
    free (debug->line);
1158
  if (debug->external_dnr != NULL)
1159
    free (debug->external_dnr);
1160
  if (debug->external_pdr != NULL)
1161
    free (debug->external_pdr);
1162
  if (debug->external_sym != NULL)
1163
    free (debug->external_sym);
1164
  if (debug->external_opt != NULL)
1165
    free (debug->external_opt);
1166
  if (debug->external_aux != NULL)
1167
    free (debug->external_aux);
1168
  if (debug->ss != NULL)
1169
    free (debug->ss);
1170
  if (debug->ssext != NULL)
1171
    free (debug->ssext);
1172
  if (debug->external_fdr != NULL)
1173
    free (debug->external_fdr);
1174
  if (debug->external_rfd != NULL)
1175
    free (debug->external_rfd);
1176
  if (debug->external_ext != NULL)
1177
    free (debug->external_ext);
1178
  return FALSE;
1179
}
1180
 
1181
/* Swap RPDR (runtime procedure table entry) for output.  */
1182
 
1183
static void
1184
ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1185
{
1186
  H_PUT_S32 (abfd, in->adr, ex->p_adr);
1187
  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1188
  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1189
  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1190
  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1191
  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1192
 
1193
  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1194
  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1195
 
1196
  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1197
}
1198
 
1199
/* Create a runtime procedure table from the .mdebug section.  */
1200
 
1201
static bfd_boolean
1202
mips_elf_create_procedure_table (void *handle, bfd *abfd,
1203
                                 struct bfd_link_info *info, asection *s,
1204
                                 struct ecoff_debug_info *debug)
1205
{
1206
  const struct ecoff_debug_swap *swap;
1207
  HDRR *hdr = &debug->symbolic_header;
1208
  RPDR *rpdr, *rp;
1209
  struct rpdr_ext *erp;
1210
  void *rtproc;
1211
  struct pdr_ext *epdr;
1212
  struct sym_ext *esym;
1213
  char *ss, **sv;
1214
  char *str;
1215
  bfd_size_type size;
1216
  bfd_size_type count;
1217
  unsigned long sindex;
1218
  unsigned long i;
1219
  PDR pdr;
1220
  SYMR sym;
1221
  const char *no_name_func = _("static procedure (no name)");
1222
 
1223
  epdr = NULL;
1224
  rpdr = NULL;
1225
  esym = NULL;
1226
  ss = NULL;
1227
  sv = NULL;
1228
 
1229
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1230
 
1231
  sindex = strlen (no_name_func) + 1;
1232
  count = hdr->ipdMax;
1233
  if (count > 0)
1234
    {
1235
      size = swap->external_pdr_size;
1236
 
1237
      epdr = bfd_malloc (size * count);
1238
      if (epdr == NULL)
1239
        goto error_return;
1240
 
1241
      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1242
        goto error_return;
1243
 
1244
      size = sizeof (RPDR);
1245
      rp = rpdr = bfd_malloc (size * count);
1246
      if (rpdr == NULL)
1247
        goto error_return;
1248
 
1249
      size = sizeof (char *);
1250
      sv = bfd_malloc (size * count);
1251
      if (sv == NULL)
1252
        goto error_return;
1253
 
1254
      count = hdr->isymMax;
1255
      size = swap->external_sym_size;
1256
      esym = bfd_malloc (size * count);
1257
      if (esym == NULL)
1258
        goto error_return;
1259
 
1260
      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1261
        goto error_return;
1262
 
1263
      count = hdr->issMax;
1264
      ss = bfd_malloc (count);
1265
      if (ss == NULL)
1266
        goto error_return;
1267
      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1268
        goto error_return;
1269
 
1270
      count = hdr->ipdMax;
1271
      for (i = 0; i < (unsigned long) count; i++, rp++)
1272
        {
1273
          (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1274
          (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1275
          rp->adr = sym.value;
1276
          rp->regmask = pdr.regmask;
1277
          rp->regoffset = pdr.regoffset;
1278
          rp->fregmask = pdr.fregmask;
1279
          rp->fregoffset = pdr.fregoffset;
1280
          rp->frameoffset = pdr.frameoffset;
1281
          rp->framereg = pdr.framereg;
1282
          rp->pcreg = pdr.pcreg;
1283
          rp->irpss = sindex;
1284
          sv[i] = ss + sym.iss;
1285
          sindex += strlen (sv[i]) + 1;
1286
        }
1287
    }
1288
 
1289
  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1290
  size = BFD_ALIGN (size, 16);
1291
  rtproc = bfd_alloc (abfd, size);
1292
  if (rtproc == NULL)
1293
    {
1294
      mips_elf_hash_table (info)->procedure_count = 0;
1295
      goto error_return;
1296
    }
1297
 
1298
  mips_elf_hash_table (info)->procedure_count = count + 2;
1299
 
1300
  erp = rtproc;
1301
  memset (erp, 0, sizeof (struct rpdr_ext));
1302
  erp++;
1303
  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1304
  strcpy (str, no_name_func);
1305
  str += strlen (no_name_func) + 1;
1306
  for (i = 0; i < count; i++)
1307
    {
1308
      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1309
      strcpy (str, sv[i]);
1310
      str += strlen (sv[i]) + 1;
1311
    }
1312
  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1313
 
1314
  /* Set the size and contents of .rtproc section.  */
1315
  s->size = size;
1316
  s->contents = rtproc;
1317
 
1318
  /* Skip this section later on (I don't think this currently
1319
     matters, but someday it might).  */
1320
  s->map_head.link_order = NULL;
1321
 
1322
  if (epdr != NULL)
1323
    free (epdr);
1324
  if (rpdr != NULL)
1325
    free (rpdr);
1326
  if (esym != NULL)
1327
    free (esym);
1328
  if (ss != NULL)
1329
    free (ss);
1330
  if (sv != NULL)
1331
    free (sv);
1332
 
1333
  return TRUE;
1334
 
1335
 error_return:
1336
  if (epdr != NULL)
1337
    free (epdr);
1338
  if (rpdr != NULL)
1339
    free (rpdr);
1340
  if (esym != NULL)
1341
    free (esym);
1342
  if (ss != NULL)
1343
    free (ss);
1344
  if (sv != NULL)
1345
    free (sv);
1346
  return FALSE;
1347
}
1348
 
1349
/* We're going to create a stub for H.  Create a symbol for the stub's
1350
   value and size, to help make the disassembly easier to read.  */
1351
 
1352
static bfd_boolean
1353
mips_elf_create_stub_symbol (struct bfd_link_info *info,
1354
                             struct mips_elf_link_hash_entry *h,
1355
                             const char *prefix, asection *s, bfd_vma value,
1356
                             bfd_vma size)
1357
{
1358
  struct bfd_link_hash_entry *bh;
1359
  struct elf_link_hash_entry *elfh;
1360
  const char *name;
1361
 
1362
  /* Create a new symbol.  */
1363
  name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1364
  bh = NULL;
1365
  if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1366
                                         BSF_LOCAL, s, value, NULL,
1367
                                         TRUE, FALSE, &bh))
1368
    return FALSE;
1369
 
1370
  /* Make it a local function.  */
1371
  elfh = (struct elf_link_hash_entry *) bh;
1372
  elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1373
  elfh->size = size;
1374
  elfh->forced_local = 1;
1375
  return TRUE;
1376
}
1377
 
1378
/* We're about to redefine H.  Create a symbol to represent H's
1379
   current value and size, to help make the disassembly easier
1380
   to read.  */
1381
 
1382
static bfd_boolean
1383
mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1384
                               struct mips_elf_link_hash_entry *h,
1385
                               const char *prefix)
1386
{
1387
  struct bfd_link_hash_entry *bh;
1388
  struct elf_link_hash_entry *elfh;
1389
  const char *name;
1390
  asection *s;
1391
  bfd_vma value;
1392
 
1393
  /* Read the symbol's value.  */
1394
  BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1395
              || h->root.root.type == bfd_link_hash_defweak);
1396
  s = h->root.root.u.def.section;
1397
  value = h->root.root.u.def.value;
1398
 
1399
  /* Create a new symbol.  */
1400
  name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1401
  bh = NULL;
1402
  if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1403
                                         BSF_LOCAL, s, value, NULL,
1404
                                         TRUE, FALSE, &bh))
1405
    return FALSE;
1406
 
1407
  /* Make it local and copy the other attributes from H.  */
1408
  elfh = (struct elf_link_hash_entry *) bh;
1409
  elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1410
  elfh->other = h->root.other;
1411
  elfh->size = h->root.size;
1412
  elfh->forced_local = 1;
1413
  return TRUE;
1414
}
1415
 
1416
/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1417
   function rather than to a hard-float stub.  */
1418
 
1419
static bfd_boolean
1420
section_allows_mips16_refs_p (asection *section)
1421
{
1422
  const char *name;
1423
 
1424
  name = bfd_get_section_name (section->owner, section);
1425
  return (FN_STUB_P (name)
1426
          || CALL_STUB_P (name)
1427
          || CALL_FP_STUB_P (name)
1428
          || strcmp (name, ".pdr") == 0);
1429
}
1430
 
1431
/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1432
   stub section of some kind.  Return the R_SYMNDX of the target
1433
   function, or 0 if we can't decide which function that is.  */
1434
 
1435
static unsigned long
1436
mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1437
                    const Elf_Internal_Rela *relocs,
1438
                    const Elf_Internal_Rela *relend)
1439
{
1440
  const Elf_Internal_Rela *rel;
1441
 
1442
  /* Trust the first R_MIPS_NONE relocation, if any.  */
1443
  for (rel = relocs; rel < relend; rel++)
1444
    if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1445
      return ELF_R_SYM (sec->owner, rel->r_info);
1446
 
1447
  /* Otherwise trust the first relocation, whatever its kind.  This is
1448
     the traditional behavior.  */
1449
  if (relocs < relend)
1450
    return ELF_R_SYM (sec->owner, relocs->r_info);
1451
 
1452
  return 0;
1453
}
1454
 
1455
/* Check the mips16 stubs for a particular symbol, and see if we can
1456
   discard them.  */
1457
 
1458
static void
1459
mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1460
                             struct mips_elf_link_hash_entry *h)
1461
{
1462
  /* Dynamic symbols must use the standard call interface, in case other
1463
     objects try to call them.  */
1464
  if (h->fn_stub != NULL
1465
      && h->root.dynindx != -1)
1466
    {
1467
      mips_elf_create_shadow_symbol (info, h, ".mips16.");
1468
      h->need_fn_stub = TRUE;
1469
    }
1470
 
1471
  if (h->fn_stub != NULL
1472
      && ! h->need_fn_stub)
1473
    {
1474
      /* We don't need the fn_stub; the only references to this symbol
1475
         are 16 bit calls.  Clobber the size to 0 to prevent it from
1476
         being included in the link.  */
1477
      h->fn_stub->size = 0;
1478
      h->fn_stub->flags &= ~SEC_RELOC;
1479
      h->fn_stub->reloc_count = 0;
1480
      h->fn_stub->flags |= SEC_EXCLUDE;
1481
    }
1482
 
1483
  if (h->call_stub != NULL
1484
      && ELF_ST_IS_MIPS16 (h->root.other))
1485
    {
1486
      /* We don't need the call_stub; this is a 16 bit function, so
1487
         calls from other 16 bit functions are OK.  Clobber the size
1488
         to 0 to prevent it from being included in the link.  */
1489
      h->call_stub->size = 0;
1490
      h->call_stub->flags &= ~SEC_RELOC;
1491
      h->call_stub->reloc_count = 0;
1492
      h->call_stub->flags |= SEC_EXCLUDE;
1493
    }
1494
 
1495
  if (h->call_fp_stub != NULL
1496
      && ELF_ST_IS_MIPS16 (h->root.other))
1497
    {
1498
      /* We don't need the call_stub; this is a 16 bit function, so
1499
         calls from other 16 bit functions are OK.  Clobber the size
1500
         to 0 to prevent it from being included in the link.  */
1501
      h->call_fp_stub->size = 0;
1502
      h->call_fp_stub->flags &= ~SEC_RELOC;
1503
      h->call_fp_stub->reloc_count = 0;
1504
      h->call_fp_stub->flags |= SEC_EXCLUDE;
1505
    }
1506
}
1507
 
1508
/* Hashtable callbacks for mips_elf_la25_stubs.  */
1509
 
1510
static hashval_t
1511
mips_elf_la25_stub_hash (const void *entry_)
1512
{
1513
  const struct mips_elf_la25_stub *entry;
1514
 
1515
  entry = (struct mips_elf_la25_stub *) entry_;
1516
  return entry->h->root.root.u.def.section->id
1517
    + entry->h->root.root.u.def.value;
1518
}
1519
 
1520
static int
1521
mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1522
{
1523
  const struct mips_elf_la25_stub *entry1, *entry2;
1524
 
1525
  entry1 = (struct mips_elf_la25_stub *) entry1_;
1526
  entry2 = (struct mips_elf_la25_stub *) entry2_;
1527
  return ((entry1->h->root.root.u.def.section
1528
           == entry2->h->root.root.u.def.section)
1529
          && (entry1->h->root.root.u.def.value
1530
              == entry2->h->root.root.u.def.value));
1531
}
1532
 
1533
/* Called by the linker to set up the la25 stub-creation code.  FN is
1534
   the linker's implementation of add_stub_function.  Return true on
1535
   success.  */
1536
 
1537
bfd_boolean
1538
_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1539
                          asection *(*fn) (const char *, asection *,
1540
                                           asection *))
1541
{
1542
  struct mips_elf_link_hash_table *htab;
1543
 
1544
  htab = mips_elf_hash_table (info);
1545
  if (htab == NULL)
1546
    return FALSE;
1547
 
1548
  htab->add_stub_section = fn;
1549
  htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1550
                                      mips_elf_la25_stub_eq, NULL);
1551
  if (htab->la25_stubs == NULL)
1552
    return FALSE;
1553
 
1554
  return TRUE;
1555
}
1556
 
1557
/* Return true if H is a locally-defined PIC function, in the sense
1558
   that it might need $25 to be valid on entry.  Note that MIPS16
1559
   functions never need $25 to be valid on entry; they set up $gp
1560
   using PC-relative instructions instead.  */
1561
 
1562
static bfd_boolean
1563
mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1564
{
1565
  return ((h->root.root.type == bfd_link_hash_defined
1566
           || h->root.root.type == bfd_link_hash_defweak)
1567
          && h->root.def_regular
1568
          && !bfd_is_abs_section (h->root.root.u.def.section)
1569
          && !ELF_ST_IS_MIPS16 (h->root.other)
1570
          && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1571
              || ELF_ST_IS_MIPS_PIC (h->root.other)));
1572
}
1573
 
1574
/* STUB describes an la25 stub that we have decided to implement
1575
   by inserting an LUI/ADDIU pair before the target function.
1576
   Create the section and redirect the function symbol to it.  */
1577
 
1578
static bfd_boolean
1579
mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1580
                         struct bfd_link_info *info)
1581
{
1582
  struct mips_elf_link_hash_table *htab;
1583
  char *name;
1584
  asection *s, *input_section;
1585
  unsigned int align;
1586
 
1587
  htab = mips_elf_hash_table (info);
1588
  if (htab == NULL)
1589
    return FALSE;
1590
 
1591
  /* Create a unique name for the new section.  */
1592
  name = bfd_malloc (11 + sizeof (".text.stub."));
1593
  if (name == NULL)
1594
    return FALSE;
1595
  sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1596
 
1597
  /* Create the section.  */
1598
  input_section = stub->h->root.root.u.def.section;
1599
  s = htab->add_stub_section (name, input_section,
1600
                              input_section->output_section);
1601
  if (s == NULL)
1602
    return FALSE;
1603
 
1604
  /* Make sure that any padding goes before the stub.  */
1605
  align = input_section->alignment_power;
1606
  if (!bfd_set_section_alignment (s->owner, s, align))
1607
    return FALSE;
1608
  if (align > 3)
1609
    s->size = (1 << align) - 8;
1610
 
1611
  /* Create a symbol for the stub.  */
1612
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1613
  stub->stub_section = s;
1614
  stub->offset = s->size;
1615
 
1616
  /* Allocate room for it.  */
1617
  s->size += 8;
1618
  return TRUE;
1619
}
1620
 
1621
/* STUB describes an la25 stub that we have decided to implement
1622
   with a separate trampoline.  Allocate room for it and redirect
1623
   the function symbol to it.  */
1624
 
1625
static bfd_boolean
1626
mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1627
                              struct bfd_link_info *info)
1628
{
1629
  struct mips_elf_link_hash_table *htab;
1630
  asection *s;
1631
 
1632
  htab = mips_elf_hash_table (info);
1633
  if (htab == NULL)
1634
    return FALSE;
1635
 
1636
  /* Create a trampoline section, if we haven't already.  */
1637
  s = htab->strampoline;
1638
  if (s == NULL)
1639
    {
1640
      asection *input_section = stub->h->root.root.u.def.section;
1641
      s = htab->add_stub_section (".text", NULL,
1642
                                  input_section->output_section);
1643
      if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1644
        return FALSE;
1645
      htab->strampoline = s;
1646
    }
1647
 
1648
  /* Create a symbol for the stub.  */
1649
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1650
  stub->stub_section = s;
1651
  stub->offset = s->size;
1652
 
1653
  /* Allocate room for it.  */
1654
  s->size += 16;
1655
  return TRUE;
1656
}
1657
 
1658
/* H describes a symbol that needs an la25 stub.  Make sure that an
1659
   appropriate stub exists and point H at it.  */
1660
 
1661
static bfd_boolean
1662
mips_elf_add_la25_stub (struct bfd_link_info *info,
1663
                        struct mips_elf_link_hash_entry *h)
1664
{
1665
  struct mips_elf_link_hash_table *htab;
1666
  struct mips_elf_la25_stub search, *stub;
1667
  bfd_boolean use_trampoline_p;
1668
  asection *s;
1669
  bfd_vma value;
1670
  void **slot;
1671
 
1672
  /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1673
     of the section and if we would need no more than 2 nops.  */
1674
  s = h->root.root.u.def.section;
1675
  value = h->root.root.u.def.value;
1676
  use_trampoline_p = (value != 0 || s->alignment_power > 4);
1677
 
1678
  /* Describe the stub we want.  */
1679
  search.stub_section = NULL;
1680
  search.offset = 0;
1681
  search.h = h;
1682
 
1683
  /* See if we've already created an equivalent stub.  */
1684
  htab = mips_elf_hash_table (info);
1685
  if (htab == NULL)
1686
    return FALSE;
1687
 
1688
  slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1689
  if (slot == NULL)
1690
    return FALSE;
1691
 
1692
  stub = (struct mips_elf_la25_stub *) *slot;
1693
  if (stub != NULL)
1694
    {
1695
      /* We can reuse the existing stub.  */
1696
      h->la25_stub = stub;
1697
      return TRUE;
1698
    }
1699
 
1700
  /* Create a permanent copy of ENTRY and add it to the hash table.  */
1701
  stub = bfd_malloc (sizeof (search));
1702
  if (stub == NULL)
1703
    return FALSE;
1704
  *stub = search;
1705
  *slot = stub;
1706
 
1707
  h->la25_stub = stub;
1708
  return (use_trampoline_p
1709
          ? mips_elf_add_la25_trampoline (stub, info)
1710
          : mips_elf_add_la25_intro (stub, info));
1711
}
1712
 
1713
/* A mips_elf_link_hash_traverse callback that is called before sizing
1714
   sections.  DATA points to a mips_htab_traverse_info structure.  */
1715
 
1716
static bfd_boolean
1717
mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1718
{
1719
  struct mips_htab_traverse_info *hti;
1720
 
1721
  hti = (struct mips_htab_traverse_info *) data;
1722
  if (h->root.root.type == bfd_link_hash_warning)
1723
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1724
 
1725
  if (!hti->info->relocatable)
1726
    mips_elf_check_mips16_stubs (hti->info, h);
1727
 
1728
  if (mips_elf_local_pic_function_p (h))
1729
    {
1730
      /* H is a function that might need $25 to be valid on entry.
1731
         If we're creating a non-PIC relocatable object, mark H as
1732
         being PIC.  If we're creating a non-relocatable object with
1733
         non-PIC branches and jumps to H, make sure that H has an la25
1734
         stub.  */
1735
      if (hti->info->relocatable)
1736
        {
1737
          if (!PIC_OBJECT_P (hti->output_bfd))
1738
            h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1739
        }
1740
      else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1741
        {
1742
          hti->error = TRUE;
1743
          return FALSE;
1744
        }
1745
    }
1746
  return TRUE;
1747
}
1748
 
1749
/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1750
   Most mips16 instructions are 16 bits, but these instructions
1751
   are 32 bits.
1752
 
1753
   The format of these instructions is:
1754
 
1755
   +--------------+--------------------------------+
1756
   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
1757
   +--------------+--------------------------------+
1758
   |                Immediate  15:0                |
1759
   +-----------------------------------------------+
1760
 
1761
   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
1762
   Note that the immediate value in the first word is swapped.
1763
 
1764
   When producing a relocatable object file, R_MIPS16_26 is
1765
   handled mostly like R_MIPS_26.  In particular, the addend is
1766
   stored as a straight 26-bit value in a 32-bit instruction.
1767
   (gas makes life simpler for itself by never adjusting a
1768
   R_MIPS16_26 reloc to be against a section, so the addend is
1769
   always zero).  However, the 32 bit instruction is stored as 2
1770
   16-bit values, rather than a single 32-bit value.  In a
1771
   big-endian file, the result is the same; in a little-endian
1772
   file, the two 16-bit halves of the 32 bit value are swapped.
1773
   This is so that a disassembler can recognize the jal
1774
   instruction.
1775
 
1776
   When doing a final link, R_MIPS16_26 is treated as a 32 bit
1777
   instruction stored as two 16-bit values.  The addend A is the
1778
   contents of the targ26 field.  The calculation is the same as
1779
   R_MIPS_26.  When storing the calculated value, reorder the
1780
   immediate value as shown above, and don't forget to store the
1781
   value as two 16-bit values.
1782
 
1783
   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1784
   defined as
1785
 
1786
   big-endian:
1787
   +--------+----------------------+
1788
   |        |                      |
1789
   |        |    targ26-16         |
1790
   |31    26|25                   0|
1791
   +--------+----------------------+
1792
 
1793
   little-endian:
1794
   +----------+------+-------------+
1795
   |          |      |             |
1796
   |  sub1    |      |     sub2    |
1797
   |0        9|10  15|16         31|
1798
   +----------+--------------------+
1799
   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1800
   ((sub1 << 16) | sub2)).
1801
 
1802
   When producing a relocatable object file, the calculation is
1803
   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1804
   When producing a fully linked file, the calculation is
1805
   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1806
   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1807
 
1808
   The table below lists the other MIPS16 instruction relocations.
1809
   Each one is calculated in the same way as the non-MIPS16 relocation
1810
   given on the right, but using the extended MIPS16 layout of 16-bit
1811
   immediate fields:
1812
 
1813
        R_MIPS16_GPREL          R_MIPS_GPREL16
1814
        R_MIPS16_GOT16          R_MIPS_GOT16
1815
        R_MIPS16_CALL16         R_MIPS_CALL16
1816
        R_MIPS16_HI16           R_MIPS_HI16
1817
        R_MIPS16_LO16           R_MIPS_LO16
1818
 
1819
   A typical instruction will have a format like this:
1820
 
1821
   +--------------+--------------------------------+
1822
   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
1823
   +--------------+--------------------------------+
1824
   |    Major     |   rx   |   ry   |   Imm  4:0   |
1825
   +--------------+--------------------------------+
1826
 
1827
   EXTEND is the five bit value 11110.  Major is the instruction
1828
   opcode.
1829
 
1830
   All we need to do here is shuffle the bits appropriately.
1831
   As above, the two 16-bit halves must be swapped on a
1832
   little-endian system.  */
1833
 
1834
static inline bfd_boolean
1835
mips16_reloc_p (int r_type)
1836
{
1837
  switch (r_type)
1838
    {
1839
    case R_MIPS16_26:
1840
    case R_MIPS16_GPREL:
1841
    case R_MIPS16_GOT16:
1842
    case R_MIPS16_CALL16:
1843
    case R_MIPS16_HI16:
1844
    case R_MIPS16_LO16:
1845
      return TRUE;
1846
 
1847
    default:
1848
      return FALSE;
1849
    }
1850
}
1851
 
1852
static inline bfd_boolean
1853
got16_reloc_p (int r_type)
1854
{
1855
  return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1856
}
1857
 
1858
static inline bfd_boolean
1859
call16_reloc_p (int r_type)
1860
{
1861
  return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1862
}
1863
 
1864
static inline bfd_boolean
1865
hi16_reloc_p (int r_type)
1866
{
1867
  return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1868
}
1869
 
1870
static inline bfd_boolean
1871
lo16_reloc_p (int r_type)
1872
{
1873
  return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1874
}
1875
 
1876
static inline bfd_boolean
1877
mips16_call_reloc_p (int r_type)
1878
{
1879
  return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1880
}
1881
 
1882
static inline bfd_boolean
1883
jal_reloc_p (int r_type)
1884
{
1885
  return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1886
}
1887
 
1888
void
1889
_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1890
                                 bfd_boolean jal_shuffle, bfd_byte *data)
1891
{
1892
  bfd_vma extend, insn, val;
1893
 
1894
  if (!mips16_reloc_p (r_type))
1895
    return;
1896
 
1897
  /* Pick up the mips16 extend instruction and the real instruction.  */
1898
  extend = bfd_get_16 (abfd, data);
1899
  insn = bfd_get_16 (abfd, data + 2);
1900
  if (r_type == R_MIPS16_26)
1901
    {
1902
      if (jal_shuffle)
1903
        val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1904
              | ((extend & 0x1f) << 21) | insn;
1905
      else
1906
        val = extend << 16 | insn;
1907
    }
1908
  else
1909
    val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1910
          | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1911
  bfd_put_32 (abfd, val, data);
1912
}
1913
 
1914
void
1915
_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1916
                               bfd_boolean jal_shuffle, bfd_byte *data)
1917
{
1918
  bfd_vma extend, insn, val;
1919
 
1920
  if (!mips16_reloc_p (r_type))
1921
    return;
1922
 
1923
  val = bfd_get_32 (abfd, data);
1924
  if (r_type == R_MIPS16_26)
1925
    {
1926
      if (jal_shuffle)
1927
        {
1928
          insn = val & 0xffff;
1929
          extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1930
                   | ((val >> 21) & 0x1f);
1931
        }
1932
      else
1933
        {
1934
          insn = val & 0xffff;
1935
          extend = val >> 16;
1936
        }
1937
    }
1938
  else
1939
    {
1940
      insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1941
      extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1942
    }
1943
  bfd_put_16 (abfd, insn, data + 2);
1944
  bfd_put_16 (abfd, extend, data);
1945
}
1946
 
1947
bfd_reloc_status_type
1948
_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1949
                               arelent *reloc_entry, asection *input_section,
1950
                               bfd_boolean relocatable, void *data, bfd_vma gp)
1951
{
1952
  bfd_vma relocation;
1953
  bfd_signed_vma val;
1954
  bfd_reloc_status_type status;
1955
 
1956
  if (bfd_is_com_section (symbol->section))
1957
    relocation = 0;
1958
  else
1959
    relocation = symbol->value;
1960
 
1961
  relocation += symbol->section->output_section->vma;
1962
  relocation += symbol->section->output_offset;
1963
 
1964
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1965
    return bfd_reloc_outofrange;
1966
 
1967
  /* Set val to the offset into the section or symbol.  */
1968
  val = reloc_entry->addend;
1969
 
1970
  _bfd_mips_elf_sign_extend (val, 16);
1971
 
1972
  /* Adjust val for the final section location and GP value.  If we
1973
     are producing relocatable output, we don't want to do this for
1974
     an external symbol.  */
1975
  if (! relocatable
1976
      || (symbol->flags & BSF_SECTION_SYM) != 0)
1977
    val += relocation - gp;
1978
 
1979
  if (reloc_entry->howto->partial_inplace)
1980
    {
1981
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1982
                                       (bfd_byte *) data
1983
                                       + reloc_entry->address);
1984
      if (status != bfd_reloc_ok)
1985
        return status;
1986
    }
1987
  else
1988
    reloc_entry->addend = val;
1989
 
1990
  if (relocatable)
1991
    reloc_entry->address += input_section->output_offset;
1992
 
1993
  return bfd_reloc_ok;
1994
}
1995
 
1996
/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1997
   R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
1998
   that contains the relocation field and DATA points to the start of
1999
   INPUT_SECTION.  */
2000
 
2001
struct mips_hi16
2002
{
2003
  struct mips_hi16 *next;
2004
  bfd_byte *data;
2005
  asection *input_section;
2006
  arelent rel;
2007
};
2008
 
2009
/* FIXME: This should not be a static variable.  */
2010
 
2011
static struct mips_hi16 *mips_hi16_list;
2012
 
2013
/* A howto special_function for REL *HI16 relocations.  We can only
2014
   calculate the correct value once we've seen the partnering
2015
   *LO16 relocation, so just save the information for later.
2016
 
2017
   The ABI requires that the *LO16 immediately follow the *HI16.
2018
   However, as a GNU extension, we permit an arbitrary number of
2019
   *HI16s to be associated with a single *LO16.  This significantly
2020
   simplies the relocation handling in gcc.  */
2021
 
2022
bfd_reloc_status_type
2023
_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2024
                          asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2025
                          asection *input_section, bfd *output_bfd,
2026
                          char **error_message ATTRIBUTE_UNUSED)
2027
{
2028
  struct mips_hi16 *n;
2029
 
2030
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2031
    return bfd_reloc_outofrange;
2032
 
2033
  n = bfd_malloc (sizeof *n);
2034
  if (n == NULL)
2035
    return bfd_reloc_outofrange;
2036
 
2037
  n->next = mips_hi16_list;
2038
  n->data = data;
2039
  n->input_section = input_section;
2040
  n->rel = *reloc_entry;
2041
  mips_hi16_list = n;
2042
 
2043
  if (output_bfd != NULL)
2044
    reloc_entry->address += input_section->output_offset;
2045
 
2046
  return bfd_reloc_ok;
2047
}
2048
 
2049
/* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2050
   like any other 16-bit relocation when applied to global symbols, but is
2051
   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2052
 
2053
bfd_reloc_status_type
2054
_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2055
                           void *data, asection *input_section,
2056
                           bfd *output_bfd, char **error_message)
2057
{
2058
  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2059
      || bfd_is_und_section (bfd_get_section (symbol))
2060
      || bfd_is_com_section (bfd_get_section (symbol)))
2061
    /* The relocation is against a global symbol.  */
2062
    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2063
                                        input_section, output_bfd,
2064
                                        error_message);
2065
 
2066
  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2067
                                   input_section, output_bfd, error_message);
2068
}
2069
 
2070
/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2071
   is a straightforward 16 bit inplace relocation, but we must deal with
2072
   any partnering high-part relocations as well.  */
2073
 
2074
bfd_reloc_status_type
2075
_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2076
                          void *data, asection *input_section,
2077
                          bfd *output_bfd, char **error_message)
2078
{
2079
  bfd_vma vallo;
2080
  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2081
 
2082
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2083
    return bfd_reloc_outofrange;
2084
 
2085
  _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2086
                                   location);
2087
  vallo = bfd_get_32 (abfd, location);
2088
  _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2089
                                 location);
2090
 
2091
  while (mips_hi16_list != NULL)
2092
    {
2093
      bfd_reloc_status_type ret;
2094
      struct mips_hi16 *hi;
2095
 
2096
      hi = mips_hi16_list;
2097
 
2098
      /* R_MIPS*_GOT16 relocations are something of a special case.  We
2099
         want to install the addend in the same way as for a R_MIPS*_HI16
2100
         relocation (with a rightshift of 16).  However, since GOT16
2101
         relocations can also be used with global symbols, their howto
2102
         has a rightshift of 0.  */
2103
      if (hi->rel.howto->type == R_MIPS_GOT16)
2104
        hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2105
      else if (hi->rel.howto->type == R_MIPS16_GOT16)
2106
        hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2107
 
2108
      /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
2109
         carry or borrow will induce a change of +1 or -1 in the high part.  */
2110
      hi->rel.addend += (vallo + 0x8000) & 0xffff;
2111
 
2112
      ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2113
                                         hi->input_section, output_bfd,
2114
                                         error_message);
2115
      if (ret != bfd_reloc_ok)
2116
        return ret;
2117
 
2118
      mips_hi16_list = hi->next;
2119
      free (hi);
2120
    }
2121
 
2122
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2123
                                      input_section, output_bfd,
2124
                                      error_message);
2125
}
2126
 
2127
/* A generic howto special_function.  This calculates and installs the
2128
   relocation itself, thus avoiding the oft-discussed problems in
2129
   bfd_perform_relocation and bfd_install_relocation.  */
2130
 
2131
bfd_reloc_status_type
2132
_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2133
                             asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2134
                             asection *input_section, bfd *output_bfd,
2135
                             char **error_message ATTRIBUTE_UNUSED)
2136
{
2137
  bfd_signed_vma val;
2138
  bfd_reloc_status_type status;
2139
  bfd_boolean relocatable;
2140
 
2141
  relocatable = (output_bfd != NULL);
2142
 
2143
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2144
    return bfd_reloc_outofrange;
2145
 
2146
  /* Build up the field adjustment in VAL.  */
2147
  val = 0;
2148
  if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2149
    {
2150
      /* Either we're calculating the final field value or we have a
2151
         relocation against a section symbol.  Add in the section's
2152
         offset or address.  */
2153
      val += symbol->section->output_section->vma;
2154
      val += symbol->section->output_offset;
2155
    }
2156
 
2157
  if (!relocatable)
2158
    {
2159
      /* We're calculating the final field value.  Add in the symbol's value
2160
         and, if pc-relative, subtract the address of the field itself.  */
2161
      val += symbol->value;
2162
      if (reloc_entry->howto->pc_relative)
2163
        {
2164
          val -= input_section->output_section->vma;
2165
          val -= input_section->output_offset;
2166
          val -= reloc_entry->address;
2167
        }
2168
    }
2169
 
2170
  /* VAL is now the final adjustment.  If we're keeping this relocation
2171
     in the output file, and if the relocation uses a separate addend,
2172
     we just need to add VAL to that addend.  Otherwise we need to add
2173
     VAL to the relocation field itself.  */
2174
  if (relocatable && !reloc_entry->howto->partial_inplace)
2175
    reloc_entry->addend += val;
2176
  else
2177
    {
2178
      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2179
 
2180
      /* Add in the separate addend, if any.  */
2181
      val += reloc_entry->addend;
2182
 
2183
      /* Add VAL to the relocation field.  */
2184
      _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2185
                                       location);
2186
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2187
                                       location);
2188
      _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2189
                                     location);
2190
 
2191
      if (status != bfd_reloc_ok)
2192
        return status;
2193
    }
2194
 
2195
  if (relocatable)
2196
    reloc_entry->address += input_section->output_offset;
2197
 
2198
  return bfd_reloc_ok;
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 (bfd *abfd, const Elf32_External_gptab *ex,
2206
                              Elf32_gptab *in)
2207
{
2208
  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2209
  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2210
}
2211
 
2212
static void
2213
bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2214
                               Elf32_External_gptab *ex)
2215
{
2216
  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2217
  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2218
}
2219
 
2220
static void
2221
bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2222
                                Elf32_External_compact_rel *ex)
2223
{
2224
  H_PUT_32 (abfd, in->id1, ex->id1);
2225
  H_PUT_32 (abfd, in->num, ex->num);
2226
  H_PUT_32 (abfd, in->id2, ex->id2);
2227
  H_PUT_32 (abfd, in->offset, ex->offset);
2228
  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2229
  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2230
}
2231
 
2232
static void
2233
bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2234
                           Elf32_External_crinfo *ex)
2235
{
2236
  unsigned long l;
2237
 
2238
  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2239
       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2240
       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2241
       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2242
  H_PUT_32 (abfd, l, ex->info);
2243
  H_PUT_32 (abfd, in->konst, ex->konst);
2244
  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2245
}
2246
 
2247
/* A .reginfo section holds a single Elf32_RegInfo structure.  These
2248
   routines swap this structure in and out.  They are used outside of
2249
   BFD, so they are globally visible.  */
2250
 
2251
void
2252
bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2253
                                Elf32_RegInfo *in)
2254
{
2255
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2256
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2257
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2258
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2259
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2260
  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2261
}
2262
 
2263
void
2264
bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2265
                                 Elf32_External_RegInfo *ex)
2266
{
2267
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2268
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2269
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2270
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2271
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2272
  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2273
}
2274
 
2275
/* In the 64 bit ABI, the .MIPS.options section holds register
2276
   information in an Elf64_Reginfo structure.  These routines swap
2277
   them in and out.  They are globally visible because they are used
2278
   outside of BFD.  These routines are here so that gas can call them
2279
   without worrying about whether the 64 bit ABI has been included.  */
2280
 
2281
void
2282
bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2283
                                Elf64_Internal_RegInfo *in)
2284
{
2285
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2286
  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2287
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2288
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2289
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2290
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2291
  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2292
}
2293
 
2294
void
2295
bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2296
                                 Elf64_External_RegInfo *ex)
2297
{
2298
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2299
  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2300
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2301
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2302
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2303
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2304
  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2305
}
2306
 
2307
/* Swap in an options header.  */
2308
 
2309
void
2310
bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2311
                              Elf_Internal_Options *in)
2312
{
2313
  in->kind = H_GET_8 (abfd, ex->kind);
2314
  in->size = H_GET_8 (abfd, ex->size);
2315
  in->section = H_GET_16 (abfd, ex->section);
2316
  in->info = H_GET_32 (abfd, ex->info);
2317
}
2318
 
2319
/* Swap out an options header.  */
2320
 
2321
void
2322
bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2323
                               Elf_External_Options *ex)
2324
{
2325
  H_PUT_8 (abfd, in->kind, ex->kind);
2326
  H_PUT_8 (abfd, in->size, ex->size);
2327
  H_PUT_16 (abfd, in->section, ex->section);
2328
  H_PUT_32 (abfd, in->info, ex->info);
2329
}
2330
 
2331
/* This function is called via qsort() to sort the dynamic relocation
2332
   entries by increasing r_symndx value.  */
2333
 
2334
static int
2335
sort_dynamic_relocs (const void *arg1, const void *arg2)
2336
{
2337
  Elf_Internal_Rela int_reloc1;
2338
  Elf_Internal_Rela int_reloc2;
2339
  int diff;
2340
 
2341
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2342
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2343
 
2344
  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2345
  if (diff != 0)
2346
    return diff;
2347
 
2348
  if (int_reloc1.r_offset < int_reloc2.r_offset)
2349
    return -1;
2350
  if (int_reloc1.r_offset > int_reloc2.r_offset)
2351
    return 1;
2352
  return 0;
2353
}
2354
 
2355
/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2356
 
2357
static int
2358
sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2359
                        const void *arg2 ATTRIBUTE_UNUSED)
2360
{
2361
#ifdef BFD64
2362
  Elf_Internal_Rela int_reloc1[3];
2363
  Elf_Internal_Rela int_reloc2[3];
2364
 
2365
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2366
    (reldyn_sorting_bfd, arg1, int_reloc1);
2367
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2368
    (reldyn_sorting_bfd, arg2, int_reloc2);
2369
 
2370
  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2371
    return -1;
2372
  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2373
    return 1;
2374
 
2375
  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2376
    return -1;
2377
  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2378
    return 1;
2379
  return 0;
2380
#else
2381
  abort ();
2382
#endif
2383
}
2384
 
2385
 
2386
/* This routine is used to write out ECOFF debugging external symbol
2387
   information.  It is called via mips_elf_link_hash_traverse.  The
2388
   ECOFF external symbol information must match the ELF external
2389
   symbol information.  Unfortunately, at this point we don't know
2390
   whether a symbol is required by reloc information, so the two
2391
   tables may wind up being different.  We must sort out the external
2392
   symbol information before we can set the final size of the .mdebug
2393
   section, and we must set the size of the .mdebug section before we
2394
   can relocate any sections, and we can't know which symbols are
2395
   required by relocation until we relocate the sections.
2396
   Fortunately, it is relatively unlikely that any symbol will be
2397
   stripped but required by a reloc.  In particular, it can not happen
2398
   when generating a final executable.  */
2399
 
2400
static bfd_boolean
2401
mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2402
{
2403
  struct extsym_info *einfo = data;
2404
  bfd_boolean strip;
2405
  asection *sec, *output_section;
2406
 
2407
  if (h->root.root.type == bfd_link_hash_warning)
2408
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2409
 
2410
  if (h->root.indx == -2)
2411
    strip = FALSE;
2412
  else if ((h->root.def_dynamic
2413
            || h->root.ref_dynamic
2414
            || h->root.type == bfd_link_hash_new)
2415
           && !h->root.def_regular
2416
           && !h->root.ref_regular)
2417
    strip = TRUE;
2418
  else if (einfo->info->strip == strip_all
2419
           || (einfo->info->strip == strip_some
2420
               && bfd_hash_lookup (einfo->info->keep_hash,
2421
                                   h->root.root.root.string,
2422
                                   FALSE, FALSE) == NULL))
2423
    strip = TRUE;
2424
  else
2425
    strip = FALSE;
2426
 
2427
  if (strip)
2428
    return TRUE;
2429
 
2430
  if (h->esym.ifd == -2)
2431
    {
2432
      h->esym.jmptbl = 0;
2433
      h->esym.cobol_main = 0;
2434
      h->esym.weakext = 0;
2435
      h->esym.reserved = 0;
2436
      h->esym.ifd = ifdNil;
2437
      h->esym.asym.value = 0;
2438
      h->esym.asym.st = stGlobal;
2439
 
2440
      if (h->root.root.type == bfd_link_hash_undefined
2441
          || h->root.root.type == bfd_link_hash_undefweak)
2442
        {
2443
          const char *name;
2444
 
2445
          /* Use undefined class.  Also, set class and type for some
2446
             special symbols.  */
2447
          name = h->root.root.root.string;
2448
          if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2449
              || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2450
            {
2451
              h->esym.asym.sc = scData;
2452
              h->esym.asym.st = stLabel;
2453
              h->esym.asym.value = 0;
2454
            }
2455
          else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2456
            {
2457
              h->esym.asym.sc = scAbs;
2458
              h->esym.asym.st = stLabel;
2459
              h->esym.asym.value =
2460
                mips_elf_hash_table (einfo->info)->procedure_count;
2461
            }
2462
          else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2463
            {
2464
              h->esym.asym.sc = scAbs;
2465
              h->esym.asym.st = stLabel;
2466
              h->esym.asym.value = elf_gp (einfo->abfd);
2467
            }
2468
          else
2469
            h->esym.asym.sc = scUndefined;
2470
        }
2471
      else if (h->root.root.type != bfd_link_hash_defined
2472
          && h->root.root.type != bfd_link_hash_defweak)
2473
        h->esym.asym.sc = scAbs;
2474
      else
2475
        {
2476
          const char *name;
2477
 
2478
          sec = h->root.root.u.def.section;
2479
          output_section = sec->output_section;
2480
 
2481
          /* When making a shared library and symbol h is the one from
2482
             the another shared library, OUTPUT_SECTION may be null.  */
2483
          if (output_section == NULL)
2484
            h->esym.asym.sc = scUndefined;
2485
          else
2486
            {
2487
              name = bfd_section_name (output_section->owner, output_section);
2488
 
2489
              if (strcmp (name, ".text") == 0)
2490
                h->esym.asym.sc = scText;
2491
              else if (strcmp (name, ".data") == 0)
2492
                h->esym.asym.sc = scData;
2493
              else if (strcmp (name, ".sdata") == 0)
2494
                h->esym.asym.sc = scSData;
2495
              else if (strcmp (name, ".rodata") == 0
2496
                       || strcmp (name, ".rdata") == 0)
2497
                h->esym.asym.sc = scRData;
2498
              else if (strcmp (name, ".bss") == 0)
2499
                h->esym.asym.sc = scBss;
2500
              else if (strcmp (name, ".sbss") == 0)
2501
                h->esym.asym.sc = scSBss;
2502
              else if (strcmp (name, ".init") == 0)
2503
                h->esym.asym.sc = scInit;
2504
              else if (strcmp (name, ".fini") == 0)
2505
                h->esym.asym.sc = scFini;
2506
              else
2507
                h->esym.asym.sc = scAbs;
2508
            }
2509
        }
2510
 
2511
      h->esym.asym.reserved = 0;
2512
      h->esym.asym.index = indexNil;
2513
    }
2514
 
2515
  if (h->root.root.type == bfd_link_hash_common)
2516
    h->esym.asym.value = h->root.root.u.c.size;
2517
  else if (h->root.root.type == bfd_link_hash_defined
2518
           || h->root.root.type == bfd_link_hash_defweak)
2519
    {
2520
      if (h->esym.asym.sc == scCommon)
2521
        h->esym.asym.sc = scBss;
2522
      else if (h->esym.asym.sc == scSCommon)
2523
        h->esym.asym.sc = scSBss;
2524
 
2525
      sec = h->root.root.u.def.section;
2526
      output_section = sec->output_section;
2527
      if (output_section != NULL)
2528
        h->esym.asym.value = (h->root.root.u.def.value
2529
                              + sec->output_offset
2530
                              + output_section->vma);
2531
      else
2532
        h->esym.asym.value = 0;
2533
    }
2534
  else
2535
    {
2536
      struct mips_elf_link_hash_entry *hd = h;
2537
 
2538
      while (hd->root.root.type == bfd_link_hash_indirect)
2539
        hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2540
 
2541
      if (hd->needs_lazy_stub)
2542
        {
2543
          /* Set type and value for a symbol with a function stub.  */
2544
          h->esym.asym.st = stProc;
2545
          sec = hd->root.root.u.def.section;
2546
          if (sec == NULL)
2547
            h->esym.asym.value = 0;
2548
          else
2549
            {
2550
              output_section = sec->output_section;
2551
              if (output_section != NULL)
2552
                h->esym.asym.value = (hd->root.plt.offset
2553
                                      + sec->output_offset
2554
                                      + output_section->vma);
2555
              else
2556
                h->esym.asym.value = 0;
2557
            }
2558
        }
2559
    }
2560
 
2561
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2562
                                      h->root.root.root.string,
2563
                                      &h->esym))
2564
    {
2565
      einfo->failed = TRUE;
2566
      return FALSE;
2567
    }
2568
 
2569
  return TRUE;
2570
}
2571
 
2572
/* A comparison routine used to sort .gptab entries.  */
2573
 
2574
static int
2575
gptab_compare (const void *p1, const void *p2)
2576
{
2577
  const Elf32_gptab *a1 = p1;
2578
  const Elf32_gptab *a2 = p2;
2579
 
2580
  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2581
}
2582
 
2583
/* Functions to manage the got entry hash table.  */
2584
 
2585
/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2586
   hash number.  */
2587
 
2588
static INLINE hashval_t
2589
mips_elf_hash_bfd_vma (bfd_vma addr)
2590
{
2591
#ifdef BFD64
2592
  return addr + (addr >> 32);
2593
#else
2594
  return addr;
2595
#endif
2596
}
2597
 
2598
/* got_entries only match if they're identical, except for gotidx, so
2599
   use all fields to compute the hash, and compare the appropriate
2600
   union members.  */
2601
 
2602
static hashval_t
2603
mips_elf_got_entry_hash (const void *entry_)
2604
{
2605
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2606
 
2607
  return entry->symndx
2608
    + ((entry->tls_type & GOT_TLS_LDM) << 17)
2609
    + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2610
       : entry->abfd->id
2611
         + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2612
            : entry->d.h->root.root.root.hash));
2613
}
2614
 
2615
static int
2616
mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2617
{
2618
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2619
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2620
 
2621
  /* An LDM entry can only match another LDM entry.  */
2622
  if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2623
    return 0;
2624
 
2625
  return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2626
    && (! e1->abfd ? e1->d.address == e2->d.address
2627
        : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2628
        : e1->d.h == e2->d.h);
2629
}
2630
 
2631
/* multi_got_entries are still a match in the case of global objects,
2632
   even if the input bfd in which they're referenced differs, so the
2633
   hash computation and compare functions are adjusted
2634
   accordingly.  */
2635
 
2636
static hashval_t
2637
mips_elf_multi_got_entry_hash (const void *entry_)
2638
{
2639
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2640
 
2641
  return entry->symndx
2642
    + (! entry->abfd
2643
       ? mips_elf_hash_bfd_vma (entry->d.address)
2644
       : entry->symndx >= 0
2645
       ? ((entry->tls_type & GOT_TLS_LDM)
2646
          ? (GOT_TLS_LDM << 17)
2647
          : (entry->abfd->id
2648
             + mips_elf_hash_bfd_vma (entry->d.addend)))
2649
       : entry->d.h->root.root.root.hash);
2650
}
2651
 
2652
static int
2653
mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2654
{
2655
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2656
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2657
 
2658
  /* Any two LDM entries match.  */
2659
  if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2660
    return 1;
2661
 
2662
  /* Nothing else matches an LDM entry.  */
2663
  if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2664
    return 0;
2665
 
2666
  return e1->symndx == e2->symndx
2667
    && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2668
        : e1->abfd == NULL || e2->abfd == NULL
2669
        ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2670
        : e1->d.h == e2->d.h);
2671
}
2672
 
2673
static hashval_t
2674
mips_got_page_entry_hash (const void *entry_)
2675
{
2676
  const struct mips_got_page_entry *entry;
2677
 
2678
  entry = (const struct mips_got_page_entry *) entry_;
2679
  return entry->abfd->id + entry->symndx;
2680
}
2681
 
2682
static int
2683
mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2684
{
2685
  const struct mips_got_page_entry *entry1, *entry2;
2686
 
2687
  entry1 = (const struct mips_got_page_entry *) entry1_;
2688
  entry2 = (const struct mips_got_page_entry *) entry2_;
2689
  return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2690
}
2691
 
2692
/* Return the dynamic relocation section.  If it doesn't exist, try to
2693
   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
2694
   if creation fails.  */
2695
 
2696
static asection *
2697
mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2698
{
2699
  const char *dname;
2700
  asection *sreloc;
2701
  bfd *dynobj;
2702
 
2703
  dname = MIPS_ELF_REL_DYN_NAME (info);
2704
  dynobj = elf_hash_table (info)->dynobj;
2705
  sreloc = bfd_get_section_by_name (dynobj, dname);
2706
  if (sreloc == NULL && create_p)
2707
    {
2708
      sreloc = bfd_make_section_with_flags (dynobj, dname,
2709
                                            (SEC_ALLOC
2710
                                             | SEC_LOAD
2711
                                             | SEC_HAS_CONTENTS
2712
                                             | SEC_IN_MEMORY
2713
                                             | SEC_LINKER_CREATED
2714
                                             | SEC_READONLY));
2715
      if (sreloc == NULL
2716
          || ! bfd_set_section_alignment (dynobj, sreloc,
2717
                                          MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2718
        return NULL;
2719
    }
2720
  return sreloc;
2721
}
2722
 
2723
/* Count the number of relocations needed for a TLS GOT entry, with
2724
   access types from TLS_TYPE, and symbol H (or a local symbol if H
2725
   is NULL).  */
2726
 
2727
static int
2728
mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2729
                     struct elf_link_hash_entry *h)
2730
{
2731
  int indx = 0;
2732
  int ret = 0;
2733
  bfd_boolean need_relocs = FALSE;
2734
  bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2735
 
2736
  if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2737
      && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2738
    indx = h->dynindx;
2739
 
2740
  if ((info->shared || indx != 0)
2741
      && (h == NULL
2742
          || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2743
          || h->root.type != bfd_link_hash_undefweak))
2744
    need_relocs = TRUE;
2745
 
2746
  if (!need_relocs)
2747
    return FALSE;
2748
 
2749
  if (tls_type & GOT_TLS_GD)
2750
    {
2751
      ret++;
2752
      if (indx != 0)
2753
        ret++;
2754
    }
2755
 
2756
  if (tls_type & GOT_TLS_IE)
2757
    ret++;
2758
 
2759
  if ((tls_type & GOT_TLS_LDM) && info->shared)
2760
    ret++;
2761
 
2762
  return ret;
2763
}
2764
 
2765
/* Count the number of TLS relocations required for the GOT entry in
2766
   ARG1, if it describes a local symbol.  */
2767
 
2768
static int
2769
mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2770
{
2771
  struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2772
  struct mips_elf_count_tls_arg *arg = arg2;
2773
 
2774
  if (entry->abfd != NULL && entry->symndx != -1)
2775
    arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2776
 
2777
  return 1;
2778
}
2779
 
2780
/* Count the number of TLS GOT entries required for the global (or
2781
   forced-local) symbol in ARG1.  */
2782
 
2783
static int
2784
mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2785
{
2786
  struct mips_elf_link_hash_entry *hm
2787
    = (struct mips_elf_link_hash_entry *) arg1;
2788
  struct mips_elf_count_tls_arg *arg = arg2;
2789
 
2790
  if (hm->tls_type & GOT_TLS_GD)
2791
    arg->needed += 2;
2792
  if (hm->tls_type & GOT_TLS_IE)
2793
    arg->needed += 1;
2794
 
2795
  return 1;
2796
}
2797
 
2798
/* Count the number of TLS relocations required for the global (or
2799
   forced-local) symbol in ARG1.  */
2800
 
2801
static int
2802
mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2803
{
2804
  struct mips_elf_link_hash_entry *hm
2805
    = (struct mips_elf_link_hash_entry *) arg1;
2806
  struct mips_elf_count_tls_arg *arg = arg2;
2807
 
2808
  arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2809
 
2810
  return 1;
2811
}
2812
 
2813
/* Output a simple dynamic relocation into SRELOC.  */
2814
 
2815
static void
2816
mips_elf_output_dynamic_relocation (bfd *output_bfd,
2817
                                    asection *sreloc,
2818
                                    unsigned long reloc_index,
2819
                                    unsigned long indx,
2820
                                    int r_type,
2821
                                    bfd_vma offset)
2822
{
2823
  Elf_Internal_Rela rel[3];
2824
 
2825
  memset (rel, 0, sizeof (rel));
2826
 
2827
  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2828
  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2829
 
2830
  if (ABI_64_P (output_bfd))
2831
    {
2832
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2833
        (output_bfd, &rel[0],
2834
         (sreloc->contents
2835
          + reloc_index * sizeof (Elf64_Mips_External_Rel)));
2836
    }
2837
  else
2838
    bfd_elf32_swap_reloc_out
2839
      (output_bfd, &rel[0],
2840
       (sreloc->contents
2841
        + reloc_index * sizeof (Elf32_External_Rel)));
2842
}
2843
 
2844
/* Initialize a set of TLS GOT entries for one symbol.  */
2845
 
2846
static void
2847
mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2848
                               unsigned char *tls_type_p,
2849
                               struct bfd_link_info *info,
2850
                               struct mips_elf_link_hash_entry *h,
2851
                               bfd_vma value)
2852
{
2853
  struct mips_elf_link_hash_table *htab;
2854
  int indx;
2855
  asection *sreloc, *sgot;
2856
  bfd_vma offset, offset2;
2857
  bfd_boolean need_relocs = FALSE;
2858
 
2859
  htab = mips_elf_hash_table (info);
2860
  if (htab == NULL)
2861
    return;
2862
 
2863
  sgot = htab->sgot;
2864
 
2865
  indx = 0;
2866
  if (h != NULL)
2867
    {
2868
      bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2869
 
2870
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2871
          && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2872
        indx = h->root.dynindx;
2873
    }
2874
 
2875
  if (*tls_type_p & GOT_TLS_DONE)
2876
    return;
2877
 
2878
  if ((info->shared || indx != 0)
2879
      && (h == NULL
2880
          || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2881
          || h->root.type != bfd_link_hash_undefweak))
2882
    need_relocs = TRUE;
2883
 
2884
  /* MINUS_ONE means the symbol is not defined in this object.  It may not
2885
     be defined at all; assume that the value doesn't matter in that
2886
     case.  Otherwise complain if we would use the value.  */
2887
  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2888
              || h->root.root.type == bfd_link_hash_undefweak);
2889
 
2890
  /* Emit necessary relocations.  */
2891
  sreloc = mips_elf_rel_dyn_section (info, FALSE);
2892
 
2893
  /* General Dynamic.  */
2894
  if (*tls_type_p & GOT_TLS_GD)
2895
    {
2896
      offset = got_offset;
2897
      offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2898
 
2899
      if (need_relocs)
2900
        {
2901
          mips_elf_output_dynamic_relocation
2902
            (abfd, sreloc, sreloc->reloc_count++, indx,
2903
             ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2904
             sgot->output_offset + sgot->output_section->vma + offset);
2905
 
2906
          if (indx)
2907
            mips_elf_output_dynamic_relocation
2908
              (abfd, sreloc, sreloc->reloc_count++, indx,
2909
               ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2910
               sgot->output_offset + sgot->output_section->vma + offset2);
2911
          else
2912
            MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2913
                               sgot->contents + offset2);
2914
        }
2915
      else
2916
        {
2917
          MIPS_ELF_PUT_WORD (abfd, 1,
2918
                             sgot->contents + offset);
2919
          MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2920
                             sgot->contents + offset2);
2921
        }
2922
 
2923
      got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2924
    }
2925
 
2926
  /* Initial Exec model.  */
2927
  if (*tls_type_p & GOT_TLS_IE)
2928
    {
2929
      offset = got_offset;
2930
 
2931
      if (need_relocs)
2932
        {
2933
          if (indx == 0)
2934
            MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2935
                               sgot->contents + offset);
2936
          else
2937
            MIPS_ELF_PUT_WORD (abfd, 0,
2938
                               sgot->contents + offset);
2939
 
2940
          mips_elf_output_dynamic_relocation
2941
            (abfd, sreloc, sreloc->reloc_count++, indx,
2942
             ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2943
             sgot->output_offset + sgot->output_section->vma + offset);
2944
        }
2945
      else
2946
        MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2947
                           sgot->contents + offset);
2948
    }
2949
 
2950
  if (*tls_type_p & GOT_TLS_LDM)
2951
    {
2952
      /* The initial offset is zero, and the LD offsets will include the
2953
         bias by DTP_OFFSET.  */
2954
      MIPS_ELF_PUT_WORD (abfd, 0,
2955
                         sgot->contents + got_offset
2956
                         + MIPS_ELF_GOT_SIZE (abfd));
2957
 
2958
      if (!info->shared)
2959
        MIPS_ELF_PUT_WORD (abfd, 1,
2960
                           sgot->contents + got_offset);
2961
      else
2962
        mips_elf_output_dynamic_relocation
2963
          (abfd, sreloc, sreloc->reloc_count++, indx,
2964
           ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2965
           sgot->output_offset + sgot->output_section->vma + got_offset);
2966
    }
2967
 
2968
  *tls_type_p |= GOT_TLS_DONE;
2969
}
2970
 
2971
/* Return the GOT index to use for a relocation of type R_TYPE against
2972
   a symbol accessed using TLS_TYPE models.  The GOT entries for this
2973
   symbol in this GOT start at GOT_INDEX.  This function initializes the
2974
   GOT entries and corresponding relocations.  */
2975
 
2976
static bfd_vma
2977
mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2978
                    int r_type, struct bfd_link_info *info,
2979
                    struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2980
{
2981
  BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2982
              || r_type == R_MIPS_TLS_LDM);
2983
 
2984
  mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2985
 
2986
  if (r_type == R_MIPS_TLS_GOTTPREL)
2987
    {
2988
      BFD_ASSERT (*tls_type & GOT_TLS_IE);
2989
      if (*tls_type & GOT_TLS_GD)
2990
        return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2991
      else
2992
        return got_index;
2993
    }
2994
 
2995
  if (r_type == R_MIPS_TLS_GD)
2996
    {
2997
      BFD_ASSERT (*tls_type & GOT_TLS_GD);
2998
      return got_index;
2999
    }
3000
 
3001
  if (r_type == R_MIPS_TLS_LDM)
3002
    {
3003
      BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3004
      return got_index;
3005
    }
3006
 
3007
  return got_index;
3008
}
3009
 
3010
/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3011
   for global symbol H.  .got.plt comes before the GOT, so the offset
3012
   will be negative.  */
3013
 
3014
static bfd_vma
3015
mips_elf_gotplt_index (struct bfd_link_info *info,
3016
                       struct elf_link_hash_entry *h)
3017
{
3018
  bfd_vma plt_index, got_address, got_value;
3019
  struct mips_elf_link_hash_table *htab;
3020
 
3021
  htab = mips_elf_hash_table (info);
3022
  BFD_ASSERT (htab != NULL);
3023
 
3024
  BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3025
 
3026
  /* This function only works for VxWorks, because a non-VxWorks .got.plt
3027
     section starts with reserved entries.  */
3028
  BFD_ASSERT (htab->is_vxworks);
3029
 
3030
  /* Calculate the index of the symbol's PLT entry.  */
3031
  plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3032
 
3033
  /* Calculate the address of the associated .got.plt entry.  */
3034
  got_address = (htab->sgotplt->output_section->vma
3035
                 + htab->sgotplt->output_offset
3036
                 + plt_index * 4);
3037
 
3038
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3039
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3040
               + htab->root.hgot->root.u.def.section->output_offset
3041
               + htab->root.hgot->root.u.def.value);
3042
 
3043
  return got_address - got_value;
3044
}
3045
 
3046
/* Return the GOT offset for address VALUE.   If there is not yet a GOT
3047
   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3048
   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3049
   offset can be found.  */
3050
 
3051
static bfd_vma
3052
mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3053
                          bfd_vma value, unsigned long r_symndx,
3054
                          struct mips_elf_link_hash_entry *h, int r_type)
3055
{
3056
  struct mips_elf_link_hash_table *htab;
3057
  struct mips_got_entry *entry;
3058
 
3059
  htab = mips_elf_hash_table (info);
3060
  BFD_ASSERT (htab != NULL);
3061
 
3062
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3063
                                           r_symndx, h, r_type);
3064
  if (!entry)
3065
    return MINUS_ONE;
3066
 
3067
  if (TLS_RELOC_P (r_type))
3068
    {
3069
      if (entry->symndx == -1 && htab->got_info->next == NULL)
3070
        /* A type (3) entry in the single-GOT case.  We use the symbol's
3071
           hash table entry to track the index.  */
3072
        return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3073
                                   r_type, info, h, value);
3074
      else
3075
        return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3076
                                   r_type, info, h, value);
3077
    }
3078
  else
3079
    return entry->gotidx;
3080
}
3081
 
3082
/* Returns the GOT index for the global symbol indicated by H.  */
3083
 
3084
static bfd_vma
3085
mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3086
                           int r_type, struct bfd_link_info *info)
3087
{
3088
  struct mips_elf_link_hash_table *htab;
3089
  bfd_vma got_index;
3090
  struct mips_got_info *g, *gg;
3091
  long global_got_dynindx = 0;
3092
 
3093
  htab = mips_elf_hash_table (info);
3094
  BFD_ASSERT (htab != NULL);
3095
 
3096
  gg = g = htab->got_info;
3097
  if (g->bfd2got && ibfd)
3098
    {
3099
      struct mips_got_entry e, *p;
3100
 
3101
      BFD_ASSERT (h->dynindx >= 0);
3102
 
3103
      g = mips_elf_got_for_ibfd (g, ibfd);
3104
      if (g->next != gg || TLS_RELOC_P (r_type))
3105
        {
3106
          e.abfd = ibfd;
3107
          e.symndx = -1;
3108
          e.d.h = (struct mips_elf_link_hash_entry *)h;
3109
          e.tls_type = 0;
3110
 
3111
          p = htab_find (g->got_entries, &e);
3112
 
3113
          BFD_ASSERT (p->gotidx > 0);
3114
 
3115
          if (TLS_RELOC_P (r_type))
3116
            {
3117
              bfd_vma value = MINUS_ONE;
3118
              if ((h->root.type == bfd_link_hash_defined
3119
                   || h->root.type == bfd_link_hash_defweak)
3120
                  && h->root.u.def.section->output_section)
3121
                value = (h->root.u.def.value
3122
                         + h->root.u.def.section->output_offset
3123
                         + h->root.u.def.section->output_section->vma);
3124
 
3125
              return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3126
                                         info, e.d.h, value);
3127
            }
3128
          else
3129
            return p->gotidx;
3130
        }
3131
    }
3132
 
3133
  if (gg->global_gotsym != NULL)
3134
    global_got_dynindx = gg->global_gotsym->dynindx;
3135
 
3136
  if (TLS_RELOC_P (r_type))
3137
    {
3138
      struct mips_elf_link_hash_entry *hm
3139
        = (struct mips_elf_link_hash_entry *) h;
3140
      bfd_vma value = MINUS_ONE;
3141
 
3142
      if ((h->root.type == bfd_link_hash_defined
3143
           || h->root.type == bfd_link_hash_defweak)
3144
          && h->root.u.def.section->output_section)
3145
        value = (h->root.u.def.value
3146
                 + h->root.u.def.section->output_offset
3147
                 + h->root.u.def.section->output_section->vma);
3148
 
3149
      got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3150
                                      r_type, info, hm, value);
3151
    }
3152
  else
3153
    {
3154
      /* Once we determine the global GOT entry with the lowest dynamic
3155
         symbol table index, we must put all dynamic symbols with greater
3156
         indices into the GOT.  That makes it easy to calculate the GOT
3157
         offset.  */
3158
      BFD_ASSERT (h->dynindx >= global_got_dynindx);
3159
      got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3160
                   * MIPS_ELF_GOT_SIZE (abfd));
3161
    }
3162
  BFD_ASSERT (got_index < htab->sgot->size);
3163
 
3164
  return got_index;
3165
}
3166
 
3167
/* Find a GOT page entry that points to within 32KB of VALUE.  These
3168
   entries are supposed to be placed at small offsets in the GOT, i.e.,
3169
   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3170
   entry could be created.  If OFFSETP is nonnull, use it to return the
3171
   offset of the GOT entry from VALUE.  */
3172
 
3173
static bfd_vma
3174
mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3175
                   bfd_vma value, bfd_vma *offsetp)
3176
{
3177
  bfd_vma page, got_index;
3178
  struct mips_got_entry *entry;
3179
 
3180
  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3181
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3182
                                           NULL, R_MIPS_GOT_PAGE);
3183
 
3184
  if (!entry)
3185
    return MINUS_ONE;
3186
 
3187
  got_index = entry->gotidx;
3188
 
3189
  if (offsetp)
3190
    *offsetp = value - entry->d.address;
3191
 
3192
  return got_index;
3193
}
3194
 
3195
/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3196
   EXTERNAL is true if the relocation was originally against a global
3197
   symbol that binds locally.  */
3198
 
3199
static bfd_vma
3200
mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3201
                      bfd_vma value, bfd_boolean external)
3202
{
3203
  struct mips_got_entry *entry;
3204
 
3205
  /* GOT16 relocations against local symbols are followed by a LO16
3206
     relocation; those against global symbols are not.  Thus if the
3207
     symbol was originally local, the GOT16 relocation should load the
3208
     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3209
  if (! external)
3210
    value = mips_elf_high (value) << 16;
3211
 
3212
  /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3213
     R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3214
     same in all cases.  */
3215
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3216
                                           NULL, R_MIPS_GOT16);
3217
  if (entry)
3218
    return entry->gotidx;
3219
  else
3220
    return MINUS_ONE;
3221
}
3222
 
3223
/* Returns the offset for the entry at the INDEXth position
3224
   in the GOT.  */
3225
 
3226
static bfd_vma
3227
mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3228
                                bfd *input_bfd, bfd_vma got_index)
3229
{
3230
  struct mips_elf_link_hash_table *htab;
3231
  asection *sgot;
3232
  bfd_vma gp;
3233
 
3234
  htab = mips_elf_hash_table (info);
3235
  BFD_ASSERT (htab != NULL);
3236
 
3237
  sgot = htab->sgot;
3238
  gp = _bfd_get_gp_value (output_bfd)
3239
    + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3240
 
3241
  return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3242
}
3243
 
3244
/* Create and return a local GOT entry for VALUE, which was calculated
3245
   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3246
   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3247
   instead.  */
3248
 
3249
static struct mips_got_entry *
3250
mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3251
                                 bfd *ibfd, bfd_vma value,
3252
                                 unsigned long r_symndx,
3253
                                 struct mips_elf_link_hash_entry *h,
3254
                                 int r_type)
3255
{
3256
  struct mips_got_entry entry, **loc;
3257
  struct mips_got_info *g;
3258
  struct mips_elf_link_hash_table *htab;
3259
 
3260
  htab = mips_elf_hash_table (info);
3261
  BFD_ASSERT (htab != NULL);
3262
 
3263
  entry.abfd = NULL;
3264
  entry.symndx = -1;
3265
  entry.d.address = value;
3266
  entry.tls_type = 0;
3267
 
3268
  g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
3269
  if (g == NULL)
3270
    {
3271
      g = mips_elf_got_for_ibfd (htab->got_info, abfd);
3272
      BFD_ASSERT (g != NULL);
3273
    }
3274
 
3275
  /* This function shouldn't be called for symbols that live in the global
3276
     area of the GOT.  */
3277
  BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3278
  if (TLS_RELOC_P (r_type))
3279
    {
3280
      struct mips_got_entry *p;
3281
 
3282
      entry.abfd = ibfd;
3283
      if (r_type == R_MIPS_TLS_LDM)
3284
        {
3285
          entry.tls_type = GOT_TLS_LDM;
3286
          entry.symndx = 0;
3287
          entry.d.addend = 0;
3288
        }
3289
      else if (h == NULL)
3290
        {
3291
          entry.symndx = r_symndx;
3292
          entry.d.addend = 0;
3293
        }
3294
      else
3295
        entry.d.h = h;
3296
 
3297
      p = (struct mips_got_entry *)
3298
        htab_find (g->got_entries, &entry);
3299
 
3300
      BFD_ASSERT (p);
3301
      return p;
3302
    }
3303
 
3304
  loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3305
                                                   INSERT);
3306
  if (*loc)
3307
    return *loc;
3308
 
3309
  entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3310
  entry.tls_type = 0;
3311
 
3312
  *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3313
 
3314
  if (! *loc)
3315
    return NULL;
3316
 
3317
  memcpy (*loc, &entry, sizeof entry);
3318
 
3319
  if (g->assigned_gotno > g->local_gotno)
3320
    {
3321
      (*loc)->gotidx = -1;
3322
      /* We didn't allocate enough space in the GOT.  */
3323
      (*_bfd_error_handler)
3324
        (_("not enough GOT space for local GOT entries"));
3325
      bfd_set_error (bfd_error_bad_value);
3326
      return NULL;
3327
    }
3328
 
3329
  MIPS_ELF_PUT_WORD (abfd, value,
3330
                     (htab->sgot->contents + entry.gotidx));
3331
 
3332
  /* These GOT entries need a dynamic relocation on VxWorks.  */
3333
  if (htab->is_vxworks)
3334
    {
3335
      Elf_Internal_Rela outrel;
3336
      asection *s;
3337
      bfd_byte *rloc;
3338
      bfd_vma got_address;
3339
 
3340
      s = mips_elf_rel_dyn_section (info, FALSE);
3341
      got_address = (htab->sgot->output_section->vma
3342
                     + htab->sgot->output_offset
3343
                     + entry.gotidx);
3344
 
3345
      rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3346
      outrel.r_offset = got_address;
3347
      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3348
      outrel.r_addend = value;
3349
      bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3350
    }
3351
 
3352
  return *loc;
3353
}
3354
 
3355
/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3356
   The number might be exact or a worst-case estimate, depending on how
3357
   much information is available to elf_backend_omit_section_dynsym at
3358
   the current linking stage.  */
3359
 
3360
static bfd_size_type
3361
count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3362
{
3363
  bfd_size_type count;
3364
 
3365
  count = 0;
3366
  if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3367
    {
3368
      asection *p;
3369
      const struct elf_backend_data *bed;
3370
 
3371
      bed = get_elf_backend_data (output_bfd);
3372
      for (p = output_bfd->sections; p ; p = p->next)
3373
        if ((p->flags & SEC_EXCLUDE) == 0
3374
            && (p->flags & SEC_ALLOC) != 0
3375
            && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3376
          ++count;
3377
    }
3378
  return count;
3379
}
3380
 
3381
/* Sort the dynamic symbol table so that symbols that need GOT entries
3382
   appear towards the end.  */
3383
 
3384
static bfd_boolean
3385
mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3386
{
3387
  struct mips_elf_link_hash_table *htab;
3388
  struct mips_elf_hash_sort_data hsd;
3389
  struct mips_got_info *g;
3390
 
3391
  if (elf_hash_table (info)->dynsymcount == 0)
3392
    return TRUE;
3393
 
3394
  htab = mips_elf_hash_table (info);
3395
  BFD_ASSERT (htab != NULL);
3396
 
3397
  g = htab->got_info;
3398
  if (g == NULL)
3399
    return TRUE;
3400
 
3401
  hsd.low = NULL;
3402
  hsd.max_unref_got_dynindx
3403
    = hsd.min_got_dynindx
3404
    = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3405
  hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3406
  mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3407
                                elf_hash_table (info)),
3408
                               mips_elf_sort_hash_table_f,
3409
                               &hsd);
3410
 
3411
  /* There should have been enough room in the symbol table to
3412
     accommodate both the GOT and non-GOT symbols.  */
3413
  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3414
  BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3415
              == elf_hash_table (info)->dynsymcount);
3416
  BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3417
              == g->global_gotno);
3418
 
3419
  /* Now we know which dynamic symbol has the lowest dynamic symbol
3420
     table index in the GOT.  */
3421
  g->global_gotsym = hsd.low;
3422
 
3423
  return TRUE;
3424
}
3425
 
3426
/* If H needs a GOT entry, assign it the highest available dynamic
3427
   index.  Otherwise, assign it the lowest available dynamic
3428
   index.  */
3429
 
3430
static bfd_boolean
3431
mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3432
{
3433
  struct mips_elf_hash_sort_data *hsd = data;
3434
 
3435
  if (h->root.root.type == bfd_link_hash_warning)
3436
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3437
 
3438
  /* Symbols without dynamic symbol table entries aren't interesting
3439
     at all.  */
3440
  if (h->root.dynindx == -1)
3441
    return TRUE;
3442
 
3443
  switch (h->global_got_area)
3444
    {
3445
    case GGA_NONE:
3446
      h->root.dynindx = hsd->max_non_got_dynindx++;
3447
      break;
3448
 
3449
    case GGA_NORMAL:
3450
      BFD_ASSERT (h->tls_type == GOT_NORMAL);
3451
 
3452
      h->root.dynindx = --hsd->min_got_dynindx;
3453
      hsd->low = (struct elf_link_hash_entry *) h;
3454
      break;
3455
 
3456
    case GGA_RELOC_ONLY:
3457
      BFD_ASSERT (h->tls_type == GOT_NORMAL);
3458
 
3459
      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3460
        hsd->low = (struct elf_link_hash_entry *) h;
3461
      h->root.dynindx = hsd->max_unref_got_dynindx++;
3462
      break;
3463
    }
3464
 
3465
  return TRUE;
3466
}
3467
 
3468
/* If H is a symbol that needs a global GOT entry, but has a dynamic
3469
   symbol table index lower than any we've seen to date, record it for
3470
   posterity.  FOR_CALL is true if the caller is only interested in
3471
   using the GOT entry for calls.  */
3472
 
3473
static bfd_boolean
3474
mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3475
                                   bfd *abfd, struct bfd_link_info *info,
3476
                                   bfd_boolean for_call,
3477
                                   unsigned char tls_flag)
3478
{
3479
  struct mips_elf_link_hash_table *htab;
3480
  struct mips_elf_link_hash_entry *hmips;
3481
  struct mips_got_entry entry, **loc;
3482
  struct mips_got_info *g;
3483
 
3484
  htab = mips_elf_hash_table (info);
3485
  BFD_ASSERT (htab != NULL);
3486
 
3487
  hmips = (struct mips_elf_link_hash_entry *) h;
3488
  if (!for_call)
3489
    hmips->got_only_for_calls = FALSE;
3490
 
3491
  /* A global symbol in the GOT must also be in the dynamic symbol
3492
     table.  */
3493
  if (h->dynindx == -1)
3494
    {
3495
      switch (ELF_ST_VISIBILITY (h->other))
3496
        {
3497
        case STV_INTERNAL:
3498
        case STV_HIDDEN:
3499
          _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3500
          break;
3501
        }
3502
      if (!bfd_elf_link_record_dynamic_symbol (info, h))
3503
        return FALSE;
3504
    }
3505
 
3506
  /* Make sure we have a GOT to put this entry into.  */
3507
  g = htab->got_info;
3508
  BFD_ASSERT (g != NULL);
3509
 
3510
  entry.abfd = abfd;
3511
  entry.symndx = -1;
3512
  entry.d.h = (struct mips_elf_link_hash_entry *) h;
3513
  entry.tls_type = 0;
3514
 
3515
  loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3516
                                                   INSERT);
3517
 
3518
  /* If we've already marked this entry as needing GOT space, we don't
3519
     need to do it again.  */
3520
  if (*loc)
3521
    {
3522
      (*loc)->tls_type |= tls_flag;
3523
      return TRUE;
3524
    }
3525
 
3526
  *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3527
 
3528
  if (! *loc)
3529
    return FALSE;
3530
 
3531
  entry.gotidx = -1;
3532
  entry.tls_type = tls_flag;
3533
 
3534
  memcpy (*loc, &entry, sizeof entry);
3535
 
3536
  if (tls_flag == 0)
3537
    hmips->global_got_area = GGA_NORMAL;
3538
 
3539
  return TRUE;
3540
}
3541
 
3542
/* Reserve space in G for a GOT entry containing the value of symbol
3543
   SYMNDX in input bfd ABDF, plus ADDEND.  */
3544
 
3545
static bfd_boolean
3546
mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3547
                                  struct bfd_link_info *info,
3548
                                  unsigned char tls_flag)
3549
{
3550
  struct mips_elf_link_hash_table *htab;
3551
  struct mips_got_info *g;
3552
  struct mips_got_entry entry, **loc;
3553
 
3554
  htab = mips_elf_hash_table (info);
3555
  BFD_ASSERT (htab != NULL);
3556
 
3557
  g = htab->got_info;
3558
  BFD_ASSERT (g != NULL);
3559
 
3560
  entry.abfd = abfd;
3561
  entry.symndx = symndx;
3562
  entry.d.addend = addend;
3563
  entry.tls_type = tls_flag;
3564
  loc = (struct mips_got_entry **)
3565
    htab_find_slot (g->got_entries, &entry, INSERT);
3566
 
3567
  if (*loc)
3568
    {
3569
      if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3570
        {
3571
          g->tls_gotno += 2;
3572
          (*loc)->tls_type |= tls_flag;
3573
        }
3574
      else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3575
        {
3576
          g->tls_gotno += 1;
3577
          (*loc)->tls_type |= tls_flag;
3578
        }
3579
      return TRUE;
3580
    }
3581
 
3582
  if (tls_flag != 0)
3583
    {
3584
      entry.gotidx = -1;
3585
      entry.tls_type = tls_flag;
3586
      if (tls_flag == GOT_TLS_IE)
3587
        g->tls_gotno += 1;
3588
      else if (tls_flag == GOT_TLS_GD)
3589
        g->tls_gotno += 2;
3590
      else if (g->tls_ldm_offset == MINUS_ONE)
3591
        {
3592
          g->tls_ldm_offset = MINUS_TWO;
3593
          g->tls_gotno += 2;
3594
        }
3595
    }
3596
  else
3597
    {
3598
      entry.gotidx = g->local_gotno++;
3599
      entry.tls_type = 0;
3600
    }
3601
 
3602
  *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3603
 
3604
  if (! *loc)
3605
    return FALSE;
3606
 
3607
  memcpy (*loc, &entry, sizeof entry);
3608
 
3609
  return TRUE;
3610
}
3611
 
3612
/* Return the maximum number of GOT page entries required for RANGE.  */
3613
 
3614
static bfd_vma
3615
mips_elf_pages_for_range (const struct mips_got_page_range *range)
3616
{
3617
  return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3618
}
3619
 
3620
/* Record that ABFD has a page relocation against symbol SYMNDX and
3621
   that ADDEND is the addend for that relocation.
3622
 
3623
   This function creates an upper bound on the number of GOT slots
3624
   required; no attempt is made to combine references to non-overridable
3625
   global symbols across multiple input files.  */
3626
 
3627
static bfd_boolean
3628
mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3629
                                long symndx, bfd_signed_vma addend)
3630
{
3631
  struct mips_elf_link_hash_table *htab;
3632
  struct mips_got_info *g;
3633
  struct mips_got_page_entry lookup, *entry;
3634
  struct mips_got_page_range **range_ptr, *range;
3635
  bfd_vma old_pages, new_pages;
3636
  void **loc;
3637
 
3638
  htab = mips_elf_hash_table (info);
3639
  BFD_ASSERT (htab != NULL);
3640
 
3641
  g = htab->got_info;
3642
  BFD_ASSERT (g != NULL);
3643
 
3644
  /* Find the mips_got_page_entry hash table entry for this symbol.  */
3645
  lookup.abfd = abfd;
3646
  lookup.symndx = symndx;
3647
  loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3648
  if (loc == NULL)
3649
    return FALSE;
3650
 
3651
  /* Create a mips_got_page_entry if this is the first time we've
3652
     seen the symbol.  */
3653
  entry = (struct mips_got_page_entry *) *loc;
3654
  if (!entry)
3655
    {
3656
      entry = bfd_alloc (abfd, sizeof (*entry));
3657
      if (!entry)
3658
        return FALSE;
3659
 
3660
      entry->abfd = abfd;
3661
      entry->symndx = symndx;
3662
      entry->ranges = NULL;
3663
      entry->num_pages = 0;
3664
      *loc = entry;
3665
    }
3666
 
3667
  /* Skip over ranges whose maximum extent cannot share a page entry
3668
     with ADDEND.  */
3669
  range_ptr = &entry->ranges;
3670
  while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3671
    range_ptr = &(*range_ptr)->next;
3672
 
3673
  /* If we scanned to the end of the list, or found a range whose
3674
     minimum extent cannot share a page entry with ADDEND, create
3675
     a new singleton range.  */
3676
  range = *range_ptr;
3677
  if (!range || addend < range->min_addend - 0xffff)
3678
    {
3679
      range = bfd_alloc (abfd, sizeof (*range));
3680
      if (!range)
3681
        return FALSE;
3682
 
3683
      range->next = *range_ptr;
3684
      range->min_addend = addend;
3685
      range->max_addend = addend;
3686
 
3687
      *range_ptr = range;
3688
      entry->num_pages++;
3689
      g->page_gotno++;
3690
      return TRUE;
3691
    }
3692
 
3693
  /* Remember how many pages the old range contributed.  */
3694
  old_pages = mips_elf_pages_for_range (range);
3695
 
3696
  /* Update the ranges.  */
3697
  if (addend < range->min_addend)
3698
    range->min_addend = addend;
3699
  else if (addend > range->max_addend)
3700
    {
3701
      if (range->next && addend >= range->next->min_addend - 0xffff)
3702
        {
3703
          old_pages += mips_elf_pages_for_range (range->next);
3704
          range->max_addend = range->next->max_addend;
3705
          range->next = range->next->next;
3706
        }
3707
      else
3708
        range->max_addend = addend;
3709
    }
3710
 
3711
  /* Record any change in the total estimate.  */
3712
  new_pages = mips_elf_pages_for_range (range);
3713
  if (old_pages != new_pages)
3714
    {
3715
      entry->num_pages += new_pages - old_pages;
3716
      g->page_gotno += new_pages - old_pages;
3717
    }
3718
 
3719
  return TRUE;
3720
}
3721
 
3722
/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
3723
 
3724
static void
3725
mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3726
                                       unsigned int n)
3727
{
3728
  asection *s;
3729
  struct mips_elf_link_hash_table *htab;
3730
 
3731
  htab = mips_elf_hash_table (info);
3732
  BFD_ASSERT (htab != NULL);
3733
 
3734
  s = mips_elf_rel_dyn_section (info, FALSE);
3735
  BFD_ASSERT (s != NULL);
3736
 
3737
  if (htab->is_vxworks)
3738
    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3739
  else
3740
    {
3741
      if (s->size == 0)
3742
        {
3743
          /* Make room for a null element.  */
3744
          s->size += MIPS_ELF_REL_SIZE (abfd);
3745
          ++s->reloc_count;
3746
        }
3747
      s->size += n * MIPS_ELF_REL_SIZE (abfd);
3748
    }
3749
}
3750
 
3751
/* A htab_traverse callback for GOT entries.  Set boolean *DATA to true
3752
   if the GOT entry is for an indirect or warning symbol.  */
3753
 
3754
static int
3755
mips_elf_check_recreate_got (void **entryp, void *data)
3756
{
3757
  struct mips_got_entry *entry;
3758
  bfd_boolean *must_recreate;
3759
 
3760
  entry = (struct mips_got_entry *) *entryp;
3761
  must_recreate = (bfd_boolean *) data;
3762
  if (entry->abfd != NULL && entry->symndx == -1)
3763
    {
3764
      struct mips_elf_link_hash_entry *h;
3765
 
3766
      h = entry->d.h;
3767
      if (h->root.root.type == bfd_link_hash_indirect
3768
          || h->root.root.type == bfd_link_hash_warning)
3769
        {
3770
          *must_recreate = TRUE;
3771
          return 0;
3772
        }
3773
    }
3774
  return 1;
3775
}
3776
 
3777
/* A htab_traverse callback for GOT entries.  Add all entries to
3778
   hash table *DATA, converting entries for indirect and warning
3779
   symbols into entries for the target symbol.  Set *DATA to null
3780
   on error.  */
3781
 
3782
static int
3783
mips_elf_recreate_got (void **entryp, void *data)
3784
{
3785
  htab_t *new_got;
3786
  struct mips_got_entry *entry;
3787
  void **slot;
3788
 
3789
  new_got = (htab_t *) data;
3790
  entry = (struct mips_got_entry *) *entryp;
3791
  if (entry->abfd != NULL && entry->symndx == -1)
3792
    {
3793
      struct mips_elf_link_hash_entry *h;
3794
 
3795
      h = entry->d.h;
3796
      while (h->root.root.type == bfd_link_hash_indirect
3797
             || h->root.root.type == bfd_link_hash_warning)
3798
        {
3799
          BFD_ASSERT (h->global_got_area == GGA_NONE);
3800
          h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3801
        }
3802
      entry->d.h = h;
3803
    }
3804
  slot = htab_find_slot (*new_got, entry, INSERT);
3805
  if (slot == NULL)
3806
    {
3807
      *new_got = NULL;
3808
      return 0;
3809
    }
3810
  if (*slot == NULL)
3811
    *slot = entry;
3812
  else
3813
    free (entry);
3814
  return 1;
3815
}
3816
 
3817
/* If any entries in G->got_entries are for indirect or warning symbols,
3818
   replace them with entries for the target symbol.  */
3819
 
3820
static bfd_boolean
3821
mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3822
{
3823
  bfd_boolean must_recreate;
3824
  htab_t new_got;
3825
 
3826
  must_recreate = FALSE;
3827
  htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3828
  if (must_recreate)
3829
    {
3830
      new_got = htab_create (htab_size (g->got_entries),
3831
                             mips_elf_got_entry_hash,
3832
                             mips_elf_got_entry_eq, NULL);
3833
      htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3834
      if (new_got == NULL)
3835
        return FALSE;
3836
 
3837
      /* Each entry in g->got_entries has either been copied to new_got
3838
         or freed.  Now delete the hash table itself.  */
3839
      htab_delete (g->got_entries);
3840
      g->got_entries = new_got;
3841
    }
3842
  return TRUE;
3843
}
3844
 
3845
/* A mips_elf_link_hash_traverse callback for which DATA points
3846
   to the link_info structure.  Count the number of type (3) entries
3847
   in the master GOT.  */
3848
 
3849
static int
3850
mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
3851
{
3852
  struct bfd_link_info *info;
3853
  struct mips_elf_link_hash_table *htab;
3854
  struct mips_got_info *g;
3855
 
3856
  info = (struct bfd_link_info *) data;
3857
  htab = mips_elf_hash_table (info);
3858
  g = htab->got_info;
3859
  if (h->global_got_area != GGA_NONE)
3860
    {
3861
      /* Make a final decision about whether the symbol belongs in the
3862
         local or global GOT.  Symbols that bind locally can (and in the
3863
         case of forced-local symbols, must) live in the local GOT.
3864
         Those that are aren't in the dynamic symbol table must also
3865
         live in the local GOT.
3866
 
3867
         Note that the former condition does not always imply the
3868
         latter: symbols do not bind locally if they are completely
3869
         undefined.  We'll report undefined symbols later if appropriate.  */
3870
      if (h->root.dynindx == -1
3871
          || (h->got_only_for_calls
3872
              ? SYMBOL_CALLS_LOCAL (info, &h->root)
3873
              : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3874
        {
3875
          /* The symbol belongs in the local GOT.  We no longer need this
3876
             entry if it was only used for relocations; those relocations
3877
             will be against the null or section symbol instead of H.  */
3878
          if (h->global_got_area != GGA_RELOC_ONLY)
3879
            g->local_gotno++;
3880
          h->global_got_area = GGA_NONE;
3881
        }
3882
      else if (htab->is_vxworks
3883
               && h->got_only_for_calls
3884
               && h->root.plt.offset != MINUS_ONE)
3885
        /* On VxWorks, calls can refer directly to the .got.plt entry;
3886
           they don't need entries in the regular GOT.  .got.plt entries
3887
           will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
3888
        h->global_got_area = GGA_NONE;
3889
      else
3890
        {
3891
          g->global_gotno++;
3892
          if (h->global_got_area == GGA_RELOC_ONLY)
3893
            g->reloc_only_gotno++;
3894
        }
3895
    }
3896
  return 1;
3897
}
3898
 
3899
/* Compute the hash value of the bfd in a bfd2got hash entry.  */
3900
 
3901
static hashval_t
3902
mips_elf_bfd2got_entry_hash (const void *entry_)
3903
{
3904
  const struct mips_elf_bfd2got_hash *entry
3905
    = (struct mips_elf_bfd2got_hash *)entry_;
3906
 
3907
  return entry->bfd->id;
3908
}
3909
 
3910
/* Check whether two hash entries have the same bfd.  */
3911
 
3912
static int
3913
mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3914
{
3915
  const struct mips_elf_bfd2got_hash *e1
3916
    = (const struct mips_elf_bfd2got_hash *)entry1;
3917
  const struct mips_elf_bfd2got_hash *e2
3918
    = (const struct mips_elf_bfd2got_hash *)entry2;
3919
 
3920
  return e1->bfd == e2->bfd;
3921
}
3922
 
3923
/* In a multi-got link, determine the GOT to be used for IBFD.  G must
3924
   be the master GOT data.  */
3925
 
3926
static struct mips_got_info *
3927
mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3928
{
3929
  struct mips_elf_bfd2got_hash e, *p;
3930
 
3931
  if (! g->bfd2got)
3932
    return g;
3933
 
3934
  e.bfd = ibfd;
3935
  p = htab_find (g->bfd2got, &e);
3936
  return p ? p->g : NULL;
3937
}
3938
 
3939
/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3940
   Return NULL if an error occured.  */
3941
 
3942
static struct mips_got_info *
3943
mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3944
                          bfd *input_bfd)
3945
{
3946
  struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3947
  struct mips_got_info *g;
3948
  void **bfdgotp;
3949
 
3950
  bfdgot_entry.bfd = input_bfd;
3951
  bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3952
  bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3953
 
3954
  if (bfdgot == NULL)
3955
    {
3956
      bfdgot = ((struct mips_elf_bfd2got_hash *)
3957
                bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3958
      if (bfdgot == NULL)
3959
        return NULL;
3960
 
3961
      *bfdgotp = bfdgot;
3962
 
3963
      g = ((struct mips_got_info *)
3964
           bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3965
      if (g == NULL)
3966
        return NULL;
3967
 
3968
      bfdgot->bfd = input_bfd;
3969
      bfdgot->g = g;
3970
 
3971
      g->global_gotsym = NULL;
3972
      g->global_gotno = 0;
3973
      g->reloc_only_gotno = 0;
3974
      g->local_gotno = 0;
3975
      g->page_gotno = 0;
3976
      g->assigned_gotno = -1;
3977
      g->tls_gotno = 0;
3978
      g->tls_assigned_gotno = 0;
3979
      g->tls_ldm_offset = MINUS_ONE;
3980
      g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3981
                                        mips_elf_multi_got_entry_eq, NULL);
3982
      if (g->got_entries == NULL)
3983
        return NULL;
3984
 
3985
      g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3986
                                             mips_got_page_entry_eq, NULL);
3987
      if (g->got_page_entries == NULL)
3988
        return NULL;
3989
 
3990
      g->bfd2got = NULL;
3991
      g->next = NULL;
3992
    }
3993
 
3994
  return bfdgot->g;
3995
}
3996
 
3997
/* A htab_traverse callback for the entries in the master got.
3998
   Create one separate got for each bfd that has entries in the global
3999
   got, such that we can tell how many local and global entries each
4000
   bfd requires.  */
4001
 
4002
static int
4003
mips_elf_make_got_per_bfd (void **entryp, void *p)
4004
{
4005
  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4006
  struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4007
  struct mips_got_info *g;
4008
 
4009
  g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4010
  if (g == NULL)
4011
    {
4012
      arg->obfd = NULL;
4013
      return 0;
4014
    }
4015
 
4016
  /* Insert the GOT entry in the bfd's got entry hash table.  */
4017
  entryp = htab_find_slot (g->got_entries, entry, INSERT);
4018
  if (*entryp != NULL)
4019
    return 1;
4020
 
4021
  *entryp = entry;
4022
 
4023
  if (entry->tls_type)
4024
    {
4025
      if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4026
        g->tls_gotno += 2;
4027
      if (entry->tls_type & GOT_TLS_IE)
4028
        g->tls_gotno += 1;
4029
    }
4030
  else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
4031
    ++g->local_gotno;
4032
  else
4033
    ++g->global_gotno;
4034
 
4035
  return 1;
4036
}
4037
 
4038
/* A htab_traverse callback for the page entries in the master got.
4039
   Associate each page entry with the bfd's got.  */
4040
 
4041
static int
4042
mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4043
{
4044
  struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4045
  struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4046
  struct mips_got_info *g;
4047
 
4048
  g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4049
  if (g == NULL)
4050
    {
4051
      arg->obfd = NULL;
4052
      return 0;
4053
    }
4054
 
4055
  /* Insert the GOT entry in the bfd's got entry hash table.  */
4056
  entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4057
  if (*entryp != NULL)
4058
    return 1;
4059
 
4060
  *entryp = entry;
4061
  g->page_gotno += entry->num_pages;
4062
  return 1;
4063
}
4064
 
4065
/* Consider merging the got described by BFD2GOT with TO, using the
4066
   information given by ARG.  Return -1 if this would lead to overflow,
4067
   1 if they were merged successfully, and 0 if a merge failed due to
4068
   lack of memory.  (These values are chosen so that nonnegative return
4069
   values can be returned by a htab_traverse callback.)  */
4070
 
4071
static int
4072
mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4073
                         struct mips_got_info *to,
4074
                         struct mips_elf_got_per_bfd_arg *arg)
4075
{
4076
  struct mips_got_info *from = bfd2got->g;
4077
  unsigned int estimate;
4078
 
4079
  /* Work out how many page entries we would need for the combined GOT.  */
4080
  estimate = arg->max_pages;
4081
  if (estimate >= from->page_gotno + to->page_gotno)
4082
    estimate = from->page_gotno + to->page_gotno;
4083
 
4084
  /* And conservatively estimate how many local and TLS entries
4085
     would be needed.  */
4086
  estimate += from->local_gotno + to->local_gotno;
4087
  estimate += from->tls_gotno + to->tls_gotno;
4088
 
4089
  /* If we're merging with the primary got, we will always have
4090
     the full set of global entries.  Otherwise estimate those
4091
     conservatively as well.  */
4092
  if (to == arg->primary)
4093
    estimate += arg->global_count;
4094
  else
4095
    estimate += from->global_gotno + to->global_gotno;
4096
 
4097
  /* Bail out if the combined GOT might be too big.  */
4098
  if (estimate > arg->max_count)
4099
    return -1;
4100
 
4101
  /* Commit to the merge.  Record that TO is now the bfd for this got.  */
4102
  bfd2got->g = to;
4103
 
4104
  /* Transfer the bfd's got information from FROM to TO.  */
4105
  htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4106
  if (arg->obfd == NULL)
4107
    return 0;
4108
 
4109
  htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4110
  if (arg->obfd == NULL)
4111
    return 0;
4112
 
4113
  /* We don't have to worry about releasing memory of the actual
4114
     got entries, since they're all in the master got_entries hash
4115
     table anyway.  */
4116
  htab_delete (from->got_entries);
4117
  htab_delete (from->got_page_entries);
4118
  return 1;
4119
}
4120
 
4121
/* Attempt to merge gots of different input bfds.  Try to use as much
4122
   as possible of the primary got, since it doesn't require explicit
4123
   dynamic relocations, but don't use bfds that would reference global
4124
   symbols out of the addressable range.  Failing the primary got,
4125
   attempt to merge with the current got, or finish the current got
4126
   and then make make the new got current.  */
4127
 
4128
static int
4129
mips_elf_merge_gots (void **bfd2got_, void *p)
4130
{
4131
  struct mips_elf_bfd2got_hash *bfd2got
4132
    = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4133
  struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4134
  struct mips_got_info *g;
4135
  unsigned int estimate;
4136
  int result;
4137
 
4138
  g = bfd2got->g;
4139
 
4140
  /* Work out the number of page, local and TLS entries.  */
4141
  estimate = arg->max_pages;
4142
  if (estimate > g->page_gotno)
4143
    estimate = g->page_gotno;
4144
  estimate += g->local_gotno + g->tls_gotno;
4145
 
4146
  /* We place TLS GOT entries after both locals and globals.  The globals
4147
     for the primary GOT may overflow the normal GOT size limit, so be
4148
     sure not to merge a GOT which requires TLS with the primary GOT in that
4149
     case.  This doesn't affect non-primary GOTs.  */
4150
  estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4151
 
4152
  if (estimate <= arg->max_count)
4153
    {
4154
      /* If we don't have a primary GOT, use it as
4155
         a starting point for the primary GOT.  */
4156
      if (!arg->primary)
4157
        {
4158
          arg->primary = bfd2got->g;
4159
          return 1;
4160
        }
4161
 
4162
      /* Try merging with the primary GOT.  */
4163
      result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4164
      if (result >= 0)
4165
        return result;
4166
    }
4167
 
4168
  /* If we can merge with the last-created got, do it.  */
4169
  if (arg->current)
4170
    {
4171
      result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4172
      if (result >= 0)
4173
        return result;
4174
    }
4175
 
4176
  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4177
     fits; if it turns out that it doesn't, we'll get relocation
4178
     overflows anyway.  */
4179
  g->next = arg->current;
4180
  arg->current = g;
4181
 
4182
  return 1;
4183
}
4184
 
4185
/* Set the TLS GOT index for the GOT entry in ENTRYP.  ENTRYP's NEXT field
4186
   is null iff there is just a single GOT.  */
4187
 
4188
static int
4189
mips_elf_initialize_tls_index (void **entryp, void *p)
4190
{
4191
  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4192
  struct mips_got_info *g = p;
4193
  bfd_vma next_index;
4194
  unsigned char tls_type;
4195
 
4196
  /* We're only interested in TLS symbols.  */
4197
  if (entry->tls_type == 0)
4198
    return 1;
4199
 
4200
  next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4201
 
4202
  if (entry->symndx == -1 && g->next == NULL)
4203
    {
4204
      /* A type (3) got entry in the single-GOT case.  We use the symbol's
4205
         hash table entry to track its index.  */
4206
      if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4207
        return 1;
4208
      entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4209
      entry->d.h->tls_got_offset = next_index;
4210
      tls_type = entry->d.h->tls_type;
4211
    }
4212
  else
4213
    {
4214
      if (entry->tls_type & GOT_TLS_LDM)
4215
        {
4216
          /* There are separate mips_got_entry objects for each input bfd
4217
             that requires an LDM entry.  Make sure that all LDM entries in
4218
             a GOT resolve to the same index.  */
4219
          if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4220
            {
4221
              entry->gotidx = g->tls_ldm_offset;
4222
              return 1;
4223
            }
4224
          g->tls_ldm_offset = next_index;
4225
        }
4226
      entry->gotidx = next_index;
4227
      tls_type = entry->tls_type;
4228
    }
4229
 
4230
  /* Account for the entries we've just allocated.  */
4231
  if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4232
    g->tls_assigned_gotno += 2;
4233
  if (tls_type & GOT_TLS_IE)
4234
    g->tls_assigned_gotno += 1;
4235
 
4236
  return 1;
4237
}
4238
 
4239
/* If passed a NULL mips_got_info in the argument, set the marker used
4240
   to tell whether a global symbol needs a got entry (in the primary
4241
   got) to the given VALUE.
4242
 
4243
   If passed a pointer G to a mips_got_info in the argument (it must
4244
   not be the primary GOT), compute the offset from the beginning of
4245
   the (primary) GOT section to the entry in G corresponding to the
4246
   global symbol.  G's assigned_gotno must contain the index of the
4247
   first available global GOT entry in G.  VALUE must contain the size
4248
   of a GOT entry in bytes.  For each global GOT entry that requires a
4249
   dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4250
   marked as not eligible for lazy resolution through a function
4251
   stub.  */
4252
static int
4253
mips_elf_set_global_got_offset (void **entryp, void *p)
4254
{
4255
  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4256
  struct mips_elf_set_global_got_offset_arg *arg
4257
    = (struct mips_elf_set_global_got_offset_arg *)p;
4258
  struct mips_got_info *g = arg->g;
4259
 
4260
  if (g && entry->tls_type != GOT_NORMAL)
4261
    arg->needed_relocs +=
4262
      mips_tls_got_relocs (arg->info, entry->tls_type,
4263
                           entry->symndx == -1 ? &entry->d.h->root : NULL);
4264
 
4265
  if (entry->abfd != NULL
4266
      && entry->symndx == -1
4267
      && entry->d.h->global_got_area != GGA_NONE)
4268
    {
4269
      if (g)
4270
        {
4271
          BFD_ASSERT (g->global_gotsym == NULL);
4272
 
4273
          entry->gotidx = arg->value * (long) g->assigned_gotno++;
4274
          if (arg->info->shared
4275
              || (elf_hash_table (arg->info)->dynamic_sections_created
4276
                  && entry->d.h->root.def_dynamic
4277
                  && !entry->d.h->root.def_regular))
4278
            ++arg->needed_relocs;
4279
        }
4280
      else
4281
        entry->d.h->global_got_area = arg->value;
4282
    }
4283
 
4284
  return 1;
4285
}
4286
 
4287
/* A htab_traverse callback for GOT entries for which DATA is the
4288
   bfd_link_info.  Forbid any global symbols from having traditional
4289
   lazy-binding stubs.  */
4290
 
4291
static int
4292
mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4293
{
4294
  struct bfd_link_info *info;
4295
  struct mips_elf_link_hash_table *htab;
4296
  struct mips_got_entry *entry;
4297
 
4298
  entry = (struct mips_got_entry *) *entryp;
4299
  info = (struct bfd_link_info *) data;
4300
  htab = mips_elf_hash_table (info);
4301
  BFD_ASSERT (htab != NULL);
4302
 
4303
  if (entry->abfd != NULL
4304
      && entry->symndx == -1
4305
      && entry->d.h->needs_lazy_stub)
4306
    {
4307
      entry->d.h->needs_lazy_stub = FALSE;
4308
      htab->lazy_stub_count--;
4309
    }
4310
 
4311
  return 1;
4312
}
4313
 
4314
/* Return the offset of an input bfd IBFD's GOT from the beginning of
4315
   the primary GOT.  */
4316
static bfd_vma
4317
mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4318
{
4319
  if (g->bfd2got == NULL)
4320
    return 0;
4321
 
4322
  g = mips_elf_got_for_ibfd (g, ibfd);
4323
  if (! g)
4324
    return 0;
4325
 
4326
  BFD_ASSERT (g->next);
4327
 
4328
  g = g->next;
4329
 
4330
  return (g->local_gotno + g->global_gotno + g->tls_gotno)
4331
    * MIPS_ELF_GOT_SIZE (abfd);
4332
}
4333
 
4334
/* Turn a single GOT that is too big for 16-bit addressing into
4335
   a sequence of GOTs, each one 16-bit addressable.  */
4336
 
4337
static bfd_boolean
4338
mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4339
                    asection *got, bfd_size_type pages)
4340
{
4341
  struct mips_elf_link_hash_table *htab;
4342
  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4343
  struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
4344
  struct mips_got_info *g, *gg;
4345
  unsigned int assign, needed_relocs;
4346
  bfd *dynobj;
4347
 
4348
  dynobj = elf_hash_table (info)->dynobj;
4349
  htab = mips_elf_hash_table (info);
4350
  BFD_ASSERT (htab != NULL);
4351
 
4352
  g = htab->got_info;
4353
  g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
4354
                                mips_elf_bfd2got_entry_eq, NULL);
4355
  if (g->bfd2got == NULL)
4356
    return FALSE;
4357
 
4358
  got_per_bfd_arg.bfd2got = g->bfd2got;
4359
  got_per_bfd_arg.obfd = abfd;
4360
  got_per_bfd_arg.info = info;
4361
 
4362
  /* Count how many GOT entries each input bfd requires, creating a
4363
     map from bfd to got info while at that.  */
4364
  htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4365
  if (got_per_bfd_arg.obfd == NULL)
4366
    return FALSE;
4367
 
4368
  /* Also count how many page entries each input bfd requires.  */
4369
  htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4370
                 &got_per_bfd_arg);
4371
  if (got_per_bfd_arg.obfd == NULL)
4372
    return FALSE;
4373
 
4374
  got_per_bfd_arg.current = NULL;
4375
  got_per_bfd_arg.primary = NULL;
4376
  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4377
                                / MIPS_ELF_GOT_SIZE (abfd))
4378
                               - htab->reserved_gotno);
4379
  got_per_bfd_arg.max_pages = pages;
4380
  /* The number of globals that will be included in the primary GOT.
4381
     See the calls to mips_elf_set_global_got_offset below for more
4382
     information.  */
4383
  got_per_bfd_arg.global_count = g->global_gotno;
4384
 
4385
  /* Try to merge the GOTs of input bfds together, as long as they
4386
     don't seem to exceed the maximum GOT size, choosing one of them
4387
     to be the primary GOT.  */
4388
  htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4389
  if (got_per_bfd_arg.obfd == NULL)
4390
    return FALSE;
4391
 
4392
  /* If we do not find any suitable primary GOT, create an empty one.  */
4393
  if (got_per_bfd_arg.primary == NULL)
4394
    {
4395
      g->next = (struct mips_got_info *)
4396
        bfd_alloc (abfd, sizeof (struct mips_got_info));
4397
      if (g->next == NULL)
4398
        return FALSE;
4399
 
4400
      g->next->global_gotsym = NULL;
4401
      g->next->global_gotno = 0;
4402
      g->next->reloc_only_gotno = 0;
4403
      g->next->local_gotno = 0;
4404
      g->next->page_gotno = 0;
4405
      g->next->tls_gotno = 0;
4406
      g->next->assigned_gotno = 0;
4407
      g->next->tls_assigned_gotno = 0;
4408
      g->next->tls_ldm_offset = MINUS_ONE;
4409
      g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4410
                                              mips_elf_multi_got_entry_eq,
4411
                                              NULL);
4412
      if (g->next->got_entries == NULL)
4413
        return FALSE;
4414
      g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4415
                                                   mips_got_page_entry_eq,
4416
                                                   NULL);
4417
      if (g->next->got_page_entries == NULL)
4418
        return FALSE;
4419
      g->next->bfd2got = NULL;
4420
    }
4421
  else
4422
    g->next = got_per_bfd_arg.primary;
4423
  g->next->next = got_per_bfd_arg.current;
4424
 
4425
  /* GG is now the master GOT, and G is the primary GOT.  */
4426
  gg = g;
4427
  g = g->next;
4428
 
4429
  /* Map the output bfd to the primary got.  That's what we're going
4430
     to use for bfds that use GOT16 or GOT_PAGE relocations that we
4431
     didn't mark in check_relocs, and we want a quick way to find it.
4432
     We can't just use gg->next because we're going to reverse the
4433
     list.  */
4434
  {
4435
    struct mips_elf_bfd2got_hash *bfdgot;
4436
    void **bfdgotp;
4437
 
4438
    bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4439
      (abfd, sizeof (struct mips_elf_bfd2got_hash));
4440
 
4441
    if (bfdgot == NULL)
4442
      return FALSE;
4443
 
4444
    bfdgot->bfd = abfd;
4445
    bfdgot->g = g;
4446
    bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4447
 
4448
    BFD_ASSERT (*bfdgotp == NULL);
4449
    *bfdgotp = bfdgot;
4450
  }
4451
 
4452
  /* Every symbol that is referenced in a dynamic relocation must be
4453
     present in the primary GOT, so arrange for them to appear after
4454
     those that are actually referenced.  */
4455
  gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4456
  g->global_gotno = gg->global_gotno;
4457
 
4458
  set_got_offset_arg.g = NULL;
4459
  set_got_offset_arg.value = GGA_RELOC_ONLY;
4460
  htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4461
                 &set_got_offset_arg);
4462
  set_got_offset_arg.value = GGA_NORMAL;
4463
  htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4464
                 &set_got_offset_arg);
4465
 
4466
  /* Now go through the GOTs assigning them offset ranges.
4467
     [assigned_gotno, local_gotno[ will be set to the range of local
4468
     entries in each GOT.  We can then compute the end of a GOT by
4469
     adding local_gotno to global_gotno.  We reverse the list and make
4470
     it circular since then we'll be able to quickly compute the
4471
     beginning of a GOT, by computing the end of its predecessor.  To
4472
     avoid special cases for the primary GOT, while still preserving
4473
     assertions that are valid for both single- and multi-got links,
4474
     we arrange for the main got struct to have the right number of
4475
     global entries, but set its local_gotno such that the initial
4476
     offset of the primary GOT is zero.  Remember that the primary GOT
4477
     will become the last item in the circular linked list, so it
4478
     points back to the master GOT.  */
4479
  gg->local_gotno = -g->global_gotno;
4480
  gg->global_gotno = g->global_gotno;
4481
  gg->tls_gotno = 0;
4482
  assign = 0;
4483
  gg->next = gg;
4484
 
4485
  do
4486
    {
4487
      struct mips_got_info *gn;
4488
 
4489
      assign += htab->reserved_gotno;
4490
      g->assigned_gotno = assign;
4491
      g->local_gotno += assign;
4492
      g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4493
      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4494
 
4495
      /* Take g out of the direct list, and push it onto the reversed
4496
         list that gg points to.  g->next is guaranteed to be nonnull after
4497
         this operation, as required by mips_elf_initialize_tls_index. */
4498
      gn = g->next;
4499
      g->next = gg->next;
4500
      gg->next = g;
4501
 
4502
      /* Set up any TLS entries.  We always place the TLS entries after
4503
         all non-TLS entries.  */
4504
      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4505
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
4506
 
4507
      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
4508
      g = gn;
4509
 
4510
      /* Forbid global symbols in every non-primary GOT from having
4511
         lazy-binding stubs.  */
4512
      if (g)
4513
        htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4514
    }
4515
  while (g);
4516
 
4517
  got->size = (gg->next->local_gotno
4518
               + gg->next->global_gotno
4519
               + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4520
 
4521
  needed_relocs = 0;
4522
  set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4523
  set_got_offset_arg.info = info;
4524
  for (g = gg->next; g && g->next != gg; g = g->next)
4525
    {
4526
      unsigned int save_assign;
4527
 
4528
      /* Assign offsets to global GOT entries.  */
4529
      save_assign = g->assigned_gotno;
4530
      g->assigned_gotno = g->local_gotno;
4531
      set_got_offset_arg.g = g;
4532
      set_got_offset_arg.needed_relocs = 0;
4533
      htab_traverse (g->got_entries,
4534
                     mips_elf_set_global_got_offset,
4535
                     &set_got_offset_arg);
4536
      needed_relocs += set_got_offset_arg.needed_relocs;
4537
      BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4538
 
4539
      g->assigned_gotno = save_assign;
4540
      if (info->shared)
4541
        {
4542
          needed_relocs += g->local_gotno - g->assigned_gotno;
4543
          BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4544
                      + g->next->global_gotno
4545
                      + g->next->tls_gotno
4546
                      + htab->reserved_gotno);
4547
        }
4548
    }
4549
 
4550
  if (needed_relocs)
4551
    mips_elf_allocate_dynamic_relocations (dynobj, info,
4552
                                           needed_relocs);
4553
 
4554
  return TRUE;
4555
}
4556
 
4557
 
4558
/* Returns the first relocation of type r_type found, beginning with
4559
   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
4560
 
4561
static const Elf_Internal_Rela *
4562
mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4563
                          const Elf_Internal_Rela *relocation,
4564
                          const Elf_Internal_Rela *relend)
4565
{
4566
  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4567
 
4568
  while (relocation < relend)
4569
    {
4570
      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4571
          && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4572
        return relocation;
4573
 
4574
      ++relocation;
4575
    }
4576
 
4577
  /* We didn't find it.  */
4578
  return NULL;
4579
}
4580
 
4581
/* Return whether an input relocation is against a local symbol.  */
4582
 
4583
static bfd_boolean
4584
mips_elf_local_relocation_p (bfd *input_bfd,
4585
                             const Elf_Internal_Rela *relocation,
4586
                             asection **local_sections)
4587
{
4588
  unsigned long r_symndx;
4589
  Elf_Internal_Shdr *symtab_hdr;
4590
  size_t extsymoff;
4591
 
4592
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4593
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4594
  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4595
 
4596
  if (r_symndx < extsymoff)
4597
    return TRUE;
4598
  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4599
    return TRUE;
4600
 
4601
  return FALSE;
4602
}
4603
 
4604
/* Sign-extend VALUE, which has the indicated number of BITS.  */
4605
 
4606
bfd_vma
4607
_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4608
{
4609
  if (value & ((bfd_vma) 1 << (bits - 1)))
4610
    /* VALUE is negative.  */
4611
    value |= ((bfd_vma) - 1) << bits;
4612
 
4613
  return value;
4614
}
4615
 
4616
/* Return non-zero if the indicated VALUE has overflowed the maximum
4617
   range expressible by a signed number with the indicated number of
4618
   BITS.  */
4619
 
4620
static bfd_boolean
4621
mips_elf_overflow_p (bfd_vma value, int bits)
4622
{
4623
  bfd_signed_vma svalue = (bfd_signed_vma) value;
4624
 
4625
  if (svalue > (1 << (bits - 1)) - 1)
4626
    /* The value is too big.  */
4627
    return TRUE;
4628
  else if (svalue < -(1 << (bits - 1)))
4629
    /* The value is too small.  */
4630
    return TRUE;
4631
 
4632
  /* All is well.  */
4633
  return FALSE;
4634
}
4635
 
4636
/* Calculate the %high function.  */
4637
 
4638
static bfd_vma
4639
mips_elf_high (bfd_vma value)
4640
{
4641
  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4642
}
4643
 
4644
/* Calculate the %higher function.  */
4645
 
4646
static bfd_vma
4647
mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4648
{
4649
#ifdef BFD64
4650
  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4651
#else
4652
  abort ();
4653
  return MINUS_ONE;
4654
#endif
4655
}
4656
 
4657
/* Calculate the %highest function.  */
4658
 
4659
static bfd_vma
4660
mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4661
{
4662
#ifdef BFD64
4663
  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4664
#else
4665
  abort ();
4666
  return MINUS_ONE;
4667
#endif
4668
}
4669
 
4670
/* Create the .compact_rel section.  */
4671
 
4672
static bfd_boolean
4673
mips_elf_create_compact_rel_section
4674
  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4675
{
4676
  flagword flags;
4677
  register asection *s;
4678
 
4679
  if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4680
    {
4681
      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4682
               | SEC_READONLY);
4683
 
4684
      s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4685
      if (s == NULL
4686
          || ! bfd_set_section_alignment (abfd, s,
4687
                                          MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4688
        return FALSE;
4689
 
4690
      s->size = sizeof (Elf32_External_compact_rel);
4691
    }
4692
 
4693
  return TRUE;
4694
}
4695
 
4696
/* Create the .got section to hold the global offset table.  */
4697
 
4698
static bfd_boolean
4699
mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4700
{
4701
  flagword flags;
4702
  register asection *s;
4703
  struct elf_link_hash_entry *h;
4704
  struct bfd_link_hash_entry *bh;
4705
  struct mips_got_info *g;
4706
  bfd_size_type amt;
4707
  struct mips_elf_link_hash_table *htab;
4708
 
4709
  htab = mips_elf_hash_table (info);
4710
  BFD_ASSERT (htab != NULL);
4711
 
4712
  /* This function may be called more than once.  */
4713
  if (htab->sgot)
4714
    return TRUE;
4715
 
4716
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4717
           | SEC_LINKER_CREATED);
4718
 
4719
  /* We have to use an alignment of 2**4 here because this is hardcoded
4720
     in the function stub generation and in the linker script.  */
4721
  s = bfd_make_section_with_flags (abfd, ".got", flags);
4722
  if (s == NULL
4723
      || ! bfd_set_section_alignment (abfd, s, 4))
4724
    return FALSE;
4725
  htab->sgot = s;
4726
 
4727
  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
4728
     linker script because we don't want to define the symbol if we
4729
     are not creating a global offset table.  */
4730
  bh = NULL;
4731
  if (! (_bfd_generic_link_add_one_symbol
4732
         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4733
          0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4734
    return FALSE;
4735
 
4736
  h = (struct elf_link_hash_entry *) bh;
4737
  h->non_elf = 0;
4738
  h->def_regular = 1;
4739
  h->type = STT_OBJECT;
4740
  elf_hash_table (info)->hgot = h;
4741
 
4742
  if (info->shared
4743
      && ! bfd_elf_link_record_dynamic_symbol (info, h))
4744
    return FALSE;
4745
 
4746
  amt = sizeof (struct mips_got_info);
4747
  g = bfd_alloc (abfd, amt);
4748
  if (g == NULL)
4749
    return FALSE;
4750
  g->global_gotsym = NULL;
4751
  g->global_gotno = 0;
4752
  g->reloc_only_gotno = 0;
4753
  g->tls_gotno = 0;
4754
  g->local_gotno = 0;
4755
  g->page_gotno = 0;
4756
  g->assigned_gotno = 0;
4757
  g->bfd2got = NULL;
4758
  g->next = NULL;
4759
  g->tls_ldm_offset = MINUS_ONE;
4760
  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4761
                                    mips_elf_got_entry_eq, NULL);
4762
  if (g->got_entries == NULL)
4763
    return FALSE;
4764
  g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4765
                                         mips_got_page_entry_eq, NULL);
4766
  if (g->got_page_entries == NULL)
4767
    return FALSE;
4768
  htab->got_info = g;
4769
  mips_elf_section_data (s)->elf.this_hdr.sh_flags
4770
    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4771
 
4772
  /* We also need a .got.plt section when generating PLTs.  */
4773
  s = bfd_make_section_with_flags (abfd, ".got.plt",
4774
                                   SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4775
                                   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4776
  if (s == NULL)
4777
    return FALSE;
4778
  htab->sgotplt = s;
4779
 
4780
  return TRUE;
4781
}
4782
 
4783
/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4784
   __GOTT_INDEX__ symbols.  These symbols are only special for
4785
   shared objects; they are not used in executables.  */
4786
 
4787
static bfd_boolean
4788
is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4789
{
4790
  return (mips_elf_hash_table (info)->is_vxworks
4791
          && info->shared
4792
          && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4793
              || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4794
}
4795
 
4796
/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4797
   require an la25 stub.  See also mips_elf_local_pic_function_p,
4798
   which determines whether the destination function ever requires a
4799
   stub.  */
4800
 
4801
static bfd_boolean
4802
mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4803
{
4804
  /* We specifically ignore branches and jumps from EF_PIC objects,
4805
     where the onus is on the compiler or programmer to perform any
4806
     necessary initialization of $25.  Sometimes such initialization
4807
     is unnecessary; for example, -mno-shared functions do not use
4808
     the incoming value of $25, and may therefore be called directly.  */
4809
  if (PIC_OBJECT_P (input_bfd))
4810
    return FALSE;
4811
 
4812
  switch (r_type)
4813
    {
4814
    case R_MIPS_26:
4815
    case R_MIPS_PC16:
4816
    case R_MIPS16_26:
4817
      return TRUE;
4818
 
4819
    default:
4820
      return FALSE;
4821
    }
4822
}
4823
 
4824
/* Calculate the value produced by the RELOCATION (which comes from
4825
   the INPUT_BFD).  The ADDEND is the addend to use for this
4826
   RELOCATION; RELOCATION->R_ADDEND is ignored.
4827
 
4828
   The result of the relocation calculation is stored in VALUEP.
4829
   On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4830
   is a MIPS16 jump to non-MIPS16 code, or vice versa.
4831
 
4832
   This function returns bfd_reloc_continue if the caller need take no
4833
   further action regarding this relocation, bfd_reloc_notsupported if
4834
   something goes dramatically wrong, bfd_reloc_overflow if an
4835
   overflow occurs, and bfd_reloc_ok to indicate success.  */
4836
 
4837
static bfd_reloc_status_type
4838
mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4839
                               asection *input_section,
4840
                               struct bfd_link_info *info,
4841
                               const Elf_Internal_Rela *relocation,
4842
                               bfd_vma addend, reloc_howto_type *howto,
4843
                               Elf_Internal_Sym *local_syms,
4844
                               asection **local_sections, bfd_vma *valuep,
4845
                               const char **namep,
4846
                               bfd_boolean *cross_mode_jump_p,
4847
                               bfd_boolean save_addend)
4848
{
4849
  /* The eventual value we will return.  */
4850
  bfd_vma value;
4851
  /* The address of the symbol against which the relocation is
4852
     occurring.  */
4853
  bfd_vma symbol = 0;
4854
  /* The final GP value to be used for the relocatable, executable, or
4855
     shared object file being produced.  */
4856
  bfd_vma gp;
4857
  /* The place (section offset or address) of the storage unit being
4858
     relocated.  */
4859
  bfd_vma p;
4860
  /* The value of GP used to create the relocatable object.  */
4861
  bfd_vma gp0;
4862
  /* The offset into the global offset table at which the address of
4863
     the relocation entry symbol, adjusted by the addend, resides
4864
     during execution.  */
4865
  bfd_vma g = MINUS_ONE;
4866
  /* The section in which the symbol referenced by the relocation is
4867
     located.  */
4868
  asection *sec = NULL;
4869
  struct mips_elf_link_hash_entry *h = NULL;
4870
  /* TRUE if the symbol referred to by this relocation is a local
4871
     symbol.  */
4872
  bfd_boolean local_p, was_local_p;
4873
  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
4874
  bfd_boolean gp_disp_p = FALSE;
4875
  /* TRUE if the symbol referred to by this relocation is
4876
     "__gnu_local_gp".  */
4877
  bfd_boolean gnu_local_gp_p = FALSE;
4878
  Elf_Internal_Shdr *symtab_hdr;
4879
  size_t extsymoff;
4880
  unsigned long r_symndx;
4881
  int r_type;
4882
  /* TRUE if overflow occurred during the calculation of the
4883
     relocation value.  */
4884
  bfd_boolean overflowed_p;
4885
  /* TRUE if this relocation refers to a MIPS16 function.  */
4886
  bfd_boolean target_is_16_bit_code_p = FALSE;
4887
  struct mips_elf_link_hash_table *htab;
4888
  bfd *dynobj;
4889
 
4890
  dynobj = elf_hash_table (info)->dynobj;
4891
  htab = mips_elf_hash_table (info);
4892
  BFD_ASSERT (htab != NULL);
4893
 
4894
  /* Parse the relocation.  */
4895
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4896
  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4897
  p = (input_section->output_section->vma
4898
       + input_section->output_offset
4899
       + relocation->r_offset);
4900
 
4901
  /* Assume that there will be no overflow.  */
4902
  overflowed_p = FALSE;
4903
 
4904
  /* Figure out whether or not the symbol is local, and get the offset
4905
     used in the array of hash table entries.  */
4906
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4907
  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4908
                                         local_sections);
4909
  was_local_p = local_p;
4910
  if (! elf_bad_symtab (input_bfd))
4911
    extsymoff = symtab_hdr->sh_info;
4912
  else
4913
    {
4914
      /* The symbol table does not follow the rule that local symbols
4915
         must come before globals.  */
4916
      extsymoff = 0;
4917
    }
4918
 
4919
  /* Figure out the value of the symbol.  */
4920
  if (local_p)
4921
    {
4922
      Elf_Internal_Sym *sym;
4923
 
4924
      sym = local_syms + r_symndx;
4925
      sec = local_sections[r_symndx];
4926
 
4927
      symbol = sec->output_section->vma + sec->output_offset;
4928
      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4929
          || (sec->flags & SEC_MERGE))
4930
        symbol += sym->st_value;
4931
      if ((sec->flags & SEC_MERGE)
4932
          && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4933
        {
4934
          addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4935
          addend -= symbol;
4936
          addend += sec->output_section->vma + sec->output_offset;
4937
        }
4938
 
4939
      /* MIPS16 text labels should be treated as odd.  */
4940
      if (ELF_ST_IS_MIPS16 (sym->st_other))
4941
        ++symbol;
4942
 
4943
      /* Record the name of this symbol, for our caller.  */
4944
      *namep = bfd_elf_string_from_elf_section (input_bfd,
4945
                                                symtab_hdr->sh_link,
4946
                                                sym->st_name);
4947
      if (*namep == '\0')
4948
        *namep = bfd_section_name (input_bfd, sec);
4949
 
4950
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4951
    }
4952
  else
4953
    {
4954
      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
4955
 
4956
      /* For global symbols we look up the symbol in the hash-table.  */
4957
      h = ((struct mips_elf_link_hash_entry *)
4958
           elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4959
      /* Find the real hash-table entry for this symbol.  */
4960
      while (h->root.root.type == bfd_link_hash_indirect
4961
             || h->root.root.type == bfd_link_hash_warning)
4962
        h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4963
 
4964
      /* Record the name of this symbol, for our caller.  */
4965
      *namep = h->root.root.root.string;
4966
 
4967
      /* See if this is the special _gp_disp symbol.  Note that such a
4968
         symbol must always be a global symbol.  */
4969
      if (strcmp (*namep, "_gp_disp") == 0
4970
          && ! NEWABI_P (input_bfd))
4971
        {
4972
          /* Relocations against _gp_disp are permitted only with
4973
             R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
4974
          if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4975
            return bfd_reloc_notsupported;
4976
 
4977
          gp_disp_p = TRUE;
4978
        }
4979
      /* See if this is the special _gp symbol.  Note that such a
4980
         symbol must always be a global symbol.  */
4981
      else if (strcmp (*namep, "__gnu_local_gp") == 0)
4982
        gnu_local_gp_p = TRUE;
4983
 
4984
 
4985
      /* If this symbol is defined, calculate its address.  Note that
4986
         _gp_disp is a magic symbol, always implicitly defined by the
4987
         linker, so it's inappropriate to check to see whether or not
4988
         its defined.  */
4989
      else if ((h->root.root.type == bfd_link_hash_defined
4990
                || h->root.root.type == bfd_link_hash_defweak)
4991
               && h->root.root.u.def.section)
4992
        {
4993
          sec = h->root.root.u.def.section;
4994
          if (sec->output_section)
4995
            symbol = (h->root.root.u.def.value
4996
                      + sec->output_section->vma
4997
                      + sec->output_offset);
4998
          else
4999
            symbol = h->root.root.u.def.value;
5000
        }
5001
      else if (h->root.root.type == bfd_link_hash_undefweak)
5002
        /* We allow relocations against undefined weak symbols, giving
5003
           it the value zero, so that you can undefined weak functions
5004
           and check to see if they exist by looking at their
5005
           addresses.  */
5006
        symbol = 0;
5007
      else if (info->unresolved_syms_in_objects == RM_IGNORE
5008
               && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5009
        symbol = 0;
5010
      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5011
                       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5012
        {
5013
          /* If this is a dynamic link, we should have created a
5014
             _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5015
             in in _bfd_mips_elf_create_dynamic_sections.
5016
             Otherwise, we should define the symbol with a value of 0.
5017
             FIXME: It should probably get into the symbol table
5018
             somehow as well.  */
5019
          BFD_ASSERT (! info->shared);
5020
          BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5021
          symbol = 0;
5022
        }
5023
      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5024
        {
5025
          /* This is an optional symbol - an Irix specific extension to the
5026
             ELF spec.  Ignore it for now.
5027
             XXX - FIXME - there is more to the spec for OPTIONAL symbols
5028
             than simply ignoring them, but we do not handle this for now.
5029
             For information see the "64-bit ELF Object File Specification"
5030
             which is available from here:
5031
             http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5032
          symbol = 0;
5033
        }
5034
      else if ((*info->callbacks->undefined_symbol)
5035
               (info, h->root.root.root.string, input_bfd,
5036
                input_section, relocation->r_offset,
5037
                (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5038
                 || ELF_ST_VISIBILITY (h->root.other)))
5039
        {
5040
          return bfd_reloc_undefined;
5041
        }
5042
      else
5043
        {
5044
          return bfd_reloc_notsupported;
5045
        }
5046
 
5047
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5048
    }
5049
 
5050
  /* If this is a reference to a 16-bit function with a stub, we need
5051
     to redirect the relocation to the stub unless:
5052
 
5053
     (a) the relocation is for a MIPS16 JAL;
5054
 
5055
     (b) the relocation is for a MIPS16 PIC call, and there are no
5056
         non-MIPS16 uses of the GOT slot; or
5057
 
5058
     (c) the section allows direct references to MIPS16 functions.  */
5059
  if (r_type != R_MIPS16_26
5060
      && !info->relocatable
5061
      && ((h != NULL
5062
           && h->fn_stub != NULL
5063
           && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5064
          || (local_p
5065
              && elf_tdata (input_bfd)->local_stubs != NULL
5066
              && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5067
      && !section_allows_mips16_refs_p (input_section))
5068
    {
5069
      /* This is a 32- or 64-bit call to a 16-bit function.  We should
5070
         have already noticed that we were going to need the
5071
         stub.  */
5072
      if (local_p)
5073
        sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5074
      else
5075
        {
5076
          BFD_ASSERT (h->need_fn_stub);
5077
          sec = h->fn_stub;
5078
        }
5079
 
5080
      symbol = sec->output_section->vma + sec->output_offset;
5081
      /* The target is 16-bit, but the stub isn't.  */
5082
      target_is_16_bit_code_p = FALSE;
5083
    }
5084
  /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5085
     need to redirect the call to the stub.  Note that we specifically
5086
     exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5087
     use an indirect stub instead.  */
5088
  else if (r_type == R_MIPS16_26 && !info->relocatable
5089
           && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5090
               || (local_p
5091
                   && elf_tdata (input_bfd)->local_call_stubs != NULL
5092
                   && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5093
           && !target_is_16_bit_code_p)
5094
    {
5095
      if (local_p)
5096
        sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5097
      else
5098
        {
5099
          /* If both call_stub and call_fp_stub are defined, we can figure
5100
             out which one to use by checking which one appears in the input
5101
             file.  */
5102
          if (h->call_stub != NULL && h->call_fp_stub != NULL)
5103
            {
5104
              asection *o;
5105
 
5106
              sec = NULL;
5107
              for (o = input_bfd->sections; o != NULL; o = o->next)
5108
                {
5109
                  if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5110
                    {
5111
                      sec = h->call_fp_stub;
5112
                      break;
5113
                    }
5114
                }
5115
              if (sec == NULL)
5116
                sec = h->call_stub;
5117
            }
5118
          else if (h->call_stub != NULL)
5119
            sec = h->call_stub;
5120
          else
5121
            sec = h->call_fp_stub;
5122
        }
5123
 
5124
      BFD_ASSERT (sec->size > 0);
5125
      symbol = sec->output_section->vma + sec->output_offset;
5126
    }
5127
  /* If this is a direct call to a PIC function, redirect to the
5128
     non-PIC stub.  */
5129
  else if (h != NULL && h->la25_stub
5130
           && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5131
    symbol = (h->la25_stub->stub_section->output_section->vma
5132
              + h->la25_stub->stub_section->output_offset
5133
              + h->la25_stub->offset);
5134
 
5135
  /* Calls from 16-bit code to 32-bit code and vice versa require the
5136
     mode change.  */
5137
  *cross_mode_jump_p = !info->relocatable
5138
                       && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5139
                           || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5140
                               && target_is_16_bit_code_p));
5141
 
5142
  local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
5143
 
5144
  gp0 = _bfd_get_gp_value (input_bfd);
5145
  gp = _bfd_get_gp_value (abfd);
5146
  if (htab->got_info)
5147
    gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5148
 
5149
  if (gnu_local_gp_p)
5150
    symbol = gp;
5151
 
5152
  /* Global R_MIPS_GOT_PAGE relocations are equivalent to R_MIPS_GOT_DISP.
5153
     The addend is applied by the corresponding R_MIPS_GOT_OFST.  */
5154
  if (r_type == R_MIPS_GOT_PAGE && !local_p)
5155
    {
5156
      r_type = R_MIPS_GOT_DISP;
5157
      addend = 0;
5158
    }
5159
 
5160
  /* If we haven't already determined the GOT offset, and we're going
5161
     to need it, get it now.  */
5162
  switch (r_type)
5163
    {
5164
    case R_MIPS16_CALL16:
5165
    case R_MIPS16_GOT16:
5166
    case R_MIPS_CALL16:
5167
    case R_MIPS_GOT16:
5168
    case R_MIPS_GOT_DISP:
5169
    case R_MIPS_GOT_HI16:
5170
    case R_MIPS_CALL_HI16:
5171
    case R_MIPS_GOT_LO16:
5172
    case R_MIPS_CALL_LO16:
5173
    case R_MIPS_TLS_GD:
5174
    case R_MIPS_TLS_GOTTPREL:
5175
    case R_MIPS_TLS_LDM:
5176
      /* Find the index into the GOT where this value is located.  */
5177
      if (r_type == R_MIPS_TLS_LDM)
5178
        {
5179
          g = mips_elf_local_got_index (abfd, input_bfd, info,
5180
                                        0, 0, NULL, r_type);
5181
          if (g == MINUS_ONE)
5182
            return bfd_reloc_outofrange;
5183
        }
5184
      else if (!local_p)
5185
        {
5186
          /* On VxWorks, CALL relocations should refer to the .got.plt
5187
             entry, which is initialized to point at the PLT stub.  */
5188
          if (htab->is_vxworks
5189
              && (r_type == R_MIPS_CALL_HI16
5190
                  || r_type == R_MIPS_CALL_LO16
5191
                  || call16_reloc_p (r_type)))
5192
            {
5193
              BFD_ASSERT (addend == 0);
5194
              BFD_ASSERT (h->root.needs_plt);
5195
              g = mips_elf_gotplt_index (info, &h->root);
5196
            }
5197
          else
5198
            {
5199
              BFD_ASSERT (addend == 0);
5200
              g = mips_elf_global_got_index (dynobj, input_bfd,
5201
                                             &h->root, r_type, info);
5202
              if (h->tls_type == GOT_NORMAL
5203
                  && !elf_hash_table (info)->dynamic_sections_created)
5204
                /* This is a static link.  We must initialize the GOT entry.  */
5205
                MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5206
            }
5207
        }
5208
      else if (!htab->is_vxworks
5209
               && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5210
        /* The calculation below does not involve "g".  */
5211
        break;
5212
      else
5213
        {
5214
          g = mips_elf_local_got_index (abfd, input_bfd, info,
5215
                                        symbol + addend, r_symndx, h, r_type);
5216
          if (g == MINUS_ONE)
5217
            return bfd_reloc_outofrange;
5218
        }
5219
 
5220
      /* Convert GOT indices to actual offsets.  */
5221
      g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5222
      break;
5223
    }
5224
 
5225
  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5226
     symbols are resolved by the loader.  Add them to .rela.dyn.  */
5227
  if (h != NULL && is_gott_symbol (info, &h->root))
5228
    {
5229
      Elf_Internal_Rela outrel;
5230
      bfd_byte *loc;
5231
      asection *s;
5232
 
5233
      s = mips_elf_rel_dyn_section (info, FALSE);
5234
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5235
 
5236
      outrel.r_offset = (input_section->output_section->vma
5237
                         + input_section->output_offset
5238
                         + relocation->r_offset);
5239
      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5240
      outrel.r_addend = addend;
5241
      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5242
 
5243
      /* If we've written this relocation for a readonly section,
5244
         we need to set DF_TEXTREL again, so that we do not delete the
5245
         DT_TEXTREL tag.  */
5246
      if (MIPS_ELF_READONLY_SECTION (input_section))
5247
        info->flags |= DF_TEXTREL;
5248
 
5249
      *valuep = 0;
5250
      return bfd_reloc_ok;
5251
    }
5252
 
5253
  /* Figure out what kind of relocation is being performed.  */
5254
  switch (r_type)
5255
    {
5256
    case R_MIPS_NONE:
5257
      return bfd_reloc_continue;
5258
 
5259
    case R_MIPS_16:
5260
      value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5261
      overflowed_p = mips_elf_overflow_p (value, 16);
5262
      break;
5263
 
5264
    case R_MIPS_32:
5265
    case R_MIPS_REL32:
5266
    case R_MIPS_64:
5267
      if ((info->shared
5268
           || (htab->root.dynamic_sections_created
5269
               && h != NULL
5270
               && h->root.def_dynamic
5271
               && !h->root.def_regular
5272
               && !h->has_static_relocs))
5273
          && r_symndx != STN_UNDEF
5274
          && (h == NULL
5275
              || h->root.root.type != bfd_link_hash_undefweak
5276
              || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5277
          && (input_section->flags & SEC_ALLOC) != 0)
5278
        {
5279
          /* If we're creating a shared library, then we can't know
5280
             where the symbol will end up.  So, we create a relocation
5281
             record in the output, and leave the job up to the dynamic
5282
             linker.  We must do the same for executable references to
5283
             shared library symbols, unless we've decided to use copy
5284
             relocs or PLTs instead.  */
5285
          value = addend;
5286
          if (!mips_elf_create_dynamic_relocation (abfd,
5287
                                                   info,
5288
                                                   relocation,
5289
                                                   h,
5290
                                                   sec,
5291
                                                   symbol,
5292
                                                   &value,
5293
                                                   input_section))
5294
            return bfd_reloc_undefined;
5295
        }
5296
      else
5297
        {
5298
          if (r_type != R_MIPS_REL32)
5299
            value = symbol + addend;
5300
          else
5301
            value = addend;
5302
        }
5303
      value &= howto->dst_mask;
5304
      break;
5305
 
5306
    case R_MIPS_PC32:
5307
      value = symbol + addend - p;
5308
      value &= howto->dst_mask;
5309
      break;
5310
 
5311
    case R_MIPS16_26:
5312
      /* The calculation for R_MIPS16_26 is just the same as for an
5313
         R_MIPS_26.  It's only the storage of the relocated field into
5314
         the output file that's different.  That's handled in
5315
         mips_elf_perform_relocation.  So, we just fall through to the
5316
         R_MIPS_26 case here.  */
5317
    case R_MIPS_26:
5318
      if (was_local_p)
5319
        value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
5320
      else
5321
        {
5322
          value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
5323
          if (h->root.root.type != bfd_link_hash_undefweak)
5324
            overflowed_p = (value >> 26) != ((p + 4) >> 28);
5325
        }
5326
      value &= howto->dst_mask;
5327
      break;
5328
 
5329
    case R_MIPS_TLS_DTPREL_HI16:
5330
      value = (mips_elf_high (addend + symbol - dtprel_base (info))
5331
               & howto->dst_mask);
5332
      break;
5333
 
5334
    case R_MIPS_TLS_DTPREL_LO16:
5335
    case R_MIPS_TLS_DTPREL32:
5336
    case R_MIPS_TLS_DTPREL64:
5337
      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5338
      break;
5339
 
5340
    case R_MIPS_TLS_TPREL_HI16:
5341
      value = (mips_elf_high (addend + symbol - tprel_base (info))
5342
               & howto->dst_mask);
5343
      break;
5344
 
5345
    case R_MIPS_TLS_TPREL_LO16:
5346
      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5347
      break;
5348
 
5349
    case R_MIPS_HI16:
5350
    case R_MIPS16_HI16:
5351
      if (!gp_disp_p)
5352
        {
5353
          value = mips_elf_high (addend + symbol);
5354
          value &= howto->dst_mask;
5355
        }
5356
      else
5357
        {
5358
          /* For MIPS16 ABI code we generate this sequence
5359
                0: li      $v0,%hi(_gp_disp)
5360
                4: addiupc $v1,%lo(_gp_disp)
5361
                8: sll     $v0,16
5362
               12: addu    $v0,$v1
5363
               14: move    $gp,$v0
5364
             So the offsets of hi and lo relocs are the same, but the
5365
             $pc is four higher than $t9 would be, so reduce
5366
             both reloc addends by 4. */
5367
          if (r_type == R_MIPS16_HI16)
5368
            value = mips_elf_high (addend + gp - p - 4);
5369
          else
5370
            value = mips_elf_high (addend + gp - p);
5371
          overflowed_p = mips_elf_overflow_p (value, 16);
5372
        }
5373
      break;
5374
 
5375
    case R_MIPS_LO16:
5376
    case R_MIPS16_LO16:
5377
      if (!gp_disp_p)
5378
        value = (symbol + addend) & howto->dst_mask;
5379
      else
5380
        {
5381
          /* See the comment for R_MIPS16_HI16 above for the reason
5382
             for this conditional.  */
5383
          if (r_type == R_MIPS16_LO16)
5384
            value = addend + gp - p;
5385
          else
5386
            value = addend + gp - p + 4;
5387
          /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5388
             for overflow.  But, on, say, IRIX5, relocations against
5389
             _gp_disp are normally generated from the .cpload
5390
             pseudo-op.  It generates code that normally looks like
5391
             this:
5392
 
5393
               lui    $gp,%hi(_gp_disp)
5394
               addiu  $gp,$gp,%lo(_gp_disp)
5395
               addu   $gp,$gp,$t9
5396
 
5397
             Here $t9 holds the address of the function being called,
5398
             as required by the MIPS ELF ABI.  The R_MIPS_LO16
5399
             relocation can easily overflow in this situation, but the
5400
             R_MIPS_HI16 relocation will handle the overflow.
5401
             Therefore, we consider this a bug in the MIPS ABI, and do
5402
             not check for overflow here.  */
5403
        }
5404
      break;
5405
 
5406
    case R_MIPS_LITERAL:
5407
      /* Because we don't merge literal sections, we can handle this
5408
         just like R_MIPS_GPREL16.  In the long run, we should merge
5409
         shared literals, and then we will need to additional work
5410
         here.  */
5411
 
5412
      /* Fall through.  */
5413
 
5414
    case R_MIPS16_GPREL:
5415
      /* The R_MIPS16_GPREL performs the same calculation as
5416
         R_MIPS_GPREL16, but stores the relocated bits in a different
5417
         order.  We don't need to do anything special here; the
5418
         differences are handled in mips_elf_perform_relocation.  */
5419
    case R_MIPS_GPREL16:
5420
      /* Only sign-extend the addend if it was extracted from the
5421
         instruction.  If the addend was separate, leave it alone,
5422
         otherwise we may lose significant bits.  */
5423
      if (howto->partial_inplace)
5424
        addend = _bfd_mips_elf_sign_extend (addend, 16);
5425
      value = symbol + addend - gp;
5426
      /* If the symbol was local, any earlier relocatable links will
5427
         have adjusted its addend with the gp offset, so compensate
5428
         for that now.  Don't do it for symbols forced local in this
5429
         link, though, since they won't have had the gp offset applied
5430
         to them before.  */
5431
      if (was_local_p)
5432
        value += gp0;
5433
      overflowed_p = mips_elf_overflow_p (value, 16);
5434
      break;
5435
 
5436
    case R_MIPS16_GOT16:
5437
    case R_MIPS16_CALL16:
5438
    case R_MIPS_GOT16:
5439
    case R_MIPS_CALL16:
5440
      /* VxWorks does not have separate local and global semantics for
5441
         R_MIPS*_GOT16; every relocation evaluates to "G".  */
5442
      if (!htab->is_vxworks && local_p)
5443
        {
5444
          value = mips_elf_got16_entry (abfd, input_bfd, info,
5445
                                        symbol + addend, !was_local_p);
5446
          if (value == MINUS_ONE)
5447
            return bfd_reloc_outofrange;
5448
          value
5449
            = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5450
          overflowed_p = mips_elf_overflow_p (value, 16);
5451
          break;
5452
        }
5453
 
5454
      /* Fall through.  */
5455
 
5456
    case R_MIPS_TLS_GD:
5457
    case R_MIPS_TLS_GOTTPREL:
5458
    case R_MIPS_TLS_LDM:
5459
    case R_MIPS_GOT_DISP:
5460
      value = g;
5461
      overflowed_p = mips_elf_overflow_p (value, 16);
5462
      break;
5463
 
5464
    case R_MIPS_GPREL32:
5465
      value = (addend + symbol + gp0 - gp);
5466
      if (!save_addend)
5467
        value &= howto->dst_mask;
5468
      break;
5469
 
5470
    case R_MIPS_PC16:
5471
    case R_MIPS_GNU_REL16_S2:
5472
      value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5473
      overflowed_p = mips_elf_overflow_p (value, 18);
5474
      value >>= howto->rightshift;
5475
      value &= howto->dst_mask;
5476
      break;
5477
 
5478
    case R_MIPS_GOT_HI16:
5479
    case R_MIPS_CALL_HI16:
5480
      /* We're allowed to handle these two relocations identically.
5481
         The dynamic linker is allowed to handle the CALL relocations
5482
         differently by creating a lazy evaluation stub.  */
5483
      value = g;
5484
      value = mips_elf_high (value);
5485
      value &= howto->dst_mask;
5486
      break;
5487
 
5488
    case R_MIPS_GOT_LO16:
5489
    case R_MIPS_CALL_LO16:
5490
      value = g & howto->dst_mask;
5491
      break;
5492
 
5493
    case R_MIPS_GOT_PAGE:
5494
      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5495
      if (value == MINUS_ONE)
5496
        return bfd_reloc_outofrange;
5497
      value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5498
      overflowed_p = mips_elf_overflow_p (value, 16);
5499
      break;
5500
 
5501
    case R_MIPS_GOT_OFST:
5502
      if (local_p)
5503
        mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5504
      else
5505
        value = addend;
5506
      overflowed_p = mips_elf_overflow_p (value, 16);
5507
      break;
5508
 
5509
    case R_MIPS_SUB:
5510
      value = symbol - addend;
5511
      value &= howto->dst_mask;
5512
      break;
5513
 
5514
    case R_MIPS_HIGHER:
5515
      value = mips_elf_higher (addend + symbol);
5516
      value &= howto->dst_mask;
5517
      break;
5518
 
5519
    case R_MIPS_HIGHEST:
5520
      value = mips_elf_highest (addend + symbol);
5521
      value &= howto->dst_mask;
5522
      break;
5523
 
5524
    case R_MIPS_SCN_DISP:
5525
      value = symbol + addend - sec->output_offset;
5526
      value &= howto->dst_mask;
5527
      break;
5528
 
5529
    case R_MIPS_JALR:
5530
      /* This relocation is only a hint.  In some cases, we optimize
5531
         it into a bal instruction.  But we don't try to optimize
5532
         when the symbol does not resolve locally.  */
5533
      if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5534
        return bfd_reloc_continue;
5535
      value = symbol + addend;
5536
      break;
5537
 
5538
    case R_MIPS_PJUMP:
5539
    case R_MIPS_GNU_VTINHERIT:
5540
    case R_MIPS_GNU_VTENTRY:
5541
      /* We don't do anything with these at present.  */
5542
      return bfd_reloc_continue;
5543
 
5544
    default:
5545
      /* An unrecognized relocation type.  */
5546
      return bfd_reloc_notsupported;
5547
    }
5548
 
5549
  /* Store the VALUE for our caller.  */
5550
  *valuep = value;
5551
  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5552
}
5553
 
5554
/* Obtain the field relocated by RELOCATION.  */
5555
 
5556
static bfd_vma
5557
mips_elf_obtain_contents (reloc_howto_type *howto,
5558
                          const Elf_Internal_Rela *relocation,
5559
                          bfd *input_bfd, bfd_byte *contents)
5560
{
5561
  bfd_vma x;
5562
  bfd_byte *location = contents + relocation->r_offset;
5563
 
5564
  /* Obtain the bytes.  */
5565
  x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5566
 
5567
  return x;
5568
}
5569
 
5570
/* It has been determined that the result of the RELOCATION is the
5571
   VALUE.  Use HOWTO to place VALUE into the output file at the
5572
   appropriate position.  The SECTION is the section to which the
5573
   relocation applies.
5574
   CROSS_MODE_JUMP_P is true if the relocation field
5575
   is a MIPS16 jump to non-MIPS16 code, or vice versa.
5576
 
5577
   Returns FALSE if anything goes wrong.  */
5578
 
5579
static bfd_boolean
5580
mips_elf_perform_relocation (struct bfd_link_info *info,
5581
                             reloc_howto_type *howto,
5582
                             const Elf_Internal_Rela *relocation,
5583
                             bfd_vma value, bfd *input_bfd,
5584
                             asection *input_section, bfd_byte *contents,
5585
                             bfd_boolean cross_mode_jump_p)
5586
{
5587
  bfd_vma x;
5588
  bfd_byte *location;
5589
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5590
 
5591
  /* Figure out where the relocation is occurring.  */
5592
  location = contents + relocation->r_offset;
5593
 
5594
  _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5595
 
5596
  /* Obtain the current value.  */
5597
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5598
 
5599
  /* Clear the field we are setting.  */
5600
  x &= ~howto->dst_mask;
5601
 
5602
  /* Set the field.  */
5603
  x |= (value & howto->dst_mask);
5604
 
5605
  /* If required, turn JAL into JALX.  */
5606
  if (cross_mode_jump_p && jal_reloc_p (r_type))
5607
    {
5608
      bfd_boolean ok;
5609
      bfd_vma opcode = x >> 26;
5610
      bfd_vma jalx_opcode;
5611
 
5612
      /* Check to see if the opcode is already JAL or JALX.  */
5613
      if (r_type == R_MIPS16_26)
5614
        {
5615
          ok = ((opcode == 0x6) || (opcode == 0x7));
5616
          jalx_opcode = 0x7;
5617
        }
5618
      else
5619
        {
5620
          ok = ((opcode == 0x3) || (opcode == 0x1d));
5621
          jalx_opcode = 0x1d;
5622
        }
5623
 
5624
      /* If the opcode is not JAL or JALX, there's a problem.  */
5625
      if (!ok)
5626
        {
5627
          (*_bfd_error_handler)
5628
            (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
5629
             input_bfd,
5630
             input_section,
5631
             (unsigned long) relocation->r_offset);
5632
          bfd_set_error (bfd_error_bad_value);
5633
          return FALSE;
5634
        }
5635
 
5636
      /* Make this the JALX opcode.  */
5637
      x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5638
    }
5639
 
5640
  /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5641
     range.  */
5642
  if (!info->relocatable
5643
      && !cross_mode_jump_p
5644
      && ((JAL_TO_BAL_P (input_bfd)
5645
           && r_type == R_MIPS_26
5646
           && (x >> 26) == 0x3)         /* jal addr */
5647
          || (JALR_TO_BAL_P (input_bfd)
5648
              && r_type == R_MIPS_JALR
5649
              && x == 0x0320f809)       /* jalr t9 */
5650
          || (JR_TO_B_P (input_bfd)
5651
              && r_type == R_MIPS_JALR
5652
              && x == 0x03200008)))     /* jr t9 */
5653
    {
5654
      bfd_vma addr;
5655
      bfd_vma dest;
5656
      bfd_signed_vma off;
5657
 
5658
      addr = (input_section->output_section->vma
5659
              + input_section->output_offset
5660
              + relocation->r_offset
5661
              + 4);
5662
      if (r_type == R_MIPS_26)
5663
        dest = (value << 2) | ((addr >> 28) << 28);
5664
      else
5665
        dest = value;
5666
      off = dest - addr;
5667
      if (off <= 0x1ffff && off >= -0x20000)
5668
        {
5669
          if (x == 0x03200008)  /* jr t9 */
5670
            x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
5671
          else
5672
            x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
5673
        }
5674
    }
5675
 
5676
  /* Put the value into the output.  */
5677
  bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5678
 
5679
  _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5680
                                location);
5681
 
5682
  return TRUE;
5683
}
5684
 
5685
/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
5686
   is the original relocation, which is now being transformed into a
5687
   dynamic relocation.  The ADDENDP is adjusted if necessary; the
5688
   caller should store the result in place of the original addend.  */
5689
 
5690
static bfd_boolean
5691
mips_elf_create_dynamic_relocation (bfd *output_bfd,
5692
                                    struct bfd_link_info *info,
5693
                                    const Elf_Internal_Rela *rel,
5694
                                    struct mips_elf_link_hash_entry *h,
5695
                                    asection *sec, bfd_vma symbol,
5696
                                    bfd_vma *addendp, asection *input_section)
5697
{
5698
  Elf_Internal_Rela outrel[3];
5699
  asection *sreloc;
5700
  bfd *dynobj;
5701
  int r_type;
5702
  long indx;
5703
  bfd_boolean defined_p;
5704
  struct mips_elf_link_hash_table *htab;
5705
 
5706
  htab = mips_elf_hash_table (info);
5707
  BFD_ASSERT (htab != NULL);
5708
 
5709
  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5710
  dynobj = elf_hash_table (info)->dynobj;
5711
  sreloc = mips_elf_rel_dyn_section (info, FALSE);
5712
  BFD_ASSERT (sreloc != NULL);
5713
  BFD_ASSERT (sreloc->contents != NULL);
5714
  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5715
              < sreloc->size);
5716
 
5717
  outrel[0].r_offset =
5718
    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5719
  if (ABI_64_P (output_bfd))
5720
    {
5721
      outrel[1].r_offset =
5722
        _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5723
      outrel[2].r_offset =
5724
        _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5725
    }
5726
 
5727
  if (outrel[0].r_offset == MINUS_ONE)
5728
    /* The relocation field has been deleted.  */
5729
    return TRUE;
5730
 
5731
  if (outrel[0].r_offset == MINUS_TWO)
5732
    {
5733
      /* The relocation field has been converted into a relative value of
5734
         some sort.  Functions like _bfd_elf_write_section_eh_frame expect
5735
         the field to be fully relocated, so add in the symbol's value.  */
5736
      *addendp += symbol;
5737
      return TRUE;
5738
    }
5739
 
5740
  /* We must now calculate the dynamic symbol table index to use
5741
     in the relocation.  */
5742
  if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5743
    {
5744
      BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5745
      indx = h->root.dynindx;
5746
      if (SGI_COMPAT (output_bfd))
5747
        defined_p = h->root.def_regular;
5748
      else
5749
        /* ??? glibc's ld.so just adds the final GOT entry to the
5750
           relocation field.  It therefore treats relocs against
5751
           defined symbols in the same way as relocs against
5752
           undefined symbols.  */
5753
        defined_p = FALSE;
5754
    }
5755
  else
5756
    {
5757
      if (sec != NULL && bfd_is_abs_section (sec))
5758
        indx = 0;
5759
      else if (sec == NULL || sec->owner == NULL)
5760
        {
5761
          bfd_set_error (bfd_error_bad_value);
5762
          return FALSE;
5763
        }
5764
      else
5765
        {
5766
          indx = elf_section_data (sec->output_section)->dynindx;
5767
          if (indx == 0)
5768
            {
5769
              asection *osec = htab->root.text_index_section;
5770
              indx = elf_section_data (osec)->dynindx;
5771
            }
5772
          if (indx == 0)
5773
            abort ();
5774
        }
5775
 
5776
      /* Instead of generating a relocation using the section
5777
         symbol, we may as well make it a fully relative
5778
         relocation.  We want to avoid generating relocations to
5779
         local symbols because we used to generate them
5780
         incorrectly, without adding the original symbol value,
5781
         which is mandated by the ABI for section symbols.  In
5782
         order to give dynamic loaders and applications time to
5783
         phase out the incorrect use, we refrain from emitting
5784
         section-relative relocations.  It's not like they're
5785
         useful, after all.  This should be a bit more efficient
5786
         as well.  */
5787
      /* ??? Although this behavior is compatible with glibc's ld.so,
5788
         the ABI says that relocations against STN_UNDEF should have
5789
         a symbol value of 0.  Irix rld honors this, so relocations
5790
         against STN_UNDEF have no effect.  */
5791
      if (!SGI_COMPAT (output_bfd))
5792
        indx = 0;
5793
      defined_p = TRUE;
5794
    }
5795
 
5796
  /* If the relocation was previously an absolute relocation and
5797
     this symbol will not be referred to by the relocation, we must
5798
     adjust it by the value we give it in the dynamic symbol table.
5799
     Otherwise leave the job up to the dynamic linker.  */
5800
  if (defined_p && r_type != R_MIPS_REL32)
5801
    *addendp += symbol;
5802
 
5803
  if (htab->is_vxworks)
5804
    /* VxWorks uses non-relative relocations for this.  */
5805
    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5806
  else
5807
    /* The relocation is always an REL32 relocation because we don't
5808
       know where the shared library will wind up at load-time.  */
5809
    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5810
                                   R_MIPS_REL32);
5811
 
5812
  /* For strict adherence to the ABI specification, we should
5813
     generate a R_MIPS_64 relocation record by itself before the
5814
     _REL32/_64 record as well, such that the addend is read in as
5815
     a 64-bit value (REL32 is a 32-bit relocation, after all).
5816
     However, since none of the existing ELF64 MIPS dynamic
5817
     loaders seems to care, we don't waste space with these
5818
     artificial relocations.  If this turns out to not be true,
5819
     mips_elf_allocate_dynamic_relocation() should be tweaked so
5820
     as to make room for a pair of dynamic relocations per
5821
     invocation if ABI_64_P, and here we should generate an
5822
     additional relocation record with R_MIPS_64 by itself for a
5823
     NULL symbol before this relocation record.  */
5824
  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5825
                                 ABI_64_P (output_bfd)
5826
                                 ? R_MIPS_64
5827
                                 : R_MIPS_NONE);
5828
  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5829
 
5830
  /* Adjust the output offset of the relocation to reference the
5831
     correct location in the output file.  */
5832
  outrel[0].r_offset += (input_section->output_section->vma
5833
                         + input_section->output_offset);
5834
  outrel[1].r_offset += (input_section->output_section->vma
5835
                         + input_section->output_offset);
5836
  outrel[2].r_offset += (input_section->output_section->vma
5837
                         + input_section->output_offset);
5838
 
5839
  /* Put the relocation back out.  We have to use the special
5840
     relocation outputter in the 64-bit case since the 64-bit
5841
     relocation format is non-standard.  */
5842
  if (ABI_64_P (output_bfd))
5843
    {
5844
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5845
        (output_bfd, &outrel[0],
5846
         (sreloc->contents
5847
          + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5848
    }
5849
  else if (htab->is_vxworks)
5850
    {
5851
      /* VxWorks uses RELA rather than REL dynamic relocations.  */
5852
      outrel[0].r_addend = *addendp;
5853
      bfd_elf32_swap_reloca_out
5854
        (output_bfd, &outrel[0],
5855
         (sreloc->contents
5856
          + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5857
    }
5858
  else
5859
    bfd_elf32_swap_reloc_out
5860
      (output_bfd, &outrel[0],
5861
       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5862
 
5863
  /* We've now added another relocation.  */
5864
  ++sreloc->reloc_count;
5865
 
5866
  /* Make sure the output section is writable.  The dynamic linker
5867
     will be writing to it.  */
5868
  elf_section_data (input_section->output_section)->this_hdr.sh_flags
5869
    |= SHF_WRITE;
5870
 
5871
  /* On IRIX5, make an entry of compact relocation info.  */
5872
  if (IRIX_COMPAT (output_bfd) == ict_irix5)
5873
    {
5874
      asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5875
      bfd_byte *cr;
5876
 
5877
      if (scpt)
5878
        {
5879
          Elf32_crinfo cptrel;
5880
 
5881
          mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5882
          cptrel.vaddr = (rel->r_offset
5883
                          + input_section->output_section->vma
5884
                          + input_section->output_offset);
5885
          if (r_type == R_MIPS_REL32)
5886
            mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5887
          else
5888
            mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5889
          mips_elf_set_cr_dist2to (cptrel, 0);
5890
          cptrel.konst = *addendp;
5891
 
5892
          cr = (scpt->contents
5893
                + sizeof (Elf32_External_compact_rel));
5894
          mips_elf_set_cr_relvaddr (cptrel, 0);
5895
          bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5896
                                     ((Elf32_External_crinfo *) cr
5897
                                      + scpt->reloc_count));
5898
          ++scpt->reloc_count;
5899
        }
5900
    }
5901
 
5902
  /* If we've written this relocation for a readonly section,
5903
     we need to set DF_TEXTREL again, so that we do not delete the
5904
     DT_TEXTREL tag.  */
5905
  if (MIPS_ELF_READONLY_SECTION (input_section))
5906
    info->flags |= DF_TEXTREL;
5907
 
5908
  return TRUE;
5909
}
5910
 
5911
/* Return the MACH for a MIPS e_flags value.  */
5912
 
5913
unsigned long
5914
_bfd_elf_mips_mach (flagword flags)
5915
{
5916
  switch (flags & EF_MIPS_MACH)
5917
    {
5918
    case E_MIPS_MACH_3900:
5919
      return bfd_mach_mips3900;
5920
 
5921
    case E_MIPS_MACH_4010:
5922
      return bfd_mach_mips4010;
5923
 
5924
    case E_MIPS_MACH_4100:
5925
      return bfd_mach_mips4100;
5926
 
5927
    case E_MIPS_MACH_4111:
5928
      return bfd_mach_mips4111;
5929
 
5930
    case E_MIPS_MACH_4120:
5931
      return bfd_mach_mips4120;
5932
 
5933
    case E_MIPS_MACH_4650:
5934
      return bfd_mach_mips4650;
5935
 
5936
    case E_MIPS_MACH_5400:
5937
      return bfd_mach_mips5400;
5938
 
5939
    case E_MIPS_MACH_5500:
5940
      return bfd_mach_mips5500;
5941
 
5942
    case E_MIPS_MACH_9000:
5943
      return bfd_mach_mips9000;
5944
 
5945
    case E_MIPS_MACH_SB1:
5946
      return bfd_mach_mips_sb1;
5947
 
5948
    case E_MIPS_MACH_LS2E:
5949
      return bfd_mach_mips_loongson_2e;
5950
 
5951
    case E_MIPS_MACH_LS2F:
5952
      return bfd_mach_mips_loongson_2f;
5953
 
5954
    case E_MIPS_MACH_LS3A:
5955
      return bfd_mach_mips_loongson_3a;
5956
 
5957
    case E_MIPS_MACH_OCTEON:
5958
      return bfd_mach_mips_octeon;
5959
 
5960
    case E_MIPS_MACH_XLR:
5961
      return bfd_mach_mips_xlr;
5962
 
5963
    default:
5964
      switch (flags & EF_MIPS_ARCH)
5965
        {
5966
        default:
5967
        case E_MIPS_ARCH_1:
5968
          return bfd_mach_mips3000;
5969
 
5970
        case E_MIPS_ARCH_2:
5971
          return bfd_mach_mips6000;
5972
 
5973
        case E_MIPS_ARCH_3:
5974
          return bfd_mach_mips4000;
5975
 
5976
        case E_MIPS_ARCH_4:
5977
          return bfd_mach_mips8000;
5978
 
5979
        case E_MIPS_ARCH_5:
5980
          return bfd_mach_mips5;
5981
 
5982
        case E_MIPS_ARCH_32:
5983
          return bfd_mach_mipsisa32;
5984
 
5985
        case E_MIPS_ARCH_64:
5986
          return bfd_mach_mipsisa64;
5987
 
5988
        case E_MIPS_ARCH_32R2:
5989
          return bfd_mach_mipsisa32r2;
5990
 
5991
        case E_MIPS_ARCH_64R2:
5992
          return bfd_mach_mipsisa64r2;
5993
        }
5994
    }
5995
 
5996
  return 0;
5997
}
5998
 
5999
/* Return printable name for ABI.  */
6000
 
6001
static INLINE char *
6002
elf_mips_abi_name (bfd *abfd)
6003
{
6004
  flagword flags;
6005
 
6006
  flags = elf_elfheader (abfd)->e_flags;
6007
  switch (flags & EF_MIPS_ABI)
6008
    {
6009
    case 0:
6010
      if (ABI_N32_P (abfd))
6011
        return "N32";
6012
      else if (ABI_64_P (abfd))
6013
        return "64";
6014
      else
6015
        return "none";
6016
    case E_MIPS_ABI_O32:
6017
      return "O32";
6018
    case E_MIPS_ABI_O64:
6019
      return "O64";
6020
    case E_MIPS_ABI_EABI32:
6021
      return "EABI32";
6022
    case E_MIPS_ABI_EABI64:
6023
      return "EABI64";
6024
    default:
6025
      return "unknown abi";
6026
    }
6027
}
6028
 
6029
/* MIPS ELF uses two common sections.  One is the usual one, and the
6030
   other is for small objects.  All the small objects are kept
6031
   together, and then referenced via the gp pointer, which yields
6032
   faster assembler code.  This is what we use for the small common
6033
   section.  This approach is copied from ecoff.c.  */
6034
static asection mips_elf_scom_section;
6035
static asymbol mips_elf_scom_symbol;
6036
static asymbol *mips_elf_scom_symbol_ptr;
6037
 
6038
/* MIPS ELF also uses an acommon section, which represents an
6039
   allocated common symbol which may be overridden by a
6040
   definition in a shared library.  */
6041
static asection mips_elf_acom_section;
6042
static asymbol mips_elf_acom_symbol;
6043
static asymbol *mips_elf_acom_symbol_ptr;
6044
 
6045
/* This is used for both the 32-bit and the 64-bit ABI.  */
6046
 
6047
void
6048
_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6049
{
6050
  elf_symbol_type *elfsym;
6051
 
6052
  /* Handle the special MIPS section numbers that a symbol may use.  */
6053
  elfsym = (elf_symbol_type *) asym;
6054
  switch (elfsym->internal_elf_sym.st_shndx)
6055
    {
6056
    case SHN_MIPS_ACOMMON:
6057
      /* This section is used in a dynamically linked executable file.
6058
         It is an allocated common section.  The dynamic linker can
6059
         either resolve these symbols to something in a shared
6060
         library, or it can just leave them here.  For our purposes,
6061
         we can consider these symbols to be in a new section.  */
6062
      if (mips_elf_acom_section.name == NULL)
6063
        {
6064
          /* Initialize the acommon section.  */
6065
          mips_elf_acom_section.name = ".acommon";
6066
          mips_elf_acom_section.flags = SEC_ALLOC;
6067
          mips_elf_acom_section.output_section = &mips_elf_acom_section;
6068
          mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6069
          mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6070
          mips_elf_acom_symbol.name = ".acommon";
6071
          mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6072
          mips_elf_acom_symbol.section = &mips_elf_acom_section;
6073
          mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6074
        }
6075
      asym->section = &mips_elf_acom_section;
6076
      break;
6077
 
6078
    case SHN_COMMON:
6079
      /* Common symbols less than the GP size are automatically
6080
         treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
6081
      if (asym->value > elf_gp_size (abfd)
6082
          || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6083
          || IRIX_COMPAT (abfd) == ict_irix6)
6084
        break;
6085
      /* Fall through.  */
6086
    case SHN_MIPS_SCOMMON:
6087
      if (mips_elf_scom_section.name == NULL)
6088
        {
6089
          /* Initialize the small common section.  */
6090
          mips_elf_scom_section.name = ".scommon";
6091
          mips_elf_scom_section.flags = SEC_IS_COMMON;
6092
          mips_elf_scom_section.output_section = &mips_elf_scom_section;
6093
          mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6094
          mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6095
          mips_elf_scom_symbol.name = ".scommon";
6096
          mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6097
          mips_elf_scom_symbol.section = &mips_elf_scom_section;
6098
          mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6099
        }
6100
      asym->section = &mips_elf_scom_section;
6101
      asym->value = elfsym->internal_elf_sym.st_size;
6102
      break;
6103
 
6104
    case SHN_MIPS_SUNDEFINED:
6105
      asym->section = bfd_und_section_ptr;
6106
      break;
6107
 
6108
    case SHN_MIPS_TEXT:
6109
      {
6110
        asection *section = bfd_get_section_by_name (abfd, ".text");
6111
 
6112
        BFD_ASSERT (SGI_COMPAT (abfd));
6113
        if (section != NULL)
6114
          {
6115
            asym->section = section;
6116
            /* MIPS_TEXT is a bit special, the address is not an offset
6117
               to the base of the .text section.  So substract the section
6118
               base address to make it an offset.  */
6119
            asym->value -= section->vma;
6120
          }
6121
      }
6122
      break;
6123
 
6124
    case SHN_MIPS_DATA:
6125
      {
6126
        asection *section = bfd_get_section_by_name (abfd, ".data");
6127
 
6128
        BFD_ASSERT (SGI_COMPAT (abfd));
6129
        if (section != NULL)
6130
          {
6131
            asym->section = section;
6132
            /* MIPS_DATA is a bit special, the address is not an offset
6133
               to the base of the .data section.  So substract the section
6134
               base address to make it an offset.  */
6135
            asym->value -= section->vma;
6136
          }
6137
      }
6138
      break;
6139
    }
6140
 
6141
  /* If this is an odd-valued function symbol, assume it's a MIPS16 one.  */
6142
  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6143
      && (asym->value & 1) != 0)
6144
    {
6145
      asym->value--;
6146
      elfsym->internal_elf_sym.st_other
6147
        = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6148
    }
6149
}
6150
 
6151
/* Implement elf_backend_eh_frame_address_size.  This differs from
6152
   the default in the way it handles EABI64.
6153
 
6154
   EABI64 was originally specified as an LP64 ABI, and that is what
6155
   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
6156
   historically accepted the combination of -mabi=eabi and -mlong32,
6157
   and this ILP32 variation has become semi-official over time.
6158
   Both forms use elf32 and have pointer-sized FDE addresses.
6159
 
6160
   If an EABI object was generated by GCC 4.0 or above, it will have
6161
   an empty .gcc_compiled_longXX section, where XX is the size of longs
6162
   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
6163
   have no special marking to distinguish them from LP64 objects.
6164
 
6165
   We don't want users of the official LP64 ABI to be punished for the
6166
   existence of the ILP32 variant, but at the same time, we don't want
6167
   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6168
   We therefore take the following approach:
6169
 
6170
      - If ABFD contains a .gcc_compiled_longXX section, use it to
6171
        determine the pointer size.
6172
 
6173
      - Otherwise check the type of the first relocation.  Assume that
6174
        the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6175
 
6176
      - Otherwise punt.
6177
 
6178
   The second check is enough to detect LP64 objects generated by pre-4.0
6179
   compilers because, in the kind of output generated by those compilers,
6180
   the first relocation will be associated with either a CIE personality
6181
   routine or an FDE start address.  Furthermore, the compilers never
6182
   used a special (non-pointer) encoding for this ABI.
6183
 
6184
   Checking the relocation type should also be safe because there is no
6185
   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
6186
   did so.  */
6187
 
6188
unsigned int
6189
_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6190
{
6191
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6192
    return 8;
6193
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6194
    {
6195
      bfd_boolean long32_p, long64_p;
6196
 
6197
      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6198
      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6199
      if (long32_p && long64_p)
6200
        return 0;
6201
      if (long32_p)
6202
        return 4;
6203
      if (long64_p)
6204
        return 8;
6205
 
6206
      if (sec->reloc_count > 0
6207
          && elf_section_data (sec)->relocs != NULL
6208
          && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6209
              == R_MIPS_64))
6210
        return 8;
6211
 
6212
      return 0;
6213
    }
6214
  return 4;
6215
}
6216
 
6217
/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6218
   relocations against two unnamed section symbols to resolve to the
6219
   same address.  For example, if we have code like:
6220
 
6221
        lw      $4,%got_disp(.data)($gp)
6222
        lw      $25,%got_disp(.text)($gp)
6223
        jalr    $25
6224
 
6225
   then the linker will resolve both relocations to .data and the program
6226
   will jump there rather than to .text.
6227
 
6228
   We can work around this problem by giving names to local section symbols.
6229
   This is also what the MIPSpro tools do.  */
6230
 
6231
bfd_boolean
6232
_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6233
{
6234
  return SGI_COMPAT (abfd);
6235
}
6236
 
6237
/* Work over a section just before writing it out.  This routine is
6238
   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
6239
   sections that need the SHF_MIPS_GPREL flag by name; there has to be
6240
   a better way.  */
6241
 
6242
bfd_boolean
6243
_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6244
{
6245
  if (hdr->sh_type == SHT_MIPS_REGINFO
6246
      && hdr->sh_size > 0)
6247
    {
6248
      bfd_byte buf[4];
6249
 
6250
      BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6251
      BFD_ASSERT (hdr->contents == NULL);
6252
 
6253
      if (bfd_seek (abfd,
6254
                    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6255
                    SEEK_SET) != 0)
6256
        return FALSE;
6257
      H_PUT_32 (abfd, elf_gp (abfd), buf);
6258
      if (bfd_bwrite (buf, 4, abfd) != 4)
6259
        return FALSE;
6260
    }
6261
 
6262
  if (hdr->sh_type == SHT_MIPS_OPTIONS
6263
      && hdr->bfd_section != NULL
6264
      && mips_elf_section_data (hdr->bfd_section) != NULL
6265
      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6266
    {
6267
      bfd_byte *contents, *l, *lend;
6268
 
6269
      /* We stored the section contents in the tdata field in the
6270
         set_section_contents routine.  We save the section contents
6271
         so that we don't have to read them again.
6272
         At this point we know that elf_gp is set, so we can look
6273
         through the section contents to see if there is an
6274
         ODK_REGINFO structure.  */
6275
 
6276
      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6277
      l = contents;
6278
      lend = contents + hdr->sh_size;
6279
      while (l + sizeof (Elf_External_Options) <= lend)
6280
        {
6281
          Elf_Internal_Options intopt;
6282
 
6283
          bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6284
                                        &intopt);
6285
          if (intopt.size < sizeof (Elf_External_Options))
6286
            {
6287
              (*_bfd_error_handler)
6288
                (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6289
                abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6290
              break;
6291
            }
6292
          if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6293
            {
6294
              bfd_byte buf[8];
6295
 
6296
              if (bfd_seek (abfd,
6297
                            (hdr->sh_offset
6298
                             + (l - contents)
6299
                             + sizeof (Elf_External_Options)
6300
                             + (sizeof (Elf64_External_RegInfo) - 8)),
6301
                             SEEK_SET) != 0)
6302
                return FALSE;
6303
              H_PUT_64 (abfd, elf_gp (abfd), buf);
6304
              if (bfd_bwrite (buf, 8, abfd) != 8)
6305
                return FALSE;
6306
            }
6307
          else if (intopt.kind == ODK_REGINFO)
6308
            {
6309
              bfd_byte buf[4];
6310
 
6311
              if (bfd_seek (abfd,
6312
                            (hdr->sh_offset
6313
                             + (l - contents)
6314
                             + sizeof (Elf_External_Options)
6315
                             + (sizeof (Elf32_External_RegInfo) - 4)),
6316
                            SEEK_SET) != 0)
6317
                return FALSE;
6318
              H_PUT_32 (abfd, elf_gp (abfd), buf);
6319
              if (bfd_bwrite (buf, 4, abfd) != 4)
6320
                return FALSE;
6321
            }
6322
          l += intopt.size;
6323
        }
6324
    }
6325
 
6326
  if (hdr->bfd_section != NULL)
6327
    {
6328
      const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6329
 
6330
      /* .sbss is not handled specially here because the GNU/Linux
6331
         prelinker can convert .sbss from NOBITS to PROGBITS and
6332
         changing it back to NOBITS breaks the binary.  The entry in
6333
         _bfd_mips_elf_special_sections will ensure the correct flags
6334
         are set on .sbss if BFD creates it without reading it from an
6335
         input file, and without special handling here the flags set
6336
         on it in an input file will be followed.  */
6337
      if (strcmp (name, ".sdata") == 0
6338
          || strcmp (name, ".lit8") == 0
6339
          || strcmp (name, ".lit4") == 0)
6340
        {
6341
          hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6342
          hdr->sh_type = SHT_PROGBITS;
6343
        }
6344
      else if (strcmp (name, ".srdata") == 0)
6345
        {
6346
          hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6347
          hdr->sh_type = SHT_PROGBITS;
6348
        }
6349
      else if (strcmp (name, ".compact_rel") == 0)
6350
        {
6351
          hdr->sh_flags = 0;
6352
          hdr->sh_type = SHT_PROGBITS;
6353
        }
6354
      else if (strcmp (name, ".rtproc") == 0)
6355
        {
6356
          if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6357
            {
6358
              unsigned int adjust;
6359
 
6360
              adjust = hdr->sh_size % hdr->sh_addralign;
6361
              if (adjust != 0)
6362
                hdr->sh_size += hdr->sh_addralign - adjust;
6363
            }
6364
        }
6365
    }
6366
 
6367
  return TRUE;
6368
}
6369
 
6370
/* Handle a MIPS specific section when reading an object file.  This
6371
   is called when elfcode.h finds a section with an unknown type.
6372
   This routine supports both the 32-bit and 64-bit ELF ABI.
6373
 
6374
   FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6375
   how to.  */
6376
 
6377
bfd_boolean
6378
_bfd_mips_elf_section_from_shdr (bfd *abfd,
6379
                                 Elf_Internal_Shdr *hdr,
6380
                                 const char *name,
6381
                                 int shindex)
6382
{
6383
  flagword flags = 0;
6384
 
6385
  /* There ought to be a place to keep ELF backend specific flags, but
6386
     at the moment there isn't one.  We just keep track of the
6387
     sections by their name, instead.  Fortunately, the ABI gives
6388
     suggested names for all the MIPS specific sections, so we will
6389
     probably get away with this.  */
6390
  switch (hdr->sh_type)
6391
    {
6392
    case SHT_MIPS_LIBLIST:
6393
      if (strcmp (name, ".liblist") != 0)
6394
        return FALSE;
6395
      break;
6396
    case SHT_MIPS_MSYM:
6397
      if (strcmp (name, ".msym") != 0)
6398
        return FALSE;
6399
      break;
6400
    case SHT_MIPS_CONFLICT:
6401
      if (strcmp (name, ".conflict") != 0)
6402
        return FALSE;
6403
      break;
6404
    case SHT_MIPS_GPTAB:
6405
      if (! CONST_STRNEQ (name, ".gptab."))
6406
        return FALSE;
6407
      break;
6408
    case SHT_MIPS_UCODE:
6409
      if (strcmp (name, ".ucode") != 0)
6410
        return FALSE;
6411
      break;
6412
    case SHT_MIPS_DEBUG:
6413
      if (strcmp (name, ".mdebug") != 0)
6414
        return FALSE;
6415
      flags = SEC_DEBUGGING;
6416
      break;
6417
    case SHT_MIPS_REGINFO:
6418
      if (strcmp (name, ".reginfo") != 0
6419
          || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6420
        return FALSE;
6421
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6422
      break;
6423
    case SHT_MIPS_IFACE:
6424
      if (strcmp (name, ".MIPS.interfaces") != 0)
6425
        return FALSE;
6426
      break;
6427
    case SHT_MIPS_CONTENT:
6428
      if (! CONST_STRNEQ (name, ".MIPS.content"))
6429
        return FALSE;
6430
      break;
6431
    case SHT_MIPS_OPTIONS:
6432
      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6433
        return FALSE;
6434
      break;
6435
    case SHT_MIPS_DWARF:
6436
      if (! CONST_STRNEQ (name, ".debug_")
6437
          && ! CONST_STRNEQ (name, ".zdebug_"))
6438
        return FALSE;
6439
      break;
6440
    case SHT_MIPS_SYMBOL_LIB:
6441
      if (strcmp (name, ".MIPS.symlib") != 0)
6442
        return FALSE;
6443
      break;
6444
    case SHT_MIPS_EVENTS:
6445
      if (! CONST_STRNEQ (name, ".MIPS.events")
6446
          && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6447
        return FALSE;
6448
      break;
6449
    default:
6450
      break;
6451
    }
6452
 
6453
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6454
    return FALSE;
6455
 
6456
  if (flags)
6457
    {
6458
      if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6459
                                   (bfd_get_section_flags (abfd,
6460
                                                           hdr->bfd_section)
6461
                                    | flags)))
6462
        return FALSE;
6463
    }
6464
 
6465
  /* FIXME: We should record sh_info for a .gptab section.  */
6466
 
6467
  /* For a .reginfo section, set the gp value in the tdata information
6468
     from the contents of this section.  We need the gp value while
6469
     processing relocs, so we just get it now.  The .reginfo section
6470
     is not used in the 64-bit MIPS ELF ABI.  */
6471
  if (hdr->sh_type == SHT_MIPS_REGINFO)
6472
    {
6473
      Elf32_External_RegInfo ext;
6474
      Elf32_RegInfo s;
6475
 
6476
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6477
                                      &ext, 0, sizeof ext))
6478
        return FALSE;
6479
      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6480
      elf_gp (abfd) = s.ri_gp_value;
6481
    }
6482
 
6483
  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6484
     set the gp value based on what we find.  We may see both
6485
     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6486
     they should agree.  */
6487
  if (hdr->sh_type == SHT_MIPS_OPTIONS)
6488
    {
6489
      bfd_byte *contents, *l, *lend;
6490
 
6491
      contents = bfd_malloc (hdr->sh_size);
6492
      if (contents == NULL)
6493
        return FALSE;
6494
      if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6495
                                      0, hdr->sh_size))
6496
        {
6497
          free (contents);
6498
          return FALSE;
6499
        }
6500
      l = contents;
6501
      lend = contents + hdr->sh_size;
6502
      while (l + sizeof (Elf_External_Options) <= lend)
6503
        {
6504
          Elf_Internal_Options intopt;
6505
 
6506
          bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6507
                                        &intopt);
6508
          if (intopt.size < sizeof (Elf_External_Options))
6509
            {
6510
              (*_bfd_error_handler)
6511
                (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6512
                abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6513
              break;
6514
            }
6515
          if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6516
            {
6517
              Elf64_Internal_RegInfo intreg;
6518
 
6519
              bfd_mips_elf64_swap_reginfo_in
6520
                (abfd,
6521
                 ((Elf64_External_RegInfo *)
6522
                  (l + sizeof (Elf_External_Options))),
6523
                 &intreg);
6524
              elf_gp (abfd) = intreg.ri_gp_value;
6525
            }
6526
          else if (intopt.kind == ODK_REGINFO)
6527
            {
6528
              Elf32_RegInfo intreg;
6529
 
6530
              bfd_mips_elf32_swap_reginfo_in
6531
                (abfd,
6532
                 ((Elf32_External_RegInfo *)
6533
                  (l + sizeof (Elf_External_Options))),
6534
                 &intreg);
6535
              elf_gp (abfd) = intreg.ri_gp_value;
6536
            }
6537
          l += intopt.size;
6538
        }
6539
      free (contents);
6540
    }
6541
 
6542
  return TRUE;
6543
}
6544
 
6545
/* Set the correct type for a MIPS ELF section.  We do this by the
6546
   section name, which is a hack, but ought to work.  This routine is
6547
   used by both the 32-bit and the 64-bit ABI.  */
6548
 
6549
bfd_boolean
6550
_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6551
{
6552
  const char *name = bfd_get_section_name (abfd, sec);
6553
 
6554
  if (strcmp (name, ".liblist") == 0)
6555
    {
6556
      hdr->sh_type = SHT_MIPS_LIBLIST;
6557
      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6558
      /* The sh_link field is set in final_write_processing.  */
6559
    }
6560
  else if (strcmp (name, ".conflict") == 0)
6561
    hdr->sh_type = SHT_MIPS_CONFLICT;
6562
  else if (CONST_STRNEQ (name, ".gptab."))
6563
    {
6564
      hdr->sh_type = SHT_MIPS_GPTAB;
6565
      hdr->sh_entsize = sizeof (Elf32_External_gptab);
6566
      /* The sh_info field is set in final_write_processing.  */
6567
    }
6568
  else if (strcmp (name, ".ucode") == 0)
6569
    hdr->sh_type = SHT_MIPS_UCODE;
6570
  else if (strcmp (name, ".mdebug") == 0)
6571
    {
6572
      hdr->sh_type = SHT_MIPS_DEBUG;
6573
      /* In a shared object on IRIX 5.3, the .mdebug section has an
6574
         entsize of 0.  FIXME: Does this matter?  */
6575
      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6576
        hdr->sh_entsize = 0;
6577
      else
6578
        hdr->sh_entsize = 1;
6579
    }
6580
  else if (strcmp (name, ".reginfo") == 0)
6581
    {
6582
      hdr->sh_type = SHT_MIPS_REGINFO;
6583
      /* In a shared object on IRIX 5.3, the .reginfo section has an
6584
         entsize of 0x18.  FIXME: Does this matter?  */
6585
      if (SGI_COMPAT (abfd))
6586
        {
6587
          if ((abfd->flags & DYNAMIC) != 0)
6588
            hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6589
          else
6590
            hdr->sh_entsize = 1;
6591
        }
6592
      else
6593
        hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6594
    }
6595
  else if (SGI_COMPAT (abfd)
6596
           && (strcmp (name, ".hash") == 0
6597
               || strcmp (name, ".dynamic") == 0
6598
               || strcmp (name, ".dynstr") == 0))
6599
    {
6600
      if (SGI_COMPAT (abfd))
6601
        hdr->sh_entsize = 0;
6602
#if 0
6603
      /* This isn't how the IRIX6 linker behaves.  */
6604
      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6605
#endif
6606
    }
6607
  else if (strcmp (name, ".got") == 0
6608
           || strcmp (name, ".srdata") == 0
6609
           || strcmp (name, ".sdata") == 0
6610
           || strcmp (name, ".sbss") == 0
6611
           || strcmp (name, ".lit4") == 0
6612
           || strcmp (name, ".lit8") == 0)
6613
    hdr->sh_flags |= SHF_MIPS_GPREL;
6614
  else if (strcmp (name, ".MIPS.interfaces") == 0)
6615
    {
6616
      hdr->sh_type = SHT_MIPS_IFACE;
6617
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6618
    }
6619
  else if (CONST_STRNEQ (name, ".MIPS.content"))
6620
    {
6621
      hdr->sh_type = SHT_MIPS_CONTENT;
6622
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6623
      /* The sh_info field is set in final_write_processing.  */
6624
    }
6625
  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6626
    {
6627
      hdr->sh_type = SHT_MIPS_OPTIONS;
6628
      hdr->sh_entsize = 1;
6629
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6630
    }
6631
  else if (CONST_STRNEQ (name, ".debug_")
6632
           || CONST_STRNEQ (name, ".zdebug_"))
6633
    {
6634
      hdr->sh_type = SHT_MIPS_DWARF;
6635
 
6636
      /* Irix facilities such as libexc expect a single .debug_frame
6637
         per executable, the system ones have NOSTRIP set and the linker
6638
         doesn't merge sections with different flags so ...  */
6639
      if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6640
        hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6641
    }
6642
  else if (strcmp (name, ".MIPS.symlib") == 0)
6643
    {
6644
      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6645
      /* The sh_link and sh_info fields are set in
6646
         final_write_processing.  */
6647
    }
6648
  else if (CONST_STRNEQ (name, ".MIPS.events")
6649
           || CONST_STRNEQ (name, ".MIPS.post_rel"))
6650
    {
6651
      hdr->sh_type = SHT_MIPS_EVENTS;
6652
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6653
      /* The sh_link field is set in final_write_processing.  */
6654
    }
6655
  else if (strcmp (name, ".msym") == 0)
6656
    {
6657
      hdr->sh_type = SHT_MIPS_MSYM;
6658
      hdr->sh_flags |= SHF_ALLOC;
6659
      hdr->sh_entsize = 8;
6660
    }
6661
 
6662
  /* The generic elf_fake_sections will set up REL_HDR using the default
6663
   kind of relocations.  We used to set up a second header for the
6664
   non-default kind of relocations here, but only NewABI would use
6665
   these, and the IRIX ld doesn't like resulting empty RELA sections.
6666
   Thus we create those header only on demand now.  */
6667
 
6668
  return TRUE;
6669
}
6670
 
6671
/* Given a BFD section, try to locate the corresponding ELF section
6672
   index.  This is used by both the 32-bit and the 64-bit ABI.
6673
   Actually, it's not clear to me that the 64-bit ABI supports these,
6674
   but for non-PIC objects we will certainly want support for at least
6675
   the .scommon section.  */
6676
 
6677
bfd_boolean
6678
_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6679
                                        asection *sec, int *retval)
6680
{
6681
  if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6682
    {
6683
      *retval = SHN_MIPS_SCOMMON;
6684
      return TRUE;
6685
    }
6686
  if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6687
    {
6688
      *retval = SHN_MIPS_ACOMMON;
6689
      return TRUE;
6690
    }
6691
  return FALSE;
6692
}
6693
 
6694
/* Hook called by the linker routine which adds symbols from an object
6695
   file.  We must handle the special MIPS section numbers here.  */
6696
 
6697
bfd_boolean
6698
_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6699
                               Elf_Internal_Sym *sym, const char **namep,
6700
                               flagword *flagsp ATTRIBUTE_UNUSED,
6701
                               asection **secp, bfd_vma *valp)
6702
{
6703
  if (SGI_COMPAT (abfd)
6704
      && (abfd->flags & DYNAMIC) != 0
6705
      && strcmp (*namep, "_rld_new_interface") == 0)
6706
    {
6707
      /* Skip IRIX5 rld entry name.  */
6708
      *namep = NULL;
6709
      return TRUE;
6710
    }
6711
 
6712
  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6713
     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
6714
     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
6715
     a magic symbol resolved by the linker, we ignore this bogus definition
6716
     of _gp_disp.  New ABI objects do not suffer from this problem so this
6717
     is not done for them. */
6718
  if (!NEWABI_P(abfd)
6719
      && (sym->st_shndx == SHN_ABS)
6720
      && (strcmp (*namep, "_gp_disp") == 0))
6721
    {
6722
      *namep = NULL;
6723
      return TRUE;
6724
    }
6725
 
6726
  switch (sym->st_shndx)
6727
    {
6728
    case SHN_COMMON:
6729
      /* Common symbols less than the GP size are automatically
6730
         treated as SHN_MIPS_SCOMMON symbols.  */
6731
      if (sym->st_size > elf_gp_size (abfd)
6732
          || ELF_ST_TYPE (sym->st_info) == STT_TLS
6733
          || IRIX_COMPAT (abfd) == ict_irix6)
6734
        break;
6735
      /* Fall through.  */
6736
    case SHN_MIPS_SCOMMON:
6737
      *secp = bfd_make_section_old_way (abfd, ".scommon");
6738
      (*secp)->flags |= SEC_IS_COMMON;
6739
      *valp = sym->st_size;
6740
      break;
6741
 
6742
    case SHN_MIPS_TEXT:
6743
      /* This section is used in a shared object.  */
6744
      if (elf_tdata (abfd)->elf_text_section == NULL)
6745
        {
6746
          asymbol *elf_text_symbol;
6747
          asection *elf_text_section;
6748
          bfd_size_type amt = sizeof (asection);
6749
 
6750
          elf_text_section = bfd_zalloc (abfd, amt);
6751
          if (elf_text_section == NULL)
6752
            return FALSE;
6753
 
6754
          amt = sizeof (asymbol);
6755
          elf_text_symbol = bfd_zalloc (abfd, amt);
6756
          if (elf_text_symbol == NULL)
6757
            return FALSE;
6758
 
6759
          /* Initialize the section.  */
6760
 
6761
          elf_tdata (abfd)->elf_text_section = elf_text_section;
6762
          elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6763
 
6764
          elf_text_section->symbol = elf_text_symbol;
6765
          elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6766
 
6767
          elf_text_section->name = ".text";
6768
          elf_text_section->flags = SEC_NO_FLAGS;
6769
          elf_text_section->output_section = NULL;
6770
          elf_text_section->owner = abfd;
6771
          elf_text_symbol->name = ".text";
6772
          elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6773
          elf_text_symbol->section = elf_text_section;
6774
        }
6775
      /* This code used to do *secp = bfd_und_section_ptr if
6776
         info->shared.  I don't know why, and that doesn't make sense,
6777
         so I took it out.  */
6778
      *secp = elf_tdata (abfd)->elf_text_section;
6779
      break;
6780
 
6781
    case SHN_MIPS_ACOMMON:
6782
      /* Fall through. XXX Can we treat this as allocated data?  */
6783
    case SHN_MIPS_DATA:
6784
      /* This section is used in a shared object.  */
6785
      if (elf_tdata (abfd)->elf_data_section == NULL)
6786
        {
6787
          asymbol *elf_data_symbol;
6788
          asection *elf_data_section;
6789
          bfd_size_type amt = sizeof (asection);
6790
 
6791
          elf_data_section = bfd_zalloc (abfd, amt);
6792
          if (elf_data_section == NULL)
6793
            return FALSE;
6794
 
6795
          amt = sizeof (asymbol);
6796
          elf_data_symbol = bfd_zalloc (abfd, amt);
6797
          if (elf_data_symbol == NULL)
6798
            return FALSE;
6799
 
6800
          /* Initialize the section.  */
6801
 
6802
          elf_tdata (abfd)->elf_data_section = elf_data_section;
6803
          elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6804
 
6805
          elf_data_section->symbol = elf_data_symbol;
6806
          elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6807
 
6808
          elf_data_section->name = ".data";
6809
          elf_data_section->flags = SEC_NO_FLAGS;
6810
          elf_data_section->output_section = NULL;
6811
          elf_data_section->owner = abfd;
6812
          elf_data_symbol->name = ".data";
6813
          elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6814
          elf_data_symbol->section = elf_data_section;
6815
        }
6816
      /* This code used to do *secp = bfd_und_section_ptr if
6817
         info->shared.  I don't know why, and that doesn't make sense,
6818
         so I took it out.  */
6819
      *secp = elf_tdata (abfd)->elf_data_section;
6820
      break;
6821
 
6822
    case SHN_MIPS_SUNDEFINED:
6823
      *secp = bfd_und_section_ptr;
6824
      break;
6825
    }
6826
 
6827
  if (SGI_COMPAT (abfd)
6828
      && ! info->shared
6829
      && info->output_bfd->xvec == abfd->xvec
6830
      && strcmp (*namep, "__rld_obj_head") == 0)
6831
    {
6832
      struct elf_link_hash_entry *h;
6833
      struct bfd_link_hash_entry *bh;
6834
 
6835
      /* Mark __rld_obj_head as dynamic.  */
6836
      bh = NULL;
6837
      if (! (_bfd_generic_link_add_one_symbol
6838
             (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6839
              get_elf_backend_data (abfd)->collect, &bh)))
6840
        return FALSE;
6841
 
6842
      h = (struct elf_link_hash_entry *) bh;
6843
      h->non_elf = 0;
6844
      h->def_regular = 1;
6845
      h->type = STT_OBJECT;
6846
 
6847
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6848
        return FALSE;
6849
 
6850
      mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6851
    }
6852
 
6853
  /* If this is a mips16 text symbol, add 1 to the value to make it
6854
     odd.  This will cause something like .word SYM to come up with
6855
     the right value when it is loaded into the PC.  */
6856
  if (ELF_ST_IS_MIPS16 (sym->st_other))
6857
    ++*valp;
6858
 
6859
  return TRUE;
6860
}
6861
 
6862
/* This hook function is called before the linker writes out a global
6863
   symbol.  We mark symbols as small common if appropriate.  This is
6864
   also where we undo the increment of the value for a mips16 symbol.  */
6865
 
6866
int
6867
_bfd_mips_elf_link_output_symbol_hook
6868
  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6869
   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6870
   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6871
{
6872
  /* If we see a common symbol, which implies a relocatable link, then
6873
     if a symbol was small common in an input file, mark it as small
6874
     common in the output file.  */
6875
  if (sym->st_shndx == SHN_COMMON
6876
      && strcmp (input_sec->name, ".scommon") == 0)
6877
    sym->st_shndx = SHN_MIPS_SCOMMON;
6878
 
6879
  if (ELF_ST_IS_MIPS16 (sym->st_other))
6880
    sym->st_value &= ~1;
6881
 
6882
  return 1;
6883
}
6884
 
6885
/* Functions for the dynamic linker.  */
6886
 
6887
/* Create dynamic sections when linking against a dynamic object.  */
6888
 
6889
bfd_boolean
6890
_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6891
{
6892
  struct elf_link_hash_entry *h;
6893
  struct bfd_link_hash_entry *bh;
6894
  flagword flags;
6895
  register asection *s;
6896
  const char * const *namep;
6897
  struct mips_elf_link_hash_table *htab;
6898
 
6899
  htab = mips_elf_hash_table (info);
6900
  BFD_ASSERT (htab != NULL);
6901
 
6902
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6903
           | SEC_LINKER_CREATED | SEC_READONLY);
6904
 
6905
  /* The psABI requires a read-only .dynamic section, but the VxWorks
6906
     EABI doesn't.  */
6907
  if (!htab->is_vxworks)
6908
    {
6909
      s = bfd_get_section_by_name (abfd, ".dynamic");
6910
      if (s != NULL)
6911
        {
6912
          if (! bfd_set_section_flags (abfd, s, flags))
6913
            return FALSE;
6914
        }
6915
    }
6916
 
6917
  /* We need to create .got section.  */
6918
  if (!mips_elf_create_got_section (abfd, info))
6919
    return FALSE;
6920
 
6921
  if (! mips_elf_rel_dyn_section (info, TRUE))
6922
    return FALSE;
6923
 
6924
  /* Create .stub section.  */
6925
  s = bfd_make_section_with_flags (abfd,
6926
                                   MIPS_ELF_STUB_SECTION_NAME (abfd),
6927
                                   flags | SEC_CODE);
6928
  if (s == NULL
6929
      || ! bfd_set_section_alignment (abfd, s,
6930
                                      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6931
    return FALSE;
6932
  htab->sstubs = s;
6933
 
6934
  if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6935
      && !info->shared
6936
      && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6937
    {
6938
      s = bfd_make_section_with_flags (abfd, ".rld_map",
6939
                                       flags &~ (flagword) SEC_READONLY);
6940
      if (s == NULL
6941
          || ! bfd_set_section_alignment (abfd, s,
6942
                                          MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6943
        return FALSE;
6944
    }
6945
 
6946
  /* On IRIX5, we adjust add some additional symbols and change the
6947
     alignments of several sections.  There is no ABI documentation
6948
     indicating that this is necessary on IRIX6, nor any evidence that
6949
     the linker takes such action.  */
6950
  if (IRIX_COMPAT (abfd) == ict_irix5)
6951
    {
6952
      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6953
        {
6954
          bh = NULL;
6955
          if (! (_bfd_generic_link_add_one_symbol
6956
                 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6957
                  NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6958
            return FALSE;
6959
 
6960
          h = (struct elf_link_hash_entry *) bh;
6961
          h->non_elf = 0;
6962
          h->def_regular = 1;
6963
          h->type = STT_SECTION;
6964
 
6965
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6966
            return FALSE;
6967
        }
6968
 
6969
      /* We need to create a .compact_rel section.  */
6970
      if (SGI_COMPAT (abfd))
6971
        {
6972
          if (!mips_elf_create_compact_rel_section (abfd, info))
6973
            return FALSE;
6974
        }
6975
 
6976
      /* Change alignments of some sections.  */
6977
      s = bfd_get_section_by_name (abfd, ".hash");
6978
      if (s != NULL)
6979
        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6980
      s = bfd_get_section_by_name (abfd, ".dynsym");
6981
      if (s != NULL)
6982
        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6983
      s = bfd_get_section_by_name (abfd, ".dynstr");
6984
      if (s != NULL)
6985
        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6986
      s = bfd_get_section_by_name (abfd, ".reginfo");
6987
      if (s != NULL)
6988
        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6989
      s = bfd_get_section_by_name (abfd, ".dynamic");
6990
      if (s != NULL)
6991
        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6992
    }
6993
 
6994
  if (!info->shared)
6995
    {
6996
      const char *name;
6997
 
6998
      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6999
      bh = NULL;
7000
      if (!(_bfd_generic_link_add_one_symbol
7001
            (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7002
             NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7003
        return FALSE;
7004
 
7005
      h = (struct elf_link_hash_entry *) bh;
7006
      h->non_elf = 0;
7007
      h->def_regular = 1;
7008
      h->type = STT_SECTION;
7009
 
7010
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
7011
        return FALSE;
7012
 
7013
      if (! mips_elf_hash_table (info)->use_rld_obj_head)
7014
        {
7015
          /* __rld_map is a four byte word located in the .data section
7016
             and is filled in by the rtld to contain a pointer to
7017
             the _r_debug structure. Its symbol value will be set in
7018
             _bfd_mips_elf_finish_dynamic_symbol.  */
7019
          s = bfd_get_section_by_name (abfd, ".rld_map");
7020
          BFD_ASSERT (s != NULL);
7021
 
7022
          name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7023
          bh = NULL;
7024
          if (!(_bfd_generic_link_add_one_symbol
7025
                (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7026
                 get_elf_backend_data (abfd)->collect, &bh)))
7027
            return FALSE;
7028
 
7029
          h = (struct elf_link_hash_entry *) bh;
7030
          h->non_elf = 0;
7031
          h->def_regular = 1;
7032
          h->type = STT_OBJECT;
7033
 
7034
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
7035
            return FALSE;
7036
        }
7037
    }
7038
 
7039
  /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7040
     Also create the _PROCEDURE_LINKAGE_TABLE symbol.  */
7041
  if (!_bfd_elf_create_dynamic_sections (abfd, info))
7042
    return FALSE;
7043
 
7044
  /* Cache the sections created above.  */
7045
  htab->splt = bfd_get_section_by_name (abfd, ".plt");
7046
  htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
7047
  if (htab->is_vxworks)
7048
    {
7049
      htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7050
      htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
7051
    }
7052
  else
7053
    htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7054
  if (!htab->sdynbss
7055
      || (htab->is_vxworks && !htab->srelbss && !info->shared)
7056
      || !htab->srelplt
7057
      || !htab->splt)
7058
    abort ();
7059
 
7060
  if (htab->is_vxworks)
7061
    {
7062
      /* Do the usual VxWorks handling.  */
7063
      if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7064
        return FALSE;
7065
 
7066
      /* Work out the PLT sizes.  */
7067
      if (info->shared)
7068
        {
7069
          htab->plt_header_size
7070
            = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7071
          htab->plt_entry_size
7072
            = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7073
        }
7074
      else
7075
        {
7076
          htab->plt_header_size
7077
            = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7078
          htab->plt_entry_size
7079
            = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7080
        }
7081
    }
7082
  else if (!info->shared)
7083
    {
7084
      /* All variants of the plt0 entry are the same size.  */
7085
      htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7086
      htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7087
    }
7088
 
7089
  return TRUE;
7090
}
7091
 
7092
/* Return true if relocation REL against section SEC is a REL rather than
7093
   RELA relocation.  RELOCS is the first relocation in the section and
7094
   ABFD is the bfd that contains SEC.  */
7095
 
7096
static bfd_boolean
7097
mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7098
                           const Elf_Internal_Rela *relocs,
7099
                           const Elf_Internal_Rela *rel)
7100
{
7101
  Elf_Internal_Shdr *rel_hdr;
7102
  const struct elf_backend_data *bed;
7103
 
7104
  /* To determine which flavor of relocation this is, we depend on the
7105
     fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
7106
  rel_hdr = elf_section_data (sec)->rel.hdr;
7107
  if (rel_hdr == NULL)
7108
    return FALSE;
7109
  bed = get_elf_backend_data (abfd);
7110
  return ((size_t) (rel - relocs)
7111
          < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7112
}
7113
 
7114
/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7115
   HOWTO is the relocation's howto and CONTENTS points to the contents
7116
   of the section that REL is against.  */
7117
 
7118
static bfd_vma
7119
mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7120
                          reloc_howto_type *howto, bfd_byte *contents)
7121
{
7122
  bfd_byte *location;
7123
  unsigned int r_type;
7124
  bfd_vma addend;
7125
 
7126
  r_type = ELF_R_TYPE (abfd, rel->r_info);
7127
  location = contents + rel->r_offset;
7128
 
7129
  /* Get the addend, which is stored in the input file.  */
7130
  _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7131
  addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7132
  _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7133
 
7134
  return addend & howto->src_mask;
7135
}
7136
 
7137
/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7138
   and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
7139
   and update *ADDEND with the final addend.  Return true on success
7140
   or false if the LO16 could not be found.  RELEND is the exclusive
7141
   upper bound on the relocations for REL's section.  */
7142
 
7143
static bfd_boolean
7144
mips_elf_add_lo16_rel_addend (bfd *abfd,
7145
                              const Elf_Internal_Rela *rel,
7146
                              const Elf_Internal_Rela *relend,
7147
                              bfd_byte *contents, bfd_vma *addend)
7148
{
7149
  unsigned int r_type, lo16_type;
7150
  const Elf_Internal_Rela *lo16_relocation;
7151
  reloc_howto_type *lo16_howto;
7152
  bfd_vma l;
7153
 
7154
  r_type = ELF_R_TYPE (abfd, rel->r_info);
7155
  if (mips16_reloc_p (r_type))
7156
    lo16_type = R_MIPS16_LO16;
7157
  else
7158
    lo16_type = R_MIPS_LO16;
7159
 
7160
  /* The combined value is the sum of the HI16 addend, left-shifted by
7161
     sixteen bits, and the LO16 addend, sign extended.  (Usually, the
7162
     code does a `lui' of the HI16 value, and then an `addiu' of the
7163
     LO16 value.)
7164
 
7165
     Scan ahead to find a matching LO16 relocation.
7166
 
7167
     According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7168
     be immediately following.  However, for the IRIX6 ABI, the next
7169
     relocation may be a composed relocation consisting of several
7170
     relocations for the same address.  In that case, the R_MIPS_LO16
7171
     relocation may occur as one of these.  We permit a similar
7172
     extension in general, as that is useful for GCC.
7173
 
7174
     In some cases GCC dead code elimination removes the LO16 but keeps
7175
     the corresponding HI16.  This is strictly speaking a violation of
7176
     the ABI but not immediately harmful.  */
7177
  lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7178
  if (lo16_relocation == NULL)
7179
    return FALSE;
7180
 
7181
  /* Obtain the addend kept there.  */
7182
  lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7183
  l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7184
 
7185
  l <<= lo16_howto->rightshift;
7186
  l = _bfd_mips_elf_sign_extend (l, 16);
7187
 
7188
  *addend <<= 16;
7189
  *addend += l;
7190
  return TRUE;
7191
}
7192
 
7193
/* Try to read the contents of section SEC in bfd ABFD.  Return true and
7194
   store the contents in *CONTENTS on success.  Assume that *CONTENTS
7195
   already holds the contents if it is nonull on entry.  */
7196
 
7197
static bfd_boolean
7198
mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7199
{
7200
  if (*contents)
7201
    return TRUE;
7202
 
7203
  /* Get cached copy if it exists.  */
7204
  if (elf_section_data (sec)->this_hdr.contents != NULL)
7205
    {
7206
      *contents = elf_section_data (sec)->this_hdr.contents;
7207
      return TRUE;
7208
    }
7209
 
7210
  return bfd_malloc_and_get_section (abfd, sec, contents);
7211
}
7212
 
7213
/* Look through the relocs for a section during the first phase, and
7214
   allocate space in the global offset table.  */
7215
 
7216
bfd_boolean
7217
_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7218
                            asection *sec, const Elf_Internal_Rela *relocs)
7219
{
7220
  const char *name;
7221
  bfd *dynobj;
7222
  Elf_Internal_Shdr *symtab_hdr;
7223
  struct elf_link_hash_entry **sym_hashes;
7224
  size_t extsymoff;
7225
  const Elf_Internal_Rela *rel;
7226
  const Elf_Internal_Rela *rel_end;
7227
  asection *sreloc;
7228
  const struct elf_backend_data *bed;
7229
  struct mips_elf_link_hash_table *htab;
7230
  bfd_byte *contents;
7231
  bfd_vma addend;
7232
  reloc_howto_type *howto;
7233
 
7234
  if (info->relocatable)
7235
    return TRUE;
7236
 
7237
  htab = mips_elf_hash_table (info);
7238
  BFD_ASSERT (htab != NULL);
7239
 
7240
  dynobj = elf_hash_table (info)->dynobj;
7241
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7242
  sym_hashes = elf_sym_hashes (abfd);
7243
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7244
 
7245
  bed = get_elf_backend_data (abfd);
7246
  rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7247
 
7248
  /* Check for the mips16 stub sections.  */
7249
 
7250
  name = bfd_get_section_name (abfd, sec);
7251
  if (FN_STUB_P (name))
7252
    {
7253
      unsigned long r_symndx;
7254
 
7255
      /* Look at the relocation information to figure out which symbol
7256
         this is for.  */
7257
 
7258
      r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7259
      if (r_symndx == 0)
7260
        {
7261
          (*_bfd_error_handler)
7262
            (_("%B: Warning: cannot determine the target function for"
7263
               " stub section `%s'"),
7264
             abfd, name);
7265
          bfd_set_error (bfd_error_bad_value);
7266
          return FALSE;
7267
        }
7268
 
7269
      if (r_symndx < extsymoff
7270
          || sym_hashes[r_symndx - extsymoff] == NULL)
7271
        {
7272
          asection *o;
7273
 
7274
          /* This stub is for a local symbol.  This stub will only be
7275
             needed if there is some relocation in this BFD, other
7276
             than a 16 bit function call, which refers to this symbol.  */
7277
          for (o = abfd->sections; o != NULL; o = o->next)
7278
            {
7279
              Elf_Internal_Rela *sec_relocs;
7280
              const Elf_Internal_Rela *r, *rend;
7281
 
7282
              /* We can ignore stub sections when looking for relocs.  */
7283
              if ((o->flags & SEC_RELOC) == 0
7284
                  || o->reloc_count == 0
7285
                  || section_allows_mips16_refs_p (o))
7286
                continue;
7287
 
7288
              sec_relocs
7289
                = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7290
                                             info->keep_memory);
7291
              if (sec_relocs == NULL)
7292
                return FALSE;
7293
 
7294
              rend = sec_relocs + o->reloc_count;
7295
              for (r = sec_relocs; r < rend; r++)
7296
                if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7297
                    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7298
                  break;
7299
 
7300
              if (elf_section_data (o)->relocs != sec_relocs)
7301
                free (sec_relocs);
7302
 
7303
              if (r < rend)
7304
                break;
7305
            }
7306
 
7307
          if (o == NULL)
7308
            {
7309
              /* There is no non-call reloc for this stub, so we do
7310
                 not need it.  Since this function is called before
7311
                 the linker maps input sections to output sections, we
7312
                 can easily discard it by setting the SEC_EXCLUDE
7313
                 flag.  */
7314
              sec->flags |= SEC_EXCLUDE;
7315
              return TRUE;
7316
            }
7317
 
7318
          /* Record this stub in an array of local symbol stubs for
7319
             this BFD.  */
7320
          if (elf_tdata (abfd)->local_stubs == NULL)
7321
            {
7322
              unsigned long symcount;
7323
              asection **n;
7324
              bfd_size_type amt;
7325
 
7326
              if (elf_bad_symtab (abfd))
7327
                symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7328
              else
7329
                symcount = symtab_hdr->sh_info;
7330
              amt = symcount * sizeof (asection *);
7331
              n = bfd_zalloc (abfd, amt);
7332
              if (n == NULL)
7333
                return FALSE;
7334
              elf_tdata (abfd)->local_stubs = n;
7335
            }
7336
 
7337
          sec->flags |= SEC_KEEP;
7338
          elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7339
 
7340
          /* We don't need to set mips16_stubs_seen in this case.
7341
             That flag is used to see whether we need to look through
7342
             the global symbol table for stubs.  We don't need to set
7343
             it here, because we just have a local stub.  */
7344
        }
7345
      else
7346
        {
7347
          struct mips_elf_link_hash_entry *h;
7348
 
7349
          h = ((struct mips_elf_link_hash_entry *)
7350
               sym_hashes[r_symndx - extsymoff]);
7351
 
7352
          while (h->root.root.type == bfd_link_hash_indirect
7353
                 || h->root.root.type == bfd_link_hash_warning)
7354
            h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7355
 
7356
          /* H is the symbol this stub is for.  */
7357
 
7358
          /* If we already have an appropriate stub for this function, we
7359
             don't need another one, so we can discard this one.  Since
7360
             this function is called before the linker maps input sections
7361
             to output sections, we can easily discard it by setting the
7362
             SEC_EXCLUDE flag.  */
7363
          if (h->fn_stub != NULL)
7364
            {
7365
              sec->flags |= SEC_EXCLUDE;
7366
              return TRUE;
7367
            }
7368
 
7369
          sec->flags |= SEC_KEEP;
7370
          h->fn_stub = sec;
7371
          mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7372
        }
7373
    }
7374
  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7375
    {
7376
      unsigned long r_symndx;
7377
      struct mips_elf_link_hash_entry *h;
7378
      asection **loc;
7379
 
7380
      /* Look at the relocation information to figure out which symbol
7381
         this is for.  */
7382
 
7383
      r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7384
      if (r_symndx == 0)
7385
        {
7386
          (*_bfd_error_handler)
7387
            (_("%B: Warning: cannot determine the target function for"
7388
               " stub section `%s'"),
7389
             abfd, name);
7390
          bfd_set_error (bfd_error_bad_value);
7391
          return FALSE;
7392
        }
7393
 
7394
      if (r_symndx < extsymoff
7395
          || sym_hashes[r_symndx - extsymoff] == NULL)
7396
        {
7397
          asection *o;
7398
 
7399
          /* This stub is for a local symbol.  This stub will only be
7400
             needed if there is some relocation (R_MIPS16_26) in this BFD
7401
             that refers to this symbol.  */
7402
          for (o = abfd->sections; o != NULL; o = o->next)
7403
            {
7404
              Elf_Internal_Rela *sec_relocs;
7405
              const Elf_Internal_Rela *r, *rend;
7406
 
7407
              /* We can ignore stub sections when looking for relocs.  */
7408
              if ((o->flags & SEC_RELOC) == 0
7409
                  || o->reloc_count == 0
7410
                  || section_allows_mips16_refs_p (o))
7411
                continue;
7412
 
7413
              sec_relocs
7414
                = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7415
                                             info->keep_memory);
7416
              if (sec_relocs == NULL)
7417
                return FALSE;
7418
 
7419
              rend = sec_relocs + o->reloc_count;
7420
              for (r = sec_relocs; r < rend; r++)
7421
                if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7422
                    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7423
                    break;
7424
 
7425
              if (elf_section_data (o)->relocs != sec_relocs)
7426
                free (sec_relocs);
7427
 
7428
              if (r < rend)
7429
                break;
7430
            }
7431
 
7432
          if (o == NULL)
7433
            {
7434
              /* There is no non-call reloc for this stub, so we do
7435
                 not need it.  Since this function is called before
7436
                 the linker maps input sections to output sections, we
7437
                 can easily discard it by setting the SEC_EXCLUDE
7438
                 flag.  */
7439
              sec->flags |= SEC_EXCLUDE;
7440
              return TRUE;
7441
            }
7442
 
7443
          /* Record this stub in an array of local symbol call_stubs for
7444
             this BFD.  */
7445
          if (elf_tdata (abfd)->local_call_stubs == NULL)
7446
            {
7447
              unsigned long symcount;
7448
              asection **n;
7449
              bfd_size_type amt;
7450
 
7451
              if (elf_bad_symtab (abfd))
7452
                symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7453
              else
7454
                symcount = symtab_hdr->sh_info;
7455
              amt = symcount * sizeof (asection *);
7456
              n = bfd_zalloc (abfd, amt);
7457
              if (n == NULL)
7458
                return FALSE;
7459
              elf_tdata (abfd)->local_call_stubs = n;
7460
            }
7461
 
7462
          sec->flags |= SEC_KEEP;
7463
          elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7464
 
7465
          /* We don't need to set mips16_stubs_seen in this case.
7466
             That flag is used to see whether we need to look through
7467
             the global symbol table for stubs.  We don't need to set
7468
             it here, because we just have a local stub.  */
7469
        }
7470
      else
7471
        {
7472
          h = ((struct mips_elf_link_hash_entry *)
7473
               sym_hashes[r_symndx - extsymoff]);
7474
 
7475
          /* H is the symbol this stub is for.  */
7476
 
7477
          if (CALL_FP_STUB_P (name))
7478
            loc = &h->call_fp_stub;
7479
          else
7480
            loc = &h->call_stub;
7481
 
7482
          /* If we already have an appropriate stub for this function, we
7483
             don't need another one, so we can discard this one.  Since
7484
             this function is called before the linker maps input sections
7485
             to output sections, we can easily discard it by setting the
7486
             SEC_EXCLUDE flag.  */
7487
          if (*loc != NULL)
7488
            {
7489
              sec->flags |= SEC_EXCLUDE;
7490
              return TRUE;
7491
            }
7492
 
7493
          sec->flags |= SEC_KEEP;
7494
          *loc = sec;
7495
          mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7496
        }
7497
    }
7498
 
7499
  sreloc = NULL;
7500
  contents = NULL;
7501
  for (rel = relocs; rel < rel_end; ++rel)
7502
    {
7503
      unsigned long r_symndx;
7504
      unsigned int r_type;
7505
      struct elf_link_hash_entry *h;
7506
      bfd_boolean can_make_dynamic_p;
7507
 
7508
      r_symndx = ELF_R_SYM (abfd, rel->r_info);
7509
      r_type = ELF_R_TYPE (abfd, rel->r_info);
7510
 
7511
      if (r_symndx < extsymoff)
7512
        h = NULL;
7513
      else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7514
        {
7515
          (*_bfd_error_handler)
7516
            (_("%B: Malformed reloc detected for section %s"),
7517
             abfd, name);
7518
          bfd_set_error (bfd_error_bad_value);
7519
          return FALSE;
7520
        }
7521
      else
7522
        {
7523
          h = sym_hashes[r_symndx - extsymoff];
7524
          while (h != NULL
7525
                 && (h->root.type == bfd_link_hash_indirect
7526
                     || h->root.type == bfd_link_hash_warning))
7527
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
7528
        }
7529
 
7530
      /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7531
         relocation into a dynamic one.  */
7532
      can_make_dynamic_p = FALSE;
7533
      switch (r_type)
7534
        {
7535
        case R_MIPS16_GOT16:
7536
        case R_MIPS16_CALL16:
7537
        case R_MIPS_GOT16:
7538
        case R_MIPS_CALL16:
7539
        case R_MIPS_CALL_HI16:
7540
        case R_MIPS_CALL_LO16:
7541
        case R_MIPS_GOT_HI16:
7542
        case R_MIPS_GOT_LO16:
7543
        case R_MIPS_GOT_PAGE:
7544
        case R_MIPS_GOT_OFST:
7545
        case R_MIPS_GOT_DISP:
7546
        case R_MIPS_TLS_GOTTPREL:
7547
        case R_MIPS_TLS_GD:
7548
        case R_MIPS_TLS_LDM:
7549
          if (dynobj == NULL)
7550
            elf_hash_table (info)->dynobj = dynobj = abfd;
7551
          if (!mips_elf_create_got_section (dynobj, info))
7552
            return FALSE;
7553
          if (htab->is_vxworks && !info->shared)
7554
            {
7555
              (*_bfd_error_handler)
7556
                (_("%B: GOT reloc at 0x%lx not expected in executables"),
7557
                 abfd, (unsigned long) rel->r_offset);
7558
              bfd_set_error (bfd_error_bad_value);
7559
              return FALSE;
7560
            }
7561
          break;
7562
 
7563
          /* This is just a hint; it can safely be ignored.  Don't set
7564
             has_static_relocs for the corresponding symbol.  */
7565
        case R_MIPS_JALR:
7566
          break;
7567
 
7568
        case R_MIPS_32:
7569
        case R_MIPS_REL32:
7570
        case R_MIPS_64:
7571
          /* In VxWorks executables, references to external symbols
7572
             must be handled using copy relocs or PLT entries; it is not
7573
             possible to convert this relocation into a dynamic one.
7574
 
7575
             For executables that use PLTs and copy-relocs, we have a
7576
             choice between converting the relocation into a dynamic
7577
             one or using copy relocations or PLT entries.  It is
7578
             usually better to do the former, unless the relocation is
7579
             against a read-only section.  */
7580
          if ((info->shared
7581
               || (h != NULL
7582
                   && !htab->is_vxworks
7583
                   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7584
                   && !(!info->nocopyreloc
7585
                        && !PIC_OBJECT_P (abfd)
7586
                        && MIPS_ELF_READONLY_SECTION (sec))))
7587
              && (sec->flags & SEC_ALLOC) != 0)
7588
            {
7589
              can_make_dynamic_p = TRUE;
7590
              if (dynobj == NULL)
7591
                elf_hash_table (info)->dynobj = dynobj = abfd;
7592
              break;
7593
            }
7594
          /* For sections that are not SEC_ALLOC a copy reloc would be
7595
             output if possible (implying questionable semantics for
7596
             read-only data objects) or otherwise the final link would
7597
             fail as ld.so will not process them and could not therefore
7598
             handle any outstanding dynamic relocations.
7599
 
7600
             For such sections that are also SEC_DEBUGGING, we can avoid
7601
             these problems by simply ignoring any relocs as these
7602
             sections have a predefined use and we know it is safe to do
7603
             so.
7604
 
7605
             This is needed in cases such as a global symbol definition
7606
             in a shared library causing a common symbol from an object
7607
             file to be converted to an undefined reference.  If that
7608
             happens, then all the relocations against this symbol from
7609
             SEC_DEBUGGING sections in the object file will resolve to
7610
             nil.  */
7611
          if ((sec->flags & SEC_DEBUGGING) != 0)
7612
            break;
7613
          /* Fall through.  */
7614
 
7615
        default:
7616
          /* Most static relocations require pointer equality, except
7617
             for branches.  */
7618
          if (h)
7619
            h->pointer_equality_needed = TRUE;
7620
          /* Fall through.  */
7621
 
7622
        case R_MIPS_26:
7623
        case R_MIPS_PC16:
7624
        case R_MIPS16_26:
7625
          if (h)
7626
            ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7627
          break;
7628
        }
7629
 
7630
      if (h)
7631
        {
7632
          /* Relocations against the special VxWorks __GOTT_BASE__ and
7633
             __GOTT_INDEX__ symbols must be left to the loader.  Allocate
7634
             room for them in .rela.dyn.  */
7635
          if (is_gott_symbol (info, h))
7636
            {
7637
              if (sreloc == NULL)
7638
                {
7639
                  sreloc = mips_elf_rel_dyn_section (info, TRUE);
7640
                  if (sreloc == NULL)
7641
                    return FALSE;
7642
                }
7643
              mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7644
              if (MIPS_ELF_READONLY_SECTION (sec))
7645
                /* We tell the dynamic linker that there are
7646
                   relocations against the text segment.  */
7647
                info->flags |= DF_TEXTREL;
7648
            }
7649
        }
7650
      else if (r_type == R_MIPS_CALL_LO16
7651
               || r_type == R_MIPS_GOT_LO16
7652
               || r_type == R_MIPS_GOT_DISP
7653
               || (got16_reloc_p (r_type) && htab->is_vxworks))
7654
        {
7655
          /* We may need a local GOT entry for this relocation.  We
7656
             don't count R_MIPS_GOT_PAGE because we can estimate the
7657
             maximum number of pages needed by looking at the size of
7658
             the segment.  Similar comments apply to R_MIPS*_GOT16 and
7659
             R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7660
             always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
7661
             R_MIPS_CALL_HI16 because these are always followed by an
7662
             R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
7663
          if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7664
                                                 rel->r_addend, info, 0))
7665
            return FALSE;
7666
        }
7667
 
7668
      if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7669
        ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7670
 
7671
      switch (r_type)
7672
        {
7673
        case R_MIPS_CALL16:
7674
        case R_MIPS16_CALL16:
7675
          if (h == NULL)
7676
            {
7677
              (*_bfd_error_handler)
7678
                (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7679
                 abfd, (unsigned long) rel->r_offset);
7680
              bfd_set_error (bfd_error_bad_value);
7681
              return FALSE;
7682
            }
7683
          /* Fall through.  */
7684
 
7685
        case R_MIPS_CALL_HI16:
7686
        case R_MIPS_CALL_LO16:
7687
          if (h != NULL)
7688
            {
7689
              /* Make sure there is room in the regular GOT to hold the
7690
                 function's address.  We may eliminate it in favour of
7691
                 a .got.plt entry later; see mips_elf_count_got_symbols.  */
7692
              if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
7693
                return FALSE;
7694
 
7695
              /* We need a stub, not a plt entry for the undefined
7696
                 function.  But we record it as if it needs plt.  See
7697
                 _bfd_elf_adjust_dynamic_symbol.  */
7698
              h->needs_plt = 1;
7699
              h->type = STT_FUNC;
7700
            }
7701
          break;
7702
 
7703
        case R_MIPS_GOT_PAGE:
7704
          /* If this is a global, overridable symbol, GOT_PAGE will
7705
             decay to GOT_DISP, so we'll need a GOT entry for it.  */
7706
          if (h)
7707
            {
7708
              struct mips_elf_link_hash_entry *hmips =
7709
                (struct mips_elf_link_hash_entry *) h;
7710
 
7711
              /* This symbol is definitely not overridable.  */
7712
              if (hmips->root.def_regular
7713
                  && ! (info->shared && ! info->symbolic
7714
                        && ! hmips->root.forced_local))
7715
                h = NULL;
7716
            }
7717
          /* Fall through.  */
7718
 
7719
        case R_MIPS16_GOT16:
7720
        case R_MIPS_GOT16:
7721
        case R_MIPS_GOT_HI16:
7722
        case R_MIPS_GOT_LO16:
7723
          if (!h || r_type == R_MIPS_GOT_PAGE)
7724
            {
7725
              /* This relocation needs (or may need, if h != NULL) a
7726
                 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
7727
                 know for sure until we know whether the symbol is
7728
                 preemptible.  */
7729
              if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7730
                {
7731
                  if (!mips_elf_get_section_contents (abfd, sec, &contents))
7732
                    return FALSE;
7733
                  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7734
                  addend = mips_elf_read_rel_addend (abfd, rel,
7735
                                                     howto, contents);
7736
                  if (got16_reloc_p (r_type))
7737
                    mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7738
                                                  contents, &addend);
7739
                  else
7740
                    addend <<= howto->rightshift;
7741
                }
7742
              else
7743
                addend = rel->r_addend;
7744
              if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7745
                                                   addend))
7746
                return FALSE;
7747
            }
7748
          /* Fall through.  */
7749
 
7750
        case R_MIPS_GOT_DISP:
7751
          if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7752
                                                       FALSE, 0))
7753
            return FALSE;
7754
          break;
7755
 
7756
        case R_MIPS_TLS_GOTTPREL:
7757
          if (info->shared)
7758
            info->flags |= DF_STATIC_TLS;
7759
          /* Fall through */
7760
 
7761
        case R_MIPS_TLS_LDM:
7762
          if (r_type == R_MIPS_TLS_LDM)
7763
            {
7764
              r_symndx = STN_UNDEF;
7765
              h = NULL;
7766
            }
7767
          /* Fall through */
7768
 
7769
        case R_MIPS_TLS_GD:
7770
          /* This symbol requires a global offset table entry, or two
7771
             for TLS GD relocations.  */
7772
          {
7773
            unsigned char flag = (r_type == R_MIPS_TLS_GD
7774
                                  ? GOT_TLS_GD
7775
                                  : r_type == R_MIPS_TLS_LDM
7776
                                  ? GOT_TLS_LDM
7777
                                  : GOT_TLS_IE);
7778
            if (h != NULL)
7779
              {
7780
                struct mips_elf_link_hash_entry *hmips =
7781
                  (struct mips_elf_link_hash_entry *) h;
7782
                hmips->tls_type |= flag;
7783
 
7784
                if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7785
                                                             FALSE, flag))
7786
                  return FALSE;
7787
              }
7788
            else
7789
              {
7790
                BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
7791
 
7792
                if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7793
                                                       rel->r_addend,
7794
                                                       info, flag))
7795
                  return FALSE;
7796
              }
7797
          }
7798
          break;
7799
 
7800
        case R_MIPS_32:
7801
        case R_MIPS_REL32:
7802
        case R_MIPS_64:
7803
          /* In VxWorks executables, references to external symbols
7804
             are handled using copy relocs or PLT stubs, so there's
7805
             no need to add a .rela.dyn entry for this relocation.  */
7806
          if (can_make_dynamic_p)
7807
            {
7808
              if (sreloc == NULL)
7809
                {
7810
                  sreloc = mips_elf_rel_dyn_section (info, TRUE);
7811
                  if (sreloc == NULL)
7812
                    return FALSE;
7813
                }
7814
              if (info->shared && h == NULL)
7815
                {
7816
                  /* When creating a shared object, we must copy these
7817
                     reloc types into the output file as R_MIPS_REL32
7818
                     relocs.  Make room for this reloc in .rel(a).dyn.  */
7819
                  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7820
                  if (MIPS_ELF_READONLY_SECTION (sec))
7821
                    /* We tell the dynamic linker that there are
7822
                       relocations against the text segment.  */
7823
                    info->flags |= DF_TEXTREL;
7824
                }
7825
              else
7826
                {
7827
                  struct mips_elf_link_hash_entry *hmips;
7828
 
7829
                  /* For a shared object, we must copy this relocation
7830
                     unless the symbol turns out to be undefined and
7831
                     weak with non-default visibility, in which case
7832
                     it will be left as zero.
7833
 
7834
                     We could elide R_MIPS_REL32 for locally binding symbols
7835
                     in shared libraries, but do not yet do so.
7836
 
7837
                     For an executable, we only need to copy this
7838
                     reloc if the symbol is defined in a dynamic
7839
                     object.  */
7840
                  hmips = (struct mips_elf_link_hash_entry *) h;
7841
                  ++hmips->possibly_dynamic_relocs;
7842
                  if (MIPS_ELF_READONLY_SECTION (sec))
7843
                    /* We need it to tell the dynamic linker if there
7844
                       are relocations against the text segment.  */
7845
                    hmips->readonly_reloc = TRUE;
7846
                }
7847
            }
7848
 
7849
          if (SGI_COMPAT (abfd))
7850
            mips_elf_hash_table (info)->compact_rel_size +=
7851
              sizeof (Elf32_External_crinfo);
7852
          break;
7853
 
7854
        case R_MIPS_26:
7855
        case R_MIPS_GPREL16:
7856
        case R_MIPS_LITERAL:
7857
        case R_MIPS_GPREL32:
7858
          if (SGI_COMPAT (abfd))
7859
            mips_elf_hash_table (info)->compact_rel_size +=
7860
              sizeof (Elf32_External_crinfo);
7861
          break;
7862
 
7863
          /* This relocation describes the C++ object vtable hierarchy.
7864
             Reconstruct it for later use during GC.  */
7865
        case R_MIPS_GNU_VTINHERIT:
7866
          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7867
            return FALSE;
7868
          break;
7869
 
7870
          /* This relocation describes which C++ vtable entries are actually
7871
             used.  Record for later use during GC.  */
7872
        case R_MIPS_GNU_VTENTRY:
7873
          BFD_ASSERT (h != NULL);
7874
          if (h != NULL
7875
              && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7876
            return FALSE;
7877
          break;
7878
 
7879
        default:
7880
          break;
7881
        }
7882
 
7883
      /* We must not create a stub for a symbol that has relocations
7884
         related to taking the function's address.  This doesn't apply to
7885
         VxWorks, where CALL relocs refer to a .got.plt entry instead of
7886
         a normal .got entry.  */
7887
      if (!htab->is_vxworks && h != NULL)
7888
        switch (r_type)
7889
          {
7890
          default:
7891
            ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7892
            break;
7893
          case R_MIPS16_CALL16:
7894
          case R_MIPS_CALL16:
7895
          case R_MIPS_CALL_HI16:
7896
          case R_MIPS_CALL_LO16:
7897
          case R_MIPS_JALR:
7898
            break;
7899
          }
7900
 
7901
      /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7902
         if there is one.  We only need to handle global symbols here;
7903
         we decide whether to keep or delete stubs for local symbols
7904
         when processing the stub's relocations.  */
7905
      if (h != NULL
7906
          && !mips16_call_reloc_p (r_type)
7907
          && !section_allows_mips16_refs_p (sec))
7908
        {
7909
          struct mips_elf_link_hash_entry *mh;
7910
 
7911
          mh = (struct mips_elf_link_hash_entry *) h;
7912
          mh->need_fn_stub = TRUE;
7913
        }
7914
 
7915
      /* Refuse some position-dependent relocations when creating a
7916
         shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
7917
         not PIC, but we can create dynamic relocations and the result
7918
         will be fine.  Also do not refuse R_MIPS_LO16, which can be
7919
         combined with R_MIPS_GOT16.  */
7920
      if (info->shared)
7921
        {
7922
          switch (r_type)
7923
            {
7924
            case R_MIPS16_HI16:
7925
            case R_MIPS_HI16:
7926
            case R_MIPS_HIGHER:
7927
            case R_MIPS_HIGHEST:
7928
              /* Don't refuse a high part relocation if it's against
7929
                 no symbol (e.g. part of a compound relocation).  */
7930
              if (r_symndx == STN_UNDEF)
7931
                break;
7932
 
7933
              /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7934
                 and has a special meaning.  */
7935
              if (!NEWABI_P (abfd) && h != NULL
7936
                  && strcmp (h->root.root.string, "_gp_disp") == 0)
7937
                break;
7938
 
7939
              /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
7940
              if (is_gott_symbol (info, h))
7941
                break;
7942
 
7943
              /* FALLTHROUGH */
7944
 
7945
            case R_MIPS16_26:
7946
            case R_MIPS_26:
7947
              howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7948
              (*_bfd_error_handler)
7949
                (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7950
                 abfd, howto->name,
7951
                 (h) ? h->root.root.string : "a local symbol");
7952
              bfd_set_error (bfd_error_bad_value);
7953
              return FALSE;
7954
            default:
7955
              break;
7956
            }
7957
        }
7958
    }
7959
 
7960
  return TRUE;
7961
}
7962
 
7963
bfd_boolean
7964
_bfd_mips_relax_section (bfd *abfd, asection *sec,
7965
                         struct bfd_link_info *link_info,
7966
                         bfd_boolean *again)
7967
{
7968
  Elf_Internal_Rela *internal_relocs;
7969
  Elf_Internal_Rela *irel, *irelend;
7970
  Elf_Internal_Shdr *symtab_hdr;
7971
  bfd_byte *contents = NULL;
7972
  size_t extsymoff;
7973
  bfd_boolean changed_contents = FALSE;
7974
  bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7975
  Elf_Internal_Sym *isymbuf = NULL;
7976
 
7977
  /* We are not currently changing any sizes, so only one pass.  */
7978
  *again = FALSE;
7979
 
7980
  if (link_info->relocatable)
7981
    return TRUE;
7982
 
7983
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7984
                                               link_info->keep_memory);
7985
  if (internal_relocs == NULL)
7986
    return TRUE;
7987
 
7988
  irelend = internal_relocs + sec->reloc_count
7989
    * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7990
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7991
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7992
 
7993
  for (irel = internal_relocs; irel < irelend; irel++)
7994
    {
7995
      bfd_vma symval;
7996
      bfd_signed_vma sym_offset;
7997
      unsigned int r_type;
7998
      unsigned long r_symndx;
7999
      asection *sym_sec;
8000
      unsigned long instruction;
8001
 
8002
      /* Turn jalr into bgezal, and jr into beq, if they're marked
8003
         with a JALR relocation, that indicate where they jump to.
8004
         This saves some pipeline bubbles.  */
8005
      r_type = ELF_R_TYPE (abfd, irel->r_info);
8006
      if (r_type != R_MIPS_JALR)
8007
        continue;
8008
 
8009
      r_symndx = ELF_R_SYM (abfd, irel->r_info);
8010
      /* Compute the address of the jump target.  */
8011
      if (r_symndx >= extsymoff)
8012
        {
8013
          struct mips_elf_link_hash_entry *h
8014
            = ((struct mips_elf_link_hash_entry *)
8015
               elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8016
 
8017
          while (h->root.root.type == bfd_link_hash_indirect
8018
                 || h->root.root.type == bfd_link_hash_warning)
8019
            h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8020
 
8021
          /* If a symbol is undefined, or if it may be overridden,
8022
             skip it.  */
8023
          if (! ((h->root.root.type == bfd_link_hash_defined
8024
                  || h->root.root.type == bfd_link_hash_defweak)
8025
                 && h->root.root.u.def.section)
8026
              || (link_info->shared && ! link_info->symbolic
8027
                  && !h->root.forced_local))
8028
            continue;
8029
 
8030
          sym_sec = h->root.root.u.def.section;
8031
          if (sym_sec->output_section)
8032
            symval = (h->root.root.u.def.value
8033
                      + sym_sec->output_section->vma
8034
                      + sym_sec->output_offset);
8035
          else
8036
            symval = h->root.root.u.def.value;
8037
        }
8038
      else
8039
        {
8040
          Elf_Internal_Sym *isym;
8041
 
8042
          /* Read this BFD's symbols if we haven't done so already.  */
8043
          if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8044
            {
8045
              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8046
              if (isymbuf == NULL)
8047
                isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8048
                                                symtab_hdr->sh_info, 0,
8049
                                                NULL, NULL, NULL);
8050
              if (isymbuf == NULL)
8051
                goto relax_return;
8052
            }
8053
 
8054
          isym = isymbuf + r_symndx;
8055
          if (isym->st_shndx == SHN_UNDEF)
8056
            continue;
8057
          else if (isym->st_shndx == SHN_ABS)
8058
            sym_sec = bfd_abs_section_ptr;
8059
          else if (isym->st_shndx == SHN_COMMON)
8060
            sym_sec = bfd_com_section_ptr;
8061
          else
8062
            sym_sec
8063
              = bfd_section_from_elf_index (abfd, isym->st_shndx);
8064
          symval = isym->st_value
8065
            + sym_sec->output_section->vma
8066
            + sym_sec->output_offset;
8067
        }
8068
 
8069
      /* Compute branch offset, from delay slot of the jump to the
8070
         branch target.  */
8071
      sym_offset = (symval + irel->r_addend)
8072
        - (sec_start + irel->r_offset + 4);
8073
 
8074
      /* Branch offset must be properly aligned.  */
8075
      if ((sym_offset & 3) != 0)
8076
        continue;
8077
 
8078
      sym_offset >>= 2;
8079
 
8080
      /* Check that it's in range.  */
8081
      if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8082
        continue;
8083
 
8084
      /* Get the section contents if we haven't done so already.  */
8085
      if (!mips_elf_get_section_contents (abfd, sec, &contents))
8086
        goto relax_return;
8087
 
8088
      instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8089
 
8090
      /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
8091
      if ((instruction & 0xfc1fffff) == 0x0000f809)
8092
        instruction = 0x04110000;
8093
      /* If it was jr <reg>, turn it into b <target>.  */
8094
      else if ((instruction & 0xfc1fffff) == 0x00000008)
8095
        instruction = 0x10000000;
8096
      else
8097
        continue;
8098
 
8099
      instruction |= (sym_offset & 0xffff);
8100
      bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8101
      changed_contents = TRUE;
8102
    }
8103
 
8104
  if (contents != NULL
8105
      && elf_section_data (sec)->this_hdr.contents != contents)
8106
    {
8107
      if (!changed_contents && !link_info->keep_memory)
8108
        free (contents);
8109
      else
8110
        {
8111
          /* Cache the section contents for elf_link_input_bfd.  */
8112
          elf_section_data (sec)->this_hdr.contents = contents;
8113
        }
8114
    }
8115
  return TRUE;
8116
 
8117
 relax_return:
8118
  if (contents != NULL
8119
      && elf_section_data (sec)->this_hdr.contents != contents)
8120
    free (contents);
8121
  return FALSE;
8122
}
8123
 
8124
/* Allocate space for global sym dynamic relocs.  */
8125
 
8126
static bfd_boolean
8127
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8128
{
8129
  struct bfd_link_info *info = inf;
8130
  bfd *dynobj;
8131
  struct mips_elf_link_hash_entry *hmips;
8132
  struct mips_elf_link_hash_table *htab;
8133
 
8134
  htab = mips_elf_hash_table (info);
8135
  BFD_ASSERT (htab != NULL);
8136
 
8137
  dynobj = elf_hash_table (info)->dynobj;
8138
  hmips = (struct mips_elf_link_hash_entry *) h;
8139
 
8140
  /* VxWorks executables are handled elsewhere; we only need to
8141
     allocate relocations in shared objects.  */
8142
  if (htab->is_vxworks && !info->shared)
8143
    return TRUE;
8144
 
8145
  /* Ignore indirect and warning symbols.  All relocations against
8146
     such symbols will be redirected to the target symbol.  */
8147
  if (h->root.type == bfd_link_hash_indirect
8148
      || h->root.type == bfd_link_hash_warning)
8149
    return TRUE;
8150
 
8151
  /* If this symbol is defined in a dynamic object, or we are creating
8152
     a shared library, we will need to copy any R_MIPS_32 or
8153
     R_MIPS_REL32 relocs against it into the output file.  */
8154
  if (! info->relocatable
8155
      && hmips->possibly_dynamic_relocs != 0
8156
      && (h->root.type == bfd_link_hash_defweak
8157
          || !h->def_regular
8158
          || info->shared))
8159
    {
8160
      bfd_boolean do_copy = TRUE;
8161
 
8162
      if (h->root.type == bfd_link_hash_undefweak)
8163
        {
8164
          /* Do not copy relocations for undefined weak symbols with
8165
             non-default visibility.  */
8166
          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8167
            do_copy = FALSE;
8168
 
8169
          /* Make sure undefined weak symbols are output as a dynamic
8170
             symbol in PIEs.  */
8171
          else if (h->dynindx == -1 && !h->forced_local)
8172
            {
8173
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
8174
                return FALSE;
8175
            }
8176
        }
8177
 
8178
      if (do_copy)
8179
        {
8180
          /* Even though we don't directly need a GOT entry for this symbol,
8181
             the SVR4 psABI requires it to have a dynamic symbol table
8182
             index greater that DT_MIPS_GOTSYM if there are dynamic
8183
             relocations against it.
8184
 
8185
             VxWorks does not enforce the same mapping between the GOT
8186
             and the symbol table, so the same requirement does not
8187
             apply there.  */
8188
          if (!htab->is_vxworks)
8189
            {
8190
              if (hmips->global_got_area > GGA_RELOC_ONLY)
8191
                hmips->global_got_area = GGA_RELOC_ONLY;
8192
              hmips->got_only_for_calls = FALSE;
8193
            }
8194
 
8195
          mips_elf_allocate_dynamic_relocations
8196
            (dynobj, info, hmips->possibly_dynamic_relocs);
8197
          if (hmips->readonly_reloc)
8198
            /* We tell the dynamic linker that there are relocations
8199
               against the text segment.  */
8200
            info->flags |= DF_TEXTREL;
8201
        }
8202
    }
8203
 
8204
  return TRUE;
8205
}
8206
 
8207
/* Adjust a symbol defined by a dynamic object and referenced by a
8208
   regular object.  The current definition is in some section of the
8209
   dynamic object, but we're not including those sections.  We have to
8210
   change the definition to something the rest of the link can
8211
   understand.  */
8212
 
8213
bfd_boolean
8214
_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8215
                                     struct elf_link_hash_entry *h)
8216
{
8217
  bfd *dynobj;
8218
  struct mips_elf_link_hash_entry *hmips;
8219
  struct mips_elf_link_hash_table *htab;
8220
 
8221
  htab = mips_elf_hash_table (info);
8222
  BFD_ASSERT (htab != NULL);
8223
 
8224
  dynobj = elf_hash_table (info)->dynobj;
8225
  hmips = (struct mips_elf_link_hash_entry *) h;
8226
 
8227
  /* Make sure we know what is going on here.  */
8228
  BFD_ASSERT (dynobj != NULL
8229
              && (h->needs_plt
8230
                  || h->u.weakdef != NULL
8231
                  || (h->def_dynamic
8232
                      && h->ref_regular
8233
                      && !h->def_regular)));
8234
 
8235
  hmips = (struct mips_elf_link_hash_entry *) h;
8236
 
8237
  /* If there are call relocations against an externally-defined symbol,
8238
     see whether we can create a MIPS lazy-binding stub for it.  We can
8239
     only do this if all references to the function are through call
8240
     relocations, and in that case, the traditional lazy-binding stubs
8241
     are much more efficient than PLT entries.
8242
 
8243
     Traditional stubs are only available on SVR4 psABI-based systems;
8244
     VxWorks always uses PLTs instead.  */
8245
  if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8246
    {
8247
      if (! elf_hash_table (info)->dynamic_sections_created)
8248
        return TRUE;
8249
 
8250
      /* If this symbol is not defined in a regular file, then set
8251
         the symbol to the stub location.  This is required to make
8252
         function pointers compare as equal between the normal
8253
         executable and the shared library.  */
8254
      if (!h->def_regular)
8255
        {
8256
          hmips->needs_lazy_stub = TRUE;
8257
          htab->lazy_stub_count++;
8258
          return TRUE;
8259
        }
8260
    }
8261
  /* As above, VxWorks requires PLT entries for externally-defined
8262
     functions that are only accessed through call relocations.
8263
 
8264
     Both VxWorks and non-VxWorks targets also need PLT entries if there
8265
     are static-only relocations against an externally-defined function.
8266
     This can technically occur for shared libraries if there are
8267
     branches to the symbol, although it is unlikely that this will be
8268
     used in practice due to the short ranges involved.  It can occur
8269
     for any relative or absolute relocation in executables; in that
8270
     case, the PLT entry becomes the function's canonical address.  */
8271
  else if (((h->needs_plt && !hmips->no_fn_stub)
8272
            || (h->type == STT_FUNC && hmips->has_static_relocs))
8273
           && htab->use_plts_and_copy_relocs
8274
           && !SYMBOL_CALLS_LOCAL (info, h)
8275
           && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8276
                && h->root.type == bfd_link_hash_undefweak))
8277
    {
8278
      /* If this is the first symbol to need a PLT entry, allocate room
8279
         for the header.  */
8280
      if (htab->splt->size == 0)
8281
        {
8282
          BFD_ASSERT (htab->sgotplt->size == 0);
8283
 
8284
          /* If we're using the PLT additions to the psABI, each PLT
8285
             entry is 16 bytes and the PLT0 entry is 32 bytes.
8286
             Encourage better cache usage by aligning.  We do this
8287
             lazily to avoid pessimizing traditional objects.  */
8288
          if (!htab->is_vxworks
8289
              && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8290
            return FALSE;
8291
 
8292
          /* Make sure that .got.plt is word-aligned.  We do this lazily
8293
             for the same reason as above.  */
8294
          if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8295
                                          MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8296
            return FALSE;
8297
 
8298
          htab->splt->size += htab->plt_header_size;
8299
 
8300
          /* On non-VxWorks targets, the first two entries in .got.plt
8301
             are reserved.  */
8302
          if (!htab->is_vxworks)
8303
            htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
8304
 
8305
          /* On VxWorks, also allocate room for the header's
8306
             .rela.plt.unloaded entries.  */
8307
          if (htab->is_vxworks && !info->shared)
8308
            htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8309
        }
8310
 
8311
      /* Assign the next .plt entry to this symbol.  */
8312
      h->plt.offset = htab->splt->size;
8313
      htab->splt->size += htab->plt_entry_size;
8314
 
8315
      /* If the output file has no definition of the symbol, set the
8316
         symbol's value to the address of the stub.  */
8317
      if (!info->shared && !h->def_regular)
8318
        {
8319
          h->root.u.def.section = htab->splt;
8320
          h->root.u.def.value = h->plt.offset;
8321
          /* For VxWorks, point at the PLT load stub rather than the
8322
             lazy resolution stub; this stub will become the canonical
8323
             function address.  */
8324
          if (htab->is_vxworks)
8325
            h->root.u.def.value += 8;
8326
        }
8327
 
8328
      /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8329
         relocation.  */
8330
      htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8331
      htab->srelplt->size += (htab->is_vxworks
8332
                              ? MIPS_ELF_RELA_SIZE (dynobj)
8333
                              : MIPS_ELF_REL_SIZE (dynobj));
8334
 
8335
      /* Make room for the .rela.plt.unloaded relocations.  */
8336
      if (htab->is_vxworks && !info->shared)
8337
        htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8338
 
8339
      /* All relocations against this symbol that could have been made
8340
         dynamic will now refer to the PLT entry instead.  */
8341
      hmips->possibly_dynamic_relocs = 0;
8342
 
8343
      return TRUE;
8344
    }
8345
 
8346
  /* If this is a weak symbol, and there is a real definition, the
8347
     processor independent code will have arranged for us to see the
8348
     real definition first, and we can just use the same value.  */
8349
  if (h->u.weakdef != NULL)
8350
    {
8351
      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8352
                  || h->u.weakdef->root.type == bfd_link_hash_defweak);
8353
      h->root.u.def.section = h->u.weakdef->root.u.def.section;
8354
      h->root.u.def.value = h->u.weakdef->root.u.def.value;
8355
      return TRUE;
8356
    }
8357
 
8358
  /* Otherwise, there is nothing further to do for symbols defined
8359
     in regular objects.  */
8360
  if (h->def_regular)
8361
    return TRUE;
8362
 
8363
  /* There's also nothing more to do if we'll convert all relocations
8364
     against this symbol into dynamic relocations.  */
8365
  if (!hmips->has_static_relocs)
8366
    return TRUE;
8367
 
8368
  /* We're now relying on copy relocations.  Complain if we have
8369
     some that we can't convert.  */
8370
  if (!htab->use_plts_and_copy_relocs || info->shared)
8371
    {
8372
      (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8373
                               "dynamic symbol %s"),
8374
                             h->root.root.string);
8375
      bfd_set_error (bfd_error_bad_value);
8376
      return FALSE;
8377
    }
8378
 
8379
  /* We must allocate the symbol in our .dynbss section, which will
8380
     become part of the .bss section of the executable.  There will be
8381
     an entry for this symbol in the .dynsym section.  The dynamic
8382
     object will contain position independent code, so all references
8383
     from the dynamic object to this symbol will go through the global
8384
     offset table.  The dynamic linker will use the .dynsym entry to
8385
     determine the address it must put in the global offset table, so
8386
     both the dynamic object and the regular object will refer to the
8387
     same memory location for the variable.  */
8388
 
8389
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8390
    {
8391
      if (htab->is_vxworks)
8392
        htab->srelbss->size += sizeof (Elf32_External_Rela);
8393
      else
8394
        mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8395
      h->needs_copy = 1;
8396
    }
8397
 
8398
  /* All relocations against this symbol that could have been made
8399
     dynamic will now refer to the local copy instead.  */
8400
  hmips->possibly_dynamic_relocs = 0;
8401
 
8402
  return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8403
}
8404
 
8405
/* This function is called after all the input files have been read,
8406
   and the input sections have been assigned to output sections.  We
8407
   check for any mips16 stub sections that we can discard.  */
8408
 
8409
bfd_boolean
8410
_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8411
                                    struct bfd_link_info *info)
8412
{
8413
  asection *ri;
8414
  struct mips_elf_link_hash_table *htab;
8415
  struct mips_htab_traverse_info hti;
8416
 
8417
  htab = mips_elf_hash_table (info);
8418
  BFD_ASSERT (htab != NULL);
8419
 
8420
  /* The .reginfo section has a fixed size.  */
8421
  ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8422
  if (ri != NULL)
8423
    bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8424
 
8425
  hti.info = info;
8426
  hti.output_bfd = output_bfd;
8427
  hti.error = FALSE;
8428
  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8429
                               mips_elf_check_symbols, &hti);
8430
  if (hti.error)
8431
    return FALSE;
8432
 
8433
  return TRUE;
8434
}
8435
 
8436
/* If the link uses a GOT, lay it out and work out its size.  */
8437
 
8438
static bfd_boolean
8439
mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8440
{
8441
  bfd *dynobj;
8442
  asection *s;
8443
  struct mips_got_info *g;
8444
  bfd_size_type loadable_size = 0;
8445
  bfd_size_type page_gotno;
8446
  bfd *sub;
8447
  struct mips_elf_count_tls_arg count_tls_arg;
8448
  struct mips_elf_link_hash_table *htab;
8449
 
8450
  htab = mips_elf_hash_table (info);
8451
  BFD_ASSERT (htab != NULL);
8452
 
8453
  s = htab->sgot;
8454
  if (s == NULL)
8455
    return TRUE;
8456
 
8457
  dynobj = elf_hash_table (info)->dynobj;
8458
  g = htab->got_info;
8459
 
8460
  /* Allocate room for the reserved entries.  VxWorks always reserves
8461
     3 entries; other objects only reserve 2 entries.  */
8462
  BFD_ASSERT (g->assigned_gotno == 0);
8463
  if (htab->is_vxworks)
8464
    htab->reserved_gotno = 3;
8465
  else
8466
    htab->reserved_gotno = 2;
8467
  g->local_gotno += htab->reserved_gotno;
8468
  g->assigned_gotno = htab->reserved_gotno;
8469
 
8470
  /* Replace entries for indirect and warning symbols with entries for
8471
     the target symbol.  */
8472
  if (!mips_elf_resolve_final_got_entries (g))
8473
    return FALSE;
8474
 
8475
  /* Count the number of GOT symbols.  */
8476
  mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
8477
 
8478
  /* Calculate the total loadable size of the output.  That
8479
     will give us the maximum number of GOT_PAGE entries
8480
     required.  */
8481
  for (sub = info->input_bfds; sub; sub = sub->link_next)
8482
    {
8483
      asection *subsection;
8484
 
8485
      for (subsection = sub->sections;
8486
           subsection;
8487
           subsection = subsection->next)
8488
        {
8489
          if ((subsection->flags & SEC_ALLOC) == 0)
8490
            continue;
8491
          loadable_size += ((subsection->size + 0xf)
8492
                            &~ (bfd_size_type) 0xf);
8493
        }
8494
    }
8495
 
8496
  if (htab->is_vxworks)
8497
    /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8498
       relocations against local symbols evaluate to "G", and the EABI does
8499
       not include R_MIPS_GOT_PAGE.  */
8500
    page_gotno = 0;
8501
  else
8502
    /* Assume there are two loadable segments consisting of contiguous
8503
       sections.  Is 5 enough?  */
8504
    page_gotno = (loadable_size >> 16) + 5;
8505
 
8506
  /* Choose the smaller of the two estimates; both are intended to be
8507
     conservative.  */
8508
  if (page_gotno > g->page_gotno)
8509
    page_gotno = g->page_gotno;
8510
 
8511
  g->local_gotno += page_gotno;
8512
  s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8513
  s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8514
 
8515
  /* We need to calculate tls_gotno for global symbols at this point
8516
     instead of building it up earlier, to avoid doublecounting
8517
     entries for one global symbol from multiple input files.  */
8518
  count_tls_arg.info = info;
8519
  count_tls_arg.needed = 0;
8520
  elf_link_hash_traverse (elf_hash_table (info),
8521
                          mips_elf_count_global_tls_entries,
8522
                          &count_tls_arg);
8523
  g->tls_gotno += count_tls_arg.needed;
8524
  s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8525
 
8526
  /* VxWorks does not support multiple GOTs.  It initializes $gp to
8527
     __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8528
     dynamic loader.  */
8529
  if (htab->is_vxworks)
8530
    {
8531
      /* VxWorks executables do not need a GOT.  */
8532
      if (info->shared)
8533
        {
8534
          /* Each VxWorks GOT entry needs an explicit relocation.  */
8535
          unsigned int count;
8536
 
8537
          count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8538
          if (count)
8539
            mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8540
        }
8541
    }
8542
  else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8543
    {
8544
      if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8545
        return FALSE;
8546
    }
8547
  else
8548
    {
8549
      struct mips_elf_count_tls_arg arg;
8550
 
8551
      /* Set up TLS entries.  */
8552
      g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8553
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
8554
 
8555
      /* Allocate room for the TLS relocations.  */
8556
      arg.info = info;
8557
      arg.needed = 0;
8558
      htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8559
      elf_link_hash_traverse (elf_hash_table (info),
8560
                              mips_elf_count_global_tls_relocs,
8561
                              &arg);
8562
      if (arg.needed)
8563
        mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
8564
    }
8565
 
8566
  return TRUE;
8567
}
8568
 
8569
/* Estimate the size of the .MIPS.stubs section.  */
8570
 
8571
static void
8572
mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8573
{
8574
  struct mips_elf_link_hash_table *htab;
8575
  bfd_size_type dynsymcount;
8576
 
8577
  htab = mips_elf_hash_table (info);
8578
  BFD_ASSERT (htab != NULL);
8579
 
8580
  if (htab->lazy_stub_count == 0)
8581
    return;
8582
 
8583
  /* IRIX rld assumes that a function stub isn't at the end of the .text
8584
     section, so add a dummy entry to the end.  */
8585
  htab->lazy_stub_count++;
8586
 
8587
  /* Get a worst-case estimate of the number of dynamic symbols needed.
8588
     At this point, dynsymcount does not account for section symbols
8589
     and count_section_dynsyms may overestimate the number that will
8590
     be needed.  */
8591
  dynsymcount = (elf_hash_table (info)->dynsymcount
8592
                 + count_section_dynsyms (output_bfd, info));
8593
 
8594
  /* Determine the size of one stub entry.  */
8595
  htab->function_stub_size = (dynsymcount > 0x10000
8596
                              ? MIPS_FUNCTION_STUB_BIG_SIZE
8597
                              : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8598
 
8599
  htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8600
}
8601
 
8602
/* A mips_elf_link_hash_traverse callback for which DATA points to the
8603
   MIPS hash table.  If H needs a traditional MIPS lazy-binding stub,
8604
   allocate an entry in the stubs section.  */
8605
 
8606
static bfd_boolean
8607
mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8608
{
8609
  struct mips_elf_link_hash_table *htab;
8610
 
8611
  htab = (struct mips_elf_link_hash_table *) data;
8612
  if (h->needs_lazy_stub)
8613
    {
8614
      h->root.root.u.def.section = htab->sstubs;
8615
      h->root.root.u.def.value = htab->sstubs->size;
8616
      h->root.plt.offset = htab->sstubs->size;
8617
      htab->sstubs->size += htab->function_stub_size;
8618
    }
8619
  return TRUE;
8620
}
8621
 
8622
/* Allocate offsets in the stubs section to each symbol that needs one.
8623
   Set the final size of the .MIPS.stub section.  */
8624
 
8625
static void
8626
mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8627
{
8628
  struct mips_elf_link_hash_table *htab;
8629
 
8630
  htab = mips_elf_hash_table (info);
8631
  BFD_ASSERT (htab != NULL);
8632
 
8633
  if (htab->lazy_stub_count == 0)
8634
    return;
8635
 
8636
  htab->sstubs->size = 0;
8637
  mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
8638
  htab->sstubs->size += htab->function_stub_size;
8639
  BFD_ASSERT (htab->sstubs->size
8640
              == htab->lazy_stub_count * htab->function_stub_size);
8641
}
8642
 
8643
/* Set the sizes of the dynamic sections.  */
8644
 
8645
bfd_boolean
8646
_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8647
                                     struct bfd_link_info *info)
8648
{
8649
  bfd *dynobj;
8650
  asection *s, *sreldyn;
8651
  bfd_boolean reltext;
8652
  struct mips_elf_link_hash_table *htab;
8653
 
8654
  htab = mips_elf_hash_table (info);
8655
  BFD_ASSERT (htab != NULL);
8656
  dynobj = elf_hash_table (info)->dynobj;
8657
  BFD_ASSERT (dynobj != NULL);
8658
 
8659
  if (elf_hash_table (info)->dynamic_sections_created)
8660
    {
8661
      /* Set the contents of the .interp section to the interpreter.  */
8662
      if (info->executable)
8663
        {
8664
          s = bfd_get_section_by_name (dynobj, ".interp");
8665
          BFD_ASSERT (s != NULL);
8666
          s->size
8667
            = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8668
          s->contents
8669
            = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8670
        }
8671
 
8672
      /* Create a symbol for the PLT, if we know that we are using it.  */
8673
      if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8674
        {
8675
          struct elf_link_hash_entry *h;
8676
 
8677
          BFD_ASSERT (htab->use_plts_and_copy_relocs);
8678
 
8679
          h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8680
                                           "_PROCEDURE_LINKAGE_TABLE_");
8681
          htab->root.hplt = h;
8682
          if (h == NULL)
8683
            return FALSE;
8684
          h->type = STT_FUNC;
8685
        }
8686
    }
8687
 
8688
  /* Allocate space for global sym dynamic relocs.  */
8689
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8690
 
8691
  mips_elf_estimate_stub_size (output_bfd, info);
8692
 
8693
  if (!mips_elf_lay_out_got (output_bfd, info))
8694
    return FALSE;
8695
 
8696
  mips_elf_lay_out_lazy_stubs (info);
8697
 
8698
  /* The check_relocs and adjust_dynamic_symbol entry points have
8699
     determined the sizes of the various dynamic sections.  Allocate
8700
     memory for them.  */
8701
  reltext = FALSE;
8702
  for (s = dynobj->sections; s != NULL; s = s->next)
8703
    {
8704
      const char *name;
8705
 
8706
      /* It's OK to base decisions on the section name, because none
8707
         of the dynobj section names depend upon the input files.  */
8708
      name = bfd_get_section_name (dynobj, s);
8709
 
8710
      if ((s->flags & SEC_LINKER_CREATED) == 0)
8711
        continue;
8712
 
8713
      if (CONST_STRNEQ (name, ".rel"))
8714
        {
8715
          if (s->size != 0)
8716
            {
8717
              const char *outname;
8718
              asection *target;
8719
 
8720
              /* If this relocation section applies to a read only
8721
                 section, then we probably need a DT_TEXTREL entry.
8722
                 If the relocation section is .rel(a).dyn, we always
8723
                 assert a DT_TEXTREL entry rather than testing whether
8724
                 there exists a relocation to a read only section or
8725
                 not.  */
8726
              outname = bfd_get_section_name (output_bfd,
8727
                                              s->output_section);
8728
              target = bfd_get_section_by_name (output_bfd, outname + 4);
8729
              if ((target != NULL
8730
                   && (target->flags & SEC_READONLY) != 0
8731
                   && (target->flags & SEC_ALLOC) != 0)
8732
                  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8733
                reltext = TRUE;
8734
 
8735
              /* We use the reloc_count field as a counter if we need
8736
                 to copy relocs into the output file.  */
8737
              if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8738
                s->reloc_count = 0;
8739
 
8740
              /* If combreloc is enabled, elf_link_sort_relocs() will
8741
                 sort relocations, but in a different way than we do,
8742
                 and before we're done creating relocations.  Also, it
8743
                 will move them around between input sections'
8744
                 relocation's contents, so our sorting would be
8745
                 broken, so don't let it run.  */
8746
              info->combreloc = 0;
8747
            }
8748
        }
8749
      else if (! info->shared
8750
               && ! mips_elf_hash_table (info)->use_rld_obj_head
8751
               && CONST_STRNEQ (name, ".rld_map"))
8752
        {
8753
          /* We add a room for __rld_map.  It will be filled in by the
8754
             rtld to contain a pointer to the _r_debug structure.  */
8755
          s->size += 4;
8756
        }
8757
      else if (SGI_COMPAT (output_bfd)
8758
               && CONST_STRNEQ (name, ".compact_rel"))
8759
        s->size += mips_elf_hash_table (info)->compact_rel_size;
8760
      else if (s == htab->splt)
8761
        {
8762
          /* If the last PLT entry has a branch delay slot, allocate
8763
             room for an extra nop to fill the delay slot.  This is
8764
             for CPUs without load interlocking.  */
8765
          if (! LOAD_INTERLOCKS_P (output_bfd)
8766
              && ! htab->is_vxworks && s->size > 0)
8767
            s->size += 4;
8768
        }
8769
      else if (! CONST_STRNEQ (name, ".init")
8770
               && s != htab->sgot
8771
               && s != htab->sgotplt
8772
               && s != htab->sstubs
8773
               && s != htab->sdynbss)
8774
        {
8775
          /* It's not one of our sections, so don't allocate space.  */
8776
          continue;
8777
        }
8778
 
8779
      if (s->size == 0)
8780
        {
8781
          s->flags |= SEC_EXCLUDE;
8782
          continue;
8783
        }
8784
 
8785
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
8786
        continue;
8787
 
8788
      /* Allocate memory for the section contents.  */
8789
      s->contents = bfd_zalloc (dynobj, s->size);
8790
      if (s->contents == NULL)
8791
        {
8792
          bfd_set_error (bfd_error_no_memory);
8793
          return FALSE;
8794
        }
8795
    }
8796
 
8797
  if (elf_hash_table (info)->dynamic_sections_created)
8798
    {
8799
      /* Add some entries to the .dynamic section.  We fill in the
8800
         values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8801
         must add the entries now so that we get the correct size for
8802
         the .dynamic section.  */
8803
 
8804
      /* SGI object has the equivalence of DT_DEBUG in the
8805
         DT_MIPS_RLD_MAP entry.  This must come first because glibc
8806
         only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8807
         looks at the first one it sees.  */
8808
      if (!info->shared
8809
          && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8810
        return FALSE;
8811
 
8812
      /* The DT_DEBUG entry may be filled in by the dynamic linker and
8813
         used by the debugger.  */
8814
      if (info->executable
8815
          && !SGI_COMPAT (output_bfd)
8816
          && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8817
        return FALSE;
8818
 
8819
      if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8820
        info->flags |= DF_TEXTREL;
8821
 
8822
      if ((info->flags & DF_TEXTREL) != 0)
8823
        {
8824
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8825
            return FALSE;
8826
 
8827
          /* Clear the DF_TEXTREL flag.  It will be set again if we
8828
             write out an actual text relocation; we may not, because
8829
             at this point we do not know whether e.g. any .eh_frame
8830
             absolute relocations have been converted to PC-relative.  */
8831
          info->flags &= ~DF_TEXTREL;
8832
        }
8833
 
8834
      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8835
        return FALSE;
8836
 
8837
      sreldyn = mips_elf_rel_dyn_section (info, FALSE);
8838
      if (htab->is_vxworks)
8839
        {
8840
          /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
8841
             use any of the DT_MIPS_* tags.  */
8842
          if (sreldyn && sreldyn->size > 0)
8843
            {
8844
              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8845
                return FALSE;
8846
 
8847
              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8848
                return FALSE;
8849
 
8850
              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8851
                return FALSE;
8852
            }
8853
        }
8854
      else
8855
        {
8856
          if (sreldyn && sreldyn->size > 0)
8857
            {
8858
              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8859
                return FALSE;
8860
 
8861
              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8862
                return FALSE;
8863
 
8864
              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8865
                return FALSE;
8866
            }
8867
 
8868
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8869
            return FALSE;
8870
 
8871
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8872
            return FALSE;
8873
 
8874
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8875
            return FALSE;
8876
 
8877
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8878
            return FALSE;
8879
 
8880
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8881
            return FALSE;
8882
 
8883
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8884
            return FALSE;
8885
 
8886
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8887
            return FALSE;
8888
 
8889
          if (IRIX_COMPAT (dynobj) == ict_irix5
8890
              && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8891
            return FALSE;
8892
 
8893
          if (IRIX_COMPAT (dynobj) == ict_irix6
8894
              && (bfd_get_section_by_name
8895
                  (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8896
              && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8897
            return FALSE;
8898
        }
8899
      if (htab->splt->size > 0)
8900
        {
8901
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8902
            return FALSE;
8903
 
8904
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8905
            return FALSE;
8906
 
8907
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8908
            return FALSE;
8909
 
8910
          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8911
            return FALSE;
8912
        }
8913
      if (htab->is_vxworks
8914
          && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8915
        return FALSE;
8916
    }
8917
 
8918
  return TRUE;
8919
}
8920
 
8921
/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8922
   Adjust its R_ADDEND field so that it is correct for the output file.
8923
   LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8924
   and sections respectively; both use symbol indexes.  */
8925
 
8926
static void
8927
mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8928
                        bfd *input_bfd, Elf_Internal_Sym *local_syms,
8929
                        asection **local_sections, Elf_Internal_Rela *rel)
8930
{
8931
  unsigned int r_type, r_symndx;
8932
  Elf_Internal_Sym *sym;
8933
  asection *sec;
8934
 
8935
  if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
8936
    {
8937
      r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8938
      if (r_type == R_MIPS16_GPREL
8939
          || r_type == R_MIPS_GPREL16
8940
          || r_type == R_MIPS_GPREL32
8941
          || r_type == R_MIPS_LITERAL)
8942
        {
8943
          rel->r_addend += _bfd_get_gp_value (input_bfd);
8944
          rel->r_addend -= _bfd_get_gp_value (output_bfd);
8945
        }
8946
 
8947
      r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8948
      sym = local_syms + r_symndx;
8949
 
8950
      /* Adjust REL's addend to account for section merging.  */
8951
      if (!info->relocatable)
8952
        {
8953
          sec = local_sections[r_symndx];
8954
          _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8955
        }
8956
 
8957
      /* This would normally be done by the rela_normal code in elflink.c.  */
8958
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8959
        rel->r_addend += local_sections[r_symndx]->output_offset;
8960
    }
8961
}
8962
 
8963
/* Relocate a MIPS ELF section.  */
8964
 
8965
bfd_boolean
8966
_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8967
                                bfd *input_bfd, asection *input_section,
8968
                                bfd_byte *contents, Elf_Internal_Rela *relocs,
8969
                                Elf_Internal_Sym *local_syms,
8970
                                asection **local_sections)
8971
{
8972
  Elf_Internal_Rela *rel;
8973
  const Elf_Internal_Rela *relend;
8974
  bfd_vma addend = 0;
8975
  bfd_boolean use_saved_addend_p = FALSE;
8976
  const struct elf_backend_data *bed;
8977
 
8978
  bed = get_elf_backend_data (output_bfd);
8979
  relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8980
  for (rel = relocs; rel < relend; ++rel)
8981
    {
8982
      const char *name;
8983
      bfd_vma value = 0;
8984
      reloc_howto_type *howto;
8985
      bfd_boolean cross_mode_jump_p;
8986
      /* TRUE if the relocation is a RELA relocation, rather than a
8987
         REL relocation.  */
8988
      bfd_boolean rela_relocation_p = TRUE;
8989
      unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8990
      const char *msg;
8991
      unsigned long r_symndx;
8992
      asection *sec;
8993
      Elf_Internal_Shdr *symtab_hdr;
8994
      struct elf_link_hash_entry *h;
8995
      bfd_boolean rel_reloc;
8996
 
8997
      rel_reloc = (NEWABI_P (input_bfd)
8998
                   && mips_elf_rel_relocation_p (input_bfd, input_section,
8999
                                                 relocs, rel));
9000
      /* Find the relocation howto for this relocation.  */
9001
      howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9002
 
9003
      r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9004
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9005
      if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9006
        {
9007
          sec = local_sections[r_symndx];
9008
          h = NULL;
9009
        }
9010
      else
9011
        {
9012
          unsigned long extsymoff;
9013
 
9014
          extsymoff = 0;
9015
          if (!elf_bad_symtab (input_bfd))
9016
            extsymoff = symtab_hdr->sh_info;
9017
          h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9018
          while (h->root.type == bfd_link_hash_indirect
9019
                 || h->root.type == bfd_link_hash_warning)
9020
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
9021
 
9022
          sec = NULL;
9023
          if (h->root.type == bfd_link_hash_defined
9024
              || h->root.type == bfd_link_hash_defweak)
9025
            sec = h->root.u.def.section;
9026
        }
9027
 
9028
      if (sec != NULL && elf_discarded_section (sec))
9029
        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9030
                                         rel, relend, howto, contents);
9031
 
9032
      if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9033
        {
9034
          /* Some 32-bit code uses R_MIPS_64.  In particular, people use
9035
             64-bit code, but make sure all their addresses are in the
9036
             lowermost or uppermost 32-bit section of the 64-bit address
9037
             space.  Thus, when they use an R_MIPS_64 they mean what is
9038
             usually meant by R_MIPS_32, with the exception that the
9039
             stored value is sign-extended to 64 bits.  */
9040
          howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9041
 
9042
          /* On big-endian systems, we need to lie about the position
9043
             of the reloc.  */
9044
          if (bfd_big_endian (input_bfd))
9045
            rel->r_offset += 4;
9046
        }
9047
 
9048
      if (!use_saved_addend_p)
9049
        {
9050
          /* If these relocations were originally of the REL variety,
9051
             we must pull the addend out of the field that will be
9052
             relocated.  Otherwise, we simply use the contents of the
9053
             RELA relocation.  */
9054
          if (mips_elf_rel_relocation_p (input_bfd, input_section,
9055
                                         relocs, rel))
9056
            {
9057
              rela_relocation_p = FALSE;
9058
              addend = mips_elf_read_rel_addend (input_bfd, rel,
9059
                                                 howto, contents);
9060
              if (hi16_reloc_p (r_type)
9061
                  || (got16_reloc_p (r_type)
9062
                      && mips_elf_local_relocation_p (input_bfd, rel,
9063
                                                      local_sections)))
9064
                {
9065
                  if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9066
                                                     contents, &addend))
9067
                    {
9068
                      if (h)
9069
                        name = h->root.root.string;
9070
                      else
9071
                        name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9072
                                                 local_syms + r_symndx,
9073
                                                 sec);
9074
                      (*_bfd_error_handler)
9075
                        (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9076
                         input_bfd, input_section, name, howto->name,
9077
                         rel->r_offset);
9078
                    }
9079
                }
9080
              else
9081
                addend <<= howto->rightshift;
9082
            }
9083
          else
9084
            addend = rel->r_addend;
9085
          mips_elf_adjust_addend (output_bfd, info, input_bfd,
9086
                                  local_syms, local_sections, rel);
9087
        }
9088
 
9089
      if (info->relocatable)
9090
        {
9091
          if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9092
              && bfd_big_endian (input_bfd))
9093
            rel->r_offset -= 4;
9094
 
9095
          if (!rela_relocation_p && rel->r_addend)
9096
            {
9097
              addend += rel->r_addend;
9098
              if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9099
                addend = mips_elf_high (addend);
9100
              else if (r_type == R_MIPS_HIGHER)
9101
                addend = mips_elf_higher (addend);
9102
              else if (r_type == R_MIPS_HIGHEST)
9103
                addend = mips_elf_highest (addend);
9104
              else
9105
                addend >>= howto->rightshift;
9106
 
9107
              /* We use the source mask, rather than the destination
9108
                 mask because the place to which we are writing will be
9109
                 source of the addend in the final link.  */
9110
              addend &= howto->src_mask;
9111
 
9112
              if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9113
                /* See the comment above about using R_MIPS_64 in the 32-bit
9114
                   ABI.  Here, we need to update the addend.  It would be
9115
                   possible to get away with just using the R_MIPS_32 reloc
9116
                   but for endianness.  */
9117
                {
9118
                  bfd_vma sign_bits;
9119
                  bfd_vma low_bits;
9120
                  bfd_vma high_bits;
9121
 
9122
                  if (addend & ((bfd_vma) 1 << 31))
9123
#ifdef BFD64
9124
                    sign_bits = ((bfd_vma) 1 << 32) - 1;
9125
#else
9126
                    sign_bits = -1;
9127
#endif
9128
                  else
9129
                    sign_bits = 0;
9130
 
9131
                  /* If we don't know that we have a 64-bit type,
9132
                     do two separate stores.  */
9133
                  if (bfd_big_endian (input_bfd))
9134
                    {
9135
                      /* Store the sign-bits (which are most significant)
9136
                         first.  */
9137
                      low_bits = sign_bits;
9138
                      high_bits = addend;
9139
                    }
9140
                  else
9141
                    {
9142
                      low_bits = addend;
9143
                      high_bits = sign_bits;
9144
                    }
9145
                  bfd_put_32 (input_bfd, low_bits,
9146
                              contents + rel->r_offset);
9147
                  bfd_put_32 (input_bfd, high_bits,
9148
                              contents + rel->r_offset + 4);
9149
                  continue;
9150
                }
9151
 
9152
              if (! mips_elf_perform_relocation (info, howto, rel, addend,
9153
                                                 input_bfd, input_section,
9154
                                                 contents, FALSE))
9155
                return FALSE;
9156
            }
9157
 
9158
          /* Go on to the next relocation.  */
9159
          continue;
9160
        }
9161
 
9162
      /* In the N32 and 64-bit ABIs there may be multiple consecutive
9163
         relocations for the same offset.  In that case we are
9164
         supposed to treat the output of each relocation as the addend
9165
         for the next.  */
9166
      if (rel + 1 < relend
9167
          && rel->r_offset == rel[1].r_offset
9168
          && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9169
        use_saved_addend_p = TRUE;
9170
      else
9171
        use_saved_addend_p = FALSE;
9172
 
9173
      /* Figure out what value we are supposed to relocate.  */
9174
      switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9175
                                             input_section, info, rel,
9176
                                             addend, howto, local_syms,
9177
                                             local_sections, &value,
9178
                                             &name, &cross_mode_jump_p,
9179
                                             use_saved_addend_p))
9180
        {
9181
        case bfd_reloc_continue:
9182
          /* There's nothing to do.  */
9183
          continue;
9184
 
9185
        case bfd_reloc_undefined:
9186
          /* mips_elf_calculate_relocation already called the
9187
             undefined_symbol callback.  There's no real point in
9188
             trying to perform the relocation at this point, so we
9189
             just skip ahead to the next relocation.  */
9190
          continue;
9191
 
9192
        case bfd_reloc_notsupported:
9193
          msg = _("internal error: unsupported relocation error");
9194
          info->callbacks->warning
9195
            (info, msg, name, input_bfd, input_section, rel->r_offset);
9196
          return FALSE;
9197
 
9198
        case bfd_reloc_overflow:
9199
          if (use_saved_addend_p)
9200
            /* Ignore overflow until we reach the last relocation for
9201
               a given location.  */
9202
            ;
9203
          else
9204
            {
9205
              struct mips_elf_link_hash_table *htab;
9206
 
9207
              htab = mips_elf_hash_table (info);
9208
              BFD_ASSERT (htab != NULL);
9209
              BFD_ASSERT (name != NULL);
9210
              if (!htab->small_data_overflow_reported
9211
                  && (gprel16_reloc_p (howto->type)
9212
                      || howto->type == R_MIPS_LITERAL))
9213
                {
9214
                  msg = _("small-data section exceeds 64KB;"
9215
                          " lower small-data size limit (see option -G)");
9216
 
9217
                  htab->small_data_overflow_reported = TRUE;
9218
                  (*info->callbacks->einfo) ("%P: %s\n", msg);
9219
                }
9220
              if (! ((*info->callbacks->reloc_overflow)
9221
                     (info, NULL, name, howto->name, (bfd_vma) 0,
9222
                      input_bfd, input_section, rel->r_offset)))
9223
                return FALSE;
9224
            }
9225
          break;
9226
 
9227
        case bfd_reloc_ok:
9228
          break;
9229
 
9230
        default:
9231
          abort ();
9232
          break;
9233
        }
9234
 
9235
      /* If we've got another relocation for the address, keep going
9236
         until we reach the last one.  */
9237
      if (use_saved_addend_p)
9238
        {
9239
          addend = value;
9240
          continue;
9241
        }
9242
 
9243
      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9244
        /* See the comment above about using R_MIPS_64 in the 32-bit
9245
           ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
9246
           that calculated the right value.  Now, however, we
9247
           sign-extend the 32-bit result to 64-bits, and store it as a
9248
           64-bit value.  We are especially generous here in that we
9249
           go to extreme lengths to support this usage on systems with
9250
           only a 32-bit VMA.  */
9251
        {
9252
          bfd_vma sign_bits;
9253
          bfd_vma low_bits;
9254
          bfd_vma high_bits;
9255
 
9256
          if (value & ((bfd_vma) 1 << 31))
9257
#ifdef BFD64
9258
            sign_bits = ((bfd_vma) 1 << 32) - 1;
9259
#else
9260
            sign_bits = -1;
9261
#endif
9262
          else
9263
            sign_bits = 0;
9264
 
9265
          /* If we don't know that we have a 64-bit type,
9266
             do two separate stores.  */
9267
          if (bfd_big_endian (input_bfd))
9268
            {
9269
              /* Undo what we did above.  */
9270
              rel->r_offset -= 4;
9271
              /* Store the sign-bits (which are most significant)
9272
                 first.  */
9273
              low_bits = sign_bits;
9274
              high_bits = value;
9275
            }
9276
          else
9277
            {
9278
              low_bits = value;
9279
              high_bits = sign_bits;
9280
            }
9281
          bfd_put_32 (input_bfd, low_bits,
9282
                      contents + rel->r_offset);
9283
          bfd_put_32 (input_bfd, high_bits,
9284
                      contents + rel->r_offset + 4);
9285
          continue;
9286
        }
9287
 
9288
      /* Actually perform the relocation.  */
9289
      if (! mips_elf_perform_relocation (info, howto, rel, value,
9290
                                         input_bfd, input_section,
9291
                                         contents, cross_mode_jump_p))
9292
        return FALSE;
9293
    }
9294
 
9295
  return TRUE;
9296
}
9297
 
9298
/* A function that iterates over each entry in la25_stubs and fills
9299
   in the code for each one.  DATA points to a mips_htab_traverse_info.  */
9300
 
9301
static int
9302
mips_elf_create_la25_stub (void **slot, void *data)
9303
{
9304
  struct mips_htab_traverse_info *hti;
9305
  struct mips_elf_link_hash_table *htab;
9306
  struct mips_elf_la25_stub *stub;
9307
  asection *s;
9308
  bfd_byte *loc;
9309
  bfd_vma offset, target, target_high, target_low;
9310
 
9311
  stub = (struct mips_elf_la25_stub *) *slot;
9312
  hti = (struct mips_htab_traverse_info *) data;
9313
  htab = mips_elf_hash_table (hti->info);
9314
  BFD_ASSERT (htab != NULL);
9315
 
9316
  /* Create the section contents, if we haven't already.  */
9317
  s = stub->stub_section;
9318
  loc = s->contents;
9319
  if (loc == NULL)
9320
    {
9321
      loc = bfd_malloc (s->size);
9322
      if (loc == NULL)
9323
        {
9324
          hti->error = TRUE;
9325
          return FALSE;
9326
        }
9327
      s->contents = loc;
9328
    }
9329
 
9330
  /* Work out where in the section this stub should go.  */
9331
  offset = stub->offset;
9332
 
9333
  /* Work out the target address.  */
9334
  target = (stub->h->root.root.u.def.section->output_section->vma
9335
            + stub->h->root.root.u.def.section->output_offset
9336
            + stub->h->root.root.u.def.value);
9337
  target_high = ((target + 0x8000) >> 16) & 0xffff;
9338
  target_low = (target & 0xffff);
9339
 
9340
  if (stub->stub_section != htab->strampoline)
9341
    {
9342
      /* This is a simple LUI/ADIDU stub.  Zero out the beginning
9343
         of the section and write the two instructions at the end.  */
9344
      memset (loc, 0, offset);
9345
      loc += offset;
9346
      bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9347
      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9348
    }
9349
  else
9350
    {
9351
      /* This is trampoline.  */
9352
      loc += offset;
9353
      bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9354
      bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9355
      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9356
      bfd_put_32 (hti->output_bfd, 0, loc + 12);
9357
    }
9358
  return TRUE;
9359
}
9360
 
9361
/* If NAME is one of the special IRIX6 symbols defined by the linker,
9362
   adjust it appropriately now.  */
9363
 
9364
static void
9365
mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9366
                                      const char *name, Elf_Internal_Sym *sym)
9367
{
9368
  /* The linker script takes care of providing names and values for
9369
     these, but we must place them into the right sections.  */
9370
  static const char* const text_section_symbols[] = {
9371
    "_ftext",
9372
    "_etext",
9373
    "__dso_displacement",
9374
    "__elf_header",
9375
    "__program_header_table",
9376
    NULL
9377
  };
9378
 
9379
  static const char* const data_section_symbols[] = {
9380
    "_fdata",
9381
    "_edata",
9382
    "_end",
9383
    "_fbss",
9384
    NULL
9385
  };
9386
 
9387
  const char* const *p;
9388
  int i;
9389
 
9390
  for (i = 0; i < 2; ++i)
9391
    for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9392
         *p;
9393
         ++p)
9394
      if (strcmp (*p, name) == 0)
9395
        {
9396
          /* All of these symbols are given type STT_SECTION by the
9397
             IRIX6 linker.  */
9398
          sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9399
          sym->st_other = STO_PROTECTED;
9400
 
9401
          /* The IRIX linker puts these symbols in special sections.  */
9402
          if (i == 0)
9403
            sym->st_shndx = SHN_MIPS_TEXT;
9404
          else
9405
            sym->st_shndx = SHN_MIPS_DATA;
9406
 
9407
          break;
9408
        }
9409
}
9410
 
9411
/* Finish up dynamic symbol handling.  We set the contents of various
9412
   dynamic sections here.  */
9413
 
9414
bfd_boolean
9415
_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9416
                                     struct bfd_link_info *info,
9417
                                     struct elf_link_hash_entry *h,
9418
                                     Elf_Internal_Sym *sym)
9419
{
9420
  bfd *dynobj;
9421
  asection *sgot;
9422
  struct mips_got_info *g, *gg;
9423
  const char *name;
9424
  int idx;
9425
  struct mips_elf_link_hash_table *htab;
9426
  struct mips_elf_link_hash_entry *hmips;
9427
 
9428
  htab = mips_elf_hash_table (info);
9429
  BFD_ASSERT (htab != NULL);
9430
  dynobj = elf_hash_table (info)->dynobj;
9431
  hmips = (struct mips_elf_link_hash_entry *) h;
9432
 
9433
  BFD_ASSERT (!htab->is_vxworks);
9434
 
9435
  if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9436
    {
9437
      /* We've decided to create a PLT entry for this symbol.  */
9438
      bfd_byte *loc;
9439
      bfd_vma header_address, plt_index, got_address;
9440
      bfd_vma got_address_high, got_address_low, load;
9441
      const bfd_vma *plt_entry;
9442
 
9443
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
9444
      BFD_ASSERT (h->dynindx != -1);
9445
      BFD_ASSERT (htab->splt != NULL);
9446
      BFD_ASSERT (h->plt.offset <= htab->splt->size);
9447
      BFD_ASSERT (!h->def_regular);
9448
 
9449
      /* Calculate the address of the PLT header.  */
9450
      header_address = (htab->splt->output_section->vma
9451
                        + htab->splt->output_offset);
9452
 
9453
      /* Calculate the index of the entry.  */
9454
      plt_index = ((h->plt.offset - htab->plt_header_size)
9455
                   / htab->plt_entry_size);
9456
 
9457
      /* Calculate the address of the .got.plt entry.  */
9458
      got_address = (htab->sgotplt->output_section->vma
9459
                     + htab->sgotplt->output_offset
9460
                     + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9461
      got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9462
      got_address_low = got_address & 0xffff;
9463
 
9464
      /* Initially point the .got.plt entry at the PLT header.  */
9465
      loc = (htab->sgotplt->contents
9466
             + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9467
      if (ABI_64_P (output_bfd))
9468
        bfd_put_64 (output_bfd, header_address, loc);
9469
      else
9470
        bfd_put_32 (output_bfd, header_address, loc);
9471
 
9472
      /* Find out where the .plt entry should go.  */
9473
      loc = htab->splt->contents + h->plt.offset;
9474
 
9475
      /* Pick the load opcode.  */
9476
      load = MIPS_ELF_LOAD_WORD (output_bfd);
9477
 
9478
      /* Fill in the PLT entry itself.  */
9479
      plt_entry = mips_exec_plt_entry;
9480
      bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9481
      bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9482
 
9483
      if (! LOAD_INTERLOCKS_P (output_bfd))
9484
        {
9485
          bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9486
          bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9487
        }
9488
      else
9489
        {
9490
          bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9491
          bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9492
        }
9493
 
9494
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
9495
      mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9496
                                          plt_index, h->dynindx,
9497
                                          R_MIPS_JUMP_SLOT, got_address);
9498
 
9499
      /* We distinguish between PLT entries and lazy-binding stubs by
9500
         giving the former an st_other value of STO_MIPS_PLT.  Set the
9501
         flag and leave the value if there are any relocations in the
9502
         binary where pointer equality matters.  */
9503
      sym->st_shndx = SHN_UNDEF;
9504
      if (h->pointer_equality_needed)
9505
        sym->st_other = STO_MIPS_PLT;
9506
      else
9507
        sym->st_value = 0;
9508
    }
9509
  else if (h->plt.offset != MINUS_ONE)
9510
    {
9511
      /* We've decided to create a lazy-binding stub.  */
9512
      bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9513
 
9514
      /* This symbol has a stub.  Set it up.  */
9515
 
9516
      BFD_ASSERT (h->dynindx != -1);
9517
 
9518
      BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9519
                  || (h->dynindx <= 0xffff));
9520
 
9521
      /* Values up to 2^31 - 1 are allowed.  Larger values would cause
9522
         sign extension at runtime in the stub, resulting in a negative
9523
         index value.  */
9524
      if (h->dynindx & ~0x7fffffff)
9525
        return FALSE;
9526
 
9527
      /* Fill the stub.  */
9528
      idx = 0;
9529
      bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9530
      idx += 4;
9531
      bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9532
      idx += 4;
9533
      if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9534
        {
9535
          bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9536
                      stub + idx);
9537
          idx += 4;
9538
        }
9539
      bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9540
      idx += 4;
9541
 
9542
      /* If a large stub is not required and sign extension is not a
9543
         problem, then use legacy code in the stub.  */
9544
      if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9545
        bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9546
      else if (h->dynindx & ~0x7fff)
9547
        bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9548
      else
9549
        bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9550
                    stub + idx);
9551
 
9552
      BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9553
      memcpy (htab->sstubs->contents + h->plt.offset,
9554
              stub, htab->function_stub_size);
9555
 
9556
      /* Mark the symbol as undefined.  plt.offset != -1 occurs
9557
         only for the referenced symbol.  */
9558
      sym->st_shndx = SHN_UNDEF;
9559
 
9560
      /* The run-time linker uses the st_value field of the symbol
9561
         to reset the global offset table entry for this external
9562
         to its stub address when unlinking a shared object.  */
9563
      sym->st_value = (htab->sstubs->output_section->vma
9564
                       + htab->sstubs->output_offset
9565
                       + h->plt.offset);
9566
    }
9567
 
9568
  /* If we have a MIPS16 function with a stub, the dynamic symbol must
9569
     refer to the stub, since only the stub uses the standard calling
9570
     conventions.  */
9571
  if (h->dynindx != -1 && hmips->fn_stub != NULL)
9572
    {
9573
      BFD_ASSERT (hmips->need_fn_stub);
9574
      sym->st_value = (hmips->fn_stub->output_section->vma
9575
                       + hmips->fn_stub->output_offset);
9576
      sym->st_size = hmips->fn_stub->size;
9577
      sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9578
    }
9579
 
9580
  BFD_ASSERT (h->dynindx != -1
9581
              || h->forced_local);
9582
 
9583
  sgot = htab->sgot;
9584
  g = htab->got_info;
9585
  BFD_ASSERT (g != NULL);
9586
 
9587
  /* Run through the global symbol table, creating GOT entries for all
9588
     the symbols that need them.  */
9589
  if (hmips->global_got_area != GGA_NONE)
9590
    {
9591
      bfd_vma offset;
9592
      bfd_vma value;
9593
 
9594
      value = sym->st_value;
9595
      offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9596
                                          R_MIPS_GOT16, info);
9597
      MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9598
    }
9599
 
9600
  if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
9601
    {
9602
      struct mips_got_entry e, *p;
9603
      bfd_vma entry;
9604
      bfd_vma offset;
9605
 
9606
      gg = g;
9607
 
9608
      e.abfd = output_bfd;
9609
      e.symndx = -1;
9610
      e.d.h = hmips;
9611
      e.tls_type = 0;
9612
 
9613
      for (g = g->next; g->next != gg; g = g->next)
9614
        {
9615
          if (g->got_entries
9616
              && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9617
                                                           &e)))
9618
            {
9619
              offset = p->gotidx;
9620
              if (info->shared
9621
                  || (elf_hash_table (info)->dynamic_sections_created
9622
                      && p->d.h != NULL
9623
                      && p->d.h->root.def_dynamic
9624
                      && !p->d.h->root.def_regular))
9625
                {
9626
                  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
9627
                     the various compatibility problems, it's easier to mock
9628
                     up an R_MIPS_32 or R_MIPS_64 relocation and leave
9629
                     mips_elf_create_dynamic_relocation to calculate the
9630
                     appropriate addend.  */
9631
                  Elf_Internal_Rela rel[3];
9632
 
9633
                  memset (rel, 0, sizeof (rel));
9634
                  if (ABI_64_P (output_bfd))
9635
                    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9636
                  else
9637
                    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9638
                  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9639
 
9640
                  entry = 0;
9641
                  if (! (mips_elf_create_dynamic_relocation
9642
                         (output_bfd, info, rel,
9643
                          e.d.h, NULL, sym->st_value, &entry, sgot)))
9644
                    return FALSE;
9645
                }
9646
              else
9647
                entry = sym->st_value;
9648
              MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
9649
            }
9650
        }
9651
    }
9652
 
9653
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
9654
  name = h->root.root.string;
9655
  if (strcmp (name, "_DYNAMIC") == 0
9656
      || h == elf_hash_table (info)->hgot)
9657
    sym->st_shndx = SHN_ABS;
9658
  else if (strcmp (name, "_DYNAMIC_LINK") == 0
9659
           || strcmp (name, "_DYNAMIC_LINKING") == 0)
9660
    {
9661
      sym->st_shndx = SHN_ABS;
9662
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9663
      sym->st_value = 1;
9664
    }
9665
  else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
9666
    {
9667
      sym->st_shndx = SHN_ABS;
9668
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9669
      sym->st_value = elf_gp (output_bfd);
9670
    }
9671
  else if (SGI_COMPAT (output_bfd))
9672
    {
9673
      if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9674
          || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9675
        {
9676
          sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9677
          sym->st_other = STO_PROTECTED;
9678
          sym->st_value = 0;
9679
          sym->st_shndx = SHN_MIPS_DATA;
9680
        }
9681
      else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9682
        {
9683
          sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9684
          sym->st_other = STO_PROTECTED;
9685
          sym->st_value = mips_elf_hash_table (info)->procedure_count;
9686
          sym->st_shndx = SHN_ABS;
9687
        }
9688
      else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9689
        {
9690
          if (h->type == STT_FUNC)
9691
            sym->st_shndx = SHN_MIPS_TEXT;
9692
          else if (h->type == STT_OBJECT)
9693
            sym->st_shndx = SHN_MIPS_DATA;
9694
        }
9695
    }
9696
 
9697
  /* Emit a copy reloc, if needed.  */
9698
  if (h->needs_copy)
9699
    {
9700
      asection *s;
9701
      bfd_vma symval;
9702
 
9703
      BFD_ASSERT (h->dynindx != -1);
9704
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
9705
 
9706
      s = mips_elf_rel_dyn_section (info, FALSE);
9707
      symval = (h->root.u.def.section->output_section->vma
9708
                + h->root.u.def.section->output_offset
9709
                + h->root.u.def.value);
9710
      mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9711
                                          h->dynindx, R_MIPS_COPY, symval);
9712
    }
9713
 
9714
  /* Handle the IRIX6-specific symbols.  */
9715
  if (IRIX_COMPAT (output_bfd) == ict_irix6)
9716
    mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9717
 
9718
  if (! info->shared)
9719
    {
9720
      if (! mips_elf_hash_table (info)->use_rld_obj_head
9721
          && (strcmp (name, "__rld_map") == 0
9722
              || strcmp (name, "__RLD_MAP") == 0))
9723
        {
9724
          asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9725
          BFD_ASSERT (s != NULL);
9726
          sym->st_value = s->output_section->vma + s->output_offset;
9727
          bfd_put_32 (output_bfd, 0, s->contents);
9728
          if (mips_elf_hash_table (info)->rld_value == 0)
9729
            mips_elf_hash_table (info)->rld_value = sym->st_value;
9730
        }
9731
      else if (mips_elf_hash_table (info)->use_rld_obj_head
9732
               && strcmp (name, "__rld_obj_head") == 0)
9733
        {
9734
          /* IRIX6 does not use a .rld_map section.  */
9735
          if (IRIX_COMPAT (output_bfd) == ict_irix5
9736
              || IRIX_COMPAT (output_bfd) == ict_none)
9737
            BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9738
                        != NULL);
9739
          mips_elf_hash_table (info)->rld_value = sym->st_value;
9740
        }
9741
    }
9742
 
9743
  /* Keep dynamic MIPS16 symbols odd.  This allows the dynamic linker to
9744
     treat MIPS16 symbols like any other.  */
9745
  if (ELF_ST_IS_MIPS16 (sym->st_other))
9746
    {
9747
      BFD_ASSERT (sym->st_value & 1);
9748
      sym->st_other -= STO_MIPS16;
9749
    }
9750
 
9751
  return TRUE;
9752
}
9753
 
9754
/* Likewise, for VxWorks.  */
9755
 
9756
bfd_boolean
9757
_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9758
                                         struct bfd_link_info *info,
9759
                                         struct elf_link_hash_entry *h,
9760
                                         Elf_Internal_Sym *sym)
9761
{
9762
  bfd *dynobj;
9763
  asection *sgot;
9764
  struct mips_got_info *g;
9765
  struct mips_elf_link_hash_table *htab;
9766
  struct mips_elf_link_hash_entry *hmips;
9767
 
9768
  htab = mips_elf_hash_table (info);
9769
  BFD_ASSERT (htab != NULL);
9770
  dynobj = elf_hash_table (info)->dynobj;
9771
  hmips = (struct mips_elf_link_hash_entry *) h;
9772
 
9773
  if (h->plt.offset != (bfd_vma) -1)
9774
    {
9775
      bfd_byte *loc;
9776
      bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9777
      Elf_Internal_Rela rel;
9778
      static const bfd_vma *plt_entry;
9779
 
9780
      BFD_ASSERT (h->dynindx != -1);
9781
      BFD_ASSERT (htab->splt != NULL);
9782
      BFD_ASSERT (h->plt.offset <= htab->splt->size);
9783
 
9784
      /* Calculate the address of the .plt entry.  */
9785
      plt_address = (htab->splt->output_section->vma
9786
                     + htab->splt->output_offset
9787
                     + h->plt.offset);
9788
 
9789
      /* Calculate the index of the entry.  */
9790
      plt_index = ((h->plt.offset - htab->plt_header_size)
9791
                   / htab->plt_entry_size);
9792
 
9793
      /* Calculate the address of the .got.plt entry.  */
9794
      got_address = (htab->sgotplt->output_section->vma
9795
                     + htab->sgotplt->output_offset
9796
                     + plt_index * 4);
9797
 
9798
      /* Calculate the offset of the .got.plt entry from
9799
         _GLOBAL_OFFSET_TABLE_.  */
9800
      got_offset = mips_elf_gotplt_index (info, h);
9801
 
9802
      /* Calculate the offset for the branch at the start of the PLT
9803
         entry.  The branch jumps to the beginning of .plt.  */
9804
      branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9805
 
9806
      /* Fill in the initial value of the .got.plt entry.  */
9807
      bfd_put_32 (output_bfd, plt_address,
9808
                  htab->sgotplt->contents + plt_index * 4);
9809
 
9810
      /* Find out where the .plt entry should go.  */
9811
      loc = htab->splt->contents + h->plt.offset;
9812
 
9813
      if (info->shared)
9814
        {
9815
          plt_entry = mips_vxworks_shared_plt_entry;
9816
          bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9817
          bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9818
        }
9819
      else
9820
        {
9821
          bfd_vma got_address_high, got_address_low;
9822
 
9823
          plt_entry = mips_vxworks_exec_plt_entry;
9824
          got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9825
          got_address_low = got_address & 0xffff;
9826
 
9827
          bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9828
          bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9829
          bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9830
          bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9831
          bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9832
          bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9833
          bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9834
          bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9835
 
9836
          loc = (htab->srelplt2->contents
9837
                 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9838
 
9839
          /* Emit a relocation for the .got.plt entry.  */
9840
          rel.r_offset = got_address;
9841
          rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9842
          rel.r_addend = h->plt.offset;
9843
          bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9844
 
9845
          /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
9846
          loc += sizeof (Elf32_External_Rela);
9847
          rel.r_offset = plt_address + 8;
9848
          rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9849
          rel.r_addend = got_offset;
9850
          bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9851
 
9852
          /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
9853
          loc += sizeof (Elf32_External_Rela);
9854
          rel.r_offset += 4;
9855
          rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9856
          bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9857
        }
9858
 
9859
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
9860
      loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9861
      rel.r_offset = got_address;
9862
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9863
      rel.r_addend = 0;
9864
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9865
 
9866
      if (!h->def_regular)
9867
        sym->st_shndx = SHN_UNDEF;
9868
    }
9869
 
9870
  BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9871
 
9872
  sgot = htab->sgot;
9873
  g = htab->got_info;
9874
  BFD_ASSERT (g != NULL);
9875
 
9876
  /* See if this symbol has an entry in the GOT.  */
9877
  if (hmips->global_got_area != GGA_NONE)
9878
    {
9879
      bfd_vma offset;
9880
      Elf_Internal_Rela outrel;
9881
      bfd_byte *loc;
9882
      asection *s;
9883
 
9884
      /* Install the symbol value in the GOT.   */
9885
      offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9886
                                          R_MIPS_GOT16, info);
9887
      MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9888
 
9889
      /* Add a dynamic relocation for it.  */
9890
      s = mips_elf_rel_dyn_section (info, FALSE);
9891
      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9892
      outrel.r_offset = (sgot->output_section->vma
9893
                         + sgot->output_offset
9894
                         + offset);
9895
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9896
      outrel.r_addend = 0;
9897
      bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9898
    }
9899
 
9900
  /* Emit a copy reloc, if needed.  */
9901
  if (h->needs_copy)
9902
    {
9903
      Elf_Internal_Rela rel;
9904
 
9905
      BFD_ASSERT (h->dynindx != -1);
9906
 
9907
      rel.r_offset = (h->root.u.def.section->output_section->vma
9908
                      + h->root.u.def.section->output_offset
9909
                      + h->root.u.def.value);
9910
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9911
      rel.r_addend = 0;
9912
      bfd_elf32_swap_reloca_out (output_bfd, &rel,
9913
                                 htab->srelbss->contents
9914
                                 + (htab->srelbss->reloc_count
9915
                                    * sizeof (Elf32_External_Rela)));
9916
      ++htab->srelbss->reloc_count;
9917
    }
9918
 
9919
  /* If this is a mips16 symbol, force the value to be even.  */
9920
  if (ELF_ST_IS_MIPS16 (sym->st_other))
9921
    sym->st_value &= ~1;
9922
 
9923
  return TRUE;
9924
}
9925
 
9926
/* Write out a plt0 entry to the beginning of .plt.  */
9927
 
9928
static void
9929
mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9930
{
9931
  bfd_byte *loc;
9932
  bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9933
  static const bfd_vma *plt_entry;
9934
  struct mips_elf_link_hash_table *htab;
9935
 
9936
  htab = mips_elf_hash_table (info);
9937
  BFD_ASSERT (htab != NULL);
9938
 
9939
  if (ABI_64_P (output_bfd))
9940
    plt_entry = mips_n64_exec_plt0_entry;
9941
  else if (ABI_N32_P (output_bfd))
9942
    plt_entry = mips_n32_exec_plt0_entry;
9943
  else
9944
    plt_entry = mips_o32_exec_plt0_entry;
9945
 
9946
  /* Calculate the value of .got.plt.  */
9947
  gotplt_value = (htab->sgotplt->output_section->vma
9948
                  + htab->sgotplt->output_offset);
9949
  gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9950
  gotplt_value_low = gotplt_value & 0xffff;
9951
 
9952
  /* The PLT sequence is not safe for N64 if .got.plt's address can
9953
     not be loaded in two instructions.  */
9954
  BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9955
              || ~(gotplt_value | 0x7fffffff) == 0);
9956
 
9957
  /* Install the PLT header.  */
9958
  loc = htab->splt->contents;
9959
  bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9960
  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9961
  bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9962
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9963
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9964
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9965
  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9966
  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9967
}
9968
 
9969
/* Install the PLT header for a VxWorks executable and finalize the
9970
   contents of .rela.plt.unloaded.  */
9971
 
9972
static void
9973
mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9974
{
9975
  Elf_Internal_Rela rela;
9976
  bfd_byte *loc;
9977
  bfd_vma got_value, got_value_high, got_value_low, plt_address;
9978
  static const bfd_vma *plt_entry;
9979
  struct mips_elf_link_hash_table *htab;
9980
 
9981
  htab = mips_elf_hash_table (info);
9982
  BFD_ASSERT (htab != NULL);
9983
 
9984
  plt_entry = mips_vxworks_exec_plt0_entry;
9985
 
9986
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
9987
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9988
               + htab->root.hgot->root.u.def.section->output_offset
9989
               + htab->root.hgot->root.u.def.value);
9990
 
9991
  got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9992
  got_value_low = got_value & 0xffff;
9993
 
9994
  /* Calculate the address of the PLT header.  */
9995
  plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9996
 
9997
  /* Install the PLT header.  */
9998
  loc = htab->splt->contents;
9999
  bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
10000
  bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
10001
  bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10002
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10003
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10004
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10005
 
10006
  /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
10007
  loc = htab->srelplt2->contents;
10008
  rela.r_offset = plt_address;
10009
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10010
  rela.r_addend = 0;
10011
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10012
  loc += sizeof (Elf32_External_Rela);
10013
 
10014
  /* Output the relocation for the following addiu of
10015
     %lo(_GLOBAL_OFFSET_TABLE_).  */
10016
  rela.r_offset += 4;
10017
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10018
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10019
  loc += sizeof (Elf32_External_Rela);
10020
 
10021
  /* Fix up the remaining relocations.  They may have the wrong
10022
     symbol index for _G_O_T_ or _P_L_T_ depending on the order
10023
     in which symbols were output.  */
10024
  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10025
    {
10026
      Elf_Internal_Rela rel;
10027
 
10028
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10029
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10030
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10031
      loc += sizeof (Elf32_External_Rela);
10032
 
10033
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10034
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10035
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10036
      loc += sizeof (Elf32_External_Rela);
10037
 
10038
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10039
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10040
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10041
      loc += sizeof (Elf32_External_Rela);
10042
    }
10043
}
10044
 
10045
/* Install the PLT header for a VxWorks shared library.  */
10046
 
10047
static void
10048
mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10049
{
10050
  unsigned int i;
10051
  struct mips_elf_link_hash_table *htab;
10052
 
10053
  htab = mips_elf_hash_table (info);
10054
  BFD_ASSERT (htab != NULL);
10055
 
10056
  /* We just need to copy the entry byte-by-byte.  */
10057
  for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10058
    bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10059
                htab->splt->contents + i * 4);
10060
}
10061
 
10062
/* Finish up the dynamic sections.  */
10063
 
10064
bfd_boolean
10065
_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10066
                                       struct bfd_link_info *info)
10067
{
10068
  bfd *dynobj;
10069
  asection *sdyn;
10070
  asection *sgot;
10071
  struct mips_got_info *gg, *g;
10072
  struct mips_elf_link_hash_table *htab;
10073
 
10074
  htab = mips_elf_hash_table (info);
10075
  BFD_ASSERT (htab != NULL);
10076
 
10077
  dynobj = elf_hash_table (info)->dynobj;
10078
 
10079
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10080
 
10081
  sgot = htab->sgot;
10082
  gg = htab->got_info;
10083
 
10084
  if (elf_hash_table (info)->dynamic_sections_created)
10085
    {
10086
      bfd_byte *b;
10087
      int dyn_to_skip = 0, dyn_skipped = 0;
10088
 
10089
      BFD_ASSERT (sdyn != NULL);
10090
      BFD_ASSERT (gg != NULL);
10091
 
10092
      g = mips_elf_got_for_ibfd (gg, output_bfd);
10093
      BFD_ASSERT (g != NULL);
10094
 
10095
      for (b = sdyn->contents;
10096
           b < sdyn->contents + sdyn->size;
10097
           b += MIPS_ELF_DYN_SIZE (dynobj))
10098
        {
10099
          Elf_Internal_Dyn dyn;
10100
          const char *name;
10101
          size_t elemsize;
10102
          asection *s;
10103
          bfd_boolean swap_out_p;
10104
 
10105
          /* Read in the current dynamic entry.  */
10106
          (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10107
 
10108
          /* Assume that we're going to modify it and write it out.  */
10109
          swap_out_p = TRUE;
10110
 
10111
          switch (dyn.d_tag)
10112
            {
10113
            case DT_RELENT:
10114
              dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10115
              break;
10116
 
10117
            case DT_RELAENT:
10118
              BFD_ASSERT (htab->is_vxworks);
10119
              dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10120
              break;
10121
 
10122
            case DT_STRSZ:
10123
              /* Rewrite DT_STRSZ.  */
10124
              dyn.d_un.d_val =
10125
                _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10126
              break;
10127
 
10128
            case DT_PLTGOT:
10129
              s = htab->sgot;
10130
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10131
              break;
10132
 
10133
            case DT_MIPS_PLTGOT:
10134
              s = htab->sgotplt;
10135
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10136
              break;
10137
 
10138
            case DT_MIPS_RLD_VERSION:
10139
              dyn.d_un.d_val = 1; /* XXX */
10140
              break;
10141
 
10142
            case DT_MIPS_FLAGS:
10143
              dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10144
              break;
10145
 
10146
            case DT_MIPS_TIME_STAMP:
10147
              {
10148
                time_t t;
10149
                time (&t);
10150
                dyn.d_un.d_val = t;
10151
              }
10152
              break;
10153
 
10154
            case DT_MIPS_ICHECKSUM:
10155
              /* XXX FIXME: */
10156
              swap_out_p = FALSE;
10157
              break;
10158
 
10159
            case DT_MIPS_IVERSION:
10160
              /* XXX FIXME: */
10161
              swap_out_p = FALSE;
10162
              break;
10163
 
10164
            case DT_MIPS_BASE_ADDRESS:
10165
              s = output_bfd->sections;
10166
              BFD_ASSERT (s != NULL);
10167
              dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10168
              break;
10169
 
10170
            case DT_MIPS_LOCAL_GOTNO:
10171
              dyn.d_un.d_val = g->local_gotno;
10172
              break;
10173
 
10174
            case DT_MIPS_UNREFEXTNO:
10175
              /* The index into the dynamic symbol table which is the
10176
                 entry of the first external symbol that is not
10177
                 referenced within the same object.  */
10178
              dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10179
              break;
10180
 
10181
            case DT_MIPS_GOTSYM:
10182
              if (gg->global_gotsym)
10183
                {
10184
                  dyn.d_un.d_val = gg->global_gotsym->dynindx;
10185
                  break;
10186
                }
10187
              /* In case if we don't have global got symbols we default
10188
                 to setting DT_MIPS_GOTSYM to the same value as
10189
                 DT_MIPS_SYMTABNO, so we just fall through.  */
10190
 
10191
            case DT_MIPS_SYMTABNO:
10192
              name = ".dynsym";
10193
              elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10194
              s = bfd_get_section_by_name (output_bfd, name);
10195
              BFD_ASSERT (s != NULL);
10196
 
10197
              dyn.d_un.d_val = s->size / elemsize;
10198
              break;
10199
 
10200
            case DT_MIPS_HIPAGENO:
10201
              dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10202
              break;
10203
 
10204
            case DT_MIPS_RLD_MAP:
10205
              dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10206
              break;
10207
 
10208
            case DT_MIPS_OPTIONS:
10209
              s = (bfd_get_section_by_name
10210
                   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10211
              dyn.d_un.d_ptr = s->vma;
10212
              break;
10213
 
10214
            case DT_RELASZ:
10215
              BFD_ASSERT (htab->is_vxworks);
10216
              /* The count does not include the JUMP_SLOT relocations.  */
10217
              if (htab->srelplt)
10218
                dyn.d_un.d_val -= htab->srelplt->size;
10219
              break;
10220
 
10221
            case DT_PLTREL:
10222
              BFD_ASSERT (htab->use_plts_and_copy_relocs);
10223
              if (htab->is_vxworks)
10224
                dyn.d_un.d_val = DT_RELA;
10225
              else
10226
                dyn.d_un.d_val = DT_REL;
10227
              break;
10228
 
10229
            case DT_PLTRELSZ:
10230
              BFD_ASSERT (htab->use_plts_and_copy_relocs);
10231
              dyn.d_un.d_val = htab->srelplt->size;
10232
              break;
10233
 
10234
            case DT_JMPREL:
10235
              BFD_ASSERT (htab->use_plts_and_copy_relocs);
10236
              dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10237
                                + htab->srelplt->output_offset);
10238
              break;
10239
 
10240
            case DT_TEXTREL:
10241
              /* If we didn't need any text relocations after all, delete
10242
                 the dynamic tag.  */
10243
              if (!(info->flags & DF_TEXTREL))
10244
                {
10245
                  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10246
                  swap_out_p = FALSE;
10247
                }
10248
              break;
10249
 
10250
            case DT_FLAGS:
10251
              /* If we didn't need any text relocations after all, clear
10252
                 DF_TEXTREL from DT_FLAGS.  */
10253
              if (!(info->flags & DF_TEXTREL))
10254
                dyn.d_un.d_val &= ~DF_TEXTREL;
10255
              else
10256
                swap_out_p = FALSE;
10257
              break;
10258
 
10259
            default:
10260
              swap_out_p = FALSE;
10261
              if (htab->is_vxworks
10262
                  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10263
                swap_out_p = TRUE;
10264
              break;
10265
            }
10266
 
10267
          if (swap_out_p || dyn_skipped)
10268
            (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10269
              (dynobj, &dyn, b - dyn_skipped);
10270
 
10271
          if (dyn_to_skip)
10272
            {
10273
              dyn_skipped += dyn_to_skip;
10274
              dyn_to_skip = 0;
10275
            }
10276
        }
10277
 
10278
      /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
10279
      if (dyn_skipped > 0)
10280
        memset (b - dyn_skipped, 0, dyn_skipped);
10281
    }
10282
 
10283
  if (sgot != NULL && sgot->size > 0
10284
      && !bfd_is_abs_section (sgot->output_section))
10285
    {
10286
      if (htab->is_vxworks)
10287
        {
10288
          /* The first entry of the global offset table points to the
10289
             ".dynamic" section.  The second is initialized by the
10290
             loader and contains the shared library identifier.
10291
             The third is also initialized by the loader and points
10292
             to the lazy resolution stub.  */
10293
          MIPS_ELF_PUT_WORD (output_bfd,
10294
                             sdyn->output_offset + sdyn->output_section->vma,
10295
                             sgot->contents);
10296
          MIPS_ELF_PUT_WORD (output_bfd, 0,
10297
                             sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10298
          MIPS_ELF_PUT_WORD (output_bfd, 0,
10299
                             sgot->contents
10300
                             + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10301
        }
10302
      else
10303
        {
10304
          /* The first entry of the global offset table will be filled at
10305
             runtime. The second entry will be used by some runtime loaders.
10306
             This isn't the case of IRIX rld.  */
10307
          MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10308
          MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10309
                             sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10310
        }
10311
 
10312
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10313
         = MIPS_ELF_GOT_SIZE (output_bfd);
10314
    }
10315
 
10316
  /* Generate dynamic relocations for the non-primary gots.  */
10317
  if (gg != NULL && gg->next)
10318
    {
10319
      Elf_Internal_Rela rel[3];
10320
      bfd_vma addend = 0;
10321
 
10322
      memset (rel, 0, sizeof (rel));
10323
      rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10324
 
10325
      for (g = gg->next; g->next != gg; g = g->next)
10326
        {
10327
          bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
10328
            + g->next->tls_gotno;
10329
 
10330
          MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10331
                             + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10332
          MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10333
                             sgot->contents
10334
                             + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10335
 
10336
          if (! info->shared)
10337
            continue;
10338
 
10339
          while (got_index < g->assigned_gotno)
10340
            {
10341
              rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10342
                = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10343
              if (!(mips_elf_create_dynamic_relocation
10344
                    (output_bfd, info, rel, NULL,
10345
                     bfd_abs_section_ptr,
10346
                     0, &addend, sgot)))
10347
                return FALSE;
10348
              BFD_ASSERT (addend == 0);
10349
            }
10350
        }
10351
    }
10352
 
10353
  /* The generation of dynamic relocations for the non-primary gots
10354
     adds more dynamic relocations.  We cannot count them until
10355
     here.  */
10356
 
10357
  if (elf_hash_table (info)->dynamic_sections_created)
10358
    {
10359
      bfd_byte *b;
10360
      bfd_boolean swap_out_p;
10361
 
10362
      BFD_ASSERT (sdyn != NULL);
10363
 
10364
      for (b = sdyn->contents;
10365
           b < sdyn->contents + sdyn->size;
10366
           b += MIPS_ELF_DYN_SIZE (dynobj))
10367
        {
10368
          Elf_Internal_Dyn dyn;
10369
          asection *s;
10370
 
10371
          /* Read in the current dynamic entry.  */
10372
          (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10373
 
10374
          /* Assume that we're going to modify it and write it out.  */
10375
          swap_out_p = TRUE;
10376
 
10377
          switch (dyn.d_tag)
10378
            {
10379
            case DT_RELSZ:
10380
              /* Reduce DT_RELSZ to account for any relocations we
10381
                 decided not to make.  This is for the n64 irix rld,
10382
                 which doesn't seem to apply any relocations if there
10383
                 are trailing null entries.  */
10384
              s = mips_elf_rel_dyn_section (info, FALSE);
10385
              dyn.d_un.d_val = (s->reloc_count
10386
                                * (ABI_64_P (output_bfd)
10387
                                   ? sizeof (Elf64_Mips_External_Rel)
10388
                                   : sizeof (Elf32_External_Rel)));
10389
              /* Adjust the section size too.  Tools like the prelinker
10390
                 can reasonably expect the values to the same.  */
10391
              elf_section_data (s->output_section)->this_hdr.sh_size
10392
                = dyn.d_un.d_val;
10393
              break;
10394
 
10395
            default:
10396
              swap_out_p = FALSE;
10397
              break;
10398
            }
10399
 
10400
          if (swap_out_p)
10401
            (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10402
              (dynobj, &dyn, b);
10403
        }
10404
    }
10405
 
10406
  {
10407
    asection *s;
10408
    Elf32_compact_rel cpt;
10409
 
10410
    if (SGI_COMPAT (output_bfd))
10411
      {
10412
        /* Write .compact_rel section out.  */
10413
        s = bfd_get_section_by_name (dynobj, ".compact_rel");
10414
        if (s != NULL)
10415
          {
10416
            cpt.id1 = 1;
10417
            cpt.num = s->reloc_count;
10418
            cpt.id2 = 2;
10419
            cpt.offset = (s->output_section->filepos
10420
                          + sizeof (Elf32_External_compact_rel));
10421
            cpt.reserved0 = 0;
10422
            cpt.reserved1 = 0;
10423
            bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10424
                                            ((Elf32_External_compact_rel *)
10425
                                             s->contents));
10426
 
10427
            /* Clean up a dummy stub function entry in .text.  */
10428
            if (htab->sstubs != NULL)
10429
              {
10430
                file_ptr dummy_offset;
10431
 
10432
                BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10433
                dummy_offset = htab->sstubs->size - htab->function_stub_size;
10434
                memset (htab->sstubs->contents + dummy_offset, 0,
10435
                        htab->function_stub_size);
10436
              }
10437
          }
10438
      }
10439
 
10440
    /* The psABI says that the dynamic relocations must be sorted in
10441
       increasing order of r_symndx.  The VxWorks EABI doesn't require
10442
       this, and because the code below handles REL rather than RELA
10443
       relocations, using it for VxWorks would be outright harmful.  */
10444
    if (!htab->is_vxworks)
10445
      {
10446
        s = mips_elf_rel_dyn_section (info, FALSE);
10447
        if (s != NULL
10448
            && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10449
          {
10450
            reldyn_sorting_bfd = output_bfd;
10451
 
10452
            if (ABI_64_P (output_bfd))
10453
              qsort ((Elf64_External_Rel *) s->contents + 1,
10454
                     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10455
                     sort_dynamic_relocs_64);
10456
            else
10457
              qsort ((Elf32_External_Rel *) s->contents + 1,
10458
                     s->reloc_count - 1, sizeof (Elf32_External_Rel),
10459
                     sort_dynamic_relocs);
10460
          }
10461
      }
10462
  }
10463
 
10464
  if (htab->splt && htab->splt->size > 0)
10465
    {
10466
      if (htab->is_vxworks)
10467
        {
10468
          if (info->shared)
10469
            mips_vxworks_finish_shared_plt (output_bfd, info);
10470
          else
10471
            mips_vxworks_finish_exec_plt (output_bfd, info);
10472
        }
10473
      else
10474
        {
10475
          BFD_ASSERT (!info->shared);
10476
          mips_finish_exec_plt (output_bfd, info);
10477
        }
10478
    }
10479
  return TRUE;
10480
}
10481
 
10482
 
10483
/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
10484
 
10485
static void
10486
mips_set_isa_flags (bfd *abfd)
10487
{
10488
  flagword val;
10489
 
10490
  switch (bfd_get_mach (abfd))
10491
    {
10492
    default:
10493
    case bfd_mach_mips3000:
10494
      val = E_MIPS_ARCH_1;
10495
      break;
10496
 
10497
    case bfd_mach_mips3900:
10498
      val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10499
      break;
10500
 
10501
    case bfd_mach_mips6000:
10502
      val = E_MIPS_ARCH_2;
10503
      break;
10504
 
10505
    case bfd_mach_mips4000:
10506
    case bfd_mach_mips4300:
10507
    case bfd_mach_mips4400:
10508
    case bfd_mach_mips4600:
10509
      val = E_MIPS_ARCH_3;
10510
      break;
10511
 
10512
    case bfd_mach_mips4010:
10513
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10514
      break;
10515
 
10516
    case bfd_mach_mips4100:
10517
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10518
      break;
10519
 
10520
    case bfd_mach_mips4111:
10521
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10522
      break;
10523
 
10524
    case bfd_mach_mips4120:
10525
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10526
      break;
10527
 
10528
    case bfd_mach_mips4650:
10529
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10530
      break;
10531
 
10532
    case bfd_mach_mips5400:
10533
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10534
      break;
10535
 
10536
    case bfd_mach_mips5500:
10537
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10538
      break;
10539
 
10540
    case bfd_mach_mips9000:
10541
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10542
      break;
10543
 
10544
    case bfd_mach_mips5000:
10545
    case bfd_mach_mips7000:
10546
    case bfd_mach_mips8000:
10547
    case bfd_mach_mips10000:
10548
    case bfd_mach_mips12000:
10549
    case bfd_mach_mips14000:
10550
    case bfd_mach_mips16000:
10551
      val = E_MIPS_ARCH_4;
10552
      break;
10553
 
10554
    case bfd_mach_mips5:
10555
      val = E_MIPS_ARCH_5;
10556
      break;
10557
 
10558
    case bfd_mach_mips_loongson_2e:
10559
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10560
      break;
10561
 
10562
    case bfd_mach_mips_loongson_2f:
10563
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10564
      break;
10565
 
10566
    case bfd_mach_mips_sb1:
10567
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10568
      break;
10569
 
10570
    case bfd_mach_mips_loongson_3a:
10571
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
10572
      break;
10573
 
10574
    case bfd_mach_mips_octeon:
10575
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10576
      break;
10577
 
10578
    case bfd_mach_mips_xlr:
10579
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10580
      break;
10581
 
10582
    case bfd_mach_mipsisa32:
10583
      val = E_MIPS_ARCH_32;
10584
      break;
10585
 
10586
    case bfd_mach_mipsisa64:
10587
      val = E_MIPS_ARCH_64;
10588
      break;
10589
 
10590
    case bfd_mach_mipsisa32r2:
10591
      val = E_MIPS_ARCH_32R2;
10592
      break;
10593
 
10594
    case bfd_mach_mipsisa64r2:
10595
      val = E_MIPS_ARCH_64R2;
10596
      break;
10597
    }
10598
  elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10599
  elf_elfheader (abfd)->e_flags |= val;
10600
 
10601
}
10602
 
10603
 
10604
/* The final processing done just before writing out a MIPS ELF object
10605
   file.  This gets the MIPS architecture right based on the machine
10606
   number.  This is used by both the 32-bit and the 64-bit ABI.  */
10607
 
10608
void
10609
_bfd_mips_elf_final_write_processing (bfd *abfd,
10610
                                      bfd_boolean linker ATTRIBUTE_UNUSED)
10611
{
10612
  unsigned int i;
10613
  Elf_Internal_Shdr **hdrpp;
10614
  const char *name;
10615
  asection *sec;
10616
 
10617
  /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10618
     is nonzero.  This is for compatibility with old objects, which used
10619
     a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
10620
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10621
    mips_set_isa_flags (abfd);
10622
 
10623
  /* Set the sh_info field for .gptab sections and other appropriate
10624
     info for each special section.  */
10625
  for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10626
       i < elf_numsections (abfd);
10627
       i++, hdrpp++)
10628
    {
10629
      switch ((*hdrpp)->sh_type)
10630
        {
10631
        case SHT_MIPS_MSYM:
10632
        case SHT_MIPS_LIBLIST:
10633
          sec = bfd_get_section_by_name (abfd, ".dynstr");
10634
          if (sec != NULL)
10635
            (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10636
          break;
10637
 
10638
        case SHT_MIPS_GPTAB:
10639
          BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10640
          name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10641
          BFD_ASSERT (name != NULL
10642
                      && CONST_STRNEQ (name, ".gptab."));
10643
          sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10644
          BFD_ASSERT (sec != NULL);
10645
          (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10646
          break;
10647
 
10648
        case SHT_MIPS_CONTENT:
10649
          BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10650
          name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10651
          BFD_ASSERT (name != NULL
10652
                      && CONST_STRNEQ (name, ".MIPS.content"));
10653
          sec = bfd_get_section_by_name (abfd,
10654
                                         name + sizeof ".MIPS.content" - 1);
10655
          BFD_ASSERT (sec != NULL);
10656
          (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10657
          break;
10658
 
10659
        case SHT_MIPS_SYMBOL_LIB:
10660
          sec = bfd_get_section_by_name (abfd, ".dynsym");
10661
          if (sec != NULL)
10662
            (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10663
          sec = bfd_get_section_by_name (abfd, ".liblist");
10664
          if (sec != NULL)
10665
            (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10666
          break;
10667
 
10668
        case SHT_MIPS_EVENTS:
10669
          BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10670
          name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10671
          BFD_ASSERT (name != NULL);
10672
          if (CONST_STRNEQ (name, ".MIPS.events"))
10673
            sec = bfd_get_section_by_name (abfd,
10674
                                           name + sizeof ".MIPS.events" - 1);
10675
          else
10676
            {
10677
              BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
10678
              sec = bfd_get_section_by_name (abfd,
10679
                                             (name
10680
                                              + sizeof ".MIPS.post_rel" - 1));
10681
            }
10682
          BFD_ASSERT (sec != NULL);
10683
          (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10684
          break;
10685
 
10686
        }
10687
    }
10688
}
10689
 
10690
/* When creating an IRIX5 executable, we need REGINFO and RTPROC
10691
   segments.  */
10692
 
10693
int
10694
_bfd_mips_elf_additional_program_headers (bfd *abfd,
10695
                                          struct bfd_link_info *info ATTRIBUTE_UNUSED)
10696
{
10697
  asection *s;
10698
  int ret = 0;
10699
 
10700
  /* See if we need a PT_MIPS_REGINFO segment.  */
10701
  s = bfd_get_section_by_name (abfd, ".reginfo");
10702
  if (s && (s->flags & SEC_LOAD))
10703
    ++ret;
10704
 
10705
  /* See if we need a PT_MIPS_OPTIONS segment.  */
10706
  if (IRIX_COMPAT (abfd) == ict_irix6
10707
      && bfd_get_section_by_name (abfd,
10708
                                  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10709
    ++ret;
10710
 
10711
  /* See if we need a PT_MIPS_RTPROC segment.  */
10712
  if (IRIX_COMPAT (abfd) == ict_irix5
10713
      && bfd_get_section_by_name (abfd, ".dynamic")
10714
      && bfd_get_section_by_name (abfd, ".mdebug"))
10715
    ++ret;
10716
 
10717
  /* Allocate a PT_NULL header in dynamic objects.  See
10718
     _bfd_mips_elf_modify_segment_map for details.  */
10719
  if (!SGI_COMPAT (abfd)
10720
      && bfd_get_section_by_name (abfd, ".dynamic"))
10721
    ++ret;
10722
 
10723
  return ret;
10724
}
10725
 
10726
/* Modify the segment map for an IRIX5 executable.  */
10727
 
10728
bfd_boolean
10729
_bfd_mips_elf_modify_segment_map (bfd *abfd,
10730
                                  struct bfd_link_info *info)
10731
{
10732
  asection *s;
10733
  struct elf_segment_map *m, **pm;
10734
  bfd_size_type amt;
10735
 
10736
  /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10737
     segment.  */
10738
  s = bfd_get_section_by_name (abfd, ".reginfo");
10739
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
10740
    {
10741
      for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10742
        if (m->p_type == PT_MIPS_REGINFO)
10743
          break;
10744
      if (m == NULL)
10745
        {
10746
          amt = sizeof *m;
10747
          m = bfd_zalloc (abfd, amt);
10748
          if (m == NULL)
10749
            return FALSE;
10750
 
10751
          m->p_type = PT_MIPS_REGINFO;
10752
          m->count = 1;
10753
          m->sections[0] = s;
10754
 
10755
          /* We want to put it after the PHDR and INTERP segments.  */
10756
          pm = &elf_tdata (abfd)->segment_map;
10757
          while (*pm != NULL
10758
                 && ((*pm)->p_type == PT_PHDR
10759
                     || (*pm)->p_type == PT_INTERP))
10760
            pm = &(*pm)->next;
10761
 
10762
          m->next = *pm;
10763
          *pm = m;
10764
        }
10765
    }
10766
 
10767
  /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10768
     .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
10769
     PT_MIPS_OPTIONS segment immediately following the program header
10770
     table.  */
10771
  if (NEWABI_P (abfd)
10772
      /* On non-IRIX6 new abi, we'll have already created a segment
10773
         for this section, so don't create another.  I'm not sure this
10774
         is not also the case for IRIX 6, but I can't test it right
10775
         now.  */
10776
      && IRIX_COMPAT (abfd) == ict_irix6)
10777
    {
10778
      for (s = abfd->sections; s; s = s->next)
10779
        if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10780
          break;
10781
 
10782
      if (s)
10783
        {
10784
          struct elf_segment_map *options_segment;
10785
 
10786
          pm = &elf_tdata (abfd)->segment_map;
10787
          while (*pm != NULL
10788
                 && ((*pm)->p_type == PT_PHDR
10789
                     || (*pm)->p_type == PT_INTERP))
10790
            pm = &(*pm)->next;
10791
 
10792
          if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10793
            {
10794
              amt = sizeof (struct elf_segment_map);
10795
              options_segment = bfd_zalloc (abfd, amt);
10796
              options_segment->next = *pm;
10797
              options_segment->p_type = PT_MIPS_OPTIONS;
10798
              options_segment->p_flags = PF_R;
10799
              options_segment->p_flags_valid = TRUE;
10800
              options_segment->count = 1;
10801
              options_segment->sections[0] = s;
10802
              *pm = options_segment;
10803
            }
10804
        }
10805
    }
10806
  else
10807
    {
10808
      if (IRIX_COMPAT (abfd) == ict_irix5)
10809
        {
10810
          /* If there are .dynamic and .mdebug sections, we make a room
10811
             for the RTPROC header.  FIXME: Rewrite without section names.  */
10812
          if (bfd_get_section_by_name (abfd, ".interp") == NULL
10813
              && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10814
              && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10815
            {
10816
              for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10817
                if (m->p_type == PT_MIPS_RTPROC)
10818
                  break;
10819
              if (m == NULL)
10820
                {
10821
                  amt = sizeof *m;
10822
                  m = bfd_zalloc (abfd, amt);
10823
                  if (m == NULL)
10824
                    return FALSE;
10825
 
10826
                  m->p_type = PT_MIPS_RTPROC;
10827
 
10828
                  s = bfd_get_section_by_name (abfd, ".rtproc");
10829
                  if (s == NULL)
10830
                    {
10831
                      m->count = 0;
10832
                      m->p_flags = 0;
10833
                      m->p_flags_valid = 1;
10834
                    }
10835
                  else
10836
                    {
10837
                      m->count = 1;
10838
                      m->sections[0] = s;
10839
                    }
10840
 
10841
                  /* We want to put it after the DYNAMIC segment.  */
10842
                  pm = &elf_tdata (abfd)->segment_map;
10843
                  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10844
                    pm = &(*pm)->next;
10845
                  if (*pm != NULL)
10846
                    pm = &(*pm)->next;
10847
 
10848
                  m->next = *pm;
10849
                  *pm = m;
10850
                }
10851
            }
10852
        }
10853
      /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10854
         .dynstr, .dynsym, and .hash sections, and everything in
10855
         between.  */
10856
      for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10857
           pm = &(*pm)->next)
10858
        if ((*pm)->p_type == PT_DYNAMIC)
10859
          break;
10860
      m = *pm;
10861
      if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10862
        {
10863
          /* For a normal mips executable the permissions for the PT_DYNAMIC
10864
             segment are read, write and execute. We do that here since
10865
             the code in elf.c sets only the read permission. This matters
10866
             sometimes for the dynamic linker.  */
10867
          if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10868
            {
10869
              m->p_flags = PF_R | PF_W | PF_X;
10870
              m->p_flags_valid = 1;
10871
            }
10872
        }
10873
      /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10874
         glibc's dynamic linker has traditionally derived the number of
10875
         tags from the p_filesz field, and sometimes allocates stack
10876
         arrays of that size.  An overly-big PT_DYNAMIC segment can
10877
         be actively harmful in such cases.  Making PT_DYNAMIC contain
10878
         other sections can also make life hard for the prelinker,
10879
         which might move one of the other sections to a different
10880
         PT_LOAD segment.  */
10881
      if (SGI_COMPAT (abfd)
10882
          && m != NULL
10883
          && m->count == 1
10884
          && strcmp (m->sections[0]->name, ".dynamic") == 0)
10885
        {
10886
          static const char *sec_names[] =
10887
          {
10888
            ".dynamic", ".dynstr", ".dynsym", ".hash"
10889
          };
10890
          bfd_vma low, high;
10891
          unsigned int i, c;
10892
          struct elf_segment_map *n;
10893
 
10894
          low = ~(bfd_vma) 0;
10895
          high = 0;
10896
          for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10897
            {
10898
              s = bfd_get_section_by_name (abfd, sec_names[i]);
10899
              if (s != NULL && (s->flags & SEC_LOAD) != 0)
10900
                {
10901
                  bfd_size_type sz;
10902
 
10903
                  if (low > s->vma)
10904
                    low = s->vma;
10905
                  sz = s->size;
10906
                  if (high < s->vma + sz)
10907
                    high = s->vma + sz;
10908
                }
10909
            }
10910
 
10911
          c = 0;
10912
          for (s = abfd->sections; s != NULL; s = s->next)
10913
            if ((s->flags & SEC_LOAD) != 0
10914
                && s->vma >= low
10915
                && s->vma + s->size <= high)
10916
              ++c;
10917
 
10918
          amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10919
          n = bfd_zalloc (abfd, amt);
10920
          if (n == NULL)
10921
            return FALSE;
10922
          *n = *m;
10923
          n->count = c;
10924
 
10925
          i = 0;
10926
          for (s = abfd->sections; s != NULL; s = s->next)
10927
            {
10928
              if ((s->flags & SEC_LOAD) != 0
10929
                  && s->vma >= low
10930
                  && s->vma + s->size <= high)
10931
                {
10932
                  n->sections[i] = s;
10933
                  ++i;
10934
                }
10935
            }
10936
 
10937
          *pm = n;
10938
        }
10939
    }
10940
 
10941
  /* Allocate a spare program header in dynamic objects so that tools
10942
     like the prelinker can add an extra PT_LOAD entry.
10943
 
10944
     If the prelinker needs to make room for a new PT_LOAD entry, its
10945
     standard procedure is to move the first (read-only) sections into
10946
     the new (writable) segment.  However, the MIPS ABI requires
10947
     .dynamic to be in a read-only segment, and the section will often
10948
     start within sizeof (ElfNN_Phdr) bytes of the last program header.
10949
 
10950
     Although the prelinker could in principle move .dynamic to a
10951
     writable segment, it seems better to allocate a spare program
10952
     header instead, and avoid the need to move any sections.
10953
     There is a long tradition of allocating spare dynamic tags,
10954
     so allocating a spare program header seems like a natural
10955
     extension.
10956
 
10957
     If INFO is NULL, we may be copying an already prelinked binary
10958
     with objcopy or strip, so do not add this header.  */
10959
  if (info != NULL
10960
      && !SGI_COMPAT (abfd)
10961
      && bfd_get_section_by_name (abfd, ".dynamic"))
10962
    {
10963
      for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10964
        if ((*pm)->p_type == PT_NULL)
10965
          break;
10966
      if (*pm == NULL)
10967
        {
10968
          m = bfd_zalloc (abfd, sizeof (*m));
10969
          if (m == NULL)
10970
            return FALSE;
10971
 
10972
          m->p_type = PT_NULL;
10973
          *pm = m;
10974
        }
10975
    }
10976
 
10977
  return TRUE;
10978
}
10979
 
10980
/* Return the section that should be marked against GC for a given
10981
   relocation.  */
10982
 
10983
asection *
10984
_bfd_mips_elf_gc_mark_hook (asection *sec,
10985
                            struct bfd_link_info *info,
10986
                            Elf_Internal_Rela *rel,
10987
                            struct elf_link_hash_entry *h,
10988
                            Elf_Internal_Sym *sym)
10989
{
10990
  /* ??? Do mips16 stub sections need to be handled special?  */
10991
 
10992
  if (h != NULL)
10993
    switch (ELF_R_TYPE (sec->owner, rel->r_info))
10994
      {
10995
      case R_MIPS_GNU_VTINHERIT:
10996
      case R_MIPS_GNU_VTENTRY:
10997
        return NULL;
10998
      }
10999
 
11000
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
11001
}
11002
 
11003
/* Update the got entry reference counts for the section being removed.  */
11004
 
11005
bfd_boolean
11006
_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11007
                             struct bfd_link_info *info ATTRIBUTE_UNUSED,
11008
                             asection *sec ATTRIBUTE_UNUSED,
11009
                             const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
11010
{
11011
#if 0
11012
  Elf_Internal_Shdr *symtab_hdr;
11013
  struct elf_link_hash_entry **sym_hashes;
11014
  bfd_signed_vma *local_got_refcounts;
11015
  const Elf_Internal_Rela *rel, *relend;
11016
  unsigned long r_symndx;
11017
  struct elf_link_hash_entry *h;
11018
 
11019
  if (info->relocatable)
11020
    return TRUE;
11021
 
11022
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11023
  sym_hashes = elf_sym_hashes (abfd);
11024
  local_got_refcounts = elf_local_got_refcounts (abfd);
11025
 
11026
  relend = relocs + sec->reloc_count;
11027
  for (rel = relocs; rel < relend; rel++)
11028
    switch (ELF_R_TYPE (abfd, rel->r_info))
11029
      {
11030
      case R_MIPS16_GOT16:
11031
      case R_MIPS16_CALL16:
11032
      case R_MIPS_GOT16:
11033
      case R_MIPS_CALL16:
11034
      case R_MIPS_CALL_HI16:
11035
      case R_MIPS_CALL_LO16:
11036
      case R_MIPS_GOT_HI16:
11037
      case R_MIPS_GOT_LO16:
11038
      case R_MIPS_GOT_DISP:
11039
      case R_MIPS_GOT_PAGE:
11040
      case R_MIPS_GOT_OFST:
11041
        /* ??? It would seem that the existing MIPS code does no sort
11042
           of reference counting or whatnot on its GOT and PLT entries,
11043
           so it is not possible to garbage collect them at this time.  */
11044
        break;
11045
 
11046
      default:
11047
        break;
11048
      }
11049
#endif
11050
 
11051
  return TRUE;
11052
}
11053
 
11054
/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11055
   hiding the old indirect symbol.  Process additional relocation
11056
   information.  Also called for weakdefs, in which case we just let
11057
   _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
11058
 
11059
void
11060
_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
11061
                                    struct elf_link_hash_entry *dir,
11062
                                    struct elf_link_hash_entry *ind)
11063
{
11064
  struct mips_elf_link_hash_entry *dirmips, *indmips;
11065
 
11066
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
11067
 
11068
  dirmips = (struct mips_elf_link_hash_entry *) dir;
11069
  indmips = (struct mips_elf_link_hash_entry *) ind;
11070
  /* Any absolute non-dynamic relocations against an indirect or weak
11071
     definition will be against the target symbol.  */
11072
  if (indmips->has_static_relocs)
11073
    dirmips->has_static_relocs = TRUE;
11074
 
11075
  if (ind->root.type != bfd_link_hash_indirect)
11076
    return;
11077
 
11078
  dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11079
  if (indmips->readonly_reloc)
11080
    dirmips->readonly_reloc = TRUE;
11081
  if (indmips->no_fn_stub)
11082
    dirmips->no_fn_stub = TRUE;
11083
  if (indmips->fn_stub)
11084
    {
11085
      dirmips->fn_stub = indmips->fn_stub;
11086
      indmips->fn_stub = NULL;
11087
    }
11088
  if (indmips->need_fn_stub)
11089
    {
11090
      dirmips->need_fn_stub = TRUE;
11091
      indmips->need_fn_stub = FALSE;
11092
    }
11093
  if (indmips->call_stub)
11094
    {
11095
      dirmips->call_stub = indmips->call_stub;
11096
      indmips->call_stub = NULL;
11097
    }
11098
  if (indmips->call_fp_stub)
11099
    {
11100
      dirmips->call_fp_stub = indmips->call_fp_stub;
11101
      indmips->call_fp_stub = NULL;
11102
    }
11103
  if (indmips->global_got_area < dirmips->global_got_area)
11104
    dirmips->global_got_area = indmips->global_got_area;
11105
  if (indmips->global_got_area < GGA_NONE)
11106
    indmips->global_got_area = GGA_NONE;
11107
  if (indmips->has_nonpic_branches)
11108
    dirmips->has_nonpic_branches = TRUE;
11109
 
11110
  if (dirmips->tls_type == 0)
11111
    dirmips->tls_type = indmips->tls_type;
11112
}
11113
 
11114
#define PDR_SIZE 32
11115
 
11116
bfd_boolean
11117
_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11118
                            struct bfd_link_info *info)
11119
{
11120
  asection *o;
11121
  bfd_boolean ret = FALSE;
11122
  unsigned char *tdata;
11123
  size_t i, skip;
11124
 
11125
  o = bfd_get_section_by_name (abfd, ".pdr");
11126
  if (! o)
11127
    return FALSE;
11128
  if (o->size == 0)
11129
    return FALSE;
11130
  if (o->size % PDR_SIZE != 0)
11131
    return FALSE;
11132
  if (o->output_section != NULL
11133
      && bfd_is_abs_section (o->output_section))
11134
    return FALSE;
11135
 
11136
  tdata = bfd_zmalloc (o->size / PDR_SIZE);
11137
  if (! tdata)
11138
    return FALSE;
11139
 
11140
  cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11141
                                            info->keep_memory);
11142
  if (!cookie->rels)
11143
    {
11144
      free (tdata);
11145
      return FALSE;
11146
    }
11147
 
11148
  cookie->rel = cookie->rels;
11149
  cookie->relend = cookie->rels + o->reloc_count;
11150
 
11151
  for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11152
    {
11153
      if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11154
        {
11155
          tdata[i] = 1;
11156
          skip ++;
11157
        }
11158
    }
11159
 
11160
  if (skip != 0)
11161
    {
11162
      mips_elf_section_data (o)->u.tdata = tdata;
11163
      o->size -= skip * PDR_SIZE;
11164
      ret = TRUE;
11165
    }
11166
  else
11167
    free (tdata);
11168
 
11169
  if (! info->keep_memory)
11170
    free (cookie->rels);
11171
 
11172
  return ret;
11173
}
11174
 
11175
bfd_boolean
11176
_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11177
{
11178
  if (strcmp (sec->name, ".pdr") == 0)
11179
    return TRUE;
11180
  return FALSE;
11181
}
11182
 
11183
bfd_boolean
11184
_bfd_mips_elf_write_section (bfd *output_bfd,
11185
                             struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11186
                             asection *sec, bfd_byte *contents)
11187
{
11188
  bfd_byte *to, *from, *end;
11189
  int i;
11190
 
11191
  if (strcmp (sec->name, ".pdr") != 0)
11192
    return FALSE;
11193
 
11194
  if (mips_elf_section_data (sec)->u.tdata == NULL)
11195
    return FALSE;
11196
 
11197
  to = contents;
11198
  end = contents + sec->size;
11199
  for (from = contents, i = 0;
11200
       from < end;
11201
       from += PDR_SIZE, i++)
11202
    {
11203
      if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11204
        continue;
11205
      if (to != from)
11206
        memcpy (to, from, PDR_SIZE);
11207
      to += PDR_SIZE;
11208
    }
11209
  bfd_set_section_contents (output_bfd, sec->output_section, contents,
11210
                            sec->output_offset, sec->size);
11211
  return TRUE;
11212
}
11213
 
11214
/* MIPS ELF uses a special find_nearest_line routine in order the
11215
   handle the ECOFF debugging information.  */
11216
 
11217
struct mips_elf_find_line
11218
{
11219
  struct ecoff_debug_info d;
11220
  struct ecoff_find_line i;
11221
};
11222
 
11223
bfd_boolean
11224
_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11225
                                 asymbol **symbols, bfd_vma offset,
11226
                                 const char **filename_ptr,
11227
                                 const char **functionname_ptr,
11228
                                 unsigned int *line_ptr)
11229
{
11230
  asection *msec;
11231
 
11232
  if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11233
                                     filename_ptr, functionname_ptr,
11234
                                     line_ptr))
11235
    return TRUE;
11236
 
11237
  if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11238
                                     filename_ptr, functionname_ptr,
11239
                                     line_ptr, ABI_64_P (abfd) ? 8 : 0,
11240
                                     &elf_tdata (abfd)->dwarf2_find_line_info))
11241
    return TRUE;
11242
 
11243
  msec = bfd_get_section_by_name (abfd, ".mdebug");
11244
  if (msec != NULL)
11245
    {
11246
      flagword origflags;
11247
      struct mips_elf_find_line *fi;
11248
      const struct ecoff_debug_swap * const swap =
11249
        get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11250
 
11251
      /* If we are called during a link, mips_elf_final_link may have
11252
         cleared the SEC_HAS_CONTENTS field.  We force it back on here
11253
         if appropriate (which it normally will be).  */
11254
      origflags = msec->flags;
11255
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11256
        msec->flags |= SEC_HAS_CONTENTS;
11257
 
11258
      fi = elf_tdata (abfd)->find_line_info;
11259
      if (fi == NULL)
11260
        {
11261
          bfd_size_type external_fdr_size;
11262
          char *fraw_src;
11263
          char *fraw_end;
11264
          struct fdr *fdr_ptr;
11265
          bfd_size_type amt = sizeof (struct mips_elf_find_line);
11266
 
11267
          fi = bfd_zalloc (abfd, amt);
11268
          if (fi == NULL)
11269
            {
11270
              msec->flags = origflags;
11271
              return FALSE;
11272
            }
11273
 
11274
          if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11275
            {
11276
              msec->flags = origflags;
11277
              return FALSE;
11278
            }
11279
 
11280
          /* Swap in the FDR information.  */
11281
          amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11282
          fi->d.fdr = bfd_alloc (abfd, amt);
11283
          if (fi->d.fdr == NULL)
11284
            {
11285
              msec->flags = origflags;
11286
              return FALSE;
11287
            }
11288
          external_fdr_size = swap->external_fdr_size;
11289
          fdr_ptr = fi->d.fdr;
11290
          fraw_src = (char *) fi->d.external_fdr;
11291
          fraw_end = (fraw_src
11292
                      + fi->d.symbolic_header.ifdMax * external_fdr_size);
11293
          for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11294
            (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11295
 
11296
          elf_tdata (abfd)->find_line_info = fi;
11297
 
11298
          /* Note that we don't bother to ever free this information.
11299
             find_nearest_line is either called all the time, as in
11300
             objdump -l, so the information should be saved, or it is
11301
             rarely called, as in ld error messages, so the memory
11302
             wasted is unimportant.  Still, it would probably be a
11303
             good idea for free_cached_info to throw it away.  */
11304
        }
11305
 
11306
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11307
                                  &fi->i, filename_ptr, functionname_ptr,
11308
                                  line_ptr))
11309
        {
11310
          msec->flags = origflags;
11311
          return TRUE;
11312
        }
11313
 
11314
      msec->flags = origflags;
11315
    }
11316
 
11317
  /* Fall back on the generic ELF find_nearest_line routine.  */
11318
 
11319
  return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11320
                                     filename_ptr, functionname_ptr,
11321
                                     line_ptr);
11322
}
11323
 
11324
bfd_boolean
11325
_bfd_mips_elf_find_inliner_info (bfd *abfd,
11326
                                 const char **filename_ptr,
11327
                                 const char **functionname_ptr,
11328
                                 unsigned int *line_ptr)
11329
{
11330
  bfd_boolean found;
11331
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11332
                                         functionname_ptr, line_ptr,
11333
                                         & elf_tdata (abfd)->dwarf2_find_line_info);
11334
  return found;
11335
}
11336
 
11337
 
11338
/* When are writing out the .options or .MIPS.options section,
11339
   remember the bytes we are writing out, so that we can install the
11340
   GP value in the section_processing routine.  */
11341
 
11342
bfd_boolean
11343
_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11344
                                    const void *location,
11345
                                    file_ptr offset, bfd_size_type count)
11346
{
11347
  if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11348
    {
11349
      bfd_byte *c;
11350
 
11351
      if (elf_section_data (section) == NULL)
11352
        {
11353
          bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11354
          section->used_by_bfd = bfd_zalloc (abfd, amt);
11355
          if (elf_section_data (section) == NULL)
11356
            return FALSE;
11357
        }
11358
      c = mips_elf_section_data (section)->u.tdata;
11359
      if (c == NULL)
11360
        {
11361
          c = bfd_zalloc (abfd, section->size);
11362
          if (c == NULL)
11363
            return FALSE;
11364
          mips_elf_section_data (section)->u.tdata = c;
11365
        }
11366
 
11367
      memcpy (c + offset, location, count);
11368
    }
11369
 
11370
  return _bfd_elf_set_section_contents (abfd, section, location, offset,
11371
                                        count);
11372
}
11373
 
11374
/* This is almost identical to bfd_generic_get_... except that some
11375
   MIPS relocations need to be handled specially.  Sigh.  */
11376
 
11377
bfd_byte *
11378
_bfd_elf_mips_get_relocated_section_contents
11379
  (bfd *abfd,
11380
   struct bfd_link_info *link_info,
11381
   struct bfd_link_order *link_order,
11382
   bfd_byte *data,
11383
   bfd_boolean relocatable,
11384
   asymbol **symbols)
11385
{
11386
  /* Get enough memory to hold the stuff */
11387
  bfd *input_bfd = link_order->u.indirect.section->owner;
11388
  asection *input_section = link_order->u.indirect.section;
11389
  bfd_size_type sz;
11390
 
11391
  long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11392
  arelent **reloc_vector = NULL;
11393
  long reloc_count;
11394
 
11395
  if (reloc_size < 0)
11396
    goto error_return;
11397
 
11398
  reloc_vector = bfd_malloc (reloc_size);
11399
  if (reloc_vector == NULL && reloc_size != 0)
11400
    goto error_return;
11401
 
11402
  /* read in the section */
11403
  sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11404
  if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11405
    goto error_return;
11406
 
11407
  reloc_count = bfd_canonicalize_reloc (input_bfd,
11408
                                        input_section,
11409
                                        reloc_vector,
11410
                                        symbols);
11411
  if (reloc_count < 0)
11412
    goto error_return;
11413
 
11414
  if (reloc_count > 0)
11415
    {
11416
      arelent **parent;
11417
      /* for mips */
11418
      int gp_found;
11419
      bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
11420
 
11421
      {
11422
        struct bfd_hash_entry *h;
11423
        struct bfd_link_hash_entry *lh;
11424
        /* Skip all this stuff if we aren't mixing formats.  */
11425
        if (abfd && input_bfd
11426
            && abfd->xvec == input_bfd->xvec)
11427
          lh = 0;
11428
        else
11429
          {
11430
            h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11431
            lh = (struct bfd_link_hash_entry *) h;
11432
          }
11433
      lookup:
11434
        if (lh)
11435
          {
11436
            switch (lh->type)
11437
              {
11438
              case bfd_link_hash_undefined:
11439
              case bfd_link_hash_undefweak:
11440
              case bfd_link_hash_common:
11441
                gp_found = 0;
11442
                break;
11443
              case bfd_link_hash_defined:
11444
              case bfd_link_hash_defweak:
11445
                gp_found = 1;
11446
                gp = lh->u.def.value;
11447
                break;
11448
              case bfd_link_hash_indirect:
11449
              case bfd_link_hash_warning:
11450
                lh = lh->u.i.link;
11451
                /* @@FIXME  ignoring warning for now */
11452
                goto lookup;
11453
              case bfd_link_hash_new:
11454
              default:
11455
                abort ();
11456
              }
11457
          }
11458
        else
11459
          gp_found = 0;
11460
      }
11461
      /* end mips */
11462
      for (parent = reloc_vector; *parent != NULL; parent++)
11463
        {
11464
          char *error_message = NULL;
11465
          bfd_reloc_status_type r;
11466
 
11467
          /* Specific to MIPS: Deal with relocation types that require
11468
             knowing the gp of the output bfd.  */
11469
          asymbol *sym = *(*parent)->sym_ptr_ptr;
11470
 
11471
          /* If we've managed to find the gp and have a special
11472
             function for the relocation then go ahead, else default
11473
             to the generic handling.  */
11474
          if (gp_found
11475
              && (*parent)->howto->special_function
11476
              == _bfd_mips_elf32_gprel16_reloc)
11477
            r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11478
                                               input_section, relocatable,
11479
                                               data, gp);
11480
          else
11481
            r = bfd_perform_relocation (input_bfd, *parent, data,
11482
                                        input_section,
11483
                                        relocatable ? abfd : NULL,
11484
                                        &error_message);
11485
 
11486
          if (relocatable)
11487
            {
11488
              asection *os = input_section->output_section;
11489
 
11490
              /* A partial link, so keep the relocs */
11491
              os->orelocation[os->reloc_count] = *parent;
11492
              os->reloc_count++;
11493
            }
11494
 
11495
          if (r != bfd_reloc_ok)
11496
            {
11497
              switch (r)
11498
                {
11499
                case bfd_reloc_undefined:
11500
                  if (!((*link_info->callbacks->undefined_symbol)
11501
                        (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11502
                         input_bfd, input_section, (*parent)->address, TRUE)))
11503
                    goto error_return;
11504
                  break;
11505
                case bfd_reloc_dangerous:
11506
                  BFD_ASSERT (error_message != NULL);
11507
                  if (!((*link_info->callbacks->reloc_dangerous)
11508
                        (link_info, error_message, input_bfd, input_section,
11509
                         (*parent)->address)))
11510
                    goto error_return;
11511
                  break;
11512
                case bfd_reloc_overflow:
11513
                  if (!((*link_info->callbacks->reloc_overflow)
11514
                        (link_info, NULL,
11515
                         bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11516
                         (*parent)->howto->name, (*parent)->addend,
11517
                         input_bfd, input_section, (*parent)->address)))
11518
                    goto error_return;
11519
                  break;
11520
                case bfd_reloc_outofrange:
11521
                default:
11522
                  abort ();
11523
                  break;
11524
                }
11525
 
11526
            }
11527
        }
11528
    }
11529
  if (reloc_vector != NULL)
11530
    free (reloc_vector);
11531
  return data;
11532
 
11533
error_return:
11534
  if (reloc_vector != NULL)
11535
    free (reloc_vector);
11536
  return NULL;
11537
}
11538
 
11539
/* Create a MIPS ELF linker hash table.  */
11540
 
11541
struct bfd_link_hash_table *
11542
_bfd_mips_elf_link_hash_table_create (bfd *abfd)
11543
{
11544
  struct mips_elf_link_hash_table *ret;
11545
  bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11546
 
11547
  ret = bfd_malloc (amt);
11548
  if (ret == NULL)
11549
    return NULL;
11550
 
11551
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11552
                                      mips_elf_link_hash_newfunc,
11553
                                      sizeof (struct mips_elf_link_hash_entry),
11554
                                      MIPS_ELF_DATA))
11555
    {
11556
      free (ret);
11557
      return NULL;
11558
    }
11559
 
11560
#if 0
11561
  /* We no longer use this.  */
11562
  for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11563
    ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11564
#endif
11565
  ret->procedure_count = 0;
11566
  ret->compact_rel_size = 0;
11567
  ret->use_rld_obj_head = FALSE;
11568
  ret->rld_value = 0;
11569
  ret->mips16_stubs_seen = FALSE;
11570
  ret->use_plts_and_copy_relocs = FALSE;
11571
  ret->is_vxworks = FALSE;
11572
  ret->small_data_overflow_reported = FALSE;
11573
  ret->srelbss = NULL;
11574
  ret->sdynbss = NULL;
11575
  ret->srelplt = NULL;
11576
  ret->srelplt2 = NULL;
11577
  ret->sgotplt = NULL;
11578
  ret->splt = NULL;
11579
  ret->sstubs = NULL;
11580
  ret->sgot = NULL;
11581
  ret->got_info = NULL;
11582
  ret->plt_header_size = 0;
11583
  ret->plt_entry_size = 0;
11584
  ret->lazy_stub_count = 0;
11585
  ret->function_stub_size = 0;
11586
  ret->strampoline = NULL;
11587
  ret->la25_stubs = NULL;
11588
  ret->add_stub_section = NULL;
11589
 
11590
  return &ret->root.root;
11591
}
11592
 
11593
/* Likewise, but indicate that the target is VxWorks.  */
11594
 
11595
struct bfd_link_hash_table *
11596
_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11597
{
11598
  struct bfd_link_hash_table *ret;
11599
 
11600
  ret = _bfd_mips_elf_link_hash_table_create (abfd);
11601
  if (ret)
11602
    {
11603
      struct mips_elf_link_hash_table *htab;
11604
 
11605
      htab = (struct mips_elf_link_hash_table *) ret;
11606
      htab->use_plts_and_copy_relocs = TRUE;
11607
      htab->is_vxworks = TRUE;
11608
    }
11609
  return ret;
11610
}
11611
 
11612
/* A function that the linker calls if we are allowed to use PLTs
11613
   and copy relocs.  */
11614
 
11615
void
11616
_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11617
{
11618
  mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11619
}
11620
 
11621
/* We need to use a special link routine to handle the .reginfo and
11622
   the .mdebug sections.  We need to merge all instances of these
11623
   sections together, not write them all out sequentially.  */
11624
 
11625
bfd_boolean
11626
_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11627
{
11628
  asection *o;
11629
  struct bfd_link_order *p;
11630
  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11631
  asection *rtproc_sec;
11632
  Elf32_RegInfo reginfo;
11633
  struct ecoff_debug_info debug;
11634
  struct mips_htab_traverse_info hti;
11635
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11636
  const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
11637
  HDRR *symhdr = &debug.symbolic_header;
11638
  void *mdebug_handle = NULL;
11639
  asection *s;
11640
  EXTR esym;
11641
  unsigned int i;
11642
  bfd_size_type amt;
11643
  struct mips_elf_link_hash_table *htab;
11644
 
11645
  static const char * const secname[] =
11646
  {
11647
    ".text", ".init", ".fini", ".data",
11648
    ".rodata", ".sdata", ".sbss", ".bss"
11649
  };
11650
  static const int sc[] =
11651
  {
11652
    scText, scInit, scFini, scData,
11653
    scRData, scSData, scSBss, scBss
11654
  };
11655
 
11656
  /* Sort the dynamic symbols so that those with GOT entries come after
11657
     those without.  */
11658
  htab = mips_elf_hash_table (info);
11659
  BFD_ASSERT (htab != NULL);
11660
 
11661
  if (!mips_elf_sort_hash_table (abfd, info))
11662
    return FALSE;
11663
 
11664
  /* Create any scheduled LA25 stubs.  */
11665
  hti.info = info;
11666
  hti.output_bfd = abfd;
11667
  hti.error = FALSE;
11668
  htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11669
  if (hti.error)
11670
    return FALSE;
11671
 
11672
  /* Get a value for the GP register.  */
11673
  if (elf_gp (abfd) == 0)
11674
    {
11675
      struct bfd_link_hash_entry *h;
11676
 
11677
      h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
11678
      if (h != NULL && h->type == bfd_link_hash_defined)
11679
        elf_gp (abfd) = (h->u.def.value
11680
                         + h->u.def.section->output_section->vma
11681
                         + h->u.def.section->output_offset);
11682
      else if (htab->is_vxworks
11683
               && (h = bfd_link_hash_lookup (info->hash,
11684
                                             "_GLOBAL_OFFSET_TABLE_",
11685
                                             FALSE, FALSE, TRUE))
11686
               && h->type == bfd_link_hash_defined)
11687
        elf_gp (abfd) = (h->u.def.section->output_section->vma
11688
                         + h->u.def.section->output_offset
11689
                         + h->u.def.value);
11690
      else if (info->relocatable)
11691
        {
11692
          bfd_vma lo = MINUS_ONE;
11693
 
11694
          /* Find the GP-relative section with the lowest offset.  */
11695
          for (o = abfd->sections; o != NULL; o = o->next)
11696
            if (o->vma < lo
11697
                && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11698
              lo = o->vma;
11699
 
11700
          /* And calculate GP relative to that.  */
11701
          elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
11702
        }
11703
      else
11704
        {
11705
          /* If the relocate_section function needs to do a reloc
11706
             involving the GP value, it should make a reloc_dangerous
11707
             callback to warn that GP is not defined.  */
11708
        }
11709
    }
11710
 
11711
  /* Go through the sections and collect the .reginfo and .mdebug
11712
     information.  */
11713
  reginfo_sec = NULL;
11714
  mdebug_sec = NULL;
11715
  gptab_data_sec = NULL;
11716
  gptab_bss_sec = NULL;
11717
  for (o = abfd->sections; o != NULL; o = o->next)
11718
    {
11719
      if (strcmp (o->name, ".reginfo") == 0)
11720
        {
11721
          memset (&reginfo, 0, sizeof reginfo);
11722
 
11723
          /* We have found the .reginfo section in the output file.
11724
             Look through all the link_orders comprising it and merge
11725
             the information together.  */
11726
          for (p = o->map_head.link_order; p != NULL; p = p->next)
11727
            {
11728
              asection *input_section;
11729
              bfd *input_bfd;
11730
              Elf32_External_RegInfo ext;
11731
              Elf32_RegInfo sub;
11732
 
11733
              if (p->type != bfd_indirect_link_order)
11734
                {
11735
                  if (p->type == bfd_data_link_order)
11736
                    continue;
11737
                  abort ();
11738
                }
11739
 
11740
              input_section = p->u.indirect.section;
11741
              input_bfd = input_section->owner;
11742
 
11743
              if (! bfd_get_section_contents (input_bfd, input_section,
11744
                                              &ext, 0, sizeof ext))
11745
                return FALSE;
11746
 
11747
              bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11748
 
11749
              reginfo.ri_gprmask |= sub.ri_gprmask;
11750
              reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11751
              reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11752
              reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11753
              reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11754
 
11755
              /* ri_gp_value is set by the function
11756
                 mips_elf32_section_processing when the section is
11757
                 finally written out.  */
11758
 
11759
              /* Hack: reset the SEC_HAS_CONTENTS flag so that
11760
                 elf_link_input_bfd ignores this section.  */
11761
              input_section->flags &= ~SEC_HAS_CONTENTS;
11762
            }
11763
 
11764
          /* Size has been set in _bfd_mips_elf_always_size_sections.  */
11765
          BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
11766
 
11767
          /* Skip this section later on (I don't think this currently
11768
             matters, but someday it might).  */
11769
          o->map_head.link_order = NULL;
11770
 
11771
          reginfo_sec = o;
11772
        }
11773
 
11774
      if (strcmp (o->name, ".mdebug") == 0)
11775
        {
11776
          struct extsym_info einfo;
11777
          bfd_vma last;
11778
 
11779
          /* We have found the .mdebug section in the output file.
11780
             Look through all the link_orders comprising it and merge
11781
             the information together.  */
11782
          symhdr->magic = swap->sym_magic;
11783
          /* FIXME: What should the version stamp be?  */
11784
          symhdr->vstamp = 0;
11785
          symhdr->ilineMax = 0;
11786
          symhdr->cbLine = 0;
11787
          symhdr->idnMax = 0;
11788
          symhdr->ipdMax = 0;
11789
          symhdr->isymMax = 0;
11790
          symhdr->ioptMax = 0;
11791
          symhdr->iauxMax = 0;
11792
          symhdr->issMax = 0;
11793
          symhdr->issExtMax = 0;
11794
          symhdr->ifdMax = 0;
11795
          symhdr->crfd = 0;
11796
          symhdr->iextMax = 0;
11797
 
11798
          /* We accumulate the debugging information itself in the
11799
             debug_info structure.  */
11800
          debug.line = NULL;
11801
          debug.external_dnr = NULL;
11802
          debug.external_pdr = NULL;
11803
          debug.external_sym = NULL;
11804
          debug.external_opt = NULL;
11805
          debug.external_aux = NULL;
11806
          debug.ss = NULL;
11807
          debug.ssext = debug.ssext_end = NULL;
11808
          debug.external_fdr = NULL;
11809
          debug.external_rfd = NULL;
11810
          debug.external_ext = debug.external_ext_end = NULL;
11811
 
11812
          mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11813
          if (mdebug_handle == NULL)
11814
            return FALSE;
11815
 
11816
          esym.jmptbl = 0;
11817
          esym.cobol_main = 0;
11818
          esym.weakext = 0;
11819
          esym.reserved = 0;
11820
          esym.ifd = ifdNil;
11821
          esym.asym.iss = issNil;
11822
          esym.asym.st = stLocal;
11823
          esym.asym.reserved = 0;
11824
          esym.asym.index = indexNil;
11825
          last = 0;
11826
          for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11827
            {
11828
              esym.asym.sc = sc[i];
11829
              s = bfd_get_section_by_name (abfd, secname[i]);
11830
              if (s != NULL)
11831
                {
11832
                  esym.asym.value = s->vma;
11833
                  last = s->vma + s->size;
11834
                }
11835
              else
11836
                esym.asym.value = last;
11837
              if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11838
                                                 secname[i], &esym))
11839
                return FALSE;
11840
            }
11841
 
11842
          for (p = o->map_head.link_order; p != NULL; p = p->next)
11843
            {
11844
              asection *input_section;
11845
              bfd *input_bfd;
11846
              const struct ecoff_debug_swap *input_swap;
11847
              struct ecoff_debug_info input_debug;
11848
              char *eraw_src;
11849
              char *eraw_end;
11850
 
11851
              if (p->type != bfd_indirect_link_order)
11852
                {
11853
                  if (p->type == bfd_data_link_order)
11854
                    continue;
11855
                  abort ();
11856
                }
11857
 
11858
              input_section = p->u.indirect.section;
11859
              input_bfd = input_section->owner;
11860
 
11861
              if (!is_mips_elf (input_bfd))
11862
                {
11863
                  /* I don't know what a non MIPS ELF bfd would be
11864
                     doing with a .mdebug section, but I don't really
11865
                     want to deal with it.  */
11866
                  continue;
11867
                }
11868
 
11869
              input_swap = (get_elf_backend_data (input_bfd)
11870
                            ->elf_backend_ecoff_debug_swap);
11871
 
11872
              BFD_ASSERT (p->size == input_section->size);
11873
 
11874
              /* The ECOFF linking code expects that we have already
11875
                 read in the debugging information and set up an
11876
                 ecoff_debug_info structure, so we do that now.  */
11877
              if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11878
                                                   &input_debug))
11879
                return FALSE;
11880
 
11881
              if (! (bfd_ecoff_debug_accumulate
11882
                     (mdebug_handle, abfd, &debug, swap, input_bfd,
11883
                      &input_debug, input_swap, info)))
11884
                return FALSE;
11885
 
11886
              /* Loop through the external symbols.  For each one with
11887
                 interesting information, try to find the symbol in
11888
                 the linker global hash table and save the information
11889
                 for the output external symbols.  */
11890
              eraw_src = input_debug.external_ext;
11891
              eraw_end = (eraw_src
11892
                          + (input_debug.symbolic_header.iextMax
11893
                             * input_swap->external_ext_size));
11894
              for (;
11895
                   eraw_src < eraw_end;
11896
                   eraw_src += input_swap->external_ext_size)
11897
                {
11898
                  EXTR ext;
11899
                  const char *name;
11900
                  struct mips_elf_link_hash_entry *h;
11901
 
11902
                  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11903
                  if (ext.asym.sc == scNil
11904
                      || ext.asym.sc == scUndefined
11905
                      || ext.asym.sc == scSUndefined)
11906
                    continue;
11907
 
11908
                  name = input_debug.ssext + ext.asym.iss;
11909
                  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11910
                                                 name, FALSE, FALSE, TRUE);
11911
                  if (h == NULL || h->esym.ifd != -2)
11912
                    continue;
11913
 
11914
                  if (ext.ifd != -1)
11915
                    {
11916
                      BFD_ASSERT (ext.ifd
11917
                                  < input_debug.symbolic_header.ifdMax);
11918
                      ext.ifd = input_debug.ifdmap[ext.ifd];
11919
                    }
11920
 
11921
                  h->esym = ext;
11922
                }
11923
 
11924
              /* Free up the information we just read.  */
11925
              free (input_debug.line);
11926
              free (input_debug.external_dnr);
11927
              free (input_debug.external_pdr);
11928
              free (input_debug.external_sym);
11929
              free (input_debug.external_opt);
11930
              free (input_debug.external_aux);
11931
              free (input_debug.ss);
11932
              free (input_debug.ssext);
11933
              free (input_debug.external_fdr);
11934
              free (input_debug.external_rfd);
11935
              free (input_debug.external_ext);
11936
 
11937
              /* Hack: reset the SEC_HAS_CONTENTS flag so that
11938
                 elf_link_input_bfd ignores this section.  */
11939
              input_section->flags &= ~SEC_HAS_CONTENTS;
11940
            }
11941
 
11942
          if (SGI_COMPAT (abfd) && info->shared)
11943
            {
11944
              /* Create .rtproc section.  */
11945
              rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11946
              if (rtproc_sec == NULL)
11947
                {
11948
                  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11949
                                    | SEC_LINKER_CREATED | SEC_READONLY);
11950
 
11951
                  rtproc_sec = bfd_make_section_with_flags (abfd,
11952
                                                            ".rtproc",
11953
                                                            flags);
11954
                  if (rtproc_sec == NULL
11955
                      || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11956
                    return FALSE;
11957
                }
11958
 
11959
              if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11960
                                                     info, rtproc_sec,
11961
                                                     &debug))
11962
                return FALSE;
11963
            }
11964
 
11965
          /* Build the external symbol information.  */
11966
          einfo.abfd = abfd;
11967
          einfo.info = info;
11968
          einfo.debug = &debug;
11969
          einfo.swap = swap;
11970
          einfo.failed = FALSE;
11971
          mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11972
                                       mips_elf_output_extsym, &einfo);
11973
          if (einfo.failed)
11974
            return FALSE;
11975
 
11976
          /* Set the size of the .mdebug section.  */
11977
          o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11978
 
11979
          /* Skip this section later on (I don't think this currently
11980
             matters, but someday it might).  */
11981
          o->map_head.link_order = NULL;
11982
 
11983
          mdebug_sec = o;
11984
        }
11985
 
11986
      if (CONST_STRNEQ (o->name, ".gptab."))
11987
        {
11988
          const char *subname;
11989
          unsigned int c;
11990
          Elf32_gptab *tab;
11991
          Elf32_External_gptab *ext_tab;
11992
          unsigned int j;
11993
 
11994
          /* The .gptab.sdata and .gptab.sbss sections hold
11995
             information describing how the small data area would
11996
             change depending upon the -G switch.  These sections
11997
             not used in executables files.  */
11998
          if (! info->relocatable)
11999
            {
12000
              for (p = o->map_head.link_order; p != NULL; p = p->next)
12001
                {
12002
                  asection *input_section;
12003
 
12004
                  if (p->type != bfd_indirect_link_order)
12005
                    {
12006
                      if (p->type == bfd_data_link_order)
12007
                        continue;
12008
                      abort ();
12009
                    }
12010
 
12011
                  input_section = p->u.indirect.section;
12012
 
12013
                  /* Hack: reset the SEC_HAS_CONTENTS flag so that
12014
                     elf_link_input_bfd ignores this section.  */
12015
                  input_section->flags &= ~SEC_HAS_CONTENTS;
12016
                }
12017
 
12018
              /* Skip this section later on (I don't think this
12019
                 currently matters, but someday it might).  */
12020
              o->map_head.link_order = NULL;
12021
 
12022
              /* Really remove the section.  */
12023
              bfd_section_list_remove (abfd, o);
12024
              --abfd->section_count;
12025
 
12026
              continue;
12027
            }
12028
 
12029
          /* There is one gptab for initialized data, and one for
12030
             uninitialized data.  */
12031
          if (strcmp (o->name, ".gptab.sdata") == 0)
12032
            gptab_data_sec = o;
12033
          else if (strcmp (o->name, ".gptab.sbss") == 0)
12034
            gptab_bss_sec = o;
12035
          else
12036
            {
12037
              (*_bfd_error_handler)
12038
                (_("%s: illegal section name `%s'"),
12039
                 bfd_get_filename (abfd), o->name);
12040
              bfd_set_error (bfd_error_nonrepresentable_section);
12041
              return FALSE;
12042
            }
12043
 
12044
          /* The linker script always combines .gptab.data and
12045
             .gptab.sdata into .gptab.sdata, and likewise for
12046
             .gptab.bss and .gptab.sbss.  It is possible that there is
12047
             no .sdata or .sbss section in the output file, in which
12048
             case we must change the name of the output section.  */
12049
          subname = o->name + sizeof ".gptab" - 1;
12050
          if (bfd_get_section_by_name (abfd, subname) == NULL)
12051
            {
12052
              if (o == gptab_data_sec)
12053
                o->name = ".gptab.data";
12054
              else
12055
                o->name = ".gptab.bss";
12056
              subname = o->name + sizeof ".gptab" - 1;
12057
              BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
12058
            }
12059
 
12060
          /* Set up the first entry.  */
12061
          c = 1;
12062
          amt = c * sizeof (Elf32_gptab);
12063
          tab = bfd_malloc (amt);
12064
          if (tab == NULL)
12065
            return FALSE;
12066
          tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
12067
          tab[0].gt_header.gt_unused = 0;
12068
 
12069
          /* Combine the input sections.  */
12070
          for (p = o->map_head.link_order; p != NULL; p = p->next)
12071
            {
12072
              asection *input_section;
12073
              bfd *input_bfd;
12074
              bfd_size_type size;
12075
              unsigned long last;
12076
              bfd_size_type gpentry;
12077
 
12078
              if (p->type != bfd_indirect_link_order)
12079
                {
12080
                  if (p->type == bfd_data_link_order)
12081
                    continue;
12082
                  abort ();
12083
                }
12084
 
12085
              input_section = p->u.indirect.section;
12086
              input_bfd = input_section->owner;
12087
 
12088
              /* Combine the gptab entries for this input section one
12089
                 by one.  We know that the input gptab entries are
12090
                 sorted by ascending -G value.  */
12091
              size = input_section->size;
12092
              last = 0;
12093
              for (gpentry = sizeof (Elf32_External_gptab);
12094
                   gpentry < size;
12095
                   gpentry += sizeof (Elf32_External_gptab))
12096
                {
12097
                  Elf32_External_gptab ext_gptab;
12098
                  Elf32_gptab int_gptab;
12099
                  unsigned long val;
12100
                  unsigned long add;
12101
                  bfd_boolean exact;
12102
                  unsigned int look;
12103
 
12104
                  if (! (bfd_get_section_contents
12105
                         (input_bfd, input_section, &ext_gptab, gpentry,
12106
                          sizeof (Elf32_External_gptab))))
12107
                    {
12108
                      free (tab);
12109
                      return FALSE;
12110
                    }
12111
 
12112
                  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12113
                                                &int_gptab);
12114
                  val = int_gptab.gt_entry.gt_g_value;
12115
                  add = int_gptab.gt_entry.gt_bytes - last;
12116
 
12117
                  exact = FALSE;
12118
                  for (look = 1; look < c; look++)
12119
                    {
12120
                      if (tab[look].gt_entry.gt_g_value >= val)
12121
                        tab[look].gt_entry.gt_bytes += add;
12122
 
12123
                      if (tab[look].gt_entry.gt_g_value == val)
12124
                        exact = TRUE;
12125
                    }
12126
 
12127
                  if (! exact)
12128
                    {
12129
                      Elf32_gptab *new_tab;
12130
                      unsigned int max;
12131
 
12132
                      /* We need a new table entry.  */
12133
                      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
12134
                      new_tab = bfd_realloc (tab, amt);
12135
                      if (new_tab == NULL)
12136
                        {
12137
                          free (tab);
12138
                          return FALSE;
12139
                        }
12140
                      tab = new_tab;
12141
                      tab[c].gt_entry.gt_g_value = val;
12142
                      tab[c].gt_entry.gt_bytes = add;
12143
 
12144
                      /* Merge in the size for the next smallest -G
12145
                         value, since that will be implied by this new
12146
                         value.  */
12147
                      max = 0;
12148
                      for (look = 1; look < c; look++)
12149
                        {
12150
                          if (tab[look].gt_entry.gt_g_value < val
12151
                              && (max == 0
12152
                                  || (tab[look].gt_entry.gt_g_value
12153
                                      > tab[max].gt_entry.gt_g_value)))
12154
                            max = look;
12155
                        }
12156
                      if (max != 0)
12157
                        tab[c].gt_entry.gt_bytes +=
12158
                          tab[max].gt_entry.gt_bytes;
12159
 
12160
                      ++c;
12161
                    }
12162
 
12163
                  last = int_gptab.gt_entry.gt_bytes;
12164
                }
12165
 
12166
              /* Hack: reset the SEC_HAS_CONTENTS flag so that
12167
                 elf_link_input_bfd ignores this section.  */
12168
              input_section->flags &= ~SEC_HAS_CONTENTS;
12169
            }
12170
 
12171
          /* The table must be sorted by -G value.  */
12172
          if (c > 2)
12173
            qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12174
 
12175
          /* Swap out the table.  */
12176
          amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
12177
          ext_tab = bfd_alloc (abfd, amt);
12178
          if (ext_tab == NULL)
12179
            {
12180
              free (tab);
12181
              return FALSE;
12182
            }
12183
 
12184
          for (j = 0; j < c; j++)
12185
            bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12186
          free (tab);
12187
 
12188
          o->size = c * sizeof (Elf32_External_gptab);
12189
          o->contents = (bfd_byte *) ext_tab;
12190
 
12191
          /* Skip this section later on (I don't think this currently
12192
             matters, but someday it might).  */
12193
          o->map_head.link_order = NULL;
12194
        }
12195
    }
12196
 
12197
  /* Invoke the regular ELF backend linker to do all the work.  */
12198
  if (!bfd_elf_final_link (abfd, info))
12199
    return FALSE;
12200
 
12201
  /* Now write out the computed sections.  */
12202
 
12203
  if (reginfo_sec != NULL)
12204
    {
12205
      Elf32_External_RegInfo ext;
12206
 
12207
      bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
12208
      if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
12209
        return FALSE;
12210
    }
12211
 
12212
  if (mdebug_sec != NULL)
12213
    {
12214
      BFD_ASSERT (abfd->output_has_begun);
12215
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12216
                                               swap, info,
12217
                                               mdebug_sec->filepos))
12218
        return FALSE;
12219
 
12220
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12221
    }
12222
 
12223
  if (gptab_data_sec != NULL)
12224
    {
12225
      if (! bfd_set_section_contents (abfd, gptab_data_sec,
12226
                                      gptab_data_sec->contents,
12227
                                      0, gptab_data_sec->size))
12228
        return FALSE;
12229
    }
12230
 
12231
  if (gptab_bss_sec != NULL)
12232
    {
12233
      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12234
                                      gptab_bss_sec->contents,
12235
                                      0, gptab_bss_sec->size))
12236
        return FALSE;
12237
    }
12238
 
12239
  if (SGI_COMPAT (abfd))
12240
    {
12241
      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12242
      if (rtproc_sec != NULL)
12243
        {
12244
          if (! bfd_set_section_contents (abfd, rtproc_sec,
12245
                                          rtproc_sec->contents,
12246
                                          0, rtproc_sec->size))
12247
            return FALSE;
12248
        }
12249
    }
12250
 
12251
  return TRUE;
12252
}
12253
 
12254
/* Structure for saying that BFD machine EXTENSION extends BASE.  */
12255
 
12256
struct mips_mach_extension {
12257
  unsigned long extension, base;
12258
};
12259
 
12260
 
12261
/* An array describing how BFD machines relate to one another.  The entries
12262
   are ordered topologically with MIPS I extensions listed last.  */
12263
 
12264
static const struct mips_mach_extension mips_mach_extensions[] = {
12265
  /* MIPS64r2 extensions.  */
12266
  { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12267
 
12268
  /* MIPS64 extensions.  */
12269
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
12270
  { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
12271
  { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
12272
  { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
12273
 
12274
  /* MIPS V extensions.  */
12275
  { bfd_mach_mipsisa64, bfd_mach_mips5 },
12276
 
12277
  /* R10000 extensions.  */
12278
  { bfd_mach_mips12000, bfd_mach_mips10000 },
12279
  { bfd_mach_mips14000, bfd_mach_mips10000 },
12280
  { bfd_mach_mips16000, bfd_mach_mips10000 },
12281
 
12282
  /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
12283
     vr5400 ISA, but doesn't include the multimedia stuff.  It seems
12284
     better to allow vr5400 and vr5500 code to be merged anyway, since
12285
     many libraries will just use the core ISA.  Perhaps we could add
12286
     some sort of ASE flag if this ever proves a problem.  */
12287
  { bfd_mach_mips5500, bfd_mach_mips5400 },
12288
  { bfd_mach_mips5400, bfd_mach_mips5000 },
12289
 
12290
  /* MIPS IV extensions.  */
12291
  { bfd_mach_mips5, bfd_mach_mips8000 },
12292
  { bfd_mach_mips10000, bfd_mach_mips8000 },
12293
  { bfd_mach_mips5000, bfd_mach_mips8000 },
12294
  { bfd_mach_mips7000, bfd_mach_mips8000 },
12295
  { bfd_mach_mips9000, bfd_mach_mips8000 },
12296
 
12297
  /* VR4100 extensions.  */
12298
  { bfd_mach_mips4120, bfd_mach_mips4100 },
12299
  { bfd_mach_mips4111, bfd_mach_mips4100 },
12300
 
12301
  /* MIPS III extensions.  */
12302
  { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12303
  { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
12304
  { bfd_mach_mips8000, bfd_mach_mips4000 },
12305
  { bfd_mach_mips4650, bfd_mach_mips4000 },
12306
  { bfd_mach_mips4600, bfd_mach_mips4000 },
12307
  { bfd_mach_mips4400, bfd_mach_mips4000 },
12308
  { bfd_mach_mips4300, bfd_mach_mips4000 },
12309
  { bfd_mach_mips4100, bfd_mach_mips4000 },
12310
  { bfd_mach_mips4010, bfd_mach_mips4000 },
12311
 
12312
  /* MIPS32 extensions.  */
12313
  { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12314
 
12315
  /* MIPS II extensions.  */
12316
  { bfd_mach_mips4000, bfd_mach_mips6000 },
12317
  { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12318
 
12319
  /* MIPS I extensions.  */
12320
  { bfd_mach_mips6000, bfd_mach_mips3000 },
12321
  { bfd_mach_mips3900, bfd_mach_mips3000 }
12322
};
12323
 
12324
 
12325
/* Return true if bfd machine EXTENSION is an extension of machine BASE.  */
12326
 
12327
static bfd_boolean
12328
mips_mach_extends_p (unsigned long base, unsigned long extension)
12329
{
12330
  size_t i;
12331
 
12332
  if (extension == base)
12333
    return TRUE;
12334
 
12335
  if (base == bfd_mach_mipsisa32
12336
      && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12337
    return TRUE;
12338
 
12339
  if (base == bfd_mach_mipsisa32r2
12340
      && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12341
    return TRUE;
12342
 
12343
  for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
12344
    if (extension == mips_mach_extensions[i].extension)
12345
      {
12346
        extension = mips_mach_extensions[i].base;
12347
        if (extension == base)
12348
          return TRUE;
12349
      }
12350
 
12351
  return FALSE;
12352
}
12353
 
12354
 
12355
/* Return true if the given ELF header flags describe a 32-bit binary.  */
12356
 
12357
static bfd_boolean
12358
mips_32bit_flags_p (flagword flags)
12359
{
12360
  return ((flags & EF_MIPS_32BITMODE) != 0
12361
          || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12362
          || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12363
          || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12364
          || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12365
          || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12366
          || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
12367
}
12368
 
12369
 
12370
/* Merge object attributes from IBFD into OBFD.  Raise an error if
12371
   there are conflicting attributes.  */
12372
static bfd_boolean
12373
mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12374
{
12375
  obj_attribute *in_attr;
12376
  obj_attribute *out_attr;
12377
 
12378
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
12379
    {
12380
      /* This is the first object.  Copy the attributes.  */
12381
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
12382
 
12383
      /* Use the Tag_null value to indicate the attributes have been
12384
         initialized.  */
12385
      elf_known_obj_attributes_proc (obfd)[0].i = 1;
12386
 
12387
      return TRUE;
12388
    }
12389
 
12390
  /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12391
     non-conflicting ones.  */
12392
  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12393
  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12394
  if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12395
    {
12396
      out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12397
      if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12398
        out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12399
      else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12400
        ;
12401
      else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12402
        _bfd_error_handler
12403
          (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12404
           in_attr[Tag_GNU_MIPS_ABI_FP].i);
12405
      else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12406
        _bfd_error_handler
12407
          (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12408
           out_attr[Tag_GNU_MIPS_ABI_FP].i);
12409
      else
12410
        switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12411
          {
12412
          case 1:
12413
            switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12414
              {
12415
              case 2:
12416
                _bfd_error_handler
12417
                  (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12418
                   obfd, ibfd);
12419
                break;
12420
 
12421
              case 3:
12422
                _bfd_error_handler
12423
                  (_("Warning: %B uses hard float, %B uses soft float"),
12424
                   obfd, ibfd);
12425
                break;
12426
 
12427
              case 4:
12428
                _bfd_error_handler
12429
                  (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12430
                   obfd, ibfd);
12431
                break;
12432
 
12433
              default:
12434
                abort ();
12435
              }
12436
            break;
12437
 
12438
          case 2:
12439
            switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12440
              {
12441
              case 1:
12442
                _bfd_error_handler
12443
                  (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12444
                   ibfd, obfd);
12445
                break;
12446
 
12447
              case 3:
12448
                _bfd_error_handler
12449
                  (_("Warning: %B uses hard float, %B uses soft float"),
12450
                   obfd, ibfd);
12451
                break;
12452
 
12453
              case 4:
12454
                _bfd_error_handler
12455
                  (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12456
                   obfd, ibfd);
12457
                break;
12458
 
12459
              default:
12460
                abort ();
12461
              }
12462
            break;
12463
 
12464
          case 3:
12465
            switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12466
              {
12467
              case 1:
12468
              case 2:
12469
              case 4:
12470
                _bfd_error_handler
12471
                  (_("Warning: %B uses hard float, %B uses soft float"),
12472
                   ibfd, obfd);
12473
                break;
12474
 
12475
              default:
12476
                abort ();
12477
              }
12478
            break;
12479
 
12480
          case 4:
12481
            switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12482
              {
12483
              case 1:
12484
                _bfd_error_handler
12485
                  (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12486
                   ibfd, obfd);
12487
                break;
12488
 
12489
              case 2:
12490
                _bfd_error_handler
12491
                  (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12492
                   ibfd, obfd);
12493
                break;
12494
 
12495
              case 3:
12496
                _bfd_error_handler
12497
                  (_("Warning: %B uses hard float, %B uses soft float"),
12498
                   obfd, ibfd);
12499
                break;
12500
 
12501
              default:
12502
                abort ();
12503
              }
12504
            break;
12505
 
12506
          default:
12507
            abort ();
12508
          }
12509
    }
12510
 
12511
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
12512
  _bfd_elf_merge_object_attributes (ibfd, obfd);
12513
 
12514
  return TRUE;
12515
}
12516
 
12517
/* Merge backend specific data from an object file to the output
12518
   object file when linking.  */
12519
 
12520
bfd_boolean
12521
_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
12522
{
12523
  flagword old_flags;
12524
  flagword new_flags;
12525
  bfd_boolean ok;
12526
  bfd_boolean null_input_bfd = TRUE;
12527
  asection *sec;
12528
 
12529
  /* Check if we have the same endianness.  */
12530
  if (! _bfd_generic_verify_endian_match (ibfd, obfd))
12531
    {
12532
      (*_bfd_error_handler)
12533
        (_("%B: endianness incompatible with that of the selected emulation"),
12534
         ibfd);
12535
      return FALSE;
12536
    }
12537
 
12538
  if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
12539
    return TRUE;
12540
 
12541
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12542
    {
12543
      (*_bfd_error_handler)
12544
        (_("%B: ABI is incompatible with that of the selected emulation"),
12545
         ibfd);
12546
      return FALSE;
12547
    }
12548
 
12549
  if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12550
    return FALSE;
12551
 
12552
  new_flags = elf_elfheader (ibfd)->e_flags;
12553
  elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12554
  old_flags = elf_elfheader (obfd)->e_flags;
12555
 
12556
  if (! elf_flags_init (obfd))
12557
    {
12558
      elf_flags_init (obfd) = TRUE;
12559
      elf_elfheader (obfd)->e_flags = new_flags;
12560
      elf_elfheader (obfd)->e_ident[EI_CLASS]
12561
        = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12562
 
12563
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
12564
          && (bfd_get_arch_info (obfd)->the_default
12565
              || mips_mach_extends_p (bfd_get_mach (obfd),
12566
                                      bfd_get_mach (ibfd))))
12567
        {
12568
          if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12569
                                   bfd_get_mach (ibfd)))
12570
            return FALSE;
12571
        }
12572
 
12573
      return TRUE;
12574
    }
12575
 
12576
  /* Check flag compatibility.  */
12577
 
12578
  new_flags &= ~EF_MIPS_NOREORDER;
12579
  old_flags &= ~EF_MIPS_NOREORDER;
12580
 
12581
  /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
12582
     doesn't seem to matter.  */
12583
  new_flags &= ~EF_MIPS_XGOT;
12584
  old_flags &= ~EF_MIPS_XGOT;
12585
 
12586
  /* MIPSpro generates ucode info in n64 objects.  Again, we should
12587
     just be able to ignore this.  */
12588
  new_flags &= ~EF_MIPS_UCODE;
12589
  old_flags &= ~EF_MIPS_UCODE;
12590
 
12591
  /* DSOs should only be linked with CPIC code.  */
12592
  if ((ibfd->flags & DYNAMIC) != 0)
12593
    new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
12594
 
12595
  if (new_flags == old_flags)
12596
    return TRUE;
12597
 
12598
  /* Check to see if the input BFD actually contains any sections.
12599
     If not, its flags may not have been initialised either, but it cannot
12600
     actually cause any incompatibility.  */
12601
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12602
    {
12603
      /* Ignore synthetic sections and empty .text, .data and .bss sections
12604
         which are automatically generated by gas.  Also ignore fake
12605
         (s)common sections, since merely defining a common symbol does
12606
         not affect compatibility.  */
12607
      if ((sec->flags & SEC_IS_COMMON) == 0
12608
          && strcmp (sec->name, ".reginfo")
12609
          && strcmp (sec->name, ".mdebug")
12610
          && (sec->size != 0
12611
              || (strcmp (sec->name, ".text")
12612
                  && strcmp (sec->name, ".data")
12613
                  && strcmp (sec->name, ".bss"))))
12614
        {
12615
          null_input_bfd = FALSE;
12616
          break;
12617
        }
12618
    }
12619
  if (null_input_bfd)
12620
    return TRUE;
12621
 
12622
  ok = TRUE;
12623
 
12624
  if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12625
      != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
12626
    {
12627
      (*_bfd_error_handler)
12628
        (_("%B: warning: linking abicalls files with non-abicalls files"),
12629
         ibfd);
12630
      ok = TRUE;
12631
    }
12632
 
12633
  if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12634
    elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12635
  if (! (new_flags & EF_MIPS_PIC))
12636
    elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12637
 
12638
  new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12639
  old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12640
 
12641
  /* Compare the ISAs.  */
12642
  if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
12643
    {
12644
      (*_bfd_error_handler)
12645
        (_("%B: linking 32-bit code with 64-bit code"),
12646
         ibfd);
12647
      ok = FALSE;
12648
    }
12649
  else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12650
    {
12651
      /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
12652
      if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
12653
        {
12654
          /* Copy the architecture info from IBFD to OBFD.  Also copy
12655
             the 32-bit flag (if set) so that we continue to recognise
12656
             OBFD as a 32-bit binary.  */
12657
          bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12658
          elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12659
          elf_elfheader (obfd)->e_flags
12660
            |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12661
 
12662
          /* Copy across the ABI flags if OBFD doesn't use them
12663
             and if that was what caused us to treat IBFD as 32-bit.  */
12664
          if ((old_flags & EF_MIPS_ABI) == 0
12665
              && mips_32bit_flags_p (new_flags)
12666
              && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12667
            elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
12668
        }
12669
      else
12670
        {
12671
          /* The ISAs aren't compatible.  */
12672
          (*_bfd_error_handler)
12673
            (_("%B: linking %s module with previous %s modules"),
12674
             ibfd,
12675
             bfd_printable_name (ibfd),
12676
             bfd_printable_name (obfd));
12677
          ok = FALSE;
12678
        }
12679
    }
12680
 
12681
  new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12682
  old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12683
 
12684
  /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
12685
     does set EI_CLASS differently from any 32-bit ABI.  */
12686
  if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12687
      || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12688
          != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12689
    {
12690
      /* Only error if both are set (to different values).  */
12691
      if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12692
          || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12693
              != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12694
        {
12695
          (*_bfd_error_handler)
12696
            (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12697
             ibfd,
12698
             elf_mips_abi_name (ibfd),
12699
             elf_mips_abi_name (obfd));
12700
          ok = FALSE;
12701
        }
12702
      new_flags &= ~EF_MIPS_ABI;
12703
      old_flags &= ~EF_MIPS_ABI;
12704
    }
12705
 
12706
  /* For now, allow arbitrary mixing of ASEs (retain the union).  */
12707
  if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12708
    {
12709
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12710
 
12711
      new_flags &= ~ EF_MIPS_ARCH_ASE;
12712
      old_flags &= ~ EF_MIPS_ARCH_ASE;
12713
    }
12714
 
12715
  /* Warn about any other mismatches */
12716
  if (new_flags != old_flags)
12717
    {
12718
      (*_bfd_error_handler)
12719
        (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12720
         ibfd, (unsigned long) new_flags,
12721
         (unsigned long) old_flags);
12722
      ok = FALSE;
12723
    }
12724
 
12725
  if (! ok)
12726
    {
12727
      bfd_set_error (bfd_error_bad_value);
12728
      return FALSE;
12729
    }
12730
 
12731
  return TRUE;
12732
}
12733
 
12734
/* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
12735
 
12736
bfd_boolean
12737
_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
12738
{
12739
  BFD_ASSERT (!elf_flags_init (abfd)
12740
              || elf_elfheader (abfd)->e_flags == flags);
12741
 
12742
  elf_elfheader (abfd)->e_flags = flags;
12743
  elf_flags_init (abfd) = TRUE;
12744
  return TRUE;
12745
}
12746
 
12747
char *
12748
_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12749
{
12750
  switch (dtag)
12751
    {
12752
    default: return "";
12753
    case DT_MIPS_RLD_VERSION:
12754
      return "MIPS_RLD_VERSION";
12755
    case DT_MIPS_TIME_STAMP:
12756
      return "MIPS_TIME_STAMP";
12757
    case DT_MIPS_ICHECKSUM:
12758
      return "MIPS_ICHECKSUM";
12759
    case DT_MIPS_IVERSION:
12760
      return "MIPS_IVERSION";
12761
    case DT_MIPS_FLAGS:
12762
      return "MIPS_FLAGS";
12763
    case DT_MIPS_BASE_ADDRESS:
12764
      return "MIPS_BASE_ADDRESS";
12765
    case DT_MIPS_MSYM:
12766
      return "MIPS_MSYM";
12767
    case DT_MIPS_CONFLICT:
12768
      return "MIPS_CONFLICT";
12769
    case DT_MIPS_LIBLIST:
12770
      return "MIPS_LIBLIST";
12771
    case DT_MIPS_LOCAL_GOTNO:
12772
      return "MIPS_LOCAL_GOTNO";
12773
    case DT_MIPS_CONFLICTNO:
12774
      return "MIPS_CONFLICTNO";
12775
    case DT_MIPS_LIBLISTNO:
12776
      return "MIPS_LIBLISTNO";
12777
    case DT_MIPS_SYMTABNO:
12778
      return "MIPS_SYMTABNO";
12779
    case DT_MIPS_UNREFEXTNO:
12780
      return "MIPS_UNREFEXTNO";
12781
    case DT_MIPS_GOTSYM:
12782
      return "MIPS_GOTSYM";
12783
    case DT_MIPS_HIPAGENO:
12784
      return "MIPS_HIPAGENO";
12785
    case DT_MIPS_RLD_MAP:
12786
      return "MIPS_RLD_MAP";
12787
    case DT_MIPS_DELTA_CLASS:
12788
      return "MIPS_DELTA_CLASS";
12789
    case DT_MIPS_DELTA_CLASS_NO:
12790
      return "MIPS_DELTA_CLASS_NO";
12791
    case DT_MIPS_DELTA_INSTANCE:
12792
      return "MIPS_DELTA_INSTANCE";
12793
    case DT_MIPS_DELTA_INSTANCE_NO:
12794
      return "MIPS_DELTA_INSTANCE_NO";
12795
    case DT_MIPS_DELTA_RELOC:
12796
      return "MIPS_DELTA_RELOC";
12797
    case DT_MIPS_DELTA_RELOC_NO:
12798
      return "MIPS_DELTA_RELOC_NO";
12799
    case DT_MIPS_DELTA_SYM:
12800
      return "MIPS_DELTA_SYM";
12801
    case DT_MIPS_DELTA_SYM_NO:
12802
      return "MIPS_DELTA_SYM_NO";
12803
    case DT_MIPS_DELTA_CLASSSYM:
12804
      return "MIPS_DELTA_CLASSSYM";
12805
    case DT_MIPS_DELTA_CLASSSYM_NO:
12806
      return "MIPS_DELTA_CLASSSYM_NO";
12807
    case DT_MIPS_CXX_FLAGS:
12808
      return "MIPS_CXX_FLAGS";
12809
    case DT_MIPS_PIXIE_INIT:
12810
      return "MIPS_PIXIE_INIT";
12811
    case DT_MIPS_SYMBOL_LIB:
12812
      return "MIPS_SYMBOL_LIB";
12813
    case DT_MIPS_LOCALPAGE_GOTIDX:
12814
      return "MIPS_LOCALPAGE_GOTIDX";
12815
    case DT_MIPS_LOCAL_GOTIDX:
12816
      return "MIPS_LOCAL_GOTIDX";
12817
    case DT_MIPS_HIDDEN_GOTIDX:
12818
      return "MIPS_HIDDEN_GOTIDX";
12819
    case DT_MIPS_PROTECTED_GOTIDX:
12820
      return "MIPS_PROTECTED_GOT_IDX";
12821
    case DT_MIPS_OPTIONS:
12822
      return "MIPS_OPTIONS";
12823
    case DT_MIPS_INTERFACE:
12824
      return "MIPS_INTERFACE";
12825
    case DT_MIPS_DYNSTR_ALIGN:
12826
      return "DT_MIPS_DYNSTR_ALIGN";
12827
    case DT_MIPS_INTERFACE_SIZE:
12828
      return "DT_MIPS_INTERFACE_SIZE";
12829
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12830
      return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12831
    case DT_MIPS_PERF_SUFFIX:
12832
      return "DT_MIPS_PERF_SUFFIX";
12833
    case DT_MIPS_COMPACT_SIZE:
12834
      return "DT_MIPS_COMPACT_SIZE";
12835
    case DT_MIPS_GP_VALUE:
12836
      return "DT_MIPS_GP_VALUE";
12837
    case DT_MIPS_AUX_DYNAMIC:
12838
      return "DT_MIPS_AUX_DYNAMIC";
12839
    case DT_MIPS_PLTGOT:
12840
      return "DT_MIPS_PLTGOT";
12841
    case DT_MIPS_RWPLT:
12842
      return "DT_MIPS_RWPLT";
12843
    }
12844
}
12845
 
12846
bfd_boolean
12847
_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12848
{
12849
  FILE *file = ptr;
12850
 
12851
  BFD_ASSERT (abfd != NULL && ptr != NULL);
12852
 
12853
  /* Print normal ELF private data.  */
12854
  _bfd_elf_print_private_bfd_data (abfd, ptr);
12855
 
12856
  /* xgettext:c-format */
12857
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12858
 
12859
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12860
    fprintf (file, _(" [abi=O32]"));
12861
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12862
    fprintf (file, _(" [abi=O64]"));
12863
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12864
    fprintf (file, _(" [abi=EABI32]"));
12865
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12866
    fprintf (file, _(" [abi=EABI64]"));
12867
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12868
    fprintf (file, _(" [abi unknown]"));
12869
  else if (ABI_N32_P (abfd))
12870
    fprintf (file, _(" [abi=N32]"));
12871
  else if (ABI_64_P (abfd))
12872
    fprintf (file, _(" [abi=64]"));
12873
  else
12874
    fprintf (file, _(" [no abi set]"));
12875
 
12876
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12877
    fprintf (file, " [mips1]");
12878
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12879
    fprintf (file, " [mips2]");
12880
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12881
    fprintf (file, " [mips3]");
12882
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12883
    fprintf (file, " [mips4]");
12884
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12885
    fprintf (file, " [mips5]");
12886
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12887
    fprintf (file, " [mips32]");
12888
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12889
    fprintf (file, " [mips64]");
12890
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12891
    fprintf (file, " [mips32r2]");
12892
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12893
    fprintf (file, " [mips64r2]");
12894
  else
12895
    fprintf (file, _(" [unknown ISA]"));
12896
 
12897
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12898
    fprintf (file, " [mdmx]");
12899
 
12900
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12901
    fprintf (file, " [mips16]");
12902
 
12903
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12904
    fprintf (file, " [32bitmode]");
12905
  else
12906
    fprintf (file, _(" [not 32bitmode]"));
12907
 
12908
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12909
    fprintf (file, " [noreorder]");
12910
 
12911
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12912
    fprintf (file, " [PIC]");
12913
 
12914
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12915
    fprintf (file, " [CPIC]");
12916
 
12917
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12918
    fprintf (file, " [XGOT]");
12919
 
12920
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12921
    fprintf (file, " [UCODE]");
12922
 
12923
  fputc ('\n', file);
12924
 
12925
  return TRUE;
12926
}
12927
 
12928
const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12929
{
12930
  { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12931
  { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12932
  { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12933
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12934
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12935
  { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
12936
  { NULL,                     0,  0, 0,              0 }
12937
};
12938
 
12939
/* Merge non visibility st_other attributes.  Ensure that the
12940
   STO_OPTIONAL flag is copied into h->other, even if this is not a
12941
   definiton of the symbol.  */
12942
void
12943
_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12944
                                      const Elf_Internal_Sym *isym,
12945
                                      bfd_boolean definition,
12946
                                      bfd_boolean dynamic ATTRIBUTE_UNUSED)
12947
{
12948
  if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12949
    {
12950
      unsigned char other;
12951
 
12952
      other = (definition ? isym->st_other : h->other);
12953
      other &= ~ELF_ST_VISIBILITY (-1);
12954
      h->other = other | ELF_ST_VISIBILITY (h->other);
12955
    }
12956
 
12957
  if (!definition
12958
      && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12959
    h->other |= STO_OPTIONAL;
12960
}
12961
 
12962
/* Decide whether an undefined symbol is special and can be ignored.
12963
   This is the case for OPTIONAL symbols on IRIX.  */
12964
bfd_boolean
12965
_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12966
{
12967
  return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12968
}
12969
 
12970
bfd_boolean
12971
_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12972
{
12973
  return (sym->st_shndx == SHN_COMMON
12974
          || sym->st_shndx == SHN_MIPS_ACOMMON
12975
          || sym->st_shndx == SHN_MIPS_SCOMMON);
12976
}
12977
 
12978
/* Return address for Ith PLT stub in section PLT, for relocation REL
12979
   or (bfd_vma) -1 if it should not be included.  */
12980
 
12981
bfd_vma
12982
_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12983
                           const arelent *rel ATTRIBUTE_UNUSED)
12984
{
12985
  return (plt->vma
12986
          + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12987
          + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12988
}
12989
 
12990
void
12991
_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12992
{
12993
  struct mips_elf_link_hash_table *htab;
12994
  Elf_Internal_Ehdr *i_ehdrp;
12995
 
12996
  i_ehdrp = elf_elfheader (abfd);
12997
  if (link_info)
12998
    {
12999
      htab = mips_elf_hash_table (link_info);
13000
      BFD_ASSERT (htab != NULL);
13001
 
13002
      if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
13003
        i_ehdrp->e_ident[EI_ABIVERSION] = 1;
13004
    }
13005
}

powered by: WebSVN 2.1.0

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