Coverage Report

Created: 2023-08-28 06:23

/src/binutils-gdb/bfd/elfxx-mips.c
Line
Count
Source (jump to first uncovered line)
1
/* MIPS-specific support for ELF
2
   Copyright (C) 1993-2023 Free Software Foundation, Inc.
3
4
   Most of the information added by Ian Lance Taylor, Cygnus Support,
5
   <ian@cygnus.com>.
6
   N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7
   <mark@codesourcery.com>
8
   Traditional MIPS targets support added by Koundinya.K, Dansk Data
9
   Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
10
11
   This file is part of BFD, the Binary File Descriptor library.
12
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License as published by
15
   the Free Software Foundation; either version 3 of the License, or
16
   (at your option) any later version.
17
18
   This program is distributed in the hope that it will be useful,
19
   but WITHOUT ANY WARRANTY; without even the implied warranty of
20
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
   GNU General Public License for more details.
22
23
   You should have received a copy of the GNU General Public License
24
   along with this program; if not, write to the Free Software
25
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26
   MA 02110-1301, USA.  */
27
28
29
/* This file handles functionality common to the different MIPS ABI's.  */
30
31
#include "sysdep.h"
32
#include "bfd.h"
33
#include "libbfd.h"
34
#include "libiberty.h"
35
#include "elf-bfd.h"
36
#include "ecoff-bfd.h"
37
#include "elfxx-mips.h"
38
#include "elf/mips.h"
39
#include "elf-vxworks.h"
40
#include "dwarf2.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
/* Types of TLS GOT entry.  */
51
enum mips_got_tls_type {
52
  GOT_TLS_NONE,
53
  GOT_TLS_GD,
54
  GOT_TLS_LDM,
55
  GOT_TLS_IE
56
};
57
58
/* This structure is used to hold information about one GOT entry.
59
   There are four types of entry:
60
61
      (1) an absolute address
62
      requires: abfd == NULL
63
      fields: d.address
64
65
      (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66
      requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67
      fields: abfd, symndx, d.addend, tls_type
68
69
      (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70
      requires: abfd != NULL, symndx == -1
71
      fields: d.h, tls_type
72
73
      (4) a TLS LDM slot
74
      requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75
      fields: none; there's only one of these per GOT.  */
76
struct mips_got_entry
77
{
78
  /* One input bfd that needs the GOT entry.  */
79
  bfd *abfd;
80
  /* The index of the symbol, as stored in the relocation r_info, if
81
     we have a local symbol; -1 otherwise.  */
82
  long symndx;
83
  union
84
  {
85
    /* If abfd == NULL, an address that must be stored in the got.  */
86
    bfd_vma address;
87
    /* If abfd != NULL && symndx != -1, the addend of the relocation
88
       that should be added to the symbol value.  */
89
    bfd_vma addend;
90
    /* If abfd != NULL && symndx == -1, the hash table entry
91
       corresponding to a symbol in the GOT.  The symbol's entry
92
       is in the local area if h->global_got_area is GGA_NONE,
93
       otherwise it is in the global area.  */
94
    struct mips_elf_link_hash_entry *h;
95
  } d;
96
97
  /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
98
     symbol entry with r_symndx == 0.  */
99
  unsigned char tls_type;
100
101
  /* True if we have filled in the GOT contents for a TLS entry,
102
     and created the associated relocations.  */
103
  unsigned char tls_initialized;
104
105
  /* The offset from the beginning of the .got section to the entry
106
     corresponding to this symbol+addend.  If it's a global symbol
107
     whose offset is yet to be decided, it's going to be -1.  */
108
  long gotidx;
109
};
110
111
/* This structure represents a GOT page reference from an input bfd.
112
   Each instance represents a symbol + ADDEND, where the representation
113
   of the symbol depends on whether it is local to the input bfd.
114
   If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115
   Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
116
117
   Page references with SYMNDX >= 0 always become page references
118
   in the output.  Page references with SYMNDX < 0 only become page
119
   references if the symbol binds locally; in other cases, the page
120
   reference decays to a global GOT reference.  */
121
struct mips_got_page_ref
122
{
123
  long symndx;
124
  union
125
  {
126
    struct mips_elf_link_hash_entry *h;
127
    bfd *abfd;
128
  } u;
129
  bfd_vma addend;
130
};
131
132
/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133
   The structures form a non-overlapping list that is sorted by increasing
134
   MIN_ADDEND.  */
135
struct mips_got_page_range
136
{
137
  struct mips_got_page_range *next;
138
  bfd_signed_vma min_addend;
139
  bfd_signed_vma max_addend;
140
};
141
142
/* This structure describes the range of addends that are applied to page
143
   relocations against a given section.  */
144
struct mips_got_page_entry
145
{
146
  /* The section that these entries are based on.  */
147
  asection *sec;
148
  /* The ranges for this page entry.  */
149
  struct mips_got_page_range *ranges;
150
  /* The maximum number of page entries needed for RANGES.  */
151
  bfd_vma num_pages;
152
};
153
154
/* This structure is used to hold .got information when linking.  */
155
156
struct mips_got_info
157
{
158
  /* The number of global .got entries.  */
159
  unsigned int global_gotno;
160
  /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
161
  unsigned int reloc_only_gotno;
162
  /* The number of .got slots used for TLS.  */
163
  unsigned int tls_gotno;
164
  /* The first unused TLS .got entry.  Used only during
165
     mips_elf_initialize_tls_index.  */
166
  unsigned int tls_assigned_gotno;
167
  /* The number of local .got entries, eventually including page entries.  */
168
  unsigned int local_gotno;
169
  /* The maximum number of page entries needed.  */
170
  unsigned int page_gotno;
171
  /* The number of relocations needed for the GOT entries.  */
172
  unsigned int relocs;
173
  /* The first unused local .got entry.  */
174
  unsigned int assigned_low_gotno;
175
  /* The last unused local .got entry.  */
176
  unsigned int assigned_high_gotno;
177
  /* A hash table holding members of the got.  */
178
  struct htab *got_entries;
179
  /* A hash table holding mips_got_page_ref structures.  */
180
  struct htab *got_page_refs;
181
  /* A hash table of mips_got_page_entry structures.  */
182
  struct htab *got_page_entries;
183
  /* In multi-got links, a pointer to the next got (err, rather, most
184
     of the time, it points to the previous got).  */
185
  struct mips_got_info *next;
186
};
187
188
/* Structure passed when merging bfds' gots.  */
189
190
struct mips_elf_got_per_bfd_arg
191
{
192
  /* The output bfd.  */
193
  bfd *obfd;
194
  /* The link information.  */
195
  struct bfd_link_info *info;
196
  /* A pointer to the primary got, i.e., the one that's going to get
197
     the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198
     DT_MIPS_GOTSYM.  */
199
  struct mips_got_info *primary;
200
  /* A non-primary got we're trying to merge with other input bfd's
201
     gots.  */
202
  struct mips_got_info *current;
203
  /* The maximum number of got entries that can be addressed with a
204
     16-bit offset.  */
205
  unsigned int max_count;
206
  /* The maximum number of page entries needed by each got.  */
207
  unsigned int max_pages;
208
  /* The total number of global entries which will live in the
209
     primary got and be automatically relocated.  This includes
210
     those not referenced by the primary GOT but included in
211
     the "master" GOT.  */
212
  unsigned int global_count;
213
};
214
215
/* A structure used to pass information to htab_traverse callbacks
216
   when laying out the GOT.  */
217
218
struct mips_elf_traverse_got_arg
219
{
220
  struct bfd_link_info *info;
221
  struct mips_got_info *g;
222
  int value;
223
};
224
225
struct _mips_elf_section_data
226
{
227
  struct bfd_elf_section_data elf;
228
  union
229
  {
230
    bfd_byte *tdata;
231
  } u;
232
};
233
234
#define mips_elf_section_data(sec) \
235
0
  ((struct _mips_elf_section_data *) elf_section_data (sec))
236
237
#define is_mips_elf(bfd)        \
238
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
239
0
   && elf_tdata (bfd) != NULL        \
240
0
   && elf_object_id (bfd) == MIPS_ELF_DATA)
241
242
/* The ABI says that every symbol used by dynamic relocations must have
243
   a global GOT entry.  Among other things, this provides the dynamic
244
   linker with a free, directly-indexed cache.  The GOT can therefore
245
   contain symbols that are not referenced by GOT relocations themselves
246
   (in other words, it may have symbols that are not referenced by things
247
   like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
248
249
   GOT relocations are less likely to overflow if we put the associated
250
   GOT entries towards the beginning.  We therefore divide the global
251
   GOT entries into two areas: "normal" and "reloc-only".  Entries in
252
   the first area can be used for both dynamic relocations and GP-relative
253
   accesses, while those in the "reloc-only" area are for dynamic
254
   relocations only.
255
256
   These GGA_* ("Global GOT Area") values are organised so that lower
257
   values are more general than higher values.  Also, non-GGA_NONE
258
   values are ordered by the position of the area in the GOT.  */
259
0
#define GGA_NORMAL 0
260
0
#define GGA_RELOC_ONLY 1
261
0
#define GGA_NONE 2
262
263
/* Information about a non-PIC interface to a PIC function.  There are
264
   two ways of creating these interfaces.  The first is to add:
265
266
  lui $25,%hi(func)
267
  addiu $25,$25,%lo(func)
268
269
   immediately before a PIC function "func".  The second is to add:
270
271
  lui $25,%hi(func)
272
  j func
273
  addiu $25,$25,%lo(func)
274
275
   to a separate trampoline section.
276
277
   Stubs of the first kind go in a new section immediately before the
278
   target function.  Stubs of the second kind go in a single section
279
   pointed to by the hash table's "strampoline" field.  */
280
struct mips_elf_la25_stub {
281
  /* The generated section that contains this stub.  */
282
  asection *stub_section;
283
284
  /* The offset of the stub from the start of STUB_SECTION.  */
285
  bfd_vma offset;
286
287
  /* One symbol for the original function.  Its location is available
288
     in H->root.root.u.def.  */
289
  struct mips_elf_link_hash_entry *h;
290
};
291
292
/* Macros for populating a mips_elf_la25_stub.  */
293
294
#define LA25_LUI(VAL) (0x3c190000 | (VAL))  /* lui t9,VAL */
295
#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296
#define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297
#define LA25_ADDIU(VAL) (0x27390000 | (VAL))  /* addiu t9,t9,VAL */
298
#define LA25_LUI_MICROMIPS(VAL)           \
299
0
  (0x41b90000 | (VAL))        /* lui t9,VAL */
300
#define LA25_J_MICROMIPS(VAL)           \
301
0
  (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))  /* j VAL */
302
#define LA25_ADDIU_MICROMIPS(VAL)         \
303
0
  (0x33390000 | (VAL))        /* addiu t9,t9,VAL */
304
305
/* This structure is passed to mips_elf_sort_hash_table_f when sorting
306
   the dynamic symbols.  */
307
308
struct mips_elf_hash_sort_data
309
{
310
  /* The symbol in the global GOT with the lowest dynamic symbol table
311
     index.  */
312
  struct elf_link_hash_entry *low;
313
  /* The least dynamic symbol table index corresponding to a non-TLS
314
     symbol with a GOT entry.  */
315
  bfd_size_type min_got_dynindx;
316
  /* The greatest dynamic symbol table index corresponding to a symbol
317
     with a GOT entry that is not referenced (e.g., a dynamic symbol
318
     with dynamic relocations pointing to it from non-primary GOTs).  */
319
  bfd_size_type max_unref_got_dynindx;
320
  /* The greatest dynamic symbol table index corresponding to a local
321
     symbol.  */
322
  bfd_size_type max_local_dynindx;
323
  /* The greatest dynamic symbol table index corresponding to an external
324
     symbol without a GOT entry.  */
325
  bfd_size_type max_non_got_dynindx;
326
  /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
327
  bfd *output_bfd;
328
  /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329
     real final dynindx.  */
330
  bfd_byte *mipsxhash;
331
};
332
333
/* We make up to two PLT entries if needed, one for standard MIPS code
334
   and one for compressed code, either a MIPS16 or microMIPS one.  We
335
   keep a separate record of traditional lazy-binding stubs, for easier
336
   processing.  */
337
338
struct plt_entry
339
{
340
  /* Traditional SVR4 stub offset, or -1 if none.  */
341
  bfd_vma stub_offset;
342
343
  /* Standard PLT entry offset, or -1 if none.  */
344
  bfd_vma mips_offset;
345
346
  /* Compressed PLT entry offset, or -1 if none.  */
347
  bfd_vma comp_offset;
348
349
  /* The corresponding .got.plt index, or -1 if none.  */
350
  bfd_vma gotplt_index;
351
352
  /* Whether we need a standard PLT entry.  */
353
  unsigned int need_mips : 1;
354
355
  /* Whether we need a compressed PLT entry.  */
356
  unsigned int need_comp : 1;
357
};
358
359
/* The MIPS ELF linker needs additional information for each symbol in
360
   the global hash table.  */
361
362
struct mips_elf_link_hash_entry
363
{
364
  struct elf_link_hash_entry root;
365
366
  /* External symbol information.  */
367
  EXTR esym;
368
369
  /* The la25 stub we have created for ths symbol, if any.  */
370
  struct mips_elf_la25_stub *la25_stub;
371
372
  /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373
     this symbol.  */
374
  unsigned int possibly_dynamic_relocs;
375
376
  /* If there is a stub that 32 bit functions should use to call this
377
     16 bit function, this points to the section containing the stub.  */
378
  asection *fn_stub;
379
380
  /* If there is a stub that 16 bit functions should use to call this
381
     32 bit function, this points to the section containing the stub.  */
382
  asection *call_stub;
383
384
  /* This is like the call_stub field, but it is used if the function
385
     being called returns a floating point value.  */
386
  asection *call_fp_stub;
387
388
  /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
389
  bfd_vma mipsxhash_loc;
390
391
  /* The highest GGA_* value that satisfies all references to this symbol.  */
392
  unsigned int global_got_area : 2;
393
394
  /* True if all GOT relocations against this symbol are for calls.  This is
395
     a looser condition than no_fn_stub below, because there may be other
396
     non-call non-GOT relocations against the symbol.  */
397
  unsigned int got_only_for_calls : 1;
398
399
  /* True if one of the relocations described by possibly_dynamic_relocs
400
     is against a readonly section.  */
401
  unsigned int readonly_reloc : 1;
402
403
  /* True if there is a relocation against this symbol that must be
404
     resolved by the static linker (in other words, if the relocation
405
     cannot possibly be made dynamic).  */
406
  unsigned int has_static_relocs : 1;
407
408
  /* True if we must not create a .MIPS.stubs entry for this symbol.
409
     This is set, for example, if there are relocations related to
410
     taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411
     See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
412
  unsigned int no_fn_stub : 1;
413
414
  /* Whether we need the fn_stub; this is true if this symbol appears
415
     in any relocs other than a 16 bit call.  */
416
  unsigned int need_fn_stub : 1;
417
418
  /* True if this symbol is referenced by branch relocations from
419
     any non-PIC input file.  This is used to determine whether an
420
     la25 stub is required.  */
421
  unsigned int has_nonpic_branches : 1;
422
423
  /* Does this symbol need a traditional MIPS lazy-binding stub
424
     (as opposed to a PLT entry)?  */
425
  unsigned int needs_lazy_stub : 1;
426
427
  /* Does this symbol resolve to a PLT entry?  */
428
  unsigned int use_plt_entry : 1;
429
};
430
431
/* MIPS ELF linker hash table.  */
432
433
struct mips_elf_link_hash_table
434
{
435
  struct elf_link_hash_table root;
436
437
  /* The number of .rtproc entries.  */
438
  bfd_size_type procedure_count;
439
440
  /* The size of the .compact_rel section (if SGI_COMPAT).  */
441
  bfd_size_type compact_rel_size;
442
443
  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444
     is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
445
  bool use_rld_obj_head;
446
447
  /* The  __rld_map or __rld_obj_head symbol. */
448
  struct elf_link_hash_entry *rld_symbol;
449
450
  /* This is set if we see any mips16 stub sections.  */
451
  bool mips16_stubs_seen;
452
453
  /* True if we can generate copy relocs and PLTs.  */
454
  bool use_plts_and_copy_relocs;
455
456
  /* True if we can only use 32-bit microMIPS instructions.  */
457
  bool insn32;
458
459
  /* True if we suppress checks for invalid branches between ISA modes.  */
460
  bool ignore_branch_isa;
461
462
  /* True if we are targetting R6 compact branches.  */
463
  bool compact_branches;
464
465
  /* True if we already reported the small-data section overflow.  */
466
  bool small_data_overflow_reported;
467
468
  /* True if we use the special `__gnu_absolute_zero' symbol.  */
469
  bool use_absolute_zero;
470
471
  /* True if we have been configured for a GNU target.  */
472
  bool gnu_target;
473
474
  /* Shortcuts to some dynamic sections, or NULL if they are not
475
     being used.  */
476
  asection *srelplt2;
477
  asection *sstubs;
478
479
  /* The master GOT information.  */
480
  struct mips_got_info *got_info;
481
482
  /* The global symbol in the GOT with the lowest index in the dynamic
483
     symbol table.  */
484
  struct elf_link_hash_entry *global_gotsym;
485
486
  /* The size of the PLT header in bytes.  */
487
  bfd_vma plt_header_size;
488
489
  /* The size of a standard PLT entry in bytes.  */
490
  bfd_vma plt_mips_entry_size;
491
492
  /* The size of a compressed PLT entry in bytes.  */
493
  bfd_vma plt_comp_entry_size;
494
495
  /* The offset of the next standard PLT entry to create.  */
496
  bfd_vma plt_mips_offset;
497
498
  /* The offset of the next compressed PLT entry to create.  */
499
  bfd_vma plt_comp_offset;
500
501
  /* The index of the next .got.plt entry to create.  */
502
  bfd_vma plt_got_index;
503
504
  /* The number of functions that need a lazy-binding stub.  */
505
  bfd_vma lazy_stub_count;
506
507
  /* The size of a function stub entry in bytes.  */
508
  bfd_vma function_stub_size;
509
510
  /* The number of reserved entries at the beginning of the GOT.  */
511
  unsigned int reserved_gotno;
512
513
  /* The section used for mips_elf_la25_stub trampolines.
514
     See the comment above that structure for details.  */
515
  asection *strampoline;
516
517
  /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518
     pairs.  */
519
  htab_t la25_stubs;
520
521
  /* A function FN (NAME, IS, OS) that creates a new input section
522
     called NAME and links it to output section OS.  If IS is nonnull,
523
     the new section should go immediately before it, otherwise it
524
     should go at the (current) beginning of OS.
525
526
     The function returns the new section on success, otherwise it
527
     returns null.  */
528
  asection *(*add_stub_section) (const char *, asection *, asection *);
529
530
  /* Is the PLT header compressed?  */
531
  unsigned int plt_header_is_comp : 1;
532
};
533
534
/* Get the MIPS ELF linker hash table from a link_info structure.  */
535
536
#define mips_elf_hash_table(p) \
537
0
  ((is_elf_hash_table ((p)->hash)          \
538
0
    && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)   \
539
0
   ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
540
541
/* A structure used to communicate with htab_traverse callbacks.  */
542
struct mips_htab_traverse_info
543
{
544
  /* The usual link-wide information.  */
545
  struct bfd_link_info *info;
546
  bfd *output_bfd;
547
548
  /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
549
  bool error;
550
};
551
552
/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553
   R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
554
   that contains the relocation field and DATA points to the start of
555
   INPUT_SECTION.  */
556
557
struct mips_hi16
558
{
559
  struct mips_hi16 *next;
560
  bfd_byte *data;
561
  asection *input_section;
562
  arelent rel;
563
};
564
565
/* MIPS ELF private object data.  */
566
567
struct mips_elf_obj_tdata
568
{
569
  /* Generic ELF private object data.  */
570
  struct elf_obj_tdata root;
571
572
  /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
573
  bfd *abi_fp_bfd;
574
575
  /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
576
  bfd *abi_msa_bfd;
577
578
  /* The abiflags for this object.  */
579
  Elf_Internal_ABIFlags_v0 abiflags;
580
  bool abiflags_valid;
581
582
  /* The GOT requirements of input bfds.  */
583
  struct mips_got_info *got;
584
585
  /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
586
     included directly in this one, but there's no point to wasting
587
     the memory just for the infrequently called find_nearest_line.  */
588
  struct mips_elf_find_line *find_line_info;
589
590
  /* An array of stub sections indexed by symbol number.  */
591
  asection **local_stubs;
592
  asection **local_call_stubs;
593
594
  /* The Irix 5 support uses two virtual sections, which represent
595
     text/data symbols defined in dynamic objects.  */
596
  asymbol *elf_data_symbol;
597
  asymbol *elf_text_symbol;
598
  asection *elf_data_section;
599
  asection *elf_text_section;
600
601
  struct mips_hi16 *mips_hi16_list;
602
};
603
604
/* Get MIPS ELF private object data from BFD's tdata.  */
605
606
#define mips_elf_tdata(bfd) \
607
0
  ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
608
609
#define TLS_RELOC_P(r_type) \
610
0
  (r_type == R_MIPS_TLS_DTPMOD32    \
611
0
   || r_type == R_MIPS_TLS_DTPMOD64    \
612
0
   || r_type == R_MIPS_TLS_DTPREL32    \
613
0
   || r_type == R_MIPS_TLS_DTPREL64    \
614
0
   || r_type == R_MIPS_TLS_GD      \
615
0
   || r_type == R_MIPS_TLS_LDM      \
616
0
   || r_type == R_MIPS_TLS_DTPREL_HI16    \
617
0
   || r_type == R_MIPS_TLS_DTPREL_LO16    \
618
0
   || r_type == R_MIPS_TLS_GOTTPREL    \
619
0
   || r_type == R_MIPS_TLS_TPREL32    \
620
0
   || r_type == R_MIPS_TLS_TPREL64    \
621
0
   || r_type == R_MIPS_TLS_TPREL_HI16    \
622
0
   || r_type == R_MIPS_TLS_TPREL_LO16    \
623
0
   || r_type == R_MIPS16_TLS_GD      \
624
0
   || r_type == R_MIPS16_TLS_LDM    \
625
0
   || r_type == R_MIPS16_TLS_DTPREL_HI16  \
626
0
   || r_type == R_MIPS16_TLS_DTPREL_LO16  \
627
0
   || r_type == R_MIPS16_TLS_GOTTPREL    \
628
0
   || r_type == R_MIPS16_TLS_TPREL_HI16    \
629
0
   || r_type == R_MIPS16_TLS_TPREL_LO16    \
630
0
   || r_type == R_MICROMIPS_TLS_GD    \
631
0
   || r_type == R_MICROMIPS_TLS_LDM    \
632
0
   || r_type == R_MICROMIPS_TLS_DTPREL_HI16  \
633
0
   || r_type == R_MICROMIPS_TLS_DTPREL_LO16  \
634
0
   || r_type == R_MICROMIPS_TLS_GOTTPREL  \
635
0
   || r_type == R_MICROMIPS_TLS_TPREL_HI16  \
636
0
   || r_type == R_MICROMIPS_TLS_TPREL_LO16)
637
638
/* Structure used to pass information to mips_elf_output_extsym.  */
639
640
struct extsym_info
641
{
642
  bfd *abfd;
643
  struct bfd_link_info *info;
644
  struct ecoff_debug_info *debug;
645
  const struct ecoff_debug_swap *swap;
646
  bool failed;
647
};
648
649
/* The names of the runtime procedure table symbols used on IRIX5.  */
650
651
static const char * const mips_elf_dynsym_rtproc_names[] =
652
{
653
  "_procedure_table",
654
  "_procedure_string_table",
655
  "_procedure_table_size",
656
  NULL
657
};
658
659
/* These structures are used to generate the .compact_rel section on
660
   IRIX5.  */
661
662
typedef struct
663
{
664
  unsigned long id1;    /* Always one?  */
665
  unsigned long num;    /* Number of compact relocation entries.  */
666
  unsigned long id2;    /* Always two?  */
667
  unsigned long offset;   /* The file offset of the first relocation.  */
668
  unsigned long reserved0;  /* Zero?  */
669
  unsigned long reserved1;  /* Zero?  */
670
} Elf32_compact_rel;
671
672
typedef struct
673
{
674
  bfd_byte id1[4];
675
  bfd_byte num[4];
676
  bfd_byte id2[4];
677
  bfd_byte offset[4];
678
  bfd_byte reserved0[4];
679
  bfd_byte reserved1[4];
680
} Elf32_External_compact_rel;
681
682
typedef struct
683
{
684
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
685
  unsigned int rtype : 4; /* Relocation types. See below.  */
686
  unsigned int dist2to : 8;
687
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688
  unsigned long konst;    /* KONST field. See below.  */
689
  unsigned long vaddr;    /* VADDR to be relocated.  */
690
} Elf32_crinfo;
691
692
typedef struct
693
{
694
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
695
  unsigned int rtype : 4; /* Relocation types. See below.  */
696
  unsigned int dist2to : 8;
697
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698
  unsigned long konst;    /* KONST field. See below.  */
699
} Elf32_crinfo2;
700
701
typedef struct
702
{
703
  bfd_byte info[4];
704
  bfd_byte konst[4];
705
  bfd_byte vaddr[4];
706
} Elf32_External_crinfo;
707
708
typedef struct
709
{
710
  bfd_byte info[4];
711
  bfd_byte konst[4];
712
} Elf32_External_crinfo2;
713
714
/* These are the constants used to swap the bitfields in a crinfo.  */
715
716
0
#define CRINFO_CTYPE (0x1U)
717
0
#define CRINFO_CTYPE_SH (31)
718
0
#define CRINFO_RTYPE (0xfU)
719
0
#define CRINFO_RTYPE_SH (27)
720
0
#define CRINFO_DIST2TO (0xffU)
721
0
#define CRINFO_DIST2TO_SH (19)
722
0
#define CRINFO_RELVADDR (0x7ffffU)
723
0
#define CRINFO_RELVADDR_SH (0)
724
725
/* A compact relocation info has long (3 words) or short (2 words)
726
   formats.  A short format doesn't have VADDR field and relvaddr
727
   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
728
#define CRF_MIPS_LONG     1
729
#define CRF_MIPS_SHORT      0
730
731
/* There are 4 types of compact relocation at least. The value KONST
732
   has different meaning for each type:
733
734
   (type)   (konst)
735
   CT_MIPS_REL32  Address in data
736
   CT_MIPS_WORD   Address in word (XXX)
737
   CT_MIPS_GPHI_LO  GP - vaddr
738
   CT_MIPS_JMPAD  Address to jump
739
   */
740
741
#define CRT_MIPS_REL32      0xa
742
#define CRT_MIPS_WORD     0xb
743
#define CRT_MIPS_GPHI_LO    0xc
744
#define CRT_MIPS_JMPAD      0xd
745
746
0
#define mips_elf_set_cr_format(x,format)  ((x).ctype = (format))
747
0
#define mips_elf_set_cr_type(x,type)    ((x).rtype = (type))
748
0
#define mips_elf_set_cr_dist2to(x,v)    ((x).dist2to = (v))
749
0
#define mips_elf_set_cr_relvaddr(x,d)   ((x).relvaddr = (d)<<2)
750

751
/* The structure of the runtime procedure descriptor created by the
752
   loader for use by the static exception system.  */
753
754
typedef struct runtime_pdr {
755
  bfd_vma adr;    /* Memory address of start of procedure.  */
756
  long  regmask;  /* Save register mask.  */
757
  long  regoffset;  /* Save register offset.  */
758
  long  fregmask; /* Save floating point register mask.  */
759
  long  fregoffset; /* Save floating point register offset.  */
760
  long  frameoffset;  /* Frame size.  */
761
  short framereg; /* Frame pointer register.  */
762
  short pcreg;    /* Offset or reg of return pc.  */
763
  long  irpss;    /* Index into the runtime string table.  */
764
  long  reserved;
765
  struct exception_info *exception_info;/* Pointer to exception array.  */
766
} RPDR, *pRPDR;
767
#define cbRPDR sizeof (RPDR)
768
#define rpdNil ((pRPDR) 0)
769

770
static struct mips_got_entry *mips_elf_create_local_got_entry
771
  (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772
   struct mips_elf_link_hash_entry *, int);
773
static bool mips_elf_sort_hash_table_f
774
  (struct mips_elf_link_hash_entry *, void *);
775
static bfd_vma mips_elf_high
776
  (bfd_vma);
777
static bool mips_elf_create_dynamic_relocation
778
  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779
   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780
   bfd_vma *, asection *);
781
static bfd_vma mips_elf_adjust_gp
782
  (bfd *, struct mips_got_info *, bfd *);
783
784
/* This will be used when we sort the dynamic relocation records.  */
785
static bfd *reldyn_sorting_bfd;
786
787
/* True if ABFD is for CPUs with load interlocking that include
788
   non-MIPS1 CPUs and R3900.  */
789
#define LOAD_INTERLOCKS_P(abfd) \
790
0
  (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
791
0
   || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
792
793
/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794
   This should be safe for all architectures.  We enable this predicate
795
   for RM9000 for now.  */
796
#define JAL_TO_BAL_P(abfd) \
797
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
798
799
/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800
   This should be safe for all architectures.  We enable this predicate for
801
   all CPUs.  */
802
0
#define JALR_TO_BAL_P(abfd) 1
803
804
/* True if ABFD is for CPUs that are faster if JR is converted to B.
805
   This should be safe for all architectures.  We enable this predicate for
806
   all CPUs.  */
807
0
#define JR_TO_B_P(abfd) 1
808
809
/* True if ABFD is a PIC object.  */
810
#define PIC_OBJECT_P(abfd) \
811
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
812
813
/* Nonzero if ABFD is using the O32 ABI.  */
814
#define ABI_O32_P(abfd) \
815
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
816
817
/* Nonzero if ABFD is using the N32 ABI.  */
818
#define ABI_N32_P(abfd) \
819
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
820
821
/* Nonzero if ABFD is using the N64 ABI.  */
822
#define ABI_64_P(abfd) \
823
0
  (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
824
825
/* Nonzero if ABFD is using NewABI conventions.  */
826
0
#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
827
828
/* Nonzero if ABFD has microMIPS code.  */
829
#define MICROMIPS_P(abfd) \
830
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
831
832
/* Nonzero if ABFD is MIPS R6.  */
833
#define MIPSR6_P(abfd) \
834
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
835
0
    || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
836
837
/* The IRIX compatibility level we are striving for.  */
838
#define IRIX_COMPAT(abfd) \
839
0
  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
840
841
/* Whether we are trying to be compatible with IRIX at all.  */
842
#define SGI_COMPAT(abfd) \
843
0
  (IRIX_COMPAT (abfd) != ict_none)
844
845
/* The name of the options section.  */
846
#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847
0
  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
848
849
/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850
   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
851
#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852
0
  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
853
854
/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
855
#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856
0
  (strcmp (NAME, ".MIPS.abiflags") == 0)
857
858
/* Whether the section is readonly.  */
859
#define MIPS_ELF_READONLY_SECTION(sec) \
860
0
  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))    \
861
0
   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
862
863
/* The name of the stub section.  */
864
0
#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
865
866
/* The size of an external REL relocation.  */
867
#define MIPS_ELF_REL_SIZE(abfd) \
868
0
  (get_elf_backend_data (abfd)->s->sizeof_rel)
869
870
/* The size of an external RELA relocation.  */
871
#define MIPS_ELF_RELA_SIZE(abfd) \
872
0
  (get_elf_backend_data (abfd)->s->sizeof_rela)
873
874
/* The size of an external dynamic table entry.  */
875
#define MIPS_ELF_DYN_SIZE(abfd) \
876
0
  (get_elf_backend_data (abfd)->s->sizeof_dyn)
877
878
/* The size of a GOT entry.  */
879
#define MIPS_ELF_GOT_SIZE(abfd) \
880
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
881
882
/* The size of the .rld_map section. */
883
#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
885
886
/* The size of a symbol-table entry.  */
887
#define MIPS_ELF_SYM_SIZE(abfd) \
888
0
  (get_elf_backend_data (abfd)->s->sizeof_sym)
889
890
/* The default alignment for sections, as a power of two.  */
891
#define MIPS_ELF_LOG_FILE_ALIGN(abfd)       \
892
0
  (get_elf_backend_data (abfd)->s->log_file_align)
893
894
/* Get word-sized data.  */
895
#define MIPS_ELF_GET_WORD(abfd, ptr) \
896
  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
897
898
/* Put out word-sized data.  */
899
#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
900
0
  (ABI_64_P (abfd)       \
901
0
   ? bfd_put_64 (abfd, val, ptr)   \
902
0
   : bfd_put_32 (abfd, val, ptr))
903
904
/* The opcode for word-sized loads (LW or LD).  */
905
#define MIPS_ELF_LOAD_WORD(abfd) \
906
0
  (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
907
908
/* Add a dynamic symbol table-entry.  */
909
#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)  \
910
0
  _bfd_elf_add_dynamic_entry (info, tag, val)
911
912
#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)      \
913
0
  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
914
915
/* The name of the dynamic relocation section.  */
916
#define MIPS_ELF_REL_DYN_NAME(INFO) \
917
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918
0
   ? ".rela.dyn" : ".rel.dyn")
919
920
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921
   from smaller values.  Start with zero, widen, *then* decrement.  */
922
0
#define MINUS_ONE (((bfd_vma)0) - 1)
923
0
#define MINUS_TWO (((bfd_vma)0) - 2)
924
925
/* The value to write into got[1] for SVR4 targets, to identify it is
926
   a GNU object.  The dynamic linker can then use got[1] to store the
927
   module pointer.  */
928
#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929
  ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
930
931
/* The offset of $gp from the beginning of the .got section.  */
932
#define ELF_MIPS_GP_OFFSET(INFO) \
933
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934
0
   ? 0x0 : 0x7ff0)
935
936
/* The maximum size of the GOT for it to be addressable using 16-bit
937
   offsets from $gp.  */
938
0
#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
939
940
/* Instructions which appear in a stub.  */
941
#define STUB_LW(abfd)             \
942
  ((ABI_64_P (abfd)             \
943
    ? 0xdf998010        /* ld t9,0x8010(gp) */  \
944
    : 0x8f998010))        /* lw t9,0x8010(gp) */
945
#define STUB_MOVE 0x03e07825      /* or t7,ra,zero */
946
#define STUB_LUI(VAL) (0x3c180000 + (VAL))  /* lui t8,VAL */
947
#define STUB_JALR 0x0320f809      /* jalr ra,t9 */
948
#define STUB_JALRC 0xf8190000     /* jalrc ra,t9 */
949
#define STUB_ORI(VAL) (0x37180000 + (VAL))  /* ori t8,t8,VAL */
950
#define STUB_LI16U(VAL) (0x34180000 + (VAL))  /* ori t8,zero,VAL unsigned */
951
#define STUB_LI16S(abfd, VAL)           \
952
   ((ABI_64_P (abfd)              \
953
    ? (0x64180000 + (VAL))  /* daddiu t8,zero,VAL sign extended */  \
954
    : (0x24180000 + (VAL))))  /* addiu t8,zero,VAL sign extended */
955
956
/* Likewise for the microMIPS ASE.  */
957
#define STUB_LW_MICROMIPS(abfd)           \
958
0
  (ABI_64_P (abfd)             \
959
0
   ? 0xdf3c8010          /* ld t9,0x8010(gp) */  \
960
0
   : 0xff3c8010)        /* lw t9,0x8010(gp) */
961
#define STUB_MOVE_MICROMIPS 0x0dff    /* move t7,ra */
962
0
#define STUB_MOVE32_MICROMIPS 0x001f7a90  /* or t7,ra,zero */
963
#define STUB_LUI_MICROMIPS(VAL)           \
964
0
   (0x41b80000 + (VAL))        /* lui t8,VAL */
965
#define STUB_JALR_MICROMIPS 0x45d9    /* jalr t9 */
966
0
#define STUB_JALR32_MICROMIPS 0x03f90f3c  /* jalr ra,t9 */
967
#define STUB_ORI_MICROMIPS(VAL)           \
968
0
  (0x53180000 + (VAL))        /* ori t8,t8,VAL */
969
#define STUB_LI16U_MICROMIPS(VAL)         \
970
0
  (0x53000000 + (VAL))        /* ori t8,zero,VAL unsigned */
971
#define STUB_LI16S_MICROMIPS(abfd, VAL)         \
972
0
   (ABI_64_P (abfd)             \
973
0
    ? 0x5f000000 + (VAL)  /* daddiu t8,zero,VAL sign extended */  \
974
0
    : 0x33000000 + (VAL))  /* addiu t8,zero,VAL sign extended */
975
976
0
#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977
0
#define MIPS_FUNCTION_STUB_BIG_SIZE 20
978
0
#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979
0
#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980
0
#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981
0
#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
982
983
/* The name of the dynamic interpreter.  This is put in the .interp
984
   section.  */
985
986
#define ELF_DYNAMIC_INTERPRETER(abfd)   \
987
0
   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"  \
988
0
    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
989
0
    : "/usr/lib/libc.so.1")
990
991
#ifdef BFD64
992
#define MNAME(bfd,pre,pos) \
993
  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994
#define ELF_R_SYM(bfd, i)         \
995
0
  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996
#define ELF_R_TYPE(bfd, i)          \
997
0
  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998
#define ELF_R_INFO(bfd, s, t)         \
999
0
  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000
#else
1001
#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002
#define ELF_R_SYM(bfd, i)         \
1003
  (ELF32_R_SYM (i))
1004
#define ELF_R_TYPE(bfd, i)          \
1005
  (ELF32_R_TYPE (i))
1006
#define ELF_R_INFO(bfd, s, t)         \
1007
  (ELF32_R_INFO (s, t))
1008
#endif
1009

1010
  /* The mips16 compiler uses a couple of special sections to handle
1011
     floating point arguments.
1012
1013
     Section names that look like .mips16.fn.FNNAME contain stubs that
1014
     copy floating point arguments from the fp regs to the gp regs and
1015
     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1016
     call should be redirected to the stub instead.  If no 32 bit
1017
     function calls FNNAME, the stub should be discarded.  We need to
1018
     consider any reference to the function, not just a call, because
1019
     if the address of the function is taken we will need the stub,
1020
     since the address might be passed to a 32 bit function.
1021
1022
     Section names that look like .mips16.call.FNNAME contain stubs
1023
     that copy floating point arguments from the gp regs to the fp
1024
     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1025
     then any 16 bit function that calls FNNAME should be redirected
1026
     to the stub instead.  If FNNAME is not a 32 bit function, the
1027
     stub should be discarded.
1028
1029
     .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030
     which call FNNAME and then copy the return value from the fp regs
1031
     to the gp regs.  These stubs store the return value in $18 while
1032
     calling FNNAME; any function which might call one of these stubs
1033
     must arrange to save $18 around the call.  (This case is not
1034
     needed for 32 bit functions that call 16 bit functions, because
1035
     16 bit functions always return floating point values in both
1036
     $f0/$f1 and $2/$3.)
1037
1038
     Note that in all cases FNNAME might be defined statically.
1039
     Therefore, FNNAME is not used literally.  Instead, the relocation
1040
     information will indicate which symbol the section is for.
1041
1042
     We record any stubs that we find in the symbol table.  */
1043
1044
0
#define FN_STUB ".mips16.fn."
1045
0
#define CALL_STUB ".mips16.call."
1046
0
#define CALL_FP_STUB ".mips16.call.fp."
1047
1048
0
#define FN_STUB_P(name) startswith (name, FN_STUB)
1049
0
#define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050
0
#define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1051

1052
/* The format of the first PLT entry in an O32 executable.  */
1053
static const bfd_vma mips_o32_exec_plt0_entry[] =
1054
{
1055
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1056
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1057
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1058
  0x031cc023, /* subu $24, $24, $28         */
1059
  0x03e07825, /* or t7, ra, zero          */
1060
  0x0018c082, /* srl $24, $24, 2          */
1061
  0x0320f809, /* jalr $25           */
1062
  0x2718fffe  /* subu $24, $24, 2         */
1063
};
1064
1065
/* The format of the first PLT entry in an O32 executable using compact
1066
   jumps.  */
1067
static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1068
{
1069
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1070
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1071
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1072
  0x031cc023, /* subu $24, $24, $28         */
1073
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1074
  0x0018c082, /* srl $24, $24, 2          */
1075
  0x2718fffe, /* subu $24, $24, 2         */
1076
  0xf8190000  /* jalrc $25            */
1077
};
1078
1079
/* The format of the first PLT entry in an N32 executable.  Different
1080
   because gp ($28) is not available; we use t2 ($14) instead.  */
1081
static const bfd_vma mips_n32_exec_plt0_entry[] =
1082
{
1083
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1084
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1085
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1086
  0x030ec023, /* subu $24, $24, $14         */
1087
  0x03e07825, /* or t7, ra, zero          */
1088
  0x0018c082, /* srl $24, $24, 2          */
1089
  0x0320f809, /* jalr $25           */
1090
  0x2718fffe  /* subu $24, $24, 2         */
1091
};
1092
1093
/* The format of the first PLT entry in an N32 executable using compact
1094
   jumps.  Different because gp ($28) is not available; we use t2 ($14)
1095
   instead.  */
1096
static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1097
{
1098
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1099
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1100
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1101
  0x030ec023, /* subu $24, $24, $14         */
1102
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1103
  0x0018c082, /* srl $24, $24, 2          */
1104
  0x2718fffe, /* subu $24, $24, 2         */
1105
  0xf8190000  /* jalrc $25            */
1106
};
1107
1108
/* The format of the first PLT entry in an N64 executable.  Different
1109
   from N32 because of the increased size of GOT entries.  */
1110
static const bfd_vma mips_n64_exec_plt0_entry[] =
1111
{
1112
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1113
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1114
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1115
  0x030ec023, /* subu $24, $24, $14         */
1116
  0x03e07825, /* or t7, ra, zero          */
1117
  0x0018c0c2, /* srl $24, $24, 3          */
1118
  0x0320f809, /* jalr $25           */
1119
  0x2718fffe  /* subu $24, $24, 2         */
1120
};
1121
1122
/* The format of the first PLT entry in an N64 executable using compact
1123
   jumps.  Different from N32 because of the increased size of GOT
1124
   entries.  */
1125
static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1126
{
1127
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1128
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1129
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1130
  0x030ec023, /* subu $24, $24, $14         */
1131
  0x03e0782d, /* move $15, $31  # 64-bit move (daddu)   */
1132
  0x0018c0c2, /* srl $24, $24, 3          */
1133
  0x2718fffe, /* subu $24, $24, 2         */
1134
  0xf8190000  /* jalrc $25            */
1135
};
1136
1137
1138
/* The format of the microMIPS first PLT entry in an O32 executable.
1139
   We rely on v0 ($2) rather than t8 ($24) to contain the address
1140
   of the GOTPLT entry handled, so this stub may only be used when
1141
   all the subsequent PLT entries are microMIPS code too.
1142
1143
   The trailing NOP is for alignment and correct disassembly only.  */
1144
static const bfd_vma micromips_o32_exec_plt0_entry[] =
1145
{
1146
  0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - .     */
1147
  0xff23, 0x0000, /* lw $25, 0($3)        */
1148
  0x0535,   /* subu $2, $2, $3        */
1149
  0x2525,   /* srl $2, $2, 2        */
1150
  0x3302, 0xfffe, /* subu $24, $2, 2        */
1151
  0x0dff,   /* move $15, $31        */
1152
  0x45f9,   /* jalrs $25          */
1153
  0x0f83,   /* move $28, $3         */
1154
  0x0c00    /* nop            */
1155
};
1156
1157
/* The format of the microMIPS first PLT entry in an O32 executable
1158
   in the insn32 mode.  */
1159
static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1160
{
1161
  0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0])     */
1162
  0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28)     */
1163
  0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0])    */
1164
  0x0398, 0xc1d0, /* subu $24, $24, $28       */
1165
  0x001f, 0x7a90, /* or $15, $31, zero        */
1166
  0x0318, 0x1040, /* srl $24, $24, 2        */
1167
  0x03f9, 0x0f3c, /* jalr $25         */
1168
  0x3318, 0xfffe  /* subu $24, $24, 2       */
1169
};
1170
1171
/* The format of subsequent standard PLT entries.  */
1172
static const bfd_vma mips_exec_plt_entry[] =
1173
{
1174
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1175
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1176
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1177
  0x03200008  /* jr $25         */
1178
};
1179
1180
static const bfd_vma mipsr6_exec_plt_entry[] =
1181
{
1182
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1183
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1184
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1185
  0x03200009  /* jr $25         */
1186
};
1187
1188
static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1189
{
1190
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1191
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1192
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1193
  0xd8190000  /* jic $25, 0         */
1194
};
1195
1196
/* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
1197
   and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198
   directly addressable.  */
1199
static const bfd_vma mips16_o32_exec_plt_entry[] =
1200
{
1201
  0xb203,   /* lw $2, 12($pc)     */
1202
  0x9a60,   /* lw $3, 0($2)       */
1203
  0x651a,   /* move $24, $2       */
1204
  0xeb00,   /* jr $3        */
1205
  0x653b,   /* move $25, $3       */
1206
  0x6500,   /* nop          */
1207
  0x0000, 0x0000  /* .word (.got.plt entry)   */
1208
};
1209
1210
/* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
1211
   as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
1212
static const bfd_vma micromips_o32_exec_plt_entry[] =
1213
{
1214
  0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215
  0xff22, 0x0000, /* lw $25, 0($2)      */
1216
  0x4599,   /* jr $25       */
1217
  0x0f02    /* move $24, $2       */
1218
};
1219
1220
/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
1221
static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1222
{
1223
  0x41af, 0x0000, /* lui $15, %hi(.got.plt entry)   */
1224
  0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225
  0x0019, 0x0f3c, /* jr $25       */
1226
  0x330f, 0x0000  /* addiu $24, $15, %lo(.got.plt entry)  */
1227
};
1228
1229
/* The format of the first PLT entry in a VxWorks executable.  */
1230
static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1231
{
1232
  0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)   */
1233
  0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234
  0x8f390008, /* lw t9, 8(t9)         */
1235
  0x00000000, /* nop            */
1236
  0x03200008, /* jr t9          */
1237
  0x00000000  /* nop            */
1238
};
1239
1240
/* The format of subsequent PLT entries.  */
1241
static const bfd_vma mips_vxworks_exec_plt_entry[] =
1242
{
1243
  0x10000000, /* b .PLT_resolver      */
1244
  0x24180000, /* li t8, <pltindex>      */
1245
  0x3c190000, /* lui t9, %hi(<.got.plt slot>)   */
1246
  0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247
  0x8f390000, /* lw t9, 0(t9)       */
1248
  0x00000000, /* nop          */
1249
  0x03200008, /* jr t9        */
1250
  0x00000000  /* nop          */
1251
};
1252
1253
/* The format of the first PLT entry in a VxWorks shared object.  */
1254
static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1255
{
1256
  0x8f990008, /* lw t9, 8(gp)   */
1257
  0x00000000, /* nop      */
1258
  0x03200008, /* jr t9    */
1259
  0x00000000, /* nop      */
1260
  0x00000000, /* nop      */
1261
  0x00000000  /* nop      */
1262
};
1263
1264
/* The format of subsequent PLT entries.  */
1265
static const bfd_vma mips_vxworks_shared_plt_entry[] =
1266
{
1267
  0x10000000, /* b .PLT_resolver  */
1268
  0x24180000  /* li t8, <pltindex>  */
1269
};
1270

1271
/* microMIPS 32-bit opcode helper installer.  */
1272
1273
static void
1274
bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1275
0
{
1276
0
  bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277
0
  bfd_put_16 (abfd,  opcode    & 0xffff, ptr + 2);
1278
0
}
1279
1280
/* microMIPS 32-bit opcode helper retriever.  */
1281
1282
static bfd_vma
1283
bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1284
0
{
1285
0
  return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286
0
}
1287

1288
/* Look up an entry in a MIPS ELF linker hash table.  */
1289
1290
#define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
1291
0
  ((struct mips_elf_link_hash_entry *)          \
1292
0
   elf_link_hash_lookup (&(table)->root, (string), (create),    \
1293
0
       (copy), (follow)))
1294
1295
/* Traverse a MIPS ELF linker hash table.  */
1296
1297
#define mips_elf_link_hash_traverse(table, func, info)      \
1298
0
  (elf_link_hash_traverse           \
1299
0
   (&(table)->root,              \
1300
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
1301
0
    (info)))
1302
1303
/* Find the base offsets for thread-local storage in this object,
1304
   for GD/LD and IE/LE respectively.  */
1305
1306
0
#define TP_OFFSET 0x7000
1307
0
#define DTP_OFFSET 0x8000
1308
1309
static bfd_vma
1310
dtprel_base (struct bfd_link_info *info)
1311
0
{
1312
  /* If tls_sec is NULL, we should have signalled an error already.  */
1313
0
  if (elf_hash_table (info)->tls_sec == NULL)
1314
0
    return 0;
1315
0
  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316
0
}
1317
1318
static bfd_vma
1319
tprel_base (struct bfd_link_info *info)
1320
0
{
1321
  /* If tls_sec is NULL, we should have signalled an error already.  */
1322
0
  if (elf_hash_table (info)->tls_sec == NULL)
1323
0
    return 0;
1324
0
  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325
0
}
1326
1327
/* Create an entry in a MIPS ELF linker hash table.  */
1328
1329
static struct bfd_hash_entry *
1330
mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331
          struct bfd_hash_table *table, const char *string)
1332
0
{
1333
0
  struct mips_elf_link_hash_entry *ret =
1334
0
    (struct mips_elf_link_hash_entry *) entry;
1335
1336
  /* Allocate the structure if it has not already been allocated by a
1337
     subclass.  */
1338
0
  if (ret == NULL)
1339
0
    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340
0
  if (ret == NULL)
1341
0
    return (struct bfd_hash_entry *) ret;
1342
1343
  /* Call the allocation method of the superclass.  */
1344
0
  ret = ((struct mips_elf_link_hash_entry *)
1345
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346
0
             table, string));
1347
0
  if (ret != NULL)
1348
0
    {
1349
      /* Set local fields.  */
1350
0
      memset (&ret->esym, 0, sizeof (EXTR));
1351
      /* We use -2 as a marker to indicate that the information has
1352
   not been set.  -1 means there is no associated ifd.  */
1353
0
      ret->esym.ifd = -2;
1354
0
      ret->la25_stub = 0;
1355
0
      ret->possibly_dynamic_relocs = 0;
1356
0
      ret->fn_stub = NULL;
1357
0
      ret->call_stub = NULL;
1358
0
      ret->call_fp_stub = NULL;
1359
0
      ret->mipsxhash_loc = 0;
1360
0
      ret->global_got_area = GGA_NONE;
1361
0
      ret->got_only_for_calls = true;
1362
0
      ret->readonly_reloc = false;
1363
0
      ret->has_static_relocs = false;
1364
0
      ret->no_fn_stub = false;
1365
0
      ret->need_fn_stub = false;
1366
0
      ret->has_nonpic_branches = false;
1367
0
      ret->needs_lazy_stub = false;
1368
0
      ret->use_plt_entry = false;
1369
0
    }
1370
1371
0
  return (struct bfd_hash_entry *) ret;
1372
0
}
1373
1374
/* Allocate MIPS ELF private object data.  */
1375
1376
bool
1377
_bfd_mips_elf_mkobject (bfd *abfd)
1378
0
{
1379
0
  return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380
0
          MIPS_ELF_DATA);
1381
0
}
1382
1383
/* MIPS ELF uses a special find_nearest_line routine in order the
1384
   handle the ECOFF debugging information.  */
1385
1386
struct mips_elf_find_line
1387
{
1388
  struct ecoff_debug_info d;
1389
  struct ecoff_find_line i;
1390
};
1391
1392
bool
1393
_bfd_mips_elf_free_cached_info (bfd *abfd)
1394
0
{
1395
0
  struct mips_elf_obj_tdata *tdata;
1396
1397
0
  if ((bfd_get_format (abfd) == bfd_object
1398
0
       || bfd_get_format (abfd) == bfd_core)
1399
0
      && (tdata = mips_elf_tdata (abfd)) != NULL)
1400
0
    {
1401
0
      BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402
0
      while (tdata->mips_hi16_list != NULL)
1403
0
  {
1404
0
    struct mips_hi16 *hi = tdata->mips_hi16_list;
1405
0
    tdata->mips_hi16_list = hi->next;
1406
0
    free (hi);
1407
0
  }
1408
0
      if (tdata->find_line_info != NULL)
1409
0
  _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1410
0
    }
1411
0
  return _bfd_elf_free_cached_info (abfd);
1412
0
}
1413
1414
bool
1415
_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1416
0
{
1417
0
  if (!sec->used_by_bfd)
1418
0
    {
1419
0
      struct _mips_elf_section_data *sdata;
1420
0
      size_t amt = sizeof (*sdata);
1421
1422
0
      sdata = bfd_zalloc (abfd, amt);
1423
0
      if (sdata == NULL)
1424
0
  return false;
1425
0
      sec->used_by_bfd = sdata;
1426
0
    }
1427
1428
0
  return _bfd_elf_new_section_hook (abfd, sec);
1429
0
}
1430

1431
/* Read ECOFF debugging information from a .mdebug section into a
1432
   ecoff_debug_info structure.  */
1433
1434
bool
1435
_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436
             struct ecoff_debug_info *debug)
1437
0
{
1438
0
  HDRR *symhdr;
1439
0
  const struct ecoff_debug_swap *swap;
1440
0
  char *ext_hdr;
1441
1442
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443
0
  memset (debug, 0, sizeof (*debug));
1444
1445
0
  ext_hdr = bfd_malloc (swap->external_hdr_size);
1446
0
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447
0
    goto error_return;
1448
1449
0
  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450
0
          swap->external_hdr_size))
1451
0
    goto error_return;
1452
1453
0
  symhdr = &debug->symbolic_header;
1454
0
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455
0
  free (ext_hdr);
1456
0
  ext_hdr = NULL;
1457
1458
  /* The symbolic header contains absolute file offsets and sizes to
1459
     read.  */
1460
0
#define READ(ptr, offset, count, size)          \
1461
0
  do                  \
1462
0
    {                 \
1463
0
      size_t amt;             \
1464
0
      debug->ptr = NULL;            \
1465
0
      if (symhdr->count == 0)           \
1466
0
  break;               \
1467
0
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
1468
0
  {               \
1469
0
    bfd_set_error (bfd_error_file_too_big);     \
1470
0
    goto error_return;            \
1471
0
  }                \
1472
0
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)   \
1473
0
  goto error_return;           \
1474
0
      debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);   \
1475
0
      if (debug->ptr == NULL)           \
1476
0
  goto error_return;           \
1477
0
      ((char *) debug->ptr)[amt] = 0;         \
1478
0
    } while (0)
1479
1480
0
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481
0
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482
0
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483
0
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484
0
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485
0
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486
0
  READ (ss, cbSsOffset, issMax, sizeof (char));
1487
0
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488
0
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489
0
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490
0
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491
0
#undef READ
1492
1493
0
  return true;
1494
1495
0
 error_return:
1496
0
  free (ext_hdr);
1497
0
  _bfd_ecoff_free_ecoff_debug_info (debug);
1498
0
  return false;
1499
0
}
1500

1501
/* Swap RPDR (runtime procedure table entry) for output.  */
1502
1503
static void
1504
ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1505
0
{
1506
0
  H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507
0
  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508
0
  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509
0
  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510
0
  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511
0
  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1512
1513
0
  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514
0
  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1515
1516
0
  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1517
0
}
1518
1519
/* Create a runtime procedure table from the .mdebug section.  */
1520
1521
static bool
1522
mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523
         struct bfd_link_info *info, asection *s,
1524
         struct ecoff_debug_info *debug)
1525
0
{
1526
0
  const struct ecoff_debug_swap *swap;
1527
0
  HDRR *hdr = &debug->symbolic_header;
1528
0
  RPDR *rpdr, *rp;
1529
0
  struct rpdr_ext *erp;
1530
0
  void *rtproc;
1531
0
  struct pdr_ext *epdr;
1532
0
  struct sym_ext *esym;
1533
0
  char *ss, **sv;
1534
0
  char *str;
1535
0
  bfd_size_type size;
1536
0
  bfd_size_type count;
1537
0
  unsigned long sindex;
1538
0
  unsigned long i;
1539
0
  PDR pdr;
1540
0
  SYMR sym;
1541
0
  const char *no_name_func = _("static procedure (no name)");
1542
1543
0
  epdr = NULL;
1544
0
  rpdr = NULL;
1545
0
  esym = NULL;
1546
0
  ss = NULL;
1547
0
  sv = NULL;
1548
1549
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1550
1551
0
  sindex = strlen (no_name_func) + 1;
1552
0
  count = hdr->ipdMax;
1553
0
  if (count > 0)
1554
0
    {
1555
0
      size = swap->external_pdr_size;
1556
1557
0
      epdr = bfd_malloc (size * count);
1558
0
      if (epdr == NULL)
1559
0
  goto error_return;
1560
1561
0
      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562
0
  goto error_return;
1563
1564
0
      size = sizeof (RPDR);
1565
0
      rp = rpdr = bfd_malloc (size * count);
1566
0
      if (rpdr == NULL)
1567
0
  goto error_return;
1568
1569
0
      size = sizeof (char *);
1570
0
      sv = bfd_malloc (size * count);
1571
0
      if (sv == NULL)
1572
0
  goto error_return;
1573
1574
0
      count = hdr->isymMax;
1575
0
      size = swap->external_sym_size;
1576
0
      esym = bfd_malloc (size * count);
1577
0
      if (esym == NULL)
1578
0
  goto error_return;
1579
1580
0
      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581
0
  goto error_return;
1582
1583
0
      count = hdr->issMax;
1584
0
      ss = bfd_malloc (count);
1585
0
      if (ss == NULL)
1586
0
  goto error_return;
1587
0
      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588
0
  goto error_return;
1589
1590
0
      count = hdr->ipdMax;
1591
0
      for (i = 0; i < (unsigned long) count; i++, rp++)
1592
0
  {
1593
0
    (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594
0
    (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595
0
    rp->adr = sym.value;
1596
0
    rp->regmask = pdr.regmask;
1597
0
    rp->regoffset = pdr.regoffset;
1598
0
    rp->fregmask = pdr.fregmask;
1599
0
    rp->fregoffset = pdr.fregoffset;
1600
0
    rp->frameoffset = pdr.frameoffset;
1601
0
    rp->framereg = pdr.framereg;
1602
0
    rp->pcreg = pdr.pcreg;
1603
0
    rp->irpss = sindex;
1604
0
    sv[i] = ss + sym.iss;
1605
0
    sindex += strlen (sv[i]) + 1;
1606
0
  }
1607
0
    }
1608
1609
0
  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610
0
  size = BFD_ALIGN (size, 16);
1611
0
  rtproc = bfd_alloc (abfd, size);
1612
0
  if (rtproc == NULL)
1613
0
    {
1614
0
      mips_elf_hash_table (info)->procedure_count = 0;
1615
0
      goto error_return;
1616
0
    }
1617
1618
0
  mips_elf_hash_table (info)->procedure_count = count + 2;
1619
1620
0
  erp = rtproc;
1621
0
  memset (erp, 0, sizeof (struct rpdr_ext));
1622
0
  erp++;
1623
0
  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624
0
  strcpy (str, no_name_func);
1625
0
  str += strlen (no_name_func) + 1;
1626
0
  for (i = 0; i < count; i++)
1627
0
    {
1628
0
      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629
0
      strcpy (str, sv[i]);
1630
0
      str += strlen (sv[i]) + 1;
1631
0
    }
1632
0
  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1633
1634
  /* Set the size and contents of .rtproc section.  */
1635
0
  s->size = size;
1636
0
  s->contents = rtproc;
1637
1638
  /* Skip this section later on (I don't think this currently
1639
     matters, but someday it might).  */
1640
0
  s->map_head.link_order = NULL;
1641
1642
0
  free (epdr);
1643
0
  free (rpdr);
1644
0
  free (esym);
1645
0
  free (ss);
1646
0
  free (sv);
1647
0
  return true;
1648
1649
0
 error_return:
1650
0
  free (epdr);
1651
0
  free (rpdr);
1652
0
  free (esym);
1653
0
  free (ss);
1654
0
  free (sv);
1655
0
  return false;
1656
0
}
1657

1658
/* We're going to create a stub for H.  Create a symbol for the stub's
1659
   value and size, to help make the disassembly easier to read.  */
1660
1661
static bool
1662
mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663
           struct mips_elf_link_hash_entry *h,
1664
           const char *prefix, asection *s, bfd_vma value,
1665
           bfd_vma size)
1666
0
{
1667
0
  bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668
0
  struct bfd_link_hash_entry *bh;
1669
0
  struct elf_link_hash_entry *elfh;
1670
0
  char *name;
1671
0
  bool res;
1672
1673
0
  if (micromips_p)
1674
0
    value |= 1;
1675
1676
  /* Create a new symbol.  */
1677
0
  name = concat (prefix, h->root.root.root.string, NULL);
1678
0
  bh = NULL;
1679
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680
0
            BSF_LOCAL, s, value, NULL,
1681
0
            true, false, &bh);
1682
0
  free (name);
1683
0
  if (! res)
1684
0
    return false;
1685
1686
  /* Make it a local function.  */
1687
0
  elfh = (struct elf_link_hash_entry *) bh;
1688
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689
0
  elfh->size = size;
1690
0
  elfh->forced_local = 1;
1691
0
  if (micromips_p)
1692
0
    elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693
0
  return true;
1694
0
}
1695
1696
/* We're about to redefine H.  Create a symbol to represent H's
1697
   current value and size, to help make the disassembly easier
1698
   to read.  */
1699
1700
static bool
1701
mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702
             struct mips_elf_link_hash_entry *h,
1703
             const char *prefix)
1704
0
{
1705
0
  struct bfd_link_hash_entry *bh;
1706
0
  struct elf_link_hash_entry *elfh;
1707
0
  char *name;
1708
0
  asection *s;
1709
0
  bfd_vma value;
1710
0
  bool res;
1711
1712
  /* Read the symbol's value.  */
1713
0
  BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714
0
        || h->root.root.type == bfd_link_hash_defweak);
1715
0
  s = h->root.root.u.def.section;
1716
0
  value = h->root.root.u.def.value;
1717
1718
  /* Create a new symbol.  */
1719
0
  name = concat (prefix, h->root.root.root.string, NULL);
1720
0
  bh = NULL;
1721
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722
0
            BSF_LOCAL, s, value, NULL,
1723
0
            true, false, &bh);
1724
0
  free (name);
1725
0
  if (! res)
1726
0
    return false;
1727
1728
  /* Make it local and copy the other attributes from H.  */
1729
0
  elfh = (struct elf_link_hash_entry *) bh;
1730
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731
0
  elfh->other = h->root.other;
1732
0
  elfh->size = h->root.size;
1733
0
  elfh->forced_local = 1;
1734
0
  return true;
1735
0
}
1736
1737
/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738
   function rather than to a hard-float stub.  */
1739
1740
static bool
1741
section_allows_mips16_refs_p (asection *section)
1742
0
{
1743
0
  const char *name;
1744
1745
0
  name = bfd_section_name (section);
1746
0
  return (FN_STUB_P (name)
1747
0
    || CALL_STUB_P (name)
1748
0
    || CALL_FP_STUB_P (name)
1749
0
    || strcmp (name, ".pdr") == 0);
1750
0
}
1751
1752
/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753
   stub section of some kind.  Return the R_SYMNDX of the target
1754
   function, or 0 if we can't decide which function that is.  */
1755
1756
static unsigned long
1757
mips16_stub_symndx (const struct elf_backend_data *bed,
1758
        asection *sec ATTRIBUTE_UNUSED,
1759
        const Elf_Internal_Rela *relocs,
1760
        const Elf_Internal_Rela *relend)
1761
0
{
1762
0
  int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763
0
  const Elf_Internal_Rela *rel;
1764
1765
  /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766
     one in a compound relocation.  */
1767
0
  for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768
0
    if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769
0
      return ELF_R_SYM (sec->owner, rel->r_info);
1770
1771
  /* Otherwise trust the first relocation, whatever its kind.  This is
1772
     the traditional behavior.  */
1773
0
  if (relocs < relend)
1774
0
    return ELF_R_SYM (sec->owner, relocs->r_info);
1775
1776
0
  return 0;
1777
0
}
1778
1779
/* Check the mips16 stubs for a particular symbol, and see if we can
1780
   discard them.  */
1781
1782
static void
1783
mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784
           struct mips_elf_link_hash_entry *h)
1785
0
{
1786
  /* Dynamic symbols must use the standard call interface, in case other
1787
     objects try to call them.  */
1788
0
  if (h->fn_stub != NULL
1789
0
      && h->root.dynindx != -1)
1790
0
    {
1791
0
      mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792
0
      h->need_fn_stub = true;
1793
0
    }
1794
1795
0
  if (h->fn_stub != NULL
1796
0
      && ! h->need_fn_stub)
1797
0
    {
1798
      /* We don't need the fn_stub; the only references to this symbol
1799
   are 16 bit calls.  Clobber the size to 0 to prevent it from
1800
   being included in the link.  */
1801
0
      h->fn_stub->size = 0;
1802
0
      h->fn_stub->flags &= ~SEC_RELOC;
1803
0
      h->fn_stub->reloc_count = 0;
1804
0
      h->fn_stub->flags |= SEC_EXCLUDE;
1805
0
      h->fn_stub->output_section = bfd_abs_section_ptr;
1806
0
    }
1807
1808
0
  if (h->call_stub != NULL
1809
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1810
0
    {
1811
      /* We don't need the call_stub; this is a 16 bit function, so
1812
   calls from other 16 bit functions are OK.  Clobber the size
1813
   to 0 to prevent it from being included in the link.  */
1814
0
      h->call_stub->size = 0;
1815
0
      h->call_stub->flags &= ~SEC_RELOC;
1816
0
      h->call_stub->reloc_count = 0;
1817
0
      h->call_stub->flags |= SEC_EXCLUDE;
1818
0
      h->call_stub->output_section = bfd_abs_section_ptr;
1819
0
    }
1820
1821
0
  if (h->call_fp_stub != NULL
1822
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1823
0
    {
1824
      /* We don't need the call_stub; this is a 16 bit function, so
1825
   calls from other 16 bit functions are OK.  Clobber the size
1826
   to 0 to prevent it from being included in the link.  */
1827
0
      h->call_fp_stub->size = 0;
1828
0
      h->call_fp_stub->flags &= ~SEC_RELOC;
1829
0
      h->call_fp_stub->reloc_count = 0;
1830
0
      h->call_fp_stub->flags |= SEC_EXCLUDE;
1831
0
      h->call_fp_stub->output_section = bfd_abs_section_ptr;
1832
0
    }
1833
0
}
1834
1835
/* Hashtable callbacks for mips_elf_la25_stubs.  */
1836
1837
static hashval_t
1838
mips_elf_la25_stub_hash (const void *entry_)
1839
0
{
1840
0
  const struct mips_elf_la25_stub *entry;
1841
1842
0
  entry = (struct mips_elf_la25_stub *) entry_;
1843
0
  return entry->h->root.root.u.def.section->id
1844
0
    + entry->h->root.root.u.def.value;
1845
0
}
1846
1847
static int
1848
mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1849
0
{
1850
0
  const struct mips_elf_la25_stub *entry1, *entry2;
1851
1852
0
  entry1 = (struct mips_elf_la25_stub *) entry1_;
1853
0
  entry2 = (struct mips_elf_la25_stub *) entry2_;
1854
0
  return ((entry1->h->root.root.u.def.section
1855
0
     == entry2->h->root.root.u.def.section)
1856
0
    && (entry1->h->root.root.u.def.value
1857
0
        == entry2->h->root.root.u.def.value));
1858
0
}
1859
1860
/* Called by the linker to set up the la25 stub-creation code.  FN is
1861
   the linker's implementation of add_stub_function.  Return true on
1862
   success.  */
1863
1864
bool
1865
_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866
        asection *(*fn) (const char *, asection *,
1867
             asection *))
1868
0
{
1869
0
  struct mips_elf_link_hash_table *htab;
1870
1871
0
  htab = mips_elf_hash_table (info);
1872
0
  if (htab == NULL)
1873
0
    return false;
1874
1875
0
  htab->add_stub_section = fn;
1876
0
  htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877
0
              mips_elf_la25_stub_eq, NULL);
1878
0
  if (htab->la25_stubs == NULL)
1879
0
    return false;
1880
1881
0
  return true;
1882
0
}
1883
1884
/* Return true if H is a locally-defined PIC function, in the sense
1885
   that it or its fn_stub might need $25 to be valid on entry.
1886
   Note that MIPS16 functions set up $gp using PC-relative instructions,
1887
   so they themselves never need $25 to be valid.  Only non-MIPS16
1888
   entry points are of interest here.  */
1889
1890
static bool
1891
mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1892
0
{
1893
0
  return ((h->root.root.type == bfd_link_hash_defined
1894
0
     || h->root.root.type == bfd_link_hash_defweak)
1895
0
    && h->root.def_regular
1896
0
    && !bfd_is_abs_section (h->root.root.u.def.section)
1897
0
    && !bfd_is_und_section (h->root.root.u.def.section)
1898
0
    && (!ELF_ST_IS_MIPS16 (h->root.other)
1899
0
        || (h->fn_stub && h->need_fn_stub))
1900
0
    && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901
0
        || ELF_ST_IS_MIPS_PIC (h->root.other)));
1902
0
}
1903
1904
/* Set *SEC to the input section that contains the target of STUB.
1905
   Return the offset of the target from the start of that section.  */
1906
1907
static bfd_vma
1908
mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909
        asection **sec)
1910
0
{
1911
0
  if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1912
0
    {
1913
0
      BFD_ASSERT (stub->h->need_fn_stub);
1914
0
      *sec = stub->h->fn_stub;
1915
0
      return 0;
1916
0
    }
1917
0
  else
1918
0
    {
1919
0
      *sec = stub->h->root.root.u.def.section;
1920
0
      return stub->h->root.root.u.def.value;
1921
0
    }
1922
0
}
1923
1924
/* STUB describes an la25 stub that we have decided to implement
1925
   by inserting an LUI/ADDIU pair before the target function.
1926
   Create the section and redirect the function symbol to it.  */
1927
1928
static bool
1929
mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930
       struct bfd_link_info *info)
1931
0
{
1932
0
  struct mips_elf_link_hash_table *htab;
1933
0
  char *name;
1934
0
  asection *s, *input_section;
1935
0
  unsigned int align;
1936
1937
0
  htab = mips_elf_hash_table (info);
1938
0
  if (htab == NULL)
1939
0
    return false;
1940
1941
  /* Create a unique name for the new section.  */
1942
0
  name = bfd_malloc (11 + sizeof (".text.stub."));
1943
0
  if (name == NULL)
1944
0
    return false;
1945
0
  sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1946
1947
  /* Create the section.  */
1948
0
  mips_elf_get_la25_target (stub, &input_section);
1949
0
  s = htab->add_stub_section (name, input_section,
1950
0
            input_section->output_section);
1951
0
  if (s == NULL)
1952
0
    return false;
1953
1954
  /* Make sure that any padding goes before the stub.  */
1955
0
  align = input_section->alignment_power;
1956
0
  if (!bfd_set_section_alignment (s, align))
1957
0
    return false;
1958
0
  if (align > 3)
1959
0
    s->size = (1 << align) - 8;
1960
1961
  /* Create a symbol for the stub.  */
1962
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963
0
  stub->stub_section = s;
1964
0
  stub->offset = s->size;
1965
1966
  /* Allocate room for it.  */
1967
0
  s->size += 8;
1968
0
  return true;
1969
0
}
1970
1971
/* STUB describes an la25 stub that we have decided to implement
1972
   with a separate trampoline.  Allocate room for it and redirect
1973
   the function symbol to it.  */
1974
1975
static bool
1976
mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977
            struct bfd_link_info *info)
1978
0
{
1979
0
  struct mips_elf_link_hash_table *htab;
1980
0
  asection *s;
1981
1982
0
  htab = mips_elf_hash_table (info);
1983
0
  if (htab == NULL)
1984
0
    return false;
1985
1986
  /* Create a trampoline section, if we haven't already.  */
1987
0
  s = htab->strampoline;
1988
0
  if (s == NULL)
1989
0
    {
1990
0
      asection *input_section = stub->h->root.root.u.def.section;
1991
0
      s = htab->add_stub_section (".text", NULL,
1992
0
          input_section->output_section);
1993
0
      if (s == NULL || !bfd_set_section_alignment (s, 4))
1994
0
  return false;
1995
0
      htab->strampoline = s;
1996
0
    }
1997
1998
  /* Create a symbol for the stub.  */
1999
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000
0
  stub->stub_section = s;
2001
0
  stub->offset = s->size;
2002
2003
  /* Allocate room for it.  */
2004
0
  s->size += 16;
2005
0
  return true;
2006
0
}
2007
2008
/* H describes a symbol that needs an la25 stub.  Make sure that an
2009
   appropriate stub exists and point H at it.  */
2010
2011
static bool
2012
mips_elf_add_la25_stub (struct bfd_link_info *info,
2013
      struct mips_elf_link_hash_entry *h)
2014
0
{
2015
0
  struct mips_elf_link_hash_table *htab;
2016
0
  struct mips_elf_la25_stub search, *stub;
2017
0
  bool use_trampoline_p;
2018
0
  asection *s;
2019
0
  bfd_vma value;
2020
0
  void **slot;
2021
2022
  /* Describe the stub we want.  */
2023
0
  search.stub_section = NULL;
2024
0
  search.offset = 0;
2025
0
  search.h = h;
2026
2027
  /* See if we've already created an equivalent stub.  */
2028
0
  htab = mips_elf_hash_table (info);
2029
0
  if (htab == NULL)
2030
0
    return false;
2031
2032
0
  slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033
0
  if (slot == NULL)
2034
0
    return false;
2035
2036
0
  stub = (struct mips_elf_la25_stub *) *slot;
2037
0
  if (stub != NULL)
2038
0
    {
2039
      /* We can reuse the existing stub.  */
2040
0
      h->la25_stub = stub;
2041
0
      return true;
2042
0
    }
2043
2044
  /* Create a permanent copy of ENTRY and add it to the hash table.  */
2045
0
  stub = bfd_malloc (sizeof (search));
2046
0
  if (stub == NULL)
2047
0
    return false;
2048
0
  *stub = search;
2049
0
  *slot = stub;
2050
2051
  /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052
     of the section and if we would need no more than 2 nops.  */
2053
0
  value = mips_elf_get_la25_target (stub, &s);
2054
0
  if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055
0
    value &= ~1;
2056
0
  use_trampoline_p = (value != 0 || s->alignment_power > 4);
2057
2058
0
  h->la25_stub = stub;
2059
0
  return (use_trampoline_p
2060
0
    ? mips_elf_add_la25_trampoline (stub, info)
2061
0
    : mips_elf_add_la25_intro (stub, info));
2062
0
}
2063
2064
/* A mips_elf_link_hash_traverse callback that is called before sizing
2065
   sections.  DATA points to a mips_htab_traverse_info structure.  */
2066
2067
static bool
2068
mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2069
0
{
2070
0
  struct mips_htab_traverse_info *hti;
2071
2072
0
  hti = (struct mips_htab_traverse_info *) data;
2073
0
  if (!bfd_link_relocatable (hti->info))
2074
0
    mips_elf_check_mips16_stubs (hti->info, h);
2075
2076
0
  if (mips_elf_local_pic_function_p (h))
2077
0
    {
2078
      /* PR 12845: If H is in a section that has been garbage
2079
   collected it will have its output section set to *ABS*.  */
2080
0
      if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081
0
  return true;
2082
2083
      /* H is a function that might need $25 to be valid on entry.
2084
   If we're creating a non-PIC relocatable object, mark H as
2085
   being PIC.  If we're creating a non-relocatable object with
2086
   non-PIC branches and jumps to H, make sure that H has an la25
2087
   stub.  */
2088
0
      if (bfd_link_relocatable (hti->info))
2089
0
  {
2090
0
    if (!PIC_OBJECT_P (hti->output_bfd))
2091
0
      h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2092
0
  }
2093
0
      else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2094
0
  {
2095
0
    hti->error = true;
2096
0
    return false;
2097
0
  }
2098
0
    }
2099
0
  return true;
2100
0
}
2101

2102
/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103
   Most mips16 instructions are 16 bits, but these instructions
2104
   are 32 bits.
2105
2106
   The format of these instructions is:
2107
2108
   +--------------+--------------------------------+
2109
   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
2110
   +--------------+--------------------------------+
2111
   |        Immediate  15:0      |
2112
   +-----------------------------------------------+
2113
2114
   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
2115
   Note that the immediate value in the first word is swapped.
2116
2117
   When producing a relocatable object file, R_MIPS16_26 is
2118
   handled mostly like R_MIPS_26.  In particular, the addend is
2119
   stored as a straight 26-bit value in a 32-bit instruction.
2120
   (gas makes life simpler for itself by never adjusting a
2121
   R_MIPS16_26 reloc to be against a section, so the addend is
2122
   always zero).  However, the 32 bit instruction is stored as 2
2123
   16-bit values, rather than a single 32-bit value.  In a
2124
   big-endian file, the result is the same; in a little-endian
2125
   file, the two 16-bit halves of the 32 bit value are swapped.
2126
   This is so that a disassembler can recognize the jal
2127
   instruction.
2128
2129
   When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130
   instruction stored as two 16-bit values.  The addend A is the
2131
   contents of the targ26 field.  The calculation is the same as
2132
   R_MIPS_26.  When storing the calculated value, reorder the
2133
   immediate value as shown above, and don't forget to store the
2134
   value as two 16-bit values.
2135
2136
   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137
   defined as
2138
2139
   big-endian:
2140
   +--------+----------------------+
2141
   |      |        |
2142
   |      |  targ26-16     |
2143
   |31    26|25       0|
2144
   +--------+----------------------+
2145
2146
   little-endian:
2147
   +----------+------+-------------+
2148
   |        |      |       |
2149
   |  sub1    |      |     sub2    |
2150
   |0      9|10  15|16   31|
2151
   +----------+--------------------+
2152
   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153
   ((sub1 << 16) | sub2)).
2154
2155
   When producing a relocatable object file, the calculation is
2156
   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157
   When producing a fully linked file, the calculation is
2158
   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159
   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2160
2161
   The table below lists the other MIPS16 instruction relocations.
2162
   Each one is calculated in the same way as the non-MIPS16 relocation
2163
   given on the right, but using the extended MIPS16 layout of 16-bit
2164
   immediate fields:
2165
2166
  R_MIPS16_GPREL    R_MIPS_GPREL16
2167
  R_MIPS16_GOT16    R_MIPS_GOT16
2168
  R_MIPS16_CALL16   R_MIPS_CALL16
2169
  R_MIPS16_HI16   R_MIPS_HI16
2170
  R_MIPS16_LO16   R_MIPS_LO16
2171
2172
   A typical instruction will have a format like this:
2173
2174
   +--------------+--------------------------------+
2175
   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
2176
   +--------------+--------------------------------+
2177
   |    Major     |   rx   |   ry   |   Imm  4:0   |
2178
   +--------------+--------------------------------+
2179
2180
   EXTEND is the five bit value 11110.  Major is the instruction
2181
   opcode.
2182
2183
   All we need to do here is shuffle the bits appropriately.
2184
   As above, the two 16-bit halves must be swapped on a
2185
   little-endian system.
2186
2187
   Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188
   relocatable field is shifted by 1 rather than 2 and the same bit
2189
   shuffling is done as with the relocations above.  */
2190
2191
static inline bool
2192
mips16_reloc_p (int r_type)
2193
0
{
2194
0
  switch (r_type)
2195
0
    {
2196
0
    case R_MIPS16_26:
2197
0
    case R_MIPS16_GPREL:
2198
0
    case R_MIPS16_GOT16:
2199
0
    case R_MIPS16_CALL16:
2200
0
    case R_MIPS16_HI16:
2201
0
    case R_MIPS16_LO16:
2202
0
    case R_MIPS16_TLS_GD:
2203
0
    case R_MIPS16_TLS_LDM:
2204
0
    case R_MIPS16_TLS_DTPREL_HI16:
2205
0
    case R_MIPS16_TLS_DTPREL_LO16:
2206
0
    case R_MIPS16_TLS_GOTTPREL:
2207
0
    case R_MIPS16_TLS_TPREL_HI16:
2208
0
    case R_MIPS16_TLS_TPREL_LO16:
2209
0
    case R_MIPS16_PC16_S1:
2210
0
      return true;
2211
2212
0
    default:
2213
0
      return false;
2214
0
    }
2215
0
}
2216
2217
/* Check if a microMIPS reloc.  */
2218
2219
static inline bool
2220
micromips_reloc_p (unsigned int r_type)
2221
0
{
2222
0
  return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2223
0
}
2224
2225
/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226
   on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1
2227
   and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.  */
2228
2229
static inline bool
2230
micromips_reloc_shuffle_p (unsigned int r_type)
2231
0
{
2232
0
  return (micromips_reloc_p (r_type)
2233
0
    && r_type != R_MICROMIPS_PC7_S1
2234
0
    && r_type != R_MICROMIPS_PC10_S1);
2235
0
}
2236
2237
static inline bool
2238
got16_reloc_p (int r_type)
2239
0
{
2240
0
  return (r_type == R_MIPS_GOT16
2241
0
    || r_type == R_MIPS16_GOT16
2242
0
    || r_type == R_MICROMIPS_GOT16);
2243
0
}
2244
2245
static inline bool
2246
call16_reloc_p (int r_type)
2247
0
{
2248
0
  return (r_type == R_MIPS_CALL16
2249
0
    || r_type == R_MIPS16_CALL16
2250
0
    || r_type == R_MICROMIPS_CALL16);
2251
0
}
2252
2253
static inline bool
2254
got_disp_reloc_p (unsigned int r_type)
2255
0
{
2256
0
  return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2257
0
}
2258
2259
static inline bool
2260
got_page_reloc_p (unsigned int r_type)
2261
0
{
2262
0
  return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2263
0
}
2264
2265
static inline bool
2266
got_lo16_reloc_p (unsigned int r_type)
2267
0
{
2268
0
  return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2269
0
}
2270
2271
static inline bool
2272
call_hi16_reloc_p (unsigned int r_type)
2273
0
{
2274
0
  return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2275
0
}
2276
2277
static inline bool
2278
call_lo16_reloc_p (unsigned int r_type)
2279
0
{
2280
0
  return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2281
0
}
2282
2283
static inline bool
2284
hi16_reloc_p (int r_type)
2285
0
{
2286
0
  return (r_type == R_MIPS_HI16
2287
0
    || r_type == R_MIPS16_HI16
2288
0
    || r_type == R_MICROMIPS_HI16
2289
0
    || r_type == R_MIPS_PCHI16);
2290
0
}
2291
2292
static inline bool
2293
lo16_reloc_p (int r_type)
2294
0
{
2295
0
  return (r_type == R_MIPS_LO16
2296
0
    || r_type == R_MIPS16_LO16
2297
0
    || r_type == R_MICROMIPS_LO16
2298
0
    || r_type == R_MIPS_PCLO16);
2299
0
}
2300
2301
static inline bool
2302
mips16_call_reloc_p (int r_type)
2303
0
{
2304
0
  return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2305
0
}
2306
2307
static inline bool
2308
jal_reloc_p (int r_type)
2309
0
{
2310
0
  return (r_type == R_MIPS_26
2311
0
    || r_type == R_MIPS16_26
2312
0
    || r_type == R_MICROMIPS_26_S1);
2313
0
}
2314
2315
static inline bool
2316
b_reloc_p (int r_type)
2317
0
{
2318
0
  return (r_type == R_MIPS_PC26_S2
2319
0
    || r_type == R_MIPS_PC21_S2
2320
0
    || r_type == R_MIPS_PC16
2321
0
    || r_type == R_MIPS_GNU_REL16_S2
2322
0
    || r_type == R_MIPS16_PC16_S1
2323
0
    || r_type == R_MICROMIPS_PC16_S1
2324
0
    || r_type == R_MICROMIPS_PC10_S1
2325
0
    || r_type == R_MICROMIPS_PC7_S1);
2326
0
}
2327
2328
static inline bool
2329
aligned_pcrel_reloc_p (int r_type)
2330
0
{
2331
0
  return (r_type == R_MIPS_PC18_S3
2332
0
    || r_type == R_MIPS_PC19_S2);
2333
0
}
2334
2335
static inline bool
2336
branch_reloc_p (int r_type)
2337
0
{
2338
0
  return (r_type == R_MIPS_26
2339
0
    || r_type == R_MIPS_PC26_S2
2340
0
    || r_type == R_MIPS_PC21_S2
2341
0
    || r_type == R_MIPS_PC16
2342
0
    || r_type == R_MIPS_GNU_REL16_S2);
2343
0
}
2344
2345
static inline bool
2346
mips16_branch_reloc_p (int r_type)
2347
0
{
2348
0
  return (r_type == R_MIPS16_26
2349
0
    || r_type == R_MIPS16_PC16_S1);
2350
0
}
2351
2352
static inline bool
2353
micromips_branch_reloc_p (int r_type)
2354
0
{
2355
0
  return (r_type == R_MICROMIPS_26_S1
2356
0
    || r_type == R_MICROMIPS_PC16_S1
2357
0
    || r_type == R_MICROMIPS_PC10_S1
2358
0
    || r_type == R_MICROMIPS_PC7_S1);
2359
0
}
2360
2361
static inline bool
2362
tls_gd_reloc_p (unsigned int r_type)
2363
0
{
2364
0
  return (r_type == R_MIPS_TLS_GD
2365
0
    || r_type == R_MIPS16_TLS_GD
2366
0
    || r_type == R_MICROMIPS_TLS_GD);
2367
0
}
2368
2369
static inline bool
2370
tls_ldm_reloc_p (unsigned int r_type)
2371
0
{
2372
0
  return (r_type == R_MIPS_TLS_LDM
2373
0
    || r_type == R_MIPS16_TLS_LDM
2374
0
    || r_type == R_MICROMIPS_TLS_LDM);
2375
0
}
2376
2377
static inline bool
2378
tls_gottprel_reloc_p (unsigned int r_type)
2379
0
{
2380
0
  return (r_type == R_MIPS_TLS_GOTTPREL
2381
0
    || r_type == R_MIPS16_TLS_GOTTPREL
2382
0
    || r_type == R_MICROMIPS_TLS_GOTTPREL);
2383
0
}
2384
2385
static inline bool
2386
needs_shuffle (int r_type)
2387
0
{
2388
0
  return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2389
0
}
2390
2391
void
2392
_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2393
             bool jal_shuffle, bfd_byte *data)
2394
0
{
2395
0
  bfd_vma first, second, val;
2396
2397
0
  if (!needs_shuffle (r_type))
2398
0
    return;
2399
2400
  /* Pick up the first and second halfwords of the instruction.  */
2401
0
  first = bfd_get_16 (abfd, data);
2402
0
  second = bfd_get_16 (abfd, data + 2);
2403
0
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2404
0
    val = first << 16 | second;
2405
0
  else if (r_type != R_MIPS16_26)
2406
0
    val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2407
0
     | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2408
0
  else
2409
0
    val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2410
0
     | ((first & 0x1f) << 21) | second);
2411
0
  bfd_put_32 (abfd, val, data);
2412
0
}
2413
2414
void
2415
_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2416
           bool jal_shuffle, bfd_byte *data)
2417
0
{
2418
0
  bfd_vma first, second, val;
2419
2420
0
  if (!needs_shuffle (r_type))
2421
0
    return;
2422
2423
0
  val = bfd_get_32 (abfd, data);
2424
0
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2425
0
    {
2426
0
      second = val & 0xffff;
2427
0
      first = val >> 16;
2428
0
    }
2429
0
  else if (r_type != R_MIPS16_26)
2430
0
    {
2431
0
      second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2432
0
      first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2433
0
    }
2434
0
  else
2435
0
    {
2436
0
      second = val & 0xffff;
2437
0
      first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2438
0
         | ((val >> 21) & 0x1f);
2439
0
    }
2440
0
  bfd_put_16 (abfd, second, data + 2);
2441
0
  bfd_put_16 (abfd, first, data);
2442
0
}
2443
2444
/* Perform reloc offset checking.
2445
   We can only use bfd_reloc_offset_in_range, which takes into account
2446
   the size of the field being relocated, when section contents will
2447
   be accessed because mips object files may use relocations that seem
2448
   to access beyond section limits.
2449
   gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2450
   R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2451
   section.  The R_MIPS_SUB applies to the addend for the next reloc
2452
   rather than the section contents.
2453
2454
   CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2455
   CHECK_INPLACE to only check partial_inplace relocs, and
2456
   CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
2457
2458
bool
2459
_bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2460
         arelent *reloc_entry, enum reloc_check check)
2461
0
{
2462
0
  if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2463
0
    return true;
2464
0
  if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2465
0
    return true;
2466
0
  return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2467
0
            input_section, reloc_entry->address);
2468
0
}
2469
2470
bfd_reloc_status_type
2471
_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2472
             arelent *reloc_entry, asection *input_section,
2473
             bool relocatable, void *data, bfd_vma gp)
2474
0
{
2475
0
  bfd_vma relocation;
2476
0
  bfd_signed_vma val;
2477
0
  bfd_reloc_status_type status;
2478
2479
0
  if (bfd_is_com_section (symbol->section))
2480
0
    relocation = 0;
2481
0
  else
2482
0
    relocation = symbol->value;
2483
2484
0
  if (symbol->section->output_section != NULL)
2485
0
    {
2486
0
      relocation += symbol->section->output_section->vma;
2487
0
      relocation += symbol->section->output_offset;
2488
0
    }
2489
2490
  /* Set val to the offset into the section or symbol.  */
2491
0
  val = reloc_entry->addend;
2492
2493
0
  _bfd_mips_elf_sign_extend (val, 16);
2494
2495
  /* Adjust val for the final section location and GP value.  If we
2496
     are producing relocatable output, we don't want to do this for
2497
     an external symbol.  */
2498
0
  if (! relocatable
2499
0
      || (symbol->flags & BSF_SECTION_SYM) != 0)
2500
0
    val += relocation - gp;
2501
2502
0
  if (reloc_entry->howto->partial_inplace)
2503
0
    {
2504
0
      if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2505
0
              reloc_entry->address))
2506
0
  return bfd_reloc_outofrange;
2507
2508
0
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2509
0
               (bfd_byte *) data
2510
0
               + reloc_entry->address);
2511
0
      if (status != bfd_reloc_ok)
2512
0
  return status;
2513
0
    }
2514
0
  else
2515
0
    reloc_entry->addend = val;
2516
2517
0
  if (relocatable)
2518
0
    reloc_entry->address += input_section->output_offset;
2519
2520
0
  return bfd_reloc_ok;
2521
0
}
2522
2523
/* A howto special_function for REL *HI16 relocations.  We can only
2524
   calculate the correct value once we've seen the partnering
2525
   *LO16 relocation, so just save the information for later.
2526
2527
   The ABI requires that the *LO16 immediately follow the *HI16.
2528
   However, as a GNU extension, we permit an arbitrary number of
2529
   *HI16s to be associated with a single *LO16.  This significantly
2530
   simplies the relocation handling in gcc.  */
2531
2532
bfd_reloc_status_type
2533
_bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2534
        asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2535
        asection *input_section, bfd *output_bfd,
2536
        char **error_message ATTRIBUTE_UNUSED)
2537
0
{
2538
0
  struct mips_hi16 *n;
2539
0
  struct mips_elf_obj_tdata *tdata;
2540
2541
0
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2542
0
    return bfd_reloc_outofrange;
2543
2544
0
  n = bfd_malloc (sizeof *n);
2545
0
  if (n == NULL)
2546
0
    return bfd_reloc_outofrange;
2547
2548
0
  tdata = mips_elf_tdata (abfd);
2549
0
  n->next = tdata->mips_hi16_list;
2550
0
  n->data = data;
2551
0
  n->input_section = input_section;
2552
0
  n->rel = *reloc_entry;
2553
0
  tdata->mips_hi16_list = n;
2554
2555
0
  if (output_bfd != NULL)
2556
0
    reloc_entry->address += input_section->output_offset;
2557
2558
0
  return bfd_reloc_ok;
2559
0
}
2560
2561
/* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2562
   like any other 16-bit relocation when applied to global symbols, but is
2563
   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2564
2565
bfd_reloc_status_type
2566
_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2567
         void *data, asection *input_section,
2568
         bfd *output_bfd, char **error_message)
2569
0
{
2570
0
  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2571
0
      || bfd_is_und_section (bfd_asymbol_section (symbol))
2572
0
      || bfd_is_com_section (bfd_asymbol_section (symbol)))
2573
    /* The relocation is against a global symbol.  */
2574
0
    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2575
0
          input_section, output_bfd,
2576
0
          error_message);
2577
2578
0
  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2579
0
           input_section, output_bfd, error_message);
2580
0
}
2581
2582
/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2583
   is a straightforward 16 bit inplace relocation, but we must deal with
2584
   any partnering high-part relocations as well.  */
2585
2586
bfd_reloc_status_type
2587
_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588
        void *data, asection *input_section,
2589
        bfd *output_bfd, char **error_message)
2590
0
{
2591
0
  bfd_vma vallo;
2592
0
  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2593
0
  struct mips_elf_obj_tdata *tdata;
2594
2595
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2596
0
          reloc_entry->address))
2597
0
    return bfd_reloc_outofrange;
2598
2599
0
  _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2600
0
         location);
2601
  /* The high 16 bits of the addend are stored in the high insn, the
2602
     low 16 bits in the low insn, but there is a catch:  You can't
2603
     just concatenate the high and low parts.  The high part of the
2604
     addend is adjusted for the fact that the low part is sign
2605
     extended.  For example, an addend of 0x38000 would have 0x0004 in
2606
     the high part and 0x8000 (=0xff..f8000) in the low part.
2607
     To extract the actual addend, calculate (a)
2608
     ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2609
     We will be applying (symbol + addend) & 0xffff to the low insn,
2610
     and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2611
     high insn (the +0x8000 adjusting for when the applied low part is
2612
     negative).  Substituting (a) into (b) and recognising that
2613
     (hi & 0xffff) is already in the high insn gives a high part
2614
     addend adjustment of (lo & 0xffff) ^ 0x8000.  */
2615
0
  vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2616
0
  _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2617
0
             location);
2618
2619
0
  tdata = mips_elf_tdata (abfd);
2620
0
  while (tdata->mips_hi16_list != NULL)
2621
0
    {
2622
0
      bfd_reloc_status_type ret;
2623
0
      struct mips_hi16 *hi;
2624
2625
0
      hi = tdata->mips_hi16_list;
2626
2627
      /* R_MIPS*_GOT16 relocations are something of a special case.  We
2628
   want to install the addend in the same way as for a R_MIPS*_HI16
2629
   relocation (with a rightshift of 16).  However, since GOT16
2630
   relocations can also be used with global symbols, their howto
2631
   has a rightshift of 0.  */
2632
0
      if (hi->rel.howto->type == R_MIPS_GOT16)
2633
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2634
0
      else if (hi->rel.howto->type == R_MIPS16_GOT16)
2635
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2636
0
      else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2637
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2638
2639
0
      hi->rel.addend += vallo;
2640
2641
0
      ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2642
0
           hi->input_section, output_bfd,
2643
0
           error_message);
2644
0
      if (ret != bfd_reloc_ok)
2645
0
  return ret;
2646
2647
0
      tdata->mips_hi16_list = hi->next;
2648
0
      free (hi);
2649
0
    }
2650
2651
0
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2652
0
              input_section, output_bfd,
2653
0
              error_message);
2654
0
}
2655
2656
/* A generic howto special_function.  This calculates and installs the
2657
   relocation itself, thus avoiding the oft-discussed problems in
2658
   bfd_perform_relocation and bfd_install_relocation.  */
2659
2660
bfd_reloc_status_type
2661
_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2662
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2663
           asection *input_section, bfd *output_bfd,
2664
           char **error_message ATTRIBUTE_UNUSED)
2665
0
{
2666
0
  bfd_signed_vma val;
2667
0
  bfd_reloc_status_type status;
2668
0
  bool relocatable;
2669
2670
0
  relocatable = (output_bfd != NULL);
2671
2672
0
  if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2673
0
          (relocatable
2674
0
           ? check_inplace : check_std)))
2675
0
    return bfd_reloc_outofrange;
2676
2677
  /* Build up the field adjustment in VAL.  */
2678
0
  val = 0;
2679
0
  if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2680
0
      && symbol->section->output_section != NULL)
2681
0
    {
2682
      /* Either we're calculating the final field value or we have a
2683
   relocation against a section symbol.  Add in the section's
2684
   offset or address.  */
2685
0
      val += symbol->section->output_section->vma;
2686
0
      val += symbol->section->output_offset;
2687
0
    }
2688
2689
0
  if (!relocatable)
2690
0
    {
2691
      /* We're calculating the final field value.  Add in the symbol's value
2692
   and, if pc-relative, subtract the address of the field itself.  */
2693
0
      val += symbol->value;
2694
0
      if (reloc_entry->howto->pc_relative)
2695
0
  {
2696
0
    val -= input_section->output_section->vma;
2697
0
    val -= input_section->output_offset;
2698
0
    val -= reloc_entry->address;
2699
0
  }
2700
0
    }
2701
2702
  /* VAL is now the final adjustment.  If we're keeping this relocation
2703
     in the output file, and if the relocation uses a separate addend,
2704
     we just need to add VAL to that addend.  Otherwise we need to add
2705
     VAL to the relocation field itself.  */
2706
0
  if (relocatable && !reloc_entry->howto->partial_inplace)
2707
0
    reloc_entry->addend += val;
2708
0
  else
2709
0
    {
2710
0
      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2711
2712
      /* Add in the separate addend, if any.  */
2713
0
      val += reloc_entry->addend;
2714
2715
      /* Add VAL to the relocation field.  */
2716
0
      _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2717
0
             location);
2718
0
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2719
0
               location);
2720
0
      _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2721
0
           location);
2722
2723
0
      if (status != bfd_reloc_ok)
2724
0
  return status;
2725
0
    }
2726
2727
0
  if (relocatable)
2728
0
    reloc_entry->address += input_section->output_offset;
2729
2730
0
  return bfd_reloc_ok;
2731
0
}
2732

2733
/* Swap an entry in a .gptab section.  Note that these routines rely
2734
   on the equivalence of the two elements of the union.  */
2735
2736
static void
2737
bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2738
            Elf32_gptab *in)
2739
0
{
2740
0
  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2741
0
  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2742
0
}
2743
2744
static void
2745
bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2746
             Elf32_External_gptab *ex)
2747
0
{
2748
0
  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2749
0
  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2750
0
}
2751
2752
static void
2753
bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2754
        Elf32_External_compact_rel *ex)
2755
0
{
2756
0
  H_PUT_32 (abfd, in->id1, ex->id1);
2757
0
  H_PUT_32 (abfd, in->num, ex->num);
2758
0
  H_PUT_32 (abfd, in->id2, ex->id2);
2759
0
  H_PUT_32 (abfd, in->offset, ex->offset);
2760
0
  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2761
0
  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2762
0
}
2763
2764
static void
2765
bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2766
         Elf32_External_crinfo *ex)
2767
0
{
2768
0
  unsigned long l;
2769
2770
0
  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2771
0
       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2772
0
       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2773
0
       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2774
0
  H_PUT_32 (abfd, l, ex->info);
2775
0
  H_PUT_32 (abfd, in->konst, ex->konst);
2776
0
  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2777
0
}
2778

2779
/* A .reginfo section holds a single Elf32_RegInfo structure.  These
2780
   routines swap this structure in and out.  They are used outside of
2781
   BFD, so they are globally visible.  */
2782
2783
void
2784
bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2785
        Elf32_RegInfo *in)
2786
0
{
2787
0
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2788
0
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2789
0
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2790
0
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2791
0
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2792
0
  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2793
0
}
2794
2795
void
2796
bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2797
         Elf32_External_RegInfo *ex)
2798
0
{
2799
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2800
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2801
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2802
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2803
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2804
0
  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2805
0
}
2806
2807
/* In the 64 bit ABI, the .MIPS.options section holds register
2808
   information in an Elf64_Reginfo structure.  These routines swap
2809
   them in and out.  They are globally visible because they are used
2810
   outside of BFD.  These routines are here so that gas can call them
2811
   without worrying about whether the 64 bit ABI has been included.  */
2812
2813
void
2814
bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2815
        Elf64_Internal_RegInfo *in)
2816
0
{
2817
0
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2818
0
  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2819
0
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2820
0
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2821
0
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2822
0
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2823
0
  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2824
0
}
2825
2826
void
2827
bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2828
         Elf64_External_RegInfo *ex)
2829
0
{
2830
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2831
0
  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2832
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2833
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2834
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2835
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2836
0
  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2837
0
}
2838
2839
/* Swap in an options header.  */
2840
2841
void
2842
bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2843
            Elf_Internal_Options *in)
2844
0
{
2845
0
  in->kind = H_GET_8 (abfd, ex->kind);
2846
0
  in->size = H_GET_8 (abfd, ex->size);
2847
0
  in->section = H_GET_16 (abfd, ex->section);
2848
0
  in->info = H_GET_32 (abfd, ex->info);
2849
0
}
2850
2851
/* Swap out an options header.  */
2852
2853
void
2854
bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2855
             Elf_External_Options *ex)
2856
0
{
2857
0
  H_PUT_8 (abfd, in->kind, ex->kind);
2858
0
  H_PUT_8 (abfd, in->size, ex->size);
2859
0
  H_PUT_16 (abfd, in->section, ex->section);
2860
0
  H_PUT_32 (abfd, in->info, ex->info);
2861
0
}
2862
2863
/* Swap in an abiflags structure.  */
2864
2865
void
2866
bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2867
          const Elf_External_ABIFlags_v0 *ex,
2868
          Elf_Internal_ABIFlags_v0 *in)
2869
0
{
2870
0
  in->version = H_GET_16 (abfd, ex->version);
2871
0
  in->isa_level = H_GET_8 (abfd, ex->isa_level);
2872
0
  in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2873
0
  in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2874
0
  in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2875
0
  in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2876
0
  in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2877
0
  in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2878
0
  in->ases = H_GET_32 (abfd, ex->ases);
2879
0
  in->flags1 = H_GET_32 (abfd, ex->flags1);
2880
0
  in->flags2 = H_GET_32 (abfd, ex->flags2);
2881
0
}
2882
2883
/* Swap out an abiflags structure.  */
2884
2885
void
2886
bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2887
           const Elf_Internal_ABIFlags_v0 *in,
2888
           Elf_External_ABIFlags_v0 *ex)
2889
0
{
2890
0
  H_PUT_16 (abfd, in->version, ex->version);
2891
0
  H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2892
0
  H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2893
0
  H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2894
0
  H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2895
0
  H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2896
0
  H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2897
0
  H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2898
0
  H_PUT_32 (abfd, in->ases, ex->ases);
2899
0
  H_PUT_32 (abfd, in->flags1, ex->flags1);
2900
0
  H_PUT_32 (abfd, in->flags2, ex->flags2);
2901
0
}
2902

2903
/* This function is called via qsort() to sort the dynamic relocation
2904
   entries by increasing r_symndx value.  */
2905
2906
static int
2907
sort_dynamic_relocs (const void *arg1, const void *arg2)
2908
0
{
2909
0
  Elf_Internal_Rela int_reloc1;
2910
0
  Elf_Internal_Rela int_reloc2;
2911
0
  int diff;
2912
2913
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2914
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2915
2916
0
  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2917
0
  if (diff != 0)
2918
0
    return diff;
2919
2920
0
  if (int_reloc1.r_offset < int_reloc2.r_offset)
2921
0
    return -1;
2922
0
  if (int_reloc1.r_offset > int_reloc2.r_offset)
2923
0
    return 1;
2924
0
  return 0;
2925
0
}
2926
2927
/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2928
2929
static int
2930
sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2931
      const void *arg2 ATTRIBUTE_UNUSED)
2932
0
{
2933
0
#ifdef BFD64
2934
0
  Elf_Internal_Rela int_reloc1[3];
2935
0
  Elf_Internal_Rela int_reloc2[3];
2936
2937
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2938
0
    (reldyn_sorting_bfd, arg1, int_reloc1);
2939
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940
0
    (reldyn_sorting_bfd, arg2, int_reloc2);
2941
2942
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2943
0
    return -1;
2944
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2945
0
    return 1;
2946
2947
0
  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2948
0
    return -1;
2949
0
  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2950
0
    return 1;
2951
0
  return 0;
2952
#else
2953
  abort ();
2954
#endif
2955
0
}
2956
2957
2958
/* This routine is used to write out ECOFF debugging external symbol
2959
   information.  It is called via mips_elf_link_hash_traverse.  The
2960
   ECOFF external symbol information must match the ELF external
2961
   symbol information.  Unfortunately, at this point we don't know
2962
   whether a symbol is required by reloc information, so the two
2963
   tables may wind up being different.  We must sort out the external
2964
   symbol information before we can set the final size of the .mdebug
2965
   section, and we must set the size of the .mdebug section before we
2966
   can relocate any sections, and we can't know which symbols are
2967
   required by relocation until we relocate the sections.
2968
   Fortunately, it is relatively unlikely that any symbol will be
2969
   stripped but required by a reloc.  In particular, it can not happen
2970
   when generating a final executable.  */
2971
2972
static bool
2973
mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2974
0
{
2975
0
  struct extsym_info *einfo = data;
2976
0
  bool strip;
2977
0
  asection *sec, *output_section;
2978
2979
0
  if (h->root.indx == -2)
2980
0
    strip = false;
2981
0
  else if ((h->root.def_dynamic
2982
0
      || h->root.ref_dynamic
2983
0
      || h->root.type == bfd_link_hash_new)
2984
0
     && !h->root.def_regular
2985
0
     && !h->root.ref_regular)
2986
0
    strip = true;
2987
0
  else if (einfo->info->strip == strip_all
2988
0
     || (einfo->info->strip == strip_some
2989
0
         && bfd_hash_lookup (einfo->info->keep_hash,
2990
0
           h->root.root.root.string,
2991
0
           false, false) == NULL))
2992
0
    strip = true;
2993
0
  else
2994
0
    strip = false;
2995
2996
0
  if (strip)
2997
0
    return true;
2998
2999
0
  if (h->esym.ifd == -2)
3000
0
    {
3001
0
      h->esym.jmptbl = 0;
3002
0
      h->esym.cobol_main = 0;
3003
0
      h->esym.weakext = 0;
3004
0
      h->esym.reserved = 0;
3005
0
      h->esym.ifd = ifdNil;
3006
0
      h->esym.asym.value = 0;
3007
0
      h->esym.asym.st = stGlobal;
3008
3009
0
      if (h->root.root.type == bfd_link_hash_undefined
3010
0
    || h->root.root.type == bfd_link_hash_undefweak)
3011
0
  {
3012
0
    const char *name;
3013
3014
    /* Use undefined class.  Also, set class and type for some
3015
       special symbols.  */
3016
0
    name = h->root.root.root.string;
3017
0
    if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3018
0
        || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3019
0
      {
3020
0
        h->esym.asym.sc = scData;
3021
0
        h->esym.asym.st = stLabel;
3022
0
        h->esym.asym.value = 0;
3023
0
      }
3024
0
    else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3025
0
      {
3026
0
        h->esym.asym.sc = scAbs;
3027
0
        h->esym.asym.st = stLabel;
3028
0
        h->esym.asym.value =
3029
0
    mips_elf_hash_table (einfo->info)->procedure_count;
3030
0
      }
3031
0
    else
3032
0
      h->esym.asym.sc = scUndefined;
3033
0
  }
3034
0
      else if (h->root.root.type != bfd_link_hash_defined
3035
0
    && h->root.root.type != bfd_link_hash_defweak)
3036
0
  h->esym.asym.sc = scAbs;
3037
0
      else
3038
0
  {
3039
0
    const char *name;
3040
3041
0
    sec = h->root.root.u.def.section;
3042
0
    output_section = sec->output_section;
3043
3044
    /* When making a shared library and symbol h is the one from
3045
       the another shared library, OUTPUT_SECTION may be null.  */
3046
0
    if (output_section == NULL)
3047
0
      h->esym.asym.sc = scUndefined;
3048
0
    else
3049
0
      {
3050
0
        name = bfd_section_name (output_section);
3051
3052
0
        if (strcmp (name, ".text") == 0)
3053
0
    h->esym.asym.sc = scText;
3054
0
        else if (strcmp (name, ".data") == 0)
3055
0
    h->esym.asym.sc = scData;
3056
0
        else if (strcmp (name, ".sdata") == 0)
3057
0
    h->esym.asym.sc = scSData;
3058
0
        else if (strcmp (name, ".rodata") == 0
3059
0
           || strcmp (name, ".rdata") == 0)
3060
0
    h->esym.asym.sc = scRData;
3061
0
        else if (strcmp (name, ".bss") == 0)
3062
0
    h->esym.asym.sc = scBss;
3063
0
        else if (strcmp (name, ".sbss") == 0)
3064
0
    h->esym.asym.sc = scSBss;
3065
0
        else if (strcmp (name, ".init") == 0)
3066
0
    h->esym.asym.sc = scInit;
3067
0
        else if (strcmp (name, ".fini") == 0)
3068
0
    h->esym.asym.sc = scFini;
3069
0
        else
3070
0
    h->esym.asym.sc = scAbs;
3071
0
      }
3072
0
  }
3073
3074
0
      h->esym.asym.reserved = 0;
3075
0
      h->esym.asym.index = indexNil;
3076
0
    }
3077
3078
0
  if (h->root.root.type == bfd_link_hash_common)
3079
0
    h->esym.asym.value = h->root.root.u.c.size;
3080
0
  else if (h->root.root.type == bfd_link_hash_defined
3081
0
     || h->root.root.type == bfd_link_hash_defweak)
3082
0
    {
3083
0
      if (h->esym.asym.sc == scCommon)
3084
0
  h->esym.asym.sc = scBss;
3085
0
      else if (h->esym.asym.sc == scSCommon)
3086
0
  h->esym.asym.sc = scSBss;
3087
3088
0
      sec = h->root.root.u.def.section;
3089
0
      output_section = sec->output_section;
3090
0
      if (output_section != NULL)
3091
0
  h->esym.asym.value = (h->root.root.u.def.value
3092
0
            + sec->output_offset
3093
0
            + output_section->vma);
3094
0
      else
3095
0
  h->esym.asym.value = 0;
3096
0
    }
3097
0
  else
3098
0
    {
3099
0
      struct mips_elf_link_hash_entry *hd = h;
3100
3101
0
      while (hd->root.root.type == bfd_link_hash_indirect)
3102
0
  hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3103
3104
0
      if (hd->needs_lazy_stub)
3105
0
  {
3106
0
    BFD_ASSERT (hd->root.plt.plist != NULL);
3107
0
    BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3108
    /* Set type and value for a symbol with a function stub.  */
3109
0
    h->esym.asym.st = stProc;
3110
0
    sec = hd->root.root.u.def.section;
3111
0
    if (sec == NULL)
3112
0
      h->esym.asym.value = 0;
3113
0
    else
3114
0
      {
3115
0
        output_section = sec->output_section;
3116
0
        if (output_section != NULL)
3117
0
    h->esym.asym.value = (hd->root.plt.plist->stub_offset
3118
0
              + sec->output_offset
3119
0
              + output_section->vma);
3120
0
        else
3121
0
    h->esym.asym.value = 0;
3122
0
      }
3123
0
  }
3124
0
    }
3125
3126
0
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3127
0
              h->root.root.root.string,
3128
0
              &h->esym))
3129
0
    {
3130
0
      einfo->failed = true;
3131
0
      return false;
3132
0
    }
3133
3134
0
  return true;
3135
0
}
3136
3137
/* A comparison routine used to sort .gptab entries.  */
3138
3139
static int
3140
gptab_compare (const void *p1, const void *p2)
3141
0
{
3142
0
  const Elf32_gptab *a1 = p1;
3143
0
  const Elf32_gptab *a2 = p2;
3144
3145
0
  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3146
0
}
3147

3148
/* Functions to manage the got entry hash table.  */
3149
3150
/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3151
   hash number.  */
3152
3153
static inline hashval_t
3154
mips_elf_hash_bfd_vma (bfd_vma addr)
3155
0
{
3156
0
#ifdef BFD64
3157
0
  return addr + (addr >> 32);
3158
#else
3159
  return addr;
3160
#endif
3161
0
}
3162
3163
static hashval_t
3164
mips_elf_got_entry_hash (const void *entry_)
3165
0
{
3166
0
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3167
3168
0
  return (entry->symndx
3169
0
    + ((entry->tls_type == GOT_TLS_LDM) << 18)
3170
0
    + (entry->tls_type == GOT_TLS_LDM ? 0
3171
0
       : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3172
0
       : entry->symndx >= 0 ? (entry->abfd->id
3173
0
             + mips_elf_hash_bfd_vma (entry->d.addend))
3174
0
       : entry->d.h->root.root.root.hash));
3175
0
}
3176
3177
static int
3178
mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3179
0
{
3180
0
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3181
0
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3182
3183
0
  return (e1->symndx == e2->symndx
3184
0
    && e1->tls_type == e2->tls_type
3185
0
    && (e1->tls_type == GOT_TLS_LDM ? true
3186
0
        : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3187
0
        : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3188
0
           && e1->d.addend == e2->d.addend)
3189
0
        : e2->abfd && e1->d.h == e2->d.h));
3190
0
}
3191
3192
static hashval_t
3193
mips_got_page_ref_hash (const void *ref_)
3194
0
{
3195
0
  const struct mips_got_page_ref *ref;
3196
3197
0
  ref = (const struct mips_got_page_ref *) ref_;
3198
0
  return ((ref->symndx >= 0
3199
0
     ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3200
0
     : ref->u.h->root.root.root.hash)
3201
0
    + mips_elf_hash_bfd_vma (ref->addend));
3202
0
}
3203
3204
static int
3205
mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3206
0
{
3207
0
  const struct mips_got_page_ref *ref1, *ref2;
3208
3209
0
  ref1 = (const struct mips_got_page_ref *) ref1_;
3210
0
  ref2 = (const struct mips_got_page_ref *) ref2_;
3211
0
  return (ref1->symndx == ref2->symndx
3212
0
    && (ref1->symndx < 0
3213
0
        ? ref1->u.h == ref2->u.h
3214
0
        : ref1->u.abfd == ref2->u.abfd)
3215
0
    && ref1->addend == ref2->addend);
3216
0
}
3217
3218
static hashval_t
3219
mips_got_page_entry_hash (const void *entry_)
3220
0
{
3221
0
  const struct mips_got_page_entry *entry;
3222
3223
0
  entry = (const struct mips_got_page_entry *) entry_;
3224
0
  return entry->sec->id;
3225
0
}
3226
3227
static int
3228
mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3229
0
{
3230
0
  const struct mips_got_page_entry *entry1, *entry2;
3231
3232
0
  entry1 = (const struct mips_got_page_entry *) entry1_;
3233
0
  entry2 = (const struct mips_got_page_entry *) entry2_;
3234
0
  return entry1->sec == entry2->sec;
3235
0
}
3236

3237
/* Create and return a new mips_got_info structure.  */
3238
3239
static struct mips_got_info *
3240
mips_elf_create_got_info (bfd *abfd)
3241
0
{
3242
0
  struct mips_got_info *g;
3243
3244
0
  g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3245
0
  if (g == NULL)
3246
0
    return NULL;
3247
3248
0
  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3249
0
            mips_elf_got_entry_eq, NULL);
3250
0
  if (g->got_entries == NULL)
3251
0
    return NULL;
3252
3253
0
  g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3254
0
              mips_got_page_ref_eq, NULL);
3255
0
  if (g->got_page_refs == NULL)
3256
0
    return NULL;
3257
3258
0
  return g;
3259
0
}
3260
3261
/* Return the GOT info for input bfd ABFD, trying to create a new one if
3262
   CREATE_P and if ABFD doesn't already have a GOT.  */
3263
3264
static struct mips_got_info *
3265
mips_elf_bfd_got (bfd *abfd, bool create_p)
3266
0
{
3267
0
  struct mips_elf_obj_tdata *tdata;
3268
3269
0
  if (!is_mips_elf (abfd))
3270
0
    return NULL;
3271
3272
0
  tdata = mips_elf_tdata (abfd);
3273
0
  if (!tdata->got && create_p)
3274
0
    tdata->got = mips_elf_create_got_info (abfd);
3275
0
  return tdata->got;
3276
0
}
3277
3278
/* Record that ABFD should use output GOT G.  */
3279
3280
static void
3281
mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3282
0
{
3283
0
  struct mips_elf_obj_tdata *tdata;
3284
3285
0
  BFD_ASSERT (is_mips_elf (abfd));
3286
0
  tdata = mips_elf_tdata (abfd);
3287
0
  if (tdata->got)
3288
0
    {
3289
      /* The GOT structure itself and the hash table entries are
3290
   allocated to a bfd, but the hash tables aren't.  */
3291
0
      htab_delete (tdata->got->got_entries);
3292
0
      htab_delete (tdata->got->got_page_refs);
3293
0
      if (tdata->got->got_page_entries)
3294
0
  htab_delete (tdata->got->got_page_entries);
3295
0
    }
3296
0
  tdata->got = g;
3297
0
}
3298
3299
/* Return the dynamic relocation section.  If it doesn't exist, try to
3300
   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
3301
   if creation fails.  */
3302
3303
static asection *
3304
mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3305
0
{
3306
0
  const char *dname;
3307
0
  asection *sreloc;
3308
0
  bfd *dynobj;
3309
3310
0
  dname = MIPS_ELF_REL_DYN_NAME (info);
3311
0
  dynobj = elf_hash_table (info)->dynobj;
3312
0
  sreloc = bfd_get_linker_section (dynobj, dname);
3313
0
  if (sreloc == NULL && create_p)
3314
0
    {
3315
0
      sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3316
0
               (SEC_ALLOC
3317
0
                | SEC_LOAD
3318
0
                | SEC_HAS_CONTENTS
3319
0
                | SEC_IN_MEMORY
3320
0
                | SEC_LINKER_CREATED
3321
0
                | SEC_READONLY));
3322
0
      if (sreloc == NULL
3323
0
    || !bfd_set_section_alignment (sreloc,
3324
0
           MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3325
0
  return NULL;
3326
0
    }
3327
0
  return sreloc;
3328
0
}
3329
3330
/* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
3331
3332
static int
3333
mips_elf_reloc_tls_type (unsigned int r_type)
3334
0
{
3335
0
  if (tls_gd_reloc_p (r_type))
3336
0
    return GOT_TLS_GD;
3337
3338
0
  if (tls_ldm_reloc_p (r_type))
3339
0
    return GOT_TLS_LDM;
3340
3341
0
  if (tls_gottprel_reloc_p (r_type))
3342
0
    return GOT_TLS_IE;
3343
3344
0
  return GOT_TLS_NONE;
3345
0
}
3346
3347
/* Return the number of GOT slots needed for GOT TLS type TYPE.  */
3348
3349
static int
3350
mips_tls_got_entries (unsigned int type)
3351
0
{
3352
0
  switch (type)
3353
0
    {
3354
0
    case GOT_TLS_GD:
3355
0
    case GOT_TLS_LDM:
3356
0
      return 2;
3357
3358
0
    case GOT_TLS_IE:
3359
0
      return 1;
3360
3361
0
    case GOT_TLS_NONE:
3362
0
      return 0;
3363
0
    }
3364
0
  abort ();
3365
0
}
3366
3367
/* Count the number of relocations needed for a TLS GOT entry, with
3368
   access types from TLS_TYPE, and symbol H (or a local symbol if H
3369
   is NULL).  */
3370
3371
static int
3372
mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3373
         struct elf_link_hash_entry *h)
3374
0
{
3375
0
  int indx = 0;
3376
0
  bool need_relocs = false;
3377
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3378
3379
0
  if (h != NULL
3380
0
      && h->dynindx != -1
3381
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3382
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3383
0
    indx = h->dynindx;
3384
3385
0
  if ((bfd_link_dll (info) || indx != 0)
3386
0
      && (h == NULL
3387
0
    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3388
0
    || h->root.type != bfd_link_hash_undefweak))
3389
0
    need_relocs = true;
3390
3391
0
  if (!need_relocs)
3392
0
    return 0;
3393
3394
0
  switch (tls_type)
3395
0
    {
3396
0
    case GOT_TLS_GD:
3397
0
      return indx != 0 ? 2 : 1;
3398
3399
0
    case GOT_TLS_IE:
3400
0
      return 1;
3401
3402
0
    case GOT_TLS_LDM:
3403
0
      return bfd_link_dll (info) ? 1 : 0;
3404
3405
0
    default:
3406
0
      return 0;
3407
0
    }
3408
0
}
3409
3410
/* Add the number of GOT entries and TLS relocations required by ENTRY
3411
   to G.  */
3412
3413
static void
3414
mips_elf_count_got_entry (struct bfd_link_info *info,
3415
        struct mips_got_info *g,
3416
        struct mips_got_entry *entry)
3417
0
{
3418
0
  if (entry->tls_type)
3419
0
    {
3420
0
      g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3421
0
      g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3422
0
          entry->symndx < 0
3423
0
          ? &entry->d.h->root : NULL);
3424
0
    }
3425
0
  else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3426
0
    g->local_gotno += 1;
3427
0
  else
3428
0
    g->global_gotno += 1;
3429
0
}
3430
3431
/* Output a simple dynamic relocation into SRELOC.  */
3432
3433
static void
3434
mips_elf_output_dynamic_relocation (bfd *output_bfd,
3435
            asection *sreloc,
3436
            unsigned long reloc_index,
3437
            unsigned long indx,
3438
            int r_type,
3439
            bfd_vma offset)
3440
0
{
3441
0
  Elf_Internal_Rela rel[3];
3442
3443
0
  memset (rel, 0, sizeof (rel));
3444
3445
0
  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3446
0
  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3447
3448
0
  if (ABI_64_P (output_bfd))
3449
0
    {
3450
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3451
0
  (output_bfd, &rel[0],
3452
0
   (sreloc->contents
3453
0
    + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3454
0
    }
3455
0
  else
3456
0
    bfd_elf32_swap_reloc_out
3457
0
      (output_bfd, &rel[0],
3458
0
       (sreloc->contents
3459
0
  + reloc_index * sizeof (Elf32_External_Rel)));
3460
0
}
3461
3462
/* Initialize a set of TLS GOT entries for one symbol.  */
3463
3464
static void
3465
mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3466
             struct mips_got_entry *entry,
3467
             struct mips_elf_link_hash_entry *h,
3468
             bfd_vma value)
3469
0
{
3470
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3471
0
  struct mips_elf_link_hash_table *htab;
3472
0
  int indx;
3473
0
  asection *sreloc, *sgot;
3474
0
  bfd_vma got_offset, got_offset2;
3475
0
  bool need_relocs = false;
3476
3477
0
  htab = mips_elf_hash_table (info);
3478
0
  if (htab == NULL)
3479
0
    return;
3480
3481
0
  sgot = htab->root.sgot;
3482
3483
0
  indx = 0;
3484
0
  if (h != NULL
3485
0
      && h->root.dynindx != -1
3486
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3487
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3488
0
    indx = h->root.dynindx;
3489
3490
0
  if (entry->tls_initialized)
3491
0
    return;
3492
3493
0
  if ((bfd_link_dll (info) || indx != 0)
3494
0
      && (h == NULL
3495
0
    || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3496
0
    || h->root.type != bfd_link_hash_undefweak))
3497
0
    need_relocs = true;
3498
3499
  /* MINUS_ONE means the symbol is not defined in this object.  It may not
3500
     be defined at all; assume that the value doesn't matter in that
3501
     case.  Otherwise complain if we would use the value.  */
3502
0
  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3503
0
        || h->root.root.type == bfd_link_hash_undefweak);
3504
3505
  /* Emit necessary relocations.  */
3506
0
  sreloc = mips_elf_rel_dyn_section (info, false);
3507
0
  got_offset = entry->gotidx;
3508
3509
0
  switch (entry->tls_type)
3510
0
    {
3511
0
    case GOT_TLS_GD:
3512
      /* General Dynamic.  */
3513
0
      got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3514
3515
0
      if (need_relocs)
3516
0
  {
3517
0
    mips_elf_output_dynamic_relocation
3518
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3519
0
       ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3520
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3521
3522
0
    if (indx)
3523
0
      mips_elf_output_dynamic_relocation
3524
0
        (abfd, sreloc, sreloc->reloc_count++, indx,
3525
0
         ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3526
0
         sgot->output_offset + sgot->output_section->vma + got_offset2);
3527
0
    else
3528
0
      MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3529
0
             sgot->contents + got_offset2);
3530
0
  }
3531
0
      else
3532
0
  {
3533
0
    MIPS_ELF_PUT_WORD (abfd, 1,
3534
0
           sgot->contents + got_offset);
3535
0
    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3536
0
           sgot->contents + got_offset2);
3537
0
  }
3538
0
      break;
3539
3540
0
    case GOT_TLS_IE:
3541
      /* Initial Exec model.  */
3542
0
      if (need_relocs)
3543
0
  {
3544
0
    if (indx == 0)
3545
0
      MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3546
0
             sgot->contents + got_offset);
3547
0
    else
3548
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3549
0
             sgot->contents + got_offset);
3550
3551
0
    mips_elf_output_dynamic_relocation
3552
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3553
0
       ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3554
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3555
0
  }
3556
0
      else
3557
0
  MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3558
0
         sgot->contents + got_offset);
3559
0
      break;
3560
3561
0
    case GOT_TLS_LDM:
3562
      /* The initial offset is zero, and the LD offsets will include the
3563
   bias by DTP_OFFSET.  */
3564
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3565
0
       sgot->contents + got_offset
3566
0
       + MIPS_ELF_GOT_SIZE (abfd));
3567
3568
0
      if (!bfd_link_dll (info))
3569
0
  MIPS_ELF_PUT_WORD (abfd, 1,
3570
0
         sgot->contents + got_offset);
3571
0
      else
3572
0
  mips_elf_output_dynamic_relocation
3573
0
    (abfd, sreloc, sreloc->reloc_count++, indx,
3574
0
     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3575
0
     sgot->output_offset + sgot->output_section->vma + got_offset);
3576
0
      break;
3577
3578
0
    default:
3579
0
      abort ();
3580
0
    }
3581
3582
0
  entry->tls_initialized = true;
3583
0
}
3584
3585
/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3586
   for global symbol H.  .got.plt comes before the GOT, so the offset
3587
   will be negative.  */
3588
3589
static bfd_vma
3590
mips_elf_gotplt_index (struct bfd_link_info *info,
3591
           struct elf_link_hash_entry *h)
3592
0
{
3593
0
  bfd_vma got_address, got_value;
3594
0
  struct mips_elf_link_hash_table *htab;
3595
3596
0
  htab = mips_elf_hash_table (info);
3597
0
  BFD_ASSERT (htab != NULL);
3598
3599
0
  BFD_ASSERT (h->plt.plist != NULL);
3600
0
  BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3601
3602
  /* Calculate the address of the associated .got.plt entry.  */
3603
0
  got_address = (htab->root.sgotplt->output_section->vma
3604
0
     + htab->root.sgotplt->output_offset
3605
0
     + (h->plt.plist->gotplt_index
3606
0
        * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3607
3608
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3609
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3610
0
         + htab->root.hgot->root.u.def.section->output_offset
3611
0
         + htab->root.hgot->root.u.def.value);
3612
3613
0
  return got_address - got_value;
3614
0
}
3615
3616
/* Return the GOT offset for address VALUE.   If there is not yet a GOT
3617
   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3618
   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3619
   offset can be found.  */
3620
3621
static bfd_vma
3622
mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3623
        bfd_vma value, unsigned long r_symndx,
3624
        struct mips_elf_link_hash_entry *h, int r_type)
3625
0
{
3626
0
  struct mips_elf_link_hash_table *htab;
3627
0
  struct mips_got_entry *entry;
3628
3629
0
  htab = mips_elf_hash_table (info);
3630
0
  BFD_ASSERT (htab != NULL);
3631
3632
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3633
0
             r_symndx, h, r_type);
3634
0
  if (!entry)
3635
0
    return MINUS_ONE;
3636
3637
0
  if (entry->tls_type)
3638
0
    mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3639
0
  return entry->gotidx;
3640
0
}
3641
3642
/* Return the GOT index of global symbol H in the primary GOT.  */
3643
3644
static bfd_vma
3645
mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3646
           struct elf_link_hash_entry *h)
3647
0
{
3648
0
  struct mips_elf_link_hash_table *htab;
3649
0
  long global_got_dynindx;
3650
0
  struct mips_got_info *g;
3651
0
  bfd_vma got_index;
3652
3653
0
  htab = mips_elf_hash_table (info);
3654
0
  BFD_ASSERT (htab != NULL);
3655
3656
0
  global_got_dynindx = 0;
3657
0
  if (htab->global_gotsym != NULL)
3658
0
    global_got_dynindx = htab->global_gotsym->dynindx;
3659
3660
  /* Once we determine the global GOT entry with the lowest dynamic
3661
     symbol table index, we must put all dynamic symbols with greater
3662
     indices into the primary GOT.  That makes it easy to calculate the
3663
     GOT offset.  */
3664
0
  BFD_ASSERT (h->dynindx >= global_got_dynindx);
3665
0
  g = mips_elf_bfd_got (obfd, false);
3666
0
  got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3667
0
         * MIPS_ELF_GOT_SIZE (obfd));
3668
0
  BFD_ASSERT (got_index < htab->root.sgot->size);
3669
3670
0
  return got_index;
3671
0
}
3672
3673
/* Return the GOT index for the global symbol indicated by H, which is
3674
   referenced by a relocation of type R_TYPE in IBFD.  */
3675
3676
static bfd_vma
3677
mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3678
         struct elf_link_hash_entry *h, int r_type)
3679
0
{
3680
0
  struct mips_elf_link_hash_table *htab;
3681
0
  struct mips_got_info *g;
3682
0
  struct mips_got_entry lookup, *entry;
3683
0
  bfd_vma gotidx;
3684
3685
0
  htab = mips_elf_hash_table (info);
3686
0
  BFD_ASSERT (htab != NULL);
3687
3688
0
  g = mips_elf_bfd_got (ibfd, false);
3689
0
  BFD_ASSERT (g);
3690
3691
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3692
0
  if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3693
0
    return mips_elf_primary_global_got_index (obfd, info, h);
3694
3695
0
  lookup.abfd = ibfd;
3696
0
  lookup.symndx = -1;
3697
0
  lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3698
0
  entry = htab_find (g->got_entries, &lookup);
3699
0
  BFD_ASSERT (entry);
3700
3701
0
  gotidx = entry->gotidx;
3702
0
  BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3703
3704
0
  if (lookup.tls_type)
3705
0
    {
3706
0
      bfd_vma value = MINUS_ONE;
3707
3708
0
      if ((h->root.type == bfd_link_hash_defined
3709
0
     || h->root.type == bfd_link_hash_defweak)
3710
0
    && h->root.u.def.section->output_section)
3711
0
  value = (h->root.u.def.value
3712
0
     + h->root.u.def.section->output_offset
3713
0
     + h->root.u.def.section->output_section->vma);
3714
3715
0
      mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3716
0
    }
3717
0
  return gotidx;
3718
0
}
3719
3720
/* Find a GOT page entry that points to within 32KB of VALUE.  These
3721
   entries are supposed to be placed at small offsets in the GOT, i.e.,
3722
   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3723
   entry could be created.  If OFFSETP is nonnull, use it to return the
3724
   offset of the GOT entry from VALUE.  */
3725
3726
static bfd_vma
3727
mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3728
       bfd_vma value, bfd_vma *offsetp)
3729
0
{
3730
0
  bfd_vma page, got_index;
3731
0
  struct mips_got_entry *entry;
3732
3733
0
  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3734
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3735
0
             NULL, R_MIPS_GOT_PAGE);
3736
3737
0
  if (!entry)
3738
0
    return MINUS_ONE;
3739
3740
0
  got_index = entry->gotidx;
3741
3742
0
  if (offsetp)
3743
0
    *offsetp = value - entry->d.address;
3744
3745
0
  return got_index;
3746
0
}
3747
3748
/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3749
   EXTERNAL is true if the relocation was originally against a global
3750
   symbol that binds locally.  */
3751
3752
static bfd_vma
3753
mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3754
          bfd_vma value, bool external)
3755
0
{
3756
0
  struct mips_got_entry *entry;
3757
3758
  /* GOT16 relocations against local symbols are followed by a LO16
3759
     relocation; those against global symbols are not.  Thus if the
3760
     symbol was originally local, the GOT16 relocation should load the
3761
     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3762
0
  if (! external)
3763
0
    value = mips_elf_high (value) << 16;
3764
3765
  /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3766
     R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3767
     same in all cases.  */
3768
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3769
0
             NULL, R_MIPS_GOT16);
3770
0
  if (entry)
3771
0
    return entry->gotidx;
3772
0
  else
3773
0
    return MINUS_ONE;
3774
0
}
3775
3776
/* Returns the offset for the entry at the INDEXth position
3777
   in the GOT.  */
3778
3779
static bfd_vma
3780
mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3781
        bfd *input_bfd, bfd_vma got_index)
3782
0
{
3783
0
  struct mips_elf_link_hash_table *htab;
3784
0
  asection *sgot;
3785
0
  bfd_vma gp;
3786
3787
0
  htab = mips_elf_hash_table (info);
3788
0
  BFD_ASSERT (htab != NULL);
3789
3790
0
  sgot = htab->root.sgot;
3791
0
  gp = _bfd_get_gp_value (output_bfd)
3792
0
    + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3793
3794
0
  return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3795
0
}
3796
3797
/* Create and return a local GOT entry for VALUE, which was calculated
3798
   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3799
   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3800
   instead.  */
3801
3802
static struct mips_got_entry *
3803
mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3804
         bfd *ibfd, bfd_vma value,
3805
         unsigned long r_symndx,
3806
         struct mips_elf_link_hash_entry *h,
3807
         int r_type)
3808
0
{
3809
0
  struct mips_got_entry lookup, *entry;
3810
0
  void **loc;
3811
0
  struct mips_got_info *g;
3812
0
  struct mips_elf_link_hash_table *htab;
3813
0
  bfd_vma gotidx;
3814
3815
0
  htab = mips_elf_hash_table (info);
3816
0
  BFD_ASSERT (htab != NULL);
3817
3818
0
  g = mips_elf_bfd_got (ibfd, false);
3819
0
  if (g == NULL)
3820
0
    {
3821
0
      g = mips_elf_bfd_got (abfd, false);
3822
0
      BFD_ASSERT (g != NULL);
3823
0
    }
3824
3825
  /* This function shouldn't be called for symbols that live in the global
3826
     area of the GOT.  */
3827
0
  BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3828
3829
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3830
0
  if (lookup.tls_type)
3831
0
    {
3832
0
      lookup.abfd = ibfd;
3833
0
      if (tls_ldm_reloc_p (r_type))
3834
0
  {
3835
0
    lookup.symndx = 0;
3836
0
    lookup.d.addend = 0;
3837
0
  }
3838
0
      else if (h == NULL)
3839
0
  {
3840
0
    lookup.symndx = r_symndx;
3841
0
    lookup.d.addend = 0;
3842
0
  }
3843
0
      else
3844
0
  {
3845
0
    lookup.symndx = -1;
3846
0
    lookup.d.h = h;
3847
0
  }
3848
3849
0
      entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3850
0
      BFD_ASSERT (entry);
3851
3852
0
      gotidx = entry->gotidx;
3853
0
      BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3854
3855
0
      return entry;
3856
0
    }
3857
3858
0
  lookup.abfd = NULL;
3859
0
  lookup.symndx = -1;
3860
0
  lookup.d.address = value;
3861
0
  loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3862
0
  if (!loc)
3863
0
    return NULL;
3864
3865
0
  entry = (struct mips_got_entry *) *loc;
3866
0
  if (entry)
3867
0
    return entry;
3868
3869
0
  if (g->assigned_low_gotno > g->assigned_high_gotno)
3870
0
    {
3871
      /* We didn't allocate enough space in the GOT.  */
3872
0
      _bfd_error_handler
3873
0
  (_("not enough GOT space for local GOT entries"));
3874
0
      bfd_set_error (bfd_error_bad_value);
3875
0
      return NULL;
3876
0
    }
3877
3878
0
  entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3879
0
  if (!entry)
3880
0
    return NULL;
3881
3882
0
  if (got16_reloc_p (r_type)
3883
0
      || call16_reloc_p (r_type)
3884
0
      || got_page_reloc_p (r_type)
3885
0
      || got_disp_reloc_p (r_type))
3886
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3887
0
  else
3888
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3889
3890
0
  *entry = lookup;
3891
0
  *loc = entry;
3892
3893
0
  MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3894
3895
  /* These GOT entries need a dynamic relocation on VxWorks.  */
3896
0
  if (htab->root.target_os == is_vxworks)
3897
0
    {
3898
0
      Elf_Internal_Rela outrel;
3899
0
      asection *s;
3900
0
      bfd_byte *rloc;
3901
0
      bfd_vma got_address;
3902
3903
0
      s = mips_elf_rel_dyn_section (info, false);
3904
0
      got_address = (htab->root.sgot->output_section->vma
3905
0
         + htab->root.sgot->output_offset
3906
0
         + entry->gotidx);
3907
3908
0
      rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3909
0
      outrel.r_offset = got_address;
3910
0
      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3911
0
      outrel.r_addend = value;
3912
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3913
0
    }
3914
3915
0
  return entry;
3916
0
}
3917
3918
/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3919
   The number might be exact or a worst-case estimate, depending on how
3920
   much information is available to elf_backend_omit_section_dynsym at
3921
   the current linking stage.  */
3922
3923
static bfd_size_type
3924
count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3925
0
{
3926
0
  bfd_size_type count;
3927
3928
0
  count = 0;
3929
0
  if (bfd_link_pic (info)
3930
0
      || elf_hash_table (info)->is_relocatable_executable)
3931
0
    {
3932
0
      asection *p;
3933
0
      const struct elf_backend_data *bed;
3934
3935
0
      bed = get_elf_backend_data (output_bfd);
3936
0
      for (p = output_bfd->sections; p ; p = p->next)
3937
0
  if ((p->flags & SEC_EXCLUDE) == 0
3938
0
      && (p->flags & SEC_ALLOC) != 0
3939
0
      && elf_hash_table (info)->dynamic_relocs
3940
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3941
0
    ++count;
3942
0
    }
3943
0
  return count;
3944
0
}
3945
3946
/* Sort the dynamic symbol table so that symbols that need GOT entries
3947
   appear towards the end.  */
3948
3949
static bool
3950
mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3951
0
{
3952
0
  struct mips_elf_link_hash_table *htab;
3953
0
  struct mips_elf_hash_sort_data hsd;
3954
0
  struct mips_got_info *g;
3955
3956
0
  htab = mips_elf_hash_table (info);
3957
0
  BFD_ASSERT (htab != NULL);
3958
3959
0
  if (htab->root.dynsymcount == 0)
3960
0
    return true;
3961
3962
0
  g = htab->got_info;
3963
0
  if (g == NULL)
3964
0
    return true;
3965
3966
0
  hsd.low = NULL;
3967
0
  hsd.max_unref_got_dynindx
3968
0
    = hsd.min_got_dynindx
3969
0
    = (htab->root.dynsymcount - g->reloc_only_gotno);
3970
  /* Add 1 to local symbol indices to account for the mandatory NULL entry
3971
     at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
3972
0
  hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3973
0
  hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3974
0
  hsd.output_bfd = abfd;
3975
0
  if (htab->root.dynobj != NULL
3976
0
      && htab->root.dynamic_sections_created
3977
0
      && info->emit_gnu_hash)
3978
0
    {
3979
0
      asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3980
0
      BFD_ASSERT (s != NULL);
3981
0
      hsd.mipsxhash = s->contents;
3982
0
      BFD_ASSERT (hsd.mipsxhash != NULL);
3983
0
    }
3984
0
  else
3985
0
    hsd.mipsxhash = NULL;
3986
0
  mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3987
3988
  /* There should have been enough room in the symbol table to
3989
     accommodate both the GOT and non-GOT symbols.  */
3990
0
  BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3991
0
  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3992
0
  BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3993
0
  BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3994
3995
  /* Now we know which dynamic symbol has the lowest dynamic symbol
3996
     table index in the GOT.  */
3997
0
  htab->global_gotsym = hsd.low;
3998
3999
0
  return true;
4000
0
}
4001
4002
/* If H needs a GOT entry, assign it the highest available dynamic
4003
   index.  Otherwise, assign it the lowest available dynamic
4004
   index.  */
4005
4006
static bool
4007
mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4008
0
{
4009
0
  struct mips_elf_hash_sort_data *hsd = data;
4010
4011
  /* Symbols without dynamic symbol table entries aren't interesting
4012
     at all.  */
4013
0
  if (h->root.dynindx == -1)
4014
0
    return true;
4015
4016
0
  switch (h->global_got_area)
4017
0
    {
4018
0
    case GGA_NONE:
4019
0
      if (h->root.forced_local)
4020
0
  h->root.dynindx = hsd->max_local_dynindx++;
4021
0
      else
4022
0
  h->root.dynindx = hsd->max_non_got_dynindx++;
4023
0
      break;
4024
4025
0
    case GGA_NORMAL:
4026
0
      h->root.dynindx = --hsd->min_got_dynindx;
4027
0
      hsd->low = (struct elf_link_hash_entry *) h;
4028
0
      break;
4029
4030
0
    case GGA_RELOC_ONLY:
4031
0
      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4032
0
  hsd->low = (struct elf_link_hash_entry *) h;
4033
0
      h->root.dynindx = hsd->max_unref_got_dynindx++;
4034
0
      break;
4035
0
    }
4036
4037
  /* Populate the .MIPS.xhash translation table entry with
4038
     the symbol dynindx.  */
4039
0
  if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4040
0
    bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4041
0
    hsd->mipsxhash + h->mipsxhash_loc);
4042
4043
0
  return true;
4044
0
}
4045
4046
/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4047
   (which is owned by the caller and shouldn't be added to the
4048
   hash table directly).  */
4049
4050
static bool
4051
mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4052
         struct mips_got_entry *lookup)
4053
0
{
4054
0
  struct mips_elf_link_hash_table *htab;
4055
0
  struct mips_got_entry *entry;
4056
0
  struct mips_got_info *g;
4057
0
  void **loc, **bfd_loc;
4058
4059
  /* Make sure there's a slot for this entry in the master GOT.  */
4060
0
  htab = mips_elf_hash_table (info);
4061
0
  g = htab->got_info;
4062
0
  loc = htab_find_slot (g->got_entries, lookup, INSERT);
4063
0
  if (!loc)
4064
0
    return false;
4065
4066
  /* Populate the entry if it isn't already.  */
4067
0
  entry = (struct mips_got_entry *) *loc;
4068
0
  if (!entry)
4069
0
    {
4070
0
      entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4071
0
      if (!entry)
4072
0
  return false;
4073
4074
0
      lookup->tls_initialized = false;
4075
0
      lookup->gotidx = -1;
4076
0
      *entry = *lookup;
4077
0
      *loc = entry;
4078
0
    }
4079
4080
  /* Reuse the same GOT entry for the BFD's GOT.  */
4081
0
  g = mips_elf_bfd_got (abfd, true);
4082
0
  if (!g)
4083
0
    return false;
4084
4085
0
  bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4086
0
  if (!bfd_loc)
4087
0
    return false;
4088
4089
0
  if (!*bfd_loc)
4090
0
    *bfd_loc = entry;
4091
0
  return true;
4092
0
}
4093
4094
/* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
4095
   entry for it.  FOR_CALL is true if the caller is only interested in
4096
   using the GOT entry for calls.  */
4097
4098
static bool
4099
mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4100
           bfd *abfd, struct bfd_link_info *info,
4101
           bool for_call, int r_type)
4102
0
{
4103
0
  struct mips_elf_link_hash_table *htab;
4104
0
  struct mips_elf_link_hash_entry *hmips;
4105
0
  struct mips_got_entry entry;
4106
0
  unsigned char tls_type;
4107
4108
0
  htab = mips_elf_hash_table (info);
4109
0
  BFD_ASSERT (htab != NULL);
4110
4111
0
  hmips = (struct mips_elf_link_hash_entry *) h;
4112
0
  if (!for_call)
4113
0
    hmips->got_only_for_calls = false;
4114
4115
  /* A global symbol in the GOT must also be in the dynamic symbol
4116
     table.  */
4117
0
  if (h->dynindx == -1)
4118
0
    {
4119
0
      switch (ELF_ST_VISIBILITY (h->other))
4120
0
  {
4121
0
  case STV_INTERNAL:
4122
0
  case STV_HIDDEN:
4123
0
    _bfd_mips_elf_hide_symbol (info, h, true);
4124
0
    break;
4125
0
  }
4126
0
      if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127
0
  return false;
4128
0
    }
4129
4130
0
  tls_type = mips_elf_reloc_tls_type (r_type);
4131
0
  if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4132
0
    hmips->global_got_area = GGA_NORMAL;
4133
4134
0
  entry.abfd = abfd;
4135
0
  entry.symndx = -1;
4136
0
  entry.d.h = (struct mips_elf_link_hash_entry *) h;
4137
0
  entry.tls_type = tls_type;
4138
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4139
0
}
4140
4141
/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4142
   where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
4143
4144
static bool
4145
mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4146
          struct bfd_link_info *info, int r_type)
4147
0
{
4148
0
  struct mips_elf_link_hash_table *htab;
4149
0
  struct mips_got_info *g;
4150
0
  struct mips_got_entry entry;
4151
4152
0
  htab = mips_elf_hash_table (info);
4153
0
  BFD_ASSERT (htab != NULL);
4154
4155
0
  g = htab->got_info;
4156
0
  BFD_ASSERT (g != NULL);
4157
4158
0
  entry.abfd = abfd;
4159
0
  entry.symndx = symndx;
4160
0
  entry.d.addend = addend;
4161
0
  entry.tls_type = mips_elf_reloc_tls_type (r_type);
4162
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4163
0
}
4164
4165
/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4166
   H is the symbol's hash table entry, or null if SYMNDX is local
4167
   to ABFD.  */
4168
4169
static bool
4170
mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4171
            long symndx, struct elf_link_hash_entry *h,
4172
            bfd_signed_vma addend)
4173
0
{
4174
0
  struct mips_elf_link_hash_table *htab;
4175
0
  struct mips_got_info *g1, *g2;
4176
0
  struct mips_got_page_ref lookup, *entry;
4177
0
  void **loc, **bfd_loc;
4178
4179
0
  htab = mips_elf_hash_table (info);
4180
0
  BFD_ASSERT (htab != NULL);
4181
4182
0
  g1 = htab->got_info;
4183
0
  BFD_ASSERT (g1 != NULL);
4184
4185
0
  if (h)
4186
0
    {
4187
0
      lookup.symndx = -1;
4188
0
      lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4189
0
    }
4190
0
  else
4191
0
    {
4192
0
      lookup.symndx = symndx;
4193
0
      lookup.u.abfd = abfd;
4194
0
    }
4195
0
  lookup.addend = addend;
4196
0
  loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4197
0
  if (loc == NULL)
4198
0
    return false;
4199
4200
0
  entry = (struct mips_got_page_ref *) *loc;
4201
0
  if (!entry)
4202
0
    {
4203
0
      entry = bfd_alloc (abfd, sizeof (*entry));
4204
0
      if (!entry)
4205
0
  return false;
4206
4207
0
      *entry = lookup;
4208
0
      *loc = entry;
4209
0
    }
4210
4211
  /* Add the same entry to the BFD's GOT.  */
4212
0
  g2 = mips_elf_bfd_got (abfd, true);
4213
0
  if (!g2)
4214
0
    return false;
4215
4216
0
  bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4217
0
  if (!bfd_loc)
4218
0
    return false;
4219
4220
0
  if (!*bfd_loc)
4221
0
    *bfd_loc = entry;
4222
4223
0
  return true;
4224
0
}
4225
4226
/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4227
4228
static void
4229
mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4230
               unsigned int n)
4231
0
{
4232
0
  asection *s;
4233
0
  struct mips_elf_link_hash_table *htab;
4234
4235
0
  htab = mips_elf_hash_table (info);
4236
0
  BFD_ASSERT (htab != NULL);
4237
4238
0
  s = mips_elf_rel_dyn_section (info, false);
4239
0
  BFD_ASSERT (s != NULL);
4240
4241
0
  if (htab->root.target_os == is_vxworks)
4242
0
    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4243
0
  else
4244
0
    {
4245
0
      if (s->size == 0)
4246
0
  {
4247
    /* Make room for a null element.  */
4248
0
    s->size += MIPS_ELF_REL_SIZE (abfd);
4249
0
    ++s->reloc_count;
4250
0
  }
4251
0
      s->size += n * MIPS_ELF_REL_SIZE (abfd);
4252
0
    }
4253
0
}
4254

4255
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4256
   mips_elf_traverse_got_arg structure.  Count the number of GOT
4257
   entries and TLS relocs.  Set DATA->value to true if we need
4258
   to resolve indirect or warning symbols and then recreate the GOT.  */
4259
4260
static int
4261
mips_elf_check_recreate_got (void **entryp, void *data)
4262
0
{
4263
0
  struct mips_got_entry *entry;
4264
0
  struct mips_elf_traverse_got_arg *arg;
4265
4266
0
  entry = (struct mips_got_entry *) *entryp;
4267
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4268
0
  if (entry->abfd != NULL && entry->symndx == -1)
4269
0
    {
4270
0
      struct mips_elf_link_hash_entry *h;
4271
4272
0
      h = entry->d.h;
4273
0
      if (h->root.root.type == bfd_link_hash_indirect
4274
0
    || h->root.root.type == bfd_link_hash_warning)
4275
0
  {
4276
0
    arg->value = true;
4277
0
    return 0;
4278
0
  }
4279
0
    }
4280
0
  mips_elf_count_got_entry (arg->info, arg->g, entry);
4281
0
  return 1;
4282
0
}
4283
4284
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4285
   mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
4286
   converting entries for indirect and warning symbols into entries
4287
   for the target symbol.  Set DATA->g to null on error.  */
4288
4289
static int
4290
mips_elf_recreate_got (void **entryp, void *data)
4291
0
{
4292
0
  struct mips_got_entry new_entry, *entry;
4293
0
  struct mips_elf_traverse_got_arg *arg;
4294
0
  void **slot;
4295
4296
0
  entry = (struct mips_got_entry *) *entryp;
4297
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4298
0
  if (entry->abfd != NULL
4299
0
      && entry->symndx == -1
4300
0
      && (entry->d.h->root.root.type == bfd_link_hash_indirect
4301
0
    || entry->d.h->root.root.type == bfd_link_hash_warning))
4302
0
    {
4303
0
      struct mips_elf_link_hash_entry *h;
4304
4305
0
      new_entry = *entry;
4306
0
      entry = &new_entry;
4307
0
      h = entry->d.h;
4308
0
      do
4309
0
  {
4310
0
    BFD_ASSERT (h->global_got_area == GGA_NONE);
4311
0
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4312
0
  }
4313
0
      while (h->root.root.type == bfd_link_hash_indirect
4314
0
       || h->root.root.type == bfd_link_hash_warning);
4315
0
      entry->d.h = h;
4316
0
    }
4317
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4318
0
  if (slot == NULL)
4319
0
    {
4320
0
      arg->g = NULL;
4321
0
      return 0;
4322
0
    }
4323
0
  if (*slot == NULL)
4324
0
    {
4325
0
      if (entry == &new_entry)
4326
0
  {
4327
0
    entry = bfd_alloc (entry->abfd, sizeof (*entry));
4328
0
    if (!entry)
4329
0
      {
4330
0
        arg->g = NULL;
4331
0
        return 0;
4332
0
      }
4333
0
    *entry = new_entry;
4334
0
  }
4335
0
      *slot = entry;
4336
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4337
0
    }
4338
0
  return 1;
4339
0
}
4340
4341
/* Return the maximum number of GOT page entries required for RANGE.  */
4342
4343
static bfd_vma
4344
mips_elf_pages_for_range (const struct mips_got_page_range *range)
4345
0
{
4346
0
  return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4347
0
}
4348
4349
/* Record that G requires a page entry that can reach SEC + ADDEND.  */
4350
4351
static bool
4352
mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4353
        asection *sec, bfd_signed_vma addend)
4354
0
{
4355
0
  struct mips_got_info *g = arg->g;
4356
0
  struct mips_got_page_entry lookup, *entry;
4357
0
  struct mips_got_page_range **range_ptr, *range;
4358
0
  bfd_vma old_pages, new_pages;
4359
0
  void **loc;
4360
4361
  /* Find the mips_got_page_entry hash table entry for this section.  */
4362
0
  lookup.sec = sec;
4363
0
  loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4364
0
  if (loc == NULL)
4365
0
    return false;
4366
4367
  /* Create a mips_got_page_entry if this is the first time we've
4368
     seen the section.  */
4369
0
  entry = (struct mips_got_page_entry *) *loc;
4370
0
  if (!entry)
4371
0
    {
4372
0
      entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4373
0
      if (!entry)
4374
0
  return false;
4375
4376
0
      entry->sec = sec;
4377
0
      *loc = entry;
4378
0
    }
4379
4380
  /* Skip over ranges whose maximum extent cannot share a page entry
4381
     with ADDEND.  */
4382
0
  range_ptr = &entry->ranges;
4383
0
  while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4384
0
    range_ptr = &(*range_ptr)->next;
4385
4386
  /* If we scanned to the end of the list, or found a range whose
4387
     minimum extent cannot share a page entry with ADDEND, create
4388
     a new singleton range.  */
4389
0
  range = *range_ptr;
4390
0
  if (!range || addend < range->min_addend - 0xffff)
4391
0
    {
4392
0
      range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4393
0
      if (!range)
4394
0
  return false;
4395
4396
0
      range->next = *range_ptr;
4397
0
      range->min_addend = addend;
4398
0
      range->max_addend = addend;
4399
4400
0
      *range_ptr = range;
4401
0
      entry->num_pages++;
4402
0
      g->page_gotno++;
4403
0
      return true;
4404
0
    }
4405
4406
  /* Remember how many pages the old range contributed.  */
4407
0
  old_pages = mips_elf_pages_for_range (range);
4408
4409
  /* Update the ranges.  */
4410
0
  if (addend < range->min_addend)
4411
0
    range->min_addend = addend;
4412
0
  else if (addend > range->max_addend)
4413
0
    {
4414
0
      if (range->next && addend >= range->next->min_addend - 0xffff)
4415
0
  {
4416
0
    old_pages += mips_elf_pages_for_range (range->next);
4417
0
    range->max_addend = range->next->max_addend;
4418
0
    range->next = range->next->next;
4419
0
  }
4420
0
      else
4421
0
  range->max_addend = addend;
4422
0
    }
4423
4424
  /* Record any change in the total estimate.  */
4425
0
  new_pages = mips_elf_pages_for_range (range);
4426
0
  if (old_pages != new_pages)
4427
0
    {
4428
0
      entry->num_pages += new_pages - old_pages;
4429
0
      g->page_gotno += new_pages - old_pages;
4430
0
    }
4431
4432
0
  return true;
4433
0
}
4434
4435
/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4436
   and for which DATA points to a mips_elf_traverse_got_arg.  Work out
4437
   whether the page reference described by *REFP needs a GOT page entry,
4438
   and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
4439
4440
static int
4441
mips_elf_resolve_got_page_ref (void **refp, void *data)
4442
0
{
4443
0
  struct mips_got_page_ref *ref;
4444
0
  struct mips_elf_traverse_got_arg *arg;
4445
0
  struct mips_elf_link_hash_table *htab;
4446
0
  asection *sec;
4447
0
  bfd_vma addend;
4448
4449
0
  ref = (struct mips_got_page_ref *) *refp;
4450
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4451
0
  htab = mips_elf_hash_table (arg->info);
4452
4453
0
  if (ref->symndx < 0)
4454
0
    {
4455
0
      struct mips_elf_link_hash_entry *h;
4456
4457
      /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
4458
0
      h = ref->u.h;
4459
0
      if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4460
0
  return 1;
4461
4462
      /* Ignore undefined symbols; we'll issue an error later if
4463
   appropriate.  */
4464
0
      if (!((h->root.root.type == bfd_link_hash_defined
4465
0
       || h->root.root.type == bfd_link_hash_defweak)
4466
0
      && h->root.root.u.def.section))
4467
0
  return 1;
4468
4469
0
      sec = h->root.root.u.def.section;
4470
0
      addend = h->root.root.u.def.value + ref->addend;
4471
0
    }
4472
0
  else
4473
0
    {
4474
0
      Elf_Internal_Sym *isym;
4475
4476
      /* Read in the symbol.  */
4477
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4478
0
            ref->symndx);
4479
0
      if (isym == NULL)
4480
0
  {
4481
0
    arg->g = NULL;
4482
0
    return 0;
4483
0
  }
4484
4485
      /* Get the associated input section.  */
4486
0
      sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4487
0
      if (sec == NULL)
4488
0
  {
4489
0
    arg->g = NULL;
4490
0
    return 0;
4491
0
  }
4492
4493
      /* If this is a mergable section, work out the section and offset
4494
   of the merged data.  For section symbols, the addend specifies
4495
   of the offset _of_ the first byte in the data, otherwise it
4496
   specifies the offset _from_ the first byte.  */
4497
0
      if (sec->flags & SEC_MERGE)
4498
0
  {
4499
0
    void *secinfo;
4500
4501
0
    secinfo = elf_section_data (sec)->sec_info;
4502
0
    if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4503
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4504
0
             isym->st_value + ref->addend);
4505
0
    else
4506
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4507
0
             isym->st_value) + ref->addend;
4508
0
  }
4509
0
      else
4510
0
  addend = isym->st_value + ref->addend;
4511
0
    }
4512
0
  if (!mips_elf_record_got_page_entry (arg, sec, addend))
4513
0
    {
4514
0
      arg->g = NULL;
4515
0
      return 0;
4516
0
    }
4517
0
  return 1;
4518
0
}
4519
4520
/* If any entries in G->got_entries are for indirect or warning symbols,
4521
   replace them with entries for the target symbol.  Convert g->got_page_refs
4522
   into got_page_entry structures and estimate the number of page entries
4523
   that they require.  */
4524
4525
static bool
4526
mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4527
            struct mips_got_info *g)
4528
0
{
4529
0
  struct mips_elf_traverse_got_arg tga;
4530
0
  struct mips_got_info oldg;
4531
4532
0
  oldg = *g;
4533
4534
0
  tga.info = info;
4535
0
  tga.g = g;
4536
0
  tga.value = false;
4537
0
  htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4538
0
  if (tga.value)
4539
0
    {
4540
0
      *g = oldg;
4541
0
      g->got_entries = htab_create (htab_size (oldg.got_entries),
4542
0
            mips_elf_got_entry_hash,
4543
0
            mips_elf_got_entry_eq, NULL);
4544
0
      if (!g->got_entries)
4545
0
  return false;
4546
4547
0
      htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4548
0
      if (!tga.g)
4549
0
  return false;
4550
4551
0
      htab_delete (oldg.got_entries);
4552
0
    }
4553
4554
0
  g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4555
0
           mips_got_page_entry_eq, NULL);
4556
0
  if (g->got_page_entries == NULL)
4557
0
    return false;
4558
4559
0
  tga.info = info;
4560
0
  tga.g = g;
4561
0
  htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4562
4563
0
  return true;
4564
0
}
4565
4566
/* Return true if a GOT entry for H should live in the local rather than
4567
   global GOT area.  */
4568
4569
static bool
4570
mips_use_local_got_p (struct bfd_link_info *info,
4571
          struct mips_elf_link_hash_entry *h)
4572
0
{
4573
  /* Symbols that aren't in the dynamic symbol table must live in the
4574
     local GOT.  This includes symbols that are completely undefined
4575
     and which therefore don't bind locally.  We'll report undefined
4576
     symbols later if appropriate.  */
4577
0
  if (h->root.dynindx == -1)
4578
0
    return true;
4579
4580
  /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4581
     to the local GOT, as they would be implicitly relocated by the
4582
     base address by the dynamic loader.  */
4583
0
  if (bfd_is_abs_symbol (&h->root.root))
4584
0
    return false;
4585
4586
  /* Symbols that bind locally can (and in the case of forced-local
4587
     symbols, must) live in the local GOT.  */
4588
0
  if (h->got_only_for_calls
4589
0
      ? SYMBOL_CALLS_LOCAL (info, &h->root)
4590
0
      : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4591
0
    return true;
4592
4593
  /* If this is an executable that must provide a definition of the symbol,
4594
     either though PLTs or copy relocations, then that address should go in
4595
     the local rather than global GOT.  */
4596
0
  if (bfd_link_executable (info) && h->has_static_relocs)
4597
0
    return true;
4598
4599
0
  return false;
4600
0
}
4601
4602
/* A mips_elf_link_hash_traverse callback for which DATA points to the
4603
   link_info structure.  Decide whether the hash entry needs an entry in
4604
   the global part of the primary GOT, setting global_got_area accordingly.
4605
   Count the number of global symbols that are in the primary GOT only
4606
   because they have relocations against them (reloc_only_gotno).  */
4607
4608
static bool
4609
mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4610
0
{
4611
0
  struct bfd_link_info *info;
4612
0
  struct mips_elf_link_hash_table *htab;
4613
0
  struct mips_got_info *g;
4614
4615
0
  info = (struct bfd_link_info *) data;
4616
0
  htab = mips_elf_hash_table (info);
4617
0
  g = htab->got_info;
4618
0
  if (h->global_got_area != GGA_NONE)
4619
0
    {
4620
      /* Make a final decision about whether the symbol belongs in the
4621
   local or global GOT.  */
4622
0
      if (mips_use_local_got_p (info, h))
4623
  /* The symbol belongs in the local GOT.  We no longer need this
4624
     entry if it was only used for relocations; those relocations
4625
     will be against the null or section symbol instead of H.  */
4626
0
  h->global_got_area = GGA_NONE;
4627
0
      else if (htab->root.target_os == is_vxworks
4628
0
         && h->got_only_for_calls
4629
0
         && h->root.plt.plist->mips_offset != MINUS_ONE)
4630
  /* On VxWorks, calls can refer directly to the .got.plt entry;
4631
     they don't need entries in the regular GOT.  .got.plt entries
4632
     will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
4633
0
  h->global_got_area = GGA_NONE;
4634
0
      else if (h->global_got_area == GGA_RELOC_ONLY)
4635
0
  {
4636
0
    g->reloc_only_gotno++;
4637
0
    g->global_gotno++;
4638
0
  }
4639
0
    }
4640
0
  return 1;
4641
0
}
4642

4643
/* A htab_traverse callback for GOT entries.  Add each one to the GOT
4644
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4645
4646
static int
4647
mips_elf_add_got_entry (void **entryp, void *data)
4648
0
{
4649
0
  struct mips_got_entry *entry;
4650
0
  struct mips_elf_traverse_got_arg *arg;
4651
0
  void **slot;
4652
4653
0
  entry = (struct mips_got_entry *) *entryp;
4654
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4655
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4656
0
  if (!slot)
4657
0
    {
4658
0
      arg->g = NULL;
4659
0
      return 0;
4660
0
    }
4661
0
  if (!*slot)
4662
0
    {
4663
0
      *slot = entry;
4664
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4665
0
    }
4666
0
  return 1;
4667
0
}
4668
4669
/* A htab_traverse callback for GOT page entries.  Add each one to the GOT
4670
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4671
4672
static int
4673
mips_elf_add_got_page_entry (void **entryp, void *data)
4674
0
{
4675
0
  struct mips_got_page_entry *entry;
4676
0
  struct mips_elf_traverse_got_arg *arg;
4677
0
  void **slot;
4678
4679
0
  entry = (struct mips_got_page_entry *) *entryp;
4680
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4681
0
  slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4682
0
  if (!slot)
4683
0
    {
4684
0
      arg->g = NULL;
4685
0
      return 0;
4686
0
    }
4687
0
  if (!*slot)
4688
0
    {
4689
0
      *slot = entry;
4690
0
      arg->g->page_gotno += entry->num_pages;
4691
0
    }
4692
0
  return 1;
4693
0
}
4694
4695
/* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
4696
   this would lead to overflow, 1 if they were merged successfully,
4697
   and 0 if a merge failed due to lack of memory.  (These values are chosen
4698
   so that nonnegative return values can be returned by a htab_traverse
4699
   callback.)  */
4700
4701
static int
4702
mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4703
       struct mips_got_info *to,
4704
       struct mips_elf_got_per_bfd_arg *arg)
4705
0
{
4706
0
  struct mips_elf_traverse_got_arg tga;
4707
0
  unsigned int estimate;
4708
4709
  /* Work out how many page entries we would need for the combined GOT.  */
4710
0
  estimate = arg->max_pages;
4711
0
  if (estimate >= from->page_gotno + to->page_gotno)
4712
0
    estimate = from->page_gotno + to->page_gotno;
4713
4714
  /* And conservatively estimate how many local and TLS entries
4715
     would be needed.  */
4716
0
  estimate += from->local_gotno + to->local_gotno;
4717
0
  estimate += from->tls_gotno + to->tls_gotno;
4718
4719
  /* If we're merging with the primary got, any TLS relocations will
4720
     come after the full set of global entries.  Otherwise estimate those
4721
     conservatively as well.  */
4722
0
  if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4723
0
    estimate += arg->global_count;
4724
0
  else
4725
0
    estimate += from->global_gotno + to->global_gotno;
4726
4727
  /* Bail out if the combined GOT might be too big.  */
4728
0
  if (estimate > arg->max_count)
4729
0
    return -1;
4730
4731
  /* Transfer the bfd's got information from FROM to TO.  */
4732
0
  tga.info = arg->info;
4733
0
  tga.g = to;
4734
0
  htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4735
0
  if (!tga.g)
4736
0
    return 0;
4737
4738
0
  htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4739
0
  if (!tga.g)
4740
0
    return 0;
4741
4742
0
  mips_elf_replace_bfd_got (abfd, to);
4743
0
  return 1;
4744
0
}
4745
4746
/* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
4747
   as possible of the primary got, since it doesn't require explicit
4748
   dynamic relocations, but don't use bfds that would reference global
4749
   symbols out of the addressable range.  Failing the primary got,
4750
   attempt to merge with the current got, or finish the current got
4751
   and then make make the new got current.  */
4752
4753
static bool
4754
mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4755
        struct mips_elf_got_per_bfd_arg *arg)
4756
0
{
4757
0
  unsigned int estimate;
4758
0
  int result;
4759
4760
0
  if (!mips_elf_resolve_final_got_entries (arg->info, g))
4761
0
    return false;
4762
4763
  /* Work out the number of page, local and TLS entries.  */
4764
0
  estimate = arg->max_pages;
4765
0
  if (estimate > g->page_gotno)
4766
0
    estimate = g->page_gotno;
4767
0
  estimate += g->local_gotno + g->tls_gotno;
4768
4769
  /* We place TLS GOT entries after both locals and globals.  The globals
4770
     for the primary GOT may overflow the normal GOT size limit, so be
4771
     sure not to merge a GOT which requires TLS with the primary GOT in that
4772
     case.  This doesn't affect non-primary GOTs.  */
4773
0
  estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4774
4775
0
  if (estimate <= arg->max_count)
4776
0
    {
4777
      /* If we don't have a primary GOT, use it as
4778
   a starting point for the primary GOT.  */
4779
0
      if (!arg->primary)
4780
0
  {
4781
0
    arg->primary = g;
4782
0
    return true;
4783
0
  }
4784
4785
      /* Try merging with the primary GOT.  */
4786
0
      result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4787
0
      if (result >= 0)
4788
0
  return result;
4789
0
    }
4790
4791
  /* If we can merge with the last-created got, do it.  */
4792
0
  if (arg->current)
4793
0
    {
4794
0
      result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4795
0
      if (result >= 0)
4796
0
  return result;
4797
0
    }
4798
4799
  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4800
     fits; if it turns out that it doesn't, we'll get relocation
4801
     overflows anyway.  */
4802
0
  g->next = arg->current;
4803
0
  arg->current = g;
4804
4805
0
  return true;
4806
0
}
4807
4808
/* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
4809
   to GOTIDX, duplicating the entry if it has already been assigned
4810
   an index in a different GOT.  */
4811
4812
static bool
4813
mips_elf_set_gotidx (void **entryp, long gotidx)
4814
0
{
4815
0
  struct mips_got_entry *entry;
4816
4817
0
  entry = (struct mips_got_entry *) *entryp;
4818
0
  if (entry->gotidx > 0)
4819
0
    {
4820
0
      struct mips_got_entry *new_entry;
4821
4822
0
      new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4823
0
      if (!new_entry)
4824
0
  return false;
4825
4826
0
      *new_entry = *entry;
4827
0
      *entryp = new_entry;
4828
0
      entry = new_entry;
4829
0
    }
4830
0
  entry->gotidx = gotidx;
4831
0
  return true;
4832
0
}
4833
4834
/* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
4835
   mips_elf_traverse_got_arg in which DATA->value is the size of one
4836
   GOT entry.  Set DATA->g to null on failure.  */
4837
4838
static int
4839
mips_elf_initialize_tls_index (void **entryp, void *data)
4840
0
{
4841
0
  struct mips_got_entry *entry;
4842
0
  struct mips_elf_traverse_got_arg *arg;
4843
4844
  /* We're only interested in TLS symbols.  */
4845
0
  entry = (struct mips_got_entry *) *entryp;
4846
0
  if (entry->tls_type == GOT_TLS_NONE)
4847
0
    return 1;
4848
4849
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4850
0
  if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4851
0
    {
4852
0
      arg->g = NULL;
4853
0
      return 0;
4854
0
    }
4855
4856
  /* Account for the entries we've just allocated.  */
4857
0
  arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4858
0
  return 1;
4859
0
}
4860
4861
/* A htab_traverse callback for GOT entries, where DATA points to a
4862
   mips_elf_traverse_got_arg.  Set the global_got_area of each global
4863
   symbol to DATA->value.  */
4864
4865
static int
4866
mips_elf_set_global_got_area (void **entryp, void *data)
4867
0
{
4868
0
  struct mips_got_entry *entry;
4869
0
  struct mips_elf_traverse_got_arg *arg;
4870
4871
0
  entry = (struct mips_got_entry *) *entryp;
4872
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4873
0
  if (entry->abfd != NULL
4874
0
      && entry->symndx == -1
4875
0
      && entry->d.h->global_got_area != GGA_NONE)
4876
0
    entry->d.h->global_got_area = arg->value;
4877
0
  return 1;
4878
0
}
4879
4880
/* A htab_traverse callback for secondary GOT entries, where DATA points
4881
   to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
4882
   and record the number of relocations they require.  DATA->value is
4883
   the size of one GOT entry.  Set DATA->g to null on failure.  */
4884
4885
static int
4886
mips_elf_set_global_gotidx (void **entryp, void *data)
4887
0
{
4888
0
  struct mips_got_entry *entry;
4889
0
  struct mips_elf_traverse_got_arg *arg;
4890
4891
0
  entry = (struct mips_got_entry *) *entryp;
4892
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4893
0
  if (entry->abfd != NULL
4894
0
      && entry->symndx == -1
4895
0
      && entry->d.h->global_got_area != GGA_NONE)
4896
0
    {
4897
0
      if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4898
0
  {
4899
0
    arg->g = NULL;
4900
0
    return 0;
4901
0
  }
4902
0
      arg->g->assigned_low_gotno += 1;
4903
4904
0
      if (bfd_link_pic (arg->info)
4905
0
    || (elf_hash_table (arg->info)->dynamic_sections_created
4906
0
        && entry->d.h->root.def_dynamic
4907
0
        && !entry->d.h->root.def_regular))
4908
0
  arg->g->relocs += 1;
4909
0
    }
4910
4911
0
  return 1;
4912
0
}
4913
4914
/* A htab_traverse callback for GOT entries for which DATA is the
4915
   bfd_link_info.  Forbid any global symbols from having traditional
4916
   lazy-binding stubs.  */
4917
4918
static int
4919
mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4920
0
{
4921
0
  struct bfd_link_info *info;
4922
0
  struct mips_elf_link_hash_table *htab;
4923
0
  struct mips_got_entry *entry;
4924
4925
0
  entry = (struct mips_got_entry *) *entryp;
4926
0
  info = (struct bfd_link_info *) data;
4927
0
  htab = mips_elf_hash_table (info);
4928
0
  BFD_ASSERT (htab != NULL);
4929
4930
0
  if (entry->abfd != NULL
4931
0
      && entry->symndx == -1
4932
0
      && entry->d.h->needs_lazy_stub)
4933
0
    {
4934
0
      entry->d.h->needs_lazy_stub = false;
4935
0
      htab->lazy_stub_count--;
4936
0
    }
4937
4938
0
  return 1;
4939
0
}
4940
4941
/* Return the offset of an input bfd IBFD's GOT from the beginning of
4942
   the primary GOT.  */
4943
static bfd_vma
4944
mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4945
0
{
4946
0
  if (!g->next)
4947
0
    return 0;
4948
4949
0
  g = mips_elf_bfd_got (ibfd, false);
4950
0
  if (! g)
4951
0
    return 0;
4952
4953
0
  BFD_ASSERT (g->next);
4954
4955
0
  g = g->next;
4956
4957
0
  return (g->local_gotno + g->global_gotno + g->tls_gotno)
4958
0
    * MIPS_ELF_GOT_SIZE (abfd);
4959
0
}
4960
4961
/* Turn a single GOT that is too big for 16-bit addressing into
4962
   a sequence of GOTs, each one 16-bit addressable.  */
4963
4964
static bool
4965
mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4966
        asection *got, bfd_size_type pages)
4967
0
{
4968
0
  struct mips_elf_link_hash_table *htab;
4969
0
  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4970
0
  struct mips_elf_traverse_got_arg tga;
4971
0
  struct mips_got_info *g, *gg;
4972
0
  unsigned int assign, needed_relocs;
4973
0
  bfd *dynobj, *ibfd;
4974
4975
0
  dynobj = elf_hash_table (info)->dynobj;
4976
0
  htab = mips_elf_hash_table (info);
4977
0
  BFD_ASSERT (htab != NULL);
4978
4979
0
  g = htab->got_info;
4980
4981
0
  got_per_bfd_arg.obfd = abfd;
4982
0
  got_per_bfd_arg.info = info;
4983
0
  got_per_bfd_arg.current = NULL;
4984
0
  got_per_bfd_arg.primary = NULL;
4985
0
  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4986
0
        / MIPS_ELF_GOT_SIZE (abfd))
4987
0
             - htab->reserved_gotno);
4988
0
  got_per_bfd_arg.max_pages = pages;
4989
  /* The number of globals that will be included in the primary GOT.
4990
     See the calls to mips_elf_set_global_got_area below for more
4991
     information.  */
4992
0
  got_per_bfd_arg.global_count = g->global_gotno;
4993
4994
  /* Try to merge the GOTs of input bfds together, as long as they
4995
     don't seem to exceed the maximum GOT size, choosing one of them
4996
     to be the primary GOT.  */
4997
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4998
0
    {
4999
0
      gg = mips_elf_bfd_got (ibfd, false);
5000
0
      if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5001
0
  return false;
5002
0
    }
5003
5004
  /* If we do not find any suitable primary GOT, create an empty one.  */
5005
0
  if (got_per_bfd_arg.primary == NULL)
5006
0
    g->next = mips_elf_create_got_info (abfd);
5007
0
  else
5008
0
    g->next = got_per_bfd_arg.primary;
5009
0
  g->next->next = got_per_bfd_arg.current;
5010
5011
  /* GG is now the master GOT, and G is the primary GOT.  */
5012
0
  gg = g;
5013
0
  g = g->next;
5014
5015
  /* Map the output bfd to the primary got.  That's what we're going
5016
     to use for bfds that use GOT16 or GOT_PAGE relocations that we
5017
     didn't mark in check_relocs, and we want a quick way to find it.
5018
     We can't just use gg->next because we're going to reverse the
5019
     list.  */
5020
0
  mips_elf_replace_bfd_got (abfd, g);
5021
5022
  /* Every symbol that is referenced in a dynamic relocation must be
5023
     present in the primary GOT, so arrange for them to appear after
5024
     those that are actually referenced.  */
5025
0
  gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5026
0
  g->global_gotno = gg->global_gotno;
5027
5028
0
  tga.info = info;
5029
0
  tga.value = GGA_RELOC_ONLY;
5030
0
  htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5031
0
  tga.value = GGA_NORMAL;
5032
0
  htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5033
5034
  /* Now go through the GOTs assigning them offset ranges.
5035
     [assigned_low_gotno, local_gotno[ will be set to the range of local
5036
     entries in each GOT.  We can then compute the end of a GOT by
5037
     adding local_gotno to global_gotno.  We reverse the list and make
5038
     it circular since then we'll be able to quickly compute the
5039
     beginning of a GOT, by computing the end of its predecessor.  To
5040
     avoid special cases for the primary GOT, while still preserving
5041
     assertions that are valid for both single- and multi-got links,
5042
     we arrange for the main got struct to have the right number of
5043
     global entries, but set its local_gotno such that the initial
5044
     offset of the primary GOT is zero.  Remember that the primary GOT
5045
     will become the last item in the circular linked list, so it
5046
     points back to the master GOT.  */
5047
0
  gg->local_gotno = -g->global_gotno;
5048
0
  gg->global_gotno = g->global_gotno;
5049
0
  gg->tls_gotno = 0;
5050
0
  assign = 0;
5051
0
  gg->next = gg;
5052
5053
0
  do
5054
0
    {
5055
0
      struct mips_got_info *gn;
5056
5057
0
      assign += htab->reserved_gotno;
5058
0
      g->assigned_low_gotno = assign;
5059
0
      g->local_gotno += assign;
5060
0
      g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5061
0
      g->assigned_high_gotno = g->local_gotno - 1;
5062
0
      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5063
5064
      /* Take g out of the direct list, and push it onto the reversed
5065
   list that gg points to.  g->next is guaranteed to be nonnull after
5066
   this operation, as required by mips_elf_initialize_tls_index. */
5067
0
      gn = g->next;
5068
0
      g->next = gg->next;
5069
0
      gg->next = g;
5070
5071
      /* Set up any TLS entries.  We always place the TLS entries after
5072
   all non-TLS entries.  */
5073
0
      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5074
0
      tga.g = g;
5075
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5076
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5077
0
      if (!tga.g)
5078
0
  return false;
5079
0
      BFD_ASSERT (g->tls_assigned_gotno == assign);
5080
5081
      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
5082
0
      g = gn;
5083
5084
      /* Forbid global symbols in every non-primary GOT from having
5085
   lazy-binding stubs.  */
5086
0
      if (g)
5087
0
  htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5088
0
    }
5089
0
  while (g);
5090
5091
0
  got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5092
5093
0
  needed_relocs = 0;
5094
0
  for (g = gg->next; g && g->next != gg; g = g->next)
5095
0
    {
5096
0
      unsigned int save_assign;
5097
5098
      /* Assign offsets to global GOT entries and count how many
5099
   relocations they need.  */
5100
0
      save_assign = g->assigned_low_gotno;
5101
0
      g->assigned_low_gotno = g->local_gotno;
5102
0
      tga.info = info;
5103
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5104
0
      tga.g = g;
5105
0
      htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5106
0
      if (!tga.g)
5107
0
  return false;
5108
0
      BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5109
0
      g->assigned_low_gotno = save_assign;
5110
5111
0
      if (bfd_link_pic (info))
5112
0
  {
5113
0
    g->relocs += g->local_gotno - g->assigned_low_gotno;
5114
0
    BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5115
0
          + g->next->global_gotno
5116
0
          + g->next->tls_gotno
5117
0
          + htab->reserved_gotno);
5118
0
  }
5119
0
      needed_relocs += g->relocs;
5120
0
    }
5121
0
  needed_relocs += g->relocs;
5122
5123
0
  if (needed_relocs)
5124
0
    mips_elf_allocate_dynamic_relocations (dynobj, info,
5125
0
             needed_relocs);
5126
5127
0
  return true;
5128
0
}
5129
5130

5131
/* Returns the first relocation of type r_type found, beginning with
5132
   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5133
5134
static const Elf_Internal_Rela *
5135
mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5136
        const Elf_Internal_Rela *relocation,
5137
        const Elf_Internal_Rela *relend)
5138
0
{
5139
0
  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5140
5141
0
  while (relocation < relend)
5142
0
    {
5143
0
      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5144
0
    && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5145
0
  return relocation;
5146
5147
0
      ++relocation;
5148
0
    }
5149
5150
  /* We didn't find it.  */
5151
0
  return NULL;
5152
0
}
5153
5154
/* Return whether an input relocation is against a local symbol.  */
5155
5156
static bool
5157
mips_elf_local_relocation_p (bfd *input_bfd,
5158
           const Elf_Internal_Rela *relocation,
5159
           asection **local_sections)
5160
0
{
5161
0
  unsigned long r_symndx;
5162
0
  Elf_Internal_Shdr *symtab_hdr;
5163
0
  size_t extsymoff;
5164
5165
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5166
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5167
0
  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5168
5169
0
  if (r_symndx < extsymoff)
5170
0
    return true;
5171
0
  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5172
0
    return true;
5173
5174
0
  return false;
5175
0
}
5176

5177
/* Sign-extend VALUE, which has the indicated number of BITS.  */
5178
5179
bfd_vma
5180
_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5181
0
{
5182
0
  if (value & ((bfd_vma) 1 << (bits - 1)))
5183
    /* VALUE is negative.  */
5184
0
    value |= ((bfd_vma) - 1) << bits;
5185
5186
0
  return value;
5187
0
}
5188
5189
/* Return non-zero if the indicated VALUE has overflowed the maximum
5190
   range expressible by a signed number with the indicated number of
5191
   BITS.  */
5192
5193
static bool
5194
mips_elf_overflow_p (bfd_vma value, int bits)
5195
0
{
5196
0
  bfd_signed_vma svalue = (bfd_signed_vma) value;
5197
5198
0
  if (svalue > (1 << (bits - 1)) - 1)
5199
    /* The value is too big.  */
5200
0
    return true;
5201
0
  else if (svalue < -(1 << (bits - 1)))
5202
    /* The value is too small.  */
5203
0
    return true;
5204
5205
  /* All is well.  */
5206
0
  return false;
5207
0
}
5208
5209
/* Calculate the %high function.  */
5210
5211
static bfd_vma
5212
mips_elf_high (bfd_vma value)
5213
0
{
5214
0
  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5215
0
}
5216
5217
/* Calculate the %higher function.  */
5218
5219
static bfd_vma
5220
mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5221
0
{
5222
0
#ifdef BFD64
5223
0
  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5224
#else
5225
  abort ();
5226
  return MINUS_ONE;
5227
#endif
5228
0
}
5229
5230
/* Calculate the %highest function.  */
5231
5232
static bfd_vma
5233
mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5234
0
{
5235
0
#ifdef BFD64
5236
0
  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5237
#else
5238
  abort ();
5239
  return MINUS_ONE;
5240
#endif
5241
0
}
5242

5243
/* Create the .compact_rel section.  */
5244
5245
static bool
5246
mips_elf_create_compact_rel_section
5247
  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5248
0
{
5249
0
  flagword flags;
5250
0
  register asection *s;
5251
5252
0
  if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5253
0
    {
5254
0
      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5255
0
         | SEC_READONLY);
5256
5257
0
      s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5258
0
      if (s == NULL
5259
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5260
0
  return false;
5261
5262
0
      s->size = sizeof (Elf32_External_compact_rel);
5263
0
    }
5264
5265
0
  return true;
5266
0
}
5267
5268
/* Create the .got section to hold the global offset table.  */
5269
5270
static bool
5271
mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5272
0
{
5273
0
  flagword flags;
5274
0
  register asection *s;
5275
0
  struct elf_link_hash_entry *h;
5276
0
  struct bfd_link_hash_entry *bh;
5277
0
  struct mips_elf_link_hash_table *htab;
5278
5279
0
  htab = mips_elf_hash_table (info);
5280
0
  BFD_ASSERT (htab != NULL);
5281
5282
  /* This function may be called more than once.  */
5283
0
  if (htab->root.sgot)
5284
0
    return true;
5285
5286
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5287
0
     | SEC_LINKER_CREATED);
5288
5289
  /* We have to use an alignment of 2**4 here because this is hardcoded
5290
     in the function stub generation and in the linker script.  */
5291
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5292
0
  if (s == NULL
5293
0
      || !bfd_set_section_alignment (s, 4))
5294
0
    return false;
5295
0
  htab->root.sgot = s;
5296
5297
  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5298
     linker script because we don't want to define the symbol if we
5299
     are not creating a global offset table.  */
5300
0
  bh = NULL;
5301
0
  if (! (_bfd_generic_link_add_one_symbol
5302
0
   (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5303
0
    0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5304
0
    return false;
5305
5306
0
  h = (struct elf_link_hash_entry *) bh;
5307
0
  h->non_elf = 0;
5308
0
  h->def_regular = 1;
5309
0
  h->type = STT_OBJECT;
5310
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5311
0
  elf_hash_table (info)->hgot = h;
5312
5313
0
  if (bfd_link_pic (info)
5314
0
      && ! bfd_elf_link_record_dynamic_symbol (info, h))
5315
0
    return false;
5316
5317
0
  htab->got_info = mips_elf_create_got_info (abfd);
5318
0
  mips_elf_section_data (s)->elf.this_hdr.sh_flags
5319
0
    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5320
5321
  /* We also need a .got.plt section when generating PLTs.  */
5322
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5323
0
            SEC_ALLOC | SEC_LOAD
5324
0
            | SEC_HAS_CONTENTS
5325
0
            | SEC_IN_MEMORY
5326
0
            | SEC_LINKER_CREATED);
5327
0
  if (s == NULL)
5328
0
    return false;
5329
0
  htab->root.sgotplt = s;
5330
5331
0
  return true;
5332
0
}
5333

5334
/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5335
   __GOTT_INDEX__ symbols.  These symbols are only special for
5336
   shared objects; they are not used in executables.  */
5337
5338
static bool
5339
is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5340
0
{
5341
0
  return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5342
0
    && bfd_link_pic (info)
5343
0
    && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5344
0
        || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5345
0
}
5346
5347
/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5348
   require an la25 stub.  See also mips_elf_local_pic_function_p,
5349
   which determines whether the destination function ever requires a
5350
   stub.  */
5351
5352
static bool
5353
mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5354
             bool target_is_16_bit_code_p)
5355
0
{
5356
  /* We specifically ignore branches and jumps from EF_PIC objects,
5357
     where the onus is on the compiler or programmer to perform any
5358
     necessary initialization of $25.  Sometimes such initialization
5359
     is unnecessary; for example, -mno-shared functions do not use
5360
     the incoming value of $25, and may therefore be called directly.  */
5361
0
  if (PIC_OBJECT_P (input_bfd))
5362
0
    return false;
5363
5364
0
  switch (r_type)
5365
0
    {
5366
0
    case R_MIPS_26:
5367
0
    case R_MIPS_PC16:
5368
0
    case R_MIPS_PC21_S2:
5369
0
    case R_MIPS_PC26_S2:
5370
0
    case R_MICROMIPS_26_S1:
5371
0
    case R_MICROMIPS_PC7_S1:
5372
0
    case R_MICROMIPS_PC10_S1:
5373
0
    case R_MICROMIPS_PC16_S1:
5374
0
    case R_MICROMIPS_PC23_S2:
5375
0
      return true;
5376
5377
0
    case R_MIPS16_26:
5378
0
      return !target_is_16_bit_code_p;
5379
5380
0
    default:
5381
0
      return false;
5382
0
    }
5383
0
}
5384

5385
/* Obtain the field relocated by RELOCATION.  */
5386
5387
static bfd_vma
5388
mips_elf_obtain_contents (reloc_howto_type *howto,
5389
        const Elf_Internal_Rela *relocation,
5390
        bfd *input_bfd, bfd_byte *contents)
5391
0
{
5392
0
  bfd_vma x = 0;
5393
0
  bfd_byte *location = contents + relocation->r_offset;
5394
0
  unsigned int size = bfd_get_reloc_size (howto);
5395
5396
  /* Obtain the bytes.  */
5397
0
  if (size != 0)
5398
0
    x = bfd_get (8 * size, input_bfd, location);
5399
5400
0
  return x;
5401
0
}
5402
5403
/* Store the field relocated by RELOCATION.  */
5404
5405
static void
5406
mips_elf_store_contents (reloc_howto_type *howto,
5407
       const Elf_Internal_Rela *relocation,
5408
       bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5409
0
{
5410
0
  bfd_byte *location = contents + relocation->r_offset;
5411
0
  unsigned int size = bfd_get_reloc_size (howto);
5412
5413
  /* Put the value into the output.  */
5414
0
  if (size != 0)
5415
0
    bfd_put (8 * size, input_bfd, x, location);
5416
0
}
5417
5418
/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5419
   RELOCATION described by HOWTO, with a move of 0 to the load target
5420
   register, returning TRUE if that is successful and FALSE otherwise.
5421
   If DOIT is FALSE, then only determine it patching is possible and
5422
   return status without actually changing CONTENTS.
5423
*/
5424
5425
static bool
5426
mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5427
         const Elf_Internal_Rela *relocation,
5428
         reloc_howto_type *howto, bool doit)
5429
0
{
5430
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5431
0
  bfd_byte *location = contents + relocation->r_offset;
5432
0
  bool nullified = true;
5433
0
  bfd_vma x;
5434
5435
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5436
5437
  /* Obtain the current value.  */
5438
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5439
5440
  /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5441
     while RY is at bits [18:16] of the combined 32-bit instruction word.  */
5442
0
  if (mips16_reloc_p (r_type)
5443
0
      && (((x >> 22) & 0x3ff) == 0x3d3        /* LW */
5444
0
    || ((x >> 22) & 0x3ff) == 0x3c7))      /* LD */
5445
0
    x = (0x3cdU << 22) | (x & (7 << 16)) << 3;     /* LI */
5446
0
  else if (micromips_reloc_p (r_type)
5447
0
     && ((x >> 26) & 0x37) == 0x37)     /* LW/LD */
5448
0
    x = (0xc << 26) | (x & (0x1f << 21));     /* ADDIU */
5449
0
  else if (((x >> 26) & 0x3f) == 0x23        /* LW */
5450
0
     || ((x >> 26) & 0x3f) == 0x37)     /* LD */
5451
0
    x = (0x9 << 26) | (x & (0x1f << 16));     /* ADDIU */
5452
0
  else
5453
0
    nullified = false;
5454
5455
  /* Put the value into the output.  */
5456
0
  if (doit && nullified)
5457
0
    mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5458
5459
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5460
5461
0
  return nullified;
5462
0
}
5463
5464
/* Calculate the value produced by the RELOCATION (which comes from
5465
   the INPUT_BFD).  The ADDEND is the addend to use for this
5466
   RELOCATION; RELOCATION->R_ADDEND is ignored.
5467
5468
   The result of the relocation calculation is stored in VALUEP.
5469
   On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5470
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5471
5472
   This function returns bfd_reloc_continue if the caller need take no
5473
   further action regarding this relocation, bfd_reloc_notsupported if
5474
   something goes dramatically wrong, bfd_reloc_overflow if an
5475
   overflow occurs, and bfd_reloc_ok to indicate success.  */
5476
5477
static bfd_reloc_status_type
5478
mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5479
             asection *input_section, bfd_byte *contents,
5480
             struct bfd_link_info *info,
5481
             const Elf_Internal_Rela *relocation,
5482
             bfd_vma addend, reloc_howto_type *howto,
5483
             Elf_Internal_Sym *local_syms,
5484
             asection **local_sections, bfd_vma *valuep,
5485
             const char **namep,
5486
             bool *cross_mode_jump_p,
5487
             bool save_addend)
5488
0
{
5489
  /* The eventual value we will return.  */
5490
0
  bfd_vma value;
5491
  /* The address of the symbol against which the relocation is
5492
     occurring.  */
5493
0
  bfd_vma symbol = 0;
5494
  /* The final GP value to be used for the relocatable, executable, or
5495
     shared object file being produced.  */
5496
0
  bfd_vma gp;
5497
  /* The place (section offset or address) of the storage unit being
5498
     relocated.  */
5499
0
  bfd_vma p;
5500
  /* The value of GP used to create the relocatable object.  */
5501
0
  bfd_vma gp0;
5502
  /* The offset into the global offset table at which the address of
5503
     the relocation entry symbol, adjusted by the addend, resides
5504
     during execution.  */
5505
0
  bfd_vma g = MINUS_ONE;
5506
  /* The section in which the symbol referenced by the relocation is
5507
     located.  */
5508
0
  asection *sec = NULL;
5509
0
  struct mips_elf_link_hash_entry *h = NULL;
5510
  /* TRUE if the symbol referred to by this relocation is a local
5511
     symbol.  */
5512
0
  bool local_p, was_local_p;
5513
  /* TRUE if the symbol referred to by this relocation is a section
5514
     symbol.  */
5515
0
  bool section_p = false;
5516
  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
5517
0
  bool gp_disp_p = false;
5518
  /* TRUE if the symbol referred to by this relocation is
5519
     "__gnu_local_gp".  */
5520
0
  bool gnu_local_gp_p = false;
5521
0
  Elf_Internal_Shdr *symtab_hdr;
5522
0
  size_t extsymoff;
5523
0
  unsigned long r_symndx;
5524
0
  int r_type;
5525
  /* TRUE if overflow occurred during the calculation of the
5526
     relocation value.  */
5527
0
  bool overflowed_p;
5528
  /* TRUE if this relocation refers to a MIPS16 function.  */
5529
0
  bool target_is_16_bit_code_p = false;
5530
0
  bool target_is_micromips_code_p = false;
5531
0
  struct mips_elf_link_hash_table *htab;
5532
0
  bfd *dynobj;
5533
0
  bool resolved_to_zero;
5534
5535
0
  dynobj = elf_hash_table (info)->dynobj;
5536
0
  htab = mips_elf_hash_table (info);
5537
0
  BFD_ASSERT (htab != NULL);
5538
5539
  /* Parse the relocation.  */
5540
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5541
0
  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5542
0
  p = (input_section->output_section->vma
5543
0
       + input_section->output_offset
5544
0
       + relocation->r_offset);
5545
5546
  /* Assume that there will be no overflow.  */
5547
0
  overflowed_p = false;
5548
5549
  /* Figure out whether or not the symbol is local, and get the offset
5550
     used in the array of hash table entries.  */
5551
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5552
0
  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5553
0
           local_sections);
5554
0
  was_local_p = local_p;
5555
0
  if (! elf_bad_symtab (input_bfd))
5556
0
    extsymoff = symtab_hdr->sh_info;
5557
0
  else
5558
0
    {
5559
      /* The symbol table does not follow the rule that local symbols
5560
   must come before globals.  */
5561
0
      extsymoff = 0;
5562
0
    }
5563
5564
  /* Figure out the value of the symbol.  */
5565
0
  if (local_p)
5566
0
    {
5567
0
      bool micromips_p = MICROMIPS_P (abfd);
5568
0
      Elf_Internal_Sym *sym;
5569
5570
0
      sym = local_syms + r_symndx;
5571
0
      sec = local_sections[r_symndx];
5572
5573
0
      section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5574
5575
0
      symbol = sec->output_section->vma + sec->output_offset;
5576
0
      if (!section_p || (sec->flags & SEC_MERGE))
5577
0
  symbol += sym->st_value;
5578
0
      if ((sec->flags & SEC_MERGE) && section_p)
5579
0
  {
5580
0
    addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5581
0
    addend -= symbol;
5582
0
    addend += sec->output_section->vma + sec->output_offset;
5583
0
  }
5584
5585
      /* MIPS16/microMIPS text labels should be treated as odd.  */
5586
0
      if (ELF_ST_IS_COMPRESSED (sym->st_other))
5587
0
  ++symbol;
5588
5589
      /* Record the name of this symbol, for our caller.  */
5590
0
      *namep = bfd_elf_string_from_elf_section (input_bfd,
5591
0
            symtab_hdr->sh_link,
5592
0
            sym->st_name);
5593
0
      if (*namep == NULL || **namep == '\0')
5594
0
  *namep = bfd_section_name (sec);
5595
5596
      /* For relocations against a section symbol and ones against no
5597
   symbol (absolute relocations) infer the ISA mode from the addend.  */
5598
0
      if (section_p || r_symndx == STN_UNDEF)
5599
0
  {
5600
0
    target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5601
0
    target_is_micromips_code_p = (addend & 1) && micromips_p;
5602
0
  }
5603
      /* For relocations against an absolute symbol infer the ISA mode
5604
   from the value of the symbol plus addend.  */
5605
0
      else if (bfd_is_abs_section (sec))
5606
0
  {
5607
0
    target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5608
0
    target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5609
0
  }
5610
      /* Otherwise just use the regular symbol annotation available.  */
5611
0
      else
5612
0
  {
5613
0
    target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5614
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5615
0
  }
5616
0
    }
5617
0
  else
5618
0
    {
5619
      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
5620
5621
      /* For global symbols we look up the symbol in the hash-table.  */
5622
0
      h = ((struct mips_elf_link_hash_entry *)
5623
0
     elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5624
      /* Find the real hash-table entry for this symbol.  */
5625
0
      while (h->root.root.type == bfd_link_hash_indirect
5626
0
       || h->root.root.type == bfd_link_hash_warning)
5627
0
  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5628
5629
      /* Record the name of this symbol, for our caller.  */
5630
0
      *namep = h->root.root.root.string;
5631
5632
      /* See if this is the special _gp_disp symbol.  Note that such a
5633
   symbol must always be a global symbol.  */
5634
0
      if (strcmp (*namep, "_gp_disp") == 0
5635
0
    && ! NEWABI_P (input_bfd))
5636
0
  {
5637
    /* Relocations against _gp_disp are permitted only with
5638
       R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5639
0
    if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5640
0
      return bfd_reloc_notsupported;
5641
5642
0
    gp_disp_p = true;
5643
0
  }
5644
      /* See if this is the special _gp symbol.  Note that such a
5645
   symbol must always be a global symbol.  */
5646
0
      else if (strcmp (*namep, "__gnu_local_gp") == 0)
5647
0
  gnu_local_gp_p = true;
5648
5649
5650
      /* If this symbol is defined, calculate its address.  Note that
5651
   _gp_disp is a magic symbol, always implicitly defined by the
5652
   linker, so it's inappropriate to check to see whether or not
5653
   its defined.  */
5654
0
      else if ((h->root.root.type == bfd_link_hash_defined
5655
0
    || h->root.root.type == bfd_link_hash_defweak)
5656
0
         && h->root.root.u.def.section)
5657
0
  {
5658
0
    sec = h->root.root.u.def.section;
5659
0
    if (sec->output_section)
5660
0
      symbol = (h->root.root.u.def.value
5661
0
          + sec->output_section->vma
5662
0
          + sec->output_offset);
5663
0
    else
5664
0
      symbol = h->root.root.u.def.value;
5665
0
  }
5666
0
      else if (h->root.root.type == bfd_link_hash_undefweak)
5667
  /* We allow relocations against undefined weak symbols, giving
5668
     it the value zero, so that you can undefined weak functions
5669
     and check to see if they exist by looking at their
5670
     addresses.  */
5671
0
  symbol = 0;
5672
0
      else if (info->unresolved_syms_in_objects == RM_IGNORE
5673
0
         && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5674
0
  symbol = 0;
5675
0
      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5676
0
           ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5677
0
  {
5678
    /* If this is a dynamic link, we should have created a
5679
       _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5680
       in _bfd_mips_elf_create_dynamic_sections.
5681
       Otherwise, we should define the symbol with a value of 0.
5682
       FIXME: It should probably get into the symbol table
5683
       somehow as well.  */
5684
0
    BFD_ASSERT (! bfd_link_pic (info));
5685
0
    BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5686
0
    symbol = 0;
5687
0
  }
5688
0
      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5689
0
  {
5690
    /* This is an optional symbol - an Irix specific extension to the
5691
       ELF spec.  Ignore it for now.
5692
       XXX - FIXME - there is more to the spec for OPTIONAL symbols
5693
       than simply ignoring them, but we do not handle this for now.
5694
       For information see the "64-bit ELF Object File Specification"
5695
       which is available from here:
5696
       http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5697
0
    symbol = 0;
5698
0
  }
5699
0
      else
5700
0
  {
5701
0
          bool reject_undefined
5702
0
      = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5703
0
    && !info->warn_unresolved_syms)
5704
0
         || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5705
5706
0
    info->callbacks->undefined_symbol
5707
0
      (info, h->root.root.root.string, input_bfd,
5708
0
       input_section, relocation->r_offset, reject_undefined);
5709
5710
0
    if (reject_undefined)
5711
0
      return bfd_reloc_undefined;
5712
5713
0
    symbol = 0;
5714
0
  }
5715
5716
0
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5717
0
      target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5718
0
    }
5719
5720
  /* If this is a reference to a 16-bit function with a stub, we need
5721
     to redirect the relocation to the stub unless:
5722
5723
     (a) the relocation is for a MIPS16 JAL;
5724
5725
     (b) the relocation is for a MIPS16 PIC call, and there are no
5726
   non-MIPS16 uses of the GOT slot; or
5727
5728
     (c) the section allows direct references to MIPS16 functions.  */
5729
0
  if (r_type != R_MIPS16_26
5730
0
      && !bfd_link_relocatable (info)
5731
0
      && ((h != NULL
5732
0
     && h->fn_stub != NULL
5733
0
     && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5734
0
    || (local_p
5735
0
        && mips_elf_tdata (input_bfd)->local_stubs != NULL
5736
0
        && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5737
0
      && !section_allows_mips16_refs_p (input_section))
5738
0
    {
5739
      /* This is a 32- or 64-bit call to a 16-bit function.  We should
5740
   have already noticed that we were going to need the
5741
   stub.  */
5742
0
      if (local_p)
5743
0
  {
5744
0
    sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5745
0
    value = 0;
5746
0
  }
5747
0
      else
5748
0
  {
5749
0
    BFD_ASSERT (h->need_fn_stub);
5750
0
    if (h->la25_stub)
5751
0
      {
5752
        /* If a LA25 header for the stub itself exists, point to the
5753
     prepended LUI/ADDIU sequence.  */
5754
0
        sec = h->la25_stub->stub_section;
5755
0
        value = h->la25_stub->offset;
5756
0
      }
5757
0
    else
5758
0
      {
5759
0
        sec = h->fn_stub;
5760
0
        value = 0;
5761
0
      }
5762
0
  }
5763
5764
0
      symbol = sec->output_section->vma + sec->output_offset + value;
5765
      /* The target is 16-bit, but the stub isn't.  */
5766
0
      target_is_16_bit_code_p = false;
5767
0
    }
5768
  /* If this is a MIPS16 call with a stub, that is made through the PLT or
5769
     to a standard MIPS function, we need to redirect the call to the stub.
5770
     Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5771
     indirect calls should use an indirect stub instead.  */
5772
0
  else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5773
0
     && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5774
0
         || (local_p
5775
0
       && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5776
0
       && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5777
0
     && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5778
0
    {
5779
0
      if (local_p)
5780
0
  sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5781
0
      else
5782
0
  {
5783
    /* If both call_stub and call_fp_stub are defined, we can figure
5784
       out which one to use by checking which one appears in the input
5785
       file.  */
5786
0
    if (h->call_stub != NULL && h->call_fp_stub != NULL)
5787
0
      {
5788
0
        asection *o;
5789
5790
0
        sec = NULL;
5791
0
        for (o = input_bfd->sections; o != NULL; o = o->next)
5792
0
    {
5793
0
      if (CALL_FP_STUB_P (bfd_section_name (o)))
5794
0
        {
5795
0
          sec = h->call_fp_stub;
5796
0
          break;
5797
0
        }
5798
0
    }
5799
0
        if (sec == NULL)
5800
0
    sec = h->call_stub;
5801
0
      }
5802
0
    else if (h->call_stub != NULL)
5803
0
      sec = h->call_stub;
5804
0
    else
5805
0
      sec = h->call_fp_stub;
5806
0
  }
5807
5808
0
      BFD_ASSERT (sec->size > 0);
5809
0
      symbol = sec->output_section->vma + sec->output_offset;
5810
0
    }
5811
  /* If this is a direct call to a PIC function, redirect to the
5812
     non-PIC stub.  */
5813
0
  else if (h != NULL && h->la25_stub
5814
0
     && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5815
0
               target_is_16_bit_code_p))
5816
0
    {
5817
0
  symbol = (h->la25_stub->stub_section->output_section->vma
5818
0
      + h->la25_stub->stub_section->output_offset
5819
0
      + h->la25_stub->offset);
5820
0
  if (ELF_ST_IS_MICROMIPS (h->root.other))
5821
0
    symbol |= 1;
5822
0
    }
5823
  /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5824
     entry is used if a standard PLT entry has also been made.  In this
5825
     case the symbol will have been set by mips_elf_set_plt_sym_value
5826
     to point to the standard PLT entry, so redirect to the compressed
5827
     one.  */
5828
0
  else if ((mips16_branch_reloc_p (r_type)
5829
0
      || micromips_branch_reloc_p (r_type))
5830
0
     && !bfd_link_relocatable (info)
5831
0
     && h != NULL
5832
0
     && h->use_plt_entry
5833
0
     && h->root.plt.plist->comp_offset != MINUS_ONE
5834
0
     && h->root.plt.plist->mips_offset != MINUS_ONE)
5835
0
    {
5836
0
      bool micromips_p = MICROMIPS_P (abfd);
5837
5838
0
      sec = htab->root.splt;
5839
0
      symbol = (sec->output_section->vma
5840
0
    + sec->output_offset
5841
0
    + htab->plt_header_size
5842
0
    + htab->plt_mips_offset
5843
0
    + h->root.plt.plist->comp_offset
5844
0
    + 1);
5845
5846
0
      target_is_16_bit_code_p = !micromips_p;
5847
0
      target_is_micromips_code_p = micromips_p;
5848
0
    }
5849
5850
  /* Make sure MIPS16 and microMIPS are not used together.  */
5851
0
  if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5852
0
      || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5853
0
   {
5854
0
      _bfd_error_handler
5855
0
  (_("MIPS16 and microMIPS functions cannot call each other"));
5856
0
      return bfd_reloc_notsupported;
5857
0
   }
5858
5859
  /* Calls from 16-bit code to 32-bit code and vice versa require the
5860
     mode change.  However, we can ignore calls to undefined weak symbols,
5861
     which should never be executed at runtime.  This exception is important
5862
     because the assembly writer may have "known" that any definition of the
5863
     symbol would be 16-bit code, and that direct jumps were therefore
5864
     acceptable.  */
5865
0
  *cross_mode_jump_p = (!bfd_link_relocatable (info)
5866
0
      && !(h && h->root.root.type == bfd_link_hash_undefweak)
5867
0
      && ((mips16_branch_reloc_p (r_type)
5868
0
           && !target_is_16_bit_code_p)
5869
0
          || (micromips_branch_reloc_p (r_type)
5870
0
        && !target_is_micromips_code_p)
5871
0
          || ((branch_reloc_p (r_type)
5872
0
         || r_type == R_MIPS_JALR)
5873
0
        && (target_is_16_bit_code_p
5874
0
            || target_is_micromips_code_p))));
5875
5876
0
  resolved_to_zero = (h != NULL
5877
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5878
5879
0
  switch (r_type)
5880
0
    {
5881
0
    case R_MIPS16_CALL16:
5882
0
    case R_MIPS16_GOT16:
5883
0
    case R_MIPS_CALL16:
5884
0
    case R_MIPS_GOT16:
5885
0
    case R_MIPS_GOT_PAGE:
5886
0
    case R_MIPS_GOT_DISP:
5887
0
    case R_MIPS_GOT_LO16:
5888
0
    case R_MIPS_CALL_LO16:
5889
0
    case R_MICROMIPS_CALL16:
5890
0
    case R_MICROMIPS_GOT16:
5891
0
    case R_MICROMIPS_GOT_PAGE:
5892
0
    case R_MICROMIPS_GOT_DISP:
5893
0
    case R_MICROMIPS_GOT_LO16:
5894
0
    case R_MICROMIPS_CALL_LO16:
5895
0
      if (resolved_to_zero
5896
0
    && !bfd_link_relocatable (info)
5897
0
    && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5898
0
          relocation->r_offset)
5899
0
    && mips_elf_nullify_got_load (input_bfd, contents,
5900
0
          relocation, howto, true))
5901
0
  return bfd_reloc_continue;
5902
5903
      /* Fall through.  */
5904
0
    case R_MIPS_GOT_HI16:
5905
0
    case R_MIPS_CALL_HI16:
5906
0
    case R_MICROMIPS_GOT_HI16:
5907
0
    case R_MICROMIPS_CALL_HI16:
5908
0
      if (resolved_to_zero
5909
0
    && htab->use_absolute_zero
5910
0
    && bfd_link_pic (info))
5911
0
  {
5912
    /* Redirect to the special `__gnu_absolute_zero' symbol.  */
5913
0
    h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5914
0
           false, false, false);
5915
0
    BFD_ASSERT (h != NULL);
5916
0
  }
5917
0
      break;
5918
0
    }
5919
5920
0
  local_p = (h == NULL || mips_use_local_got_p (info, h));
5921
5922
0
  gp0 = _bfd_get_gp_value (input_bfd);
5923
0
  gp = _bfd_get_gp_value (abfd);
5924
0
  if (htab->got_info)
5925
0
    gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5926
5927
0
  if (gnu_local_gp_p)
5928
0
    symbol = gp;
5929
5930
  /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5931
     to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
5932
     corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
5933
0
  if (got_page_reloc_p (r_type) && !local_p)
5934
0
    {
5935
0
      r_type = (micromips_reloc_p (r_type)
5936
0
    ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5937
0
      addend = 0;
5938
0
    }
5939
5940
  /* If we haven't already determined the GOT offset, and we're going
5941
     to need it, get it now.  */
5942
0
  switch (r_type)
5943
0
    {
5944
0
    case R_MIPS16_CALL16:
5945
0
    case R_MIPS16_GOT16:
5946
0
    case R_MIPS_CALL16:
5947
0
    case R_MIPS_GOT16:
5948
0
    case R_MIPS_GOT_DISP:
5949
0
    case R_MIPS_GOT_HI16:
5950
0
    case R_MIPS_CALL_HI16:
5951
0
    case R_MIPS_GOT_LO16:
5952
0
    case R_MIPS_CALL_LO16:
5953
0
    case R_MICROMIPS_CALL16:
5954
0
    case R_MICROMIPS_GOT16:
5955
0
    case R_MICROMIPS_GOT_DISP:
5956
0
    case R_MICROMIPS_GOT_HI16:
5957
0
    case R_MICROMIPS_CALL_HI16:
5958
0
    case R_MICROMIPS_GOT_LO16:
5959
0
    case R_MICROMIPS_CALL_LO16:
5960
0
    case R_MIPS_TLS_GD:
5961
0
    case R_MIPS_TLS_GOTTPREL:
5962
0
    case R_MIPS_TLS_LDM:
5963
0
    case R_MIPS16_TLS_GD:
5964
0
    case R_MIPS16_TLS_GOTTPREL:
5965
0
    case R_MIPS16_TLS_LDM:
5966
0
    case R_MICROMIPS_TLS_GD:
5967
0
    case R_MICROMIPS_TLS_GOTTPREL:
5968
0
    case R_MICROMIPS_TLS_LDM:
5969
      /* Find the index into the GOT where this value is located.  */
5970
0
      if (tls_ldm_reloc_p (r_type))
5971
0
  {
5972
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
5973
0
          0, 0, NULL, r_type);
5974
0
    if (g == MINUS_ONE)
5975
0
      return bfd_reloc_outofrange;
5976
0
  }
5977
0
      else if (!local_p)
5978
0
  {
5979
    /* On VxWorks, CALL relocations should refer to the .got.plt
5980
       entry, which is initialized to point at the PLT stub.  */
5981
0
    if (htab->root.target_os == is_vxworks
5982
0
        && (call_hi16_reloc_p (r_type)
5983
0
      || call_lo16_reloc_p (r_type)
5984
0
      || call16_reloc_p (r_type)))
5985
0
      {
5986
0
        BFD_ASSERT (addend == 0);
5987
0
        BFD_ASSERT (h->root.needs_plt);
5988
0
        g = mips_elf_gotplt_index (info, &h->root);
5989
0
      }
5990
0
    else
5991
0
      {
5992
0
        BFD_ASSERT (addend == 0);
5993
0
        g = mips_elf_global_got_index (abfd, info, input_bfd,
5994
0
               &h->root, r_type);
5995
0
        if (!TLS_RELOC_P (r_type)
5996
0
      && !elf_hash_table (info)->dynamic_sections_created)
5997
    /* This is a static link.  We must initialize the GOT entry.  */
5998
0
    MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5999
0
      }
6000
0
  }
6001
0
      else if (htab->root.target_os != is_vxworks
6002
0
         && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6003
  /* The calculation below does not involve "g".  */
6004
0
  break;
6005
0
      else
6006
0
  {
6007
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
6008
0
          symbol + addend, r_symndx, h, r_type);
6009
0
    if (g == MINUS_ONE)
6010
0
      return bfd_reloc_outofrange;
6011
0
  }
6012
6013
      /* Convert GOT indices to actual offsets.  */
6014
0
      g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6015
0
      break;
6016
0
    }
6017
6018
  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6019
     symbols are resolved by the loader.  Add them to .rela.dyn.  */
6020
0
  if (h != NULL && is_gott_symbol (info, &h->root))
6021
0
    {
6022
0
      Elf_Internal_Rela outrel;
6023
0
      bfd_byte *loc;
6024
0
      asection *s;
6025
6026
0
      s = mips_elf_rel_dyn_section (info, false);
6027
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6028
6029
0
      outrel.r_offset = (input_section->output_section->vma
6030
0
       + input_section->output_offset
6031
0
       + relocation->r_offset);
6032
0
      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6033
0
      outrel.r_addend = addend;
6034
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6035
6036
      /* If we've written this relocation for a readonly section,
6037
   we need to set DF_TEXTREL again, so that we do not delete the
6038
   DT_TEXTREL tag.  */
6039
0
      if (MIPS_ELF_READONLY_SECTION (input_section))
6040
0
  info->flags |= DF_TEXTREL;
6041
6042
0
      *valuep = 0;
6043
0
      return bfd_reloc_ok;
6044
0
    }
6045
6046
  /* Figure out what kind of relocation is being performed.  */
6047
0
  switch (r_type)
6048
0
    {
6049
0
    case R_MIPS_NONE:
6050
0
      return bfd_reloc_continue;
6051
6052
0
    case R_MIPS_16:
6053
0
      if (howto->partial_inplace)
6054
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6055
0
      value = symbol + addend;
6056
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6057
0
      break;
6058
6059
0
    case R_MIPS_32:
6060
0
    case R_MIPS_REL32:
6061
0
    case R_MIPS_64:
6062
0
      if ((bfd_link_pic (info)
6063
0
     || (htab->root.dynamic_sections_created
6064
0
         && h != NULL
6065
0
         && h->root.def_dynamic
6066
0
         && !h->root.def_regular
6067
0
         && !h->has_static_relocs))
6068
0
    && r_symndx != STN_UNDEF
6069
0
    && (h == NULL
6070
0
        || h->root.root.type != bfd_link_hash_undefweak
6071
0
        || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6072
0
      && !resolved_to_zero))
6073
0
    && (input_section->flags & SEC_ALLOC) != 0)
6074
0
  {
6075
    /* If we're creating a shared library, then we can't know
6076
       where the symbol will end up.  So, we create a relocation
6077
       record in the output, and leave the job up to the dynamic
6078
       linker.  We must do the same for executable references to
6079
       shared library symbols, unless we've decided to use copy
6080
       relocs or PLTs instead.  */
6081
0
    value = addend;
6082
0
    if (!mips_elf_create_dynamic_relocation (abfd,
6083
0
               info,
6084
0
               relocation,
6085
0
               h,
6086
0
               sec,
6087
0
               symbol,
6088
0
               &value,
6089
0
               input_section))
6090
0
      return bfd_reloc_undefined;
6091
0
  }
6092
0
      else
6093
0
  {
6094
0
    if (r_type != R_MIPS_REL32)
6095
0
      value = symbol + addend;
6096
0
    else
6097
0
      value = addend;
6098
0
  }
6099
0
      value &= howto->dst_mask;
6100
0
      break;
6101
6102
0
    case R_MIPS_PC32:
6103
0
      value = symbol + addend - p;
6104
0
      value &= howto->dst_mask;
6105
0
      break;
6106
6107
0
    case R_MIPS16_26:
6108
      /* The calculation for R_MIPS16_26 is just the same as for an
6109
   R_MIPS_26.  It's only the storage of the relocated field into
6110
   the output file that's different.  That's handled in
6111
   mips_elf_perform_relocation.  So, we just fall through to the
6112
   R_MIPS_26 case here.  */
6113
0
    case R_MIPS_26:
6114
0
    case R_MICROMIPS_26_S1:
6115
0
      {
6116
0
  unsigned int shift;
6117
6118
  /* Shift is 2, unusually, for microMIPS JALX.  */
6119
0
  shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6120
6121
0
  if (howto->partial_inplace && !section_p)
6122
0
    value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6123
0
  else
6124
0
    value = addend;
6125
0
  value += symbol;
6126
6127
  /* Make sure the target of a jump is suitably aligned.  Bit 0 must
6128
     be the correct ISA mode selector except for weak undefined
6129
     symbols.  */
6130
0
  if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6131
0
      && (*cross_mode_jump_p
6132
0
    ? (value & 3) != (r_type == R_MIPS_26)
6133
0
    : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6134
0
    return bfd_reloc_outofrange;
6135
6136
0
  value >>= shift;
6137
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6138
0
    overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6139
0
  value &= howto->dst_mask;
6140
0
      }
6141
0
      break;
6142
6143
0
    case R_MIPS_TLS_DTPREL_HI16:
6144
0
    case R_MIPS16_TLS_DTPREL_HI16:
6145
0
    case R_MICROMIPS_TLS_DTPREL_HI16:
6146
0
      value = (mips_elf_high (addend + symbol - dtprel_base (info))
6147
0
         & howto->dst_mask);
6148
0
      break;
6149
6150
0
    case R_MIPS_TLS_DTPREL_LO16:
6151
0
    case R_MIPS_TLS_DTPREL32:
6152
0
    case R_MIPS_TLS_DTPREL64:
6153
0
    case R_MIPS16_TLS_DTPREL_LO16:
6154
0
    case R_MICROMIPS_TLS_DTPREL_LO16:
6155
0
      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6156
0
      break;
6157
6158
0
    case R_MIPS_TLS_TPREL_HI16:
6159
0
    case R_MIPS16_TLS_TPREL_HI16:
6160
0
    case R_MICROMIPS_TLS_TPREL_HI16:
6161
0
      value = (mips_elf_high (addend + symbol - tprel_base (info))
6162
0
         & howto->dst_mask);
6163
0
      break;
6164
6165
0
    case R_MIPS_TLS_TPREL_LO16:
6166
0
    case R_MIPS_TLS_TPREL32:
6167
0
    case R_MIPS_TLS_TPREL64:
6168
0
    case R_MIPS16_TLS_TPREL_LO16:
6169
0
    case R_MICROMIPS_TLS_TPREL_LO16:
6170
0
      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6171
0
      break;
6172
6173
0
    case R_MIPS_HI16:
6174
0
    case R_MIPS16_HI16:
6175
0
    case R_MICROMIPS_HI16:
6176
0
      if (!gp_disp_p)
6177
0
  {
6178
0
    value = mips_elf_high (addend + symbol);
6179
0
    value &= howto->dst_mask;
6180
0
  }
6181
0
      else
6182
0
  {
6183
    /* For MIPS16 ABI code we generate this sequence
6184
    0: li      $v0,%hi(_gp_disp)
6185
    4: addiupc $v1,%lo(_gp_disp)
6186
    8: sll     $v0,16
6187
         12: addu    $v0,$v1
6188
         14: move    $gp,$v0
6189
       So the offsets of hi and lo relocs are the same, but the
6190
       base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6191
       ADDIUPC clears the low two bits of the instruction address,
6192
       so the base is ($t9 + 4) & ~3.  */
6193
0
    if (r_type == R_MIPS16_HI16)
6194
0
      value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6195
    /* The microMIPS .cpload sequence uses the same assembly
6196
       instructions as the traditional psABI version, but the
6197
       incoming $t9 has the low bit set.  */
6198
0
    else if (r_type == R_MICROMIPS_HI16)
6199
0
      value = mips_elf_high (addend + gp - p - 1);
6200
0
    else
6201
0
      value = mips_elf_high (addend + gp - p);
6202
0
  }
6203
0
      break;
6204
6205
0
    case R_MIPS_LO16:
6206
0
    case R_MIPS16_LO16:
6207
0
    case R_MICROMIPS_LO16:
6208
0
    case R_MICROMIPS_HI0_LO16:
6209
0
      if (!gp_disp_p)
6210
0
  value = (symbol + addend) & howto->dst_mask;
6211
0
      else
6212
0
  {
6213
    /* See the comment for R_MIPS16_HI16 above for the reason
6214
       for this conditional.  */
6215
0
    if (r_type == R_MIPS16_LO16)
6216
0
      value = addend + gp - (p & ~(bfd_vma) 0x3);
6217
0
    else if (r_type == R_MICROMIPS_LO16
6218
0
       || r_type == R_MICROMIPS_HI0_LO16)
6219
0
      value = addend + gp - p + 3;
6220
0
    else
6221
0
      value = addend + gp - p + 4;
6222
    /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6223
       for overflow.  But, on, say, IRIX5, relocations against
6224
       _gp_disp are normally generated from the .cpload
6225
       pseudo-op.  It generates code that normally looks like
6226
       this:
6227
6228
         lui    $gp,%hi(_gp_disp)
6229
         addiu  $gp,$gp,%lo(_gp_disp)
6230
         addu   $gp,$gp,$t9
6231
6232
       Here $t9 holds the address of the function being called,
6233
       as required by the MIPS ELF ABI.  The R_MIPS_LO16
6234
       relocation can easily overflow in this situation, but the
6235
       R_MIPS_HI16 relocation will handle the overflow.
6236
       Therefore, we consider this a bug in the MIPS ABI, and do
6237
       not check for overflow here.  */
6238
0
  }
6239
0
      break;
6240
6241
0
    case R_MIPS_LITERAL:
6242
0
    case R_MICROMIPS_LITERAL:
6243
      /* Because we don't merge literal sections, we can handle this
6244
   just like R_MIPS_GPREL16.  In the long run, we should merge
6245
   shared literals, and then we will need to additional work
6246
   here.  */
6247
6248
      /* Fall through.  */
6249
6250
0
    case R_MIPS16_GPREL:
6251
      /* The R_MIPS16_GPREL performs the same calculation as
6252
   R_MIPS_GPREL16, but stores the relocated bits in a different
6253
   order.  We don't need to do anything special here; the
6254
   differences are handled in mips_elf_perform_relocation.  */
6255
0
    case R_MIPS_GPREL16:
6256
0
    case R_MICROMIPS_GPREL7_S2:
6257
0
    case R_MICROMIPS_GPREL16:
6258
      /* Only sign-extend the addend if it was extracted from the
6259
   instruction.  If the addend was separate, leave it alone,
6260
   otherwise we may lose significant bits.  */
6261
0
      if (howto->partial_inplace)
6262
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6263
0
      value = symbol + addend - gp;
6264
      /* If the symbol was local, any earlier relocatable links will
6265
   have adjusted its addend with the gp offset, so compensate
6266
   for that now.  Don't do it for symbols forced local in this
6267
   link, though, since they won't have had the gp offset applied
6268
   to them before.  */
6269
0
      if (was_local_p)
6270
0
  value += gp0;
6271
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6272
0
  overflowed_p = mips_elf_overflow_p (value, 16);
6273
0
      break;
6274
6275
0
    case R_MIPS16_GOT16:
6276
0
    case R_MIPS16_CALL16:
6277
0
    case R_MIPS_GOT16:
6278
0
    case R_MIPS_CALL16:
6279
0
    case R_MICROMIPS_GOT16:
6280
0
    case R_MICROMIPS_CALL16:
6281
      /* VxWorks does not have separate local and global semantics for
6282
   R_MIPS*_GOT16; every relocation evaluates to "G".  */
6283
0
      if (htab->root.target_os != is_vxworks && local_p)
6284
0
  {
6285
0
    value = mips_elf_got16_entry (abfd, input_bfd, info,
6286
0
          symbol + addend, !was_local_p);
6287
0
    if (value == MINUS_ONE)
6288
0
      return bfd_reloc_outofrange;
6289
0
    value
6290
0
      = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6291
0
    overflowed_p = mips_elf_overflow_p (value, 16);
6292
0
    break;
6293
0
  }
6294
6295
      /* Fall through.  */
6296
6297
0
    case R_MIPS_TLS_GD:
6298
0
    case R_MIPS_TLS_GOTTPREL:
6299
0
    case R_MIPS_TLS_LDM:
6300
0
    case R_MIPS_GOT_DISP:
6301
0
    case R_MIPS16_TLS_GD:
6302
0
    case R_MIPS16_TLS_GOTTPREL:
6303
0
    case R_MIPS16_TLS_LDM:
6304
0
    case R_MICROMIPS_TLS_GD:
6305
0
    case R_MICROMIPS_TLS_GOTTPREL:
6306
0
    case R_MICROMIPS_TLS_LDM:
6307
0
    case R_MICROMIPS_GOT_DISP:
6308
0
      value = g;
6309
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6310
0
      break;
6311
6312
0
    case R_MIPS_GPREL32:
6313
0
      value = (addend + symbol + gp0 - gp);
6314
0
      if (!save_addend)
6315
0
  value &= howto->dst_mask;
6316
0
      break;
6317
6318
0
    case R_MIPS_PC16:
6319
0
    case R_MIPS_GNU_REL16_S2:
6320
0
      if (howto->partial_inplace)
6321
0
  addend = _bfd_mips_elf_sign_extend (addend, 18);
6322
6323
      /* No need to exclude weak undefined symbols here as they resolve
6324
   to 0 and never set `*cross_mode_jump_p', so this alignment check
6325
   will never trigger for them.  */
6326
0
      if (*cross_mode_jump_p
6327
0
    ? ((symbol + addend) & 3) != 1
6328
0
    : ((symbol + addend) & 3) != 0)
6329
0
  return bfd_reloc_outofrange;
6330
6331
0
      value = symbol + addend - p;
6332
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6333
0
  overflowed_p = mips_elf_overflow_p (value, 18);
6334
0
      value >>= howto->rightshift;
6335
0
      value &= howto->dst_mask;
6336
0
      break;
6337
6338
0
    case R_MIPS16_PC16_S1:
6339
0
      if (howto->partial_inplace)
6340
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6341
6342
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6343
0
    && (*cross_mode_jump_p
6344
0
        ? ((symbol + addend) & 3) != 0
6345
0
        : ((symbol + addend) & 1) == 0))
6346
0
  return bfd_reloc_outofrange;
6347
6348
0
      value = symbol + addend - p;
6349
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6350
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6351
0
      value >>= howto->rightshift;
6352
0
      value &= howto->dst_mask;
6353
0
      break;
6354
6355
0
    case R_MIPS_PC21_S2:
6356
0
      if (howto->partial_inplace)
6357
0
  addend = _bfd_mips_elf_sign_extend (addend, 23);
6358
6359
0
      if ((symbol + addend) & 3)
6360
0
  return bfd_reloc_outofrange;
6361
6362
0
      value = symbol + addend - p;
6363
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6364
0
  overflowed_p = mips_elf_overflow_p (value, 23);
6365
0
      value >>= howto->rightshift;
6366
0
      value &= howto->dst_mask;
6367
0
      break;
6368
6369
0
    case R_MIPS_PC26_S2:
6370
0
      if (howto->partial_inplace)
6371
0
  addend = _bfd_mips_elf_sign_extend (addend, 28);
6372
6373
0
      if ((symbol + addend) & 3)
6374
0
  return bfd_reloc_outofrange;
6375
6376
0
      value = symbol + addend - p;
6377
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6378
0
  overflowed_p = mips_elf_overflow_p (value, 28);
6379
0
      value >>= howto->rightshift;
6380
0
      value &= howto->dst_mask;
6381
0
      break;
6382
6383
0
    case R_MIPS_PC18_S3:
6384
0
      if (howto->partial_inplace)
6385
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6386
6387
0
      if ((symbol + addend) & 7)
6388
0
  return bfd_reloc_outofrange;
6389
6390
0
      value = symbol + addend - ((p | 7) ^ 7);
6391
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6392
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6393
0
      value >>= howto->rightshift;
6394
0
      value &= howto->dst_mask;
6395
0
      break;
6396
6397
0
    case R_MIPS_PC19_S2:
6398
0
      if (howto->partial_inplace)
6399
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6400
6401
0
      if ((symbol + addend) & 3)
6402
0
  return bfd_reloc_outofrange;
6403
6404
0
      value = symbol + addend - p;
6405
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6406
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6407
0
      value >>= howto->rightshift;
6408
0
      value &= howto->dst_mask;
6409
0
      break;
6410
6411
0
    case R_MIPS_PCHI16:
6412
0
      value = mips_elf_high (symbol + addend - p);
6413
0
      value &= howto->dst_mask;
6414
0
      break;
6415
6416
0
    case R_MIPS_PCLO16:
6417
0
      if (howto->partial_inplace)
6418
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6419
0
      value = symbol + addend - p;
6420
0
      value &= howto->dst_mask;
6421
0
      break;
6422
6423
0
    case R_MICROMIPS_PC7_S1:
6424
0
      if (howto->partial_inplace)
6425
0
  addend = _bfd_mips_elf_sign_extend (addend, 8);
6426
6427
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6428
0
    && (*cross_mode_jump_p
6429
0
        ? ((symbol + addend + 2) & 3) != 0
6430
0
        : ((symbol + addend + 2) & 1) == 0))
6431
0
  return bfd_reloc_outofrange;
6432
6433
0
      value = symbol + addend - p;
6434
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6435
0
  overflowed_p = mips_elf_overflow_p (value, 8);
6436
0
      value >>= howto->rightshift;
6437
0
      value &= howto->dst_mask;
6438
0
      break;
6439
6440
0
    case R_MICROMIPS_PC10_S1:
6441
0
      if (howto->partial_inplace)
6442
0
  addend = _bfd_mips_elf_sign_extend (addend, 11);
6443
6444
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6445
0
    && (*cross_mode_jump_p
6446
0
        ? ((symbol + addend + 2) & 3) != 0
6447
0
        : ((symbol + addend + 2) & 1) == 0))
6448
0
  return bfd_reloc_outofrange;
6449
6450
0
      value = symbol + addend - p;
6451
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6452
0
  overflowed_p = mips_elf_overflow_p (value, 11);
6453
0
      value >>= howto->rightshift;
6454
0
      value &= howto->dst_mask;
6455
0
      break;
6456
6457
0
    case R_MICROMIPS_PC16_S1:
6458
0
      if (howto->partial_inplace)
6459
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6460
6461
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6462
0
    && (*cross_mode_jump_p
6463
0
        ? ((symbol + addend) & 3) != 0
6464
0
        : ((symbol + addend) & 1) == 0))
6465
0
  return bfd_reloc_outofrange;
6466
6467
0
      value = symbol + addend - p;
6468
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6469
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6470
0
      value >>= howto->rightshift;
6471
0
      value &= howto->dst_mask;
6472
0
      break;
6473
6474
0
    case R_MICROMIPS_PC23_S2:
6475
0
      if (howto->partial_inplace)
6476
0
  addend = _bfd_mips_elf_sign_extend (addend, 25);
6477
0
      value = symbol + addend - ((p | 3) ^ 3);
6478
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6479
0
  overflowed_p = mips_elf_overflow_p (value, 25);
6480
0
      value >>= howto->rightshift;
6481
0
      value &= howto->dst_mask;
6482
0
      break;
6483
6484
0
    case R_MIPS_GOT_HI16:
6485
0
    case R_MIPS_CALL_HI16:
6486
0
    case R_MICROMIPS_GOT_HI16:
6487
0
    case R_MICROMIPS_CALL_HI16:
6488
      /* We're allowed to handle these two relocations identically.
6489
   The dynamic linker is allowed to handle the CALL relocations
6490
   differently by creating a lazy evaluation stub.  */
6491
0
      value = g;
6492
0
      value = mips_elf_high (value);
6493
0
      value &= howto->dst_mask;
6494
0
      break;
6495
6496
0
    case R_MIPS_GOT_LO16:
6497
0
    case R_MIPS_CALL_LO16:
6498
0
    case R_MICROMIPS_GOT_LO16:
6499
0
    case R_MICROMIPS_CALL_LO16:
6500
0
      value = g & howto->dst_mask;
6501
0
      break;
6502
6503
0
    case R_MIPS_GOT_PAGE:
6504
0
    case R_MICROMIPS_GOT_PAGE:
6505
0
      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6506
0
      if (value == MINUS_ONE)
6507
0
  return bfd_reloc_outofrange;
6508
0
      value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6509
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6510
0
      break;
6511
6512
0
    case R_MIPS_GOT_OFST:
6513
0
    case R_MICROMIPS_GOT_OFST:
6514
0
      if (local_p)
6515
0
  mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6516
0
      else
6517
0
  value = addend;
6518
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6519
0
      break;
6520
6521
0
    case R_MIPS_SUB:
6522
0
    case R_MICROMIPS_SUB:
6523
0
      value = symbol - addend;
6524
0
      value &= howto->dst_mask;
6525
0
      break;
6526
6527
0
    case R_MIPS_HIGHER:
6528
0
    case R_MICROMIPS_HIGHER:
6529
0
      value = mips_elf_higher (addend + symbol);
6530
0
      value &= howto->dst_mask;
6531
0
      break;
6532
6533
0
    case R_MIPS_HIGHEST:
6534
0
    case R_MICROMIPS_HIGHEST:
6535
0
      value = mips_elf_highest (addend + symbol);
6536
0
      value &= howto->dst_mask;
6537
0
      break;
6538
6539
0
    case R_MIPS_SCN_DISP:
6540
0
    case R_MICROMIPS_SCN_DISP:
6541
0
      value = symbol + addend - sec->output_offset;
6542
0
      value &= howto->dst_mask;
6543
0
      break;
6544
6545
0
    case R_MIPS_JALR:
6546
0
    case R_MICROMIPS_JALR:
6547
      /* This relocation is only a hint.  In some cases, we optimize
6548
   it into a bal instruction.  But we don't try to optimize
6549
   when the symbol does not resolve locally.  */
6550
0
      if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6551
0
  return bfd_reloc_continue;
6552
      /* We can't optimize cross-mode jumps either.  */
6553
0
      if (*cross_mode_jump_p)
6554
0
  return bfd_reloc_continue;
6555
0
      value = symbol + addend;
6556
      /* Neither we can non-instruction-aligned targets.  */
6557
0
      if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6558
0
  return bfd_reloc_continue;
6559
0
      break;
6560
6561
0
    case R_MIPS_PJUMP:
6562
0
    case R_MIPS_GNU_VTINHERIT:
6563
0
    case R_MIPS_GNU_VTENTRY:
6564
      /* We don't do anything with these at present.  */
6565
0
      return bfd_reloc_continue;
6566
6567
0
    default:
6568
      /* An unrecognized relocation type.  */
6569
0
      return bfd_reloc_notsupported;
6570
0
    }
6571
6572
  /* Store the VALUE for our caller.  */
6573
0
  *valuep = value;
6574
0
  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6575
0
}
6576
6577
/* It has been determined that the result of the RELOCATION is the
6578
   VALUE.  Use HOWTO to place VALUE into the output file at the
6579
   appropriate position.  The SECTION is the section to which the
6580
   relocation applies.
6581
   CROSS_MODE_JUMP_P is true if the relocation field
6582
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6583
6584
   Returns FALSE if anything goes wrong.  */
6585
6586
static bool
6587
mips_elf_perform_relocation (struct bfd_link_info *info,
6588
           reloc_howto_type *howto,
6589
           const Elf_Internal_Rela *relocation,
6590
           bfd_vma value, bfd *input_bfd,
6591
           asection *input_section, bfd_byte *contents,
6592
           bool cross_mode_jump_p)
6593
0
{
6594
0
  bfd_vma x;
6595
0
  bfd_byte *location;
6596
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6597
6598
  /* Figure out where the relocation is occurring.  */
6599
0
  location = contents + relocation->r_offset;
6600
6601
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6602
6603
  /* Obtain the current value.  */
6604
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6605
6606
  /* Clear the field we are setting.  */
6607
0
  x &= ~howto->dst_mask;
6608
6609
  /* Set the field.  */
6610
0
  x |= (value & howto->dst_mask);
6611
6612
  /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
6613
0
  if (!cross_mode_jump_p && jal_reloc_p (r_type))
6614
0
    {
6615
0
      bfd_vma opcode = x >> 26;
6616
6617
0
      if (r_type == R_MIPS16_26 ? opcode == 0x7
6618
0
    : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6619
0
    : opcode == 0x1d)
6620
0
  {
6621
0
    info->callbacks->einfo
6622
0
      (_("%X%H: unsupported JALX to the same ISA mode\n"),
6623
0
       input_bfd, input_section, relocation->r_offset);
6624
0
    return true;
6625
0
  }
6626
0
    }
6627
0
  if (cross_mode_jump_p && jal_reloc_p (r_type))
6628
0
    {
6629
0
      bool ok;
6630
0
      bfd_vma opcode = x >> 26;
6631
0
      bfd_vma jalx_opcode;
6632
6633
      /* Check to see if the opcode is already JAL or JALX.  */
6634
0
      if (r_type == R_MIPS16_26)
6635
0
  {
6636
0
    ok = ((opcode == 0x6) || (opcode == 0x7));
6637
0
    jalx_opcode = 0x7;
6638
0
  }
6639
0
      else if (r_type == R_MICROMIPS_26_S1)
6640
0
  {
6641
0
    ok = ((opcode == 0x3d) || (opcode == 0x3c));
6642
0
    jalx_opcode = 0x3c;
6643
0
  }
6644
0
      else
6645
0
  {
6646
0
    ok = ((opcode == 0x3) || (opcode == 0x1d));
6647
0
    jalx_opcode = 0x1d;
6648
0
  }
6649
6650
      /* If the opcode is not JAL or JALX, there's a problem.  We cannot
6651
   convert J or JALS to JALX.  */
6652
0
      if (!ok)
6653
0
  {
6654
0
    info->callbacks->einfo
6655
0
      (_("%X%H: unsupported jump between ISA modes; "
6656
0
         "consider recompiling with interlinking enabled\n"),
6657
0
       input_bfd, input_section, relocation->r_offset);
6658
0
    return true;
6659
0
  }
6660
6661
      /* Make this the JALX opcode.  */
6662
0
      x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6663
0
    }
6664
0
  else if (cross_mode_jump_p && b_reloc_p (r_type))
6665
0
    {
6666
0
      bool ok = false;
6667
0
      bfd_vma opcode = x >> 16;
6668
0
      bfd_vma jalx_opcode = 0;
6669
0
      bfd_vma sign_bit = 0;
6670
0
      bfd_vma addr;
6671
0
      bfd_vma dest;
6672
6673
0
      if (r_type == R_MICROMIPS_PC16_S1)
6674
0
  {
6675
0
    ok = opcode == 0x4060;
6676
0
    jalx_opcode = 0x3c;
6677
0
    sign_bit = 0x10000;
6678
0
    value <<= 1;
6679
0
  }
6680
0
      else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6681
0
  {
6682
0
    ok = opcode == 0x411;
6683
0
    jalx_opcode = 0x1d;
6684
0
    sign_bit = 0x20000;
6685
0
    value <<= 2;
6686
0
  }
6687
6688
0
      if (ok && !bfd_link_pic (info))
6689
0
  {
6690
0
    addr = (input_section->output_section->vma
6691
0
      + input_section->output_offset
6692
0
      + relocation->r_offset
6693
0
      + 4);
6694
0
    dest = (addr
6695
0
      + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6696
6697
0
    if ((addr >> 28) << 28 != (dest >> 28) << 28)
6698
0
      {
6699
0
        info->callbacks->einfo
6700
0
    (_("%X%H: cannot convert branch between ISA modes "
6701
0
       "to JALX: relocation out of range\n"),
6702
0
     input_bfd, input_section, relocation->r_offset);
6703
0
        return true;
6704
0
      }
6705
6706
    /* Make this the JALX opcode.  */
6707
0
    x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6708
0
  }
6709
0
      else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6710
0
  {
6711
0
    info->callbacks->einfo
6712
0
      (_("%X%H: unsupported branch between ISA modes\n"),
6713
0
       input_bfd, input_section, relocation->r_offset);
6714
0
    return true;
6715
0
  }
6716
0
    }
6717
6718
  /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6719
     range.  */
6720
0
  if (!bfd_link_relocatable (info)
6721
0
      && !cross_mode_jump_p
6722
0
      && ((JAL_TO_BAL_P (input_bfd)
6723
0
     && r_type == R_MIPS_26
6724
0
     && (x >> 26) == 0x3)     /* jal addr */
6725
0
    || (JALR_TO_BAL_P (input_bfd)
6726
0
        && r_type == R_MIPS_JALR
6727
0
        && x == 0x0320f809)   /* jalr t9 */
6728
0
    || (JR_TO_B_P (input_bfd)
6729
0
        && r_type == R_MIPS_JALR
6730
0
        && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6731
0
    {
6732
0
      bfd_vma addr;
6733
0
      bfd_vma dest;
6734
0
      bfd_signed_vma off;
6735
6736
0
      addr = (input_section->output_section->vma
6737
0
        + input_section->output_offset
6738
0
        + relocation->r_offset
6739
0
        + 4);
6740
0
      if (r_type == R_MIPS_26)
6741
0
  dest = (value << 2) | ((addr >> 28) << 28);
6742
0
      else
6743
0
  dest = value;
6744
0
      off = dest - addr;
6745
0
      if (off <= 0x1ffff && off >= -0x20000)
6746
0
  {
6747
0
    if ((x & ~1) == 0x03200008)   /* jr t9 / jalr zero, t9 */
6748
0
      x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
6749
0
    else
6750
0
      x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
6751
0
  }
6752
0
    }
6753
6754
  /* Put the value into the output.  */
6755
0
  mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6756
6757
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6758
0
             location);
6759
6760
0
  return true;
6761
0
}
6762

6763
/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
6764
   is the original relocation, which is now being transformed into a
6765
   dynamic relocation.  The ADDENDP is adjusted if necessary; the
6766
   caller should store the result in place of the original addend.  */
6767
6768
static bool
6769
mips_elf_create_dynamic_relocation (bfd *output_bfd,
6770
            struct bfd_link_info *info,
6771
            const Elf_Internal_Rela *rel,
6772
            struct mips_elf_link_hash_entry *h,
6773
            asection *sec, bfd_vma symbol,
6774
            bfd_vma *addendp, asection *input_section)
6775
0
{
6776
0
  Elf_Internal_Rela outrel[3];
6777
0
  asection *sreloc;
6778
0
  bfd *dynobj;
6779
0
  int r_type;
6780
0
  long indx;
6781
0
  bool defined_p;
6782
0
  struct mips_elf_link_hash_table *htab;
6783
6784
0
  htab = mips_elf_hash_table (info);
6785
0
  BFD_ASSERT (htab != NULL);
6786
6787
0
  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6788
0
  dynobj = elf_hash_table (info)->dynobj;
6789
0
  sreloc = mips_elf_rel_dyn_section (info, false);
6790
0
  BFD_ASSERT (sreloc != NULL);
6791
0
  BFD_ASSERT (sreloc->contents != NULL);
6792
0
  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6793
0
        < sreloc->size);
6794
6795
0
  outrel[0].r_offset =
6796
0
    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6797
0
  if (ABI_64_P (output_bfd))
6798
0
    {
6799
0
      outrel[1].r_offset =
6800
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6801
0
      outrel[2].r_offset =
6802
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6803
0
    }
6804
6805
0
  if (outrel[0].r_offset == MINUS_ONE)
6806
    /* The relocation field has been deleted.  */
6807
0
    return true;
6808
6809
0
  if (outrel[0].r_offset == MINUS_TWO)
6810
0
    {
6811
      /* The relocation field has been converted into a relative value of
6812
   some sort.  Functions like _bfd_elf_write_section_eh_frame expect
6813
   the field to be fully relocated, so add in the symbol's value.  */
6814
0
      *addendp += symbol;
6815
0
      return true;
6816
0
    }
6817
6818
  /* We must now calculate the dynamic symbol table index to use
6819
     in the relocation.  */
6820
0
  if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6821
0
    {
6822
0
      BFD_ASSERT (htab->root.target_os == is_vxworks
6823
0
      || h->global_got_area != GGA_NONE);
6824
0
      indx = h->root.dynindx;
6825
0
      if (SGI_COMPAT (output_bfd))
6826
0
  defined_p = h->root.def_regular;
6827
0
      else
6828
  /* ??? glibc's ld.so just adds the final GOT entry to the
6829
     relocation field.  It therefore treats relocs against
6830
     defined symbols in the same way as relocs against
6831
     undefined symbols.  */
6832
0
  defined_p = false;
6833
0
    }
6834
0
  else
6835
0
    {
6836
0
      if (sec != NULL && bfd_is_abs_section (sec))
6837
0
  indx = 0;
6838
0
      else if (sec == NULL || sec->owner == NULL)
6839
0
  {
6840
0
    bfd_set_error (bfd_error_bad_value);
6841
0
    return false;
6842
0
  }
6843
0
      else
6844
0
  {
6845
0
    indx = elf_section_data (sec->output_section)->dynindx;
6846
0
    if (indx == 0)
6847
0
      {
6848
0
        asection *osec = htab->root.text_index_section;
6849
0
        indx = elf_section_data (osec)->dynindx;
6850
0
      }
6851
0
    if (indx == 0)
6852
0
      abort ();
6853
0
  }
6854
6855
      /* Instead of generating a relocation using the section
6856
   symbol, we may as well make it a fully relative
6857
   relocation.  We want to avoid generating relocations to
6858
   local symbols because we used to generate them
6859
   incorrectly, without adding the original symbol value,
6860
   which is mandated by the ABI for section symbols.  In
6861
   order to give dynamic loaders and applications time to
6862
   phase out the incorrect use, we refrain from emitting
6863
   section-relative relocations.  It's not like they're
6864
   useful, after all.  This should be a bit more efficient
6865
   as well.  */
6866
      /* ??? Although this behavior is compatible with glibc's ld.so,
6867
   the ABI says that relocations against STN_UNDEF should have
6868
   a symbol value of 0.  Irix rld honors this, so relocations
6869
   against STN_UNDEF have no effect.  */
6870
0
      if (!SGI_COMPAT (output_bfd))
6871
0
  indx = 0;
6872
0
      defined_p = true;
6873
0
    }
6874
6875
  /* If the relocation was previously an absolute relocation and
6876
     this symbol will not be referred to by the relocation, we must
6877
     adjust it by the value we give it in the dynamic symbol table.
6878
     Otherwise leave the job up to the dynamic linker.  */
6879
0
  if (defined_p && r_type != R_MIPS_REL32)
6880
0
    *addendp += symbol;
6881
6882
0
  if (htab->root.target_os == is_vxworks)
6883
    /* VxWorks uses non-relative relocations for this.  */
6884
0
    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6885
0
  else
6886
    /* The relocation is always an REL32 relocation because we don't
6887
       know where the shared library will wind up at load-time.  */
6888
0
    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6889
0
           R_MIPS_REL32);
6890
6891
  /* For strict adherence to the ABI specification, we should
6892
     generate a R_MIPS_64 relocation record by itself before the
6893
     _REL32/_64 record as well, such that the addend is read in as
6894
     a 64-bit value (REL32 is a 32-bit relocation, after all).
6895
     However, since none of the existing ELF64 MIPS dynamic
6896
     loaders seems to care, we don't waste space with these
6897
     artificial relocations.  If this turns out to not be true,
6898
     mips_elf_allocate_dynamic_relocation() should be tweaked so
6899
     as to make room for a pair of dynamic relocations per
6900
     invocation if ABI_64_P, and here we should generate an
6901
     additional relocation record with R_MIPS_64 by itself for a
6902
     NULL symbol before this relocation record.  */
6903
0
  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6904
0
         ABI_64_P (output_bfd)
6905
0
         ? R_MIPS_64
6906
0
         : R_MIPS_NONE);
6907
0
  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6908
6909
  /* Adjust the output offset of the relocation to reference the
6910
     correct location in the output file.  */
6911
0
  outrel[0].r_offset += (input_section->output_section->vma
6912
0
       + input_section->output_offset);
6913
0
  outrel[1].r_offset += (input_section->output_section->vma
6914
0
       + input_section->output_offset);
6915
0
  outrel[2].r_offset += (input_section->output_section->vma
6916
0
       + input_section->output_offset);
6917
6918
  /* Put the relocation back out.  We have to use the special
6919
     relocation outputter in the 64-bit case since the 64-bit
6920
     relocation format is non-standard.  */
6921
0
  if (ABI_64_P (output_bfd))
6922
0
    {
6923
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6924
0
  (output_bfd, &outrel[0],
6925
0
   (sreloc->contents
6926
0
    + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6927
0
    }
6928
0
  else if (htab->root.target_os == is_vxworks)
6929
0
    {
6930
      /* VxWorks uses RELA rather than REL dynamic relocations.  */
6931
0
      outrel[0].r_addend = *addendp;
6932
0
      bfd_elf32_swap_reloca_out
6933
0
  (output_bfd, &outrel[0],
6934
0
   (sreloc->contents
6935
0
    + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6936
0
    }
6937
0
  else
6938
0
    bfd_elf32_swap_reloc_out
6939
0
      (output_bfd, &outrel[0],
6940
0
       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6941
6942
  /* We've now added another relocation.  */
6943
0
  ++sreloc->reloc_count;
6944
6945
  /* Make sure the output section is writable.  The dynamic linker
6946
     will be writing to it.  */
6947
0
  elf_section_data (input_section->output_section)->this_hdr.sh_flags
6948
0
    |= SHF_WRITE;
6949
6950
  /* On IRIX5, make an entry of compact relocation info.  */
6951
0
  if (IRIX_COMPAT (output_bfd) == ict_irix5)
6952
0
    {
6953
0
      asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6954
0
      bfd_byte *cr;
6955
6956
0
      if (scpt)
6957
0
  {
6958
0
    Elf32_crinfo cptrel;
6959
6960
0
    mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6961
0
    cptrel.vaddr = (rel->r_offset
6962
0
        + input_section->output_section->vma
6963
0
        + input_section->output_offset);
6964
0
    if (r_type == R_MIPS_REL32)
6965
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6966
0
    else
6967
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6968
0
    mips_elf_set_cr_dist2to (cptrel, 0);
6969
0
    cptrel.konst = *addendp;
6970
6971
0
    cr = (scpt->contents
6972
0
    + sizeof (Elf32_External_compact_rel));
6973
0
    mips_elf_set_cr_relvaddr (cptrel, 0);
6974
0
    bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6975
0
             ((Elf32_External_crinfo *) cr
6976
0
              + scpt->reloc_count));
6977
0
    ++scpt->reloc_count;
6978
0
  }
6979
0
    }
6980
6981
  /* If we've written this relocation for a readonly section,
6982
     we need to set DF_TEXTREL again, so that we do not delete the
6983
     DT_TEXTREL tag.  */
6984
0
  if (MIPS_ELF_READONLY_SECTION (input_section))
6985
0
    info->flags |= DF_TEXTREL;
6986
6987
0
  return true;
6988
0
}
6989

6990
/* Return the MACH for a MIPS e_flags value.  */
6991
6992
unsigned long
6993
_bfd_elf_mips_mach (flagword flags)
6994
0
{
6995
0
  switch (flags & EF_MIPS_MACH)
6996
0
    {
6997
0
    case E_MIPS_MACH_3900:
6998
0
      return bfd_mach_mips3900;
6999
7000
0
    case E_MIPS_MACH_4010:
7001
0
      return bfd_mach_mips4010;
7002
7003
0
    case E_MIPS_MACH_ALLEGREX:
7004
0
      return bfd_mach_mips_allegrex;
7005
7006
0
    case E_MIPS_MACH_4100:
7007
0
      return bfd_mach_mips4100;
7008
7009
0
    case E_MIPS_MACH_4111:
7010
0
      return bfd_mach_mips4111;
7011
7012
0
    case E_MIPS_MACH_4120:
7013
0
      return bfd_mach_mips4120;
7014
7015
0
    case E_MIPS_MACH_4650:
7016
0
      return bfd_mach_mips4650;
7017
7018
0
    case E_MIPS_MACH_5400:
7019
0
      return bfd_mach_mips5400;
7020
7021
0
    case E_MIPS_MACH_5500:
7022
0
      return bfd_mach_mips5500;
7023
7024
0
    case E_MIPS_MACH_5900:
7025
0
      return bfd_mach_mips5900;
7026
7027
0
    case E_MIPS_MACH_9000:
7028
0
      return bfd_mach_mips9000;
7029
7030
0
    case E_MIPS_MACH_SB1:
7031
0
      return bfd_mach_mips_sb1;
7032
7033
0
    case E_MIPS_MACH_LS2E:
7034
0
      return bfd_mach_mips_loongson_2e;
7035
7036
0
    case E_MIPS_MACH_LS2F:
7037
0
      return bfd_mach_mips_loongson_2f;
7038
7039
0
    case E_MIPS_MACH_GS464:
7040
0
      return bfd_mach_mips_gs464;
7041
7042
0
    case E_MIPS_MACH_GS464E:
7043
0
      return bfd_mach_mips_gs464e;
7044
7045
0
    case E_MIPS_MACH_GS264E:
7046
0
      return bfd_mach_mips_gs264e;
7047
7048
0
    case E_MIPS_MACH_OCTEON3:
7049
0
      return bfd_mach_mips_octeon3;
7050
7051
0
    case E_MIPS_MACH_OCTEON2:
7052
0
      return bfd_mach_mips_octeon2;
7053
7054
0
    case E_MIPS_MACH_OCTEON:
7055
0
      return bfd_mach_mips_octeon;
7056
7057
0
    case E_MIPS_MACH_XLR:
7058
0
      return bfd_mach_mips_xlr;
7059
7060
0
    case E_MIPS_MACH_IAMR2:
7061
0
      return bfd_mach_mips_interaptiv_mr2;
7062
7063
0
    default:
7064
0
      switch (flags & EF_MIPS_ARCH)
7065
0
  {
7066
0
  default:
7067
0
  case E_MIPS_ARCH_1:
7068
0
    return bfd_mach_mips3000;
7069
7070
0
  case E_MIPS_ARCH_2:
7071
0
    return bfd_mach_mips6000;
7072
7073
0
  case E_MIPS_ARCH_3:
7074
0
    return bfd_mach_mips4000;
7075
7076
0
  case E_MIPS_ARCH_4:
7077
0
    return bfd_mach_mips8000;
7078
7079
0
  case E_MIPS_ARCH_5:
7080
0
    return bfd_mach_mips5;
7081
7082
0
  case E_MIPS_ARCH_32:
7083
0
    return bfd_mach_mipsisa32;
7084
7085
0
  case E_MIPS_ARCH_64:
7086
0
    return bfd_mach_mipsisa64;
7087
7088
0
  case E_MIPS_ARCH_32R2:
7089
0
    return bfd_mach_mipsisa32r2;
7090
7091
0
  case E_MIPS_ARCH_64R2:
7092
0
    return bfd_mach_mipsisa64r2;
7093
7094
0
  case E_MIPS_ARCH_32R6:
7095
0
    return bfd_mach_mipsisa32r6;
7096
7097
0
  case E_MIPS_ARCH_64R6:
7098
0
    return bfd_mach_mipsisa64r6;
7099
0
  }
7100
0
    }
7101
7102
0
  return 0;
7103
0
}
7104
7105
/* Return printable name for ABI.  */
7106
7107
static inline char *
7108
elf_mips_abi_name (bfd *abfd)
7109
0
{
7110
0
  flagword flags;
7111
7112
0
  flags = elf_elfheader (abfd)->e_flags;
7113
0
  switch (flags & EF_MIPS_ABI)
7114
0
    {
7115
0
    case 0:
7116
0
      if (ABI_N32_P (abfd))
7117
0
  return "N32";
7118
0
      else if (ABI_64_P (abfd))
7119
0
  return "64";
7120
0
      else
7121
0
  return "none";
7122
0
    case E_MIPS_ABI_O32:
7123
0
      return "O32";
7124
0
    case E_MIPS_ABI_O64:
7125
0
      return "O64";
7126
0
    case E_MIPS_ABI_EABI32:
7127
0
      return "EABI32";
7128
0
    case E_MIPS_ABI_EABI64:
7129
0
      return "EABI64";
7130
0
    default:
7131
0
      return "unknown abi";
7132
0
    }
7133
0
}
7134

7135
/* MIPS ELF uses two common sections.  One is the usual one, and the
7136
   other is for small objects.  All the small objects are kept
7137
   together, and then referenced via the gp pointer, which yields
7138
   faster assembler code.  This is what we use for the small common
7139
   section.  This approach is copied from ecoff.c.  */
7140
static asection mips_elf_scom_section;
7141
static const asymbol mips_elf_scom_symbol =
7142
  GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7143
static asection mips_elf_scom_section =
7144
  BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7145
        ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7146
7147
/* MIPS ELF also uses an acommon section, which represents an
7148
   allocated common symbol which may be overridden by a
7149
   definition in a shared library.  */
7150
static asection mips_elf_acom_section;
7151
static const asymbol mips_elf_acom_symbol =
7152
  GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7153
static asection mips_elf_acom_section =
7154
  BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7155
        ".acommon", 0, SEC_ALLOC);
7156
7157
/* This is used for both the 32-bit and the 64-bit ABI.  */
7158
7159
void
7160
_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7161
0
{
7162
0
  elf_symbol_type *elfsym;
7163
7164
  /* Handle the special MIPS section numbers that a symbol may use.  */
7165
0
  elfsym = (elf_symbol_type *) asym;
7166
0
  switch (elfsym->internal_elf_sym.st_shndx)
7167
0
    {
7168
0
    case SHN_MIPS_ACOMMON:
7169
      /* This section is used in a dynamically linked executable file.
7170
   It is an allocated common section.  The dynamic linker can
7171
   either resolve these symbols to something in a shared
7172
   library, or it can just leave them here.  For our purposes,
7173
   we can consider these symbols to be in a new section.  */
7174
0
      asym->section = &mips_elf_acom_section;
7175
0
      break;
7176
7177
0
    case SHN_COMMON:
7178
      /* Common symbols less than the GP size are automatically
7179
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7180
0
      if (asym->value > elf_gp_size (abfd)
7181
0
    || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7182
0
    || IRIX_COMPAT (abfd) == ict_irix6
7183
0
    || strcmp (asym->name, "__gnu_lto_slim") == 0)
7184
0
  break;
7185
      /* Fall through.  */
7186
0
    case SHN_MIPS_SCOMMON:
7187
0
      asym->section = &mips_elf_scom_section;
7188
0
      asym->value = elfsym->internal_elf_sym.st_size;
7189
0
      break;
7190
7191
0
    case SHN_MIPS_SUNDEFINED:
7192
0
      asym->section = bfd_und_section_ptr;
7193
0
      break;
7194
7195
0
    case SHN_MIPS_TEXT:
7196
0
      {
7197
0
  asection *section = bfd_get_section_by_name (abfd, ".text");
7198
7199
0
  if (section != NULL)
7200
0
    {
7201
0
      asym->section = section;
7202
      /* MIPS_TEXT is a bit special, the address is not an offset
7203
         to the base of the .text section.  So subtract the section
7204
         base address to make it an offset.  */
7205
0
      asym->value -= section->vma;
7206
0
    }
7207
0
      }
7208
0
      break;
7209
7210
0
    case SHN_MIPS_DATA:
7211
0
      {
7212
0
  asection *section = bfd_get_section_by_name (abfd, ".data");
7213
7214
0
  if (section != NULL)
7215
0
    {
7216
0
      asym->section = section;
7217
      /* MIPS_DATA is a bit special, the address is not an offset
7218
         to the base of the .data section.  So subtract the section
7219
         base address to make it an offset.  */
7220
0
      asym->value -= section->vma;
7221
0
    }
7222
0
      }
7223
0
      break;
7224
0
    }
7225
7226
  /* If this is an odd-valued function symbol, assume it's a MIPS16
7227
     or microMIPS one.  */
7228
0
  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7229
0
      && (asym->value & 1) != 0)
7230
0
    {
7231
0
      asym->value--;
7232
0
      if (MICROMIPS_P (abfd))
7233
0
  elfsym->internal_elf_sym.st_other
7234
0
    = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7235
0
      else
7236
0
  elfsym->internal_elf_sym.st_other
7237
0
    = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7238
0
    }
7239
0
}
7240

7241
/* Implement elf_backend_eh_frame_address_size.  This differs from
7242
   the default in the way it handles EABI64.
7243
7244
   EABI64 was originally specified as an LP64 ABI, and that is what
7245
   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
7246
   historically accepted the combination of -mabi=eabi and -mlong32,
7247
   and this ILP32 variation has become semi-official over time.
7248
   Both forms use elf32 and have pointer-sized FDE addresses.
7249
7250
   If an EABI object was generated by GCC 4.0 or above, it will have
7251
   an empty .gcc_compiled_longXX section, where XX is the size of longs
7252
   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
7253
   have no special marking to distinguish them from LP64 objects.
7254
7255
   We don't want users of the official LP64 ABI to be punished for the
7256
   existence of the ILP32 variant, but at the same time, we don't want
7257
   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7258
   We therefore take the following approach:
7259
7260
      - If ABFD contains a .gcc_compiled_longXX section, use it to
7261
  determine the pointer size.
7262
7263
      - Otherwise check the type of the first relocation.  Assume that
7264
  the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7265
7266
      - Otherwise punt.
7267
7268
   The second check is enough to detect LP64 objects generated by pre-4.0
7269
   compilers because, in the kind of output generated by those compilers,
7270
   the first relocation will be associated with either a CIE personality
7271
   routine or an FDE start address.  Furthermore, the compilers never
7272
   used a special (non-pointer) encoding for this ABI.
7273
7274
   Checking the relocation type should also be safe because there is no
7275
   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
7276
   did so.  */
7277
7278
unsigned int
7279
_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7280
0
{
7281
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7282
0
    return 8;
7283
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7284
0
    {
7285
0
      bool long32_p, long64_p;
7286
7287
0
      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7288
0
      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7289
0
      if (long32_p && long64_p)
7290
0
  return 0;
7291
0
      if (long32_p)
7292
0
  return 4;
7293
0
      if (long64_p)
7294
0
  return 8;
7295
7296
0
      if (sec->reloc_count > 0
7297
0
    && elf_section_data (sec)->relocs != NULL
7298
0
    && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7299
0
        == R_MIPS_64))
7300
0
  return 8;
7301
7302
0
      return 0;
7303
0
    }
7304
0
  return 4;
7305
0
}
7306

7307
/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7308
   relocations against two unnamed section symbols to resolve to the
7309
   same address.  For example, if we have code like:
7310
7311
  lw  $4,%got_disp(.data)($gp)
7312
  lw  $25,%got_disp(.text)($gp)
7313
  jalr  $25
7314
7315
   then the linker will resolve both relocations to .data and the program
7316
   will jump there rather than to .text.
7317
7318
   We can work around this problem by giving names to local section symbols.
7319
   This is also what the MIPSpro tools do.  */
7320
7321
bool
7322
_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7323
0
{
7324
0
  return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7325
0
}
7326

7327
/* Work over a section just before writing it out.  This routine is
7328
   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
7329
   sections that need the SHF_MIPS_GPREL flag by name; there has to be
7330
   a better way.  */
7331
7332
bool
7333
_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7334
0
{
7335
0
  if (hdr->sh_type == SHT_MIPS_REGINFO
7336
0
      && hdr->sh_size > 0)
7337
0
    {
7338
0
      bfd_byte buf[4];
7339
7340
0
      BFD_ASSERT (hdr->contents == NULL);
7341
7342
0
      if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7343
0
  {
7344
0
    _bfd_error_handler
7345
0
      (_("%pB: incorrect `.reginfo' section size; "
7346
0
         "expected %" PRIu64 ", got %" PRIu64),
7347
0
       abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7348
0
       (uint64_t) hdr->sh_size);
7349
0
    bfd_set_error (bfd_error_bad_value);
7350
0
    return false;
7351
0
  }
7352
7353
0
      if (bfd_seek (abfd,
7354
0
        hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7355
0
        SEEK_SET) != 0)
7356
0
  return false;
7357
0
      H_PUT_32 (abfd, elf_gp (abfd), buf);
7358
0
      if (bfd_write (buf, 4, abfd) != 4)
7359
0
  return false;
7360
0
    }
7361
7362
0
  if (hdr->sh_type == SHT_MIPS_OPTIONS
7363
0
      && hdr->bfd_section != NULL
7364
0
      && mips_elf_section_data (hdr->bfd_section) != NULL
7365
0
      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7366
0
    {
7367
0
      bfd_byte *contents, *l, *lend;
7368
7369
      /* We stored the section contents in the tdata field in the
7370
   set_section_contents routine.  We save the section contents
7371
   so that we don't have to read them again.
7372
   At this point we know that elf_gp is set, so we can look
7373
   through the section contents to see if there is an
7374
   ODK_REGINFO structure.  */
7375
7376
0
      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7377
0
      l = contents;
7378
0
      lend = contents + hdr->sh_size;
7379
0
      while (l + sizeof (Elf_External_Options) <= lend)
7380
0
  {
7381
0
    Elf_Internal_Options intopt;
7382
7383
0
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7384
0
          &intopt);
7385
0
    if (intopt.size < sizeof (Elf_External_Options))
7386
0
      {
7387
0
        _bfd_error_handler
7388
    /* xgettext:c-format */
7389
0
    (_("%pB: warning: bad `%s' option size %u smaller than"
7390
0
       " its header"),
7391
0
    abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7392
0
        break;
7393
0
      }
7394
0
    if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7395
0
      {
7396
0
        bfd_byte buf[8];
7397
7398
0
        if (bfd_seek (abfd,
7399
0
          (hdr->sh_offset
7400
0
           + (l - contents)
7401
0
           + sizeof (Elf_External_Options)
7402
0
           + (sizeof (Elf64_External_RegInfo) - 8)),
7403
0
           SEEK_SET) != 0)
7404
0
    return false;
7405
0
        H_PUT_64 (abfd, elf_gp (abfd), buf);
7406
0
        if (bfd_write (buf, 8, abfd) != 8)
7407
0
    return false;
7408
0
      }
7409
0
    else if (intopt.kind == ODK_REGINFO)
7410
0
      {
7411
0
        bfd_byte buf[4];
7412
7413
0
        if (bfd_seek (abfd,
7414
0
          (hdr->sh_offset
7415
0
           + (l - contents)
7416
0
           + sizeof (Elf_External_Options)
7417
0
           + (sizeof (Elf32_External_RegInfo) - 4)),
7418
0
          SEEK_SET) != 0)
7419
0
    return false;
7420
0
        H_PUT_32 (abfd, elf_gp (abfd), buf);
7421
0
        if (bfd_write (buf, 4, abfd) != 4)
7422
0
    return false;
7423
0
      }
7424
0
    l += intopt.size;
7425
0
  }
7426
0
    }
7427
7428
0
  if (hdr->bfd_section != NULL)
7429
0
    {
7430
0
      const char *name = bfd_section_name (hdr->bfd_section);
7431
7432
      /* .sbss is not handled specially here because the GNU/Linux
7433
   prelinker can convert .sbss from NOBITS to PROGBITS and
7434
   changing it back to NOBITS breaks the binary.  The entry in
7435
   _bfd_mips_elf_special_sections will ensure the correct flags
7436
   are set on .sbss if BFD creates it without reading it from an
7437
   input file, and without special handling here the flags set
7438
   on it in an input file will be followed.  */
7439
0
      if (strcmp (name, ".sdata") == 0
7440
0
    || strcmp (name, ".lit8") == 0
7441
0
    || strcmp (name, ".lit4") == 0)
7442
0
  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7443
0
      else if (strcmp (name, ".srdata") == 0)
7444
0
  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7445
0
      else if (strcmp (name, ".compact_rel") == 0)
7446
0
  hdr->sh_flags = 0;
7447
0
      else if (strcmp (name, ".rtproc") == 0)
7448
0
  {
7449
0
    if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7450
0
      {
7451
0
        unsigned int adjust;
7452
7453
0
        adjust = hdr->sh_size % hdr->sh_addralign;
7454
0
        if (adjust != 0)
7455
0
    hdr->sh_size += hdr->sh_addralign - adjust;
7456
0
      }
7457
0
  }
7458
0
    }
7459
7460
0
  return true;
7461
0
}
7462
7463
/* Handle a MIPS specific section when reading an object file.  This
7464
   is called when elfcode.h finds a section with an unknown type.
7465
   This routine supports both the 32-bit and 64-bit ELF ABI.  */
7466
7467
bool
7468
_bfd_mips_elf_section_from_shdr (bfd *abfd,
7469
         Elf_Internal_Shdr *hdr,
7470
         const char *name,
7471
         int shindex)
7472
0
{
7473
0
  flagword flags = 0;
7474
7475
  /* There ought to be a place to keep ELF backend specific flags, but
7476
     at the moment there isn't one.  We just keep track of the
7477
     sections by their name, instead.  Fortunately, the ABI gives
7478
     suggested names for all the MIPS specific sections, so we will
7479
     probably get away with this.  */
7480
0
  switch (hdr->sh_type)
7481
0
    {
7482
0
    case SHT_MIPS_LIBLIST:
7483
0
      if (strcmp (name, ".liblist") != 0)
7484
0
  return false;
7485
0
      break;
7486
0
    case SHT_MIPS_MSYM:
7487
0
      if (strcmp (name, ".msym") != 0)
7488
0
  return false;
7489
0
      break;
7490
0
    case SHT_MIPS_CONFLICT:
7491
0
      if (strcmp (name, ".conflict") != 0)
7492
0
  return false;
7493
0
      break;
7494
0
    case SHT_MIPS_GPTAB:
7495
0
      if (! startswith (name, ".gptab."))
7496
0
  return false;
7497
0
      break;
7498
0
    case SHT_MIPS_UCODE:
7499
0
      if (strcmp (name, ".ucode") != 0)
7500
0
  return false;
7501
0
      break;
7502
0
    case SHT_MIPS_DEBUG:
7503
0
      if (strcmp (name, ".mdebug") != 0)
7504
0
  return false;
7505
0
      flags = SEC_DEBUGGING;
7506
0
      break;
7507
0
    case SHT_MIPS_REGINFO:
7508
0
      if (strcmp (name, ".reginfo") != 0
7509
0
    || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7510
0
  return false;
7511
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7512
0
      break;
7513
0
    case SHT_MIPS_IFACE:
7514
0
      if (strcmp (name, ".MIPS.interfaces") != 0)
7515
0
  return false;
7516
0
      break;
7517
0
    case SHT_MIPS_CONTENT:
7518
0
      if (! startswith (name, ".MIPS.content"))
7519
0
  return false;
7520
0
      break;
7521
0
    case SHT_MIPS_OPTIONS:
7522
0
      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7523
0
  return false;
7524
0
      break;
7525
0
    case SHT_MIPS_ABIFLAGS:
7526
0
      if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7527
0
  return false;
7528
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7529
0
      break;
7530
0
    case SHT_MIPS_DWARF:
7531
0
      if (! startswith (name, ".debug_")
7532
0
         && ! startswith (name, ".gnu.debuglto_.debug_")
7533
0
         && ! startswith (name, ".zdebug_")
7534
0
         && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7535
0
  return false;
7536
0
      break;
7537
0
    case SHT_MIPS_SYMBOL_LIB:
7538
0
      if (strcmp (name, ".MIPS.symlib") != 0)
7539
0
  return false;
7540
0
      break;
7541
0
    case SHT_MIPS_EVENTS:
7542
0
      if (! startswith (name, ".MIPS.events")
7543
0
    && ! startswith (name, ".MIPS.post_rel"))
7544
0
  return false;
7545
0
      break;
7546
0
    case SHT_MIPS_XHASH:
7547
0
      if (strcmp (name, ".MIPS.xhash") != 0)
7548
0
  return false;
7549
0
    default:
7550
0
      break;
7551
0
    }
7552
7553
0
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7554
0
    return false;
7555
7556
0
  if (hdr->sh_flags & SHF_MIPS_GPREL)
7557
0
    flags |= SEC_SMALL_DATA;
7558
7559
0
  if (flags)
7560
0
    {
7561
0
      if (!bfd_set_section_flags (hdr->bfd_section,
7562
0
          (bfd_section_flags (hdr->bfd_section)
7563
0
           | flags)))
7564
0
  return false;
7565
0
    }
7566
7567
0
  if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7568
0
    {
7569
0
      Elf_External_ABIFlags_v0 ext;
7570
7571
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7572
0
              &ext, 0, sizeof ext))
7573
0
  return false;
7574
0
      bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7575
0
          &mips_elf_tdata (abfd)->abiflags);
7576
0
      if (mips_elf_tdata (abfd)->abiflags.version != 0)
7577
0
  return false;
7578
0
      mips_elf_tdata (abfd)->abiflags_valid = true;
7579
0
    }
7580
7581
  /* FIXME: We should record sh_info for a .gptab section.  */
7582
7583
  /* For a .reginfo section, set the gp value in the tdata information
7584
     from the contents of this section.  We need the gp value while
7585
     processing relocs, so we just get it now.  The .reginfo section
7586
     is not used in the 64-bit MIPS ELF ABI.  */
7587
0
  if (hdr->sh_type == SHT_MIPS_REGINFO)
7588
0
    {
7589
0
      Elf32_External_RegInfo ext;
7590
0
      Elf32_RegInfo s;
7591
7592
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7593
0
              &ext, 0, sizeof ext))
7594
0
  return false;
7595
0
      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7596
0
      elf_gp (abfd) = s.ri_gp_value;
7597
0
    }
7598
7599
  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7600
     set the gp value based on what we find.  We may see both
7601
     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7602
     they should agree.  */
7603
0
  if (hdr->sh_type == SHT_MIPS_OPTIONS)
7604
0
    {
7605
0
      bfd_byte *contents, *l, *lend;
7606
7607
0
      if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7608
0
  {
7609
0
    free (contents);
7610
0
    return false;
7611
0
  }
7612
0
      l = contents;
7613
0
      lend = contents + hdr->sh_size;
7614
0
      while (l + sizeof (Elf_External_Options) <= lend)
7615
0
  {
7616
0
    Elf_Internal_Options intopt;
7617
7618
0
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7619
0
          &intopt);
7620
0
    if (intopt.size < sizeof (Elf_External_Options))
7621
0
      {
7622
0
      bad_opt:
7623
0
        _bfd_error_handler
7624
    /* xgettext:c-format */
7625
0
    (_("%pB: warning: truncated `%s' option"),
7626
0
     abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7627
0
        break;
7628
0
      }
7629
0
    if (intopt.kind == ODK_REGINFO)
7630
0
      {
7631
0
        if (ABI_64_P (abfd))
7632
0
    {
7633
0
      Elf64_Internal_RegInfo intreg;
7634
0
      size_t needed = (sizeof (Elf_External_Options)
7635
0
           + sizeof (Elf64_External_RegInfo));
7636
0
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7637
0
        goto bad_opt;
7638
0
      bfd_mips_elf64_swap_reginfo_in
7639
0
        (abfd,
7640
0
         ((Elf64_External_RegInfo *)
7641
0
          (l + sizeof (Elf_External_Options))),
7642
0
         &intreg);
7643
0
      elf_gp (abfd) = intreg.ri_gp_value;
7644
0
    }
7645
0
        else
7646
0
    {
7647
0
      Elf32_RegInfo intreg;
7648
0
      size_t needed = (sizeof (Elf_External_Options)
7649
0
           + sizeof (Elf32_External_RegInfo));
7650
0
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7651
0
        goto bad_opt;
7652
0
      bfd_mips_elf32_swap_reginfo_in
7653
0
        (abfd,
7654
0
         ((Elf32_External_RegInfo *)
7655
0
          (l + sizeof (Elf_External_Options))),
7656
0
         &intreg);
7657
0
      elf_gp (abfd) = intreg.ri_gp_value;
7658
0
    }
7659
0
      }
7660
0
    l += intopt.size;
7661
0
  }
7662
0
      free (contents);
7663
0
    }
7664
7665
0
  return true;
7666
0
}
7667
7668
/* Set the correct type for a MIPS ELF section.  We do this by the
7669
   section name, which is a hack, but ought to work.  This routine is
7670
   used by both the 32-bit and the 64-bit ABI.  */
7671
7672
bool
7673
_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7674
0
{
7675
0
  const char *name = bfd_section_name (sec);
7676
7677
0
  if (strcmp (name, ".liblist") == 0)
7678
0
    {
7679
0
      hdr->sh_type = SHT_MIPS_LIBLIST;
7680
0
      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7681
      /* The sh_link field is set in final_write_processing.  */
7682
0
    }
7683
0
  else if (strcmp (name, ".conflict") == 0)
7684
0
    hdr->sh_type = SHT_MIPS_CONFLICT;
7685
0
  else if (startswith (name, ".gptab."))
7686
0
    {
7687
0
      hdr->sh_type = SHT_MIPS_GPTAB;
7688
0
      hdr->sh_entsize = sizeof (Elf32_External_gptab);
7689
      /* The sh_info field is set in final_write_processing.  */
7690
0
    }
7691
0
  else if (strcmp (name, ".ucode") == 0)
7692
0
    hdr->sh_type = SHT_MIPS_UCODE;
7693
0
  else if (strcmp (name, ".mdebug") == 0)
7694
0
    {
7695
0
      hdr->sh_type = SHT_MIPS_DEBUG;
7696
      /* In a shared object on IRIX 5.3, the .mdebug section has an
7697
   entsize of 0.  FIXME: Does this matter?  */
7698
0
      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7699
0
  hdr->sh_entsize = 0;
7700
0
      else
7701
0
  hdr->sh_entsize = 1;
7702
0
    }
7703
0
  else if (strcmp (name, ".reginfo") == 0)
7704
0
    {
7705
0
      hdr->sh_type = SHT_MIPS_REGINFO;
7706
      /* In a shared object on IRIX 5.3, the .reginfo section has an
7707
   entsize of 0x18.  FIXME: Does this matter?  */
7708
0
      if (SGI_COMPAT (abfd))
7709
0
  {
7710
0
    if ((abfd->flags & DYNAMIC) != 0)
7711
0
      hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7712
0
    else
7713
0
      hdr->sh_entsize = 1;
7714
0
  }
7715
0
      else
7716
0
  hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7717
0
    }
7718
0
  else if (SGI_COMPAT (abfd)
7719
0
     && (strcmp (name, ".hash") == 0
7720
0
         || strcmp (name, ".dynamic") == 0
7721
0
         || strcmp (name, ".dynstr") == 0))
7722
0
    {
7723
0
      if (SGI_COMPAT (abfd))
7724
0
  hdr->sh_entsize = 0;
7725
#if 0
7726
      /* This isn't how the IRIX6 linker behaves.  */
7727
      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7728
#endif
7729
0
    }
7730
0
  else if (strcmp (name, ".got") == 0
7731
0
     || strcmp (name, ".srdata") == 0
7732
0
     || strcmp (name, ".sdata") == 0
7733
0
     || strcmp (name, ".sbss") == 0
7734
0
     || strcmp (name, ".lit4") == 0
7735
0
     || strcmp (name, ".lit8") == 0)
7736
0
    hdr->sh_flags |= SHF_MIPS_GPREL;
7737
0
  else if (strcmp (name, ".MIPS.interfaces") == 0)
7738
0
    {
7739
0
      hdr->sh_type = SHT_MIPS_IFACE;
7740
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7741
0
    }
7742
0
  else if (startswith (name, ".MIPS.content"))
7743
0
    {
7744
0
      hdr->sh_type = SHT_MIPS_CONTENT;
7745
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7746
      /* The sh_info field is set in final_write_processing.  */
7747
0
    }
7748
0
  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7749
0
    {
7750
0
      hdr->sh_type = SHT_MIPS_OPTIONS;
7751
0
      hdr->sh_entsize = 1;
7752
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7753
0
    }
7754
0
  else if (startswith (name, ".MIPS.abiflags"))
7755
0
    {
7756
0
      hdr->sh_type = SHT_MIPS_ABIFLAGS;
7757
0
      hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7758
0
    }
7759
0
  else if (startswith (name, ".debug_")
7760
0
     || startswith (name, ".gnu.debuglto_.debug_")
7761
0
     || startswith (name, ".zdebug_")
7762
0
     || startswith (name, ".gnu.debuglto_.zdebug_"))
7763
0
    {
7764
0
      hdr->sh_type = SHT_MIPS_DWARF;
7765
7766
      /* Irix facilities such as libexc expect a single .debug_frame
7767
   per executable, the system ones have NOSTRIP set and the linker
7768
   doesn't merge sections with different flags so ...  */
7769
0
      if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7770
0
  hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7771
0
    }
7772
0
  else if (strcmp (name, ".MIPS.symlib") == 0)
7773
0
    {
7774
0
      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7775
      /* The sh_link and sh_info fields are set in
7776
   final_write_processing.  */
7777
0
    }
7778
0
  else if (startswith (name, ".MIPS.events")
7779
0
     || startswith (name, ".MIPS.post_rel"))
7780
0
    {
7781
0
      hdr->sh_type = SHT_MIPS_EVENTS;
7782
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7783
      /* The sh_link field is set in final_write_processing.  */
7784
0
    }
7785
0
  else if (strcmp (name, ".msym") == 0)
7786
0
    {
7787
0
      hdr->sh_type = SHT_MIPS_MSYM;
7788
0
      hdr->sh_flags |= SHF_ALLOC;
7789
0
      hdr->sh_entsize = 8;
7790
0
    }
7791
0
  else if (strcmp (name, ".MIPS.xhash") == 0)
7792
0
    {
7793
0
      hdr->sh_type = SHT_MIPS_XHASH;
7794
0
      hdr->sh_flags |= SHF_ALLOC;
7795
0
      hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7796
0
    }
7797
7798
  /* The generic elf_fake_sections will set up REL_HDR using the default
7799
   kind of relocations.  We used to set up a second header for the
7800
   non-default kind of relocations here, but only NewABI would use
7801
   these, and the IRIX ld doesn't like resulting empty RELA sections.
7802
   Thus we create those header only on demand now.  */
7803
7804
0
  return true;
7805
0
}
7806
7807
/* Given a BFD section, try to locate the corresponding ELF section
7808
   index.  This is used by both the 32-bit and the 64-bit ABI.
7809
   Actually, it's not clear to me that the 64-bit ABI supports these,
7810
   but for non-PIC objects we will certainly want support for at least
7811
   the .scommon section.  */
7812
7813
bool
7814
_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7815
          asection *sec, int *retval)
7816
0
{
7817
0
  if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7818
0
    {
7819
0
      *retval = SHN_MIPS_SCOMMON;
7820
0
      return true;
7821
0
    }
7822
0
  if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7823
0
    {
7824
0
      *retval = SHN_MIPS_ACOMMON;
7825
0
      return true;
7826
0
    }
7827
0
  return false;
7828
0
}
7829

7830
/* Hook called by the linker routine which adds symbols from an object
7831
   file.  We must handle the special MIPS section numbers here.  */
7832
7833
bool
7834
_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7835
             Elf_Internal_Sym *sym, const char **namep,
7836
             flagword *flagsp ATTRIBUTE_UNUSED,
7837
             asection **secp, bfd_vma *valp)
7838
0
{
7839
0
  if (SGI_COMPAT (abfd)
7840
0
      && (abfd->flags & DYNAMIC) != 0
7841
0
      && strcmp (*namep, "_rld_new_interface") == 0)
7842
0
    {
7843
      /* Skip IRIX5 rld entry name.  */
7844
0
      *namep = NULL;
7845
0
      return true;
7846
0
    }
7847
7848
  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7849
     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
7850
     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
7851
     a magic symbol resolved by the linker, we ignore this bogus definition
7852
     of _gp_disp.  New ABI objects do not suffer from this problem so this
7853
     is not done for them. */
7854
0
  if (!NEWABI_P(abfd)
7855
0
      && (sym->st_shndx == SHN_ABS)
7856
0
      && (strcmp (*namep, "_gp_disp") == 0))
7857
0
    {
7858
0
      *namep = NULL;
7859
0
      return true;
7860
0
    }
7861
7862
0
  switch (sym->st_shndx)
7863
0
    {
7864
0
    case SHN_COMMON:
7865
      /* Common symbols less than the GP size are automatically
7866
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7867
0
      if (sym->st_size > elf_gp_size (abfd)
7868
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS
7869
0
    || IRIX_COMPAT (abfd) == ict_irix6
7870
0
    || strcmp (*namep, "__gnu_lto_slim") == 0)
7871
0
  break;
7872
      /* Fall through.  */
7873
0
    case SHN_MIPS_SCOMMON:
7874
0
      *secp = bfd_make_section_old_way (abfd, ".scommon");
7875
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7876
0
      *valp = sym->st_size;
7877
0
      break;
7878
7879
0
    case SHN_MIPS_TEXT:
7880
      /* This section is used in a shared object.  */
7881
0
      if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7882
0
  {
7883
0
    asymbol *elf_text_symbol;
7884
0
    asection *elf_text_section;
7885
0
    size_t amt = sizeof (asection);
7886
7887
0
    elf_text_section = bfd_zalloc (abfd, amt);
7888
0
    if (elf_text_section == NULL)
7889
0
      return false;
7890
7891
0
    amt = sizeof (asymbol);
7892
0
    elf_text_symbol = bfd_zalloc (abfd, amt);
7893
0
    if (elf_text_symbol == NULL)
7894
0
      return false;
7895
7896
    /* Initialize the section.  */
7897
7898
0
    mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7899
0
    mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7900
7901
0
    elf_text_section->symbol = elf_text_symbol;
7902
0
    elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7903
7904
0
    elf_text_section->name = ".text";
7905
0
    elf_text_section->flags = SEC_NO_FLAGS;
7906
0
    elf_text_section->output_section = NULL;
7907
0
    elf_text_section->owner = abfd;
7908
0
    elf_text_symbol->name = ".text";
7909
0
    elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7910
0
    elf_text_symbol->section = elf_text_section;
7911
0
  }
7912
      /* This code used to do *secp = bfd_und_section_ptr if
7913
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7914
   so I took it out.  */
7915
0
      *secp = mips_elf_tdata (abfd)->elf_text_section;
7916
0
      break;
7917
7918
0
    case SHN_MIPS_ACOMMON:
7919
      /* Fall through. XXX Can we treat this as allocated data?  */
7920
0
    case SHN_MIPS_DATA:
7921
      /* This section is used in a shared object.  */
7922
0
      if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7923
0
  {
7924
0
    asymbol *elf_data_symbol;
7925
0
    asection *elf_data_section;
7926
0
    size_t amt = sizeof (asection);
7927
7928
0
    elf_data_section = bfd_zalloc (abfd, amt);
7929
0
    if (elf_data_section == NULL)
7930
0
      return false;
7931
7932
0
    amt = sizeof (asymbol);
7933
0
    elf_data_symbol = bfd_zalloc (abfd, amt);
7934
0
    if (elf_data_symbol == NULL)
7935
0
      return false;
7936
7937
    /* Initialize the section.  */
7938
7939
0
    mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7940
0
    mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7941
7942
0
    elf_data_section->symbol = elf_data_symbol;
7943
0
    elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7944
7945
0
    elf_data_section->name = ".data";
7946
0
    elf_data_section->flags = SEC_NO_FLAGS;
7947
0
    elf_data_section->output_section = NULL;
7948
0
    elf_data_section->owner = abfd;
7949
0
    elf_data_symbol->name = ".data";
7950
0
    elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7951
0
    elf_data_symbol->section = elf_data_section;
7952
0
  }
7953
      /* This code used to do *secp = bfd_und_section_ptr if
7954
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7955
   so I took it out.  */
7956
0
      *secp = mips_elf_tdata (abfd)->elf_data_section;
7957
0
      break;
7958
7959
0
    case SHN_MIPS_SUNDEFINED:
7960
0
      *secp = bfd_und_section_ptr;
7961
0
      break;
7962
0
    }
7963
7964
0
  if (SGI_COMPAT (abfd)
7965
0
      && ! bfd_link_pic (info)
7966
0
      && info->output_bfd->xvec == abfd->xvec
7967
0
      && strcmp (*namep, "__rld_obj_head") == 0)
7968
0
    {
7969
0
      struct elf_link_hash_entry *h;
7970
0
      struct bfd_link_hash_entry *bh;
7971
7972
      /* Mark __rld_obj_head as dynamic.  */
7973
0
      bh = NULL;
7974
0
      if (! (_bfd_generic_link_add_one_symbol
7975
0
       (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7976
0
        get_elf_backend_data (abfd)->collect, &bh)))
7977
0
  return false;
7978
7979
0
      h = (struct elf_link_hash_entry *) bh;
7980
0
      h->non_elf = 0;
7981
0
      h->def_regular = 1;
7982
0
      h->type = STT_OBJECT;
7983
7984
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
7985
0
  return false;
7986
7987
0
      mips_elf_hash_table (info)->use_rld_obj_head = true;
7988
0
      mips_elf_hash_table (info)->rld_symbol = h;
7989
0
    }
7990
7991
  /* If this is a mips16 text symbol, add 1 to the value to make it
7992
     odd.  This will cause something like .word SYM to come up with
7993
     the right value when it is loaded into the PC.  */
7994
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
7995
0
    ++*valp;
7996
7997
0
  return true;
7998
0
}
7999
8000
/* This hook function is called before the linker writes out a global
8001
   symbol.  We mark symbols as small common if appropriate.  This is
8002
   also where we undo the increment of the value for a mips16 symbol.  */
8003
8004
int
8005
_bfd_mips_elf_link_output_symbol_hook
8006
  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8007
   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8008
   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8009
0
{
8010
  /* If we see a common symbol, which implies a relocatable link, then
8011
     if a symbol was small common in an input file, mark it as small
8012
     common in the output file.  */
8013
0
  if (sym->st_shndx == SHN_COMMON
8014
0
      && strcmp (input_sec->name, ".scommon") == 0)
8015
0
    sym->st_shndx = SHN_MIPS_SCOMMON;
8016
8017
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8018
0
    sym->st_value &= ~1;
8019
8020
0
  return 1;
8021
0
}
8022

8023
/* Functions for the dynamic linker.  */
8024
8025
/* Create dynamic sections when linking against a dynamic object.  */
8026
8027
bool
8028
_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8029
0
{
8030
0
  struct elf_link_hash_entry *h;
8031
0
  struct bfd_link_hash_entry *bh;
8032
0
  flagword flags;
8033
0
  register asection *s;
8034
0
  const char * const *namep;
8035
0
  struct mips_elf_link_hash_table *htab;
8036
8037
0
  htab = mips_elf_hash_table (info);
8038
0
  BFD_ASSERT (htab != NULL);
8039
8040
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8041
0
     | SEC_LINKER_CREATED | SEC_READONLY);
8042
8043
  /* The psABI requires a read-only .dynamic section, but the VxWorks
8044
     EABI doesn't.  */
8045
0
  if (htab->root.target_os != is_vxworks)
8046
0
    {
8047
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8048
0
      if (s != NULL)
8049
0
  {
8050
0
    if (!bfd_set_section_flags (s, flags))
8051
0
      return false;
8052
0
  }
8053
0
    }
8054
8055
  /* We need to create .got section.  */
8056
0
  if (!mips_elf_create_got_section (abfd, info))
8057
0
    return false;
8058
8059
0
  if (! mips_elf_rel_dyn_section (info, true))
8060
0
    return false;
8061
8062
  /* Create .stub section.  */
8063
0
  s = bfd_make_section_anyway_with_flags (abfd,
8064
0
            MIPS_ELF_STUB_SECTION_NAME (abfd),
8065
0
            flags | SEC_CODE);
8066
0
  if (s == NULL
8067
0
      || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8068
0
    return false;
8069
0
  htab->sstubs = s;
8070
8071
0
  if (!mips_elf_hash_table (info)->use_rld_obj_head
8072
0
      && bfd_link_executable (info)
8073
0
      && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8074
0
    {
8075
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8076
0
                flags &~ (flagword) SEC_READONLY);
8077
0
      if (s == NULL
8078
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8079
0
  return false;
8080
0
    }
8081
8082
  /* Create .MIPS.xhash section.  */
8083
0
  if (info->emit_gnu_hash)
8084
0
    s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8085
0
              flags | SEC_READONLY);
8086
8087
  /* On IRIX5, we adjust add some additional symbols and change the
8088
     alignments of several sections.  There is no ABI documentation
8089
     indicating that this is necessary on IRIX6, nor any evidence that
8090
     the linker takes such action.  */
8091
0
  if (IRIX_COMPAT (abfd) == ict_irix5)
8092
0
    {
8093
0
      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8094
0
  {
8095
0
    bh = NULL;
8096
0
    if (! (_bfd_generic_link_add_one_symbol
8097
0
     (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8098
0
      NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8099
0
      return false;
8100
8101
0
    h = (struct elf_link_hash_entry *) bh;
8102
0
    h->mark = 1;
8103
0
    h->non_elf = 0;
8104
0
    h->def_regular = 1;
8105
0
    h->type = STT_SECTION;
8106
8107
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8108
0
      return false;
8109
0
  }
8110
8111
      /* We need to create a .compact_rel section.  */
8112
0
      if (SGI_COMPAT (abfd))
8113
0
  {
8114
0
    if (!mips_elf_create_compact_rel_section (abfd, info))
8115
0
      return false;
8116
0
  }
8117
8118
      /* Change alignments of some sections.  */
8119
0
      s = bfd_get_linker_section (abfd, ".hash");
8120
0
      if (s != NULL)
8121
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8122
8123
0
      s = bfd_get_linker_section (abfd, ".dynsym");
8124
0
      if (s != NULL)
8125
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8126
8127
0
      s = bfd_get_linker_section (abfd, ".dynstr");
8128
0
      if (s != NULL)
8129
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8130
8131
      /* ??? */
8132
0
      s = bfd_get_section_by_name (abfd, ".reginfo");
8133
0
      if (s != NULL)
8134
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8135
8136
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8137
0
      if (s != NULL)
8138
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8139
0
    }
8140
8141
0
  if (bfd_link_executable (info))
8142
0
    {
8143
0
      const char *name;
8144
8145
0
      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8146
0
      bh = NULL;
8147
0
      if (!(_bfd_generic_link_add_one_symbol
8148
0
      (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8149
0
       NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8150
0
  return false;
8151
8152
0
      h = (struct elf_link_hash_entry *) bh;
8153
0
      h->non_elf = 0;
8154
0
      h->def_regular = 1;
8155
0
      h->type = STT_SECTION;
8156
8157
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8158
0
  return false;
8159
8160
0
      if (! mips_elf_hash_table (info)->use_rld_obj_head)
8161
0
  {
8162
    /* __rld_map is a four byte word located in the .data section
8163
       and is filled in by the rtld to contain a pointer to
8164
       the _r_debug structure. Its symbol value will be set in
8165
       _bfd_mips_elf_finish_dynamic_symbol.  */
8166
0
    s = bfd_get_linker_section (abfd, ".rld_map");
8167
0
    BFD_ASSERT (s != NULL);
8168
8169
0
    name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8170
0
    bh = NULL;
8171
0
    if (!(_bfd_generic_link_add_one_symbol
8172
0
    (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8173
0
     get_elf_backend_data (abfd)->collect, &bh)))
8174
0
      return false;
8175
8176
0
    h = (struct elf_link_hash_entry *) bh;
8177
0
    h->non_elf = 0;
8178
0
    h->def_regular = 1;
8179
0
    h->type = STT_OBJECT;
8180
8181
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8182
0
      return false;
8183
0
    mips_elf_hash_table (info)->rld_symbol = h;
8184
0
  }
8185
0
    }
8186
8187
  /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8188
     Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
8189
0
  if (!_bfd_elf_create_dynamic_sections (abfd, info))
8190
0
    return false;
8191
8192
  /* Do the usual VxWorks handling.  */
8193
0
  if (htab->root.target_os == is_vxworks
8194
0
      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8195
0
    return false;
8196
8197
0
  return true;
8198
0
}
8199

8200
/* Return true if relocation REL against section SEC is a REL rather than
8201
   RELA relocation.  RELOCS is the first relocation in the section and
8202
   ABFD is the bfd that contains SEC.  */
8203
8204
static bool
8205
mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8206
         const Elf_Internal_Rela *relocs,
8207
         const Elf_Internal_Rela *rel)
8208
0
{
8209
0
  Elf_Internal_Shdr *rel_hdr;
8210
0
  const struct elf_backend_data *bed;
8211
8212
  /* To determine which flavor of relocation this is, we depend on the
8213
     fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
8214
0
  rel_hdr = elf_section_data (sec)->rel.hdr;
8215
0
  if (rel_hdr == NULL)
8216
0
    return false;
8217
0
  bed = get_elf_backend_data (abfd);
8218
0
  return ((size_t) (rel - relocs)
8219
0
    < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8220
0
}
8221
8222
/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8223
   HOWTO is the relocation's howto and CONTENTS points to the contents
8224
   of the section that REL is against.  */
8225
8226
static bfd_vma
8227
mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8228
        const Elf_Internal_Rela *rel,
8229
        reloc_howto_type *howto, bfd_byte *contents)
8230
0
{
8231
0
  bfd_byte *location;
8232
0
  unsigned int r_type;
8233
0
  bfd_vma addend;
8234
0
  bfd_vma bytes;
8235
8236
0
  if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8237
0
    return 0;
8238
8239
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8240
0
  location = contents + rel->r_offset;
8241
8242
  /* Get the addend, which is stored in the input file.  */
8243
0
  _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8244
0
  bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8245
0
  _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8246
8247
0
  addend = bytes & howto->src_mask;
8248
8249
  /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
8250
     accordingly.  */
8251
0
  if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8252
0
    addend <<= 1;
8253
8254
0
  return addend;
8255
0
}
8256
8257
/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8258
   and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
8259
   and update *ADDEND with the final addend.  Return true on success
8260
   or false if the LO16 could not be found.  RELEND is the exclusive
8261
   upper bound on the relocations for REL's section.  */
8262
8263
static bool
8264
mips_elf_add_lo16_rel_addend (bfd *abfd,
8265
            asection *sec,
8266
            const Elf_Internal_Rela *rel,
8267
            const Elf_Internal_Rela *relend,
8268
            bfd_byte *contents, bfd_vma *addend)
8269
0
{
8270
0
  unsigned int r_type, lo16_type;
8271
0
  const Elf_Internal_Rela *lo16_relocation;
8272
0
  reloc_howto_type *lo16_howto;
8273
0
  bfd_vma l;
8274
8275
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8276
0
  if (mips16_reloc_p (r_type))
8277
0
    lo16_type = R_MIPS16_LO16;
8278
0
  else if (micromips_reloc_p (r_type))
8279
0
    lo16_type = R_MICROMIPS_LO16;
8280
0
  else if (r_type == R_MIPS_PCHI16)
8281
0
    lo16_type = R_MIPS_PCLO16;
8282
0
  else
8283
0
    lo16_type = R_MIPS_LO16;
8284
8285
  /* The combined value is the sum of the HI16 addend, left-shifted by
8286
     sixteen bits, and the LO16 addend, sign extended.  (Usually, the
8287
     code does a `lui' of the HI16 value, and then an `addiu' of the
8288
     LO16 value.)
8289
8290
     Scan ahead to find a matching LO16 relocation.
8291
8292
     According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8293
     be immediately following.  However, for the IRIX6 ABI, the next
8294
     relocation may be a composed relocation consisting of several
8295
     relocations for the same address.  In that case, the R_MIPS_LO16
8296
     relocation may occur as one of these.  We permit a similar
8297
     extension in general, as that is useful for GCC.
8298
8299
     In some cases GCC dead code elimination removes the LO16 but keeps
8300
     the corresponding HI16.  This is strictly speaking a violation of
8301
     the ABI but not immediately harmful.  */
8302
0
  lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8303
0
  if (lo16_relocation == NULL)
8304
0
    return false;
8305
8306
  /* Obtain the addend kept there.  */
8307
0
  lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8308
0
  l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8309
0
        contents);
8310
8311
0
  l <<= lo16_howto->rightshift;
8312
0
  l = _bfd_mips_elf_sign_extend (l, 16);
8313
8314
0
  *addend <<= 16;
8315
0
  *addend += l;
8316
0
  return true;
8317
0
}
8318
8319
/* Try to read the contents of section SEC in bfd ABFD.  Return true and
8320
   store the contents in *CONTENTS on success.  Assume that *CONTENTS
8321
   already holds the contents if it is nonull on entry.  */
8322
8323
static bool
8324
mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8325
0
{
8326
0
  if (*contents)
8327
0
    return true;
8328
8329
  /* Get cached copy if it exists.  */
8330
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
8331
0
    {
8332
0
      *contents = elf_section_data (sec)->this_hdr.contents;
8333
0
      return true;
8334
0
    }
8335
8336
0
  return bfd_malloc_and_get_section (abfd, sec, contents);
8337
0
}
8338
8339
/* Make a new PLT record to keep internal data.  */
8340
8341
static struct plt_entry *
8342
mips_elf_make_plt_record (bfd *abfd)
8343
0
{
8344
0
  struct plt_entry *entry;
8345
8346
0
  entry = bfd_zalloc (abfd, sizeof (*entry));
8347
0
  if (entry == NULL)
8348
0
    return NULL;
8349
8350
0
  entry->stub_offset = MINUS_ONE;
8351
0
  entry->mips_offset = MINUS_ONE;
8352
0
  entry->comp_offset = MINUS_ONE;
8353
0
  entry->gotplt_index = MINUS_ONE;
8354
0
  return entry;
8355
0
}
8356
8357
/* Define the special `__gnu_absolute_zero' symbol.  We only need this
8358
   for PIC code, as otherwise there is no load-time relocation involved
8359
   and local GOT entries whose value is zero at static link time will
8360
   retain their value at load time.  */
8361
8362
static bool
8363
mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8364
             struct mips_elf_link_hash_table *htab,
8365
             unsigned int r_type)
8366
0
{
8367
0
  union
8368
0
    {
8369
0
      struct elf_link_hash_entry *eh;
8370
0
      struct bfd_link_hash_entry *bh;
8371
0
    }
8372
0
  hzero;
8373
8374
0
  BFD_ASSERT (!htab->use_absolute_zero);
8375
0
  BFD_ASSERT (bfd_link_pic (info));
8376
8377
0
  hzero.bh = NULL;
8378
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8379
0
           BSF_GLOBAL, bfd_abs_section_ptr, 0,
8380
0
           NULL, false, false, &hzero.bh))
8381
0
    return false;
8382
8383
0
  BFD_ASSERT (hzero.bh != NULL);
8384
0
  hzero.eh->size = 0;
8385
0
  hzero.eh->type = STT_NOTYPE;
8386
0
  hzero.eh->other = STV_PROTECTED;
8387
0
  hzero.eh->def_regular = 1;
8388
0
  hzero.eh->non_elf = 0;
8389
8390
0
  if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8391
0
    return false;
8392
8393
0
  htab->use_absolute_zero = true;
8394
8395
0
  return true;
8396
0
}
8397
8398
/* Look through the relocs for a section during the first phase, and
8399
   allocate space in the global offset table and record the need for
8400
   standard MIPS and compressed procedure linkage table entries.  */
8401
8402
bool
8403
_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8404
          asection *sec, const Elf_Internal_Rela *relocs)
8405
0
{
8406
0
  const char *name;
8407
0
  bfd *dynobj;
8408
0
  Elf_Internal_Shdr *symtab_hdr;
8409
0
  struct elf_link_hash_entry **sym_hashes;
8410
0
  size_t extsymoff;
8411
0
  const Elf_Internal_Rela *rel;
8412
0
  const Elf_Internal_Rela *rel_end;
8413
0
  asection *sreloc;
8414
0
  const struct elf_backend_data *bed;
8415
0
  struct mips_elf_link_hash_table *htab;
8416
0
  bfd_byte *contents;
8417
0
  bfd_vma addend;
8418
0
  reloc_howto_type *howto;
8419
8420
0
  if (bfd_link_relocatable (info))
8421
0
    return true;
8422
8423
0
  htab = mips_elf_hash_table (info);
8424
0
  BFD_ASSERT (htab != NULL);
8425
8426
0
  dynobj = elf_hash_table (info)->dynobj;
8427
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8428
0
  sym_hashes = elf_sym_hashes (abfd);
8429
0
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8430
8431
0
  bed = get_elf_backend_data (abfd);
8432
0
  rel_end = relocs + sec->reloc_count;
8433
8434
  /* Check for the mips16 stub sections.  */
8435
8436
0
  name = bfd_section_name (sec);
8437
0
  if (FN_STUB_P (name))
8438
0
    {
8439
0
      unsigned long r_symndx;
8440
8441
      /* Look at the relocation information to figure out which symbol
8442
   this is for.  */
8443
8444
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8445
0
      if (r_symndx == 0)
8446
0
  {
8447
0
    _bfd_error_handler
8448
      /* xgettext:c-format */
8449
0
      (_("%pB: warning: cannot determine the target function for"
8450
0
         " stub section `%s'"),
8451
0
       abfd, name);
8452
0
    bfd_set_error (bfd_error_bad_value);
8453
0
    return false;
8454
0
  }
8455
8456
0
      if (r_symndx < extsymoff
8457
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8458
0
  {
8459
0
    asection *o;
8460
8461
    /* This stub is for a local symbol.  This stub will only be
8462
       needed if there is some relocation in this BFD, other
8463
       than a 16 bit function call, which refers to this symbol.  */
8464
0
    for (o = abfd->sections; o != NULL; o = o->next)
8465
0
      {
8466
0
        Elf_Internal_Rela *sec_relocs;
8467
0
        const Elf_Internal_Rela *r, *rend;
8468
8469
        /* We can ignore stub sections when looking for relocs.  */
8470
0
        if ((o->flags & SEC_RELOC) == 0
8471
0
      || o->reloc_count == 0
8472
0
      || section_allows_mips16_refs_p (o))
8473
0
    continue;
8474
8475
0
        sec_relocs
8476
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8477
0
               info->keep_memory);
8478
0
        if (sec_relocs == NULL)
8479
0
    return false;
8480
8481
0
        rend = sec_relocs + o->reloc_count;
8482
0
        for (r = sec_relocs; r < rend; r++)
8483
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8484
0
        && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8485
0
      break;
8486
8487
0
        if (elf_section_data (o)->relocs != sec_relocs)
8488
0
    free (sec_relocs);
8489
8490
0
        if (r < rend)
8491
0
    break;
8492
0
      }
8493
8494
0
    if (o == NULL)
8495
0
      {
8496
        /* There is no non-call reloc for this stub, so we do
8497
     not need it.  Since this function is called before
8498
     the linker maps input sections to output sections, we
8499
     can easily discard it by setting the SEC_EXCLUDE
8500
     flag.  */
8501
0
        sec->flags |= SEC_EXCLUDE;
8502
0
        return true;
8503
0
      }
8504
8505
    /* Record this stub in an array of local symbol stubs for
8506
       this BFD.  */
8507
0
    if (mips_elf_tdata (abfd)->local_stubs == NULL)
8508
0
      {
8509
0
        unsigned long symcount;
8510
0
        asection **n;
8511
0
        bfd_size_type amt;
8512
8513
0
        if (elf_bad_symtab (abfd))
8514
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8515
0
        else
8516
0
    symcount = symtab_hdr->sh_info;
8517
0
        amt = symcount * sizeof (asection *);
8518
0
        n = bfd_zalloc (abfd, amt);
8519
0
        if (n == NULL)
8520
0
    return false;
8521
0
        mips_elf_tdata (abfd)->local_stubs = n;
8522
0
      }
8523
8524
0
    sec->flags |= SEC_KEEP;
8525
0
    mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8526
8527
    /* We don't need to set mips16_stubs_seen in this case.
8528
       That flag is used to see whether we need to look through
8529
       the global symbol table for stubs.  We don't need to set
8530
       it here, because we just have a local stub.  */
8531
0
  }
8532
0
      else
8533
0
  {
8534
0
    struct mips_elf_link_hash_entry *h;
8535
8536
0
    h = ((struct mips_elf_link_hash_entry *)
8537
0
         sym_hashes[r_symndx - extsymoff]);
8538
8539
0
    while (h->root.root.type == bfd_link_hash_indirect
8540
0
     || h->root.root.type == bfd_link_hash_warning)
8541
0
      h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8542
8543
    /* H is the symbol this stub is for.  */
8544
8545
    /* If we already have an appropriate stub for this function, we
8546
       don't need another one, so we can discard this one.  Since
8547
       this function is called before the linker maps input sections
8548
       to output sections, we can easily discard it by setting the
8549
       SEC_EXCLUDE flag.  */
8550
0
    if (h->fn_stub != NULL)
8551
0
      {
8552
0
        sec->flags |= SEC_EXCLUDE;
8553
0
        return true;
8554
0
      }
8555
8556
0
    sec->flags |= SEC_KEEP;
8557
0
    h->fn_stub = sec;
8558
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8559
0
  }
8560
0
    }
8561
0
  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8562
0
    {
8563
0
      unsigned long r_symndx;
8564
0
      struct mips_elf_link_hash_entry *h;
8565
0
      asection **loc;
8566
8567
      /* Look at the relocation information to figure out which symbol
8568
   this is for.  */
8569
8570
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8571
0
      if (r_symndx == 0)
8572
0
  {
8573
0
    _bfd_error_handler
8574
      /* xgettext:c-format */
8575
0
      (_("%pB: warning: cannot determine the target function for"
8576
0
         " stub section `%s'"),
8577
0
       abfd, name);
8578
0
    bfd_set_error (bfd_error_bad_value);
8579
0
    return false;
8580
0
  }
8581
8582
0
      if (r_symndx < extsymoff
8583
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8584
0
  {
8585
0
    asection *o;
8586
8587
    /* This stub is for a local symbol.  This stub will only be
8588
       needed if there is some relocation (R_MIPS16_26) in this BFD
8589
       that refers to this symbol.  */
8590
0
    for (o = abfd->sections; o != NULL; o = o->next)
8591
0
      {
8592
0
        Elf_Internal_Rela *sec_relocs;
8593
0
        const Elf_Internal_Rela *r, *rend;
8594
8595
        /* We can ignore stub sections when looking for relocs.  */
8596
0
        if ((o->flags & SEC_RELOC) == 0
8597
0
      || o->reloc_count == 0
8598
0
      || section_allows_mips16_refs_p (o))
8599
0
    continue;
8600
8601
0
        sec_relocs
8602
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8603
0
               info->keep_memory);
8604
0
        if (sec_relocs == NULL)
8605
0
    return false;
8606
8607
0
        rend = sec_relocs + o->reloc_count;
8608
0
        for (r = sec_relocs; r < rend; r++)
8609
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8610
0
        && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8611
0
        break;
8612
8613
0
        if (elf_section_data (o)->relocs != sec_relocs)
8614
0
    free (sec_relocs);
8615
8616
0
        if (r < rend)
8617
0
    break;
8618
0
      }
8619
8620
0
    if (o == NULL)
8621
0
      {
8622
        /* There is no non-call reloc for this stub, so we do
8623
     not need it.  Since this function is called before
8624
     the linker maps input sections to output sections, we
8625
     can easily discard it by setting the SEC_EXCLUDE
8626
     flag.  */
8627
0
        sec->flags |= SEC_EXCLUDE;
8628
0
        return true;
8629
0
      }
8630
8631
    /* Record this stub in an array of local symbol call_stubs for
8632
       this BFD.  */
8633
0
    if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8634
0
      {
8635
0
        unsigned long symcount;
8636
0
        asection **n;
8637
0
        bfd_size_type amt;
8638
8639
0
        if (elf_bad_symtab (abfd))
8640
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8641
0
        else
8642
0
    symcount = symtab_hdr->sh_info;
8643
0
        amt = symcount * sizeof (asection *);
8644
0
        n = bfd_zalloc (abfd, amt);
8645
0
        if (n == NULL)
8646
0
    return false;
8647
0
        mips_elf_tdata (abfd)->local_call_stubs = n;
8648
0
      }
8649
8650
0
    sec->flags |= SEC_KEEP;
8651
0
    mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8652
8653
    /* We don't need to set mips16_stubs_seen in this case.
8654
       That flag is used to see whether we need to look through
8655
       the global symbol table for stubs.  We don't need to set
8656
       it here, because we just have a local stub.  */
8657
0
  }
8658
0
      else
8659
0
  {
8660
0
    h = ((struct mips_elf_link_hash_entry *)
8661
0
         sym_hashes[r_symndx - extsymoff]);
8662
8663
    /* H is the symbol this stub is for.  */
8664
8665
0
    if (CALL_FP_STUB_P (name))
8666
0
      loc = &h->call_fp_stub;
8667
0
    else
8668
0
      loc = &h->call_stub;
8669
8670
    /* If we already have an appropriate stub for this function, we
8671
       don't need another one, so we can discard this one.  Since
8672
       this function is called before the linker maps input sections
8673
       to output sections, we can easily discard it by setting the
8674
       SEC_EXCLUDE flag.  */
8675
0
    if (*loc != NULL)
8676
0
      {
8677
0
        sec->flags |= SEC_EXCLUDE;
8678
0
        return true;
8679
0
      }
8680
8681
0
    sec->flags |= SEC_KEEP;
8682
0
    *loc = sec;
8683
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8684
0
  }
8685
0
    }
8686
8687
0
  sreloc = NULL;
8688
0
  contents = NULL;
8689
0
  for (rel = relocs; rel < rel_end; ++rel)
8690
0
    {
8691
0
      unsigned long r_symndx;
8692
0
      unsigned int r_type;
8693
0
      struct elf_link_hash_entry *h;
8694
0
      bool can_make_dynamic_p;
8695
0
      bool call_reloc_p;
8696
0
      bool constrain_symbol_p;
8697
8698
0
      r_symndx = ELF_R_SYM (abfd, rel->r_info);
8699
0
      r_type = ELF_R_TYPE (abfd, rel->r_info);
8700
8701
0
      if (r_symndx < extsymoff)
8702
0
  h = NULL;
8703
0
      else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8704
0
  {
8705
0
    _bfd_error_handler
8706
      /* xgettext:c-format */
8707
0
      (_("%pB: malformed reloc detected for section %s"),
8708
0
       abfd, name);
8709
0
    bfd_set_error (bfd_error_bad_value);
8710
0
    return false;
8711
0
  }
8712
0
      else
8713
0
  {
8714
0
    h = sym_hashes[r_symndx - extsymoff];
8715
0
    if (h != NULL)
8716
0
      {
8717
0
        while (h->root.type == bfd_link_hash_indirect
8718
0
         || h->root.type == bfd_link_hash_warning)
8719
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8720
0
      }
8721
0
  }
8722
8723
      /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8724
   relocation into a dynamic one.  */
8725
0
      can_make_dynamic_p = false;
8726
8727
      /* Set CALL_RELOC_P to true if the relocation is for a call,
8728
   and if pointer equality therefore doesn't matter.  */
8729
0
      call_reloc_p = false;
8730
8731
      /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8732
   into account when deciding how to define the symbol.  */
8733
0
      constrain_symbol_p = true;
8734
8735
0
      switch (r_type)
8736
0
  {
8737
0
  case R_MIPS_CALL16:
8738
0
  case R_MIPS_CALL_HI16:
8739
0
  case R_MIPS_CALL_LO16:
8740
0
  case R_MIPS16_CALL16:
8741
0
  case R_MICROMIPS_CALL16:
8742
0
  case R_MICROMIPS_CALL_HI16:
8743
0
  case R_MICROMIPS_CALL_LO16:
8744
0
    call_reloc_p = true;
8745
    /* Fall through.  */
8746
8747
0
  case R_MIPS_GOT16:
8748
0
  case R_MIPS_GOT_LO16:
8749
0
  case R_MIPS_GOT_PAGE:
8750
0
  case R_MIPS_GOT_DISP:
8751
0
  case R_MIPS16_GOT16:
8752
0
  case R_MICROMIPS_GOT16:
8753
0
  case R_MICROMIPS_GOT_LO16:
8754
0
  case R_MICROMIPS_GOT_PAGE:
8755
0
  case R_MICROMIPS_GOT_DISP:
8756
    /* If we have a symbol that will resolve to zero at static link
8757
       time and it is used by a GOT relocation applied to code we
8758
       cannot relax to an immediate zero load, then we will be using
8759
       the special `__gnu_absolute_zero' symbol whose value is zero
8760
       at dynamic load time.  We ignore HI16-type GOT relocations at
8761
       this stage, because their handling will depend entirely on
8762
       the corresponding LO16-type GOT relocation.  */
8763
0
    if (!call_hi16_reloc_p (r_type)
8764
0
        && h != NULL
8765
0
        && bfd_link_pic (info)
8766
0
        && !htab->use_absolute_zero
8767
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8768
0
      {
8769
0
        bool rel_reloc;
8770
8771
0
        if (!mips_elf_get_section_contents (abfd, sec, &contents))
8772
0
    return false;
8773
8774
0
        rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8775
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8776
0
        if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8777
0
    if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8778
0
            false))
8779
0
      if (!mips_elf_define_absolute_zero (abfd, info, htab,
8780
0
                  r_type))
8781
0
        return false;
8782
0
      }
8783
8784
    /* Fall through.  */
8785
0
  case R_MIPS_GOT_HI16:
8786
0
  case R_MIPS_GOT_OFST:
8787
0
  case R_MIPS_TLS_GOTTPREL:
8788
0
  case R_MIPS_TLS_GD:
8789
0
  case R_MIPS_TLS_LDM:
8790
0
  case R_MIPS16_TLS_GOTTPREL:
8791
0
  case R_MIPS16_TLS_GD:
8792
0
  case R_MIPS16_TLS_LDM:
8793
0
  case R_MICROMIPS_GOT_HI16:
8794
0
  case R_MICROMIPS_GOT_OFST:
8795
0
  case R_MICROMIPS_TLS_GOTTPREL:
8796
0
  case R_MICROMIPS_TLS_GD:
8797
0
  case R_MICROMIPS_TLS_LDM:
8798
0
    if (dynobj == NULL)
8799
0
      elf_hash_table (info)->dynobj = dynobj = abfd;
8800
0
    if (!mips_elf_create_got_section (dynobj, info))
8801
0
      return false;
8802
0
    if (htab->root.target_os == is_vxworks
8803
0
        && !bfd_link_pic (info))
8804
0
      {
8805
0
        _bfd_error_handler
8806
    /* xgettext:c-format */
8807
0
    (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8808
0
     abfd, (uint64_t) rel->r_offset);
8809
0
        bfd_set_error (bfd_error_bad_value);
8810
0
        return false;
8811
0
      }
8812
0
    can_make_dynamic_p = true;
8813
0
    break;
8814
8815
0
  case R_MIPS_NONE:
8816
0
  case R_MIPS_JALR:
8817
0
  case R_MICROMIPS_JALR:
8818
    /* These relocations have empty fields and are purely there to
8819
       provide link information.  The symbol value doesn't matter.  */
8820
0
    constrain_symbol_p = false;
8821
0
    break;
8822
8823
0
  case R_MIPS_GPREL16:
8824
0
  case R_MIPS_GPREL32:
8825
0
  case R_MIPS16_GPREL:
8826
0
  case R_MICROMIPS_GPREL16:
8827
    /* GP-relative relocations always resolve to a definition in a
8828
       regular input file, ignoring the one-definition rule.  This is
8829
       important for the GP setup sequence in NewABI code, which
8830
       always resolves to a local function even if other relocations
8831
       against the symbol wouldn't.  */
8832
0
    constrain_symbol_p = false;
8833
0
    break;
8834
8835
0
  case R_MIPS_32:
8836
0
  case R_MIPS_REL32:
8837
0
  case R_MIPS_64:
8838
    /* In VxWorks executables, references to external symbols
8839
       must be handled using copy relocs or PLT entries; it is not
8840
       possible to convert this relocation into a dynamic one.
8841
8842
       For executables that use PLTs and copy-relocs, we have a
8843
       choice between converting the relocation into a dynamic
8844
       one or using copy relocations or PLT entries.  It is
8845
       usually better to do the former, unless the relocation is
8846
       against a read-only section.  */
8847
0
    if ((bfd_link_pic (info)
8848
0
         || (h != NULL
8849
0
       && htab->root.target_os != is_vxworks
8850
0
       && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8851
0
       && !(!info->nocopyreloc
8852
0
      && !PIC_OBJECT_P (abfd)
8853
0
      && MIPS_ELF_READONLY_SECTION (sec))))
8854
0
        && (sec->flags & SEC_ALLOC) != 0)
8855
0
      {
8856
0
        can_make_dynamic_p = true;
8857
0
        if (dynobj == NULL)
8858
0
    elf_hash_table (info)->dynobj = dynobj = abfd;
8859
0
      }
8860
0
    break;
8861
8862
0
  case R_MIPS_26:
8863
0
  case R_MIPS_PC16:
8864
0
  case R_MIPS_PC21_S2:
8865
0
  case R_MIPS_PC26_S2:
8866
0
  case R_MIPS16_26:
8867
0
  case R_MIPS16_PC16_S1:
8868
0
  case R_MICROMIPS_26_S1:
8869
0
  case R_MICROMIPS_PC7_S1:
8870
0
  case R_MICROMIPS_PC10_S1:
8871
0
  case R_MICROMIPS_PC16_S1:
8872
0
  case R_MICROMIPS_PC23_S2:
8873
0
    call_reloc_p = true;
8874
0
    break;
8875
0
  }
8876
8877
0
      if (h)
8878
0
  {
8879
0
    if (constrain_symbol_p)
8880
0
      {
8881
0
        if (!can_make_dynamic_p)
8882
0
    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8883
8884
0
        if (!call_reloc_p)
8885
0
    h->pointer_equality_needed = 1;
8886
8887
        /* We must not create a stub for a symbol that has
8888
     relocations related to taking the function's address.
8889
     This doesn't apply to VxWorks, where CALL relocs refer
8890
     to a .got.plt entry instead of a normal .got entry.  */
8891
0
        if (htab->root.target_os != is_vxworks
8892
0
      && (!can_make_dynamic_p || !call_reloc_p))
8893
0
    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8894
0
      }
8895
8896
    /* Relocations against the special VxWorks __GOTT_BASE__ and
8897
       __GOTT_INDEX__ symbols must be left to the loader.  Allocate
8898
       room for them in .rela.dyn.  */
8899
0
    if (is_gott_symbol (info, h))
8900
0
      {
8901
0
        if (sreloc == NULL)
8902
0
    {
8903
0
      sreloc = mips_elf_rel_dyn_section (info, true);
8904
0
      if (sreloc == NULL)
8905
0
        return false;
8906
0
    }
8907
0
        mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8908
0
        if (MIPS_ELF_READONLY_SECTION (sec))
8909
    /* We tell the dynamic linker that there are
8910
       relocations against the text segment.  */
8911
0
    info->flags |= DF_TEXTREL;
8912
0
      }
8913
0
  }
8914
0
      else if (call_lo16_reloc_p (r_type)
8915
0
         || got_lo16_reloc_p (r_type)
8916
0
         || got_disp_reloc_p (r_type)
8917
0
         || (got16_reloc_p (r_type)
8918
0
       && htab->root.target_os == is_vxworks))
8919
0
  {
8920
    /* We may need a local GOT entry for this relocation.  We
8921
       don't count R_MIPS_GOT_PAGE because we can estimate the
8922
       maximum number of pages needed by looking at the size of
8923
       the segment.  Similar comments apply to R_MIPS*_GOT16 and
8924
       R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8925
       always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
8926
       R_MIPS_CALL_HI16 because these are always followed by an
8927
       R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
8928
0
    if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8929
0
             rel->r_addend, info, r_type))
8930
0
      return false;
8931
0
  }
8932
8933
0
      if (h != NULL
8934
0
    && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8935
0
              ELF_ST_IS_MIPS16 (h->other)))
8936
0
  ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8937
8938
0
      switch (r_type)
8939
0
  {
8940
0
  case R_MIPS_CALL16:
8941
0
  case R_MIPS16_CALL16:
8942
0
  case R_MICROMIPS_CALL16:
8943
0
    if (h == NULL)
8944
0
      {
8945
0
        _bfd_error_handler
8946
    /* xgettext:c-format */
8947
0
    (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8948
0
     abfd, (uint64_t) rel->r_offset);
8949
0
        bfd_set_error (bfd_error_bad_value);
8950
0
        return false;
8951
0
      }
8952
    /* Fall through.  */
8953
8954
0
  case R_MIPS_CALL_HI16:
8955
0
  case R_MIPS_CALL_LO16:
8956
0
  case R_MICROMIPS_CALL_HI16:
8957
0
  case R_MICROMIPS_CALL_LO16:
8958
0
    if (h != NULL)
8959
0
      {
8960
        /* Make sure there is room in the regular GOT to hold the
8961
     function's address.  We may eliminate it in favour of
8962
     a .got.plt entry later; see mips_elf_count_got_symbols.  */
8963
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8964
0
                  r_type))
8965
0
    return false;
8966
8967
        /* We need a stub, not a plt entry for the undefined
8968
     function.  But we record it as if it needs plt.  See
8969
     _bfd_elf_adjust_dynamic_symbol.  */
8970
0
        h->needs_plt = 1;
8971
0
        h->type = STT_FUNC;
8972
0
      }
8973
0
    break;
8974
8975
0
  case R_MIPS_GOT_PAGE:
8976
0
  case R_MICROMIPS_GOT_PAGE:
8977
0
  case R_MIPS16_GOT16:
8978
0
  case R_MIPS_GOT16:
8979
0
  case R_MIPS_GOT_HI16:
8980
0
  case R_MIPS_GOT_LO16:
8981
0
  case R_MICROMIPS_GOT16:
8982
0
  case R_MICROMIPS_GOT_HI16:
8983
0
  case R_MICROMIPS_GOT_LO16:
8984
0
    if (!h || got_page_reloc_p (r_type))
8985
0
      {
8986
        /* This relocation needs (or may need, if h != NULL) a
8987
     page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
8988
     know for sure until we know whether the symbol is
8989
     preemptible.  */
8990
0
        if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8991
0
    {
8992
0
      if (!mips_elf_get_section_contents (abfd, sec, &contents))
8993
0
        return false;
8994
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
8995
0
      addend = mips_elf_read_rel_addend (abfd, sec, rel,
8996
0
                 howto, contents);
8997
0
      if (got16_reloc_p (r_type))
8998
0
        mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
8999
0
              contents, &addend);
9000
0
      else
9001
0
        addend <<= howto->rightshift;
9002
0
    }
9003
0
        else
9004
0
    addend = rel->r_addend;
9005
0
        if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9006
0
             h, addend))
9007
0
    return false;
9008
9009
0
        if (h)
9010
0
    {
9011
0
      struct mips_elf_link_hash_entry *hmips =
9012
0
        (struct mips_elf_link_hash_entry *) h;
9013
9014
      /* This symbol is definitely not overridable.  */
9015
0
      if (hmips->root.def_regular
9016
0
          && ! (bfd_link_pic (info) && ! info->symbolic
9017
0
          && ! hmips->root.forced_local))
9018
0
        h = NULL;
9019
0
    }
9020
0
      }
9021
    /* If this is a global, overridable symbol, GOT_PAGE will
9022
       decay to GOT_DISP, so we'll need a GOT entry for it.  */
9023
    /* Fall through.  */
9024
9025
0
  case R_MIPS_GOT_DISP:
9026
0
  case R_MICROMIPS_GOT_DISP:
9027
0
    if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9028
0
                   false, r_type))
9029
0
      return false;
9030
0
    break;
9031
9032
0
  case R_MIPS_TLS_GOTTPREL:
9033
0
  case R_MIPS16_TLS_GOTTPREL:
9034
0
  case R_MICROMIPS_TLS_GOTTPREL:
9035
0
    if (bfd_link_pic (info))
9036
0
      info->flags |= DF_STATIC_TLS;
9037
    /* Fall through */
9038
9039
0
  case R_MIPS_TLS_LDM:
9040
0
  case R_MIPS16_TLS_LDM:
9041
0
  case R_MICROMIPS_TLS_LDM:
9042
0
    if (tls_ldm_reloc_p (r_type))
9043
0
      {
9044
0
        r_symndx = STN_UNDEF;
9045
0
        h = NULL;
9046
0
      }
9047
    /* Fall through */
9048
9049
0
  case R_MIPS_TLS_GD:
9050
0
  case R_MIPS16_TLS_GD:
9051
0
  case R_MICROMIPS_TLS_GD:
9052
    /* This symbol requires a global offset table entry, or two
9053
       for TLS GD relocations.  */
9054
0
    if (h != NULL)
9055
0
      {
9056
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info,
9057
0
                  false, r_type))
9058
0
    return false;
9059
0
      }
9060
0
    else
9061
0
      {
9062
0
        if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9063
0
                 rel->r_addend,
9064
0
                 info, r_type))
9065
0
    return false;
9066
0
      }
9067
0
    break;
9068
9069
0
  case R_MIPS_32:
9070
0
  case R_MIPS_REL32:
9071
0
  case R_MIPS_64:
9072
    /* In VxWorks executables, references to external symbols
9073
       are handled using copy relocs or PLT stubs, so there's
9074
       no need to add a .rela.dyn entry for this relocation.  */
9075
0
    if (can_make_dynamic_p)
9076
0
      {
9077
0
        if (sreloc == NULL)
9078
0
    {
9079
0
      sreloc = mips_elf_rel_dyn_section (info, true);
9080
0
      if (sreloc == NULL)
9081
0
        return false;
9082
0
    }
9083
0
        if (bfd_link_pic (info) && h == NULL)
9084
0
    {
9085
      /* When creating a shared object, we must copy these
9086
         reloc types into the output file as R_MIPS_REL32
9087
         relocs.  Make room for this reloc in .rel(a).dyn.  */
9088
0
      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9089
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9090
        /* We tell the dynamic linker that there are
9091
           relocations against the text segment.  */
9092
0
        info->flags |= DF_TEXTREL;
9093
0
    }
9094
0
        else
9095
0
    {
9096
0
      struct mips_elf_link_hash_entry *hmips;
9097
9098
      /* For a shared object, we must copy this relocation
9099
         unless the symbol turns out to be undefined and
9100
         weak with non-default visibility, in which case
9101
         it will be left as zero.
9102
9103
         We could elide R_MIPS_REL32 for locally binding symbols
9104
         in shared libraries, but do not yet do so.
9105
9106
         For an executable, we only need to copy this
9107
         reloc if the symbol is defined in a dynamic
9108
         object.  */
9109
0
      hmips = (struct mips_elf_link_hash_entry *) h;
9110
0
      ++hmips->possibly_dynamic_relocs;
9111
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9112
        /* We need it to tell the dynamic linker if there
9113
           are relocations against the text segment.  */
9114
0
        hmips->readonly_reloc = true;
9115
0
    }
9116
0
      }
9117
9118
0
    if (SGI_COMPAT (abfd))
9119
0
      mips_elf_hash_table (info)->compact_rel_size +=
9120
0
        sizeof (Elf32_External_crinfo);
9121
0
    break;
9122
9123
0
  case R_MIPS_26:
9124
0
  case R_MIPS_GPREL16:
9125
0
  case R_MIPS_LITERAL:
9126
0
  case R_MIPS_GPREL32:
9127
0
  case R_MICROMIPS_26_S1:
9128
0
  case R_MICROMIPS_GPREL16:
9129
0
  case R_MICROMIPS_LITERAL:
9130
0
  case R_MICROMIPS_GPREL7_S2:
9131
0
    if (SGI_COMPAT (abfd))
9132
0
      mips_elf_hash_table (info)->compact_rel_size +=
9133
0
        sizeof (Elf32_External_crinfo);
9134
0
    break;
9135
9136
    /* This relocation describes the C++ object vtable hierarchy.
9137
       Reconstruct it for later use during GC.  */
9138
0
  case R_MIPS_GNU_VTINHERIT:
9139
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9140
0
      return false;
9141
0
    break;
9142
9143
    /* This relocation describes which C++ vtable entries are actually
9144
       used.  Record for later use during GC.  */
9145
0
  case R_MIPS_GNU_VTENTRY:
9146
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9147
0
      return false;
9148
0
    break;
9149
9150
0
  default:
9151
0
    break;
9152
0
  }
9153
9154
      /* Record the need for a PLT entry.  At this point we don't know
9155
   yet if we are going to create a PLT in the first place, but
9156
   we only record whether the relocation requires a standard MIPS
9157
   or a compressed code entry anyway.  If we don't make a PLT after
9158
   all, then we'll just ignore these arrangements.  Likewise if
9159
   a PLT entry is not created because the symbol is satisfied
9160
   locally.  */
9161
0
      if (h != NULL
9162
0
    && (branch_reloc_p (r_type)
9163
0
        || mips16_branch_reloc_p (r_type)
9164
0
        || micromips_branch_reloc_p (r_type))
9165
0
    && !SYMBOL_CALLS_LOCAL (info, h))
9166
0
  {
9167
0
    if (h->plt.plist == NULL)
9168
0
      h->plt.plist = mips_elf_make_plt_record (abfd);
9169
0
    if (h->plt.plist == NULL)
9170
0
      return false;
9171
9172
0
    if (branch_reloc_p (r_type))
9173
0
      h->plt.plist->need_mips = true;
9174
0
    else
9175
0
      h->plt.plist->need_comp = true;
9176
0
  }
9177
9178
      /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9179
   if there is one.  We only need to handle global symbols here;
9180
   we decide whether to keep or delete stubs for local symbols
9181
   when processing the stub's relocations.  */
9182
0
      if (h != NULL
9183
0
    && !mips16_call_reloc_p (r_type)
9184
0
    && !section_allows_mips16_refs_p (sec))
9185
0
  {
9186
0
    struct mips_elf_link_hash_entry *mh;
9187
9188
0
    mh = (struct mips_elf_link_hash_entry *) h;
9189
0
    mh->need_fn_stub = true;
9190
0
  }
9191
9192
      /* Refuse some position-dependent relocations when creating a
9193
   shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
9194
   not PIC, but we can create dynamic relocations and the result
9195
   will be fine.  Also do not refuse R_MIPS_LO16, which can be
9196
   combined with R_MIPS_GOT16.  */
9197
0
      if (bfd_link_pic (info))
9198
0
  {
9199
0
    switch (r_type)
9200
0
      {
9201
0
      case R_MIPS_TLS_TPREL_HI16:
9202
0
      case R_MIPS16_TLS_TPREL_HI16:
9203
0
      case R_MICROMIPS_TLS_TPREL_HI16:
9204
0
      case R_MIPS_TLS_TPREL_LO16:
9205
0
      case R_MIPS16_TLS_TPREL_LO16:
9206
0
      case R_MICROMIPS_TLS_TPREL_LO16:
9207
        /* These are okay in PIE, but not in a shared library.  */
9208
0
        if (bfd_link_executable (info))
9209
0
    break;
9210
9211
        /* FALLTHROUGH */
9212
9213
0
      case R_MIPS16_HI16:
9214
0
      case R_MIPS_HI16:
9215
0
      case R_MIPS_HIGHER:
9216
0
      case R_MIPS_HIGHEST:
9217
0
      case R_MICROMIPS_HI16:
9218
0
      case R_MICROMIPS_HIGHER:
9219
0
      case R_MICROMIPS_HIGHEST:
9220
        /* Don't refuse a high part relocation if it's against
9221
     no symbol (e.g. part of a compound relocation).  */
9222
0
        if (r_symndx == STN_UNDEF)
9223
0
    break;
9224
9225
        /* Likewise an absolute symbol.  */
9226
0
        if (h != NULL && bfd_is_abs_symbol (&h->root))
9227
0
    break;
9228
9229
        /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9230
     and has a special meaning.  */
9231
0
        if (!NEWABI_P (abfd) && h != NULL
9232
0
      && strcmp (h->root.root.string, "_gp_disp") == 0)
9233
0
    break;
9234
9235
        /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
9236
0
        if (is_gott_symbol (info, h))
9237
0
    break;
9238
9239
        /* FALLTHROUGH */
9240
9241
0
      case R_MIPS16_26:
9242
0
      case R_MIPS_26:
9243
0
      case R_MICROMIPS_26_S1:
9244
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9245
        /* An error for unsupported relocations is raised as part
9246
     of the above search, so we can skip the following.  */
9247
0
        if (howto != NULL)
9248
0
    info->callbacks->einfo
9249
      /* xgettext:c-format */
9250
0
      (_("%X%H: relocation %s against `%s' cannot be used"
9251
0
         " when making a shared object; recompile with -fPIC\n"),
9252
0
       abfd, sec, rel->r_offset, howto->name,
9253
0
       (h) ? h->root.root.string : "a local symbol");
9254
0
        break;
9255
0
      default:
9256
0
        break;
9257
0
      }
9258
0
  }
9259
0
    }
9260
9261
0
  return true;
9262
0
}
9263

9264
/* Allocate space for global sym dynamic relocs.  */
9265
9266
static bool
9267
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9268
0
{
9269
0
  struct bfd_link_info *info = inf;
9270
0
  bfd *dynobj;
9271
0
  struct mips_elf_link_hash_entry *hmips;
9272
0
  struct mips_elf_link_hash_table *htab;
9273
9274
0
  htab = mips_elf_hash_table (info);
9275
0
  BFD_ASSERT (htab != NULL);
9276
9277
0
  dynobj = elf_hash_table (info)->dynobj;
9278
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9279
9280
  /* VxWorks executables are handled elsewhere; we only need to
9281
     allocate relocations in shared objects.  */
9282
0
  if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9283
0
    return true;
9284
9285
  /* Ignore indirect symbols.  All relocations against such symbols
9286
     will be redirected to the target symbol.  */
9287
0
  if (h->root.type == bfd_link_hash_indirect)
9288
0
    return true;
9289
9290
  /* If this symbol is defined in a dynamic object, or we are creating
9291
     a shared library, we will need to copy any R_MIPS_32 or
9292
     R_MIPS_REL32 relocs against it into the output file.  */
9293
0
  if (! bfd_link_relocatable (info)
9294
0
      && hmips->possibly_dynamic_relocs != 0
9295
0
      && (h->root.type == bfd_link_hash_defweak
9296
0
    || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9297
0
    || bfd_link_pic (info)))
9298
0
    {
9299
0
      bool do_copy = true;
9300
9301
0
      if (h->root.type == bfd_link_hash_undefweak)
9302
0
  {
9303
    /* Do not copy relocations for undefined weak symbols that
9304
       we are not going to export.  */
9305
0
    if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9306
0
      do_copy = false;
9307
9308
    /* Make sure undefined weak symbols are output as a dynamic
9309
       symbol in PIEs.  */
9310
0
    else if (h->dynindx == -1 && !h->forced_local)
9311
0
      {
9312
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
9313
0
    return false;
9314
0
      }
9315
0
  }
9316
9317
0
      if (do_copy)
9318
0
  {
9319
    /* Even though we don't directly need a GOT entry for this symbol,
9320
       the SVR4 psABI requires it to have a dynamic symbol table
9321
       index greater that DT_MIPS_GOTSYM if there are dynamic
9322
       relocations against it.
9323
9324
       VxWorks does not enforce the same mapping between the GOT
9325
       and the symbol table, so the same requirement does not
9326
       apply there.  */
9327
0
    if (htab->root.target_os != is_vxworks)
9328
0
      {
9329
0
        if (hmips->global_got_area > GGA_RELOC_ONLY)
9330
0
    hmips->global_got_area = GGA_RELOC_ONLY;
9331
0
        hmips->got_only_for_calls = false;
9332
0
      }
9333
9334
0
    mips_elf_allocate_dynamic_relocations
9335
0
      (dynobj, info, hmips->possibly_dynamic_relocs);
9336
0
    if (hmips->readonly_reloc)
9337
      /* We tell the dynamic linker that there are relocations
9338
         against the text segment.  */
9339
0
      info->flags |= DF_TEXTREL;
9340
0
  }
9341
0
    }
9342
9343
0
  return true;
9344
0
}
9345
9346
/* Adjust a symbol defined by a dynamic object and referenced by a
9347
   regular object.  The current definition is in some section of the
9348
   dynamic object, but we're not including those sections.  We have to
9349
   change the definition to something the rest of the link can
9350
   understand.  */
9351
9352
bool
9353
_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9354
             struct elf_link_hash_entry *h)
9355
0
{
9356
0
  bfd *dynobj;
9357
0
  struct mips_elf_link_hash_entry *hmips;
9358
0
  struct mips_elf_link_hash_table *htab;
9359
0
  asection *s, *srel;
9360
9361
0
  htab = mips_elf_hash_table (info);
9362
0
  BFD_ASSERT (htab != NULL);
9363
9364
0
  dynobj = elf_hash_table (info)->dynobj;
9365
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9366
9367
  /* Make sure we know what is going on here.  */
9368
0
  if (dynobj == NULL
9369
0
      || (! h->needs_plt
9370
0
    && ! h->is_weakalias
9371
0
    && (! h->def_dynamic
9372
0
        || ! h->ref_regular
9373
0
        || h->def_regular)))
9374
0
    {
9375
0
      if (h->type == STT_GNU_IFUNC)
9376
0
  _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9377
0
          h->root.root.string);
9378
0
      else
9379
0
  _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9380
0
          h->root.root.string);
9381
0
      return true;
9382
0
    }
9383
9384
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9385
9386
  /* If there are call relocations against an externally-defined symbol,
9387
     see whether we can create a MIPS lazy-binding stub for it.  We can
9388
     only do this if all references to the function are through call
9389
     relocations, and in that case, the traditional lazy-binding stubs
9390
     are much more efficient than PLT entries.
9391
9392
     Traditional stubs are only available on SVR4 psABI-based systems;
9393
     VxWorks always uses PLTs instead.  */
9394
0
  if (htab->root.target_os != is_vxworks
9395
0
      && h->needs_plt
9396
0
      && !hmips->no_fn_stub)
9397
0
    {
9398
0
      if (! elf_hash_table (info)->dynamic_sections_created)
9399
0
  return true;
9400
9401
      /* If this symbol is not defined in a regular file, then set
9402
   the symbol to the stub location.  This is required to make
9403
   function pointers compare as equal between the normal
9404
   executable and the shared library.  */
9405
0
      if (!h->def_regular
9406
0
    && !bfd_is_abs_section (htab->sstubs->output_section))
9407
0
  {
9408
0
    hmips->needs_lazy_stub = true;
9409
0
    htab->lazy_stub_count++;
9410
0
    return true;
9411
0
  }
9412
0
    }
9413
  /* As above, VxWorks requires PLT entries for externally-defined
9414
     functions that are only accessed through call relocations.
9415
9416
     Both VxWorks and non-VxWorks targets also need PLT entries if there
9417
     are static-only relocations against an externally-defined function.
9418
     This can technically occur for shared libraries if there are
9419
     branches to the symbol, although it is unlikely that this will be
9420
     used in practice due to the short ranges involved.  It can occur
9421
     for any relative or absolute relocation in executables; in that
9422
     case, the PLT entry becomes the function's canonical address.  */
9423
0
  else if (((h->needs_plt && !hmips->no_fn_stub)
9424
0
      || (h->type == STT_FUNC && hmips->has_static_relocs))
9425
0
     && htab->use_plts_and_copy_relocs
9426
0
     && !SYMBOL_CALLS_LOCAL (info, h)
9427
0
     && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9428
0
    && h->root.type == bfd_link_hash_undefweak))
9429
0
    {
9430
0
      bool micromips_p = MICROMIPS_P (info->output_bfd);
9431
0
      bool newabi_p = NEWABI_P (info->output_bfd);
9432
9433
      /* If this is the first symbol to need a PLT entry, then make some
9434
   basic setup.  Also work out PLT entry sizes.  We'll need them
9435
   for PLT offset calculations.  */
9436
0
      if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9437
0
  {
9438
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
9439
0
    BFD_ASSERT (htab->plt_got_index == 0);
9440
9441
    /* If we're using the PLT additions to the psABI, each PLT
9442
       entry is 16 bytes and the PLT0 entry is 32 bytes.
9443
       Encourage better cache usage by aligning.  We do this
9444
       lazily to avoid pessimizing traditional objects.  */
9445
0
    if (htab->root.target_os != is_vxworks
9446
0
        && !bfd_set_section_alignment (htab->root.splt, 5))
9447
0
      return false;
9448
9449
    /* Make sure that .got.plt is word-aligned.  We do this lazily
9450
       for the same reason as above.  */
9451
0
    if (!bfd_set_section_alignment (htab->root.sgotplt,
9452
0
            MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9453
0
      return false;
9454
9455
    /* On non-VxWorks targets, the first two entries in .got.plt
9456
       are reserved.  */
9457
0
    if (htab->root.target_os != is_vxworks)
9458
0
      htab->plt_got_index
9459
0
        += (get_elf_backend_data (dynobj)->got_header_size
9460
0
      / MIPS_ELF_GOT_SIZE (dynobj));
9461
9462
    /* On VxWorks, also allocate room for the header's
9463
       .rela.plt.unloaded entries.  */
9464
0
    if (htab->root.target_os == is_vxworks
9465
0
        && !bfd_link_pic (info))
9466
0
      htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9467
9468
    /* Now work out the sizes of individual PLT entries.  */
9469
0
    if (htab->root.target_os == is_vxworks
9470
0
        && bfd_link_pic (info))
9471
0
      htab->plt_mips_entry_size
9472
0
        = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9473
0
    else if (htab->root.target_os == is_vxworks)
9474
0
      htab->plt_mips_entry_size
9475
0
        = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9476
0
    else if (newabi_p)
9477
0
      htab->plt_mips_entry_size
9478
0
        = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9479
0
    else if (!micromips_p)
9480
0
      {
9481
0
        htab->plt_mips_entry_size
9482
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9483
0
        htab->plt_comp_entry_size
9484
0
    = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9485
0
      }
9486
0
    else if (htab->insn32)
9487
0
      {
9488
0
        htab->plt_mips_entry_size
9489
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9490
0
        htab->plt_comp_entry_size
9491
0
    = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9492
0
      }
9493
0
    else
9494
0
      {
9495
0
        htab->plt_mips_entry_size
9496
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9497
0
        htab->plt_comp_entry_size
9498
0
    = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9499
0
      }
9500
0
  }
9501
9502
0
      if (h->plt.plist == NULL)
9503
0
  h->plt.plist = mips_elf_make_plt_record (dynobj);
9504
0
      if (h->plt.plist == NULL)
9505
0
  return false;
9506
9507
      /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9508
   n32 or n64, so always use a standard entry there.
9509
9510
   If the symbol has a MIPS16 call stub and gets a PLT entry, then
9511
   all MIPS16 calls will go via that stub, and there is no benefit
9512
   to having a MIPS16 entry.  And in the case of call_stub a
9513
   standard entry actually has to be used as the stub ends with a J
9514
   instruction.  */
9515
0
      if (newabi_p
9516
0
    || htab->root.target_os == is_vxworks
9517
0
    || hmips->call_stub
9518
0
    || hmips->call_fp_stub)
9519
0
  {
9520
0
    h->plt.plist->need_mips = true;
9521
0
    h->plt.plist->need_comp = false;
9522
0
  }
9523
9524
      /* Otherwise, if there are no direct calls to the function, we
9525
   have a free choice of whether to use standard or compressed
9526
   entries.  Prefer microMIPS entries if the object is known to
9527
   contain microMIPS code, so that it becomes possible to create
9528
   pure microMIPS binaries.  Prefer standard entries otherwise,
9529
   because MIPS16 ones are no smaller and are usually slower.  */
9530
0
      if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9531
0
  {
9532
0
    if (micromips_p)
9533
0
      h->plt.plist->need_comp = true;
9534
0
    else
9535
0
      h->plt.plist->need_mips = true;
9536
0
  }
9537
9538
0
      if (h->plt.plist->need_mips)
9539
0
  {
9540
0
    h->plt.plist->mips_offset = htab->plt_mips_offset;
9541
0
    htab->plt_mips_offset += htab->plt_mips_entry_size;
9542
0
  }
9543
0
      if (h->plt.plist->need_comp)
9544
0
  {
9545
0
    h->plt.plist->comp_offset = htab->plt_comp_offset;
9546
0
    htab->plt_comp_offset += htab->plt_comp_entry_size;
9547
0
  }
9548
9549
      /* Reserve the corresponding .got.plt entry now too.  */
9550
0
      h->plt.plist->gotplt_index = htab->plt_got_index++;
9551
9552
      /* If the output file has no definition of the symbol, set the
9553
   symbol's value to the address of the stub.  */
9554
0
      if (!bfd_link_pic (info) && !h->def_regular)
9555
0
  hmips->use_plt_entry = true;
9556
9557
      /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
9558
0
      htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9559
0
           ? MIPS_ELF_RELA_SIZE (dynobj)
9560
0
           : MIPS_ELF_REL_SIZE (dynobj));
9561
9562
      /* Make room for the .rela.plt.unloaded relocations.  */
9563
0
      if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9564
0
  htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9565
9566
      /* All relocations against this symbol that could have been made
9567
   dynamic will now refer to the PLT entry instead.  */
9568
0
      hmips->possibly_dynamic_relocs = 0;
9569
9570
0
      return true;
9571
0
    }
9572
9573
  /* If this is a weak symbol, and there is a real definition, the
9574
     processor independent code will have arranged for us to see the
9575
     real definition first, and we can just use the same value.  */
9576
0
  if (h->is_weakalias)
9577
0
    {
9578
0
      struct elf_link_hash_entry *def = weakdef (h);
9579
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9580
0
      h->root.u.def.section = def->root.u.def.section;
9581
0
      h->root.u.def.value = def->root.u.def.value;
9582
0
      return true;
9583
0
    }
9584
9585
  /* Otherwise, there is nothing further to do for symbols defined
9586
     in regular objects.  */
9587
0
  if (h->def_regular)
9588
0
    return true;
9589
9590
  /* There's also nothing more to do if we'll convert all relocations
9591
     against this symbol into dynamic relocations.  */
9592
0
  if (!hmips->has_static_relocs)
9593
0
    return true;
9594
9595
  /* We're now relying on copy relocations.  Complain if we have
9596
     some that we can't convert.  */
9597
0
  if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9598
0
    {
9599
0
      _bfd_error_handler (_("non-dynamic relocations refer to "
9600
0
          "dynamic symbol %s"),
9601
0
        h->root.root.string);
9602
0
      bfd_set_error (bfd_error_bad_value);
9603
0
      return false;
9604
0
    }
9605
9606
  /* We must allocate the symbol in our .dynbss section, which will
9607
     become part of the .bss section of the executable.  There will be
9608
     an entry for this symbol in the .dynsym section.  The dynamic
9609
     object will contain position independent code, so all references
9610
     from the dynamic object to this symbol will go through the global
9611
     offset table.  The dynamic linker will use the .dynsym entry to
9612
     determine the address it must put in the global offset table, so
9613
     both the dynamic object and the regular object will refer to the
9614
     same memory location for the variable.  */
9615
9616
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9617
0
    {
9618
0
      s = htab->root.sdynrelro;
9619
0
      srel = htab->root.sreldynrelro;
9620
0
    }
9621
0
  else
9622
0
    {
9623
0
      s = htab->root.sdynbss;
9624
0
      srel = htab->root.srelbss;
9625
0
    }
9626
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9627
0
    {
9628
0
      if (htab->root.target_os == is_vxworks)
9629
0
  srel->size += sizeof (Elf32_External_Rela);
9630
0
      else
9631
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9632
0
      h->needs_copy = 1;
9633
0
    }
9634
9635
  /* All relocations against this symbol that could have been made
9636
     dynamic will now refer to the local copy instead.  */
9637
0
  hmips->possibly_dynamic_relocs = 0;
9638
9639
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
9640
0
}
9641

9642
/* This function is called after all the input files have been read,
9643
   and the input sections have been assigned to output sections.  We
9644
   check for any mips16 stub sections that we can discard.  */
9645
9646
bool
9647
_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9648
            struct bfd_link_info *info)
9649
0
{
9650
0
  asection *sect;
9651
0
  struct mips_elf_link_hash_table *htab;
9652
0
  struct mips_htab_traverse_info hti;
9653
9654
0
  htab = mips_elf_hash_table (info);
9655
0
  BFD_ASSERT (htab != NULL);
9656
9657
  /* The .reginfo section has a fixed size.  */
9658
0
  sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9659
0
  if (sect != NULL)
9660
0
    {
9661
0
      bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
9662
0
      sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9663
0
    }
9664
9665
  /* The .MIPS.abiflags section has a fixed size.  */
9666
0
  sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9667
0
  if (sect != NULL)
9668
0
    {
9669
0
      bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
9670
0
      sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9671
0
    }
9672
9673
0
  hti.info = info;
9674
0
  hti.output_bfd = output_bfd;
9675
0
  hti.error = false;
9676
0
  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9677
0
             mips_elf_check_symbols, &hti);
9678
0
  if (hti.error)
9679
0
    return false;
9680
9681
0
  return true;
9682
0
}
9683
9684
/* If the link uses a GOT, lay it out and work out its size.  */
9685
9686
static bool
9687
mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9688
0
{
9689
0
  bfd *dynobj;
9690
0
  asection *s;
9691
0
  struct mips_got_info *g;
9692
0
  bfd_size_type loadable_size = 0;
9693
0
  bfd_size_type page_gotno;
9694
0
  bfd *ibfd;
9695
0
  struct mips_elf_traverse_got_arg tga;
9696
0
  struct mips_elf_link_hash_table *htab;
9697
9698
0
  htab = mips_elf_hash_table (info);
9699
0
  BFD_ASSERT (htab != NULL);
9700
9701
0
  s = htab->root.sgot;
9702
0
  if (s == NULL)
9703
0
    return true;
9704
9705
0
  dynobj = elf_hash_table (info)->dynobj;
9706
0
  g = htab->got_info;
9707
9708
  /* Allocate room for the reserved entries.  VxWorks always reserves
9709
     3 entries; other objects only reserve 2 entries.  */
9710
0
  BFD_ASSERT (g->assigned_low_gotno == 0);
9711
0
  if (htab->root.target_os == is_vxworks)
9712
0
    htab->reserved_gotno = 3;
9713
0
  else
9714
0
    htab->reserved_gotno = 2;
9715
0
  g->local_gotno += htab->reserved_gotno;
9716
0
  g->assigned_low_gotno = htab->reserved_gotno;
9717
9718
  /* Decide which symbols need to go in the global part of the GOT and
9719
     count the number of reloc-only GOT symbols.  */
9720
0
  mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9721
9722
0
  if (!mips_elf_resolve_final_got_entries (info, g))
9723
0
    return false;
9724
9725
  /* Calculate the total loadable size of the output.  That
9726
     will give us the maximum number of GOT_PAGE entries
9727
     required.  */
9728
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9729
0
    {
9730
0
      asection *subsection;
9731
9732
0
      for (subsection = ibfd->sections;
9733
0
     subsection;
9734
0
     subsection = subsection->next)
9735
0
  {
9736
0
    if ((subsection->flags & SEC_ALLOC) == 0)
9737
0
      continue;
9738
0
    loadable_size += ((subsection->size + 0xf)
9739
0
          &~ (bfd_size_type) 0xf);
9740
0
  }
9741
0
    }
9742
9743
0
  if (htab->root.target_os == is_vxworks)
9744
    /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9745
       relocations against local symbols evaluate to "G", and the EABI does
9746
       not include R_MIPS_GOT_PAGE.  */
9747
0
    page_gotno = 0;
9748
0
  else
9749
    /* Assume there are two loadable segments consisting of contiguous
9750
       sections.  Is 5 enough?  */
9751
0
    page_gotno = (loadable_size >> 16) + 5;
9752
9753
  /* Choose the smaller of the two page estimates; both are intended to be
9754
     conservative.  */
9755
0
  if (page_gotno > g->page_gotno)
9756
0
    page_gotno = g->page_gotno;
9757
9758
0
  g->local_gotno += page_gotno;
9759
0
  g->assigned_high_gotno = g->local_gotno - 1;
9760
9761
0
  s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9762
0
  s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9763
0
  s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9764
9765
  /* VxWorks does not support multiple GOTs.  It initializes $gp to
9766
     __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9767
     dynamic loader.  */
9768
0
  if (htab->root.target_os != is_vxworks
9769
0
      && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9770
0
    {
9771
0
      if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9772
0
  return false;
9773
0
    }
9774
0
  else
9775
0
    {
9776
      /* Record that all bfds use G.  This also has the effect of freeing
9777
   the per-bfd GOTs, which we no longer need.  */
9778
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9779
0
  if (mips_elf_bfd_got (ibfd, false))
9780
0
    mips_elf_replace_bfd_got (ibfd, g);
9781
0
      mips_elf_replace_bfd_got (output_bfd, g);
9782
9783
      /* Set up TLS entries.  */
9784
0
      g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9785
0
      tga.info = info;
9786
0
      tga.g = g;
9787
0
      tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9788
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9789
0
      if (!tga.g)
9790
0
  return false;
9791
0
      BFD_ASSERT (g->tls_assigned_gotno
9792
0
      == g->global_gotno + g->local_gotno + g->tls_gotno);
9793
9794
      /* Each VxWorks GOT entry needs an explicit relocation.  */
9795
0
      if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9796
0
  g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9797
9798
      /* Allocate room for the TLS relocations.  */
9799
0
      if (g->relocs)
9800
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9801
0
    }
9802
9803
0
  return true;
9804
0
}
9805
9806
/* Estimate the size of the .MIPS.stubs section.  */
9807
9808
static void
9809
mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9810
0
{
9811
0
  struct mips_elf_link_hash_table *htab;
9812
0
  bfd_size_type dynsymcount;
9813
9814
0
  htab = mips_elf_hash_table (info);
9815
0
  BFD_ASSERT (htab != NULL);
9816
9817
0
  if (htab->lazy_stub_count == 0)
9818
0
    return;
9819
9820
  /* IRIX rld assumes that a function stub isn't at the end of the .text
9821
     section, so add a dummy entry to the end.  */
9822
0
  htab->lazy_stub_count++;
9823
9824
  /* Get a worst-case estimate of the number of dynamic symbols needed.
9825
     At this point, dynsymcount does not account for section symbols
9826
     and count_section_dynsyms may overestimate the number that will
9827
     be needed.  */
9828
0
  dynsymcount = (elf_hash_table (info)->dynsymcount
9829
0
     + count_section_dynsyms (output_bfd, info));
9830
9831
  /* Determine the size of one stub entry.  There's no disadvantage
9832
     from using microMIPS code here, so for the sake of pure-microMIPS
9833
     binaries we prefer it whenever there's any microMIPS code in
9834
     output produced at all.  This has a benefit of stubs being
9835
     shorter by 4 bytes each too, unless in the insn32 mode.  */
9836
0
  if (!MICROMIPS_P (output_bfd))
9837
0
    htab->function_stub_size = (dynsymcount > 0x10000
9838
0
        ? MIPS_FUNCTION_STUB_BIG_SIZE
9839
0
        : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9840
0
  else if (htab->insn32)
9841
0
    htab->function_stub_size = (dynsymcount > 0x10000
9842
0
        ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9843
0
        : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9844
0
  else
9845
0
    htab->function_stub_size = (dynsymcount > 0x10000
9846
0
        ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9847
0
        : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9848
9849
0
  htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9850
0
}
9851
9852
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9853
   mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
9854
   stub, allocate an entry in the stubs section.  */
9855
9856
static bool
9857
mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9858
0
{
9859
0
  struct mips_htab_traverse_info *hti = data;
9860
0
  struct mips_elf_link_hash_table *htab;
9861
0
  struct bfd_link_info *info;
9862
0
  bfd *output_bfd;
9863
9864
0
  info = hti->info;
9865
0
  output_bfd = hti->output_bfd;
9866
0
  htab = mips_elf_hash_table (info);
9867
0
  BFD_ASSERT (htab != NULL);
9868
9869
0
  if (h->needs_lazy_stub)
9870
0
    {
9871
0
      bool micromips_p = MICROMIPS_P (output_bfd);
9872
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9873
0
      bfd_vma isa_bit = micromips_p;
9874
9875
0
      BFD_ASSERT (htab->root.dynobj != NULL);
9876
0
      if (h->root.plt.plist == NULL)
9877
0
  h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9878
0
      if (h->root.plt.plist == NULL)
9879
0
  {
9880
0
    hti->error = true;
9881
0
    return false;
9882
0
  }
9883
0
      h->root.root.u.def.section = htab->sstubs;
9884
0
      h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9885
0
      h->root.plt.plist->stub_offset = htab->sstubs->size;
9886
0
      h->root.other = other;
9887
0
      htab->sstubs->size += htab->function_stub_size;
9888
0
    }
9889
0
  return true;
9890
0
}
9891
9892
/* Allocate offsets in the stubs section to each symbol that needs one.
9893
   Set the final size of the .MIPS.stub section.  */
9894
9895
static bool
9896
mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9897
0
{
9898
0
  bfd *output_bfd = info->output_bfd;
9899
0
  bool micromips_p = MICROMIPS_P (output_bfd);
9900
0
  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9901
0
  bfd_vma isa_bit = micromips_p;
9902
0
  struct mips_elf_link_hash_table *htab;
9903
0
  struct mips_htab_traverse_info hti;
9904
0
  struct elf_link_hash_entry *h;
9905
0
  bfd *dynobj;
9906
9907
0
  htab = mips_elf_hash_table (info);
9908
0
  BFD_ASSERT (htab != NULL);
9909
9910
0
  if (htab->lazy_stub_count == 0)
9911
0
    return true;
9912
9913
0
  htab->sstubs->size = 0;
9914
0
  hti.info = info;
9915
0
  hti.output_bfd = output_bfd;
9916
0
  hti.error = false;
9917
0
  mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9918
0
  if (hti.error)
9919
0
    return false;
9920
0
  htab->sstubs->size += htab->function_stub_size;
9921
0
  BFD_ASSERT (htab->sstubs->size
9922
0
        == htab->lazy_stub_count * htab->function_stub_size);
9923
9924
0
  dynobj = elf_hash_table (info)->dynobj;
9925
0
  BFD_ASSERT (dynobj != NULL);
9926
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9927
0
  if (h == NULL)
9928
0
    return false;
9929
0
  h->root.u.def.value = isa_bit;
9930
0
  h->other = other;
9931
0
  h->type = STT_FUNC;
9932
9933
0
  return true;
9934
0
}
9935
9936
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9937
   bfd_link_info.  If H uses the address of a PLT entry as the value
9938
   of the symbol, then set the entry in the symbol table now.  Prefer
9939
   a standard MIPS PLT entry.  */
9940
9941
static bool
9942
mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9943
0
{
9944
0
  struct bfd_link_info *info = data;
9945
0
  bool micromips_p = MICROMIPS_P (info->output_bfd);
9946
0
  struct mips_elf_link_hash_table *htab;
9947
0
  unsigned int other;
9948
0
  bfd_vma isa_bit;
9949
0
  bfd_vma val;
9950
9951
0
  htab = mips_elf_hash_table (info);
9952
0
  BFD_ASSERT (htab != NULL);
9953
9954
0
  if (h->use_plt_entry)
9955
0
    {
9956
0
      BFD_ASSERT (h->root.plt.plist != NULL);
9957
0
      BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9958
0
      || h->root.plt.plist->comp_offset != MINUS_ONE);
9959
9960
0
      val = htab->plt_header_size;
9961
0
      if (h->root.plt.plist->mips_offset != MINUS_ONE)
9962
0
  {
9963
0
    isa_bit = 0;
9964
0
    val += h->root.plt.plist->mips_offset;
9965
0
    other = 0;
9966
0
  }
9967
0
      else
9968
0
  {
9969
0
    isa_bit = 1;
9970
0
    val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9971
0
    other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9972
0
  }
9973
0
      val += isa_bit;
9974
      /* For VxWorks, point at the PLT load stub rather than the lazy
9975
   resolution stub; this stub will become the canonical function
9976
   address.  */
9977
0
      if (htab->root.target_os == is_vxworks)
9978
0
  val += 8;
9979
9980
0
      h->root.root.u.def.section = htab->root.splt;
9981
0
      h->root.root.u.def.value = val;
9982
0
      h->root.other = other;
9983
0
    }
9984
9985
0
  return true;
9986
0
}
9987
9988
/* Set the sizes of the dynamic sections.  */
9989
9990
bool
9991
_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9992
             struct bfd_link_info *info)
9993
0
{
9994
0
  bfd *dynobj;
9995
0
  asection *s, *sreldyn;
9996
0
  bool reltext;
9997
0
  struct mips_elf_link_hash_table *htab;
9998
9999
0
  htab = mips_elf_hash_table (info);
10000
0
  BFD_ASSERT (htab != NULL);
10001
0
  dynobj = elf_hash_table (info)->dynobj;
10002
0
  BFD_ASSERT (dynobj != NULL);
10003
10004
0
  if (elf_hash_table (info)->dynamic_sections_created)
10005
0
    {
10006
      /* Set the contents of the .interp section to the interpreter.  */
10007
0
      if (bfd_link_executable (info) && !info->nointerp)
10008
0
  {
10009
0
    s = bfd_get_linker_section (dynobj, ".interp");
10010
0
    BFD_ASSERT (s != NULL);
10011
0
    s->size
10012
0
      = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10013
0
    s->contents
10014
0
      = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10015
0
  }
10016
10017
      /* Figure out the size of the PLT header if we know that we
10018
   are using it.  For the sake of cache alignment always use
10019
   a standard header whenever any standard entries are present
10020
   even if microMIPS entries are present as well.  This also
10021
   lets the microMIPS header rely on the value of $v0 only set
10022
   by microMIPS entries, for a small size reduction.
10023
10024
   Set symbol table entry values for symbols that use the
10025
   address of their PLT entry now that we can calculate it.
10026
10027
   Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10028
   haven't already in _bfd_elf_create_dynamic_sections.  */
10029
0
      if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10030
0
  {
10031
0
    bool micromips_p = (MICROMIPS_P (output_bfd)
10032
0
             && !htab->plt_mips_offset);
10033
0
    unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10034
0
    bfd_vma isa_bit = micromips_p;
10035
0
    struct elf_link_hash_entry *h;
10036
0
    bfd_vma size;
10037
10038
0
    BFD_ASSERT (htab->use_plts_and_copy_relocs);
10039
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
10040
0
    BFD_ASSERT (htab->root.splt->size == 0);
10041
10042
0
    if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10043
0
      size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10044
0
    else if (htab->root.target_os == is_vxworks)
10045
0
      size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10046
0
    else if (ABI_64_P (output_bfd))
10047
0
      size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10048
0
    else if (ABI_N32_P (output_bfd))
10049
0
      size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10050
0
    else if (!micromips_p)
10051
0
      size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10052
0
    else if (htab->insn32)
10053
0
      size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10054
0
    else
10055
0
      size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10056
10057
0
    htab->plt_header_is_comp = micromips_p;
10058
0
    htab->plt_header_size = size;
10059
0
    htab->root.splt->size = (size
10060
0
           + htab->plt_mips_offset
10061
0
           + htab->plt_comp_offset);
10062
0
    htab->root.sgotplt->size = (htab->plt_got_index
10063
0
              * MIPS_ELF_GOT_SIZE (dynobj));
10064
10065
0
    mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10066
10067
0
    if (htab->root.hplt == NULL)
10068
0
      {
10069
0
        h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10070
0
                 "_PROCEDURE_LINKAGE_TABLE_");
10071
0
        htab->root.hplt = h;
10072
0
        if (h == NULL)
10073
0
    return false;
10074
0
      }
10075
10076
0
    h = htab->root.hplt;
10077
0
    h->root.u.def.value = isa_bit;
10078
0
    h->other = other;
10079
0
    h->type = STT_FUNC;
10080
0
  }
10081
0
    }
10082
10083
  /* Allocate space for global sym dynamic relocs.  */
10084
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10085
10086
0
  mips_elf_estimate_stub_size (output_bfd, info);
10087
10088
0
  if (!mips_elf_lay_out_got (output_bfd, info))
10089
0
    return false;
10090
10091
0
  mips_elf_lay_out_lazy_stubs (info);
10092
10093
  /* The check_relocs and adjust_dynamic_symbol entry points have
10094
     determined the sizes of the various dynamic sections.  Allocate
10095
     memory for them.  */
10096
0
  reltext = false;
10097
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10098
0
    {
10099
0
      const char *name;
10100
10101
      /* It's OK to base decisions on the section name, because none
10102
   of the dynobj section names depend upon the input files.  */
10103
0
      name = bfd_section_name (s);
10104
10105
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10106
0
  continue;
10107
10108
0
      if (startswith (name, ".rel"))
10109
0
  {
10110
0
    if (s->size != 0)
10111
0
      {
10112
0
        const char *outname;
10113
0
        asection *target;
10114
10115
        /* If this relocation section applies to a read only
10116
     section, then we probably need a DT_TEXTREL entry.
10117
     If the relocation section is .rel(a).dyn, we always
10118
     assert a DT_TEXTREL entry rather than testing whether
10119
     there exists a relocation to a read only section or
10120
     not.  */
10121
0
        outname = bfd_section_name (s->output_section);
10122
0
        target = bfd_get_section_by_name (output_bfd, outname + 4);
10123
0
        if ((target != NULL
10124
0
       && (target->flags & SEC_READONLY) != 0
10125
0
       && (target->flags & SEC_ALLOC) != 0)
10126
0
      || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10127
0
    reltext = true;
10128
10129
        /* We use the reloc_count field as a counter if we need
10130
     to copy relocs into the output file.  */
10131
0
        if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10132
0
    s->reloc_count = 0;
10133
10134
        /* If combreloc is enabled, elf_link_sort_relocs() will
10135
     sort relocations, but in a different way than we do,
10136
     and before we're done creating relocations.  Also, it
10137
     will move them around between input sections'
10138
     relocation's contents, so our sorting would be
10139
     broken, so don't let it run.  */
10140
0
        info->combreloc = 0;
10141
0
      }
10142
0
  }
10143
0
      else if (bfd_link_executable (info)
10144
0
         && ! mips_elf_hash_table (info)->use_rld_obj_head
10145
0
         && startswith (name, ".rld_map"))
10146
0
  {
10147
    /* We add a room for __rld_map.  It will be filled in by the
10148
       rtld to contain a pointer to the _r_debug structure.  */
10149
0
    s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10150
0
  }
10151
0
      else if (SGI_COMPAT (output_bfd)
10152
0
         && startswith (name, ".compact_rel"))
10153
0
  s->size += mips_elf_hash_table (info)->compact_rel_size;
10154
0
      else if (s == htab->root.splt)
10155
0
  {
10156
    /* If the last PLT entry has a branch delay slot, allocate
10157
       room for an extra nop to fill the delay slot.  This is
10158
       for CPUs without load interlocking.  */
10159
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
10160
0
        && htab->root.target_os != is_vxworks
10161
0
        && s->size > 0)
10162
0
      s->size += 4;
10163
0
  }
10164
0
      else if (! startswith (name, ".init")
10165
0
         && s != htab->root.sgot
10166
0
         && s != htab->root.sgotplt
10167
0
         && s != htab->sstubs
10168
0
         && s != htab->root.sdynbss
10169
0
         && s != htab->root.sdynrelro)
10170
0
  {
10171
    /* It's not one of our sections, so don't allocate space.  */
10172
0
    continue;
10173
0
  }
10174
10175
0
      if (s->size == 0)
10176
0
  {
10177
0
    s->flags |= SEC_EXCLUDE;
10178
0
    continue;
10179
0
  }
10180
10181
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10182
0
  continue;
10183
10184
      /* Allocate memory for the section contents.  */
10185
0
      s->contents = bfd_zalloc (dynobj, s->size);
10186
0
      if (s->contents == NULL)
10187
0
  {
10188
0
    bfd_set_error (bfd_error_no_memory);
10189
0
    return false;
10190
0
  }
10191
0
    }
10192
10193
0
  if (elf_hash_table (info)->dynamic_sections_created)
10194
0
    {
10195
      /* Add some entries to the .dynamic section.  We fill in the
10196
   values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10197
   must add the entries now so that we get the correct size for
10198
   the .dynamic section.  */
10199
10200
      /* SGI object has the equivalence of DT_DEBUG in the
10201
   DT_MIPS_RLD_MAP entry.  This must come first because glibc
10202
   only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10203
   may only look at the first one they see.  */
10204
0
      if (!bfd_link_pic (info)
10205
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10206
0
  return false;
10207
10208
0
      if (bfd_link_executable (info)
10209
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10210
0
  return false;
10211
10212
      /* The DT_DEBUG entry may be filled in by the dynamic linker and
10213
   used by the debugger.  */
10214
0
      if (bfd_link_executable (info)
10215
0
    && !SGI_COMPAT (output_bfd)
10216
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10217
0
  return false;
10218
10219
0
      if (reltext
10220
0
    && (SGI_COMPAT (output_bfd)
10221
0
        || htab->root.target_os == is_vxworks))
10222
0
  info->flags |= DF_TEXTREL;
10223
10224
0
      if ((info->flags & DF_TEXTREL) != 0)
10225
0
  {
10226
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10227
0
      return false;
10228
10229
    /* Clear the DF_TEXTREL flag.  It will be set again if we
10230
       write out an actual text relocation; we may not, because
10231
       at this point we do not know whether e.g. any .eh_frame
10232
       absolute relocations have been converted to PC-relative.  */
10233
0
    info->flags &= ~DF_TEXTREL;
10234
0
  }
10235
10236
0
      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10237
0
  return false;
10238
10239
0
      sreldyn = mips_elf_rel_dyn_section (info, false);
10240
0
      if (htab->root.target_os == is_vxworks)
10241
0
  {
10242
    /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
10243
       use any of the DT_MIPS_* tags.  */
10244
0
    if (sreldyn && sreldyn->size > 0)
10245
0
      {
10246
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10247
0
    return false;
10248
10249
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10250
0
    return false;
10251
10252
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10253
0
    return false;
10254
0
      }
10255
0
  }
10256
0
      else
10257
0
  {
10258
0
    if (sreldyn && sreldyn->size > 0
10259
0
        && !bfd_is_abs_section (sreldyn->output_section))
10260
0
      {
10261
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10262
0
    return false;
10263
10264
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10265
0
    return false;
10266
10267
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10268
0
    return false;
10269
0
      }
10270
10271
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10272
0
      return false;
10273
10274
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10275
0
      return false;
10276
10277
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10278
0
      return false;
10279
10280
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10281
0
      return false;
10282
10283
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10284
0
      return false;
10285
10286
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10287
0
      return false;
10288
10289
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10290
0
      return false;
10291
10292
0
    if (info->emit_gnu_hash
10293
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10294
0
      return false;
10295
10296
0
    if (IRIX_COMPAT (dynobj) == ict_irix5
10297
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10298
0
      return false;
10299
10300
0
    if (IRIX_COMPAT (dynobj) == ict_irix6
10301
0
        && (bfd_get_section_by_name
10302
0
      (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10303
0
        && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10304
0
      return false;
10305
0
  }
10306
0
      if (htab->root.splt->size > 0)
10307
0
  {
10308
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10309
0
      return false;
10310
10311
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10312
0
      return false;
10313
10314
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10315
0
      return false;
10316
10317
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10318
0
      return false;
10319
0
  }
10320
0
      if (htab->root.target_os == is_vxworks
10321
0
    && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10322
0
  return false;
10323
0
    }
10324
10325
0
  return true;
10326
0
}
10327

10328
/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10329
   Adjust its R_ADDEND field so that it is correct for the output file.
10330
   LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10331
   and sections respectively; both use symbol indexes.  */
10332
10333
static void
10334
mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10335
      bfd *input_bfd, Elf_Internal_Sym *local_syms,
10336
      asection **local_sections, Elf_Internal_Rela *rel)
10337
0
{
10338
0
  unsigned int r_type, r_symndx;
10339
0
  Elf_Internal_Sym *sym;
10340
0
  asection *sec;
10341
10342
0
  if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10343
0
    {
10344
0
      r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10345
0
      if (gprel16_reloc_p (r_type)
10346
0
    || r_type == R_MIPS_GPREL32
10347
0
    || literal_reloc_p (r_type))
10348
0
  {
10349
0
    rel->r_addend += _bfd_get_gp_value (input_bfd);
10350
0
    rel->r_addend -= _bfd_get_gp_value (output_bfd);
10351
0
  }
10352
10353
0
      r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10354
0
      sym = local_syms + r_symndx;
10355
10356
      /* Adjust REL's addend to account for section merging.  */
10357
0
      if (!bfd_link_relocatable (info))
10358
0
  {
10359
0
    sec = local_sections[r_symndx];
10360
0
    _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10361
0
  }
10362
10363
      /* This would normally be done by the rela_normal code in elflink.c.  */
10364
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10365
0
  rel->r_addend += local_sections[r_symndx]->output_offset;
10366
0
    }
10367
0
}
10368
10369
/* Handle relocations against symbols from removed linkonce sections,
10370
   or sections discarded by a linker script.  We use this wrapper around
10371
   RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10372
   on 64-bit ELF targets.  In this case for any relocation handled, which
10373
   always be the first in a triplet, the remaining two have to be processed
10374
   together with the first, even if they are R_MIPS_NONE.  It is the symbol
10375
   index referred by the first reloc that applies to all the three and the
10376
   remaining two never refer to an object symbol.  And it is the final
10377
   relocation (the last non-null one) that determines the output field of
10378
   the whole relocation so retrieve the corresponding howto structure for
10379
   the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10380
10381
   Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10382
   and therefore requires to be pasted in a loop.  It also defines a block
10383
   and does not protect any of its arguments, hence the extra brackets.  */
10384
10385
static void
10386
mips_reloc_against_discarded_section (bfd *output_bfd,
10387
              struct bfd_link_info *info,
10388
              bfd *input_bfd, asection *input_section,
10389
              Elf_Internal_Rela **rel,
10390
              const Elf_Internal_Rela **relend,
10391
              bool rel_reloc,
10392
              reloc_howto_type *howto,
10393
              bfd_byte *contents)
10394
0
{
10395
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10396
0
  int count = bed->s->int_rels_per_ext_rel;
10397
0
  unsigned int r_type;
10398
0
  int i;
10399
10400
0
  for (i = count - 1; i > 0; i--)
10401
0
    {
10402
0
      r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10403
0
      if (r_type != R_MIPS_NONE)
10404
0
  {
10405
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10406
0
    break;
10407
0
  }
10408
0
    }
10409
0
  do
10410
0
    {
10411
0
       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10412
0
          (*rel), count, (*relend),
10413
0
          howto, i, contents);
10414
0
    }
10415
0
  while (0);
10416
0
}
10417
10418
/* Relocate a MIPS ELF section.  */
10419
10420
int
10421
_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10422
        bfd *input_bfd, asection *input_section,
10423
        bfd_byte *contents, Elf_Internal_Rela *relocs,
10424
        Elf_Internal_Sym *local_syms,
10425
        asection **local_sections)
10426
0
{
10427
0
  Elf_Internal_Rela *rel;
10428
0
  const Elf_Internal_Rela *relend;
10429
0
  bfd_vma addend = 0;
10430
0
  bool use_saved_addend_p = false;
10431
10432
0
  relend = relocs + input_section->reloc_count;
10433
0
  for (rel = relocs; rel < relend; ++rel)
10434
0
    {
10435
0
      const char *name;
10436
0
      bfd_vma value = 0;
10437
0
      reloc_howto_type *howto;
10438
0
      bool cross_mode_jump_p = false;
10439
      /* TRUE if the relocation is a RELA relocation, rather than a
10440
   REL relocation.  */
10441
0
      bool rela_relocation_p = true;
10442
0
      unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10443
0
      const char *msg;
10444
0
      unsigned long r_symndx;
10445
0
      asection *sec;
10446
0
      Elf_Internal_Shdr *symtab_hdr;
10447
0
      struct elf_link_hash_entry *h;
10448
0
      bool rel_reloc;
10449
10450
0
      rel_reloc = (NEWABI_P (input_bfd)
10451
0
       && mips_elf_rel_relocation_p (input_bfd, input_section,
10452
0
             relocs, rel));
10453
      /* Find the relocation howto for this relocation.  */
10454
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10455
10456
0
      r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10457
0
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10458
0
      if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10459
0
  {
10460
0
    sec = local_sections[r_symndx];
10461
0
    h = NULL;
10462
0
  }
10463
0
      else
10464
0
  {
10465
0
    unsigned long extsymoff;
10466
10467
0
    extsymoff = 0;
10468
0
    if (!elf_bad_symtab (input_bfd))
10469
0
      extsymoff = symtab_hdr->sh_info;
10470
0
    h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10471
0
    while (h->root.type == bfd_link_hash_indirect
10472
0
     || h->root.type == bfd_link_hash_warning)
10473
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10474
10475
0
    sec = NULL;
10476
0
    if (h->root.type == bfd_link_hash_defined
10477
0
        || h->root.type == bfd_link_hash_defweak)
10478
0
      sec = h->root.u.def.section;
10479
0
  }
10480
10481
0
      if (sec != NULL && discarded_section (sec))
10482
0
  {
10483
0
    mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10484
0
            input_section, &rel, &relend,
10485
0
            rel_reloc, howto, contents);
10486
0
    continue;
10487
0
  }
10488
10489
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10490
0
  {
10491
    /* Some 32-bit code uses R_MIPS_64.  In particular, people use
10492
       64-bit code, but make sure all their addresses are in the
10493
       lowermost or uppermost 32-bit section of the 64-bit address
10494
       space.  Thus, when they use an R_MIPS_64 they mean what is
10495
       usually meant by R_MIPS_32, with the exception that the
10496
       stored value is sign-extended to 64 bits.  */
10497
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10498
10499
    /* On big-endian systems, we need to lie about the position
10500
       of the reloc.  */
10501
0
    if (bfd_big_endian (input_bfd))
10502
0
      rel->r_offset += 4;
10503
0
  }
10504
10505
0
      if (!use_saved_addend_p)
10506
0
  {
10507
    /* If these relocations were originally of the REL variety,
10508
       we must pull the addend out of the field that will be
10509
       relocated.  Otherwise, we simply use the contents of the
10510
       RELA relocation.  */
10511
0
    if (mips_elf_rel_relocation_p (input_bfd, input_section,
10512
0
           relocs, rel))
10513
0
      {
10514
0
        rela_relocation_p = false;
10515
0
        addend = mips_elf_read_rel_addend (input_bfd, input_section,
10516
0
             rel, howto, contents);
10517
0
        if (hi16_reloc_p (r_type)
10518
0
      || (got16_reloc_p (r_type)
10519
0
          && mips_elf_local_relocation_p (input_bfd, rel,
10520
0
                  local_sections)))
10521
0
    {
10522
0
      if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10523
0
                 rel, relend,
10524
0
                 contents, &addend))
10525
0
        {
10526
0
          if (h)
10527
0
      name = h->root.root.string;
10528
0
          else
10529
0
      name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10530
0
             local_syms + r_symndx,
10531
0
             sec);
10532
0
          _bfd_error_handler
10533
      /* xgettext:c-format */
10534
0
      (_("%pB: can't find matching LO16 reloc against `%s'"
10535
0
         " for %s at %#" PRIx64 " in section `%pA'"),
10536
0
       input_bfd, name,
10537
0
       howto->name, (uint64_t) rel->r_offset, input_section);
10538
0
        }
10539
0
    }
10540
0
        else
10541
0
    addend <<= howto->rightshift;
10542
0
      }
10543
0
    else
10544
0
      addend = rel->r_addend;
10545
0
    mips_elf_adjust_addend (output_bfd, info, input_bfd,
10546
0
          local_syms, local_sections, rel);
10547
0
  }
10548
10549
0
      if (bfd_link_relocatable (info))
10550
0
  {
10551
0
    if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10552
0
        && bfd_big_endian (input_bfd))
10553
0
      rel->r_offset -= 4;
10554
10555
0
    if (!rela_relocation_p && rel->r_addend)
10556
0
      {
10557
0
        addend += rel->r_addend;
10558
0
        if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10559
0
    addend = mips_elf_high (addend);
10560
0
        else if (r_type == R_MIPS_HIGHER)
10561
0
    addend = mips_elf_higher (addend);
10562
0
        else if (r_type == R_MIPS_HIGHEST)
10563
0
    addend = mips_elf_highest (addend);
10564
0
        else
10565
0
    addend >>= howto->rightshift;
10566
10567
        /* We use the source mask, rather than the destination
10568
     mask because the place to which we are writing will be
10569
     source of the addend in the final link.  */
10570
0
        addend &= howto->src_mask;
10571
10572
0
        if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10573
    /* See the comment above about using R_MIPS_64 in the 32-bit
10574
       ABI.  Here, we need to update the addend.  It would be
10575
       possible to get away with just using the R_MIPS_32 reloc
10576
       but for endianness.  */
10577
0
    {
10578
0
      bfd_vma sign_bits;
10579
0
      bfd_vma low_bits;
10580
0
      bfd_vma high_bits;
10581
10582
0
      if (addend & ((bfd_vma) 1 << 31))
10583
0
#ifdef BFD64
10584
0
        sign_bits = ((bfd_vma) 1 << 32) - 1;
10585
#else
10586
        sign_bits = -1;
10587
#endif
10588
0
      else
10589
0
        sign_bits = 0;
10590
10591
      /* If we don't know that we have a 64-bit type,
10592
         do two separate stores.  */
10593
0
      if (bfd_big_endian (input_bfd))
10594
0
        {
10595
          /* Store the sign-bits (which are most significant)
10596
       first.  */
10597
0
          low_bits = sign_bits;
10598
0
          high_bits = addend;
10599
0
        }
10600
0
      else
10601
0
        {
10602
0
          low_bits = addend;
10603
0
          high_bits = sign_bits;
10604
0
        }
10605
0
      bfd_put_32 (input_bfd, low_bits,
10606
0
            contents + rel->r_offset);
10607
0
      bfd_put_32 (input_bfd, high_bits,
10608
0
            contents + rel->r_offset + 4);
10609
0
      continue;
10610
0
    }
10611
10612
0
        if (! mips_elf_perform_relocation (info, howto, rel, addend,
10613
0
             input_bfd, input_section,
10614
0
             contents, false))
10615
0
    return false;
10616
0
      }
10617
10618
    /* Go on to the next relocation.  */
10619
0
    continue;
10620
0
  }
10621
10622
      /* In the N32 and 64-bit ABIs there may be multiple consecutive
10623
   relocations for the same offset.  In that case we are
10624
   supposed to treat the output of each relocation as the addend
10625
   for the next.  */
10626
0
      if (rel + 1 < relend
10627
0
    && rel->r_offset == rel[1].r_offset
10628
0
    && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10629
0
  use_saved_addend_p = true;
10630
0
      else
10631
0
  use_saved_addend_p = false;
10632
10633
      /* Figure out what value we are supposed to relocate.  */
10634
0
      switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10635
0
               input_section, contents,
10636
0
               info, rel, addend, howto,
10637
0
               local_syms, local_sections,
10638
0
               &value, &name, &cross_mode_jump_p,
10639
0
               use_saved_addend_p))
10640
0
  {
10641
0
  case bfd_reloc_continue:
10642
    /* There's nothing to do.  */
10643
0
    continue;
10644
10645
0
  case bfd_reloc_undefined:
10646
    /* mips_elf_calculate_relocation already called the
10647
       undefined_symbol callback.  There's no real point in
10648
       trying to perform the relocation at this point, so we
10649
       just skip ahead to the next relocation.  */
10650
0
    continue;
10651
10652
0
  case bfd_reloc_notsupported:
10653
0
    msg = _("internal error: unsupported relocation error");
10654
0
    info->callbacks->warning
10655
0
      (info, msg, name, input_bfd, input_section, rel->r_offset);
10656
0
    return false;
10657
10658
0
  case bfd_reloc_overflow:
10659
0
    if (use_saved_addend_p)
10660
      /* Ignore overflow until we reach the last relocation for
10661
         a given location.  */
10662
0
      ;
10663
0
    else
10664
0
      {
10665
0
        struct mips_elf_link_hash_table *htab;
10666
10667
0
        htab = mips_elf_hash_table (info);
10668
0
        BFD_ASSERT (htab != NULL);
10669
0
        BFD_ASSERT (name != NULL);
10670
0
        if (!htab->small_data_overflow_reported
10671
0
      && (gprel16_reloc_p (howto->type)
10672
0
          || literal_reloc_p (howto->type)))
10673
0
    {
10674
0
      msg = _("small-data section exceeds 64KB;"
10675
0
        " lower small-data size limit (see option -G)");
10676
10677
0
      htab->small_data_overflow_reported = true;
10678
0
      (*info->callbacks->einfo) ("%P: %s\n", msg);
10679
0
    }
10680
0
        (*info->callbacks->reloc_overflow)
10681
0
    (info, NULL, name, howto->name, (bfd_vma) 0,
10682
0
     input_bfd, input_section, rel->r_offset);
10683
0
      }
10684
0
    break;
10685
10686
0
  case bfd_reloc_ok:
10687
0
    break;
10688
10689
0
  case bfd_reloc_outofrange:
10690
0
    msg = NULL;
10691
0
    if (jal_reloc_p (howto->type))
10692
0
      msg = (cross_mode_jump_p
10693
0
       ? _("cannot convert a jump to JALX "
10694
0
           "for a non-word-aligned address")
10695
0
       : (howto->type == R_MIPS16_26
10696
0
          ? _("jump to a non-word-aligned address")
10697
0
          : _("jump to a non-instruction-aligned address")));
10698
0
    else if (b_reloc_p (howto->type))
10699
0
      msg = (cross_mode_jump_p
10700
0
       ? _("cannot convert a branch to JALX "
10701
0
           "for a non-word-aligned address")
10702
0
       : _("branch to a non-instruction-aligned address"));
10703
0
    else if (aligned_pcrel_reloc_p (howto->type))
10704
0
      msg = _("PC-relative load from unaligned address");
10705
0
    if (msg)
10706
0
      {
10707
0
        info->callbacks->einfo
10708
0
    ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10709
0
        break;
10710
0
      }
10711
    /* Fall through.  */
10712
10713
0
  default:
10714
0
    abort ();
10715
0
    break;
10716
0
  }
10717
10718
      /* If we've got another relocation for the address, keep going
10719
   until we reach the last one.  */
10720
0
      if (use_saved_addend_p)
10721
0
  {
10722
0
    addend = value;
10723
0
    continue;
10724
0
  }
10725
10726
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10727
  /* See the comment above about using R_MIPS_64 in the 32-bit
10728
     ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
10729
     that calculated the right value.  Now, however, we
10730
     sign-extend the 32-bit result to 64-bits, and store it as a
10731
     64-bit value.  We are especially generous here in that we
10732
     go to extreme lengths to support this usage on systems with
10733
     only a 32-bit VMA.  */
10734
0
  {
10735
0
    bfd_vma sign_bits;
10736
0
    bfd_vma low_bits;
10737
0
    bfd_vma high_bits;
10738
10739
0
    if (value & ((bfd_vma) 1 << 31))
10740
0
#ifdef BFD64
10741
0
      sign_bits = ((bfd_vma) 1 << 32) - 1;
10742
#else
10743
      sign_bits = -1;
10744
#endif
10745
0
    else
10746
0
      sign_bits = 0;
10747
10748
    /* If we don't know that we have a 64-bit type,
10749
       do two separate stores.  */
10750
0
    if (bfd_big_endian (input_bfd))
10751
0
      {
10752
        /* Undo what we did above.  */
10753
0
        rel->r_offset -= 4;
10754
        /* Store the sign-bits (which are most significant)
10755
     first.  */
10756
0
        low_bits = sign_bits;
10757
0
        high_bits = value;
10758
0
      }
10759
0
    else
10760
0
      {
10761
0
        low_bits = value;
10762
0
        high_bits = sign_bits;
10763
0
      }
10764
0
    bfd_put_32 (input_bfd, low_bits,
10765
0
          contents + rel->r_offset);
10766
0
    bfd_put_32 (input_bfd, high_bits,
10767
0
          contents + rel->r_offset + 4);
10768
0
    continue;
10769
0
  }
10770
10771
      /* Actually perform the relocation.  */
10772
0
      if (! mips_elf_perform_relocation (info, howto, rel, value,
10773
0
           input_bfd, input_section,
10774
0
           contents, cross_mode_jump_p))
10775
0
  return false;
10776
0
    }
10777
10778
0
  return true;
10779
0
}
10780

10781
/* A function that iterates over each entry in la25_stubs and fills
10782
   in the code for each one.  DATA points to a mips_htab_traverse_info.  */
10783
10784
static int
10785
mips_elf_create_la25_stub (void **slot, void *data)
10786
0
{
10787
0
  struct mips_htab_traverse_info *hti;
10788
0
  struct mips_elf_link_hash_table *htab;
10789
0
  struct mips_elf_la25_stub *stub;
10790
0
  asection *s;
10791
0
  bfd_byte *loc;
10792
0
  bfd_vma offset, target, target_high, target_low;
10793
0
  bfd_vma branch_pc;
10794
0
  bfd_signed_vma pcrel_offset = 0;
10795
10796
0
  stub = (struct mips_elf_la25_stub *) *slot;
10797
0
  hti = (struct mips_htab_traverse_info *) data;
10798
0
  htab = mips_elf_hash_table (hti->info);
10799
0
  BFD_ASSERT (htab != NULL);
10800
10801
  /* Create the section contents, if we haven't already.  */
10802
0
  s = stub->stub_section;
10803
0
  loc = s->contents;
10804
0
  if (loc == NULL)
10805
0
    {
10806
0
      loc = bfd_malloc (s->size);
10807
0
      if (loc == NULL)
10808
0
  {
10809
0
    hti->error = true;
10810
0
    return false;
10811
0
  }
10812
0
      s->contents = loc;
10813
0
    }
10814
10815
  /* Work out where in the section this stub should go.  */
10816
0
  offset = stub->offset;
10817
10818
  /* We add 8 here to account for the LUI/ADDIU instructions
10819
     before the branch instruction.  This cannot be moved down to
10820
     where pcrel_offset is calculated as 's' is updated in
10821
     mips_elf_get_la25_target.  */
10822
0
  branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10823
10824
  /* Work out the target address.  */
10825
0
  target = mips_elf_get_la25_target (stub, &s);
10826
0
  target += s->output_section->vma + s->output_offset;
10827
10828
0
  target_high = ((target + 0x8000) >> 16) & 0xffff;
10829
0
  target_low = (target & 0xffff);
10830
10831
  /* Calculate the PC of the compact branch instruction (for the case where
10832
     compact branches are used for either microMIPSR6 or MIPSR6 with
10833
     compact branches.  Add 4-bytes to account for BC using the PC of the
10834
     next instruction as the base.  */
10835
0
  pcrel_offset = target - (branch_pc + 4);
10836
10837
0
  if (stub->stub_section != htab->strampoline)
10838
0
    {
10839
      /* This is a simple LUI/ADDIU stub.  Zero out the beginning
10840
   of the section and write the two instructions at the end.  */
10841
0
      memset (loc, 0, offset);
10842
0
      loc += offset;
10843
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10844
0
  {
10845
0
    bfd_put_micromips_32 (hti->output_bfd,
10846
0
        LA25_LUI_MICROMIPS (target_high),
10847
0
        loc);
10848
0
    bfd_put_micromips_32 (hti->output_bfd,
10849
0
        LA25_ADDIU_MICROMIPS (target_low),
10850
0
        loc + 4);
10851
0
  }
10852
0
      else
10853
0
  {
10854
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10855
0
    bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10856
0
  }
10857
0
    }
10858
0
  else
10859
0
    {
10860
      /* This is trampoline.  */
10861
0
      loc += offset;
10862
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10863
0
  {
10864
0
    bfd_put_micromips_32 (hti->output_bfd,
10865
0
        LA25_LUI_MICROMIPS (target_high), loc);
10866
0
    bfd_put_micromips_32 (hti->output_bfd,
10867
0
        LA25_J_MICROMIPS (target), loc + 4);
10868
0
    bfd_put_micromips_32 (hti->output_bfd,
10869
0
        LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10870
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10871
0
  }
10872
0
      else
10873
0
  {
10874
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10875
0
    if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10876
0
      {
10877
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10878
0
        bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10879
0
      }
10880
0
    else
10881
0
      {
10882
0
        bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10883
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10884
0
      }
10885
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10886
0
  }
10887
0
    }
10888
0
  return true;
10889
0
}
10890
10891
/* If NAME is one of the special IRIX6 symbols defined by the linker,
10892
   adjust it appropriately now.  */
10893
10894
static void
10895
mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10896
              const char *name, Elf_Internal_Sym *sym)
10897
0
{
10898
  /* The linker script takes care of providing names and values for
10899
     these, but we must place them into the right sections.  */
10900
0
  static const char* const text_section_symbols[] = {
10901
0
    "_ftext",
10902
0
    "_etext",
10903
0
    "__dso_displacement",
10904
0
    "__elf_header",
10905
0
    "__program_header_table",
10906
0
    NULL
10907
0
  };
10908
10909
0
  static const char* const data_section_symbols[] = {
10910
0
    "_fdata",
10911
0
    "_edata",
10912
0
    "_end",
10913
0
    "_fbss",
10914
0
    NULL
10915
0
  };
10916
10917
0
  const char* const *p;
10918
0
  int i;
10919
10920
0
  for (i = 0; i < 2; ++i)
10921
0
    for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10922
0
   *p;
10923
0
   ++p)
10924
0
      if (strcmp (*p, name) == 0)
10925
0
  {
10926
    /* All of these symbols are given type STT_SECTION by the
10927
       IRIX6 linker.  */
10928
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10929
0
    sym->st_other = STO_PROTECTED;
10930
10931
    /* The IRIX linker puts these symbols in special sections.  */
10932
0
    if (i == 0)
10933
0
      sym->st_shndx = SHN_MIPS_TEXT;
10934
0
    else
10935
0
      sym->st_shndx = SHN_MIPS_DATA;
10936
10937
0
    break;
10938
0
  }
10939
0
}
10940
10941
/* Finish up dynamic symbol handling.  We set the contents of various
10942
   dynamic sections here.  */
10943
10944
bool
10945
_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10946
             struct bfd_link_info *info,
10947
             struct elf_link_hash_entry *h,
10948
             Elf_Internal_Sym *sym)
10949
0
{
10950
0
  bfd *dynobj;
10951
0
  asection *sgot;
10952
0
  struct mips_got_info *g, *gg;
10953
0
  const char *name;
10954
0
  int idx;
10955
0
  struct mips_elf_link_hash_table *htab;
10956
0
  struct mips_elf_link_hash_entry *hmips;
10957
10958
0
  htab = mips_elf_hash_table (info);
10959
0
  BFD_ASSERT (htab != NULL);
10960
0
  dynobj = elf_hash_table (info)->dynobj;
10961
0
  hmips = (struct mips_elf_link_hash_entry *) h;
10962
10963
0
  BFD_ASSERT (htab->root.target_os != is_vxworks);
10964
10965
0
  if (h->plt.plist != NULL
10966
0
      && (h->plt.plist->mips_offset != MINUS_ONE
10967
0
    || h->plt.plist->comp_offset != MINUS_ONE))
10968
0
    {
10969
      /* We've decided to create a PLT entry for this symbol.  */
10970
0
      bfd_byte *loc;
10971
0
      bfd_vma header_address, got_address;
10972
0
      bfd_vma got_address_high, got_address_low, load;
10973
0
      bfd_vma got_index;
10974
0
      bfd_vma isa_bit;
10975
10976
0
      got_index = h->plt.plist->gotplt_index;
10977
10978
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10979
0
      BFD_ASSERT (h->dynindx != -1);
10980
0
      BFD_ASSERT (htab->root.splt != NULL);
10981
0
      BFD_ASSERT (got_index != MINUS_ONE);
10982
0
      BFD_ASSERT (!h->def_regular);
10983
10984
      /* Calculate the address of the PLT header.  */
10985
0
      isa_bit = htab->plt_header_is_comp;
10986
0
      header_address = (htab->root.splt->output_section->vma
10987
0
      + htab->root.splt->output_offset + isa_bit);
10988
10989
      /* Calculate the address of the .got.plt entry.  */
10990
0
      got_address = (htab->root.sgotplt->output_section->vma
10991
0
         + htab->root.sgotplt->output_offset
10992
0
         + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10993
10994
0
      got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10995
0
      got_address_low = got_address & 0xffff;
10996
10997
      /* The PLT sequence is not safe for N64 if .got.plt entry's address
10998
   cannot be loaded in two instructions.  */
10999
0
      if (ABI_64_P (output_bfd)
11000
0
    && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11001
0
  {
11002
0
    _bfd_error_handler
11003
      /* xgettext:c-format */
11004
0
      (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11005
0
         "supported; consider using `-Ttext-segment=...'"),
11006
0
       output_bfd,
11007
0
       htab->root.sgotplt->output_section,
11008
0
       (int64_t) got_address);
11009
0
    bfd_set_error (bfd_error_no_error);
11010
0
    return false;
11011
0
  }
11012
11013
      /* Initially point the .got.plt entry at the PLT header.  */
11014
0
      loc = (htab->root.sgotplt->contents
11015
0
       + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11016
0
      if (ABI_64_P (output_bfd))
11017
0
  bfd_put_64 (output_bfd, header_address, loc);
11018
0
      else
11019
0
  bfd_put_32 (output_bfd, header_address, loc);
11020
11021
      /* Now handle the PLT itself.  First the standard entry (the order
11022
   does not matter, we just have to pick one).  */
11023
0
      if (h->plt.plist->mips_offset != MINUS_ONE)
11024
0
  {
11025
0
    const bfd_vma *plt_entry;
11026
0
    bfd_vma plt_offset;
11027
11028
0
    plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11029
11030
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11031
11032
    /* Find out where the .plt entry should go.  */
11033
0
    loc = htab->root.splt->contents + plt_offset;
11034
11035
    /* Pick the load opcode.  */
11036
0
    load = MIPS_ELF_LOAD_WORD (output_bfd);
11037
11038
    /* Fill in the PLT entry itself.  */
11039
11040
0
    if (MIPSR6_P (output_bfd))
11041
0
      plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11042
0
                 : mipsr6_exec_plt_entry;
11043
0
    else
11044
0
      plt_entry = mips_exec_plt_entry;
11045
0
    bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11046
0
    bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11047
0
          loc + 4);
11048
11049
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
11050
0
        || (MIPSR6_P (output_bfd) && htab->compact_branches))
11051
0
      {
11052
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11053
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11054
0
      }
11055
0
    else
11056
0
      {
11057
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11058
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11059
0
        loc + 12);
11060
0
      }
11061
0
  }
11062
11063
      /* Now the compressed entry.  They come after any standard ones.  */
11064
0
      if (h->plt.plist->comp_offset != MINUS_ONE)
11065
0
  {
11066
0
    bfd_vma plt_offset;
11067
11068
0
    plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11069
0
      + h->plt.plist->comp_offset);
11070
11071
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11072
11073
    /* Find out where the .plt entry should go.  */
11074
0
    loc = htab->root.splt->contents + plt_offset;
11075
11076
    /* Fill in the PLT entry itself.  */
11077
0
    if (!MICROMIPS_P (output_bfd))
11078
0
      {
11079
0
        const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11080
11081
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11082
0
        bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11083
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11084
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11085
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11086
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11087
0
        bfd_put_32 (output_bfd, got_address, loc + 12);
11088
0
      }
11089
0
    else if (htab->insn32)
11090
0
      {
11091
0
        const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11092
11093
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11094
0
        bfd_put_16 (output_bfd, got_address_high, loc + 2);
11095
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11096
0
        bfd_put_16 (output_bfd, got_address_low, loc + 6);
11097
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11098
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11099
0
        bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11100
0
        bfd_put_16 (output_bfd, got_address_low, loc + 14);
11101
0
      }
11102
0
    else
11103
0
      {
11104
0
        const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11105
0
        bfd_signed_vma gotpc_offset;
11106
0
        bfd_vma loc_address;
11107
11108
0
        BFD_ASSERT (got_address % 4 == 0);
11109
11110
0
        loc_address = (htab->root.splt->output_section->vma
11111
0
           + htab->root.splt->output_offset + plt_offset);
11112
0
        gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11113
11114
        /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11115
0
        if (gotpc_offset + 0x1000000 >= 0x2000000)
11116
0
    {
11117
0
      _bfd_error_handler
11118
        /* xgettext:c-format */
11119
0
        (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11120
0
           "beyond the range of ADDIUPC"),
11121
0
         output_bfd,
11122
0
         htab->root.sgotplt->output_section,
11123
0
         (int64_t) gotpc_offset,
11124
0
         htab->root.splt->output_section);
11125
0
      bfd_set_error (bfd_error_no_error);
11126
0
      return false;
11127
0
    }
11128
0
        bfd_put_16 (output_bfd,
11129
0
        plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11130
0
        bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11131
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11132
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11133
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11134
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11135
0
      }
11136
0
  }
11137
11138
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11139
0
      mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11140
0
            got_index - 2, h->dynindx,
11141
0
            R_MIPS_JUMP_SLOT, got_address);
11142
11143
      /* We distinguish between PLT entries and lazy-binding stubs by
11144
   giving the former an st_other value of STO_MIPS_PLT.  Set the
11145
   flag and leave the value if there are any relocations in the
11146
   binary where pointer equality matters.  */
11147
0
      sym->st_shndx = SHN_UNDEF;
11148
0
      if (h->pointer_equality_needed)
11149
0
  sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11150
0
      else
11151
0
  {
11152
0
    sym->st_value = 0;
11153
0
    sym->st_other = 0;
11154
0
  }
11155
0
    }
11156
11157
0
  if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11158
0
    {
11159
      /* We've decided to create a lazy-binding stub.  */
11160
0
      bool micromips_p = MICROMIPS_P (output_bfd);
11161
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11162
0
      bfd_vma stub_size = htab->function_stub_size;
11163
0
      bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11164
0
      bfd_vma isa_bit = micromips_p;
11165
0
      bfd_vma stub_big_size;
11166
11167
0
      if (!micromips_p)
11168
0
  stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11169
0
      else if (htab->insn32)
11170
0
  stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11171
0
      else
11172
0
  stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11173
11174
      /* This symbol has a stub.  Set it up.  */
11175
11176
0
      BFD_ASSERT (h->dynindx != -1);
11177
11178
0
      BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11179
11180
      /* Values up to 2^31 - 1 are allowed.  Larger values would cause
11181
   sign extension at runtime in the stub, resulting in a negative
11182
   index value.  */
11183
0
      if (h->dynindx & ~0x7fffffff)
11184
0
  return false;
11185
11186
      /* Fill the stub.  */
11187
0
      if (micromips_p)
11188
0
  {
11189
0
    idx = 0;
11190
0
    bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11191
0
        stub + idx);
11192
0
    idx += 4;
11193
0
    if (htab->insn32)
11194
0
      {
11195
0
        bfd_put_micromips_32 (output_bfd,
11196
0
            STUB_MOVE32_MICROMIPS, stub + idx);
11197
0
        idx += 4;
11198
0
      }
11199
0
    else
11200
0
      {
11201
0
        bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11202
0
        idx += 2;
11203
0
      }
11204
0
    if (stub_size == stub_big_size)
11205
0
      {
11206
0
        long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11207
11208
0
        bfd_put_micromips_32 (output_bfd,
11209
0
            STUB_LUI_MICROMIPS (dynindx_hi),
11210
0
            stub + idx);
11211
0
        idx += 4;
11212
0
      }
11213
0
    if (htab->insn32)
11214
0
      {
11215
0
        bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11216
0
            stub + idx);
11217
0
        idx += 4;
11218
0
      }
11219
0
    else
11220
0
      {
11221
0
        bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11222
0
        idx += 2;
11223
0
      }
11224
11225
    /* If a large stub is not required and sign extension is not a
11226
       problem, then use legacy code in the stub.  */
11227
0
    if (stub_size == stub_big_size)
11228
0
      bfd_put_micromips_32 (output_bfd,
11229
0
          STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11230
0
          stub + idx);
11231
0
    else if (h->dynindx & ~0x7fff)
11232
0
      bfd_put_micromips_32 (output_bfd,
11233
0
          STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11234
0
          stub + idx);
11235
0
    else
11236
0
      bfd_put_micromips_32 (output_bfd,
11237
0
          STUB_LI16S_MICROMIPS (output_bfd,
11238
0
              h->dynindx),
11239
0
          stub + idx);
11240
0
  }
11241
0
      else
11242
0
  {
11243
0
    idx = 0;
11244
0
    bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11245
0
    idx += 4;
11246
0
    bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11247
0
    idx += 4;
11248
0
    if (stub_size == stub_big_size)
11249
0
      {
11250
0
        bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11251
0
        stub + idx);
11252
0
        idx += 4;
11253
0
      }
11254
11255
0
    if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11256
0
      {
11257
0
        bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11258
0
        idx += 4;
11259
0
      }
11260
11261
    /* If a large stub is not required and sign extension is not a
11262
       problem, then use legacy code in the stub.  */
11263
0
    if (stub_size == stub_big_size)
11264
0
      bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11265
0
      stub + idx);
11266
0
    else if (h->dynindx & ~0x7fff)
11267
0
      bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11268
0
      stub + idx);
11269
0
    else
11270
0
      bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11271
0
      stub + idx);
11272
0
    idx += 4;
11273
11274
0
    if (MIPSR6_P (output_bfd) && htab->compact_branches)
11275
0
      bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11276
0
  }
11277
11278
0
      BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11279
0
      memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11280
0
        stub, stub_size);
11281
11282
      /* Mark the symbol as undefined.  stub_offset != -1 occurs
11283
   only for the referenced symbol.  */
11284
0
      sym->st_shndx = SHN_UNDEF;
11285
11286
      /* The run-time linker uses the st_value field of the symbol
11287
   to reset the global offset table entry for this external
11288
   to its stub address when unlinking a shared object.  */
11289
0
      sym->st_value = (htab->sstubs->output_section->vma
11290
0
           + htab->sstubs->output_offset
11291
0
           + h->plt.plist->stub_offset
11292
0
           + isa_bit);
11293
0
      sym->st_other = other;
11294
0
    }
11295
11296
  /* If we have a MIPS16 function with a stub, the dynamic symbol must
11297
     refer to the stub, since only the stub uses the standard calling
11298
     conventions.  */
11299
0
  if (h->dynindx != -1 && hmips->fn_stub != NULL)
11300
0
    {
11301
0
      BFD_ASSERT (hmips->need_fn_stub);
11302
0
      sym->st_value = (hmips->fn_stub->output_section->vma
11303
0
           + hmips->fn_stub->output_offset);
11304
0
      sym->st_size = hmips->fn_stub->size;
11305
0
      sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11306
0
    }
11307
11308
0
  BFD_ASSERT (h->dynindx != -1
11309
0
        || h->forced_local);
11310
11311
0
  sgot = htab->root.sgot;
11312
0
  g = htab->got_info;
11313
0
  BFD_ASSERT (g != NULL);
11314
11315
  /* Run through the global symbol table, creating GOT entries for all
11316
     the symbols that need them.  */
11317
0
  if (hmips->global_got_area != GGA_NONE)
11318
0
    {
11319
0
      bfd_vma offset;
11320
0
      bfd_vma value;
11321
11322
0
      value = sym->st_value;
11323
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11324
0
      MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11325
0
    }
11326
11327
0
  if (hmips->global_got_area != GGA_NONE && g->next)
11328
0
    {
11329
0
      struct mips_got_entry e, *p;
11330
0
      bfd_vma entry;
11331
0
      bfd_vma offset;
11332
11333
0
      gg = g;
11334
11335
0
      e.abfd = output_bfd;
11336
0
      e.symndx = -1;
11337
0
      e.d.h = hmips;
11338
0
      e.tls_type = GOT_TLS_NONE;
11339
11340
0
      for (g = g->next; g->next != gg; g = g->next)
11341
0
  {
11342
0
    if (g->got_entries
11343
0
        && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11344
0
                 &e)))
11345
0
      {
11346
0
        offset = p->gotidx;
11347
0
        BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11348
0
        if (bfd_link_pic (info)
11349
0
      || (elf_hash_table (info)->dynamic_sections_created
11350
0
          && p->d.h != NULL
11351
0
          && p->d.h->root.def_dynamic
11352
0
          && !p->d.h->root.def_regular))
11353
0
    {
11354
      /* Create an R_MIPS_REL32 relocation for this entry.  Due to
11355
         the various compatibility problems, it's easier to mock
11356
         up an R_MIPS_32 or R_MIPS_64 relocation and leave
11357
         mips_elf_create_dynamic_relocation to calculate the
11358
         appropriate addend.  */
11359
0
      Elf_Internal_Rela rel[3];
11360
11361
0
      memset (rel, 0, sizeof (rel));
11362
0
      if (ABI_64_P (output_bfd))
11363
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11364
0
      else
11365
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11366
0
      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11367
11368
0
      entry = 0;
11369
0
      if (! (mips_elf_create_dynamic_relocation
11370
0
       (output_bfd, info, rel,
11371
0
        e.d.h, NULL, sym->st_value, &entry, sgot)))
11372
0
        return false;
11373
0
    }
11374
0
        else
11375
0
    entry = sym->st_value;
11376
0
        MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11377
0
      }
11378
0
  }
11379
0
    }
11380
11381
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
11382
0
  name = h->root.root.string;
11383
0
  if (h == elf_hash_table (info)->hdynamic
11384
0
      || h == elf_hash_table (info)->hgot)
11385
0
    sym->st_shndx = SHN_ABS;
11386
0
  else if (strcmp (name, "_DYNAMIC_LINK") == 0
11387
0
     || strcmp (name, "_DYNAMIC_LINKING") == 0)
11388
0
    {
11389
0
      sym->st_shndx = SHN_ABS;
11390
0
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11391
0
      sym->st_value = 1;
11392
0
    }
11393
0
  else if (SGI_COMPAT (output_bfd))
11394
0
    {
11395
0
      if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11396
0
    || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11397
0
  {
11398
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11399
0
    sym->st_other = STO_PROTECTED;
11400
0
    sym->st_value = 0;
11401
0
    sym->st_shndx = SHN_MIPS_DATA;
11402
0
  }
11403
0
      else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11404
0
  {
11405
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11406
0
    sym->st_other = STO_PROTECTED;
11407
0
    sym->st_value = mips_elf_hash_table (info)->procedure_count;
11408
0
    sym->st_shndx = SHN_ABS;
11409
0
  }
11410
0
      else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11411
0
  {
11412
0
    if (h->type == STT_FUNC)
11413
0
      sym->st_shndx = SHN_MIPS_TEXT;
11414
0
    else if (h->type == STT_OBJECT)
11415
0
      sym->st_shndx = SHN_MIPS_DATA;
11416
0
  }
11417
0
    }
11418
11419
  /* Emit a copy reloc, if needed.  */
11420
0
  if (h->needs_copy)
11421
0
    {
11422
0
      asection *s;
11423
0
      bfd_vma symval;
11424
11425
0
      BFD_ASSERT (h->dynindx != -1);
11426
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
11427
11428
0
      s = mips_elf_rel_dyn_section (info, false);
11429
0
      symval = (h->root.u.def.section->output_section->vma
11430
0
    + h->root.u.def.section->output_offset
11431
0
    + h->root.u.def.value);
11432
0
      mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11433
0
            h->dynindx, R_MIPS_COPY, symval);
11434
0
    }
11435
11436
  /* Handle the IRIX6-specific symbols.  */
11437
0
  if (IRIX_COMPAT (output_bfd) == ict_irix6)
11438
0
    mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11439
11440
  /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
11441
     to treat compressed symbols like any other.  */
11442
0
  if (ELF_ST_IS_MIPS16 (sym->st_other))
11443
0
    {
11444
0
      BFD_ASSERT (sym->st_value & 1);
11445
0
      sym->st_other -= STO_MIPS16;
11446
0
    }
11447
0
  else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11448
0
    {
11449
0
      BFD_ASSERT (sym->st_value & 1);
11450
0
      sym->st_other -= STO_MICROMIPS;
11451
0
    }
11452
11453
0
  return true;
11454
0
}
11455
11456
/* Likewise, for VxWorks.  */
11457
11458
bool
11459
_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11460
           struct bfd_link_info *info,
11461
           struct elf_link_hash_entry *h,
11462
           Elf_Internal_Sym *sym)
11463
0
{
11464
0
  bfd *dynobj;
11465
0
  asection *sgot;
11466
0
  struct mips_got_info *g;
11467
0
  struct mips_elf_link_hash_table *htab;
11468
0
  struct mips_elf_link_hash_entry *hmips;
11469
11470
0
  htab = mips_elf_hash_table (info);
11471
0
  BFD_ASSERT (htab != NULL);
11472
0
  dynobj = elf_hash_table (info)->dynobj;
11473
0
  hmips = (struct mips_elf_link_hash_entry *) h;
11474
11475
0
  if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11476
0
    {
11477
0
      bfd_byte *loc;
11478
0
      bfd_vma plt_address, got_address, got_offset, branch_offset;
11479
0
      Elf_Internal_Rela rel;
11480
0
      static const bfd_vma *plt_entry;
11481
0
      bfd_vma gotplt_index;
11482
0
      bfd_vma plt_offset;
11483
11484
0
      plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11485
0
      gotplt_index = h->plt.plist->gotplt_index;
11486
11487
0
      BFD_ASSERT (h->dynindx != -1);
11488
0
      BFD_ASSERT (htab->root.splt != NULL);
11489
0
      BFD_ASSERT (gotplt_index != MINUS_ONE);
11490
0
      BFD_ASSERT (plt_offset <= htab->root.splt->size);
11491
11492
      /* Calculate the address of the .plt entry.  */
11493
0
      plt_address = (htab->root.splt->output_section->vma
11494
0
         + htab->root.splt->output_offset
11495
0
         + plt_offset);
11496
11497
      /* Calculate the address of the .got.plt entry.  */
11498
0
      got_address = (htab->root.sgotplt->output_section->vma
11499
0
         + htab->root.sgotplt->output_offset
11500
0
         + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11501
11502
      /* Calculate the offset of the .got.plt entry from
11503
   _GLOBAL_OFFSET_TABLE_.  */
11504
0
      got_offset = mips_elf_gotplt_index (info, h);
11505
11506
      /* Calculate the offset for the branch at the start of the PLT
11507
   entry.  The branch jumps to the beginning of .plt.  */
11508
0
      branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11509
11510
      /* Fill in the initial value of the .got.plt entry.  */
11511
0
      bfd_put_32 (output_bfd, plt_address,
11512
0
      (htab->root.sgotplt->contents
11513
0
       + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11514
11515
      /* Find out where the .plt entry should go.  */
11516
0
      loc = htab->root.splt->contents + plt_offset;
11517
11518
0
      if (bfd_link_pic (info))
11519
0
  {
11520
0
    plt_entry = mips_vxworks_shared_plt_entry;
11521
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11522
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11523
0
  }
11524
0
      else
11525
0
  {
11526
0
    bfd_vma got_address_high, got_address_low;
11527
11528
0
    plt_entry = mips_vxworks_exec_plt_entry;
11529
0
    got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11530
0
    got_address_low = got_address & 0xffff;
11531
11532
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11533
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11534
0
    bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11535
0
    bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11536
0
    bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11537
0
    bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11538
0
    bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11539
0
    bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11540
11541
0
    loc = (htab->srelplt2->contents
11542
0
     + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11543
11544
    /* Emit a relocation for the .got.plt entry.  */
11545
0
    rel.r_offset = got_address;
11546
0
    rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11547
0
    rel.r_addend = plt_offset;
11548
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11549
11550
    /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
11551
0
    loc += sizeof (Elf32_External_Rela);
11552
0
    rel.r_offset = plt_address + 8;
11553
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11554
0
    rel.r_addend = got_offset;
11555
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11556
11557
    /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
11558
0
    loc += sizeof (Elf32_External_Rela);
11559
0
    rel.r_offset += 4;
11560
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11561
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11562
0
  }
11563
11564
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11565
0
      loc = (htab->root.srelplt->contents
11566
0
       + gotplt_index * sizeof (Elf32_External_Rela));
11567
0
      rel.r_offset = got_address;
11568
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11569
0
      rel.r_addend = 0;
11570
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11571
11572
0
      if (!h->def_regular)
11573
0
  sym->st_shndx = SHN_UNDEF;
11574
0
    }
11575
11576
0
  BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11577
11578
0
  sgot = htab->root.sgot;
11579
0
  g = htab->got_info;
11580
0
  BFD_ASSERT (g != NULL);
11581
11582
  /* See if this symbol has an entry in the GOT.  */
11583
0
  if (hmips->global_got_area != GGA_NONE)
11584
0
    {
11585
0
      bfd_vma offset;
11586
0
      Elf_Internal_Rela outrel;
11587
0
      bfd_byte *loc;
11588
0
      asection *s;
11589
11590
      /* Install the symbol value in the GOT.   */
11591
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11592
0
      MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11593
11594
      /* Add a dynamic relocation for it.  */
11595
0
      s = mips_elf_rel_dyn_section (info, false);
11596
0
      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11597
0
      outrel.r_offset = (sgot->output_section->vma
11598
0
       + sgot->output_offset
11599
0
       + offset);
11600
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11601
0
      outrel.r_addend = 0;
11602
0
      bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11603
0
    }
11604
11605
  /* Emit a copy reloc, if needed.  */
11606
0
  if (h->needs_copy)
11607
0
    {
11608
0
      Elf_Internal_Rela rel;
11609
0
      asection *srel;
11610
0
      bfd_byte *loc;
11611
11612
0
      BFD_ASSERT (h->dynindx != -1);
11613
11614
0
      rel.r_offset = (h->root.u.def.section->output_section->vma
11615
0
          + h->root.u.def.section->output_offset
11616
0
          + h->root.u.def.value);
11617
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11618
0
      rel.r_addend = 0;
11619
0
      if (h->root.u.def.section == htab->root.sdynrelro)
11620
0
  srel = htab->root.sreldynrelro;
11621
0
      else
11622
0
  srel = htab->root.srelbss;
11623
0
      loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11624
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11625
0
      ++srel->reloc_count;
11626
0
    }
11627
11628
  /* If this is a mips16/microMIPS symbol, force the value to be even.  */
11629
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
11630
0
    sym->st_value &= ~1;
11631
11632
0
  return true;
11633
0
}
11634
11635
/* Write out a plt0 entry to the beginning of .plt.  */
11636
11637
static bool
11638
mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11639
0
{
11640
0
  bfd_byte *loc;
11641
0
  bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11642
0
  static const bfd_vma *plt_entry;
11643
0
  struct mips_elf_link_hash_table *htab;
11644
11645
0
  htab = mips_elf_hash_table (info);
11646
0
  BFD_ASSERT (htab != NULL);
11647
11648
0
  if (ABI_64_P (output_bfd))
11649
0
    plt_entry = (htab->compact_branches
11650
0
     ? mipsr6_n64_exec_plt0_entry_compact
11651
0
     : mips_n64_exec_plt0_entry);
11652
0
  else if (ABI_N32_P (output_bfd))
11653
0
    plt_entry = (htab->compact_branches
11654
0
     ? mipsr6_n32_exec_plt0_entry_compact
11655
0
     : mips_n32_exec_plt0_entry);
11656
0
  else if (!htab->plt_header_is_comp)
11657
0
    plt_entry = (htab->compact_branches
11658
0
     ? mipsr6_o32_exec_plt0_entry_compact
11659
0
     : mips_o32_exec_plt0_entry);
11660
0
  else if (htab->insn32)
11661
0
    plt_entry = micromips_insn32_o32_exec_plt0_entry;
11662
0
  else
11663
0
    plt_entry = micromips_o32_exec_plt0_entry;
11664
11665
  /* Calculate the value of .got.plt.  */
11666
0
  gotplt_value = (htab->root.sgotplt->output_section->vma
11667
0
      + htab->root.sgotplt->output_offset);
11668
0
  gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11669
0
  gotplt_value_low = gotplt_value & 0xffff;
11670
11671
  /* The PLT sequence is not safe for N64 if .got.plt's address can
11672
     not be loaded in two instructions.  */
11673
0
  if (ABI_64_P (output_bfd)
11674
0
      && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11675
0
    {
11676
0
      _bfd_error_handler
11677
  /* xgettext:c-format */
11678
0
  (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11679
0
     "supported; consider using `-Ttext-segment=...'"),
11680
0
   output_bfd,
11681
0
   htab->root.sgotplt->output_section,
11682
0
   (int64_t) gotplt_value);
11683
0
      bfd_set_error (bfd_error_no_error);
11684
0
      return false;
11685
0
    }
11686
11687
  /* Install the PLT header.  */
11688
0
  loc = htab->root.splt->contents;
11689
0
  if (plt_entry == micromips_o32_exec_plt0_entry)
11690
0
    {
11691
0
      bfd_vma gotpc_offset;
11692
0
      bfd_vma loc_address;
11693
0
      size_t i;
11694
11695
0
      BFD_ASSERT (gotplt_value % 4 == 0);
11696
11697
0
      loc_address = (htab->root.splt->output_section->vma
11698
0
         + htab->root.splt->output_offset);
11699
0
      gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11700
11701
      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11702
0
      if (gotpc_offset + 0x1000000 >= 0x2000000)
11703
0
  {
11704
0
    _bfd_error_handler
11705
      /* xgettext:c-format */
11706
0
      (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11707
0
         "beyond the range of ADDIUPC"),
11708
0
       output_bfd,
11709
0
       htab->root.sgotplt->output_section,
11710
0
       (int64_t) gotpc_offset,
11711
0
       htab->root.splt->output_section);
11712
0
    bfd_set_error (bfd_error_no_error);
11713
0
    return false;
11714
0
  }
11715
0
      bfd_put_16 (output_bfd,
11716
0
      plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11717
0
      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11718
0
      for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11719
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11720
0
    }
11721
0
  else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11722
0
    {
11723
0
      size_t i;
11724
11725
0
      bfd_put_16 (output_bfd, plt_entry[0], loc);
11726
0
      bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11727
0
      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11728
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11729
0
      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11730
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11731
0
      for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11732
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11733
0
    }
11734
0
  else
11735
0
    {
11736
0
      bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11737
0
      bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11738
0
      bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11739
0
      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11740
0
      bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11741
0
      bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11742
0
      bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11743
0
      bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11744
0
    }
11745
11746
0
  return true;
11747
0
}
11748
11749
/* Install the PLT header for a VxWorks executable and finalize the
11750
   contents of .rela.plt.unloaded.  */
11751
11752
static void
11753
mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11754
0
{
11755
0
  Elf_Internal_Rela rela;
11756
0
  bfd_byte *loc;
11757
0
  bfd_vma got_value, got_value_high, got_value_low, plt_address;
11758
0
  static const bfd_vma *plt_entry;
11759
0
  struct mips_elf_link_hash_table *htab;
11760
11761
0
  htab = mips_elf_hash_table (info);
11762
0
  BFD_ASSERT (htab != NULL);
11763
11764
0
  plt_entry = mips_vxworks_exec_plt0_entry;
11765
11766
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
11767
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11768
0
         + htab->root.hgot->root.u.def.section->output_offset
11769
0
         + htab->root.hgot->root.u.def.value);
11770
11771
0
  got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11772
0
  got_value_low = got_value & 0xffff;
11773
11774
  /* Calculate the address of the PLT header.  */
11775
0
  plt_address = (htab->root.splt->output_section->vma
11776
0
     + htab->root.splt->output_offset);
11777
11778
  /* Install the PLT header.  */
11779
0
  loc = htab->root.splt->contents;
11780
0
  bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11781
0
  bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11782
0
  bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11783
0
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11784
0
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11785
0
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11786
11787
  /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
11788
0
  loc = htab->srelplt2->contents;
11789
0
  rela.r_offset = plt_address;
11790
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11791
0
  rela.r_addend = 0;
11792
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11793
0
  loc += sizeof (Elf32_External_Rela);
11794
11795
  /* Output the relocation for the following addiu of
11796
     %lo(_GLOBAL_OFFSET_TABLE_).  */
11797
0
  rela.r_offset += 4;
11798
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11799
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11800
0
  loc += sizeof (Elf32_External_Rela);
11801
11802
  /* Fix up the remaining relocations.  They may have the wrong
11803
     symbol index for _G_O_T_ or _P_L_T_ depending on the order
11804
     in which symbols were output.  */
11805
0
  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11806
0
    {
11807
0
      Elf_Internal_Rela rel;
11808
11809
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11810
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11811
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11812
0
      loc += sizeof (Elf32_External_Rela);
11813
11814
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11815
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11816
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11817
0
      loc += sizeof (Elf32_External_Rela);
11818
11819
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11820
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11821
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11822
0
      loc += sizeof (Elf32_External_Rela);
11823
0
    }
11824
0
}
11825
11826
/* Install the PLT header for a VxWorks shared library.  */
11827
11828
static void
11829
mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11830
0
{
11831
0
  unsigned int i;
11832
0
  struct mips_elf_link_hash_table *htab;
11833
11834
0
  htab = mips_elf_hash_table (info);
11835
0
  BFD_ASSERT (htab != NULL);
11836
11837
  /* We just need to copy the entry byte-by-byte.  */
11838
0
  for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11839
0
    bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11840
0
    htab->root.splt->contents + i * 4);
11841
0
}
11842
11843
/* Finish up the dynamic sections.  */
11844
11845
bool
11846
_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11847
               struct bfd_link_info *info)
11848
0
{
11849
0
  bfd *dynobj;
11850
0
  asection *sdyn;
11851
0
  asection *sgot;
11852
0
  struct mips_got_info *gg, *g;
11853
0
  struct mips_elf_link_hash_table *htab;
11854
11855
0
  htab = mips_elf_hash_table (info);
11856
0
  BFD_ASSERT (htab != NULL);
11857
11858
0
  dynobj = elf_hash_table (info)->dynobj;
11859
11860
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11861
11862
0
  sgot = htab->root.sgot;
11863
0
  gg = htab->got_info;
11864
11865
0
  if (elf_hash_table (info)->dynamic_sections_created)
11866
0
    {
11867
0
      bfd_byte *b;
11868
0
      int dyn_to_skip = 0, dyn_skipped = 0;
11869
11870
0
      BFD_ASSERT (sdyn != NULL);
11871
0
      BFD_ASSERT (gg != NULL);
11872
11873
0
      g = mips_elf_bfd_got (output_bfd, false);
11874
0
      BFD_ASSERT (g != NULL);
11875
11876
0
      for (b = sdyn->contents;
11877
0
     b < sdyn->contents + sdyn->size;
11878
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
11879
0
  {
11880
0
    Elf_Internal_Dyn dyn;
11881
0
    const char *name;
11882
0
    size_t elemsize;
11883
0
    asection *s;
11884
0
    bool swap_out_p;
11885
11886
    /* Read in the current dynamic entry.  */
11887
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11888
11889
    /* Assume that we're going to modify it and write it out.  */
11890
0
    swap_out_p = true;
11891
11892
0
    switch (dyn.d_tag)
11893
0
      {
11894
0
      case DT_RELENT:
11895
0
        dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11896
0
        break;
11897
11898
0
      case DT_RELAENT:
11899
0
        BFD_ASSERT (htab->root.target_os == is_vxworks);
11900
0
        dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11901
0
        break;
11902
11903
0
      case DT_STRSZ:
11904
        /* Rewrite DT_STRSZ.  */
11905
0
        dyn.d_un.d_val =
11906
0
    _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11907
0
        break;
11908
11909
0
      case DT_PLTGOT:
11910
0
        s = htab->root.sgot;
11911
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11912
0
        break;
11913
11914
0
      case DT_MIPS_PLTGOT:
11915
0
        s = htab->root.sgotplt;
11916
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11917
0
        break;
11918
11919
0
      case DT_MIPS_RLD_VERSION:
11920
0
        dyn.d_un.d_val = 1; /* XXX */
11921
0
        break;
11922
11923
0
      case DT_MIPS_FLAGS:
11924
0
        dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11925
0
        break;
11926
11927
0
      case DT_MIPS_TIME_STAMP:
11928
0
        {
11929
0
    time_t t;
11930
0
    time (&t);
11931
0
    dyn.d_un.d_val = t;
11932
0
        }
11933
0
        break;
11934
11935
0
      case DT_MIPS_ICHECKSUM:
11936
        /* XXX FIXME: */
11937
0
        swap_out_p = false;
11938
0
        break;
11939
11940
0
      case DT_MIPS_IVERSION:
11941
        /* XXX FIXME: */
11942
0
        swap_out_p = false;
11943
0
        break;
11944
11945
0
      case DT_MIPS_BASE_ADDRESS:
11946
0
        s = output_bfd->sections;
11947
0
        BFD_ASSERT (s != NULL);
11948
0
        dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11949
0
        break;
11950
11951
0
      case DT_MIPS_LOCAL_GOTNO:
11952
0
        dyn.d_un.d_val = g->local_gotno;
11953
0
        break;
11954
11955
0
      case DT_MIPS_UNREFEXTNO:
11956
        /* The index into the dynamic symbol table which is the
11957
     entry of the first external symbol that is not
11958
     referenced within the same object.  */
11959
0
        dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11960
0
        break;
11961
11962
0
      case DT_MIPS_GOTSYM:
11963
0
        if (htab->global_gotsym)
11964
0
    {
11965
0
      dyn.d_un.d_val = htab->global_gotsym->dynindx;
11966
0
      break;
11967
0
    }
11968
        /* In case if we don't have global got symbols we default
11969
     to setting DT_MIPS_GOTSYM to the same value as
11970
     DT_MIPS_SYMTABNO.  */
11971
        /* Fall through.  */
11972
11973
0
      case DT_MIPS_SYMTABNO:
11974
0
        name = ".dynsym";
11975
0
        elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11976
0
        s = bfd_get_linker_section (dynobj, name);
11977
11978
0
        if (s != NULL)
11979
0
    dyn.d_un.d_val = s->size / elemsize;
11980
0
        else
11981
0
    dyn.d_un.d_val = 0;
11982
0
        break;
11983
11984
0
      case DT_MIPS_HIPAGENO:
11985
0
        dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11986
0
        break;
11987
11988
0
      case DT_MIPS_RLD_MAP:
11989
0
        {
11990
0
    struct elf_link_hash_entry *h;
11991
0
    h = mips_elf_hash_table (info)->rld_symbol;
11992
0
    if (!h)
11993
0
      {
11994
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11995
0
        swap_out_p = false;
11996
0
        break;
11997
0
      }
11998
0
    s = h->root.u.def.section;
11999
12000
    /* The MIPS_RLD_MAP tag stores the absolute address of the
12001
       debug pointer.  */
12002
0
    dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12003
0
          + h->root.u.def.value);
12004
0
        }
12005
0
        break;
12006
12007
0
      case DT_MIPS_RLD_MAP_REL:
12008
0
        {
12009
0
    struct elf_link_hash_entry *h;
12010
0
    bfd_vma dt_addr, rld_addr;
12011
0
    h = mips_elf_hash_table (info)->rld_symbol;
12012
0
    if (!h)
12013
0
      {
12014
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12015
0
        swap_out_p = false;
12016
0
        break;
12017
0
      }
12018
0
    s = h->root.u.def.section;
12019
12020
    /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12021
       pointer, relative to the address of the tag.  */
12022
0
    dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12023
0
         + (b - sdyn->contents));
12024
0
    rld_addr = (s->output_section->vma + s->output_offset
12025
0
          + h->root.u.def.value);
12026
0
    dyn.d_un.d_ptr = rld_addr - dt_addr;
12027
0
        }
12028
0
        break;
12029
12030
0
      case DT_MIPS_OPTIONS:
12031
0
        s = (bfd_get_section_by_name
12032
0
       (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12033
0
        dyn.d_un.d_ptr = s->vma;
12034
0
        break;
12035
12036
0
      case DT_PLTREL:
12037
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12038
0
        if (htab->root.target_os == is_vxworks)
12039
0
    dyn.d_un.d_val = DT_RELA;
12040
0
        else
12041
0
    dyn.d_un.d_val = DT_REL;
12042
0
        break;
12043
12044
0
      case DT_PLTRELSZ:
12045
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12046
0
        dyn.d_un.d_val = htab->root.srelplt->size;
12047
0
        break;
12048
12049
0
      case DT_JMPREL:
12050
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12051
0
        dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12052
0
        + htab->root.srelplt->output_offset);
12053
0
        break;
12054
12055
0
      case DT_TEXTREL:
12056
        /* If we didn't need any text relocations after all, delete
12057
     the dynamic tag.  */
12058
0
        if (!(info->flags & DF_TEXTREL))
12059
0
    {
12060
0
      dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12061
0
      swap_out_p = false;
12062
0
    }
12063
0
        break;
12064
12065
0
      case DT_FLAGS:
12066
        /* If we didn't need any text relocations after all, clear
12067
     DF_TEXTREL from DT_FLAGS.  */
12068
0
        if (!(info->flags & DF_TEXTREL))
12069
0
    dyn.d_un.d_val &= ~DF_TEXTREL;
12070
0
        else
12071
0
    swap_out_p = false;
12072
0
        break;
12073
12074
0
      case DT_MIPS_XHASH:
12075
0
        name = ".MIPS.xhash";
12076
0
        s = bfd_get_linker_section (dynobj, name);
12077
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12078
0
        break;
12079
12080
0
      default:
12081
0
        swap_out_p = false;
12082
0
        if (htab->root.target_os == is_vxworks
12083
0
      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12084
0
    swap_out_p = true;
12085
0
        break;
12086
0
      }
12087
12088
0
    if (swap_out_p || dyn_skipped)
12089
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12090
0
        (dynobj, &dyn, b - dyn_skipped);
12091
12092
0
    if (dyn_to_skip)
12093
0
      {
12094
0
        dyn_skipped += dyn_to_skip;
12095
0
        dyn_to_skip = 0;
12096
0
      }
12097
0
  }
12098
12099
      /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
12100
0
      if (dyn_skipped > 0)
12101
0
  memset (b - dyn_skipped, 0, dyn_skipped);
12102
0
    }
12103
12104
0
  if (sgot != NULL && sgot->size > 0
12105
0
      && !bfd_is_abs_section (sgot->output_section))
12106
0
    {
12107
0
      if (htab->root.target_os == is_vxworks)
12108
0
  {
12109
    /* The first entry of the global offset table points to the
12110
       ".dynamic" section.  The second is initialized by the
12111
       loader and contains the shared library identifier.
12112
       The third is also initialized by the loader and points
12113
       to the lazy resolution stub.  */
12114
0
    MIPS_ELF_PUT_WORD (output_bfd,
12115
0
           sdyn->output_offset + sdyn->output_section->vma,
12116
0
           sgot->contents);
12117
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12118
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12119
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12120
0
           sgot->contents
12121
0
           + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12122
0
  }
12123
0
      else
12124
0
  {
12125
    /* The first entry of the global offset table will be filled at
12126
       runtime. The second entry will be used by some runtime loaders.
12127
       This isn't the case of IRIX rld.  */
12128
0
    MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12129
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12130
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12131
0
  }
12132
12133
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12134
0
   = MIPS_ELF_GOT_SIZE (output_bfd);
12135
0
    }
12136
12137
  /* Generate dynamic relocations for the non-primary gots.  */
12138
0
  if (gg != NULL && gg->next)
12139
0
    {
12140
0
      Elf_Internal_Rela rel[3];
12141
0
      bfd_vma addend = 0;
12142
12143
0
      memset (rel, 0, sizeof (rel));
12144
0
      rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12145
12146
0
      for (g = gg->next; g->next != gg; g = g->next)
12147
0
  {
12148
0
    bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12149
0
      + g->next->tls_gotno;
12150
12151
0
    MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12152
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12153
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12154
0
           sgot->contents
12155
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12156
12157
0
    if (! bfd_link_pic (info))
12158
0
      continue;
12159
12160
0
    for (; got_index < g->local_gotno; got_index++)
12161
0
      {
12162
0
        if (got_index >= g->assigned_low_gotno
12163
0
      && got_index <= g->assigned_high_gotno)
12164
0
    continue;
12165
12166
0
        rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12167
0
    = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12168
0
        if (!(mips_elf_create_dynamic_relocation
12169
0
        (output_bfd, info, rel, NULL,
12170
0
         bfd_abs_section_ptr,
12171
0
         0, &addend, sgot)))
12172
0
    return false;
12173
0
        BFD_ASSERT (addend == 0);
12174
0
      }
12175
0
  }
12176
0
    }
12177
12178
  /* The generation of dynamic relocations for the non-primary gots
12179
     adds more dynamic relocations.  We cannot count them until
12180
     here.  */
12181
12182
0
  if (elf_hash_table (info)->dynamic_sections_created)
12183
0
    {
12184
0
      bfd_byte *b;
12185
0
      bool swap_out_p;
12186
12187
0
      BFD_ASSERT (sdyn != NULL);
12188
12189
0
      for (b = sdyn->contents;
12190
0
     b < sdyn->contents + sdyn->size;
12191
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
12192
0
  {
12193
0
    Elf_Internal_Dyn dyn;
12194
0
    asection *s;
12195
12196
    /* Read in the current dynamic entry.  */
12197
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12198
12199
    /* Assume that we're going to modify it and write it out.  */
12200
0
    swap_out_p = true;
12201
12202
0
    switch (dyn.d_tag)
12203
0
      {
12204
0
      case DT_RELSZ:
12205
        /* Reduce DT_RELSZ to account for any relocations we
12206
     decided not to make.  This is for the n64 irix rld,
12207
     which doesn't seem to apply any relocations if there
12208
     are trailing null entries.  */
12209
0
        s = mips_elf_rel_dyn_section (info, false);
12210
0
        dyn.d_un.d_val = (s->reloc_count
12211
0
        * (ABI_64_P (output_bfd)
12212
0
           ? sizeof (Elf64_Mips_External_Rel)
12213
0
           : sizeof (Elf32_External_Rel)));
12214
        /* Adjust the section size too.  Tools like the prelinker
12215
     can reasonably expect the values to the same.  */
12216
0
        BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12217
0
        elf_section_data (s->output_section)->this_hdr.sh_size
12218
0
    = dyn.d_un.d_val;
12219
0
        break;
12220
12221
0
      default:
12222
0
        swap_out_p = false;
12223
0
        break;
12224
0
      }
12225
12226
0
    if (swap_out_p)
12227
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12228
0
        (dynobj, &dyn, b);
12229
0
  }
12230
0
    }
12231
12232
0
  {
12233
0
    asection *s;
12234
0
    Elf32_compact_rel cpt;
12235
12236
0
    if (SGI_COMPAT (output_bfd))
12237
0
      {
12238
  /* Write .compact_rel section out.  */
12239
0
  s = bfd_get_linker_section (dynobj, ".compact_rel");
12240
0
  if (s != NULL)
12241
0
    {
12242
0
      cpt.id1 = 1;
12243
0
      cpt.num = s->reloc_count;
12244
0
      cpt.id2 = 2;
12245
0
      cpt.offset = (s->output_section->filepos
12246
0
        + sizeof (Elf32_External_compact_rel));
12247
0
      cpt.reserved0 = 0;
12248
0
      cpt.reserved1 = 0;
12249
0
      bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12250
0
              ((Elf32_External_compact_rel *)
12251
0
               s->contents));
12252
12253
      /* Clean up a dummy stub function entry in .text.  */
12254
0
      if (htab->sstubs != NULL
12255
0
    && htab->sstubs->contents != NULL)
12256
0
        {
12257
0
    file_ptr dummy_offset;
12258
12259
0
    BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12260
0
    dummy_offset = htab->sstubs->size - htab->function_stub_size;
12261
0
    memset (htab->sstubs->contents + dummy_offset, 0,
12262
0
      htab->function_stub_size);
12263
0
        }
12264
0
    }
12265
0
      }
12266
12267
    /* The psABI says that the dynamic relocations must be sorted in
12268
       increasing order of r_symndx.  The VxWorks EABI doesn't require
12269
       this, and because the code below handles REL rather than RELA
12270
       relocations, using it for VxWorks would be outright harmful.  */
12271
0
    if (htab->root.target_os != is_vxworks)
12272
0
      {
12273
0
  s = mips_elf_rel_dyn_section (info, false);
12274
0
  if (s != NULL
12275
0
      && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12276
0
    {
12277
0
      reldyn_sorting_bfd = output_bfd;
12278
12279
0
      if (ABI_64_P (output_bfd))
12280
0
        qsort ((Elf64_External_Rel *) s->contents + 1,
12281
0
         s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12282
0
         sort_dynamic_relocs_64);
12283
0
      else
12284
0
        qsort ((Elf32_External_Rel *) s->contents + 1,
12285
0
         s->reloc_count - 1, sizeof (Elf32_External_Rel),
12286
0
         sort_dynamic_relocs);
12287
0
    }
12288
0
      }
12289
0
  }
12290
12291
0
  if (htab->root.splt && htab->root.splt->size > 0)
12292
0
    {
12293
0
      if (htab->root.target_os == is_vxworks)
12294
0
  {
12295
0
    if (bfd_link_pic (info))
12296
0
      mips_vxworks_finish_shared_plt (output_bfd, info);
12297
0
    else
12298
0
      mips_vxworks_finish_exec_plt (output_bfd, info);
12299
0
  }
12300
0
      else
12301
0
  {
12302
0
    BFD_ASSERT (!bfd_link_pic (info));
12303
0
    if (!mips_finish_exec_plt (output_bfd, info))
12304
0
      return false;
12305
0
  }
12306
0
    }
12307
0
  return true;
12308
0
}
12309
12310
12311
/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
12312
12313
static void
12314
mips_set_isa_flags (bfd *abfd)
12315
0
{
12316
0
  flagword val;
12317
12318
0
  switch (bfd_get_mach (abfd))
12319
0
    {
12320
0
    default:
12321
0
      if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12322
0
        val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_64R6 : E_MIPS_ARCH_3;
12323
0
      else
12324
0
        val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_32R6 : E_MIPS_ARCH_1;
12325
0
      break;
12326
12327
0
    case bfd_mach_mips3000:
12328
0
      val = E_MIPS_ARCH_1;
12329
0
      break;
12330
12331
0
    case bfd_mach_mips3900:
12332
0
      val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12333
0
      break;
12334
12335
0
    case bfd_mach_mips6000:
12336
0
      val = E_MIPS_ARCH_2;
12337
0
      break;
12338
12339
0
    case bfd_mach_mips4010:
12340
0
      val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12341
0
      break;
12342
12343
0
    case bfd_mach_mips_allegrex:
12344
0
      val = E_MIPS_ARCH_2 | E_MIPS_MACH_ALLEGREX;
12345
0
      break;
12346
12347
0
    case bfd_mach_mips4000:
12348
0
    case bfd_mach_mips4300:
12349
0
    case bfd_mach_mips4400:
12350
0
    case bfd_mach_mips4600:
12351
0
      val = E_MIPS_ARCH_3;
12352
0
      break;
12353
12354
0
    case bfd_mach_mips4100:
12355
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12356
0
      break;
12357
12358
0
    case bfd_mach_mips4111:
12359
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12360
0
      break;
12361
12362
0
    case bfd_mach_mips4120:
12363
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12364
0
      break;
12365
12366
0
    case bfd_mach_mips4650:
12367
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12368
0
      break;
12369
12370
0
    case bfd_mach_mips5400:
12371
0
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12372
0
      break;
12373
12374
0
    case bfd_mach_mips5500:
12375
0
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12376
0
      break;
12377
12378
0
    case bfd_mach_mips5900:
12379
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12380
0
      break;
12381
12382
0
    case bfd_mach_mips9000:
12383
0
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12384
0
      break;
12385
12386
0
    case bfd_mach_mips5000:
12387
0
    case bfd_mach_mips7000:
12388
0
    case bfd_mach_mips8000:
12389
0
    case bfd_mach_mips10000:
12390
0
    case bfd_mach_mips12000:
12391
0
    case bfd_mach_mips14000:
12392
0
    case bfd_mach_mips16000:
12393
0
      val = E_MIPS_ARCH_4;
12394
0
      break;
12395
12396
0
    case bfd_mach_mips5:
12397
0
      val = E_MIPS_ARCH_5;
12398
0
      break;
12399
12400
0
    case bfd_mach_mips_loongson_2e:
12401
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12402
0
      break;
12403
12404
0
    case bfd_mach_mips_loongson_2f:
12405
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12406
0
      break;
12407
12408
0
    case bfd_mach_mips_sb1:
12409
0
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12410
0
      break;
12411
12412
0
    case bfd_mach_mips_gs464:
12413
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12414
0
      break;
12415
12416
0
    case bfd_mach_mips_gs464e:
12417
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12418
0
      break;
12419
12420
0
    case bfd_mach_mips_gs264e:
12421
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12422
0
      break;
12423
12424
0
    case bfd_mach_mips_octeon:
12425
0
    case bfd_mach_mips_octeonp:
12426
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12427
0
      break;
12428
12429
0
    case bfd_mach_mips_octeon3:
12430
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12431
0
      break;
12432
12433
0
    case bfd_mach_mips_xlr:
12434
0
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12435
0
      break;
12436
12437
0
    case bfd_mach_mips_octeon2:
12438
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12439
0
      break;
12440
12441
0
    case bfd_mach_mipsisa32:
12442
0
      val = E_MIPS_ARCH_32;
12443
0
      break;
12444
12445
0
    case bfd_mach_mipsisa64:
12446
0
      val = E_MIPS_ARCH_64;
12447
0
      break;
12448
12449
0
    case bfd_mach_mipsisa32r2:
12450
0
    case bfd_mach_mipsisa32r3:
12451
0
    case bfd_mach_mipsisa32r5:
12452
0
      val = E_MIPS_ARCH_32R2;
12453
0
      break;
12454
12455
0
    case bfd_mach_mips_interaptiv_mr2:
12456
0
      val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12457
0
      break;
12458
12459
0
    case bfd_mach_mipsisa64r2:
12460
0
    case bfd_mach_mipsisa64r3:
12461
0
    case bfd_mach_mipsisa64r5:
12462
0
      val = E_MIPS_ARCH_64R2;
12463
0
      break;
12464
12465
0
    case bfd_mach_mipsisa32r6:
12466
0
      val = E_MIPS_ARCH_32R6;
12467
0
      break;
12468
12469
0
    case bfd_mach_mipsisa64r6:
12470
0
      val = E_MIPS_ARCH_64R6;
12471
0
      break;
12472
0
    }
12473
0
  elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12474
0
  elf_elfheader (abfd)->e_flags |= val;
12475
12476
0
}
12477
12478
12479
/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12480
   Don't do so for code sections.  We want to keep ordering of HI16/LO16
12481
   as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
12482
   relocs to be sorted.  */
12483
12484
bool
12485
_bfd_mips_elf_sort_relocs_p (asection *sec)
12486
0
{
12487
0
  return (sec->flags & SEC_CODE) == 0;
12488
0
}
12489
12490
12491
/* The final processing done just before writing out a MIPS ELF object
12492
   file.  This gets the MIPS architecture right based on the machine
12493
   number.  This is used by both the 32-bit and the 64-bit ABI.  */
12494
12495
void
12496
_bfd_mips_final_write_processing (bfd *abfd)
12497
0
{
12498
0
  unsigned int i;
12499
0
  Elf_Internal_Shdr **hdrpp;
12500
0
  const char *name;
12501
0
  asection *sec;
12502
12503
  /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12504
     is nonzero.  This is for compatibility with old objects, which used
12505
     a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
12506
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12507
0
    mips_set_isa_flags (abfd);
12508
12509
  /* Set the sh_info field for .gptab sections and other appropriate
12510
     info for each special section.  */
12511
0
  for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12512
0
       i < elf_numsections (abfd);
12513
0
       i++, hdrpp++)
12514
0
    {
12515
0
      switch ((*hdrpp)->sh_type)
12516
0
  {
12517
0
  case SHT_MIPS_MSYM:
12518
0
  case SHT_MIPS_LIBLIST:
12519
0
    sec = bfd_get_section_by_name (abfd, ".dynstr");
12520
0
    if (sec != NULL)
12521
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12522
0
    break;
12523
12524
0
  case SHT_MIPS_GPTAB:
12525
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12526
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12527
0
    BFD_ASSERT (name != NULL
12528
0
          && startswith (name, ".gptab."));
12529
0
    sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12530
0
    BFD_ASSERT (sec != NULL);
12531
0
    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12532
0
    break;
12533
12534
0
  case SHT_MIPS_CONTENT:
12535
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12536
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12537
0
    BFD_ASSERT (name != NULL
12538
0
          && startswith (name, ".MIPS.content"));
12539
0
    sec = bfd_get_section_by_name (abfd,
12540
0
           name + sizeof ".MIPS.content" - 1);
12541
0
    BFD_ASSERT (sec != NULL);
12542
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12543
0
    break;
12544
12545
0
  case SHT_MIPS_SYMBOL_LIB:
12546
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12547
0
    if (sec != NULL)
12548
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12549
0
    sec = bfd_get_section_by_name (abfd, ".liblist");
12550
0
    if (sec != NULL)
12551
0
      (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12552
0
    break;
12553
12554
0
  case SHT_MIPS_EVENTS:
12555
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12556
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12557
0
    BFD_ASSERT (name != NULL);
12558
0
    if (startswith (name, ".MIPS.events"))
12559
0
      sec = bfd_get_section_by_name (abfd,
12560
0
             name + sizeof ".MIPS.events" - 1);
12561
0
    else
12562
0
      {
12563
0
        BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
12564
0
        sec = bfd_get_section_by_name (abfd,
12565
0
               (name
12566
0
                + sizeof ".MIPS.post_rel" - 1));
12567
0
      }
12568
0
    BFD_ASSERT (sec != NULL);
12569
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12570
0
    break;
12571
12572
0
  case SHT_MIPS_XHASH:
12573
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12574
0
    if (sec != NULL)
12575
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12576
0
  }
12577
0
    }
12578
0
}
12579
12580
bool
12581
_bfd_mips_elf_final_write_processing (bfd *abfd)
12582
0
{
12583
0
  _bfd_mips_final_write_processing (abfd);
12584
0
  return _bfd_elf_final_write_processing (abfd);
12585
0
}
12586

12587
/* When creating an IRIX5 executable, we need REGINFO and RTPROC
12588
   segments.  */
12589
12590
int
12591
_bfd_mips_elf_additional_program_headers (bfd *abfd,
12592
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
12593
0
{
12594
0
  asection *s;
12595
0
  int ret = 0;
12596
12597
  /* See if we need a PT_MIPS_REGINFO segment.  */
12598
0
  s = bfd_get_section_by_name (abfd, ".reginfo");
12599
0
  if (s && (s->flags & SEC_LOAD))
12600
0
    ++ret;
12601
12602
  /* See if we need a PT_MIPS_ABIFLAGS segment.  */
12603
0
  if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12604
0
    ++ret;
12605
12606
  /* See if we need a PT_MIPS_OPTIONS segment.  */
12607
0
  if (IRIX_COMPAT (abfd) == ict_irix6
12608
0
      && bfd_get_section_by_name (abfd,
12609
0
          MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12610
0
    ++ret;
12611
12612
  /* See if we need a PT_MIPS_RTPROC segment.  */
12613
0
  if (IRIX_COMPAT (abfd) == ict_irix5
12614
0
      && bfd_get_section_by_name (abfd, ".dynamic")
12615
0
      && bfd_get_section_by_name (abfd, ".mdebug"))
12616
0
    ++ret;
12617
12618
  /* Allocate a PT_NULL header in dynamic objects.  See
12619
     _bfd_mips_elf_modify_segment_map for details.  */
12620
0
  if (!SGI_COMPAT (abfd)
12621
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12622
0
    ++ret;
12623
12624
0
  return ret;
12625
0
}
12626
12627
/* Modify the segment map for an IRIX5 executable.  */
12628
12629
bool
12630
_bfd_mips_elf_modify_segment_map (bfd *abfd,
12631
          struct bfd_link_info *info)
12632
0
{
12633
0
  asection *s;
12634
0
  struct elf_segment_map *m, **pm;
12635
0
  size_t amt;
12636
12637
  /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12638
     segment.  */
12639
0
  s = bfd_get_section_by_name (abfd, ".reginfo");
12640
0
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12641
0
    {
12642
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12643
0
  if (m->p_type == PT_MIPS_REGINFO)
12644
0
    break;
12645
0
      if (m == NULL)
12646
0
  {
12647
0
    amt = sizeof *m;
12648
0
    m = bfd_zalloc (abfd, amt);
12649
0
    if (m == NULL)
12650
0
      return false;
12651
12652
0
    m->p_type = PT_MIPS_REGINFO;
12653
0
    m->count = 1;
12654
0
    m->sections[0] = s;
12655
12656
    /* We want to put it after the PHDR and INTERP segments.  */
12657
0
    pm = &elf_seg_map (abfd);
12658
0
    while (*pm != NULL
12659
0
     && ((*pm)->p_type == PT_PHDR
12660
0
         || (*pm)->p_type == PT_INTERP))
12661
0
      pm = &(*pm)->next;
12662
12663
0
    m->next = *pm;
12664
0
    *pm = m;
12665
0
  }
12666
0
    }
12667
12668
  /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12669
     segment.  */
12670
0
  s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12671
0
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12672
0
    {
12673
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12674
0
  if (m->p_type == PT_MIPS_ABIFLAGS)
12675
0
    break;
12676
0
      if (m == NULL)
12677
0
  {
12678
0
    amt = sizeof *m;
12679
0
    m = bfd_zalloc (abfd, amt);
12680
0
    if (m == NULL)
12681
0
      return false;
12682
12683
0
    m->p_type = PT_MIPS_ABIFLAGS;
12684
0
    m->count = 1;
12685
0
    m->sections[0] = s;
12686
12687
    /* We want to put it after the PHDR and INTERP segments.  */
12688
0
    pm = &elf_seg_map (abfd);
12689
0
    while (*pm != NULL
12690
0
     && ((*pm)->p_type == PT_PHDR
12691
0
         || (*pm)->p_type == PT_INTERP))
12692
0
      pm = &(*pm)->next;
12693
12694
0
    m->next = *pm;
12695
0
    *pm = m;
12696
0
  }
12697
0
    }
12698
12699
  /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12700
     .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
12701
     PT_MIPS_OPTIONS segment immediately following the program header
12702
     table.  */
12703
0
  if (NEWABI_P (abfd)
12704
      /* On non-IRIX6 new abi, we'll have already created a segment
12705
   for this section, so don't create another.  I'm not sure this
12706
   is not also the case for IRIX 6, but I can't test it right
12707
   now.  */
12708
0
      && IRIX_COMPAT (abfd) == ict_irix6)
12709
0
    {
12710
0
      for (s = abfd->sections; s; s = s->next)
12711
0
  if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12712
0
    break;
12713
12714
0
      if (s)
12715
0
  {
12716
0
    struct elf_segment_map *options_segment;
12717
12718
0
    pm = &elf_seg_map (abfd);
12719
0
    while (*pm != NULL
12720
0
     && ((*pm)->p_type == PT_PHDR
12721
0
         || (*pm)->p_type == PT_INTERP))
12722
0
      pm = &(*pm)->next;
12723
12724
0
    if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12725
0
      {
12726
0
        amt = sizeof (struct elf_segment_map);
12727
0
        options_segment = bfd_zalloc (abfd, amt);
12728
0
        options_segment->next = *pm;
12729
0
        options_segment->p_type = PT_MIPS_OPTIONS;
12730
0
        options_segment->p_flags = PF_R;
12731
0
        options_segment->p_flags_valid = true;
12732
0
        options_segment->count = 1;
12733
0
        options_segment->sections[0] = s;
12734
0
        *pm = options_segment;
12735
0
      }
12736
0
  }
12737
0
    }
12738
0
  else
12739
0
    {
12740
0
      if (IRIX_COMPAT (abfd) == ict_irix5)
12741
0
  {
12742
    /* If there are .dynamic and .mdebug sections, we make a room
12743
       for the RTPROC header.  FIXME: Rewrite without section names.  */
12744
0
    if (bfd_get_section_by_name (abfd, ".interp") == NULL
12745
0
        && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12746
0
        && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12747
0
      {
12748
0
        for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12749
0
    if (m->p_type == PT_MIPS_RTPROC)
12750
0
      break;
12751
0
        if (m == NULL)
12752
0
    {
12753
0
      amt = sizeof *m;
12754
0
      m = bfd_zalloc (abfd, amt);
12755
0
      if (m == NULL)
12756
0
        return false;
12757
12758
0
      m->p_type = PT_MIPS_RTPROC;
12759
12760
0
      s = bfd_get_section_by_name (abfd, ".rtproc");
12761
0
      if (s == NULL)
12762
0
        {
12763
0
          m->count = 0;
12764
0
          m->p_flags = 0;
12765
0
          m->p_flags_valid = 1;
12766
0
        }
12767
0
      else
12768
0
        {
12769
0
          m->count = 1;
12770
0
          m->sections[0] = s;
12771
0
        }
12772
12773
      /* We want to put it after the DYNAMIC segment.  */
12774
0
      pm = &elf_seg_map (abfd);
12775
0
      while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12776
0
        pm = &(*pm)->next;
12777
0
      if (*pm != NULL)
12778
0
        pm = &(*pm)->next;
12779
12780
0
      m->next = *pm;
12781
0
      *pm = m;
12782
0
    }
12783
0
      }
12784
0
  }
12785
      /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12786
   .dynstr, .dynsym, and .hash sections, and everything in
12787
   between.  */
12788
0
      for (pm = &elf_seg_map (abfd); *pm != NULL;
12789
0
     pm = &(*pm)->next)
12790
0
  if ((*pm)->p_type == PT_DYNAMIC)
12791
0
    break;
12792
0
      m = *pm;
12793
      /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12794
   glibc's dynamic linker has traditionally derived the number of
12795
   tags from the p_filesz field, and sometimes allocates stack
12796
   arrays of that size.  An overly-big PT_DYNAMIC segment can
12797
   be actively harmful in such cases.  Making PT_DYNAMIC contain
12798
   other sections can also make life hard for the prelinker,
12799
   which might move one of the other sections to a different
12800
   PT_LOAD segment.  */
12801
0
      if (SGI_COMPAT (abfd)
12802
0
    && m != NULL
12803
0
    && m->count == 1
12804
0
    && strcmp (m->sections[0]->name, ".dynamic") == 0)
12805
0
  {
12806
0
    static const char *sec_names[] =
12807
0
    {
12808
0
      ".dynamic", ".dynstr", ".dynsym", ".hash"
12809
0
    };
12810
0
    bfd_vma low, high;
12811
0
    unsigned int i, c;
12812
0
    struct elf_segment_map *n;
12813
12814
0
    low = ~(bfd_vma) 0;
12815
0
    high = 0;
12816
0
    for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12817
0
      {
12818
0
        s = bfd_get_section_by_name (abfd, sec_names[i]);
12819
0
        if (s != NULL && (s->flags & SEC_LOAD) != 0)
12820
0
    {
12821
0
      bfd_size_type sz;
12822
12823
0
      if (low > s->vma)
12824
0
        low = s->vma;
12825
0
      sz = s->size;
12826
0
      if (high < s->vma + sz)
12827
0
        high = s->vma + sz;
12828
0
    }
12829
0
      }
12830
12831
0
    c = 0;
12832
0
    for (s = abfd->sections; s != NULL; s = s->next)
12833
0
      if ((s->flags & SEC_LOAD) != 0
12834
0
    && s->vma >= low
12835
0
    && s->vma + s->size <= high)
12836
0
        ++c;
12837
12838
0
    amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12839
0
    n = bfd_zalloc (abfd, amt);
12840
0
    if (n == NULL)
12841
0
      return false;
12842
0
    *n = *m;
12843
0
    n->count = c;
12844
12845
0
    i = 0;
12846
0
    for (s = abfd->sections; s != NULL; s = s->next)
12847
0
      {
12848
0
        if ((s->flags & SEC_LOAD) != 0
12849
0
      && s->vma >= low
12850
0
      && s->vma + s->size <= high)
12851
0
    {
12852
0
      n->sections[i] = s;
12853
0
      ++i;
12854
0
    }
12855
0
      }
12856
12857
0
    *pm = n;
12858
0
  }
12859
0
    }
12860
12861
  /* Allocate a spare program header in dynamic objects so that tools
12862
     like the prelinker can add an extra PT_LOAD entry.
12863
12864
     If the prelinker needs to make room for a new PT_LOAD entry, its
12865
     standard procedure is to move the first (read-only) sections into
12866
     the new (writable) segment.  However, the MIPS ABI requires
12867
     .dynamic to be in a read-only segment, and the section will often
12868
     start within sizeof (ElfNN_Phdr) bytes of the last program header.
12869
12870
     Although the prelinker could in principle move .dynamic to a
12871
     writable segment, it seems better to allocate a spare program
12872
     header instead, and avoid the need to move any sections.
12873
     There is a long tradition of allocating spare dynamic tags,
12874
     so allocating a spare program header seems like a natural
12875
     extension.
12876
12877
     If INFO is NULL, we may be copying an already prelinked binary
12878
     with objcopy or strip, so do not add this header.  */
12879
0
  if (info != NULL
12880
0
      && !SGI_COMPAT (abfd)
12881
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12882
0
    {
12883
0
      for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12884
0
  if ((*pm)->p_type == PT_NULL)
12885
0
    break;
12886
0
      if (*pm == NULL)
12887
0
  {
12888
0
    m = bfd_zalloc (abfd, sizeof (*m));
12889
0
    if (m == NULL)
12890
0
      return false;
12891
12892
0
    m->p_type = PT_NULL;
12893
0
    *pm = m;
12894
0
  }
12895
0
    }
12896
12897
0
  return true;
12898
0
}
12899

12900
/* Return the section that should be marked against GC for a given
12901
   relocation.  */
12902
12903
asection *
12904
_bfd_mips_elf_gc_mark_hook (asection *sec,
12905
          struct bfd_link_info *info,
12906
          Elf_Internal_Rela *rel,
12907
          struct elf_link_hash_entry *h,
12908
          Elf_Internal_Sym *sym)
12909
0
{
12910
  /* ??? Do mips16 stub sections need to be handled special?  */
12911
12912
0
  if (h != NULL)
12913
0
    switch (ELF_R_TYPE (sec->owner, rel->r_info))
12914
0
      {
12915
0
      case R_MIPS_GNU_VTINHERIT:
12916
0
      case R_MIPS_GNU_VTENTRY:
12917
0
  return NULL;
12918
0
      }
12919
12920
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12921
0
}
12922
12923
/* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
12924
12925
bool
12926
_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12927
              elf_gc_mark_hook_fn gc_mark_hook)
12928
0
{
12929
0
  bfd *sub;
12930
12931
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12932
12933
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12934
0
    {
12935
0
      asection *o;
12936
12937
0
      if (! is_mips_elf (sub))
12938
0
  continue;
12939
12940
0
      for (o = sub->sections; o != NULL; o = o->next)
12941
0
  if (!o->gc_mark
12942
0
      && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12943
0
    {
12944
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12945
0
        return false;
12946
0
    }
12947
0
    }
12948
12949
0
  return true;
12950
0
}
12951

12952
/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12953
   hiding the old indirect symbol.  Process additional relocation
12954
   information.  Also called for weakdefs, in which case we just let
12955
   _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
12956
12957
void
12958
_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12959
            struct elf_link_hash_entry *dir,
12960
            struct elf_link_hash_entry *ind)
12961
0
{
12962
0
  struct mips_elf_link_hash_entry *dirmips, *indmips;
12963
12964
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12965
12966
0
  dirmips = (struct mips_elf_link_hash_entry *) dir;
12967
0
  indmips = (struct mips_elf_link_hash_entry *) ind;
12968
  /* Any absolute non-dynamic relocations against an indirect or weak
12969
     definition will be against the target symbol.  */
12970
0
  if (indmips->has_static_relocs)
12971
0
    dirmips->has_static_relocs = true;
12972
12973
0
  if (ind->root.type != bfd_link_hash_indirect)
12974
0
    return;
12975
12976
0
  dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12977
0
  if (indmips->readonly_reloc)
12978
0
    dirmips->readonly_reloc = true;
12979
0
  if (indmips->no_fn_stub)
12980
0
    dirmips->no_fn_stub = true;
12981
0
  if (indmips->fn_stub)
12982
0
    {
12983
0
      dirmips->fn_stub = indmips->fn_stub;
12984
0
      indmips->fn_stub = NULL;
12985
0
    }
12986
0
  if (indmips->need_fn_stub)
12987
0
    {
12988
0
      dirmips->need_fn_stub = true;
12989
0
      indmips->need_fn_stub = false;
12990
0
    }
12991
0
  if (indmips->call_stub)
12992
0
    {
12993
0
      dirmips->call_stub = indmips->call_stub;
12994
0
      indmips->call_stub = NULL;
12995
0
    }
12996
0
  if (indmips->call_fp_stub)
12997
0
    {
12998
0
      dirmips->call_fp_stub = indmips->call_fp_stub;
12999
0
      indmips->call_fp_stub = NULL;
13000
0
    }
13001
0
  if (indmips->global_got_area < dirmips->global_got_area)
13002
0
    dirmips->global_got_area = indmips->global_got_area;
13003
0
  if (indmips->global_got_area < GGA_NONE)
13004
0
    indmips->global_got_area = GGA_NONE;
13005
0
  if (indmips->has_nonpic_branches)
13006
0
    dirmips->has_nonpic_branches = true;
13007
0
}
13008
13009
/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13010
   to hide it.  It has to remain global (it will also be protected) so as to
13011
   be assigned a global GOT entry, which will then remain unchanged at load
13012
   time.  */
13013
13014
void
13015
_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13016
         struct elf_link_hash_entry *entry,
13017
         bool force_local)
13018
0
{
13019
0
  struct mips_elf_link_hash_table *htab;
13020
13021
0
  htab = mips_elf_hash_table (info);
13022
0
  BFD_ASSERT (htab != NULL);
13023
0
  if (htab->use_absolute_zero
13024
0
      && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13025
0
    return;
13026
13027
0
  _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13028
0
}
13029

13030
0
#define PDR_SIZE 32
13031
13032
bool
13033
_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13034
          struct bfd_link_info *info)
13035
0
{
13036
0
  asection *o;
13037
0
  bool ret = false;
13038
0
  unsigned char *tdata;
13039
0
  size_t i, skip;
13040
13041
0
  o = bfd_get_section_by_name (abfd, ".pdr");
13042
0
  if (! o)
13043
0
    return false;
13044
0
  if (o->size == 0)
13045
0
    return false;
13046
0
  if (o->size % PDR_SIZE != 0)
13047
0
    return false;
13048
0
  if (o->output_section != NULL
13049
0
      && bfd_is_abs_section (o->output_section))
13050
0
    return false;
13051
13052
0
  tdata = bfd_zmalloc (o->size / PDR_SIZE);
13053
0
  if (! tdata)
13054
0
    return false;
13055
13056
0
  cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13057
0
              info->keep_memory);
13058
0
  if (!cookie->rels)
13059
0
    {
13060
0
      free (tdata);
13061
0
      return false;
13062
0
    }
13063
13064
0
  cookie->rel = cookie->rels;
13065
0
  cookie->relend = cookie->rels + o->reloc_count;
13066
13067
0
  for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13068
0
    {
13069
0
      if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13070
0
  {
13071
0
    tdata[i] = 1;
13072
0
    skip ++;
13073
0
  }
13074
0
    }
13075
13076
0
  if (skip != 0)
13077
0
    {
13078
0
      mips_elf_section_data (o)->u.tdata = tdata;
13079
0
      if (o->rawsize == 0)
13080
0
  o->rawsize = o->size;
13081
0
      o->size -= skip * PDR_SIZE;
13082
0
      ret = true;
13083
0
    }
13084
0
  else
13085
0
    free (tdata);
13086
13087
0
  if (! info->keep_memory)
13088
0
    free (cookie->rels);
13089
13090
0
  return ret;
13091
0
}
13092
13093
bool
13094
_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13095
0
{
13096
0
  if (strcmp (sec->name, ".pdr") == 0)
13097
0
    return true;
13098
0
  return false;
13099
0
}
13100
13101
bool
13102
_bfd_mips_elf_write_section (bfd *output_bfd,
13103
           struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13104
           asection *sec, bfd_byte *contents)
13105
0
{
13106
0
  bfd_byte *to, *from, *end;
13107
0
  int i;
13108
13109
0
  if (strcmp (sec->name, ".pdr") != 0)
13110
0
    return false;
13111
13112
0
  if (mips_elf_section_data (sec)->u.tdata == NULL)
13113
0
    return false;
13114
13115
0
  to = contents;
13116
0
  end = contents + sec->size;
13117
0
  for (from = contents, i = 0;
13118
0
       from < end;
13119
0
       from += PDR_SIZE, i++)
13120
0
    {
13121
0
      if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13122
0
  continue;
13123
0
      if (to != from)
13124
0
  memcpy (to, from, PDR_SIZE);
13125
0
      to += PDR_SIZE;
13126
0
    }
13127
0
  bfd_set_section_contents (output_bfd, sec->output_section, contents,
13128
0
          sec->output_offset, sec->size);
13129
0
  return true;
13130
0
}
13131

13132
/* microMIPS code retains local labels for linker relaxation.  Omit them
13133
   from output by default for clarity.  */
13134
13135
bool
13136
_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13137
0
{
13138
0
  return _bfd_elf_is_local_label_name (abfd, sym->name);
13139
0
}
13140
13141
bool
13142
_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13143
         asection *section, bfd_vma offset,
13144
         const char **filename_ptr,
13145
         const char **functionname_ptr,
13146
         unsigned int *line_ptr,
13147
         unsigned int *discriminator_ptr)
13148
0
{
13149
0
  asection *msec;
13150
13151
0
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13152
0
             filename_ptr, functionname_ptr,
13153
0
             line_ptr, discriminator_ptr,
13154
0
             dwarf_debug_sections,
13155
0
             &elf_tdata (abfd)->dwarf2_find_line_info)
13156
0
      == 1)
13157
0
    return true;
13158
13159
0
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13160
0
             filename_ptr, functionname_ptr,
13161
0
             line_ptr))
13162
0
    {
13163
0
      if (!*functionname_ptr)
13164
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
13165
0
        *filename_ptr ? NULL : filename_ptr,
13166
0
        functionname_ptr);
13167
0
      return true;
13168
0
    }
13169
13170
0
  msec = bfd_get_section_by_name (abfd, ".mdebug");
13171
0
  if (msec != NULL)
13172
0
    {
13173
0
      flagword origflags;
13174
0
      struct mips_elf_find_line *fi;
13175
0
      const struct ecoff_debug_swap * const swap =
13176
0
  get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13177
13178
      /* If we are called during a link, mips_elf_final_link may have
13179
   cleared the SEC_HAS_CONTENTS field.  We force it back on here
13180
   if appropriate (which it normally will be).  */
13181
0
      origflags = msec->flags;
13182
0
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13183
0
  msec->flags |= SEC_HAS_CONTENTS;
13184
13185
0
      fi = mips_elf_tdata (abfd)->find_line_info;
13186
0
      if (fi == NULL)
13187
0
  {
13188
0
    bfd_size_type external_fdr_size;
13189
0
    char *fraw_src;
13190
0
    char *fraw_end;
13191
0
    struct fdr *fdr_ptr;
13192
0
    bfd_size_type amt = sizeof (struct mips_elf_find_line);
13193
13194
0
    fi = bfd_zalloc (abfd, amt);
13195
0
    if (fi == NULL)
13196
0
      {
13197
0
        msec->flags = origflags;
13198
0
        return false;
13199
0
      }
13200
13201
0
    if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13202
0
      {
13203
0
        msec->flags = origflags;
13204
0
        return false;
13205
0
      }
13206
13207
    /* Swap in the FDR information.  */
13208
0
    amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13209
0
    fi->d.fdr = bfd_alloc (abfd, amt);
13210
0
    if (fi->d.fdr == NULL)
13211
0
      {
13212
0
        _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13213
0
        msec->flags = origflags;
13214
0
        return false;
13215
0
      }
13216
0
    external_fdr_size = swap->external_fdr_size;
13217
0
    fdr_ptr = fi->d.fdr;
13218
0
    fraw_src = (char *) fi->d.external_fdr;
13219
0
    fraw_end = (fraw_src
13220
0
          + fi->d.symbolic_header.ifdMax * external_fdr_size);
13221
0
    for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13222
0
      (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13223
13224
0
    mips_elf_tdata (abfd)->find_line_info = fi;
13225
0
  }
13226
13227
0
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13228
0
          &fi->i, filename_ptr, functionname_ptr,
13229
0
          line_ptr))
13230
0
  {
13231
0
    msec->flags = origflags;
13232
0
    return true;
13233
0
  }
13234
13235
0
      msec->flags = origflags;
13236
0
    }
13237
13238
  /* Fall back on the generic ELF find_nearest_line routine.  */
13239
13240
0
  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13241
0
             filename_ptr, functionname_ptr,
13242
0
             line_ptr, discriminator_ptr);
13243
0
}
13244
13245
bool
13246
_bfd_mips_elf_find_inliner_info (bfd *abfd,
13247
         const char **filename_ptr,
13248
         const char **functionname_ptr,
13249
         unsigned int *line_ptr)
13250
0
{
13251
0
  bool found;
13252
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13253
0
           functionname_ptr, line_ptr,
13254
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
13255
0
  return found;
13256
0
}
13257
13258

13259
/* When are writing out the .options or .MIPS.options section,
13260
   remember the bytes we are writing out, so that we can install the
13261
   GP value in the section_processing routine.  */
13262
13263
bool
13264
_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13265
            const void *location,
13266
            file_ptr offset, bfd_size_type count)
13267
0
{
13268
0
  if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13269
0
    {
13270
0
      bfd_byte *c;
13271
13272
0
      if (elf_section_data (section) == NULL)
13273
0
  {
13274
0
    size_t amt = sizeof (struct bfd_elf_section_data);
13275
0
    section->used_by_bfd = bfd_zalloc (abfd, amt);
13276
0
    if (elf_section_data (section) == NULL)
13277
0
      return false;
13278
0
  }
13279
0
      c = mips_elf_section_data (section)->u.tdata;
13280
0
      if (c == NULL)
13281
0
  {
13282
0
    c = bfd_zalloc (abfd, section->size);
13283
0
    if (c == NULL)
13284
0
      return false;
13285
0
    mips_elf_section_data (section)->u.tdata = c;
13286
0
  }
13287
13288
0
      memcpy (c + offset, location, count);
13289
0
    }
13290
13291
0
  return _bfd_elf_set_section_contents (abfd, section, location, offset,
13292
0
          count);
13293
0
}
13294
13295
/* This is almost identical to bfd_generic_get_... except that some
13296
   MIPS relocations need to be handled specially.  Sigh.  */
13297
13298
bfd_byte *
13299
_bfd_elf_mips_get_relocated_section_contents
13300
  (bfd *abfd,
13301
   struct bfd_link_info *link_info,
13302
   struct bfd_link_order *link_order,
13303
   bfd_byte *data,
13304
   bool relocatable,
13305
   asymbol **symbols)
13306
0
{
13307
0
  bfd *input_bfd = link_order->u.indirect.section->owner;
13308
0
  asection *input_section = link_order->u.indirect.section;
13309
0
  long reloc_size;
13310
0
  arelent **reloc_vector;
13311
0
  long reloc_count;
13312
13313
0
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13314
0
  if (reloc_size < 0)
13315
0
    return NULL;
13316
13317
  /* Read in the section.  */
13318
0
  bfd_byte *orig_data = data;
13319
0
  if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13320
0
    return NULL;
13321
13322
0
  if (data == NULL)
13323
0
    return NULL;
13324
13325
0
  if (reloc_size == 0)
13326
0
    return data;
13327
13328
0
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13329
0
  if (reloc_vector == NULL)
13330
0
    {
13331
0
      struct mips_elf_obj_tdata *tdata;
13332
0
      struct mips_hi16 **hip, *hi;
13333
0
    error_return:
13334
      /* If we are going to return an error, remove entries on
13335
   mips_hi16_list that point into this section's data.  Data
13336
   will typically be freed on return from this function.  */
13337
0
      tdata = mips_elf_tdata (abfd);
13338
0
      hip = &tdata->mips_hi16_list;
13339
0
      while ((hi = *hip) != NULL)
13340
0
  {
13341
0
    if (hi->input_section == input_section)
13342
0
      {
13343
0
        *hip = hi->next;
13344
0
        free (hi);
13345
0
      }
13346
0
    else
13347
0
      hip = &hi->next;
13348
0
  }
13349
0
      if (orig_data == NULL)
13350
0
  free (data);
13351
0
      data = NULL;
13352
0
      goto out;
13353
0
    }
13354
13355
0
  reloc_count = bfd_canonicalize_reloc (input_bfd,
13356
0
          input_section,
13357
0
          reloc_vector,
13358
0
          symbols);
13359
0
  if (reloc_count < 0)
13360
0
    goto error_return;
13361
13362
0
  if (reloc_count > 0)
13363
0
    {
13364
0
      arelent **parent;
13365
      /* for mips */
13366
0
      int gp_found;
13367
0
      bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
13368
13369
0
      {
13370
0
  struct bfd_hash_entry *h;
13371
0
  struct bfd_link_hash_entry *lh;
13372
  /* Skip all this stuff if we aren't mixing formats.  */
13373
0
  if (abfd && input_bfd
13374
0
      && abfd->xvec == input_bfd->xvec)
13375
0
    lh = 0;
13376
0
  else
13377
0
    {
13378
0
      h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13379
0
      lh = (struct bfd_link_hash_entry *) h;
13380
0
    }
13381
0
      lookup:
13382
0
  if (lh)
13383
0
    {
13384
0
      switch (lh->type)
13385
0
        {
13386
0
        case bfd_link_hash_undefined:
13387
0
        case bfd_link_hash_undefweak:
13388
0
        case bfd_link_hash_common:
13389
0
    gp_found = 0;
13390
0
    break;
13391
0
        case bfd_link_hash_defined:
13392
0
        case bfd_link_hash_defweak:
13393
0
    gp_found = 1;
13394
0
    gp = lh->u.def.value;
13395
0
    break;
13396
0
        case bfd_link_hash_indirect:
13397
0
        case bfd_link_hash_warning:
13398
0
    lh = lh->u.i.link;
13399
    /* @@FIXME  ignoring warning for now */
13400
0
    goto lookup;
13401
0
        case bfd_link_hash_new:
13402
0
        default:
13403
0
    abort ();
13404
0
        }
13405
0
    }
13406
0
  else
13407
0
    gp_found = 0;
13408
0
      }
13409
      /* end mips */
13410
13411
0
      for (parent = reloc_vector; *parent != NULL; parent++)
13412
0
  {
13413
0
    char *error_message = NULL;
13414
0
    asymbol *symbol;
13415
0
    bfd_reloc_status_type r;
13416
13417
0
    symbol = *(*parent)->sym_ptr_ptr;
13418
    /* PR ld/19628: A specially crafted input file
13419
       can result in a NULL symbol pointer here.  */
13420
0
    if (symbol == NULL)
13421
0
      {
13422
0
        link_info->callbacks->einfo
13423
    /* xgettext:c-format */
13424
0
    (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13425
0
     abfd, input_section, (* parent)->address);
13426
0
        goto error_return;
13427
0
      }
13428
13429
    /* Zap reloc field when the symbol is from a discarded
13430
       section, ignoring any addend.  Do the same when called
13431
       from bfd_simple_get_relocated_section_contents for
13432
       undefined symbols in debug sections.  This is to keep
13433
       debug info reasonably sane, in particular so that
13434
       DW_FORM_ref_addr to another file's .debug_info isn't
13435
       confused with an offset into the current file's
13436
       .debug_info.  */
13437
0
    if ((symbol->section != NULL && discarded_section (symbol->section))
13438
0
        || (symbol->section == bfd_und_section_ptr
13439
0
      && (input_section->flags & SEC_DEBUGGING) != 0
13440
0
      && link_info->input_bfds == link_info->output_bfd))
13441
0
      {
13442
0
        bfd_vma off;
13443
0
        static reloc_howto_type none_howto
13444
0
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13445
0
       "unused", false, 0, 0, false);
13446
13447
0
        off = ((*parent)->address
13448
0
         * bfd_octets_per_byte (input_bfd, input_section));
13449
0
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13450
0
           input_section, data, off);
13451
0
        (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13452
0
        (*parent)->addend = 0;
13453
0
        (*parent)->howto = &none_howto;
13454
0
        r = bfd_reloc_ok;
13455
0
      }
13456
13457
    /* Specific to MIPS: Deal with relocation types that require
13458
       knowing the gp of the output bfd.  */
13459
13460
    /* If we've managed to find the gp and have a special
13461
       function for the relocation then go ahead, else default
13462
       to the generic handling.  */
13463
0
    else if (gp_found
13464
0
       && ((*parent)->howto->special_function
13465
0
           == _bfd_mips_elf32_gprel16_reloc))
13466
0
      r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13467
0
                 input_section, relocatable,
13468
0
                 data, gp);
13469
0
    else
13470
0
      r = bfd_perform_relocation (input_bfd,
13471
0
          *parent,
13472
0
          data,
13473
0
          input_section,
13474
0
          relocatable ? abfd : NULL,
13475
0
          &error_message);
13476
13477
0
    if (relocatable)
13478
0
      {
13479
0
        asection *os = input_section->output_section;
13480
13481
        /* A partial link, so keep the relocs.  */
13482
0
        os->orelocation[os->reloc_count] = *parent;
13483
0
        os->reloc_count++;
13484
0
      }
13485
13486
0
    if (r != bfd_reloc_ok)
13487
0
      {
13488
0
        switch (r)
13489
0
    {
13490
0
    case bfd_reloc_undefined:
13491
0
      (*link_info->callbacks->undefined_symbol)
13492
0
        (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13493
0
         input_bfd, input_section, (*parent)->address, true);
13494
0
      break;
13495
0
    case bfd_reloc_dangerous:
13496
0
      BFD_ASSERT (error_message != NULL);
13497
0
      (*link_info->callbacks->reloc_dangerous)
13498
0
        (link_info, error_message,
13499
0
         input_bfd, input_section, (*parent)->address);
13500
0
      break;
13501
0
    case bfd_reloc_overflow:
13502
0
      (*link_info->callbacks->reloc_overflow)
13503
0
        (link_info, NULL,
13504
0
         bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13505
0
         (*parent)->howto->name, (*parent)->addend,
13506
0
         input_bfd, input_section, (*parent)->address);
13507
0
      break;
13508
0
    case bfd_reloc_outofrange:
13509
      /* PR ld/13730:
13510
         This error can result when processing some partially
13511
         complete binaries.  Do not abort, but issue an error
13512
         message instead.  */
13513
0
      link_info->callbacks->einfo
13514
        /* xgettext:c-format */
13515
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13516
0
         abfd, input_section, * parent);
13517
0
      goto error_return;
13518
13519
0
    case bfd_reloc_notsupported:
13520
      /* PR ld/17512
13521
         This error can result when processing a corrupt binary.
13522
         Do not abort.  Issue an error message instead.  */
13523
0
      link_info->callbacks->einfo
13524
        /* xgettext:c-format */
13525
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13526
0
         abfd, input_section, * parent);
13527
0
      goto error_return;
13528
13529
0
    default:
13530
      /* PR 17512; file: 90c2a92e.
13531
         Report unexpected results, without aborting.  */
13532
0
      link_info->callbacks->einfo
13533
        /* xgettext:c-format */
13534
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13535
0
         abfd, input_section, * parent, r);
13536
0
      break;
13537
0
    }
13538
13539
0
      }
13540
0
  }
13541
0
    }
13542
13543
0
 out:
13544
0
  free (reloc_vector);
13545
0
  return data;
13546
0
}
13547

13548
static bool
13549
mips_elf_relax_delete_bytes (bfd *abfd,
13550
           asection *sec, bfd_vma addr, int count)
13551
0
{
13552
0
  Elf_Internal_Shdr *symtab_hdr;
13553
0
  unsigned int sec_shndx;
13554
0
  bfd_byte *contents;
13555
0
  Elf_Internal_Rela *irel, *irelend;
13556
0
  Elf_Internal_Sym *isym;
13557
0
  Elf_Internal_Sym *isymend;
13558
0
  struct elf_link_hash_entry **sym_hashes;
13559
0
  struct elf_link_hash_entry **end_hashes;
13560
0
  struct elf_link_hash_entry **start_hashes;
13561
0
  unsigned int symcount;
13562
13563
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13564
0
  contents = elf_section_data (sec)->this_hdr.contents;
13565
13566
0
  irel = elf_section_data (sec)->relocs;
13567
0
  irelend = irel + sec->reloc_count;
13568
13569
  /* Actually delete the bytes.  */
13570
0
  memmove (contents + addr, contents + addr + count,
13571
0
     (size_t) (sec->size - addr - count));
13572
0
  sec->size -= count;
13573
13574
  /* Adjust all the relocs.  */
13575
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13576
0
    {
13577
      /* Get the new reloc address.  */
13578
0
      if (irel->r_offset > addr)
13579
0
  irel->r_offset -= count;
13580
0
    }
13581
13582
0
  BFD_ASSERT (addr % 2 == 0);
13583
0
  BFD_ASSERT (count % 2 == 0);
13584
13585
  /* Adjust the local symbols defined in this section.  */
13586
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13587
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13588
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13589
0
    if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13590
0
      isym->st_value -= count;
13591
13592
  /* Now adjust the global symbols defined in this section.  */
13593
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13594
0
        - symtab_hdr->sh_info);
13595
0
  sym_hashes = start_hashes = elf_sym_hashes (abfd);
13596
0
  end_hashes = sym_hashes + symcount;
13597
13598
0
  for (; sym_hashes < end_hashes; sym_hashes++)
13599
0
    {
13600
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
13601
13602
0
      if ((sym_hash->root.type == bfd_link_hash_defined
13603
0
     || sym_hash->root.type == bfd_link_hash_defweak)
13604
0
    && sym_hash->root.u.def.section == sec)
13605
0
  {
13606
0
    bfd_vma value = sym_hash->root.u.def.value;
13607
13608
0
    if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13609
0
      value &= MINUS_TWO;
13610
0
    if (value > addr)
13611
0
      sym_hash->root.u.def.value -= count;
13612
0
  }
13613
0
    }
13614
13615
0
  return true;
13616
0
}
13617
13618
13619
/* Opcodes needed for microMIPS relaxation as found in
13620
   opcodes/micromips-opc.c.  */
13621
13622
struct opcode_descriptor {
13623
  unsigned long match;
13624
  unsigned long mask;
13625
};
13626
13627
/* The $ra register aka $31.  */
13628
13629
0
#define RA 31
13630
13631
/* 32-bit instruction format register fields.  */
13632
13633
0
#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13634
0
#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13635
13636
/* Check if a 5-bit register index can be abbreviated to 3 bits.  */
13637
13638
#define OP16_VALID_REG(r) \
13639
0
  ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13640
13641
13642
/* 32-bit and 16-bit branches.  */
13643
13644
static const struct opcode_descriptor b_insns_32[] = {
13645
  { /* "b", "p",    */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13646
  { /* "b", "p",    */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13647
  { 0, 0 }  /* End marker for find_match().  */
13648
};
13649
13650
static const struct opcode_descriptor bc_insn_32 =
13651
  { /* "bc(1|2)(ft)", "N,p",  */ 0x42800000, 0xfec30000 };
13652
13653
static const struct opcode_descriptor bz_insn_32 =
13654
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13655
13656
static const struct opcode_descriptor bzal_insn_32 =
13657
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 };
13658
13659
static const struct opcode_descriptor beq_insn_32 =
13660
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13661
13662
static const struct opcode_descriptor b_insn_16 =
13663
  { /* "b", "mD",   */ 0xcc00,     0xfc00 };
13664
13665
static const struct opcode_descriptor bz_insn_16 =
13666
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 };
13667
13668
13669
/* 32-bit and 16-bit branch EQ and NE zero.  */
13670
13671
/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13672
   eq and second the ne.  This convention is used when replacing a
13673
   32-bit BEQ/BNE with the 16-bit version.  */
13674
13675
0
#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13676
13677
static const struct opcode_descriptor bz_rs_insns_32[] = {
13678
  { /* "beqz",  "s,p",    */ 0x94000000, 0xffe00000 },
13679
  { /* "bnez",  "s,p",    */ 0xb4000000, 0xffe00000 },
13680
  { 0, 0 }  /* End marker for find_match().  */
13681
};
13682
13683
static const struct opcode_descriptor bz_rt_insns_32[] = {
13684
  { /* "beqz",  "t,p",    */ 0x94000000, 0xfc01f000 },
13685
  { /* "bnez",  "t,p",    */ 0xb4000000, 0xfc01f000 },
13686
  { 0, 0 }  /* End marker for find_match().  */
13687
};
13688
13689
static const struct opcode_descriptor bzc_insns_32[] = {
13690
  { /* "beqzc", "s,p",    */ 0x40e00000, 0xffe00000 },
13691
  { /* "bnezc", "s,p",    */ 0x40a00000, 0xffe00000 },
13692
  { 0, 0 }  /* End marker for find_match().  */
13693
};
13694
13695
static const struct opcode_descriptor bz_insns_16[] = {
13696
  { /* "beqz",  "md,mE",  */ 0x8c00,     0xfc00 },
13697
  { /* "bnez",  "md,mE",  */ 0xac00,     0xfc00 },
13698
  { 0, 0 }  /* End marker for find_match().  */
13699
};
13700
13701
/* Switch between a 5-bit register index and its 3-bit shorthand.  */
13702
13703
0
#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13704
#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13705
13706
13707
/* 32-bit instructions with a delay slot.  */
13708
13709
static const struct opcode_descriptor jal_insn_32_bd16 =
13710
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 };
13711
13712
static const struct opcode_descriptor jal_insn_32_bd32 =
13713
  { /* "jal", "a",    */ 0xf4000000, 0xfc000000 };
13714
13715
static const struct opcode_descriptor jal_x_insn_32_bd32 =
13716
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 };
13717
13718
static const struct opcode_descriptor j_insn_32 =
13719
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 };
13720
13721
static const struct opcode_descriptor jalr_insn_32 =
13722
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff };
13723
13724
/* This table can be compacted, because no opcode replacement is made.  */
13725
13726
static const struct opcode_descriptor ds_insns_32_bd16[] = {
13727
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 },
13728
13729
  { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13730
  { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13731
13732
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13733
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13734
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 },
13735
  { 0, 0 }  /* End marker for find_match().  */
13736
};
13737
13738
/* This table can be compacted, because no opcode replacement is made.  */
13739
13740
static const struct opcode_descriptor ds_insns_32_bd32[] = {
13741
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 },
13742
13743
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff },
13744
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 },
13745
  { 0, 0 }  /* End marker for find_match().  */
13746
};
13747
13748
13749
/* 16-bit instructions with a delay slot.  */
13750
13751
static const struct opcode_descriptor jalr_insn_16_bd16 =
13752
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 };
13753
13754
static const struct opcode_descriptor jalr_insn_16_bd32 =
13755
  { /* "jalr",  "my,mj",  */ 0x45c0,     0xffe0 };
13756
13757
static const struct opcode_descriptor jr_insn_16 =
13758
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 };
13759
13760
0
#define JR16_REG(opcode) ((opcode) & 0x1f)
13761
13762
/* This table can be compacted, because no opcode replacement is made.  */
13763
13764
static const struct opcode_descriptor ds_insns_16_bd16[] = {
13765
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 },
13766
13767
  { /* "b", "mD",   */ 0xcc00,     0xfc00 },
13768
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 },
13769
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 },
13770
  { 0, 0 }  /* End marker for find_match().  */
13771
};
13772
13773
13774
/* LUI instruction.  */
13775
13776
static const struct opcode_descriptor lui_insn =
13777
 { /* "lui",  "s,u",    */ 0x41a00000, 0xffe00000 };
13778
13779
13780
/* ADDIU instruction.  */
13781
13782
static const struct opcode_descriptor addiu_insn =
13783
  { /* "addiu", "t,r,j",  */ 0x30000000, 0xfc000000 };
13784
13785
static const struct opcode_descriptor addiupc_insn =
13786
  { /* "addiu", "mb,$pc,mQ",  */ 0x78000000, 0xfc000000 };
13787
13788
#define ADDIUPC_REG_FIELD(r) \
13789
0
  (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13790
13791
13792
/* Relaxable instructions in a JAL delay slot: MOVE.  */
13793
13794
/* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
13795
   (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
13796
#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13797
#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13798
13799
#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13800
#define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
13801
13802
static const struct opcode_descriptor move_insns_32[] = {
13803
  { /* "move",  "d,s",    */ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
13804
  { /* "move",  "d,s",    */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13805
  { 0, 0 }  /* End marker for find_match().  */
13806
};
13807
13808
static const struct opcode_descriptor move_insn_16 =
13809
  { /* "move",  "mp,mj",  */ 0x0c00,     0xfc00 };
13810
13811
13812
/* NOP instructions.  */
13813
13814
static const struct opcode_descriptor nop_insn_32 =
13815
  { /* "nop", "",   */ 0x00000000, 0xffffffff };
13816
13817
static const struct opcode_descriptor nop_insn_16 =
13818
  { /* "nop", "",   */ 0x0c00,     0xffff };
13819
13820
13821
/* Instruction match support.  */
13822
13823
0
#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13824
13825
static int
13826
find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13827
0
{
13828
0
  unsigned long indx;
13829
13830
0
  for (indx = 0; insn[indx].mask != 0; indx++)
13831
0
    if (MATCH (opcode, insn[indx]))
13832
0
      return indx;
13833
13834
0
  return -1;
13835
0
}
13836
13837
13838
/* Branch and delay slot decoding support.  */
13839
13840
/* If PTR points to what *might* be a 16-bit branch or jump, then
13841
   return the minimum length of its delay slot, otherwise return 0.
13842
   Non-zero results are not definitive as we might be checking against
13843
   the second half of another instruction.  */
13844
13845
static int
13846
check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13847
0
{
13848
0
  unsigned long opcode;
13849
0
  int bdsize;
13850
13851
0
  opcode = bfd_get_16 (abfd, ptr);
13852
0
  if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13853
    /* 16-bit branch/jump with a 32-bit delay slot.  */
13854
0
    bdsize = 4;
13855
0
  else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13856
0
     || find_match (opcode, ds_insns_16_bd16) >= 0)
13857
    /* 16-bit branch/jump with a 16-bit delay slot.  */
13858
0
    bdsize = 2;
13859
0
  else
13860
    /* No delay slot.  */
13861
0
    bdsize = 0;
13862
13863
0
  return bdsize;
13864
0
}
13865
13866
/* If PTR points to what *might* be a 32-bit branch or jump, then
13867
   return the minimum length of its delay slot, otherwise return 0.
13868
   Non-zero results are not definitive as we might be checking against
13869
   the second half of another instruction.  */
13870
13871
static int
13872
check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13873
0
{
13874
0
  unsigned long opcode;
13875
0
  int bdsize;
13876
13877
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13878
0
  if (find_match (opcode, ds_insns_32_bd32) >= 0)
13879
    /* 32-bit branch/jump with a 32-bit delay slot.  */
13880
0
    bdsize = 4;
13881
0
  else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13882
    /* 32-bit branch/jump with a 16-bit delay slot.  */
13883
0
    bdsize = 2;
13884
0
  else
13885
    /* No delay slot.  */
13886
0
    bdsize = 0;
13887
13888
0
  return bdsize;
13889
0
}
13890
13891
/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13892
   that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
13893
13894
static bool
13895
check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13896
0
{
13897
0
  unsigned long opcode;
13898
13899
0
  opcode = bfd_get_16 (abfd, ptr);
13900
0
  if (MATCH (opcode, b_insn_16)
13901
            /* B16  */
13902
0
      || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13903
            /* JR16  */
13904
0
      || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13905
            /* BEQZ16, BNEZ16  */
13906
0
      || (MATCH (opcode, jalr_insn_16_bd32)
13907
            /* JALR16  */
13908
0
    && reg != JR16_REG (opcode) && reg != RA))
13909
0
    return true;
13910
13911
0
  return false;
13912
0
}
13913
13914
/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13915
   then return TRUE, otherwise FALSE.  */
13916
13917
static bool
13918
check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13919
0
{
13920
0
  unsigned long opcode;
13921
13922
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13923
0
  if (MATCH (opcode, j_insn_32)
13924
            /* J  */
13925
0
      || MATCH (opcode, bc_insn_32)
13926
            /* BC1F, BC1T, BC2F, BC2T  */
13927
0
      || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13928
            /* JAL, JALX  */
13929
0
      || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13930
            /* BGEZ, BGTZ, BLEZ, BLTZ  */
13931
0
      || (MATCH (opcode, bzal_insn_32)
13932
            /* BGEZAL, BLTZAL  */
13933
0
    && reg != OP32_SREG (opcode) && reg != RA)
13934
0
      || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13935
            /* JALR, JALR.HB, BEQ, BNE  */
13936
0
    && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13937
0
    return true;
13938
13939
0
  return false;
13940
0
}
13941
13942
/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13943
   IRELEND) at OFFSET indicate that there must be a compact branch there,
13944
   then return TRUE, otherwise FALSE.  */
13945
13946
static bool
13947
check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13948
         const Elf_Internal_Rela *internal_relocs,
13949
         const Elf_Internal_Rela *irelend)
13950
0
{
13951
0
  const Elf_Internal_Rela *irel;
13952
0
  unsigned long opcode;
13953
13954
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13955
0
  if (find_match (opcode, bzc_insns_32) < 0)
13956
0
    return false;
13957
13958
0
  for (irel = internal_relocs; irel < irelend; irel++)
13959
0
    if (irel->r_offset == offset
13960
0
  && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13961
0
      return true;
13962
13963
0
  return false;
13964
0
}
13965
13966
/* Bitsize checking.  */
13967
#define IS_BITSIZE(val, N)            \
13968
0
  (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))   \
13969
0
    - (1ULL << ((N) - 1))) == (val))
13970
13971

13972
bool
13973
_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13974
           struct bfd_link_info *link_info,
13975
           bool *again)
13976
0
{
13977
0
  bool insn32 = mips_elf_hash_table (link_info)->insn32;
13978
0
  Elf_Internal_Shdr *symtab_hdr;
13979
0
  Elf_Internal_Rela *internal_relocs;
13980
0
  Elf_Internal_Rela *irel, *irelend;
13981
0
  bfd_byte *contents = NULL;
13982
0
  Elf_Internal_Sym *isymbuf = NULL;
13983
13984
  /* Assume nothing changes.  */
13985
0
  *again = false;
13986
13987
  /* We don't have to do anything for a relocatable link, if
13988
     this section does not have relocs, or if this is not a
13989
     code section.  */
13990
13991
0
  if (bfd_link_relocatable (link_info)
13992
0
      || sec->reloc_count == 0
13993
0
      || (sec->flags & SEC_RELOC) == 0
13994
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
13995
0
      || (sec->flags & SEC_CODE) == 0)
13996
0
    return true;
13997
13998
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13999
14000
  /* Get a copy of the native relocations.  */
14001
0
  internal_relocs = (_bfd_elf_link_read_relocs
14002
0
         (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14003
0
          link_info->keep_memory));
14004
0
  if (internal_relocs == NULL)
14005
0
    goto error_return;
14006
14007
  /* Walk through them looking for relaxing opportunities.  */
14008
0
  irelend = internal_relocs + sec->reloc_count;
14009
0
  for (irel = internal_relocs; irel < irelend; irel++)
14010
0
    {
14011
0
      unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14012
0
      unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14013
0
      bool target_is_micromips_code_p;
14014
0
      unsigned long opcode;
14015
0
      bfd_vma symval;
14016
0
      bfd_vma pcrval;
14017
0
      bfd_byte *ptr;
14018
0
      int fndopc;
14019
14020
      /* The number of bytes to delete for relaxation and from where
14021
   to delete these bytes starting at irel->r_offset.  */
14022
0
      int delcnt = 0;
14023
0
      int deloff = 0;
14024
14025
      /* If this isn't something that can be relaxed, then ignore
14026
   this reloc.  */
14027
0
      if (r_type != R_MICROMIPS_HI16
14028
0
    && r_type != R_MICROMIPS_PC16_S1
14029
0
    && r_type != R_MICROMIPS_26_S1)
14030
0
  continue;
14031
14032
      /* Get the section contents if we haven't done so already.  */
14033
0
      if (contents == NULL)
14034
0
  {
14035
    /* Get cached copy if it exists.  */
14036
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
14037
0
      contents = elf_section_data (sec)->this_hdr.contents;
14038
    /* Go get them off disk.  */
14039
0
    else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14040
0
      goto error_return;
14041
0
  }
14042
0
      ptr = contents + irel->r_offset;
14043
14044
      /* Read this BFD's local symbols if we haven't done so already.  */
14045
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14046
0
  {
14047
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14048
0
    if (isymbuf == NULL)
14049
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14050
0
              symtab_hdr->sh_info, 0,
14051
0
              NULL, NULL, NULL);
14052
0
    if (isymbuf == NULL)
14053
0
      goto error_return;
14054
0
  }
14055
14056
      /* Get the value of the symbol referred to by the reloc.  */
14057
0
      if (r_symndx < symtab_hdr->sh_info)
14058
0
  {
14059
    /* A local symbol.  */
14060
0
    Elf_Internal_Sym *isym;
14061
0
    asection *sym_sec;
14062
14063
0
    isym = isymbuf + r_symndx;
14064
0
    if (isym->st_shndx == SHN_UNDEF)
14065
0
      sym_sec = bfd_und_section_ptr;
14066
0
    else if (isym->st_shndx == SHN_ABS)
14067
0
      sym_sec = bfd_abs_section_ptr;
14068
0
    else if (isym->st_shndx == SHN_COMMON)
14069
0
      sym_sec = bfd_com_section_ptr;
14070
0
    else
14071
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14072
0
    symval = (isym->st_value
14073
0
        + sym_sec->output_section->vma
14074
0
        + sym_sec->output_offset);
14075
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14076
0
  }
14077
0
      else
14078
0
  {
14079
0
    unsigned long indx;
14080
0
    struct elf_link_hash_entry *h;
14081
14082
    /* An external symbol.  */
14083
0
    indx = r_symndx - symtab_hdr->sh_info;
14084
0
    h = elf_sym_hashes (abfd)[indx];
14085
0
    BFD_ASSERT (h != NULL);
14086
14087
0
    if (h->root.type != bfd_link_hash_defined
14088
0
        && h->root.type != bfd_link_hash_defweak)
14089
      /* This appears to be a reference to an undefined
14090
         symbol.  Just ignore it -- it will be caught by the
14091
         regular reloc processing.  */
14092
0
      continue;
14093
14094
0
    symval = (h->root.u.def.value
14095
0
        + h->root.u.def.section->output_section->vma
14096
0
        + h->root.u.def.section->output_offset);
14097
0
    target_is_micromips_code_p = (!h->needs_plt
14098
0
          && ELF_ST_IS_MICROMIPS (h->other));
14099
0
  }
14100
14101
14102
      /* For simplicity of coding, we are going to modify the
14103
   section contents, the section relocs, and the BFD symbol
14104
   table.  We must tell the rest of the code not to free up this
14105
   information.  It would be possible to instead create a table
14106
   of changes which have to be made, as is done in coff-mips.c;
14107
   that would be more work, but would require less memory when
14108
   the linker is run.  */
14109
14110
      /* Only 32-bit instructions relaxed.  */
14111
0
      if (irel->r_offset + 4 > sec->size)
14112
0
  continue;
14113
14114
0
      opcode = bfd_get_micromips_32 (abfd, ptr);
14115
14116
      /* This is the pc-relative distance from the instruction the
14117
   relocation is applied to, to the symbol referred.  */
14118
0
      pcrval = (symval
14119
0
    - (sec->output_section->vma + sec->output_offset)
14120
0
    - irel->r_offset);
14121
14122
      /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14123
   of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14124
   R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
14125
14126
     (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14127
14128
   where pcrval has first to be adjusted to apply against the LO16
14129
   location (we make the adjustment later on, when we have figured
14130
   out the offset).  */
14131
0
      if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14132
0
  {
14133
0
    bool bzc = false;
14134
0
    unsigned long nextopc;
14135
0
    unsigned long reg;
14136
0
    bfd_vma offset;
14137
14138
    /* Give up if the previous reloc was a HI16 against this symbol
14139
       too.  */
14140
0
    if (irel > internal_relocs
14141
0
        && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14142
0
        && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14143
0
      continue;
14144
14145
    /* Or if the next reloc is not a LO16 against this symbol.  */
14146
0
    if (irel + 1 >= irelend
14147
0
        || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14148
0
        || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14149
0
      continue;
14150
14151
    /* Or if the second next reloc is a LO16 against this symbol too.  */
14152
0
    if (irel + 2 >= irelend
14153
0
        && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14154
0
        && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14155
0
      continue;
14156
14157
    /* See if the LUI instruction *might* be in a branch delay slot.
14158
       We check whether what looks like a 16-bit branch or jump is
14159
       actually an immediate argument to a compact branch, and let
14160
       it through if so.  */
14161
0
    if (irel->r_offset >= 2
14162
0
        && check_br16_dslot (abfd, ptr - 2)
14163
0
        && !(irel->r_offset >= 4
14164
0
       && (bzc = check_relocated_bzc (abfd,
14165
0
              ptr - 4, irel->r_offset - 4,
14166
0
              internal_relocs, irelend))))
14167
0
      continue;
14168
0
    if (irel->r_offset >= 4
14169
0
        && !bzc
14170
0
        && check_br32_dslot (abfd, ptr - 4))
14171
0
      continue;
14172
14173
0
    reg = OP32_SREG (opcode);
14174
14175
    /* We only relax adjacent instructions or ones separated with
14176
       a branch or jump that has a delay slot.  The branch or jump
14177
       must not fiddle with the register used to hold the address.
14178
       Subtract 4 for the LUI itself.  */
14179
0
    offset = irel[1].r_offset - irel[0].r_offset;
14180
0
    switch (offset - 4)
14181
0
      {
14182
0
      case 0:
14183
0
        break;
14184
0
      case 2:
14185
0
        if (check_br16 (abfd, ptr + 4, reg))
14186
0
    break;
14187
0
        continue;
14188
0
      case 4:
14189
0
        if (check_br32 (abfd, ptr + 4, reg))
14190
0
    break;
14191
0
        continue;
14192
0
      default:
14193
0
        continue;
14194
0
      }
14195
14196
0
    nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14197
14198
    /* Give up unless the same register is used with both
14199
       relocations.  */
14200
0
    if (OP32_SREG (nextopc) != reg)
14201
0
      continue;
14202
14203
    /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14204
       and rounding up to take masking of the two LSBs into account.  */
14205
0
    pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14206
14207
    /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
14208
0
    if (IS_BITSIZE (symval, 16))
14209
0
      {
14210
        /* Fix the relocation's type.  */
14211
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14212
14213
        /* Instructions using R_MICROMIPS_LO16 have the base or
14214
     source register in bits 20:16.  This register becomes $0
14215
     (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
14216
0
        nextopc &= ~0x001f0000;
14217
0
        bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14218
0
        contents + irel[1].r_offset);
14219
0
      }
14220
14221
    /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14222
       We add 4 to take LUI deletion into account while checking
14223
       the PC-relative distance.  */
14224
0
    else if (symval % 4 == 0
14225
0
       && IS_BITSIZE (pcrval + 4, 25)
14226
0
       && MATCH (nextopc, addiu_insn)
14227
0
       && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14228
0
       && OP16_VALID_REG (OP32_TREG (nextopc)))
14229
0
      {
14230
        /* Fix the relocation's type.  */
14231
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14232
14233
        /* Replace ADDIU with the ADDIUPC version.  */
14234
0
        nextopc = (addiupc_insn.match
14235
0
       | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14236
14237
0
        bfd_put_micromips_32 (abfd, nextopc,
14238
0
            contents + irel[1].r_offset);
14239
0
      }
14240
14241
    /* Can't do anything, give up, sigh...  */
14242
0
    else
14243
0
      continue;
14244
14245
    /* Fix the relocation's type.  */
14246
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14247
14248
    /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
14249
0
    delcnt = 4;
14250
0
    deloff = 0;
14251
0
  }
14252
14253
      /* Compact branch relaxation -- due to the multitude of macros
14254
   employed by the compiler/assembler, compact branches are not
14255
   always generated.  Obviously, this can/will be fixed elsewhere,
14256
   but there is no drawback in double checking it here.  */
14257
0
      else if (r_type == R_MICROMIPS_PC16_S1
14258
0
         && irel->r_offset + 5 < sec->size
14259
0
         && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14260
0
       || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14261
0
         && ((!insn32
14262
0
        && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14263
0
          nop_insn_16) ? 2 : 0))
14264
0
       || (irel->r_offset + 7 < sec->size
14265
0
           && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14266
0
                 ptr + 4),
14267
0
             nop_insn_32) ? 4 : 0))))
14268
0
  {
14269
0
    unsigned long reg;
14270
14271
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14272
14273
    /* Replace BEQZ/BNEZ with the compact version.  */
14274
0
    opcode = (bzc_insns_32[fndopc].match
14275
0
        | BZC32_REG_FIELD (reg)
14276
0
        | (opcode & 0xffff));   /* Addend value.  */
14277
14278
0
    bfd_put_micromips_32 (abfd, opcode, ptr);
14279
14280
    /* Delete the delay slot NOP: two or four bytes from
14281
       irel->offset + 4; delcnt has already been set above.  */
14282
0
    deloff = 4;
14283
0
  }
14284
14285
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
14286
   to check the distance from the next instruction, so subtract 2.  */
14287
0
      else if (!insn32
14288
0
         && r_type == R_MICROMIPS_PC16_S1
14289
0
         && IS_BITSIZE (pcrval - 2, 11)
14290
0
         && find_match (opcode, b_insns_32) >= 0)
14291
0
  {
14292
    /* Fix the relocation's type.  */
14293
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14294
14295
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14296
0
    bfd_put_16 (abfd,
14297
0
          (b_insn_16.match
14298
0
           | (opcode & 0x3ff)),   /* Addend value.  */
14299
0
          ptr);
14300
14301
    /* Delete 2 bytes from irel->r_offset + 2.  */
14302
0
    delcnt = 2;
14303
0
    deloff = 2;
14304
0
  }
14305
14306
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
14307
   to check the distance from the next instruction, so subtract 2.  */
14308
0
      else if (!insn32
14309
0
         && r_type == R_MICROMIPS_PC16_S1
14310
0
         && IS_BITSIZE (pcrval - 2, 8)
14311
0
         && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14312
0
        && OP16_VALID_REG (OP32_SREG (opcode)))
14313
0
       || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14314
0
           && OP16_VALID_REG (OP32_TREG (opcode)))))
14315
0
  {
14316
0
    unsigned long reg;
14317
14318
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14319
14320
    /* Fix the relocation's type.  */
14321
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14322
14323
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14324
0
    bfd_put_16 (abfd,
14325
0
          (bz_insns_16[fndopc].match
14326
0
           | BZ16_REG_FIELD (reg)
14327
0
           | (opcode & 0x7f)),    /* Addend value.  */
14328
0
          ptr);
14329
14330
    /* Delete 2 bytes from irel->r_offset + 2.  */
14331
0
    delcnt = 2;
14332
0
    deloff = 2;
14333
0
  }
14334
14335
      /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
14336
0
      else if (!insn32
14337
0
         && r_type == R_MICROMIPS_26_S1
14338
0
         && target_is_micromips_code_p
14339
0
         && irel->r_offset + 7 < sec->size
14340
0
         && MATCH (opcode, jal_insn_32_bd32))
14341
0
  {
14342
0
    unsigned long n32opc;
14343
0
    bool relaxed = false;
14344
14345
0
    n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14346
14347
0
    if (MATCH (n32opc, nop_insn_32))
14348
0
      {
14349
        /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
14350
0
        bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14351
14352
0
        relaxed = true;
14353
0
      }
14354
0
    else if (find_match (n32opc, move_insns_32) >= 0)
14355
0
      {
14356
        /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
14357
0
        bfd_put_16 (abfd,
14358
0
        (move_insn_16.match
14359
0
         | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14360
0
         | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14361
0
        ptr + 4);
14362
14363
0
        relaxed = true;
14364
0
      }
14365
    /* Other 32-bit instructions relaxable to 16-bit
14366
       instructions will be handled here later.  */
14367
14368
0
    if (relaxed)
14369
0
      {
14370
        /* JAL with 32-bit delay slot that is changed to a JALS
14371
     with 16-bit delay slot.  */
14372
0
        bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14373
14374
        /* Delete 2 bytes from irel->r_offset + 6.  */
14375
0
        delcnt = 2;
14376
0
        deloff = 6;
14377
0
      }
14378
0
  }
14379
14380
0
      if (delcnt != 0)
14381
0
  {
14382
    /* Note that we've changed the relocs, section contents, etc.  */
14383
0
    elf_section_data (sec)->relocs = internal_relocs;
14384
0
    elf_section_data (sec)->this_hdr.contents = contents;
14385
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14386
14387
    /* Delete bytes depending on the delcnt and deloff.  */
14388
0
    if (!mips_elf_relax_delete_bytes (abfd, sec,
14389
0
              irel->r_offset + deloff, delcnt))
14390
0
      goto error_return;
14391
14392
    /* That will change things, so we should relax again.
14393
       Note that this is not required, and it may be slow.  */
14394
0
    *again = true;
14395
0
  }
14396
0
    }
14397
14398
0
  if (isymbuf != NULL
14399
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
14400
0
    {
14401
0
      if (! link_info->keep_memory)
14402
0
  free (isymbuf);
14403
0
      else
14404
0
  {
14405
    /* Cache the symbols for elf_link_input_bfd.  */
14406
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14407
0
  }
14408
0
    }
14409
14410
0
  if (contents != NULL
14411
0
      && elf_section_data (sec)->this_hdr.contents != contents)
14412
0
    {
14413
0
      if (! link_info->keep_memory)
14414
0
  free (contents);
14415
0
      else
14416
0
  {
14417
    /* Cache the section contents for elf_link_input_bfd.  */
14418
0
    elf_section_data (sec)->this_hdr.contents = contents;
14419
0
  }
14420
0
    }
14421
14422
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14423
0
    free (internal_relocs);
14424
14425
0
  return true;
14426
14427
0
 error_return:
14428
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
14429
0
    free (isymbuf);
14430
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
14431
0
    free (contents);
14432
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14433
0
    free (internal_relocs);
14434
14435
0
  return false;
14436
0
}
14437

14438
/* Create a MIPS ELF linker hash table.  */
14439
14440
struct bfd_link_hash_table *
14441
_bfd_mips_elf_link_hash_table_create (bfd *abfd)
14442
0
{
14443
0
  struct mips_elf_link_hash_table *ret;
14444
0
  size_t amt = sizeof (struct mips_elf_link_hash_table);
14445
14446
0
  ret = bfd_zmalloc (amt);
14447
0
  if (ret == NULL)
14448
0
    return NULL;
14449
14450
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14451
0
              mips_elf_link_hash_newfunc,
14452
0
              sizeof (struct mips_elf_link_hash_entry),
14453
0
              MIPS_ELF_DATA))
14454
0
    {
14455
0
      free (ret);
14456
0
      return NULL;
14457
0
    }
14458
0
  ret->root.init_plt_refcount.plist = NULL;
14459
0
  ret->root.init_plt_offset.plist = NULL;
14460
14461
0
  return &ret->root.root;
14462
0
}
14463
14464
/* Likewise, but indicate that the target is VxWorks.  */
14465
14466
struct bfd_link_hash_table *
14467
_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14468
0
{
14469
0
  struct bfd_link_hash_table *ret;
14470
14471
0
  ret = _bfd_mips_elf_link_hash_table_create (abfd);
14472
0
  if (ret)
14473
0
    {
14474
0
      struct mips_elf_link_hash_table *htab;
14475
14476
0
      htab = (struct mips_elf_link_hash_table *) ret;
14477
0
      htab->use_plts_and_copy_relocs = true;
14478
0
    }
14479
0
  return ret;
14480
0
}
14481
14482
/* A function that the linker calls if we are allowed to use PLTs
14483
   and copy relocs.  */
14484
14485
void
14486
_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14487
0
{
14488
0
  mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14489
0
}
14490
14491
/* A function that the linker calls to select between all or only
14492
   32-bit microMIPS instructions, and between making or ignoring
14493
   branch relocation checks for invalid transitions between ISA modes.
14494
   Also record whether we have been configured for a GNU target.  */
14495
14496
void
14497
_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14498
          bool ignore_branch_isa,
14499
          bool gnu_target)
14500
0
{
14501
0
  mips_elf_hash_table (info)->insn32 = insn32;
14502
0
  mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14503
0
  mips_elf_hash_table (info)->gnu_target = gnu_target;
14504
0
}
14505
14506
/* A function that the linker calls to enable use of compact branches in
14507
   linker generated code for MIPSR6.  */
14508
14509
void
14510
_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14511
0
{
14512
0
  mips_elf_hash_table (info)->compact_branches = on;
14513
0
}
14514
14515

14516
/* Structure for saying that BFD machine EXTENSION extends BASE.  */
14517
14518
struct mips_mach_extension
14519
{
14520
  unsigned long extension, base;
14521
};
14522
14523
/* An array that maps 64-bit architectures to the corresponding 32-bit
14524
   architectures.  */
14525
static const struct mips_mach_extension mips_mach_32_64[] =
14526
{
14527
  { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14528
  { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14529
  { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14530
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14531
  { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
14532
};
14533
14534
/* An array describing how BFD machines relate to one another.  The entries
14535
   are ordered topologically with MIPS I extensions listed last.  */
14536
14537
static const struct mips_mach_extension mips_mach_extensions[] =
14538
{
14539
  /* MIPS64r2 extensions.  */
14540
  { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14541
  { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14542
  { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14543
  { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14544
  { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14545
  { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14546
  { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14547
14548
  /* MIPS64 extensions.  */
14549
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14550
  { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14551
  { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14552
14553
  /* MIPS V extensions.  */
14554
  { bfd_mach_mipsisa64, bfd_mach_mips5 },
14555
14556
  /* R10000 extensions.  */
14557
  { bfd_mach_mips12000, bfd_mach_mips10000 },
14558
  { bfd_mach_mips14000, bfd_mach_mips10000 },
14559
  { bfd_mach_mips16000, bfd_mach_mips10000 },
14560
14561
  /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
14562
     vr5400 ISA, but doesn't include the multimedia stuff.  It seems
14563
     better to allow vr5400 and vr5500 code to be merged anyway, since
14564
     many libraries will just use the core ISA.  Perhaps we could add
14565
     some sort of ASE flag if this ever proves a problem.  */
14566
  { bfd_mach_mips5500, bfd_mach_mips5400 },
14567
  { bfd_mach_mips5400, bfd_mach_mips5000 },
14568
14569
  /* MIPS IV extensions.  */
14570
  { bfd_mach_mips5, bfd_mach_mips8000 },
14571
  { bfd_mach_mips10000, bfd_mach_mips8000 },
14572
  { bfd_mach_mips5000, bfd_mach_mips8000 },
14573
  { bfd_mach_mips7000, bfd_mach_mips8000 },
14574
  { bfd_mach_mips9000, bfd_mach_mips8000 },
14575
14576
  /* VR4100 extensions.  */
14577
  { bfd_mach_mips4120, bfd_mach_mips4100 },
14578
  { bfd_mach_mips4111, bfd_mach_mips4100 },
14579
14580
  /* MIPS III extensions.  */
14581
  { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14582
  { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14583
  { bfd_mach_mips8000, bfd_mach_mips4000 },
14584
  { bfd_mach_mips4650, bfd_mach_mips4000 },
14585
  { bfd_mach_mips4600, bfd_mach_mips4000 },
14586
  { bfd_mach_mips4400, bfd_mach_mips4000 },
14587
  { bfd_mach_mips4300, bfd_mach_mips4000 },
14588
  { bfd_mach_mips4100, bfd_mach_mips4000 },
14589
  { bfd_mach_mips5900, bfd_mach_mips4000 },
14590
14591
  /* MIPS32r3 extensions.  */
14592
  { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14593
14594
  /* MIPS32r2 extensions.  */
14595
  { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14596
14597
  /* MIPS32 extensions.  */
14598
  { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14599
14600
  /* MIPS II extensions.  */
14601
  { bfd_mach_mips4000, bfd_mach_mips6000 },
14602
  { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14603
  { bfd_mach_mips4010, bfd_mach_mips6000 },
14604
  { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14605
14606
  /* MIPS I extensions.  */
14607
  { bfd_mach_mips6000, bfd_mach_mips3000 },
14608
  { bfd_mach_mips3900, bfd_mach_mips3000 }
14609
};
14610
14611
/* Return true if bfd machine EXTENSION is the same as BASE, or if
14612
   EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
14613
14614
static bool
14615
mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14616
0
{
14617
0
  size_t i;
14618
14619
0
  if (extension == base)
14620
0
    return true;
14621
14622
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14623
0
    if (extension == mips_mach_32_64[i].extension)
14624
0
      return base == mips_mach_32_64[i].base;
14625
14626
0
  return false;
14627
0
}
14628
14629
static bool
14630
mips_mach_extends_p (unsigned long base, unsigned long extension)
14631
0
{
14632
0
  size_t i;
14633
14634
0
  if (mips_mach_extends_32_64 (base, extension))
14635
0
    return true;
14636
14637
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14638
0
    if (extension == mips_mach_extensions[i].extension)
14639
0
      {
14640
0
  extension = mips_mach_extensions[i].base;
14641
0
  if (mips_mach_extends_32_64 (base, extension))
14642
0
    return true;
14643
0
      }
14644
14645
0
  return false;
14646
0
}
14647
14648
/* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
14649
14650
static unsigned long
14651
bfd_mips_isa_ext_mach (unsigned int isa_ext)
14652
0
{
14653
0
  switch (isa_ext)
14654
0
    {
14655
0
    case AFL_EXT_3900:       return bfd_mach_mips3900;
14656
0
    case AFL_EXT_4010:       return bfd_mach_mips4010;
14657
0
    case AFL_EXT_4100:       return bfd_mach_mips4100;
14658
0
    case AFL_EXT_4111:       return bfd_mach_mips4111;
14659
0
    case AFL_EXT_4120:       return bfd_mach_mips4120;
14660
0
    case AFL_EXT_4650:       return bfd_mach_mips4650;
14661
0
    case AFL_EXT_5400:       return bfd_mach_mips5400;
14662
0
    case AFL_EXT_5500:       return bfd_mach_mips5500;
14663
0
    case AFL_EXT_5900:       return bfd_mach_mips5900;
14664
0
    case AFL_EXT_10000:       return bfd_mach_mips10000;
14665
0
    case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14666
0
    case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14667
0
    case AFL_EXT_SB1:       return bfd_mach_mips_sb1;
14668
0
    case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
14669
0
    case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
14670
0
    case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
14671
0
    case AFL_EXT_XLR:       return bfd_mach_mips_xlr;
14672
0
    default:          return bfd_mach_mips3000;
14673
0
    }
14674
0
}
14675
14676
/* Return the .MIPS.abiflags value representing each ISA Extension.  */
14677
14678
unsigned int
14679
bfd_mips_isa_ext (bfd *abfd)
14680
0
{
14681
0
  switch (bfd_get_mach (abfd))
14682
0
    {
14683
0
    case bfd_mach_mips3900:     return AFL_EXT_3900;
14684
0
    case bfd_mach_mips4010:     return AFL_EXT_4010;
14685
0
    case bfd_mach_mips4100:     return AFL_EXT_4100;
14686
0
    case bfd_mach_mips4111:     return AFL_EXT_4111;
14687
0
    case bfd_mach_mips4120:     return AFL_EXT_4120;
14688
0
    case bfd_mach_mips4650:     return AFL_EXT_4650;
14689
0
    case bfd_mach_mips5400:     return AFL_EXT_5400;
14690
0
    case bfd_mach_mips5500:     return AFL_EXT_5500;
14691
0
    case bfd_mach_mips5900:     return AFL_EXT_5900;
14692
0
    case bfd_mach_mips10000:     return AFL_EXT_10000;
14693
0
    case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14694
0
    case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14695
0
    case bfd_mach_mips_sb1:     return AFL_EXT_SB1;
14696
0
    case bfd_mach_mips_octeon:     return AFL_EXT_OCTEON;
14697
0
    case bfd_mach_mips_octeonp:     return AFL_EXT_OCTEONP;
14698
0
    case bfd_mach_mips_octeon3:     return AFL_EXT_OCTEON3;
14699
0
    case bfd_mach_mips_octeon2:     return AFL_EXT_OCTEON2;
14700
0
    case bfd_mach_mips_xlr:     return AFL_EXT_XLR;
14701
0
    case bfd_mach_mips_interaptiv_mr2:
14702
0
      return AFL_EXT_INTERAPTIV_MR2;
14703
0
    default:          return 0;
14704
0
    }
14705
0
}
14706
14707
/* Encode ISA level and revision as a single value.  */
14708
0
#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14709
14710
/* Decode a single value into level and revision.  */
14711
0
#define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
14712
0
#define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
14713
14714
/* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
14715
14716
static void
14717
update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14718
0
{
14719
0
  int new_isa = 0;
14720
0
  switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14721
0
    {
14722
0
    case E_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
14723
0
    case E_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
14724
0
    case E_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
14725
0
    case E_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
14726
0
    case E_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
14727
0
    case E_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
14728
0
    case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14729
0
    case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14730
0
    case E_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
14731
0
    case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14732
0
    case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14733
0
    default:
14734
0
      _bfd_error_handler
14735
  /* xgettext:c-format */
14736
0
  (_("%pB: unknown architecture %s"),
14737
0
   abfd, bfd_printable_name (abfd));
14738
0
    }
14739
14740
0
  if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14741
0
    {
14742
0
      abiflags->isa_level = ISA_LEVEL (new_isa);
14743
0
      abiflags->isa_rev = ISA_REV (new_isa);
14744
0
    }
14745
14746
  /* Update the isa_ext if ABFD describes a further extension.  */
14747
0
  if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14748
0
         bfd_get_mach (abfd)))
14749
0
    abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14750
0
}
14751
14752
/* Return true if the given ELF header flags describe a 32-bit binary.  */
14753
14754
static bool
14755
mips_32bit_flags_p (flagword flags)
14756
0
{
14757
0
  return ((flags & EF_MIPS_32BITMODE) != 0
14758
0
    || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14759
0
    || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14760
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14761
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14762
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14763
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14764
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14765
0
}
14766
14767
/* Infer the content of the ABI flags based on the elf header.  */
14768
14769
static void
14770
infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14771
0
{
14772
0
  obj_attribute *in_attr;
14773
14774
0
  memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14775
0
  update_mips_abiflags_isa (abfd, abiflags);
14776
14777
0
  if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14778
0
    abiflags->gpr_size = AFL_REG_32;
14779
0
  else
14780
0
    abiflags->gpr_size = AFL_REG_64;
14781
14782
0
  abiflags->cpr1_size = AFL_REG_NONE;
14783
14784
0
  in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14785
0
  abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14786
14787
0
  if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14788
0
      || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14789
0
      || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14790
0
    && abiflags->gpr_size == AFL_REG_32))
14791
0
    abiflags->cpr1_size = AFL_REG_32;
14792
0
  else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14793
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14794
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14795
0
    abiflags->cpr1_size = AFL_REG_64;
14796
14797
0
  abiflags->cpr2_size = AFL_REG_NONE;
14798
14799
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14800
0
    abiflags->ases |= AFL_ASE_MDMX;
14801
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14802
0
    abiflags->ases |= AFL_ASE_MIPS16;
14803
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14804
0
    abiflags->ases |= AFL_ASE_MICROMIPS;
14805
14806
0
  if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14807
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14808
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14809
0
      && abiflags->isa_level >= 32
14810
0
      && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14811
0
    abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14812
0
}
14813
14814
/* We need to use a special link routine to handle the .reginfo and
14815
   the .mdebug sections.  We need to merge all instances of these
14816
   sections together, not write them all out sequentially.  */
14817
14818
bool
14819
_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14820
0
{
14821
0
  asection *o;
14822
0
  struct bfd_link_order *p;
14823
0
  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14824
0
  asection *rtproc_sec, *abiflags_sec;
14825
0
  Elf32_RegInfo reginfo;
14826
0
  struct ecoff_debug_info debug;
14827
0
  struct mips_htab_traverse_info hti;
14828
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14829
0
  const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14830
0
  HDRR *symhdr = &debug.symbolic_header;
14831
0
  void *mdebug_handle = NULL;
14832
0
  asection *s;
14833
0
  EXTR esym;
14834
0
  unsigned int i;
14835
0
  bfd_size_type amt;
14836
0
  struct mips_elf_link_hash_table *htab;
14837
14838
0
  static const char * const secname[] =
14839
0
  {
14840
0
    ".text", ".init", ".fini", ".data",
14841
0
    ".rodata", ".sdata", ".sbss", ".bss"
14842
0
  };
14843
0
  static const int sc[] =
14844
0
  {
14845
0
    scText, scInit, scFini, scData,
14846
0
    scRData, scSData, scSBss, scBss
14847
0
  };
14848
14849
0
  htab = mips_elf_hash_table (info);
14850
0
  BFD_ASSERT (htab != NULL);
14851
14852
  /* Sort the dynamic symbols so that those with GOT entries come after
14853
     those without.  */
14854
0
  if (!mips_elf_sort_hash_table (abfd, info))
14855
0
    return false;
14856
14857
  /* Create any scheduled LA25 stubs.  */
14858
0
  hti.info = info;
14859
0
  hti.output_bfd = abfd;
14860
0
  hti.error = false;
14861
0
  htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14862
0
  if (hti.error)
14863
0
    return false;
14864
14865
  /* Get a value for the GP register.  */
14866
0
  if (elf_gp (abfd) == 0)
14867
0
    {
14868
0
      struct bfd_link_hash_entry *h;
14869
14870
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14871
0
      if (h != NULL && h->type == bfd_link_hash_defined)
14872
0
  elf_gp (abfd) = (h->u.def.value
14873
0
       + h->u.def.section->output_section->vma
14874
0
       + h->u.def.section->output_offset);
14875
0
      else if (htab->root.target_os == is_vxworks
14876
0
         && (h = bfd_link_hash_lookup (info->hash,
14877
0
               "_GLOBAL_OFFSET_TABLE_",
14878
0
               false, false, true))
14879
0
         && h->type == bfd_link_hash_defined)
14880
0
  elf_gp (abfd) = (h->u.def.section->output_section->vma
14881
0
       + h->u.def.section->output_offset
14882
0
       + h->u.def.value);
14883
0
      else if (bfd_link_relocatable (info))
14884
0
  {
14885
0
    bfd_vma lo = MINUS_ONE;
14886
14887
    /* Find the GP-relative section with the lowest offset.  */
14888
0
    for (o = abfd->sections; o != NULL; o = o->next)
14889
0
      if (o->vma < lo
14890
0
    && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14891
0
        lo = o->vma;
14892
14893
    /* And calculate GP relative to that.  */
14894
0
    elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14895
0
  }
14896
0
      else
14897
0
  {
14898
    /* If the relocate_section function needs to do a reloc
14899
       involving the GP value, it should make a reloc_dangerous
14900
       callback to warn that GP is not defined.  */
14901
0
  }
14902
0
    }
14903
14904
  /* Go through the sections and collect the .reginfo and .mdebug
14905
     information.  */
14906
0
  abiflags_sec = NULL;
14907
0
  reginfo_sec = NULL;
14908
0
  mdebug_sec = NULL;
14909
0
  gptab_data_sec = NULL;
14910
0
  gptab_bss_sec = NULL;
14911
0
  for (o = abfd->sections; o != NULL; o = o->next)
14912
0
    {
14913
0
      if (strcmp (o->name, ".MIPS.abiflags") == 0)
14914
0
  {
14915
    /* We have found the .MIPS.abiflags section in the output file.
14916
       Look through all the link_orders comprising it and remove them.
14917
       The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
14918
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14919
0
      {
14920
0
        asection *input_section;
14921
14922
0
        if (p->type != bfd_indirect_link_order)
14923
0
    {
14924
0
      if (p->type == bfd_data_link_order)
14925
0
        continue;
14926
0
      abort ();
14927
0
    }
14928
14929
0
        input_section = p->u.indirect.section;
14930
14931
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14932
     elf_link_input_bfd ignores this section.  */
14933
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
14934
0
      }
14935
14936
    /* Size has been set in _bfd_mips_elf_always_size_sections.  */
14937
0
    BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14938
14939
    /* Skip this section later on (I don't think this currently
14940
       matters, but someday it might).  */
14941
0
    o->map_head.link_order = NULL;
14942
14943
0
    abiflags_sec = o;
14944
0
  }
14945
14946
0
      if (strcmp (o->name, ".reginfo") == 0)
14947
0
  {
14948
0
    memset (&reginfo, 0, sizeof reginfo);
14949
14950
    /* We have found the .reginfo section in the output file.
14951
       Look through all the link_orders comprising it and merge
14952
       the information together.  */
14953
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14954
0
      {
14955
0
        asection *input_section;
14956
0
        bfd *input_bfd;
14957
0
        Elf32_External_RegInfo ext;
14958
0
        Elf32_RegInfo sub;
14959
0
        bfd_size_type sz;
14960
14961
0
        if (p->type != bfd_indirect_link_order)
14962
0
    {
14963
0
      if (p->type == bfd_data_link_order)
14964
0
        continue;
14965
0
      abort ();
14966
0
    }
14967
14968
0
        input_section = p->u.indirect.section;
14969
0
        input_bfd = input_section->owner;
14970
14971
0
        sz = (input_section->size < sizeof (ext)
14972
0
        ? input_section->size : sizeof (ext));
14973
0
        memset (&ext, 0, sizeof (ext));
14974
0
        if (! bfd_get_section_contents (input_bfd, input_section,
14975
0
                &ext, 0, sz))
14976
0
    return false;
14977
14978
0
        bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14979
14980
0
        reginfo.ri_gprmask |= sub.ri_gprmask;
14981
0
        reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14982
0
        reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14983
0
        reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14984
0
        reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14985
14986
        /* ri_gp_value is set by the function
14987
     `_bfd_mips_elf_section_processing' when the section is
14988
     finally written out.  */
14989
14990
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14991
     elf_link_input_bfd ignores this section.  */
14992
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
14993
0
      }
14994
14995
    /* Size has been set in _bfd_mips_elf_always_size_sections.  */
14996
0
    BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14997
14998
    /* Skip this section later on (I don't think this currently
14999
       matters, but someday it might).  */
15000
0
    o->map_head.link_order = NULL;
15001
15002
0
    reginfo_sec = o;
15003
0
  }
15004
15005
0
      if (strcmp (o->name, ".mdebug") == 0)
15006
0
  {
15007
0
    struct extsym_info einfo;
15008
0
    bfd_vma last;
15009
15010
    /* We have found the .mdebug section in the output file.
15011
       Look through all the link_orders comprising it and merge
15012
       the information together.  */
15013
0
    symhdr->magic = swap->sym_magic;
15014
    /* FIXME: What should the version stamp be?  */
15015
0
    symhdr->vstamp = 0;
15016
0
    symhdr->ilineMax = 0;
15017
0
    symhdr->cbLine = 0;
15018
0
    symhdr->idnMax = 0;
15019
0
    symhdr->ipdMax = 0;
15020
0
    symhdr->isymMax = 0;
15021
0
    symhdr->ioptMax = 0;
15022
0
    symhdr->iauxMax = 0;
15023
0
    symhdr->issMax = 0;
15024
0
    symhdr->issExtMax = 0;
15025
0
    symhdr->ifdMax = 0;
15026
0
    symhdr->crfd = 0;
15027
0
    symhdr->iextMax = 0;
15028
15029
    /* We accumulate the debugging information itself in the
15030
       debug_info structure.  */
15031
0
    debug.alloc_syments = false;
15032
0
    debug.line = NULL;
15033
0
    debug.external_dnr = NULL;
15034
0
    debug.external_pdr = NULL;
15035
0
    debug.external_sym = NULL;
15036
0
    debug.external_opt = NULL;
15037
0
    debug.external_aux = NULL;
15038
0
    debug.ss = NULL;
15039
0
    debug.ssext = debug.ssext_end = NULL;
15040
0
    debug.external_fdr = NULL;
15041
0
    debug.external_rfd = NULL;
15042
0
    debug.external_ext = debug.external_ext_end = NULL;
15043
15044
0
    mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15045
0
    if (mdebug_handle == NULL)
15046
0
      return false;
15047
15048
0
    esym.jmptbl = 0;
15049
0
    esym.cobol_main = 0;
15050
0
    esym.weakext = 0;
15051
0
    esym.reserved = 0;
15052
0
    esym.ifd = ifdNil;
15053
0
    esym.asym.iss = issNil;
15054
0
    esym.asym.st = stLocal;
15055
0
    esym.asym.reserved = 0;
15056
0
    esym.asym.index = indexNil;
15057
0
    last = 0;
15058
0
    for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15059
0
      {
15060
0
        esym.asym.sc = sc[i];
15061
0
        s = bfd_get_section_by_name (abfd, secname[i]);
15062
0
        if (s != NULL)
15063
0
    {
15064
0
      esym.asym.value = s->vma;
15065
0
      last = s->vma + s->size;
15066
0
    }
15067
0
        else
15068
0
    esym.asym.value = last;
15069
0
        if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15070
0
             secname[i], &esym))
15071
0
    return false;
15072
0
      }
15073
15074
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15075
0
      {
15076
0
        asection *input_section;
15077
0
        bfd *input_bfd;
15078
0
        const struct ecoff_debug_swap *input_swap;
15079
0
        struct ecoff_debug_info input_debug;
15080
0
        char *eraw_src;
15081
0
        char *eraw_end;
15082
15083
0
        if (p->type != bfd_indirect_link_order)
15084
0
    {
15085
0
      if (p->type == bfd_data_link_order)
15086
0
        continue;
15087
0
      abort ();
15088
0
    }
15089
15090
0
        input_section = p->u.indirect.section;
15091
0
        input_bfd = input_section->owner;
15092
15093
0
        if (!is_mips_elf (input_bfd))
15094
0
    {
15095
      /* I don't know what a non MIPS ELF bfd would be
15096
         doing with a .mdebug section, but I don't really
15097
         want to deal with it.  */
15098
0
      continue;
15099
0
    }
15100
15101
0
        input_swap = (get_elf_backend_data (input_bfd)
15102
0
          ->elf_backend_ecoff_debug_swap);
15103
15104
0
        BFD_ASSERT (p->size == input_section->size);
15105
15106
        /* The ECOFF linking code expects that we have already
15107
     read in the debugging information and set up an
15108
     ecoff_debug_info structure, so we do that now.  */
15109
0
        if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15110
0
               &input_debug))
15111
0
    return false;
15112
15113
0
        if (! (bfd_ecoff_debug_accumulate
15114
0
         (mdebug_handle, abfd, &debug, swap, input_bfd,
15115
0
          &input_debug, input_swap, info)))
15116
0
    {
15117
0
      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15118
0
      return false;
15119
0
    }
15120
15121
        /* Loop through the external symbols.  For each one with
15122
     interesting information, try to find the symbol in
15123
     the linker global hash table and save the information
15124
     for the output external symbols.  */
15125
0
        eraw_src = input_debug.external_ext;
15126
0
        eraw_end = (eraw_src
15127
0
        + (input_debug.symbolic_header.iextMax
15128
0
           * input_swap->external_ext_size));
15129
0
        for (;
15130
0
       eraw_src < eraw_end;
15131
0
       eraw_src += input_swap->external_ext_size)
15132
0
    {
15133
0
      EXTR ext;
15134
0
      const char *name;
15135
0
      struct mips_elf_link_hash_entry *h;
15136
15137
0
      (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15138
0
      if (ext.asym.sc == scNil
15139
0
          || ext.asym.sc == scUndefined
15140
0
          || ext.asym.sc == scSUndefined)
15141
0
        continue;
15142
15143
0
      name = input_debug.ssext + ext.asym.iss;
15144
0
      h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15145
0
             name, false, false, true);
15146
0
      if (h == NULL || h->esym.ifd != -2)
15147
0
        continue;
15148
15149
0
      if (ext.ifd != -1)
15150
0
        {
15151
0
          BFD_ASSERT (ext.ifd
15152
0
          < input_debug.symbolic_header.ifdMax);
15153
0
          ext.ifd = input_debug.ifdmap[ext.ifd];
15154
0
        }
15155
15156
0
      h->esym = ext;
15157
0
    }
15158
15159
        /* Free up the information we just read.  */
15160
0
        _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15161
15162
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15163
     elf_link_input_bfd ignores this section.  */
15164
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15165
0
      }
15166
15167
0
    if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15168
0
      {
15169
        /* Create .rtproc section.  */
15170
0
        rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15171
0
        if (rtproc_sec == NULL)
15172
0
    {
15173
0
      flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15174
0
            | SEC_LINKER_CREATED | SEC_READONLY);
15175
15176
0
      rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15177
0
                   ".rtproc",
15178
0
                   flags);
15179
0
      if (rtproc_sec == NULL
15180
0
          || !bfd_set_section_alignment (rtproc_sec, 4))
15181
0
        return false;
15182
0
    }
15183
15184
0
        if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15185
0
                 info, rtproc_sec,
15186
0
                 &debug))
15187
0
    return false;
15188
0
      }
15189
15190
    /* Build the external symbol information.  */
15191
0
    einfo.abfd = abfd;
15192
0
    einfo.info = info;
15193
0
    einfo.debug = &debug;
15194
0
    einfo.swap = swap;
15195
0
    einfo.failed = false;
15196
0
    mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15197
0
               mips_elf_output_extsym, &einfo);
15198
0
    if (einfo.failed)
15199
0
      return false;
15200
15201
    /* Set the size of the .mdebug section.  */
15202
0
    o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15203
15204
    /* Skip this section later on (I don't think this currently
15205
       matters, but someday it might).  */
15206
0
    o->map_head.link_order = NULL;
15207
15208
0
    mdebug_sec = o;
15209
0
  }
15210
15211
0
      if (startswith (o->name, ".gptab."))
15212
0
  {
15213
0
    const char *subname;
15214
0
    unsigned int c;
15215
0
    Elf32_gptab *tab;
15216
0
    Elf32_External_gptab *ext_tab;
15217
0
    unsigned int j;
15218
15219
    /* The .gptab.sdata and .gptab.sbss sections hold
15220
       information describing how the small data area would
15221
       change depending upon the -G switch.  These sections
15222
       not used in executables files.  */
15223
0
    if (! bfd_link_relocatable (info))
15224
0
      {
15225
0
        for (p = o->map_head.link_order; p != NULL; p = p->next)
15226
0
    {
15227
0
      asection *input_section;
15228
15229
0
      if (p->type != bfd_indirect_link_order)
15230
0
        {
15231
0
          if (p->type == bfd_data_link_order)
15232
0
      continue;
15233
0
          abort ();
15234
0
        }
15235
15236
0
      input_section = p->u.indirect.section;
15237
15238
      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15239
         elf_link_input_bfd ignores this section.  */
15240
0
      input_section->flags &= ~SEC_HAS_CONTENTS;
15241
0
    }
15242
15243
        /* Skip this section later on (I don't think this
15244
     currently matters, but someday it might).  */
15245
0
        o->map_head.link_order = NULL;
15246
15247
        /* Really remove the section.  */
15248
0
        bfd_section_list_remove (abfd, o);
15249
0
        --abfd->section_count;
15250
15251
0
        continue;
15252
0
      }
15253
15254
    /* There is one gptab for initialized data, and one for
15255
       uninitialized data.  */
15256
0
    if (strcmp (o->name, ".gptab.sdata") == 0)
15257
0
      gptab_data_sec = o;
15258
0
    else if (strcmp (o->name, ".gptab.sbss") == 0)
15259
0
      gptab_bss_sec = o;
15260
0
    else
15261
0
      {
15262
0
        _bfd_error_handler
15263
    /* xgettext:c-format */
15264
0
    (_("%pB: illegal section name `%pA'"), abfd, o);
15265
0
        bfd_set_error (bfd_error_nonrepresentable_section);
15266
0
        return false;
15267
0
      }
15268
15269
    /* The linker script always combines .gptab.data and
15270
       .gptab.sdata into .gptab.sdata, and likewise for
15271
       .gptab.bss and .gptab.sbss.  It is possible that there is
15272
       no .sdata or .sbss section in the output file, in which
15273
       case we must change the name of the output section.  */
15274
0
    subname = o->name + sizeof ".gptab" - 1;
15275
0
    if (bfd_get_section_by_name (abfd, subname) == NULL)
15276
0
      {
15277
0
        if (o == gptab_data_sec)
15278
0
    o->name = ".gptab.data";
15279
0
        else
15280
0
    o->name = ".gptab.bss";
15281
0
        subname = o->name + sizeof ".gptab" - 1;
15282
0
        BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15283
0
      }
15284
15285
    /* Set up the first entry.  */
15286
0
    c = 1;
15287
0
    amt = c * sizeof (Elf32_gptab);
15288
0
    tab = bfd_malloc (amt);
15289
0
    if (tab == NULL)
15290
0
      return false;
15291
0
    tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15292
0
    tab[0].gt_header.gt_unused = 0;
15293
15294
    /* Combine the input sections.  */
15295
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15296
0
      {
15297
0
        asection *input_section;
15298
0
        bfd *input_bfd;
15299
0
        bfd_size_type size;
15300
0
        unsigned long last;
15301
0
        bfd_size_type gpentry;
15302
15303
0
        if (p->type != bfd_indirect_link_order)
15304
0
    {
15305
0
      if (p->type == bfd_data_link_order)
15306
0
        continue;
15307
0
      abort ();
15308
0
    }
15309
15310
0
        input_section = p->u.indirect.section;
15311
0
        input_bfd = input_section->owner;
15312
15313
        /* Combine the gptab entries for this input section one
15314
     by one.  We know that the input gptab entries are
15315
     sorted by ascending -G value.  */
15316
0
        size = input_section->size;
15317
0
        last = 0;
15318
0
        for (gpentry = sizeof (Elf32_External_gptab);
15319
0
       gpentry < size;
15320
0
       gpentry += sizeof (Elf32_External_gptab))
15321
0
    {
15322
0
      Elf32_External_gptab ext_gptab;
15323
0
      Elf32_gptab int_gptab;
15324
0
      unsigned long val;
15325
0
      unsigned long add;
15326
0
      bool exact;
15327
0
      unsigned int look;
15328
15329
0
      if (! (bfd_get_section_contents
15330
0
       (input_bfd, input_section, &ext_gptab, gpentry,
15331
0
        sizeof (Elf32_External_gptab))))
15332
0
        {
15333
0
          free (tab);
15334
0
          return false;
15335
0
        }
15336
15337
0
      bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15338
0
            &int_gptab);
15339
0
      val = int_gptab.gt_entry.gt_g_value;
15340
0
      add = int_gptab.gt_entry.gt_bytes - last;
15341
15342
0
      exact = false;
15343
0
      for (look = 1; look < c; look++)
15344
0
        {
15345
0
          if (tab[look].gt_entry.gt_g_value >= val)
15346
0
      tab[look].gt_entry.gt_bytes += add;
15347
15348
0
          if (tab[look].gt_entry.gt_g_value == val)
15349
0
      exact = true;
15350
0
        }
15351
15352
0
      if (! exact)
15353
0
        {
15354
0
          Elf32_gptab *new_tab;
15355
0
          unsigned int max;
15356
15357
          /* We need a new table entry.  */
15358
0
          amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15359
0
          new_tab = bfd_realloc (tab, amt);
15360
0
          if (new_tab == NULL)
15361
0
      {
15362
0
        free (tab);
15363
0
        return false;
15364
0
      }
15365
0
          tab = new_tab;
15366
0
          tab[c].gt_entry.gt_g_value = val;
15367
0
          tab[c].gt_entry.gt_bytes = add;
15368
15369
          /* Merge in the size for the next smallest -G
15370
       value, since that will be implied by this new
15371
       value.  */
15372
0
          max = 0;
15373
0
          for (look = 1; look < c; look++)
15374
0
      {
15375
0
        if (tab[look].gt_entry.gt_g_value < val
15376
0
            && (max == 0
15377
0
          || (tab[look].gt_entry.gt_g_value
15378
0
              > tab[max].gt_entry.gt_g_value)))
15379
0
          max = look;
15380
0
      }
15381
0
          if (max != 0)
15382
0
      tab[c].gt_entry.gt_bytes +=
15383
0
        tab[max].gt_entry.gt_bytes;
15384
15385
0
          ++c;
15386
0
        }
15387
15388
0
      last = int_gptab.gt_entry.gt_bytes;
15389
0
    }
15390
15391
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15392
     elf_link_input_bfd ignores this section.  */
15393
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15394
0
      }
15395
15396
    /* The table must be sorted by -G value.  */
15397
0
    if (c > 2)
15398
0
      qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15399
15400
    /* Swap out the table.  */
15401
0
    amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15402
0
    ext_tab = bfd_alloc (abfd, amt);
15403
0
    if (ext_tab == NULL)
15404
0
      {
15405
0
        free (tab);
15406
0
        return false;
15407
0
      }
15408
15409
0
    for (j = 0; j < c; j++)
15410
0
      bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15411
0
    free (tab);
15412
15413
0
    o->size = c * sizeof (Elf32_External_gptab);
15414
0
    o->contents = (bfd_byte *) ext_tab;
15415
15416
    /* Skip this section later on (I don't think this currently
15417
       matters, but someday it might).  */
15418
0
    o->map_head.link_order = NULL;
15419
0
  }
15420
0
    }
15421
15422
  /* Invoke the regular ELF backend linker to do all the work.  */
15423
0
  if (!bfd_elf_final_link (abfd, info))
15424
0
    return false;
15425
15426
  /* Now write out the computed sections.  */
15427
15428
0
  if (abiflags_sec != NULL)
15429
0
    {
15430
0
      Elf_External_ABIFlags_v0 ext;
15431
0
      Elf_Internal_ABIFlags_v0 *abiflags;
15432
15433
0
      abiflags = &mips_elf_tdata (abfd)->abiflags;
15434
15435
      /* Set up the abiflags if no valid input sections were found.  */
15436
0
      if (!mips_elf_tdata (abfd)->abiflags_valid)
15437
0
  {
15438
0
    infer_mips_abiflags (abfd, abiflags);
15439
0
    mips_elf_tdata (abfd)->abiflags_valid = true;
15440
0
  }
15441
0
      bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15442
0
      if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15443
0
  return false;
15444
0
    }
15445
15446
0
  if (reginfo_sec != NULL)
15447
0
    {
15448
0
      Elf32_External_RegInfo ext;
15449
15450
0
      bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15451
0
      if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15452
0
  return false;
15453
0
    }
15454
15455
0
  if (mdebug_sec != NULL)
15456
0
    {
15457
0
      BFD_ASSERT (abfd->output_has_begun);
15458
0
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15459
0
                 swap, info,
15460
0
                 mdebug_sec->filepos))
15461
0
  return false;
15462
15463
0
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15464
0
    }
15465
15466
0
  if (gptab_data_sec != NULL)
15467
0
    {
15468
0
      if (! bfd_set_section_contents (abfd, gptab_data_sec,
15469
0
              gptab_data_sec->contents,
15470
0
              0, gptab_data_sec->size))
15471
0
  return false;
15472
0
    }
15473
15474
0
  if (gptab_bss_sec != NULL)
15475
0
    {
15476
0
      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15477
0
              gptab_bss_sec->contents,
15478
0
              0, gptab_bss_sec->size))
15479
0
  return false;
15480
0
    }
15481
15482
0
  if (SGI_COMPAT (abfd))
15483
0
    {
15484
0
      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15485
0
      if (rtproc_sec != NULL)
15486
0
  {
15487
0
    if (! bfd_set_section_contents (abfd, rtproc_sec,
15488
0
            rtproc_sec->contents,
15489
0
            0, rtproc_sec->size))
15490
0
      return false;
15491
0
  }
15492
0
    }
15493
15494
0
  return true;
15495
0
}
15496

15497
/* Merge object file header flags from IBFD into OBFD.  Raise an error
15498
   if there are conflicting settings.  */
15499
15500
static bool
15501
mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15502
0
{
15503
0
  bfd *obfd = info->output_bfd;
15504
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15505
0
  flagword old_flags;
15506
0
  flagword new_flags;
15507
0
  bool ok;
15508
15509
0
  new_flags = elf_elfheader (ibfd)->e_flags;
15510
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15511
0
  old_flags = elf_elfheader (obfd)->e_flags;
15512
15513
  /* Check flag compatibility.  */
15514
15515
0
  new_flags &= ~EF_MIPS_NOREORDER;
15516
0
  old_flags &= ~EF_MIPS_NOREORDER;
15517
15518
  /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
15519
     doesn't seem to matter.  */
15520
0
  new_flags &= ~EF_MIPS_XGOT;
15521
0
  old_flags &= ~EF_MIPS_XGOT;
15522
15523
  /* MIPSpro generates ucode info in n64 objects.  Again, we should
15524
     just be able to ignore this.  */
15525
0
  new_flags &= ~EF_MIPS_UCODE;
15526
0
  old_flags &= ~EF_MIPS_UCODE;
15527
15528
  /* DSOs should only be linked with CPIC code.  */
15529
0
  if ((ibfd->flags & DYNAMIC) != 0)
15530
0
    new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15531
15532
0
  if (new_flags == old_flags)
15533
0
    return true;
15534
15535
0
  ok = true;
15536
15537
0
  if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15538
0
      != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15539
0
    {
15540
0
      _bfd_error_handler
15541
0
  (_("%pB: warning: linking abicalls files with non-abicalls files"),
15542
0
   ibfd);
15543
0
      ok = true;
15544
0
    }
15545
15546
0
  if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15547
0
    elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15548
0
  if (! (new_flags & EF_MIPS_PIC))
15549
0
    elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15550
15551
0
  new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15552
0
  old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15553
15554
  /* Compare the ISAs.  */
15555
0
  if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15556
0
    {
15557
0
      _bfd_error_handler
15558
0
  (_("%pB: linking 32-bit code with 64-bit code"),
15559
0
   ibfd);
15560
0
      ok = false;
15561
0
    }
15562
0
  else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15563
0
    {
15564
      /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
15565
0
      if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15566
0
  {
15567
    /* Copy the architecture info from IBFD to OBFD.  Also copy
15568
       the 32-bit flag (if set) so that we continue to recognise
15569
       OBFD as a 32-bit binary.  */
15570
0
    bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15571
0
    elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15572
0
    elf_elfheader (obfd)->e_flags
15573
0
      |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15574
15575
    /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
15576
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15577
15578
    /* Copy across the ABI flags if OBFD doesn't use them
15579
       and if that was what caused us to treat IBFD as 32-bit.  */
15580
0
    if ((old_flags & EF_MIPS_ABI) == 0
15581
0
        && mips_32bit_flags_p (new_flags)
15582
0
        && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15583
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15584
0
  }
15585
0
      else
15586
0
  {
15587
    /* The ISAs aren't compatible.  */
15588
0
    _bfd_error_handler
15589
      /* xgettext:c-format */
15590
0
      (_("%pB: linking %s module with previous %s modules"),
15591
0
       ibfd,
15592
0
       bfd_printable_name (ibfd),
15593
0
       bfd_printable_name (obfd));
15594
0
    ok = false;
15595
0
  }
15596
0
    }
15597
15598
0
  new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15599
0
  old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15600
15601
  /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
15602
     does set EI_CLASS differently from any 32-bit ABI.  */
15603
0
  if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15604
0
      || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15605
0
    != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15606
0
    {
15607
      /* Only error if both are set (to different values).  */
15608
0
      if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15609
0
    || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15610
0
        != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15611
0
  {
15612
0
    _bfd_error_handler
15613
      /* xgettext:c-format */
15614
0
      (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15615
0
       ibfd,
15616
0
       elf_mips_abi_name (ibfd),
15617
0
       elf_mips_abi_name (obfd));
15618
0
    ok = false;
15619
0
  }
15620
0
      new_flags &= ~EF_MIPS_ABI;
15621
0
      old_flags &= ~EF_MIPS_ABI;
15622
0
    }
15623
15624
  /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
15625
     and allow arbitrary mixing of the remaining ASEs (retain the union).  */
15626
0
  if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15627
0
    {
15628
0
      int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15629
0
      int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15630
0
      int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15631
0
      int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15632
0
      int micro_mis = old_m16 && new_micro;
15633
0
      int m16_mis = old_micro && new_m16;
15634
15635
0
      if (m16_mis || micro_mis)
15636
0
  {
15637
0
    _bfd_error_handler
15638
      /* xgettext:c-format */
15639
0
      (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15640
0
       ibfd,
15641
0
       m16_mis ? "MIPS16" : "microMIPS",
15642
0
       m16_mis ? "microMIPS" : "MIPS16");
15643
0
    ok = false;
15644
0
  }
15645
15646
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15647
15648
0
      new_flags &= ~ EF_MIPS_ARCH_ASE;
15649
0
      old_flags &= ~ EF_MIPS_ARCH_ASE;
15650
0
    }
15651
15652
  /* Compare NaN encodings.  */
15653
0
  if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15654
0
    {
15655
      /* xgettext:c-format */
15656
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15657
0
        ibfd,
15658
0
        (new_flags & EF_MIPS_NAN2008
15659
0
         ? "-mnan=2008" : "-mnan=legacy"),
15660
0
        (old_flags & EF_MIPS_NAN2008
15661
0
         ? "-mnan=2008" : "-mnan=legacy"));
15662
0
      ok = false;
15663
0
      new_flags &= ~EF_MIPS_NAN2008;
15664
0
      old_flags &= ~EF_MIPS_NAN2008;
15665
0
    }
15666
15667
  /* Compare FP64 state.  */
15668
0
  if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15669
0
    {
15670
      /* xgettext:c-format */
15671
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15672
0
        ibfd,
15673
0
        (new_flags & EF_MIPS_FP64
15674
0
         ? "-mfp64" : "-mfp32"),
15675
0
        (old_flags & EF_MIPS_FP64
15676
0
         ? "-mfp64" : "-mfp32"));
15677
0
      ok = false;
15678
0
      new_flags &= ~EF_MIPS_FP64;
15679
0
      old_flags &= ~EF_MIPS_FP64;
15680
0
    }
15681
15682
  /* Warn about any other mismatches */
15683
0
  if (new_flags != old_flags)
15684
0
    {
15685
      /* xgettext:c-format */
15686
0
      _bfd_error_handler
15687
0
  (_("%pB: uses different e_flags (%#x) fields than previous modules "
15688
0
     "(%#x)"),
15689
0
   ibfd, new_flags, old_flags);
15690
0
      ok = false;
15691
0
    }
15692
15693
0
  return ok;
15694
0
}
15695
15696
/* Merge object attributes from IBFD into OBFD.  Raise an error if
15697
   there are conflicting attributes.  */
15698
static bool
15699
mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15700
0
{
15701
0
  bfd *obfd = info->output_bfd;
15702
0
  obj_attribute *in_attr;
15703
0
  obj_attribute *out_attr;
15704
0
  bfd *abi_fp_bfd;
15705
0
  bfd *abi_msa_bfd;
15706
15707
0
  abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15708
0
  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15709
0
  if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15710
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15711
15712
0
  abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15713
0
  if (!abi_msa_bfd
15714
0
      && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15715
0
    mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15716
15717
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
15718
0
    {
15719
      /* This is the first object.  Copy the attributes.  */
15720
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
15721
15722
      /* Use the Tag_null value to indicate the attributes have been
15723
   initialized.  */
15724
0
      elf_known_obj_attributes_proc (obfd)[0].i = 1;
15725
15726
0
      return true;
15727
0
    }
15728
15729
  /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15730
     non-conflicting ones.  */
15731
0
  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15732
0
  if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15733
0
    {
15734
0
      int out_fp, in_fp;
15735
15736
0
      out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15737
0
      in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15738
0
      out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15739
0
      if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15740
0
  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15741
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15742
0
         && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15743
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64
15744
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15745
0
  {
15746
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15747
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15748
0
  }
15749
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15750
0
         && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15751
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64
15752
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15753
0
  /* Keep the current setting.  */;
15754
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15755
0
         && in_fp == Val_GNU_MIPS_ABI_FP_64)
15756
0
  {
15757
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15758
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15759
0
  }
15760
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15761
0
         && out_fp == Val_GNU_MIPS_ABI_FP_64)
15762
0
  /* Keep the current setting.  */;
15763
0
      else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15764
0
  {
15765
0
    const char *out_string, *in_string;
15766
15767
0
    out_string = _bfd_mips_fp_abi_string (out_fp);
15768
0
    in_string = _bfd_mips_fp_abi_string (in_fp);
15769
    /* First warn about cases involving unrecognised ABIs.  */
15770
0
    if (!out_string && !in_string)
15771
      /* xgettext:c-format */
15772
0
      _bfd_error_handler
15773
0
        (_("warning: %pB uses unknown floating point ABI %d "
15774
0
     "(set by %pB), %pB uses unknown floating point ABI %d"),
15775
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15776
0
    else if (!out_string)
15777
0
      _bfd_error_handler
15778
        /* xgettext:c-format */
15779
0
        (_("warning: %pB uses unknown floating point ABI %d "
15780
0
     "(set by %pB), %pB uses %s"),
15781
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15782
0
    else if (!in_string)
15783
0
      _bfd_error_handler
15784
        /* xgettext:c-format */
15785
0
        (_("warning: %pB uses %s (set by %pB), "
15786
0
     "%pB uses unknown floating point ABI %d"),
15787
0
         obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15788
0
    else
15789
0
      {
15790
        /* If one of the bfds is soft-float, the other must be
15791
     hard-float.  The exact choice of hard-float ABI isn't
15792
     really relevant to the error message.  */
15793
0
        if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15794
0
    out_string = "-mhard-float";
15795
0
        else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15796
0
    in_string = "-mhard-float";
15797
0
        _bfd_error_handler
15798
    /* xgettext:c-format */
15799
0
    (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15800
0
     obfd, out_string, abi_fp_bfd, ibfd, in_string);
15801
0
      }
15802
0
  }
15803
0
    }
15804
15805
  /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15806
     non-conflicting ones.  */
15807
0
  if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15808
0
    {
15809
0
      out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15810
0
      if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15811
0
  out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15812
0
      else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15813
0
  switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15814
0
    {
15815
0
    case Val_GNU_MIPS_ABI_MSA_128:
15816
0
      _bfd_error_handler
15817
        /* xgettext:c-format */
15818
0
        (_("warning: %pB uses %s (set by %pB), "
15819
0
     "%pB uses unknown MSA ABI %d"),
15820
0
         obfd, "-mmsa", abi_msa_bfd,
15821
0
         ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15822
0
      break;
15823
15824
0
    default:
15825
0
      switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15826
0
        {
15827
0
        case Val_GNU_MIPS_ABI_MSA_128:
15828
0
    _bfd_error_handler
15829
      /* xgettext:c-format */
15830
0
      (_("warning: %pB uses unknown MSA ABI %d "
15831
0
         "(set by %pB), %pB uses %s"),
15832
0
         obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15833
0
       abi_msa_bfd, ibfd, "-mmsa");
15834
0
      break;
15835
15836
0
        default:
15837
0
    _bfd_error_handler
15838
      /* xgettext:c-format */
15839
0
      (_("warning: %pB uses unknown MSA ABI %d "
15840
0
         "(set by %pB), %pB uses unknown MSA ABI %d"),
15841
0
       obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15842
0
       abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15843
0
    break;
15844
0
        }
15845
0
    }
15846
0
    }
15847
15848
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
15849
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
15850
0
}
15851
15852
/* Merge object ABI flags from IBFD into OBFD.  Raise an error if
15853
   there are conflicting settings.  */
15854
15855
static bool
15856
mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15857
0
{
15858
0
  obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15859
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15860
0
  struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15861
15862
  /* Update the output abiflags fp_abi using the computed fp_abi.  */
15863
0
  out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15864
15865
0
#define max(a, b) ((a) > (b) ? (a) : (b))
15866
  /* Merge abiflags.  */
15867
0
  out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15868
0
               in_tdata->abiflags.isa_level);
15869
0
  out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15870
0
             in_tdata->abiflags.isa_rev);
15871
0
  out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15872
0
              in_tdata->abiflags.gpr_size);
15873
0
  out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15874
0
               in_tdata->abiflags.cpr1_size);
15875
0
  out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15876
0
               in_tdata->abiflags.cpr2_size);
15877
0
#undef max
15878
0
  out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15879
0
  out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15880
15881
0
  return true;
15882
0
}
15883
15884
/* Merge backend specific data from an object file to the output
15885
   object file when linking.  */
15886
15887
bool
15888
_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15889
0
{
15890
0
  bfd *obfd = info->output_bfd;
15891
0
  struct mips_elf_obj_tdata *out_tdata;
15892
0
  struct mips_elf_obj_tdata *in_tdata;
15893
0
  bool null_input_bfd = true;
15894
0
  asection *sec;
15895
0
  bool ok;
15896
15897
  /* Check if we have the same endianness.  */
15898
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
15899
0
    {
15900
0
      _bfd_error_handler
15901
0
  (_("%pB: endianness incompatible with that of the selected emulation"),
15902
0
   ibfd);
15903
0
      return false;
15904
0
    }
15905
15906
0
  if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15907
0
    return true;
15908
15909
0
  in_tdata = mips_elf_tdata (ibfd);
15910
0
  out_tdata = mips_elf_tdata (obfd);
15911
15912
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15913
0
    {
15914
0
      _bfd_error_handler
15915
0
  (_("%pB: ABI is incompatible with that of the selected emulation"),
15916
0
   ibfd);
15917
0
      return false;
15918
0
    }
15919
15920
  /* Check to see if the input BFD actually contains any sections.  If not,
15921
     then it has no attributes, and its flags may not have been initialized
15922
     either, but it cannot actually cause any incompatibility.  */
15923
  /* FIXME: This excludes any input shared library from consideration.  */
15924
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15925
0
    {
15926
      /* Ignore synthetic sections and empty .text, .data and .bss sections
15927
   which are automatically generated by gas.  Also ignore fake
15928
   (s)common sections, since merely defining a common symbol does
15929
   not affect compatibility.  */
15930
0
      if ((sec->flags & SEC_IS_COMMON) == 0
15931
0
    && strcmp (sec->name, ".reginfo")
15932
0
    && strcmp (sec->name, ".mdebug")
15933
0
    && (sec->size != 0
15934
0
        || (strcmp (sec->name, ".text")
15935
0
      && strcmp (sec->name, ".data")
15936
0
      && strcmp (sec->name, ".bss"))))
15937
0
  {
15938
0
    null_input_bfd = false;
15939
0
    break;
15940
0
  }
15941
0
    }
15942
0
  if (null_input_bfd)
15943
0
    return true;
15944
15945
  /* Populate abiflags using existing information.  */
15946
0
  if (in_tdata->abiflags_valid)
15947
0
    {
15948
0
      obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15949
0
      Elf_Internal_ABIFlags_v0 in_abiflags;
15950
0
      Elf_Internal_ABIFlags_v0 abiflags;
15951
15952
      /* Set up the FP ABI attribute from the abiflags if it is not already
15953
   set.  */
15954
0
      if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15955
0
  in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15956
15957
0
      infer_mips_abiflags (ibfd, &abiflags);
15958
0
      in_abiflags = in_tdata->abiflags;
15959
15960
      /* It is not possible to infer the correct ISA revision
15961
   for R3 or R5 so drop down to R2 for the checks.  */
15962
0
      if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15963
0
  in_abiflags.isa_rev = 2;
15964
15965
0
      if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15966
0
    < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15967
0
  _bfd_error_handler
15968
0
    (_("%pB: warning: inconsistent ISA between e_flags and "
15969
0
       ".MIPS.abiflags"), ibfd);
15970
0
      if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15971
0
    && in_abiflags.fp_abi != abiflags.fp_abi)
15972
0
  _bfd_error_handler
15973
0
    (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15974
0
       ".MIPS.abiflags"), ibfd);
15975
0
      if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15976
0
  _bfd_error_handler
15977
0
    (_("%pB: warning: inconsistent ASEs between e_flags and "
15978
0
       ".MIPS.abiflags"), ibfd);
15979
      /* The isa_ext is allowed to be an extension of what can be inferred
15980
   from e_flags.  */
15981
0
      if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15982
0
        bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15983
0
  _bfd_error_handler
15984
0
    (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15985
0
       ".MIPS.abiflags"), ibfd);
15986
0
      if (in_abiflags.flags2 != 0)
15987
0
  _bfd_error_handler
15988
0
    (_("%pB: warning: unexpected flag in the flags2 field of "
15989
0
       ".MIPS.abiflags (0x%lx)"), ibfd,
15990
0
     in_abiflags.flags2);
15991
0
    }
15992
0
  else
15993
0
    {
15994
0
      infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15995
0
      in_tdata->abiflags_valid = true;
15996
0
    }
15997
15998
0
  if (!out_tdata->abiflags_valid)
15999
0
    {
16000
      /* Copy input abiflags if output abiflags are not already valid.  */
16001
0
      out_tdata->abiflags = in_tdata->abiflags;
16002
0
      out_tdata->abiflags_valid = true;
16003
0
    }
16004
16005
0
  if (! elf_flags_init (obfd))
16006
0
    {
16007
0
      elf_flags_init (obfd) = true;
16008
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16009
0
      elf_elfheader (obfd)->e_ident[EI_CLASS]
16010
0
  = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16011
16012
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16013
0
    && (bfd_get_arch_info (obfd)->the_default
16014
0
        || mips_mach_extends_p (bfd_get_mach (obfd),
16015
0
              bfd_get_mach (ibfd))))
16016
0
  {
16017
0
    if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16018
0
           bfd_get_mach (ibfd)))
16019
0
      return false;
16020
16021
    /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
16022
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16023
0
  }
16024
16025
0
      ok = true;
16026
0
    }
16027
0
  else
16028
0
    ok = mips_elf_merge_obj_e_flags (ibfd, info);
16029
16030
0
  ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16031
16032
0
  ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16033
16034
0
  if (!ok)
16035
0
    {
16036
0
      bfd_set_error (bfd_error_bad_value);
16037
0
      return false;
16038
0
    }
16039
16040
0
  return true;
16041
0
}
16042
16043
/* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
16044
16045
bool
16046
_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16047
0
{
16048
0
  BFD_ASSERT (!elf_flags_init (abfd)
16049
0
        || elf_elfheader (abfd)->e_flags == flags);
16050
16051
0
  elf_elfheader (abfd)->e_flags = flags;
16052
0
  elf_flags_init (abfd) = true;
16053
0
  return true;
16054
0
}
16055
16056
char *
16057
_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16058
0
{
16059
0
  switch (dtag)
16060
0
    {
16061
0
    default: return "";
16062
0
    case DT_MIPS_RLD_VERSION:
16063
0
      return "MIPS_RLD_VERSION";
16064
0
    case DT_MIPS_TIME_STAMP:
16065
0
      return "MIPS_TIME_STAMP";
16066
0
    case DT_MIPS_ICHECKSUM:
16067
0
      return "MIPS_ICHECKSUM";
16068
0
    case DT_MIPS_IVERSION:
16069
0
      return "MIPS_IVERSION";
16070
0
    case DT_MIPS_FLAGS:
16071
0
      return "MIPS_FLAGS";
16072
0
    case DT_MIPS_BASE_ADDRESS:
16073
0
      return "MIPS_BASE_ADDRESS";
16074
0
    case DT_MIPS_MSYM:
16075
0
      return "MIPS_MSYM";
16076
0
    case DT_MIPS_CONFLICT:
16077
0
      return "MIPS_CONFLICT";
16078
0
    case DT_MIPS_LIBLIST:
16079
0
      return "MIPS_LIBLIST";
16080
0
    case DT_MIPS_LOCAL_GOTNO:
16081
0
      return "MIPS_LOCAL_GOTNO";
16082
0
    case DT_MIPS_CONFLICTNO:
16083
0
      return "MIPS_CONFLICTNO";
16084
0
    case DT_MIPS_LIBLISTNO:
16085
0
      return "MIPS_LIBLISTNO";
16086
0
    case DT_MIPS_SYMTABNO:
16087
0
      return "MIPS_SYMTABNO";
16088
0
    case DT_MIPS_UNREFEXTNO:
16089
0
      return "MIPS_UNREFEXTNO";
16090
0
    case DT_MIPS_GOTSYM:
16091
0
      return "MIPS_GOTSYM";
16092
0
    case DT_MIPS_HIPAGENO:
16093
0
      return "MIPS_HIPAGENO";
16094
0
    case DT_MIPS_RLD_MAP:
16095
0
      return "MIPS_RLD_MAP";
16096
0
    case DT_MIPS_RLD_MAP_REL:
16097
0
      return "MIPS_RLD_MAP_REL";
16098
0
    case DT_MIPS_DELTA_CLASS:
16099
0
      return "MIPS_DELTA_CLASS";
16100
0
    case DT_MIPS_DELTA_CLASS_NO:
16101
0
      return "MIPS_DELTA_CLASS_NO";
16102
0
    case DT_MIPS_DELTA_INSTANCE:
16103
0
      return "MIPS_DELTA_INSTANCE";
16104
0
    case DT_MIPS_DELTA_INSTANCE_NO:
16105
0
      return "MIPS_DELTA_INSTANCE_NO";
16106
0
    case DT_MIPS_DELTA_RELOC:
16107
0
      return "MIPS_DELTA_RELOC";
16108
0
    case DT_MIPS_DELTA_RELOC_NO:
16109
0
      return "MIPS_DELTA_RELOC_NO";
16110
0
    case DT_MIPS_DELTA_SYM:
16111
0
      return "MIPS_DELTA_SYM";
16112
0
    case DT_MIPS_DELTA_SYM_NO:
16113
0
      return "MIPS_DELTA_SYM_NO";
16114
0
    case DT_MIPS_DELTA_CLASSSYM:
16115
0
      return "MIPS_DELTA_CLASSSYM";
16116
0
    case DT_MIPS_DELTA_CLASSSYM_NO:
16117
0
      return "MIPS_DELTA_CLASSSYM_NO";
16118
0
    case DT_MIPS_CXX_FLAGS:
16119
0
      return "MIPS_CXX_FLAGS";
16120
0
    case DT_MIPS_PIXIE_INIT:
16121
0
      return "MIPS_PIXIE_INIT";
16122
0
    case DT_MIPS_SYMBOL_LIB:
16123
0
      return "MIPS_SYMBOL_LIB";
16124
0
    case DT_MIPS_LOCALPAGE_GOTIDX:
16125
0
      return "MIPS_LOCALPAGE_GOTIDX";
16126
0
    case DT_MIPS_LOCAL_GOTIDX:
16127
0
      return "MIPS_LOCAL_GOTIDX";
16128
0
    case DT_MIPS_HIDDEN_GOTIDX:
16129
0
      return "MIPS_HIDDEN_GOTIDX";
16130
0
    case DT_MIPS_PROTECTED_GOTIDX:
16131
0
      return "MIPS_PROTECTED_GOT_IDX";
16132
0
    case DT_MIPS_OPTIONS:
16133
0
      return "MIPS_OPTIONS";
16134
0
    case DT_MIPS_INTERFACE:
16135
0
      return "MIPS_INTERFACE";
16136
0
    case DT_MIPS_DYNSTR_ALIGN:
16137
0
      return "DT_MIPS_DYNSTR_ALIGN";
16138
0
    case DT_MIPS_INTERFACE_SIZE:
16139
0
      return "DT_MIPS_INTERFACE_SIZE";
16140
0
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16141
0
      return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16142
0
    case DT_MIPS_PERF_SUFFIX:
16143
0
      return "DT_MIPS_PERF_SUFFIX";
16144
0
    case DT_MIPS_COMPACT_SIZE:
16145
0
      return "DT_MIPS_COMPACT_SIZE";
16146
0
    case DT_MIPS_GP_VALUE:
16147
0
      return "DT_MIPS_GP_VALUE";
16148
0
    case DT_MIPS_AUX_DYNAMIC:
16149
0
      return "DT_MIPS_AUX_DYNAMIC";
16150
0
    case DT_MIPS_PLTGOT:
16151
0
      return "DT_MIPS_PLTGOT";
16152
0
    case DT_MIPS_RWPLT:
16153
0
      return "DT_MIPS_RWPLT";
16154
0
    case DT_MIPS_XHASH:
16155
0
      return "DT_MIPS_XHASH";
16156
0
    }
16157
0
}
16158
16159
/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16160
   not known.  */
16161
16162
const char *
16163
_bfd_mips_fp_abi_string (int fp)
16164
0
{
16165
0
  switch (fp)
16166
0
    {
16167
      /* These strings aren't translated because they're simply
16168
   option lists.  */
16169
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16170
0
      return "-mdouble-float";
16171
16172
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16173
0
      return "-msingle-float";
16174
16175
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16176
0
      return "-msoft-float";
16177
16178
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16179
0
      return _("-mips32r2 -mfp64 (12 callee-saved)");
16180
16181
0
    case Val_GNU_MIPS_ABI_FP_XX:
16182
0
      return "-mfpxx";
16183
16184
0
    case Val_GNU_MIPS_ABI_FP_64:
16185
0
      return "-mgp32 -mfp64";
16186
16187
0
    case Val_GNU_MIPS_ABI_FP_64A:
16188
0
      return "-mgp32 -mfp64 -mno-odd-spreg";
16189
16190
0
    default:
16191
0
      return 0;
16192
0
    }
16193
0
}
16194
16195
static void
16196
print_mips_ases (FILE *file, unsigned int mask)
16197
0
{
16198
0
  if (mask & AFL_ASE_DSP)
16199
0
    fputs ("\n\tDSP ASE", file);
16200
0
  if (mask & AFL_ASE_DSPR2)
16201
0
    fputs ("\n\tDSP R2 ASE", file);
16202
0
  if (mask & AFL_ASE_DSPR3)
16203
0
    fputs ("\n\tDSP R3 ASE", file);
16204
0
  if (mask & AFL_ASE_EVA)
16205
0
    fputs ("\n\tEnhanced VA Scheme", file);
16206
0
  if (mask & AFL_ASE_MCU)
16207
0
    fputs ("\n\tMCU (MicroController) ASE", file);
16208
0
  if (mask & AFL_ASE_MDMX)
16209
0
    fputs ("\n\tMDMX ASE", file);
16210
0
  if (mask & AFL_ASE_MIPS3D)
16211
0
    fputs ("\n\tMIPS-3D ASE", file);
16212
0
  if (mask & AFL_ASE_MT)
16213
0
    fputs ("\n\tMT ASE", file);
16214
0
  if (mask & AFL_ASE_SMARTMIPS)
16215
0
    fputs ("\n\tSmartMIPS ASE", file);
16216
0
  if (mask & AFL_ASE_VIRT)
16217
0
    fputs ("\n\tVZ ASE", file);
16218
0
  if (mask & AFL_ASE_MSA)
16219
0
    fputs ("\n\tMSA ASE", file);
16220
0
  if (mask & AFL_ASE_MIPS16)
16221
0
    fputs ("\n\tMIPS16 ASE", file);
16222
0
  if (mask & AFL_ASE_MICROMIPS)
16223
0
    fputs ("\n\tMICROMIPS ASE", file);
16224
0
  if (mask & AFL_ASE_XPA)
16225
0
    fputs ("\n\tXPA ASE", file);
16226
0
  if (mask & AFL_ASE_MIPS16E2)
16227
0
    fputs ("\n\tMIPS16e2 ASE", file);
16228
0
  if (mask & AFL_ASE_CRC)
16229
0
    fputs ("\n\tCRC ASE", file);
16230
0
  if (mask & AFL_ASE_GINV)
16231
0
    fputs ("\n\tGINV ASE", file);
16232
0
  if (mask & AFL_ASE_LOONGSON_MMI)
16233
0
    fputs ("\n\tLoongson MMI ASE", file);
16234
0
  if (mask & AFL_ASE_LOONGSON_CAM)
16235
0
    fputs ("\n\tLoongson CAM ASE", file);
16236
0
  if (mask & AFL_ASE_LOONGSON_EXT)
16237
0
    fputs ("\n\tLoongson EXT ASE", file);
16238
0
  if (mask & AFL_ASE_LOONGSON_EXT2)
16239
0
    fputs ("\n\tLoongson EXT2 ASE", file);
16240
0
  if (mask == 0)
16241
0
    fprintf (file, "\n\t%s", _("None"));
16242
0
  else if ((mask & ~AFL_ASE_MASK) != 0)
16243
0
    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16244
0
}
16245
16246
static void
16247
print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16248
0
{
16249
0
  switch (isa_ext)
16250
0
    {
16251
0
    case 0:
16252
0
      fputs (_("None"), file);
16253
0
      break;
16254
0
    case AFL_EXT_XLR:
16255
0
      fputs ("RMI XLR", file);
16256
0
      break;
16257
0
    case AFL_EXT_OCTEON3:
16258
0
      fputs ("Cavium Networks Octeon3", file);
16259
0
      break;
16260
0
    case AFL_EXT_OCTEON2:
16261
0
      fputs ("Cavium Networks Octeon2", file);
16262
0
      break;
16263
0
    case AFL_EXT_OCTEONP:
16264
0
      fputs ("Cavium Networks OcteonP", file);
16265
0
      break;
16266
0
    case AFL_EXT_OCTEON:
16267
0
      fputs ("Cavium Networks Octeon", file);
16268
0
      break;
16269
0
    case AFL_EXT_5900:
16270
0
      fputs ("Toshiba R5900", file);
16271
0
      break;
16272
0
    case AFL_EXT_4650:
16273
0
      fputs ("MIPS R4650", file);
16274
0
      break;
16275
0
    case AFL_EXT_4010:
16276
0
      fputs ("LSI R4010", file);
16277
0
      break;
16278
0
    case AFL_EXT_4100:
16279
0
      fputs ("NEC VR4100", file);
16280
0
      break;
16281
0
    case AFL_EXT_3900:
16282
0
      fputs ("Toshiba R3900", file);
16283
0
      break;
16284
0
    case AFL_EXT_10000:
16285
0
      fputs ("MIPS R10000", file);
16286
0
      break;
16287
0
    case AFL_EXT_SB1:
16288
0
      fputs ("Broadcom SB-1", file);
16289
0
      break;
16290
0
    case AFL_EXT_4111:
16291
0
      fputs ("NEC VR4111/VR4181", file);
16292
0
      break;
16293
0
    case AFL_EXT_4120:
16294
0
      fputs ("NEC VR4120", file);
16295
0
      break;
16296
0
    case AFL_EXT_5400:
16297
0
      fputs ("NEC VR5400", file);
16298
0
      break;
16299
0
    case AFL_EXT_5500:
16300
0
      fputs ("NEC VR5500", file);
16301
0
      break;
16302
0
    case AFL_EXT_LOONGSON_2E:
16303
0
      fputs ("ST Microelectronics Loongson 2E", file);
16304
0
      break;
16305
0
    case AFL_EXT_LOONGSON_2F:
16306
0
      fputs ("ST Microelectronics Loongson 2F", file);
16307
0
      break;
16308
0
    case AFL_EXT_INTERAPTIV_MR2:
16309
0
      fputs ("Imagination interAptiv MR2", file);
16310
0
      break;
16311
0
    default:
16312
0
      fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16313
0
      break;
16314
0
    }
16315
0
}
16316
16317
static void
16318
print_mips_fp_abi_value (FILE *file, int val)
16319
0
{
16320
0
  switch (val)
16321
0
    {
16322
0
    case Val_GNU_MIPS_ABI_FP_ANY:
16323
0
      fprintf (file, _("Hard or soft float\n"));
16324
0
      break;
16325
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16326
0
      fprintf (file, _("Hard float (double precision)\n"));
16327
0
      break;
16328
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16329
0
      fprintf (file, _("Hard float (single precision)\n"));
16330
0
      break;
16331
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16332
0
      fprintf (file, _("Soft float\n"));
16333
0
      break;
16334
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16335
0
      fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16336
0
      break;
16337
0
    case Val_GNU_MIPS_ABI_FP_XX:
16338
0
      fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16339
0
      break;
16340
0
    case Val_GNU_MIPS_ABI_FP_64:
16341
0
      fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16342
0
      break;
16343
0
    case Val_GNU_MIPS_ABI_FP_64A:
16344
0
      fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16345
0
      break;
16346
0
    default:
16347
0
      fprintf (file, "??? (%d)\n", val);
16348
0
      break;
16349
0
    }
16350
0
}
16351
16352
static int
16353
get_mips_reg_size (int reg_size)
16354
0
{
16355
0
  return (reg_size == AFL_REG_NONE) ? 0
16356
0
   : (reg_size == AFL_REG_32) ? 32
16357
0
   : (reg_size == AFL_REG_64) ? 64
16358
0
   : (reg_size == AFL_REG_128) ? 128
16359
0
   : -1;
16360
0
}
16361
16362
bool
16363
_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16364
0
{
16365
0
  FILE *file = ptr;
16366
16367
0
  BFD_ASSERT (abfd != NULL && ptr != NULL);
16368
16369
  /* Print normal ELF private data.  */
16370
0
  _bfd_elf_print_private_bfd_data (abfd, ptr);
16371
16372
  /* xgettext:c-format */
16373
0
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16374
16375
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16376
0
    fprintf (file, _(" [abi=O32]"));
16377
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16378
0
    fprintf (file, _(" [abi=O64]"));
16379
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16380
0
    fprintf (file, _(" [abi=EABI32]"));
16381
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16382
0
    fprintf (file, _(" [abi=EABI64]"));
16383
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16384
0
    fprintf (file, _(" [abi unknown]"));
16385
0
  else if (ABI_N32_P (abfd))
16386
0
    fprintf (file, _(" [abi=N32]"));
16387
0
  else if (ABI_64_P (abfd))
16388
0
    fprintf (file, _(" [abi=64]"));
16389
0
  else
16390
0
    fprintf (file, _(" [no abi set]"));
16391
16392
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16393
0
    fprintf (file, " [mips1]");
16394
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16395
0
    fprintf (file, " [mips2]");
16396
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16397
0
    fprintf (file, " [mips3]");
16398
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16399
0
    fprintf (file, " [mips4]");
16400
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16401
0
    fprintf (file, " [mips5]");
16402
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16403
0
    fprintf (file, " [mips32]");
16404
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16405
0
    fprintf (file, " [mips64]");
16406
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16407
0
    fprintf (file, " [mips32r2]");
16408
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16409
0
    fprintf (file, " [mips64r2]");
16410
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16411
0
    fprintf (file, " [mips32r6]");
16412
0
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16413
0
    fprintf (file, " [mips64r6]");
16414
0
  else
16415
0
    fprintf (file, _(" [unknown ISA]"));
16416
16417
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16418
0
    fprintf (file, " [mdmx]");
16419
16420
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16421
0
    fprintf (file, " [mips16]");
16422
16423
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16424
0
    fprintf (file, " [micromips]");
16425
16426
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16427
0
    fprintf (file, " [nan2008]");
16428
16429
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16430
0
    fprintf (file, " [old fp64]");
16431
16432
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16433
0
    fprintf (file, " [32bitmode]");
16434
0
  else
16435
0
    fprintf (file, _(" [not 32bitmode]"));
16436
16437
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16438
0
    fprintf (file, " [noreorder]");
16439
16440
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16441
0
    fprintf (file, " [PIC]");
16442
16443
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16444
0
    fprintf (file, " [CPIC]");
16445
16446
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16447
0
    fprintf (file, " [XGOT]");
16448
16449
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16450
0
    fprintf (file, " [UCODE]");
16451
16452
0
  fputc ('\n', file);
16453
16454
0
  if (mips_elf_tdata (abfd)->abiflags_valid)
16455
0
    {
16456
0
      Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16457
0
      fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16458
0
      fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16459
0
      if (abiflags->isa_rev > 1)
16460
0
  fprintf (file, "r%d", abiflags->isa_rev);
16461
0
      fprintf (file, "\nGPR size: %d",
16462
0
         get_mips_reg_size (abiflags->gpr_size));
16463
0
      fprintf (file, "\nCPR1 size: %d",
16464
0
         get_mips_reg_size (abiflags->cpr1_size));
16465
0
      fprintf (file, "\nCPR2 size: %d",
16466
0
         get_mips_reg_size (abiflags->cpr2_size));
16467
0
      fputs ("\nFP ABI: ", file);
16468
0
      print_mips_fp_abi_value (file, abiflags->fp_abi);
16469
0
      fputs ("ISA Extension: ", file);
16470
0
      print_mips_isa_ext (file, abiflags->isa_ext);
16471
0
      fputs ("\nASEs:", file);
16472
0
      print_mips_ases (file, abiflags->ases);
16473
0
      fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16474
0
      fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16475
0
      fputc ('\n', file);
16476
0
    }
16477
16478
0
  return true;
16479
0
}
16480
16481
const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16482
{
16483
  { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16484
  { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16485
  { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16486
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16487
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16488
  { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
16489
  { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
16490
  { NULL,         0,  0, 0,        0 }
16491
};
16492
16493
/* Merge non visibility st_other attributes.  Ensure that the
16494
   STO_OPTIONAL flag is copied into h->other, even if this is not a
16495
   definiton of the symbol.  */
16496
void
16497
_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16498
              unsigned int st_other,
16499
              bool definition,
16500
              bool dynamic ATTRIBUTE_UNUSED)
16501
0
{
16502
0
  if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16503
0
    {
16504
0
      unsigned char other;
16505
16506
0
      other = (definition ? st_other : h->other);
16507
0
      other &= ~ELF_ST_VISIBILITY (-1);
16508
0
      h->other = other | ELF_ST_VISIBILITY (h->other);
16509
0
    }
16510
16511
0
  if (!definition
16512
0
      && ELF_MIPS_IS_OPTIONAL (st_other))
16513
0
    h->other |= STO_OPTIONAL;
16514
0
}
16515
16516
/* Decide whether an undefined symbol is special and can be ignored.
16517
   This is the case for OPTIONAL symbols on IRIX.  */
16518
bool
16519
_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16520
0
{
16521
0
  return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16522
0
}
16523
16524
bool
16525
_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16526
0
{
16527
0
  return (sym->st_shndx == SHN_COMMON
16528
0
    || sym->st_shndx == SHN_MIPS_ACOMMON
16529
0
    || sym->st_shndx == SHN_MIPS_SCOMMON);
16530
0
}
16531
16532
/* Return address for Ith PLT stub in section PLT, for relocation REL
16533
   or (bfd_vma) -1 if it should not be included.  */
16534
16535
bfd_vma
16536
_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16537
         const arelent *rel ATTRIBUTE_UNUSED)
16538
0
{
16539
0
  return (plt->vma
16540
0
    + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16541
0
    + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16542
0
}
16543
16544
/* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
16545
   and microMIPS PLT slots we may have a many-to-one mapping between .plt
16546
   and .got.plt and also the slots may be of a different size each we walk
16547
   the PLT manually fetching instructions and matching them against known
16548
   patterns.  To make things easier standard MIPS slots, if any, always come
16549
   first.  As we don't create proper ELF symbols we use the UDATA.I member
16550
   of ASYMBOL to carry ISA annotation.  The encoding used is the same as
16551
   with the ST_OTHER member of the ELF symbol.  */
16552
16553
long
16554
_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16555
            long symcount ATTRIBUTE_UNUSED,
16556
            asymbol **syms ATTRIBUTE_UNUSED,
16557
            long dynsymcount, asymbol **dynsyms,
16558
            asymbol **ret)
16559
0
{
16560
0
  static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16561
0
  static const char microsuffix[] = "@micromipsplt";
16562
0
  static const char m16suffix[] = "@mips16plt";
16563
0
  static const char mipssuffix[] = "@plt";
16564
16565
0
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16566
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16567
0
  bool micromips_p = MICROMIPS_P (abfd);
16568
0
  Elf_Internal_Shdr *hdr;
16569
0
  bfd_byte *plt_data;
16570
0
  bfd_vma plt_offset;
16571
0
  unsigned int other;
16572
0
  bfd_vma entry_size;
16573
0
  bfd_vma plt0_size;
16574
0
  asection *relplt;
16575
0
  bfd_vma opcode;
16576
0
  asection *plt;
16577
0
  asymbol *send;
16578
0
  size_t size;
16579
0
  char *names;
16580
0
  long counti;
16581
0
  arelent *p;
16582
0
  asymbol *s;
16583
0
  char *nend;
16584
0
  long count;
16585
0
  long pi;
16586
0
  long i;
16587
0
  long n;
16588
16589
0
  *ret = NULL;
16590
16591
0
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16592
0
    return 0;
16593
16594
0
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16595
0
  if (relplt == NULL)
16596
0
    return 0;
16597
16598
0
  hdr = &elf_section_data (relplt)->this_hdr;
16599
0
  if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16600
0
    return 0;
16601
16602
0
  plt = bfd_get_section_by_name (abfd, ".plt");
16603
0
  if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16604
0
    return 0;
16605
16606
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16607
0
  if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16608
0
    return -1;
16609
0
  p = relplt->relocation;
16610
16611
  /* Calculating the exact amount of space required for symbols would
16612
     require two passes over the PLT, so just pessimise assuming two
16613
     PLT slots per relocation.  */
16614
0
  count = NUM_SHDR_ENTRIES (hdr);
16615
0
  counti = count * bed->s->int_rels_per_ext_rel;
16616
0
  size = 2 * count * sizeof (asymbol);
16617
0
  size += count * (sizeof (mipssuffix) +
16618
0
       (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16619
0
  for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16620
0
    size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16621
16622
  /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
16623
0
  size += sizeof (asymbol) + sizeof (pltname);
16624
16625
0
  if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16626
0
    return -1;
16627
16628
0
  if (plt->size < 16)
16629
0
    return -1;
16630
16631
0
  s = *ret = bfd_malloc (size);
16632
0
  if (s == NULL)
16633
0
    return -1;
16634
0
  send = s + 2 * count + 1;
16635
16636
0
  names = (char *) send;
16637
0
  nend = (char *) s + size;
16638
0
  n = 0;
16639
16640
0
  opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16641
0
  if (opcode == 0x3302fffe)
16642
0
    {
16643
0
      if (!micromips_p)
16644
0
  return -1;
16645
0
      plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16646
0
      other = STO_MICROMIPS;
16647
0
    }
16648
0
  else if (opcode == 0x0398c1d0)
16649
0
    {
16650
0
      if (!micromips_p)
16651
0
  return -1;
16652
0
      plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16653
0
      other = STO_MICROMIPS;
16654
0
    }
16655
0
  else
16656
0
    {
16657
0
      plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16658
0
      other = 0;
16659
0
    }
16660
16661
0
  s->the_bfd = abfd;
16662
0
  s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16663
0
  s->section = plt;
16664
0
  s->value = 0;
16665
0
  s->name = names;
16666
0
  s->udata.i = other;
16667
0
  memcpy (names, pltname, sizeof (pltname));
16668
0
  names += sizeof (pltname);
16669
0
  ++s, ++n;
16670
16671
0
  pi = 0;
16672
0
  for (plt_offset = plt0_size;
16673
0
       plt_offset + 8 <= plt->size && s < send;
16674
0
       plt_offset += entry_size)
16675
0
    {
16676
0
      bfd_vma gotplt_addr;
16677
0
      const char *suffix;
16678
0
      bfd_vma gotplt_hi;
16679
0
      bfd_vma gotplt_lo;
16680
0
      size_t suffixlen;
16681
16682
0
      opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16683
16684
      /* Check if the second word matches the expected MIPS16 instruction.  */
16685
0
      if (opcode == 0x651aeb00)
16686
0
  {
16687
0
    if (micromips_p)
16688
0
      return -1;
16689
    /* Truncated table???  */
16690
0
    if (plt_offset + 16 > plt->size)
16691
0
      break;
16692
0
    gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16693
0
    entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16694
0
    suffixlen = sizeof (m16suffix);
16695
0
    suffix = m16suffix;
16696
0
    other = STO_MIPS16;
16697
0
  }
16698
      /* Likewise the expected microMIPS instruction (no insn32 mode).  */
16699
0
      else if (opcode == 0xff220000)
16700
0
  {
16701
0
    if (!micromips_p)
16702
0
      return -1;
16703
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16704
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16705
0
    gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16706
0
    gotplt_lo <<= 2;
16707
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16708
0
    gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16709
0
    entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16710
0
    suffixlen = sizeof (microsuffix);
16711
0
    suffix = microsuffix;
16712
0
    other = STO_MICROMIPS;
16713
0
  }
16714
      /* Likewise the expected microMIPS instruction (insn32 mode).  */
16715
0
      else if ((opcode & 0xffff0000) == 0xff2f0000)
16716
0
  {
16717
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16718
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16719
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16720
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16721
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16722
0
    entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16723
0
    suffixlen = sizeof (microsuffix);
16724
0
    suffix = microsuffix;
16725
0
    other = STO_MICROMIPS;
16726
0
  }
16727
      /* Otherwise assume standard MIPS code.  */
16728
0
      else
16729
0
  {
16730
0
    gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16731
0
    gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16732
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16733
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16734
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16735
0
    entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16736
0
    suffixlen = sizeof (mipssuffix);
16737
0
    suffix = mipssuffix;
16738
0
    other = 0;
16739
0
  }
16740
      /* Truncated table???  */
16741
0
      if (plt_offset + entry_size > plt->size)
16742
0
  break;
16743
16744
0
      for (i = 0;
16745
0
     i < count && p[pi].address != gotplt_addr;
16746
0
     i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16747
16748
0
      if (i < count)
16749
0
  {
16750
0
    size_t namelen;
16751
0
    size_t len;
16752
16753
0
    *s = **p[pi].sym_ptr_ptr;
16754
    /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
16755
       we are defining a symbol, ensure one of them is set.  */
16756
0
    if ((s->flags & BSF_LOCAL) == 0)
16757
0
      s->flags |= BSF_GLOBAL;
16758
0
    s->flags |= BSF_SYNTHETIC;
16759
0
    s->section = plt;
16760
0
    s->value = plt_offset;
16761
0
    s->name = names;
16762
0
    s->udata.i = other;
16763
16764
0
    len = strlen ((*p[pi].sym_ptr_ptr)->name);
16765
0
    namelen = len + suffixlen;
16766
0
    if (names + namelen > nend)
16767
0
      break;
16768
16769
0
    memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16770
0
    names += len;
16771
0
    memcpy (names, suffix, suffixlen);
16772
0
    names += suffixlen;
16773
16774
0
    ++s, ++n;
16775
0
    pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16776
0
  }
16777
0
    }
16778
16779
0
  free (plt_data);
16780
16781
0
  return n;
16782
0
}
16783
16784
/* Return the ABI flags associated with ABFD if available.  */
16785
16786
Elf_Internal_ABIFlags_v0 *
16787
bfd_mips_elf_get_abiflags (bfd *abfd)
16788
0
{
16789
0
  struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16790
16791
0
  return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16792
0
}
16793
16794
/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16795
   field.  Taken from `libc-abis.h' generated at GNU libc build time.
16796
   Using a MIPS_ prefix as other libc targets use different values.  */
16797
enum
16798
{
16799
  MIPS_LIBC_ABI_DEFAULT = 0,
16800
  MIPS_LIBC_ABI_MIPS_PLT,
16801
  MIPS_LIBC_ABI_UNIQUE,
16802
  MIPS_LIBC_ABI_MIPS_O32_FP64,
16803
  MIPS_LIBC_ABI_ABSOLUTE,
16804
  MIPS_LIBC_ABI_XHASH,
16805
  MIPS_LIBC_ABI_MAX
16806
};
16807
16808
bool
16809
_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16810
0
{
16811
0
  struct mips_elf_link_hash_table *htab = NULL;
16812
0
  Elf_Internal_Ehdr *i_ehdrp;
16813
16814
0
  if (!_bfd_elf_init_file_header (abfd, link_info))
16815
0
    return false;
16816
16817
0
  i_ehdrp = elf_elfheader (abfd);
16818
0
  if (link_info)
16819
0
    {
16820
0
      htab = mips_elf_hash_table (link_info);
16821
0
      BFD_ASSERT (htab != NULL);
16822
0
    }
16823
16824
0
  if (htab != NULL
16825
0
      && htab->use_plts_and_copy_relocs
16826
0
      && htab->root.target_os != is_vxworks)
16827
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16828
16829
0
  if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16830
0
      || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16831
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16832
16833
  /* Mark that we need support for absolute symbols in the dynamic loader.  */
16834
0
  if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16835
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16836
16837
  /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16838
     if it is the only hash section that will be created.  */
16839
0
  if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16840
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16841
0
  return true;
16842
0
}
16843
16844
int
16845
_bfd_mips_elf_compact_eh_encoding
16846
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16847
0
{
16848
0
  return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16849
0
}
16850
16851
/* Return the opcode for can't unwind.  */
16852
16853
int
16854
_bfd_mips_elf_cant_unwind_opcode
16855
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16856
0
{
16857
0
  return COMPACT_EH_CANT_UNWIND_OPCODE;
16858
0
}
16859
16860
/* Record a position XLAT_LOC in the xlat translation table, associated with
16861
   the hash entry H.  The entry in the translation table will later be
16862
   populated with the real symbol dynindx.  */
16863
16864
void
16865
_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16866
           bfd_vma xlat_loc)
16867
0
{
16868
0
  struct mips_elf_link_hash_entry *hmips;
16869
16870
0
  hmips = (struct mips_elf_link_hash_entry *) h;
16871
0
  hmips->mipsxhash_loc = xlat_loc;
16872
0
}