Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elfxx-mips.c
Line
Count
Source
1
/* MIPS-specific support for ELF
2
   Copyright (C) 1993-2026 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
/* Used to store a REL high-part relocation, such as R_MIPS_HI16 or
226
   R_MIPS_GOT16.  REL is the relocation.  DATA nominally points to the
227
   start of the section  contents, but note that gas may use multiple
228
   chunks of memory for a section (with DATA + [offset,offset+frag_size)
229
   addressing a given frag).  A HI16 reloc might need a different "data"
230
   from a lo16.  OUTPUT_BFD is the output BFD for relocatable output or
231
   a NULL pointer otherwise.  */
232
233
struct mips_hi16
234
{
235
  struct mips_hi16 *next;
236
  bfd *output_bfd;
237
  bfd_byte *data;
238
  arelent rel;
239
};
240
241
struct _mips_elf_section_data
242
{
243
  struct bfd_elf_section_data elf;
244
  union
245
  {
246
    bfd_byte *tdata;
247
  } u;
248
249
  struct mips_hi16 *mips_hi16_list;
250
};
251
252
#define mips_elf_section_data(sec) \
253
48.8k
  ((struct _mips_elf_section_data *) elf_section_data (sec))
254
255
#define is_mips_elf(bfd)        \
256
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
257
0
   && elf_tdata (bfd) != NULL        \
258
0
   && elf_object_id (bfd) == MIPS_ELF_DATA)
259
260
/* The ABI says that every symbol used by dynamic relocations must have
261
   a global GOT entry.  Among other things, this provides the dynamic
262
   linker with a free, directly-indexed cache.  The GOT can therefore
263
   contain symbols that are not referenced by GOT relocations themselves
264
   (in other words, it may have symbols that are not referenced by things
265
   like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
266
267
   GOT relocations are less likely to overflow if we put the associated
268
   GOT entries towards the beginning.  We therefore divide the global
269
   GOT entries into two areas: "normal" and "reloc-only".  Entries in
270
   the first area can be used for both dynamic relocations and GP-relative
271
   accesses, while those in the "reloc-only" area are for dynamic
272
   relocations only.
273
274
   These GGA_* ("Global GOT Area") values are organised so that lower
275
   values are more general than higher values.  Also, non-GGA_NONE
276
   values are ordered by the position of the area in the GOT.  */
277
0
#define GGA_NORMAL 0
278
0
#define GGA_RELOC_ONLY 1
279
0
#define GGA_NONE 2
280
281
/* Information about a non-PIC interface to a PIC function.  There are
282
   two ways of creating these interfaces.  The first is to add:
283
284
  lui $25,%hi(func)
285
  addiu $25,$25,%lo(func)
286
287
   immediately before a PIC function "func".  The second is to add:
288
289
  lui $25,%hi(func)
290
  j func
291
  addiu $25,$25,%lo(func)
292
293
   to a separate trampoline section.
294
295
   Stubs of the first kind go in a new section immediately before the
296
   target function.  Stubs of the second kind go in a single section
297
   pointed to by the hash table's "strampoline" field.  */
298
struct mips_elf_la25_stub {
299
  /* The generated section that contains this stub.  */
300
  asection *stub_section;
301
302
  /* The offset of the stub from the start of STUB_SECTION.  */
303
  bfd_vma offset;
304
305
  /* One symbol for the original function.  Its location is available
306
     in H->root.root.u.def.  */
307
  struct mips_elf_link_hash_entry *h;
308
};
309
310
/* Macros for populating a mips_elf_la25_stub.  */
311
312
#define LA25_LUI(VAL) (0x3c190000 | (VAL))  /* lui t9,VAL */
313
#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
314
#define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
315
#define LA25_ADDIU(VAL) (0x27390000 | (VAL))  /* addiu t9,t9,VAL */
316
#define LA25_LUI_MICROMIPS(VAL)           \
317
0
  (0x41b90000 | (VAL))        /* lui t9,VAL */
318
#define LA25_J_MICROMIPS(VAL)           \
319
0
  (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))  /* j VAL */
320
#define LA25_ADDIU_MICROMIPS(VAL)         \
321
0
  (0x33390000 | (VAL))        /* addiu t9,t9,VAL */
322
323
/* This structure is passed to mips_elf_sort_hash_table_f when sorting
324
   the dynamic symbols.  */
325
326
struct mips_elf_hash_sort_data
327
{
328
  /* The symbol in the global GOT with the lowest dynamic symbol table
329
     index.  */
330
  struct elf_link_hash_entry *low;
331
  /* The least dynamic symbol table index corresponding to a non-TLS
332
     symbol with a GOT entry.  */
333
  bfd_size_type min_got_dynindx;
334
  /* The greatest dynamic symbol table index corresponding to a symbol
335
     with a GOT entry that is not referenced (e.g., a dynamic symbol
336
     with dynamic relocations pointing to it from non-primary GOTs).  */
337
  bfd_size_type max_unref_got_dynindx;
338
  /* The greatest dynamic symbol table index corresponding to a local
339
     symbol.  */
340
  bfd_size_type max_local_dynindx;
341
  /* The greatest dynamic symbol table index corresponding to an external
342
     symbol without a GOT entry.  */
343
  bfd_size_type max_non_got_dynindx;
344
  /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
345
  bfd *output_bfd;
346
  /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
347
     real final dynindx.  */
348
  bfd_byte *mipsxhash;
349
};
350
351
/* We make up to two PLT entries if needed, one for standard MIPS code
352
   and one for compressed code, either a MIPS16 or microMIPS one.  We
353
   keep a separate record of traditional lazy-binding stubs, for easier
354
   processing.  */
355
356
struct plt_entry
357
{
358
  /* Traditional SVR4 stub offset, or -1 if none.  */
359
  bfd_vma stub_offset;
360
361
  /* Standard PLT entry offset, or -1 if none.  */
362
  bfd_vma mips_offset;
363
364
  /* Compressed PLT entry offset, or -1 if none.  */
365
  bfd_vma comp_offset;
366
367
  /* The corresponding .got.plt index, or -1 if none.  */
368
  bfd_vma gotplt_index;
369
370
  /* Whether we need a standard PLT entry.  */
371
  unsigned int need_mips : 1;
372
373
  /* Whether we need a compressed PLT entry.  */
374
  unsigned int need_comp : 1;
375
};
376
377
/* The MIPS ELF linker needs additional information for each symbol in
378
   the global hash table.  */
379
380
struct mips_elf_link_hash_entry
381
{
382
  struct elf_link_hash_entry root;
383
384
  /* External symbol information.  */
385
  EXTR esym;
386
387
  /* The la25 stub we have created for ths symbol, if any.  */
388
  struct mips_elf_la25_stub *la25_stub;
389
390
  /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
391
     this symbol.  */
392
  unsigned int possibly_dynamic_relocs;
393
394
  /* If there is a stub that 32 bit functions should use to call this
395
     16 bit function, this points to the section containing the stub.  */
396
  asection *fn_stub;
397
398
  /* If there is a stub that 16 bit functions should use to call this
399
     32 bit function, this points to the section containing the stub.  */
400
  asection *call_stub;
401
402
  /* This is like the call_stub field, but it is used if the function
403
     being called returns a floating point value.  */
404
  asection *call_fp_stub;
405
406
  /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
407
  bfd_vma mipsxhash_loc;
408
409
  /* The highest GGA_* value that satisfies all references to this symbol.  */
410
  unsigned int global_got_area : 2;
411
412
  /* True if all GOT relocations against this symbol are for calls.  This is
413
     a looser condition than no_fn_stub below, because there may be other
414
     non-call non-GOT relocations against the symbol.  */
415
  unsigned int got_only_for_calls : 1;
416
417
  /* True if one of the relocations described by possibly_dynamic_relocs
418
     is against a readonly section.  */
419
  unsigned int readonly_reloc : 1;
420
421
  /* True if there is a relocation against this symbol that must be
422
     resolved by the static linker (in other words, if the relocation
423
     cannot possibly be made dynamic).  */
424
  unsigned int has_static_relocs : 1;
425
426
  /* True if we must not create a .MIPS.stubs entry for this symbol.
427
     This is set, for example, if there are relocations related to
428
     taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
429
     See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
430
  unsigned int no_fn_stub : 1;
431
432
  /* Whether we need the fn_stub; this is true if this symbol appears
433
     in any relocs other than a 16 bit call.  */
434
  unsigned int need_fn_stub : 1;
435
436
  /* True if this symbol is referenced by branch relocations from
437
     any non-PIC input file.  This is used to determine whether an
438
     la25 stub is required.  */
439
  unsigned int has_nonpic_branches : 1;
440
441
  /* Does this symbol need a traditional MIPS lazy-binding stub
442
     (as opposed to a PLT entry)?  */
443
  unsigned int needs_lazy_stub : 1;
444
445
  /* Does this symbol resolve to a PLT entry?  */
446
  unsigned int use_plt_entry : 1;
447
};
448
449
/* MIPS ELF linker hash table.  */
450
451
struct mips_elf_link_hash_table
452
{
453
  struct elf_link_hash_table root;
454
455
  /* The number of .rtproc entries.  */
456
  bfd_size_type procedure_count;
457
458
  /* The size of the .compact_rel section (if SGI_COMPAT).  */
459
  bfd_size_type compact_rel_size;
460
461
  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
462
     is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
463
  bool use_rld_obj_head;
464
465
  /* The  __rld_map or __rld_obj_head symbol. */
466
  struct elf_link_hash_entry *rld_symbol;
467
468
  /* This is set if we see any mips16 stub sections.  */
469
  bool mips16_stubs_seen;
470
471
  /* True if we can generate copy relocs and PLTs.  */
472
  bool use_plts_and_copy_relocs;
473
474
  /* True if we can only use 32-bit microMIPS instructions.  */
475
  bool insn32;
476
477
  /* True if we suppress checks for invalid branches between ISA modes.  */
478
  bool ignore_branch_isa;
479
480
  /* True if we are targetting R6 compact branches.  */
481
  bool compact_branches;
482
483
  /* True if we already reported the small-data section overflow.  */
484
  bool small_data_overflow_reported;
485
486
  /* True if we use the special `__gnu_absolute_zero' symbol.  */
487
  bool use_absolute_zero;
488
489
  /* True if we have been configured for a GNU target.  */
490
  bool gnu_target;
491
492
  /* Shortcuts to some dynamic sections, or NULL if they are not
493
     being used.  */
494
  asection *srelplt2;
495
  asection *sstubs;
496
497
  /* The master GOT information.  */
498
  struct mips_got_info *got_info;
499
500
  /* The global symbol in the GOT with the lowest index in the dynamic
501
     symbol table.  */
502
  struct elf_link_hash_entry *global_gotsym;
503
504
  /* The size of the PLT header in bytes.  */
505
  bfd_vma plt_header_size;
506
507
  /* The size of a standard PLT entry in bytes.  */
508
  bfd_vma plt_mips_entry_size;
509
510
  /* The size of a compressed PLT entry in bytes.  */
511
  bfd_vma plt_comp_entry_size;
512
513
  /* The offset of the next standard PLT entry to create.  */
514
  bfd_vma plt_mips_offset;
515
516
  /* The offset of the next compressed PLT entry to create.  */
517
  bfd_vma plt_comp_offset;
518
519
  /* The index of the next .got.plt entry to create.  */
520
  bfd_vma plt_got_index;
521
522
  /* The number of functions that need a lazy-binding stub.  */
523
  bfd_vma lazy_stub_count;
524
525
  /* The size of a function stub entry in bytes.  */
526
  bfd_vma function_stub_size;
527
528
  /* The number of reserved entries at the beginning of the GOT.  */
529
  unsigned int reserved_gotno;
530
531
  /* The section used for mips_elf_la25_stub trampolines.
532
     See the comment above that structure for details.  */
533
  asection *strampoline;
534
535
  /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
536
     pairs.  */
537
  htab_t la25_stubs;
538
539
  /* A function FN (NAME, IS, OS) that creates a new input section
540
     called NAME and links it to output section OS.  If IS is nonnull,
541
     the new section should go immediately before it, otherwise it
542
     should go at the (current) beginning of OS.
543
544
     The function returns the new section on success, otherwise it
545
     returns null.  */
546
  asection *(*add_stub_section) (const char *, asection *, asection *);
547
548
  /* Is the PLT header compressed?  */
549
  unsigned int plt_header_is_comp : 1;
550
};
551
552
/* Get the MIPS ELF linker hash table from a link_info structure.  */
553
554
#define mips_elf_hash_table(p) \
555
0
  ((is_elf_hash_table ((p)->hash)          \
556
0
    && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)   \
557
0
   ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
558
559
/* A structure used to communicate with htab_traverse callbacks.  */
560
struct mips_htab_traverse_info
561
{
562
  /* The usual link-wide information.  */
563
  struct bfd_link_info *info;
564
  bfd *output_bfd;
565
566
  /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
567
  bool error;
568
};
569
570
/* MIPS ELF private object data.  */
571
572
struct mips_elf_obj_tdata
573
{
574
  /* Generic ELF private object data.  */
575
  struct elf_obj_tdata root;
576
577
  /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
578
  bfd *abi_fp_bfd;
579
580
  /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
581
  bfd *abi_msa_bfd;
582
583
  /* The abiflags for this object.  */
584
  Elf_Internal_ABIFlags_v0 abiflags;
585
  bool abiflags_valid;
586
587
  /* The GOT requirements of input bfds.  */
588
  struct mips_got_info *got;
589
590
  /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
591
     included directly in this one, but there's no point to wasting
592
     the memory just for the infrequently called find_nearest_line.  */
593
  struct mips_elf_find_line *find_line_info;
594
595
  /* An array of stub sections indexed by symbol number.  */
596
  asection **local_stubs;
597
  asection **local_call_stubs;
598
599
  /* The Irix 5 support uses two virtual sections, which represent
600
     text/data symbols defined in dynamic objects.  */
601
  asection *elf_data_section;
602
  asection *elf_text_section;
603
};
604
605
/* Get MIPS ELF private object data from BFD's tdata.  */
606
607
#define mips_elf_tdata(bfd) \
608
978k
  ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
609
610
#define TLS_RELOC_P(r_type) \
611
0
  (r_type == R_MIPS_TLS_DTPMOD32    \
612
0
   || r_type == R_MIPS_TLS_DTPMOD64    \
613
0
   || r_type == R_MIPS_TLS_DTPREL32    \
614
0
   || r_type == R_MIPS_TLS_DTPREL64    \
615
0
   || r_type == R_MIPS_TLS_GD      \
616
0
   || r_type == R_MIPS_TLS_LDM      \
617
0
   || r_type == R_MIPS_TLS_DTPREL_HI16    \
618
0
   || r_type == R_MIPS_TLS_DTPREL_LO16    \
619
0
   || r_type == R_MIPS_TLS_GOTTPREL    \
620
0
   || r_type == R_MIPS_TLS_TPREL32    \
621
0
   || r_type == R_MIPS_TLS_TPREL64    \
622
0
   || r_type == R_MIPS_TLS_TPREL_HI16    \
623
0
   || r_type == R_MIPS_TLS_TPREL_LO16    \
624
0
   || r_type == R_MIPS16_TLS_GD      \
625
0
   || r_type == R_MIPS16_TLS_LDM    \
626
0
   || r_type == R_MIPS16_TLS_DTPREL_HI16  \
627
0
   || r_type == R_MIPS16_TLS_DTPREL_LO16  \
628
0
   || r_type == R_MIPS16_TLS_GOTTPREL    \
629
0
   || r_type == R_MIPS16_TLS_TPREL_HI16    \
630
0
   || r_type == R_MIPS16_TLS_TPREL_LO16    \
631
0
   || r_type == R_MICROMIPS_TLS_GD    \
632
0
   || r_type == R_MICROMIPS_TLS_LDM    \
633
0
   || r_type == R_MICROMIPS_TLS_DTPREL_HI16  \
634
0
   || r_type == R_MICROMIPS_TLS_DTPREL_LO16  \
635
0
   || r_type == R_MICROMIPS_TLS_GOTTPREL  \
636
0
   || r_type == R_MICROMIPS_TLS_TPREL_HI16  \
637
0
   || r_type == R_MICROMIPS_TLS_TPREL_LO16)
638
639
/* Structure used to pass information to mips_elf_output_extsym.  */
640
641
struct extsym_info
642
{
643
  bfd *abfd;
644
  struct bfd_link_info *info;
645
  struct ecoff_debug_info *debug;
646
  const struct ecoff_debug_swap *swap;
647
  bool failed;
648
};
649
650
/* The names of the runtime procedure table symbols used on IRIX5.  */
651
652
static const char * const mips_elf_dynsym_rtproc_names[] =
653
{
654
  "_procedure_table",
655
  "_procedure_string_table",
656
  "_procedure_table_size",
657
  NULL
658
};
659
660
/* These structures are used to generate the .compact_rel section on
661
   IRIX5.  */
662
663
typedef struct
664
{
665
  unsigned long id1;    /* Always one?  */
666
  unsigned long num;    /* Number of compact relocation entries.  */
667
  unsigned long id2;    /* Always two?  */
668
  unsigned long offset;   /* The file offset of the first relocation.  */
669
  unsigned long reserved0;  /* Zero?  */
670
  unsigned long reserved1;  /* Zero?  */
671
} Elf32_compact_rel;
672
673
typedef struct
674
{
675
  bfd_byte id1[4];
676
  bfd_byte num[4];
677
  bfd_byte id2[4];
678
  bfd_byte offset[4];
679
  bfd_byte reserved0[4];
680
  bfd_byte reserved1[4];
681
} Elf32_External_compact_rel;
682
683
typedef struct
684
{
685
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
686
  unsigned int rtype : 4; /* Relocation types. See below.  */
687
  unsigned int dist2to : 8;
688
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
689
  unsigned long konst;    /* KONST field. See below.  */
690
  unsigned long vaddr;    /* VADDR to be relocated.  */
691
} Elf32_crinfo;
692
693
typedef struct
694
{
695
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
696
  unsigned int rtype : 4; /* Relocation types. See below.  */
697
  unsigned int dist2to : 8;
698
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
699
  unsigned long konst;    /* KONST field. See below.  */
700
} Elf32_crinfo2;
701
702
typedef struct
703
{
704
  bfd_byte info[4];
705
  bfd_byte konst[4];
706
  bfd_byte vaddr[4];
707
} Elf32_External_crinfo;
708
709
typedef struct
710
{
711
  bfd_byte info[4];
712
  bfd_byte konst[4];
713
} Elf32_External_crinfo2;
714
715
/* These are the constants used to swap the bitfields in a crinfo.  */
716
717
0
#define CRINFO_CTYPE (0x1U)
718
0
#define CRINFO_CTYPE_SH (31)
719
0
#define CRINFO_RTYPE (0xfU)
720
0
#define CRINFO_RTYPE_SH (27)
721
0
#define CRINFO_DIST2TO (0xffU)
722
0
#define CRINFO_DIST2TO_SH (19)
723
0
#define CRINFO_RELVADDR (0x7ffffU)
724
0
#define CRINFO_RELVADDR_SH (0)
725
726
/* A compact relocation info has long (3 words) or short (2 words)
727
   formats.  A short format doesn't have VADDR field and relvaddr
728
   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
729
#define CRF_MIPS_LONG     1
730
#define CRF_MIPS_SHORT      0
731
732
/* There are 4 types of compact relocation at least. The value KONST
733
   has different meaning for each type:
734
735
   (type)   (konst)
736
   CT_MIPS_REL32  Address in data
737
   CT_MIPS_WORD   Address in word (XXX)
738
   CT_MIPS_GPHI_LO  GP - vaddr
739
   CT_MIPS_JMPAD  Address to jump
740
   */
741
742
#define CRT_MIPS_REL32      0xa
743
#define CRT_MIPS_WORD     0xb
744
#define CRT_MIPS_GPHI_LO    0xc
745
#define CRT_MIPS_JMPAD      0xd
746
747
0
#define mips_elf_set_cr_format(x,format)  ((x).ctype = (format))
748
0
#define mips_elf_set_cr_type(x,type)    ((x).rtype = (type))
749
0
#define mips_elf_set_cr_dist2to(x,v)    ((x).dist2to = (v))
750
0
#define mips_elf_set_cr_relvaddr(x,d)   ((x).relvaddr = (d)<<2)
751

752
/* The structure of the runtime procedure descriptor created by the
753
   loader for use by the static exception system.  */
754
755
typedef struct runtime_pdr {
756
  bfd_vma adr;    /* Memory address of start of procedure.  */
757
  long  regmask;  /* Save register mask.  */
758
  long  regoffset;  /* Save register offset.  */
759
  long  fregmask; /* Save floating point register mask.  */
760
  long  fregoffset; /* Save floating point register offset.  */
761
  long  frameoffset;  /* Frame size.  */
762
  short framereg; /* Frame pointer register.  */
763
  short pcreg;    /* Offset or reg of return pc.  */
764
  long  irpss;    /* Index into the runtime string table.  */
765
  long  reserved;
766
  struct exception_info *exception_info;/* Pointer to exception array.  */
767
} RPDR, *pRPDR;
768
#define cbRPDR sizeof (RPDR)
769
#define rpdNil ((pRPDR) 0)
770

771
static struct mips_got_entry *mips_elf_create_local_got_entry
772
  (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
773
   struct mips_elf_link_hash_entry *, int);
774
static bool mips_elf_sort_hash_table_f
775
  (struct mips_elf_link_hash_entry *, void *);
776
static bfd_vma mips_elf_high
777
  (bfd_vma);
778
static bool mips_elf_create_dynamic_relocation
779
  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
780
   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
781
   bfd_vma *, asection *);
782
static bfd_vma mips_elf_adjust_gp
783
  (bfd *, struct mips_got_info *, bfd *);
784
static bool mips_elf_free_hi16_list
785
  (bfd *, asection *, bool, struct bfd_link_info *);
786
787
/* This will be used when we sort the dynamic relocation records.  */
788
static bfd *reldyn_sorting_bfd;
789
790
/* True if ABFD is for CPUs with load interlocking that include
791
   non-MIPS1 CPUs and R3900.  */
792
#define LOAD_INTERLOCKS_P(abfd) \
793
0
  (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
794
0
   || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
795
796
/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
797
   This should be safe for all architectures.  We enable this predicate
798
   for RM9000 for now.  */
799
#define JAL_TO_BAL_P(abfd) \
800
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
801
802
/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
803
   This should be safe for all architectures.  We enable this predicate for
804
   all CPUs.  */
805
0
#define JALR_TO_BAL_P(abfd) 1
806
807
/* True if ABFD is for CPUs that are faster if JR is converted to B.
808
   This should be safe for all architectures.  We enable this predicate for
809
   all CPUs.  */
810
0
#define JR_TO_B_P(abfd) 1
811
812
/* True if ABFD is a PIC object.  */
813
#define PIC_OBJECT_P(abfd) \
814
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
815
816
/* Nonzero if ABFD is using the O32 ABI.  */
817
#define ABI_O32_P(abfd) \
818
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
819
820
/* Nonzero if ABFD is using the N32 ABI.  */
821
#define ABI_N32_P(abfd) \
822
772
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
823
824
/* Nonzero if ABFD is using NewABI conventions.  */
825
223
#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
826
827
/* Nonzero if ABFD has microMIPS code.  */
828
#define MICROMIPS_P(abfd) \
829
312
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
830
831
/* Nonzero if ABFD is MIPS R6.  */
832
#define MIPSR6_P(abfd) \
833
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
834
0
    || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
835
836
/* The IRIX compatibility level we are striving for.  */
837
#define IRIX_COMPAT(abfd) \
838
333
  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
839
840
/* Whether we are trying to be compatible with IRIX at all.  */
841
#define SGI_COMPAT(abfd) \
842
553
  (IRIX_COMPAT (abfd) != ict_none)
843
844
/* The name of the options section.  */
845
#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
846
111
  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
847
848
/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
849
   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
850
#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
851
657
  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
852
853
/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
854
#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
855
49
  (strcmp (NAME, ".MIPS.abiflags") == 0)
856
857
/* Whether the section is readonly.  */
858
#define MIPS_ELF_READONLY_SECTION(sec) \
859
0
  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))    \
860
0
   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
861
862
/* The name of the stub section.  */
863
0
#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
864
865
/* The size of an external REL relocation.  */
866
#define MIPS_ELF_REL_SIZE(abfd) \
867
0
  (get_elf_backend_data (abfd)->s->sizeof_rel)
868
869
/* The size of an external RELA relocation.  */
870
#define MIPS_ELF_RELA_SIZE(abfd) \
871
0
  (get_elf_backend_data (abfd)->s->sizeof_rela)
872
873
/* The size of an external dynamic table entry.  */
874
#define MIPS_ELF_DYN_SIZE(abfd) \
875
0
  (get_elf_backend_data (abfd)->s->sizeof_dyn)
876
877
/* The size of a GOT entry.  */
878
#define MIPS_ELF_GOT_SIZE(abfd) \
879
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
880
881
/* The size of the .rld_map section. */
882
#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
883
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
884
885
/* The size of a symbol-table entry.  */
886
#define MIPS_ELF_SYM_SIZE(abfd) \
887
0
  (get_elf_backend_data (abfd)->s->sizeof_sym)
888
889
/* The default alignment for sections, as a power of two.  */
890
#define MIPS_ELF_LOG_FILE_ALIGN(abfd)       \
891
0
  (get_elf_backend_data (abfd)->s->log_file_align)
892
893
/* Get word-sized data.  */
894
#define MIPS_ELF_GET_WORD(abfd, ptr) \
895
  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
896
897
/* Put out word-sized data.  */
898
#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
899
0
  (ABI_64_P (abfd)       \
900
0
   ? bfd_put_64 (abfd, val, ptr)   \
901
0
   : bfd_put_32 (abfd, val, ptr))
902
903
/* The opcode for word-sized loads (LW or LD).  */
904
#define MIPS_ELF_LOAD_WORD(abfd) \
905
0
  (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
906
907
/* Add a dynamic symbol table-entry.  */
908
#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)  \
909
0
  _bfd_elf_add_dynamic_entry (info, tag, val)
910
911
#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)      \
912
3
  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
913
914
/* The name of the dynamic relocation section.  */
915
#define MIPS_ELF_REL_DYN_NAME(INFO) \
916
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
917
0
   ? ".rela.dyn" : ".rel.dyn")
918
919
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
920
   from smaller values.  Start with zero, widen, *then* decrement.  */
921
0
#define MINUS_ONE (((bfd_vma)0) - 1)
922
0
#define MINUS_TWO (((bfd_vma)0) - 2)
923
924
/* The value to write into got[1] for SVR4 targets, to identify it is
925
   a GNU object.  The dynamic linker can then use got[1] to store the
926
   module pointer.  */
927
#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
928
  ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
929
930
/* The offset of $gp from the beginning of the .got section.  */
931
#define ELF_MIPS_GP_OFFSET(INFO) \
932
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
933
0
   ? 0x0 : 0x7ff0)
934
935
/* The maximum size of the GOT for it to be addressable using 16-bit
936
   offsets from $gp.  */
937
0
#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
938
939
/* Instructions which appear in a stub.  */
940
#define STUB_LW(abfd)             \
941
  ((ABI_64_P (abfd)             \
942
    ? 0xdf998010        /* ld t9,0x8010(gp) */  \
943
    : 0x8f998010))        /* lw t9,0x8010(gp) */
944
#define STUB_MOVE 0x03e07825      /* or t7,ra,zero */
945
#define STUB_LUI(VAL) (0x3c180000 + (VAL))  /* lui t8,VAL */
946
#define STUB_JALR 0x0320f809      /* jalr ra,t9 */
947
#define STUB_JALRC 0xf8190000     /* jalrc ra,t9 */
948
#define STUB_ORI(VAL) (0x37180000 + (VAL))  /* ori t8,t8,VAL */
949
#define STUB_LI16U(VAL) (0x34180000 + (VAL))  /* ori t8,zero,VAL unsigned */
950
#define STUB_LI16S(abfd, VAL)           \
951
   ((ABI_64_P (abfd)              \
952
    ? (0x64180000 + (VAL))  /* daddiu t8,zero,VAL sign extended */  \
953
    : (0x24180000 + (VAL))))  /* addiu t8,zero,VAL sign extended */
954
955
/* Likewise for the microMIPS ASE.  */
956
#define STUB_LW_MICROMIPS(abfd)           \
957
0
  (ABI_64_P (abfd)             \
958
0
   ? 0xdf3c8010          /* ld t9,0x8010(gp) */  \
959
0
   : 0xff3c8010)        /* lw t9,0x8010(gp) */
960
#define STUB_MOVE_MICROMIPS 0x0dff    /* move t7,ra */
961
0
#define STUB_MOVE32_MICROMIPS 0x001f7a90  /* or t7,ra,zero */
962
#define STUB_LUI_MICROMIPS(VAL)           \
963
0
   (0x41b80000 + (VAL))        /* lui t8,VAL */
964
#define STUB_JALR_MICROMIPS 0x45d9    /* jalr t9 */
965
0
#define STUB_JALR32_MICROMIPS 0x03f90f3c  /* jalr ra,t9 */
966
#define STUB_ORI_MICROMIPS(VAL)           \
967
0
  (0x53180000 + (VAL))        /* ori t8,t8,VAL */
968
#define STUB_LI16U_MICROMIPS(VAL)         \
969
0
  (0x53000000 + (VAL))        /* ori t8,zero,VAL unsigned */
970
#define STUB_LI16S_MICROMIPS(abfd, VAL)         \
971
0
   (ABI_64_P (abfd)             \
972
0
    ? 0x5f000000 + (VAL)  /* daddiu t8,zero,VAL sign extended */  \
973
0
    : 0x33000000 + (VAL))  /* addiu t8,zero,VAL sign extended */
974
975
0
#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
976
0
#define MIPS_FUNCTION_STUB_BIG_SIZE 20
977
0
#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
978
0
#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
979
0
#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
980
0
#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
981
982
/* The name of the dynamic interpreter.  This is put in the .interp
983
   section.  */
984
985
#define ELF_DYNAMIC_INTERPRETER(abfd)   \
986
0
   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"  \
987
0
    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
988
0
    : "/usr/lib/libc.so.1")
989
990
#ifdef BFD64
991
#define MNAME(bfd,pre,pos) \
992
  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
993
#define ELF_R_SYM(bfd, i)         \
994
0
  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
995
#define ELF_R_TYPE(bfd, i)          \
996
0
  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
997
#define ELF_R_INFO(bfd, s, t)         \
998
0
  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
999
#else
1000
#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1001
#define ELF_R_SYM(bfd, i)         \
1002
  (ELF32_R_SYM (i))
1003
#define ELF_R_TYPE(bfd, i)          \
1004
  (ELF32_R_TYPE (i))
1005
#define ELF_R_INFO(bfd, s, t)         \
1006
  (ELF32_R_INFO (s, t))
1007
#endif
1008

1009
  /* The mips16 compiler uses a couple of special sections to handle
1010
     floating point arguments.
1011
1012
     Section names that look like .mips16.fn.FNNAME contain stubs that
1013
     copy floating point arguments from the fp regs to the gp regs and
1014
     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1015
     call should be redirected to the stub instead.  If no 32 bit
1016
     function calls FNNAME, the stub should be discarded.  We need to
1017
     consider any reference to the function, not just a call, because
1018
     if the address of the function is taken we will need the stub,
1019
     since the address might be passed to a 32 bit function.
1020
1021
     Section names that look like .mips16.call.FNNAME contain stubs
1022
     that copy floating point arguments from the gp regs to the fp
1023
     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1024
     then any 16 bit function that calls FNNAME should be redirected
1025
     to the stub instead.  If FNNAME is not a 32 bit function, the
1026
     stub should be discarded.
1027
1028
     .mips16.call.fp.FNNAME sections are similar, but contain stubs
1029
     which call FNNAME and then copy the return value from the fp regs
1030
     to the gp regs.  These stubs store the return value in $18 while
1031
     calling FNNAME; any function which might call one of these stubs
1032
     must arrange to save $18 around the call.  (This case is not
1033
     needed for 32 bit functions that call 16 bit functions, because
1034
     16 bit functions always return floating point values in both
1035
     $f0/$f1 and $2/$3.)
1036
1037
     Note that in all cases FNNAME might be defined statically.
1038
     Therefore, FNNAME is not used literally.  Instead, the relocation
1039
     information will indicate which symbol the section is for.
1040
1041
     We record any stubs that we find in the symbol table.  */
1042
1043
0
#define FN_STUB ".mips16.fn."
1044
0
#define CALL_STUB ".mips16.call."
1045
0
#define CALL_FP_STUB ".mips16.call.fp."
1046
1047
0
#define FN_STUB_P(name) startswith (name, FN_STUB)
1048
0
#define CALL_STUB_P(name) startswith (name, CALL_STUB)
1049
0
#define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1050

1051
/* The format of the first PLT entry in an O32 executable.  */
1052
static const bfd_vma mips_o32_exec_plt0_entry[] =
1053
{
1054
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1055
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1056
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1057
  0x031cc023, /* subu $24, $24, $28         */
1058
  0x03e07825, /* or t7, ra, zero          */
1059
  0x0018c082, /* srl $24, $24, 2          */
1060
  0x0320f809, /* jalr $25           */
1061
  0x2718fffe  /* subu $24, $24, 2         */
1062
};
1063
1064
/* The format of the first PLT entry in an O32 executable using compact
1065
   jumps.  */
1066
static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1067
{
1068
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1069
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1070
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1071
  0x031cc023, /* subu $24, $24, $28         */
1072
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1073
  0x0018c082, /* srl $24, $24, 2          */
1074
  0x2718fffe, /* subu $24, $24, 2         */
1075
  0xf8190000  /* jalrc $25            */
1076
};
1077
1078
/* The format of the first PLT entry in an N32 executable.  Different
1079
   because gp ($28) is not available; we use t2 ($14) instead.  */
1080
static const bfd_vma mips_n32_exec_plt0_entry[] =
1081
{
1082
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1083
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1084
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1085
  0x030ec023, /* subu $24, $24, $14         */
1086
  0x03e07825, /* or t7, ra, zero          */
1087
  0x0018c082, /* srl $24, $24, 2          */
1088
  0x0320f809, /* jalr $25           */
1089
  0x2718fffe  /* subu $24, $24, 2         */
1090
};
1091
1092
/* The format of the first PLT entry in an N32 executable using compact
1093
   jumps.  Different because gp ($28) is not available; we use t2 ($14)
1094
   instead.  */
1095
static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1096
{
1097
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1098
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1099
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1100
  0x030ec023, /* subu $24, $24, $14         */
1101
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1102
  0x0018c082, /* srl $24, $24, 2          */
1103
  0x2718fffe, /* subu $24, $24, 2         */
1104
  0xf8190000  /* jalrc $25            */
1105
};
1106
1107
/* The format of the first PLT entry in an N64 executable.  Different
1108
   from N32 because of the increased size of GOT entries.  */
1109
static const bfd_vma mips_n64_exec_plt0_entry[] =
1110
{
1111
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1112
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1113
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1114
  0x030ec023, /* subu $24, $24, $14         */
1115
  0x03e07825, /* or t7, ra, zero          */
1116
  0x0018c0c2, /* srl $24, $24, 3          */
1117
  0x0320f809, /* jalr $25           */
1118
  0x2718fffe  /* subu $24, $24, 2         */
1119
};
1120
1121
/* The format of the first PLT entry in an N64 executable using compact
1122
   jumps.  Different from N32 because of the increased size of GOT
1123
   entries.  */
1124
static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1125
{
1126
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1127
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1128
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1129
  0x030ec023, /* subu $24, $24, $14         */
1130
  0x03e0782d, /* move $15, $31  # 64-bit move (daddu)   */
1131
  0x0018c0c2, /* srl $24, $24, 3          */
1132
  0x2718fffe, /* subu $24, $24, 2         */
1133
  0xf8190000  /* jalrc $25            */
1134
};
1135
1136
1137
/* The format of the microMIPS first PLT entry in an O32 executable.
1138
   We rely on v0 ($2) rather than t8 ($24) to contain the address
1139
   of the GOTPLT entry handled, so this stub may only be used when
1140
   all the subsequent PLT entries are microMIPS code too.
1141
1142
   The trailing NOP is for alignment and correct disassembly only.  */
1143
static const bfd_vma micromips_o32_exec_plt0_entry[] =
1144
{
1145
  0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - .     */
1146
  0xff23, 0x0000, /* lw $25, 0($3)        */
1147
  0x0535,   /* subu $2, $2, $3        */
1148
  0x2525,   /* srl $2, $2, 2        */
1149
  0x3302, 0xfffe, /* subu $24, $2, 2        */
1150
  0x0dff,   /* move $15, $31        */
1151
  0x45f9,   /* jalrs $25          */
1152
  0x0f83,   /* move $28, $3         */
1153
  0x0c00    /* nop            */
1154
};
1155
1156
/* The format of the microMIPS first PLT entry in an O32 executable
1157
   in the insn32 mode.  */
1158
static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1159
{
1160
  0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0])     */
1161
  0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28)     */
1162
  0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0])    */
1163
  0x0398, 0xc1d0, /* subu $24, $24, $28       */
1164
  0x001f, 0x7a90, /* or $15, $31, zero        */
1165
  0x0318, 0x1040, /* srl $24, $24, 2        */
1166
  0x03f9, 0x0f3c, /* jalr $25         */
1167
  0x3318, 0xfffe  /* subu $24, $24, 2       */
1168
};
1169
1170
/* The format of subsequent standard PLT entries.  */
1171
static const bfd_vma mips_exec_plt_entry[] =
1172
{
1173
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1174
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1175
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1176
  0x03200008  /* jr $25         */
1177
};
1178
1179
static const bfd_vma mipsr6_exec_plt_entry[] =
1180
{
1181
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1182
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1183
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1184
  0x03200009  /* jr $25         */
1185
};
1186
1187
static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1188
{
1189
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1190
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1191
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1192
  0xd8190000  /* jic $25, 0         */
1193
};
1194
1195
/* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
1196
   and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1197
   directly addressable.  */
1198
static const bfd_vma mips16_o32_exec_plt_entry[] =
1199
{
1200
  0xb203,   /* lw $2, 12($pc)     */
1201
  0x9a60,   /* lw $3, 0($2)       */
1202
  0x651a,   /* move $24, $2       */
1203
  0xeb00,   /* jr $3        */
1204
  0x653b,   /* move $25, $3       */
1205
  0x6500,   /* nop          */
1206
  0x0000, 0x0000  /* .word (.got.plt entry)   */
1207
};
1208
1209
/* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
1210
   as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
1211
static const bfd_vma micromips_o32_exec_plt_entry[] =
1212
{
1213
  0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1214
  0xff22, 0x0000, /* lw $25, 0($2)      */
1215
  0x4599,   /* jr $25       */
1216
  0x0f02    /* move $24, $2       */
1217
};
1218
1219
/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
1220
static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1221
{
1222
  0x41af, 0x0000, /* lui $15, %hi(.got.plt entry)   */
1223
  0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1224
  0x0019, 0x0f3c, /* jr $25       */
1225
  0x330f, 0x0000  /* addiu $24, $15, %lo(.got.plt entry)  */
1226
};
1227
1228
/* The format of the first PLT entry in a VxWorks executable.  */
1229
static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1230
{
1231
  0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)   */
1232
  0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1233
  0x8f390008, /* lw t9, 8(t9)         */
1234
  0x00000000, /* nop            */
1235
  0x03200008, /* jr t9          */
1236
  0x00000000  /* nop            */
1237
};
1238
1239
/* The format of subsequent PLT entries.  */
1240
static const bfd_vma mips_vxworks_exec_plt_entry[] =
1241
{
1242
  0x10000000, /* b .PLT_resolver      */
1243
  0x24180000, /* li t8, <pltindex>      */
1244
  0x3c190000, /* lui t9, %hi(<.got.plt slot>)   */
1245
  0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1246
  0x8f390000, /* lw t9, 0(t9)       */
1247
  0x00000000, /* nop          */
1248
  0x03200008, /* jr t9        */
1249
  0x00000000  /* nop          */
1250
};
1251
1252
/* The format of the first PLT entry in a VxWorks shared object.  */
1253
static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1254
{
1255
  0x8f990008, /* lw t9, 8(gp)   */
1256
  0x00000000, /* nop      */
1257
  0x03200008, /* jr t9    */
1258
  0x00000000, /* nop      */
1259
  0x00000000, /* nop      */
1260
  0x00000000  /* nop      */
1261
};
1262
1263
/* The format of subsequent PLT entries.  */
1264
static const bfd_vma mips_vxworks_shared_plt_entry[] =
1265
{
1266
  0x10000000, /* b .PLT_resolver  */
1267
  0x24180000  /* li t8, <pltindex>  */
1268
};
1269

1270
/* microMIPS 32-bit opcode helper installer.  */
1271
1272
static void
1273
bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1274
0
{
1275
0
  bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1276
0
  bfd_put_16 (abfd,  opcode    & 0xffff, ptr + 2);
1277
0
}
1278
1279
/* microMIPS 32-bit opcode helper retriever.  */
1280
1281
static bfd_vma
1282
bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1283
0
{
1284
0
  return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1285
0
}
1286

1287
/* Look up an entry in a MIPS ELF linker hash table.  */
1288
1289
#define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
1290
0
  ((struct mips_elf_link_hash_entry *)          \
1291
0
   elf_link_hash_lookup (&(table)->root, (string), (create),    \
1292
0
       (copy), (follow)))
1293
1294
/* Traverse a MIPS ELF linker hash table.  */
1295
1296
#define mips_elf_link_hash_traverse(table, func, info)      \
1297
0
  (elf_link_hash_traverse           \
1298
0
   (&(table)->root,              \
1299
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
1300
0
    (info)))
1301
1302
/* Find the base offsets for thread-local storage in this object,
1303
   for GD/LD and IE/LE respectively.  */
1304
1305
0
#define TP_OFFSET 0x7000
1306
0
#define DTP_OFFSET 0x8000
1307
1308
static bfd_vma
1309
dtprel_base (struct bfd_link_info *info)
1310
0
{
1311
  /* If tls_sec is NULL, we should have signalled an error already.  */
1312
0
  if (elf_hash_table (info)->tls_sec == NULL)
1313
0
    return 0;
1314
0
  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1315
0
}
1316
1317
static bfd_vma
1318
tprel_base (struct bfd_link_info *info)
1319
0
{
1320
  /* If tls_sec is NULL, we should have signalled an error already.  */
1321
0
  if (elf_hash_table (info)->tls_sec == NULL)
1322
0
    return 0;
1323
0
  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1324
0
}
1325
1326
/* Create an entry in a MIPS ELF linker hash table.  */
1327
1328
static struct bfd_hash_entry *
1329
mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1330
          struct bfd_hash_table *table, const char *string)
1331
0
{
1332
0
  struct mips_elf_link_hash_entry *ret =
1333
0
    (struct mips_elf_link_hash_entry *) entry;
1334
1335
  /* Allocate the structure if it has not already been allocated by a
1336
     subclass.  */
1337
0
  if (ret == NULL)
1338
0
    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1339
0
  if (ret == NULL)
1340
0
    return (struct bfd_hash_entry *) ret;
1341
1342
  /* Call the allocation method of the superclass.  */
1343
0
  ret = ((struct mips_elf_link_hash_entry *)
1344
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1345
0
             table, string));
1346
0
  if (ret != NULL)
1347
0
    {
1348
      /* Set local fields.  */
1349
0
      memset (&ret->esym, 0, sizeof (EXTR));
1350
      /* We use -2 as a marker to indicate that the information has
1351
   not been set.  -1 means there is no associated ifd.  */
1352
0
      ret->esym.ifd = -2;
1353
0
      ret->la25_stub = 0;
1354
0
      ret->possibly_dynamic_relocs = 0;
1355
0
      ret->fn_stub = NULL;
1356
0
      ret->call_stub = NULL;
1357
0
      ret->call_fp_stub = NULL;
1358
0
      ret->mipsxhash_loc = 0;
1359
0
      ret->global_got_area = GGA_NONE;
1360
0
      ret->got_only_for_calls = true;
1361
0
      ret->readonly_reloc = false;
1362
0
      ret->has_static_relocs = false;
1363
0
      ret->no_fn_stub = false;
1364
0
      ret->need_fn_stub = false;
1365
0
      ret->has_nonpic_branches = false;
1366
0
      ret->needs_lazy_stub = false;
1367
0
      ret->use_plt_entry = false;
1368
0
    }
1369
1370
0
  return (struct bfd_hash_entry *) ret;
1371
0
}
1372
1373
/* Allocate MIPS ELF private object data.  */
1374
1375
bool
1376
_bfd_mips_elf_mkobject (bfd *abfd)
1377
393k
{
1378
393k
  return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata));
1379
393k
}
1380
1381
/* MIPS ELF uses a special find_nearest_line routine in order the
1382
   handle the ECOFF debugging information.  */
1383
1384
struct mips_elf_find_line
1385
{
1386
  struct ecoff_debug_info d;
1387
  struct ecoff_find_line i;
1388
};
1389
1390
bool
1391
_bfd_mips_elf_free_cached_info (bfd *abfd)
1392
827k
{
1393
827k
  struct mips_elf_obj_tdata *tdata;
1394
1395
827k
  if ((bfd_get_format (abfd) == bfd_object
1396
823k
       || bfd_get_format (abfd) == bfd_core)
1397
5.57k
      && (tdata = mips_elf_tdata (abfd)) != NULL)
1398
5.57k
    {
1399
5.57k
      BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1400
52.9k
      for (asection *sec = abfd->sections; sec; sec = sec->next)
1401
47.3k
  mips_elf_free_hi16_list (abfd, sec, false, NULL);
1402
5.57k
      if (tdata->find_line_info != NULL)
1403
160
  _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1404
5.57k
    }
1405
827k
  return _bfd_elf_free_cached_info (abfd);
1406
827k
}
1407
1408
bool
1409
_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1410
206k
{
1411
206k
  struct _mips_elf_section_data *sdata;
1412
1413
206k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
1414
206k
  if (sdata == NULL)
1415
0
    return false;
1416
206k
  sec->used_by_bfd = sdata;
1417
1418
206k
  return _bfd_elf_new_section_hook (abfd, sec);
1419
206k
}
1420

1421
/* Read ECOFF debugging information from a .mdebug section into a
1422
   ecoff_debug_info structure.  */
1423
1424
bool
1425
_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1426
             struct ecoff_debug_info *debug)
1427
2.57k
{
1428
2.57k
  HDRR *symhdr;
1429
2.57k
  const struct ecoff_debug_swap *swap;
1430
2.57k
  char *ext_hdr;
1431
1432
2.57k
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1433
2.57k
  memset (debug, 0, sizeof (*debug));
1434
1435
2.57k
  ext_hdr = bfd_malloc (swap->external_hdr_size);
1436
2.57k
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1437
0
    goto error_return;
1438
1439
2.57k
  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1440
2.57k
          swap->external_hdr_size))
1441
195
    goto error_return;
1442
1443
2.37k
  symhdr = &debug->symbolic_header;
1444
2.37k
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1445
2.37k
  free (ext_hdr);
1446
2.37k
  ext_hdr = NULL;
1447
1448
  /* The symbolic header contains absolute file offsets and sizes to
1449
     read.  */
1450
2.37k
#define READ(ptr, offset, count, size)          \
1451
13.6k
  do                  \
1452
13.6k
    {                 \
1453
13.6k
      size_t amt;             \
1454
13.6k
      debug->ptr = NULL;            \
1455
13.6k
      if (symhdr->count == 0)           \
1456
13.6k
  break;               \
1457
13.6k
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
1458
4.66k
  {               \
1459
0
    bfd_set_error (bfd_error_file_too_big);     \
1460
0
    goto error_return;            \
1461
0
  }                \
1462
4.66k
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)   \
1463
4.66k
  goto error_return;           \
1464
4.66k
      debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);   \
1465
3.69k
      if (debug->ptr == NULL)           \
1466
3.69k
  goto error_return;           \
1467
3.69k
      ((char *) debug->ptr)[amt] = 0;         \
1468
2.45k
    } while (0)
1469
1470
2.37k
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1471
1.90k
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1472
1.71k
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1473
1.53k
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1474
1.34k
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1475
1.19k
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1476
1.06k
  READ (ss, cbSsOffset, issMax, sizeof (char));
1477
846
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1478
747
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1479
530
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1480
368
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1481
160
#undef READ
1482
1483
160
  return true;
1484
1485
2.41k
 error_return:
1486
2.41k
  free (ext_hdr);
1487
2.41k
  _bfd_ecoff_free_ecoff_debug_info (debug);
1488
2.41k
  return false;
1489
368
}
1490

1491
/* Swap RPDR (runtime procedure table entry) for output.  */
1492
1493
static void
1494
ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1495
0
{
1496
0
  H_PUT_S32 (abfd, in->adr, ex->p_adr);
1497
0
  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1498
0
  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1499
0
  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1500
0
  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1501
0
  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1502
1503
0
  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1504
0
  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1505
1506
0
  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1507
0
}
1508
1509
/* Create a runtime procedure table from the .mdebug section.  */
1510
1511
static bool
1512
mips_elf_create_procedure_table (void *handle, bfd *abfd,
1513
         struct bfd_link_info *info, asection *s,
1514
         struct ecoff_debug_info *debug)
1515
0
{
1516
0
  const struct ecoff_debug_swap *swap;
1517
0
  HDRR *hdr = &debug->symbolic_header;
1518
0
  RPDR *rpdr, *rp;
1519
0
  struct rpdr_ext *erp;
1520
0
  void *rtproc;
1521
0
  struct pdr_ext *epdr;
1522
0
  struct sym_ext *esym;
1523
0
  char *ss, **sv;
1524
0
  char *str;
1525
0
  bfd_size_type size;
1526
0
  bfd_size_type count;
1527
0
  unsigned long sindex;
1528
0
  unsigned long i;
1529
0
  PDR pdr;
1530
0
  SYMR sym;
1531
0
  const char *no_name_func = _("static procedure (no name)");
1532
1533
0
  epdr = NULL;
1534
0
  rpdr = NULL;
1535
0
  esym = NULL;
1536
0
  ss = NULL;
1537
0
  sv = NULL;
1538
1539
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1540
1541
0
  sindex = strlen (no_name_func) + 1;
1542
0
  count = hdr->ipdMax;
1543
0
  if (count > 0)
1544
0
    {
1545
0
      size = swap->external_pdr_size;
1546
1547
0
      epdr = bfd_malloc (size * count);
1548
0
      if (epdr == NULL)
1549
0
  goto error_return;
1550
1551
0
      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1552
0
  goto error_return;
1553
1554
0
      size = sizeof (RPDR);
1555
0
      rp = rpdr = bfd_malloc (size * count);
1556
0
      if (rpdr == NULL)
1557
0
  goto error_return;
1558
1559
0
      size = sizeof (char *);
1560
0
      sv = bfd_malloc (size * count);
1561
0
      if (sv == NULL)
1562
0
  goto error_return;
1563
1564
0
      count = hdr->isymMax;
1565
0
      size = swap->external_sym_size;
1566
0
      esym = bfd_malloc (size * count);
1567
0
      if (esym == NULL)
1568
0
  goto error_return;
1569
1570
0
      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1571
0
  goto error_return;
1572
1573
0
      count = hdr->issMax;
1574
0
      ss = bfd_malloc (count);
1575
0
      if (ss == NULL)
1576
0
  goto error_return;
1577
0
      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1578
0
  goto error_return;
1579
1580
0
      count = hdr->ipdMax;
1581
0
      for (i = 0; i < (unsigned long) count; i++, rp++)
1582
0
  {
1583
0
    (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1584
0
    (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1585
0
    rp->adr = sym.value;
1586
0
    rp->regmask = pdr.regmask;
1587
0
    rp->regoffset = pdr.regoffset;
1588
0
    rp->fregmask = pdr.fregmask;
1589
0
    rp->fregoffset = pdr.fregoffset;
1590
0
    rp->frameoffset = pdr.frameoffset;
1591
0
    rp->framereg = pdr.framereg;
1592
0
    rp->pcreg = pdr.pcreg;
1593
0
    rp->irpss = sindex;
1594
0
    sv[i] = ss + sym.iss;
1595
0
    sindex += strlen (sv[i]) + 1;
1596
0
  }
1597
0
    }
1598
1599
0
  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1600
0
  size = BFD_ALIGN (size, 16);
1601
0
  rtproc = bfd_alloc (abfd, size);
1602
0
  if (rtproc == NULL)
1603
0
    {
1604
0
      mips_elf_hash_table (info)->procedure_count = 0;
1605
0
      goto error_return;
1606
0
    }
1607
1608
0
  mips_elf_hash_table (info)->procedure_count = count + 2;
1609
1610
0
  erp = rtproc;
1611
0
  memset (erp, 0, sizeof (struct rpdr_ext));
1612
0
  erp++;
1613
0
  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1614
0
  strcpy (str, no_name_func);
1615
0
  str += strlen (no_name_func) + 1;
1616
0
  for (i = 0; i < count; i++)
1617
0
    {
1618
0
      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1619
0
      strcpy (str, sv[i]);
1620
0
      str += strlen (sv[i]) + 1;
1621
0
    }
1622
0
  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1623
1624
  /* Set the size and contents of .rtproc section.  */
1625
0
  s->size = size;
1626
0
  s->contents = rtproc;
1627
1628
  /* Skip this section later on (I don't think this currently
1629
     matters, but someday it might).  */
1630
0
  s->map_head.link_order = NULL;
1631
1632
0
  free (epdr);
1633
0
  free (rpdr);
1634
0
  free (esym);
1635
0
  free (ss);
1636
0
  free (sv);
1637
0
  return true;
1638
1639
0
 error_return:
1640
0
  free (epdr);
1641
0
  free (rpdr);
1642
0
  free (esym);
1643
0
  free (ss);
1644
0
  free (sv);
1645
0
  return false;
1646
0
}
1647

1648
/* We're going to create a stub for H.  Create a symbol for the stub's
1649
   value and size, to help make the disassembly easier to read.  */
1650
1651
static bool
1652
mips_elf_create_stub_symbol (struct bfd_link_info *info,
1653
           struct mips_elf_link_hash_entry *h,
1654
           const char *prefix, asection *s, bfd_vma value,
1655
           bfd_vma size)
1656
0
{
1657
0
  bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1658
0
  struct bfd_link_hash_entry *bh;
1659
0
  struct elf_link_hash_entry *elfh;
1660
0
  char *name;
1661
0
  bool res;
1662
1663
0
  if (micromips_p)
1664
0
    value |= 1;
1665
1666
  /* Create a new symbol.  */
1667
0
  name = concat (prefix, h->root.root.root.string, NULL);
1668
0
  bh = NULL;
1669
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1670
0
            BSF_LOCAL, s, value, NULL,
1671
0
            true, false, &bh);
1672
0
  free (name);
1673
0
  if (! res)
1674
0
    return false;
1675
1676
  /* Make it a local function.  */
1677
0
  elfh = (struct elf_link_hash_entry *) bh;
1678
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1679
0
  elfh->size = size;
1680
0
  elfh->forced_local = 1;
1681
0
  if (micromips_p)
1682
0
    elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1683
0
  return true;
1684
0
}
1685
1686
/* We're about to redefine H.  Create a symbol to represent H's
1687
   current value and size, to help make the disassembly easier
1688
   to read.  */
1689
1690
static bool
1691
mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1692
             struct mips_elf_link_hash_entry *h,
1693
             const char *prefix)
1694
0
{
1695
0
  struct bfd_link_hash_entry *bh;
1696
0
  struct elf_link_hash_entry *elfh;
1697
0
  char *name;
1698
0
  asection *s;
1699
0
  bfd_vma value;
1700
0
  bool res;
1701
1702
  /* Read the symbol's value.  */
1703
0
  BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1704
0
        || h->root.root.type == bfd_link_hash_defweak);
1705
0
  s = h->root.root.u.def.section;
1706
0
  value = h->root.root.u.def.value;
1707
1708
  /* Create a new symbol.  */
1709
0
  name = concat (prefix, h->root.root.root.string, NULL);
1710
0
  bh = NULL;
1711
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1712
0
            BSF_LOCAL, s, value, NULL,
1713
0
            true, false, &bh);
1714
0
  free (name);
1715
0
  if (! res)
1716
0
    return false;
1717
1718
  /* Make it local and copy the other attributes from H.  */
1719
0
  elfh = (struct elf_link_hash_entry *) bh;
1720
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1721
0
  elfh->other = h->root.other;
1722
0
  elfh->size = h->root.size;
1723
0
  elfh->forced_local = 1;
1724
0
  return true;
1725
0
}
1726
1727
/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1728
   function rather than to a hard-float stub.  */
1729
1730
static bool
1731
section_allows_mips16_refs_p (asection *section)
1732
0
{
1733
0
  const char *name;
1734
1735
0
  name = bfd_section_name (section);
1736
0
  return (FN_STUB_P (name)
1737
0
    || CALL_STUB_P (name)
1738
0
    || CALL_FP_STUB_P (name)
1739
0
    || strcmp (name, ".pdr") == 0);
1740
0
}
1741
1742
/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1743
   stub section of some kind.  Return the R_SYMNDX of the target
1744
   function, or 0 if we can't decide which function that is.  */
1745
1746
static unsigned long
1747
mips16_stub_symndx (elf_backend_data *bed,
1748
        asection *sec ATTRIBUTE_UNUSED,
1749
        const Elf_Internal_Rela *relocs,
1750
        const Elf_Internal_Rela *relend)
1751
0
{
1752
0
  int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1753
0
  const Elf_Internal_Rela *rel;
1754
1755
  /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1756
     one in a compound relocation.  */
1757
0
  for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1758
0
    if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1759
0
      return ELF_R_SYM (sec->owner, rel->r_info);
1760
1761
  /* Otherwise trust the first relocation, whatever its kind.  This is
1762
     the traditional behavior.  */
1763
0
  if (relocs < relend)
1764
0
    return ELF_R_SYM (sec->owner, relocs->r_info);
1765
1766
0
  return 0;
1767
0
}
1768
1769
/* Check the mips16 stubs for a particular symbol, and see if we can
1770
   discard them.  */
1771
1772
static void
1773
mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1774
           struct mips_elf_link_hash_entry *h)
1775
0
{
1776
  /* Dynamic symbols must use the standard call interface, in case other
1777
     objects try to call them.  */
1778
0
  if (h->fn_stub != NULL
1779
0
      && h->root.dynindx != -1)
1780
0
    {
1781
0
      mips_elf_create_shadow_symbol (info, h, ".mips16.");
1782
0
      h->need_fn_stub = true;
1783
0
    }
1784
1785
0
  if (h->fn_stub != NULL
1786
0
      && ! h->need_fn_stub)
1787
0
    {
1788
      /* We don't need the fn_stub; the only references to this symbol
1789
   are 16 bit calls.  Clobber the size to 0 to prevent it from
1790
   being included in the link.  */
1791
0
      h->fn_stub->size = 0;
1792
0
      h->fn_stub->flags &= ~SEC_RELOC;
1793
0
      h->fn_stub->reloc_count = 0;
1794
0
      h->fn_stub->flags |= SEC_EXCLUDE;
1795
0
      h->fn_stub->output_section = bfd_abs_section_ptr;
1796
0
    }
1797
1798
0
  if (h->call_stub != NULL
1799
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1800
0
    {
1801
      /* We don't need the call_stub; this is a 16 bit function, so
1802
   calls from other 16 bit functions are OK.  Clobber the size
1803
   to 0 to prevent it from being included in the link.  */
1804
0
      h->call_stub->size = 0;
1805
0
      h->call_stub->flags &= ~SEC_RELOC;
1806
0
      h->call_stub->reloc_count = 0;
1807
0
      h->call_stub->flags |= SEC_EXCLUDE;
1808
0
      h->call_stub->output_section = bfd_abs_section_ptr;
1809
0
    }
1810
1811
0
  if (h->call_fp_stub != NULL
1812
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1813
0
    {
1814
      /* We don't need the call_stub; this is a 16 bit function, so
1815
   calls from other 16 bit functions are OK.  Clobber the size
1816
   to 0 to prevent it from being included in the link.  */
1817
0
      h->call_fp_stub->size = 0;
1818
0
      h->call_fp_stub->flags &= ~SEC_RELOC;
1819
0
      h->call_fp_stub->reloc_count = 0;
1820
0
      h->call_fp_stub->flags |= SEC_EXCLUDE;
1821
0
      h->call_fp_stub->output_section = bfd_abs_section_ptr;
1822
0
    }
1823
0
}
1824
1825
/* Hashtable callbacks for mips_elf_la25_stubs.  */
1826
1827
static hashval_t
1828
mips_elf_la25_stub_hash (const void *entry_)
1829
0
{
1830
0
  const struct mips_elf_la25_stub *entry;
1831
1832
0
  entry = (struct mips_elf_la25_stub *) entry_;
1833
0
  return entry->h->root.root.u.def.section->id
1834
0
    + entry->h->root.root.u.def.value;
1835
0
}
1836
1837
static int
1838
mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1839
0
{
1840
0
  const struct mips_elf_la25_stub *entry1, *entry2;
1841
1842
0
  entry1 = (struct mips_elf_la25_stub *) entry1_;
1843
0
  entry2 = (struct mips_elf_la25_stub *) entry2_;
1844
0
  return ((entry1->h->root.root.u.def.section
1845
0
     == entry2->h->root.root.u.def.section)
1846
0
    && (entry1->h->root.root.u.def.value
1847
0
        == entry2->h->root.root.u.def.value));
1848
0
}
1849
1850
/* Called by the linker to set up the la25 stub-creation code.  FN is
1851
   the linker's implementation of add_stub_function.  Return true on
1852
   success.  */
1853
1854
bool
1855
_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1856
        asection *(*fn) (const char *, asection *,
1857
             asection *))
1858
0
{
1859
0
  struct mips_elf_link_hash_table *htab;
1860
1861
0
  htab = mips_elf_hash_table (info);
1862
0
  if (htab == NULL)
1863
0
    return false;
1864
1865
0
  htab->add_stub_section = fn;
1866
0
  htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1867
0
              mips_elf_la25_stub_eq, NULL);
1868
0
  if (htab->la25_stubs == NULL)
1869
0
    return false;
1870
1871
0
  return true;
1872
0
}
1873
1874
/* Return true if H is a locally-defined PIC function, in the sense
1875
   that it or its fn_stub might need $25 to be valid on entry.
1876
   Note that MIPS16 functions set up $gp using PC-relative instructions,
1877
   so they themselves never need $25 to be valid.  Only non-MIPS16
1878
   entry points are of interest here.  */
1879
1880
static bool
1881
mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1882
0
{
1883
0
  return ((h->root.root.type == bfd_link_hash_defined
1884
0
     || h->root.root.type == bfd_link_hash_defweak)
1885
0
    && h->root.def_regular
1886
0
    && !bfd_is_abs_section (h->root.root.u.def.section)
1887
0
    && !bfd_is_und_section (h->root.root.u.def.section)
1888
0
    && (!ELF_ST_IS_MIPS16 (h->root.other)
1889
0
        || (h->fn_stub && h->need_fn_stub))
1890
0
    && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1891
0
        || ELF_ST_IS_MIPS_PIC (h->root.other)));
1892
0
}
1893
1894
/* Set *SEC to the input section that contains the target of STUB.
1895
   Return the offset of the target from the start of that section.  */
1896
1897
static bfd_vma
1898
mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1899
        asection **sec)
1900
0
{
1901
0
  if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1902
0
    {
1903
0
      BFD_ASSERT (stub->h->need_fn_stub);
1904
0
      *sec = stub->h->fn_stub;
1905
0
      return 0;
1906
0
    }
1907
0
  else
1908
0
    {
1909
0
      *sec = stub->h->root.root.u.def.section;
1910
0
      return stub->h->root.root.u.def.value;
1911
0
    }
1912
0
}
1913
1914
/* STUB describes an la25 stub that we have decided to implement
1915
   by inserting an LUI/ADDIU pair before the target function.
1916
   Create the section and redirect the function symbol to it.  */
1917
1918
static bool
1919
mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1920
       struct bfd_link_info *info)
1921
0
{
1922
0
  struct mips_elf_link_hash_table *htab;
1923
0
  char *name;
1924
0
  asection *s, *input_section;
1925
0
  unsigned int align;
1926
1927
0
  htab = mips_elf_hash_table (info);
1928
0
  if (htab == NULL)
1929
0
    return false;
1930
1931
  /* Create a unique name for the new section.  */
1932
0
  name = bfd_malloc (11 + sizeof (".text.stub."));
1933
0
  if (name == NULL)
1934
0
    return false;
1935
0
  sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1936
1937
  /* Create the section.  */
1938
0
  mips_elf_get_la25_target (stub, &input_section);
1939
0
  s = htab->add_stub_section (name, input_section,
1940
0
            input_section->output_section);
1941
0
  if (s == NULL)
1942
0
    return false;
1943
1944
  /* Make sure that any padding goes before the stub.  */
1945
0
  align = input_section->alignment_power;
1946
0
  if (!bfd_link_align_section (s, align))
1947
0
    return false;
1948
0
  if (align > 3)
1949
0
    s->size = (1 << align) - 8;
1950
1951
  /* Create a symbol for the stub.  */
1952
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1953
0
  stub->stub_section = s;
1954
0
  stub->offset = s->size;
1955
1956
  /* Allocate room for it.  */
1957
0
  s->size += 8;
1958
0
  return true;
1959
0
}
1960
1961
/* STUB describes an la25 stub that we have decided to implement
1962
   with a separate trampoline.  Allocate room for it and redirect
1963
   the function symbol to it.  */
1964
1965
static bool
1966
mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1967
            struct bfd_link_info *info)
1968
0
{
1969
0
  struct mips_elf_link_hash_table *htab;
1970
0
  asection *s;
1971
1972
0
  htab = mips_elf_hash_table (info);
1973
0
  if (htab == NULL)
1974
0
    return false;
1975
1976
  /* Create a trampoline section, if we haven't already.  */
1977
0
  s = htab->strampoline;
1978
0
  if (s == NULL)
1979
0
    {
1980
0
      asection *input_section = stub->h->root.root.u.def.section;
1981
0
      s = htab->add_stub_section (".text", NULL,
1982
0
          input_section->output_section);
1983
0
      if (s == NULL || !bfd_link_align_section (s, 4))
1984
0
  return false;
1985
0
      htab->strampoline = s;
1986
0
    }
1987
1988
  /* Create a symbol for the stub.  */
1989
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1990
0
  stub->stub_section = s;
1991
0
  stub->offset = s->size;
1992
1993
  /* Allocate room for it.  */
1994
0
  s->size += 16;
1995
0
  return true;
1996
0
}
1997
1998
/* H describes a symbol that needs an la25 stub.  Make sure that an
1999
   appropriate stub exists and point H at it.  */
2000
2001
static bool
2002
mips_elf_add_la25_stub (struct bfd_link_info *info,
2003
      struct mips_elf_link_hash_entry *h)
2004
0
{
2005
0
  struct mips_elf_link_hash_table *htab;
2006
0
  struct mips_elf_la25_stub search, *stub;
2007
0
  bool use_trampoline_p;
2008
0
  asection *s;
2009
0
  bfd_vma value;
2010
0
  void **slot;
2011
2012
  /* Describe the stub we want.  */
2013
0
  search.stub_section = NULL;
2014
0
  search.offset = 0;
2015
0
  search.h = h;
2016
2017
  /* See if we've already created an equivalent stub.  */
2018
0
  htab = mips_elf_hash_table (info);
2019
0
  if (htab == NULL)
2020
0
    return false;
2021
2022
0
  slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2023
0
  if (slot == NULL)
2024
0
    return false;
2025
2026
0
  stub = (struct mips_elf_la25_stub *) *slot;
2027
0
  if (stub != NULL)
2028
0
    {
2029
      /* We can reuse the existing stub.  */
2030
0
      h->la25_stub = stub;
2031
0
      return true;
2032
0
    }
2033
2034
  /* Create a permanent copy of ENTRY and add it to the hash table.  */
2035
0
  stub = bfd_malloc (sizeof (search));
2036
0
  if (stub == NULL)
2037
0
    return false;
2038
0
  *stub = search;
2039
0
  *slot = stub;
2040
2041
  /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2042
     of the section and if we would need no more than 2 nops.  */
2043
0
  value = mips_elf_get_la25_target (stub, &s);
2044
0
  if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2045
0
    value &= ~1;
2046
0
  use_trampoline_p = (value != 0 || s->alignment_power > 4);
2047
2048
0
  h->la25_stub = stub;
2049
0
  return (use_trampoline_p
2050
0
    ? mips_elf_add_la25_trampoline (stub, info)
2051
0
    : mips_elf_add_la25_intro (stub, info));
2052
0
}
2053
2054
/* A mips_elf_link_hash_traverse callback that is called before sizing
2055
   sections.  DATA points to a mips_htab_traverse_info structure.  */
2056
2057
static bool
2058
mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2059
0
{
2060
0
  struct mips_htab_traverse_info *hti;
2061
2062
0
  hti = (struct mips_htab_traverse_info *) data;
2063
0
  if (!bfd_link_relocatable (hti->info))
2064
0
    mips_elf_check_mips16_stubs (hti->info, h);
2065
2066
0
  if (mips_elf_local_pic_function_p (h))
2067
0
    {
2068
      /* PR 12845: If H is in a section that has been garbage
2069
   collected it will have its output section set to *ABS*.  */
2070
0
      if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2071
0
  return true;
2072
2073
      /* H is a function that might need $25 to be valid on entry.
2074
   If we're creating a non-PIC relocatable object, mark H as
2075
   being PIC.  If we're creating a non-relocatable object with
2076
   non-PIC branches and jumps to H, make sure that H has an la25
2077
   stub.  */
2078
0
      if (bfd_link_relocatable (hti->info))
2079
0
  {
2080
0
    if (!PIC_OBJECT_P (hti->output_bfd))
2081
0
      h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2082
0
  }
2083
0
      else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2084
0
  {
2085
0
    hti->error = true;
2086
0
    return false;
2087
0
  }
2088
0
    }
2089
0
  return true;
2090
0
}
2091

2092
/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2093
   Most mips16 instructions are 16 bits, but these instructions
2094
   are 32 bits.
2095
2096
   The format of these instructions is:
2097
2098
   +--------------+--------------------------------+
2099
   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
2100
   +--------------+--------------------------------+
2101
   |        Immediate  15:0      |
2102
   +-----------------------------------------------+
2103
2104
   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
2105
   Note that the immediate value in the first word is swapped.
2106
2107
   When producing a relocatable object file, R_MIPS16_26 is
2108
   handled mostly like R_MIPS_26.  In particular, the addend is
2109
   stored as a straight 26-bit value in a 32-bit instruction.
2110
   (gas makes life simpler for itself by never adjusting a
2111
   R_MIPS16_26 reloc to be against a section, so the addend is
2112
   always zero).  However, the 32 bit instruction is stored as 2
2113
   16-bit values, rather than a single 32-bit value.  In a
2114
   big-endian file, the result is the same; in a little-endian
2115
   file, the two 16-bit halves of the 32 bit value are swapped.
2116
   This is so that a disassembler can recognize the jal
2117
   instruction.
2118
2119
   When doing a final link, R_MIPS16_26 is treated as a 32 bit
2120
   instruction stored as two 16-bit values.  The addend A is the
2121
   contents of the targ26 field.  The calculation is the same as
2122
   R_MIPS_26.  When storing the calculated value, reorder the
2123
   immediate value as shown above, and don't forget to store the
2124
   value as two 16-bit values.
2125
2126
   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2127
   defined as
2128
2129
   big-endian:
2130
   +--------+----------------------+
2131
   |      |        |
2132
   |      |  targ26-16     |
2133
   |31    26|25       0|
2134
   +--------+----------------------+
2135
2136
   little-endian:
2137
   +----------+------+-------------+
2138
   |        |      |       |
2139
   |  sub1    |      |     sub2    |
2140
   |0      9|10  15|16   31|
2141
   +----------+--------------------+
2142
   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2143
   ((sub1 << 16) | sub2)).
2144
2145
   When producing a relocatable object file, the calculation is
2146
   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2147
   When producing a fully linked file, the calculation is
2148
   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2149
   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2150
2151
   The table below lists the other MIPS16 instruction relocations.
2152
   Each one is calculated in the same way as the non-MIPS16 relocation
2153
   given on the right, but using the extended MIPS16 layout of 16-bit
2154
   immediate fields:
2155
2156
  R_MIPS16_GPREL    R_MIPS_GPREL16
2157
  R_MIPS16_GOT16    R_MIPS_GOT16
2158
  R_MIPS16_CALL16   R_MIPS_CALL16
2159
  R_MIPS16_HI16   R_MIPS_HI16
2160
  R_MIPS16_LO16   R_MIPS_LO16
2161
2162
   A typical instruction will have a format like this:
2163
2164
   +--------------+--------------------------------+
2165
   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
2166
   +--------------+--------------------------------+
2167
   |    Major     |   rx   |   ry   |   Imm  4:0   |
2168
   +--------------+--------------------------------+
2169
2170
   EXTEND is the five bit value 11110.  Major is the instruction
2171
   opcode.
2172
2173
   All we need to do here is shuffle the bits appropriately.
2174
   As above, the two 16-bit halves must be swapped on a
2175
   little-endian system.
2176
2177
   Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2178
   relocatable field is shifted by 1 rather than 2 and the same bit
2179
   shuffling is done as with the relocations above.  */
2180
2181
static inline bool
2182
mips16_reloc_p (int r_type)
2183
1.67k
{
2184
1.67k
  switch (r_type)
2185
1.67k
    {
2186
18
    case R_MIPS16_26:
2187
20
    case R_MIPS16_GPREL:
2188
36
    case R_MIPS16_GOT16:
2189
38
    case R_MIPS16_CALL16:
2190
40
    case R_MIPS16_HI16:
2191
62
    case R_MIPS16_LO16:
2192
70
    case R_MIPS16_TLS_GD:
2193
70
    case R_MIPS16_TLS_LDM:
2194
76
    case R_MIPS16_TLS_DTPREL_HI16:
2195
76
    case R_MIPS16_TLS_DTPREL_LO16:
2196
82
    case R_MIPS16_TLS_GOTTPREL:
2197
84
    case R_MIPS16_TLS_TPREL_HI16:
2198
102
    case R_MIPS16_TLS_TPREL_LO16:
2199
102
    case R_MIPS16_PC16_S1:
2200
102
      return true;
2201
2202
1.57k
    default:
2203
1.57k
      return false;
2204
1.67k
    }
2205
1.67k
}
2206
2207
/* Check if a microMIPS reloc.  */
2208
2209
static inline bool
2210
micromips_reloc_p (unsigned int r_type)
2211
1.72k
{
2212
1.72k
  return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2213
1.72k
}
2214
2215
/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2216
   on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
2217
   R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
2218
   16-bit instructions.  */
2219
2220
static inline bool
2221
micromips_reloc_shuffle_p (unsigned int r_type)
2222
1.57k
{
2223
1.57k
  return (micromips_reloc_p (r_type)
2224
85
    && r_type != R_MICROMIPS_PC7_S1
2225
75
    && r_type != R_MICROMIPS_PC10_S1
2226
75
    && r_type != R_MICROMIPS_GPREL7_S2);
2227
1.57k
}
2228
2229
static inline bool
2230
got16_reloc_p (int r_type)
2231
0
{
2232
0
  return (r_type == R_MIPS_GOT16
2233
0
    || r_type == R_MIPS16_GOT16
2234
0
    || r_type == R_MICROMIPS_GOT16);
2235
0
}
2236
2237
static inline bool
2238
call16_reloc_p (int r_type)
2239
0
{
2240
0
  return (r_type == R_MIPS_CALL16
2241
0
    || r_type == R_MIPS16_CALL16
2242
0
    || r_type == R_MICROMIPS_CALL16);
2243
0
}
2244
2245
static inline bool
2246
got_disp_reloc_p (unsigned int r_type)
2247
0
{
2248
0
  return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2249
0
}
2250
2251
static inline bool
2252
got_page_reloc_p (unsigned int r_type)
2253
0
{
2254
0
  return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2255
0
}
2256
2257
static inline bool
2258
got_lo16_reloc_p (unsigned int r_type)
2259
0
{
2260
0
  return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2261
0
}
2262
2263
static inline bool
2264
call_hi16_reloc_p (unsigned int r_type)
2265
0
{
2266
0
  return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2267
0
}
2268
2269
static inline bool
2270
call_lo16_reloc_p (unsigned int r_type)
2271
0
{
2272
0
  return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2273
0
}
2274
2275
static inline bool
2276
hi16_reloc_p (int r_type)
2277
0
{
2278
0
  return (r_type == R_MIPS_HI16
2279
0
    || r_type == R_MIPS16_HI16
2280
0
    || r_type == R_MICROMIPS_HI16
2281
0
    || r_type == R_MIPS_PCHI16);
2282
0
}
2283
2284
static inline bool
2285
lo16_reloc_p (int r_type)
2286
0
{
2287
0
  return (r_type == R_MIPS_LO16
2288
0
    || r_type == R_MIPS16_LO16
2289
0
    || r_type == R_MICROMIPS_LO16
2290
0
    || r_type == R_MIPS_PCLO16);
2291
0
}
2292
2293
static inline bool
2294
tls_hi16_reloc_p (int r_type)
2295
0
{
2296
0
  return (r_type == R_MIPS_TLS_DTPREL_HI16
2297
0
    || r_type == R_MIPS_TLS_TPREL_HI16
2298
0
    || r_type == R_MIPS16_TLS_DTPREL_HI16
2299
0
    || r_type == R_MIPS16_TLS_TPREL_HI16
2300
0
    || r_type == R_MICROMIPS_TLS_DTPREL_HI16
2301
0
    || r_type == R_MICROMIPS_TLS_TPREL_HI16);
2302
0
}
2303
2304
static inline bool
2305
mips16_call_reloc_p (int r_type)
2306
0
{
2307
0
  return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2308
0
}
2309
2310
static inline bool
2311
jal_reloc_p (int r_type)
2312
0
{
2313
0
  return (r_type == R_MIPS_26
2314
0
    || r_type == R_MIPS16_26
2315
0
    || r_type == R_MICROMIPS_26_S1);
2316
0
}
2317
2318
static inline bool
2319
b_reloc_p (int r_type)
2320
0
{
2321
0
  return (r_type == R_MIPS_PC26_S2
2322
0
    || r_type == R_MIPS_PC21_S2
2323
0
    || r_type == R_MIPS_PC16
2324
0
    || r_type == R_MIPS_GNU_REL16_S2
2325
0
    || r_type == R_MIPS16_PC16_S1
2326
0
    || r_type == R_MICROMIPS_PC16_S1
2327
0
    || r_type == R_MICROMIPS_PC10_S1
2328
0
    || r_type == R_MICROMIPS_PC7_S1);
2329
0
}
2330
2331
static inline bool
2332
aligned_pcrel_reloc_p (int r_type)
2333
0
{
2334
0
  return (r_type == R_MIPS_PC18_S3
2335
0
    || r_type == R_MIPS_PC19_S2);
2336
0
}
2337
2338
static inline bool
2339
branch_reloc_p (int r_type)
2340
0
{
2341
0
  return (r_type == R_MIPS_26
2342
0
    || r_type == R_MIPS_PC26_S2
2343
0
    || r_type == R_MIPS_PC21_S2
2344
0
    || r_type == R_MIPS_PC16
2345
0
    || r_type == R_MIPS_GNU_REL16_S2);
2346
0
}
2347
2348
static inline bool
2349
mips16_branch_reloc_p (int r_type)
2350
0
{
2351
0
  return (r_type == R_MIPS16_26
2352
0
    || r_type == R_MIPS16_PC16_S1);
2353
0
}
2354
2355
static inline bool
2356
micromips_branch_reloc_p (int r_type)
2357
0
{
2358
0
  return (r_type == R_MICROMIPS_26_S1
2359
0
    || r_type == R_MICROMIPS_PC16_S1
2360
0
    || r_type == R_MICROMIPS_PC10_S1
2361
0
    || r_type == R_MICROMIPS_PC7_S1);
2362
0
}
2363
2364
static inline bool
2365
tls_gd_reloc_p (unsigned int r_type)
2366
0
{
2367
0
  return (r_type == R_MIPS_TLS_GD
2368
0
    || r_type == R_MIPS16_TLS_GD
2369
0
    || r_type == R_MICROMIPS_TLS_GD);
2370
0
}
2371
2372
static inline bool
2373
tls_ldm_reloc_p (unsigned int r_type)
2374
0
{
2375
0
  return (r_type == R_MIPS_TLS_LDM
2376
0
    || r_type == R_MIPS16_TLS_LDM
2377
0
    || r_type == R_MICROMIPS_TLS_LDM);
2378
0
}
2379
2380
static inline bool
2381
tls_gottprel_reloc_p (unsigned int r_type)
2382
0
{
2383
0
  return (r_type == R_MIPS_TLS_GOTTPREL
2384
0
    || r_type == R_MIPS16_TLS_GOTTPREL
2385
0
    || r_type == R_MICROMIPS_TLS_GOTTPREL);
2386
0
}
2387
2388
static inline bool
2389
needs_shuffle (int r_type)
2390
1.67k
{
2391
1.67k
  return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2392
1.67k
}
2393
2394
void
2395
_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2396
             bool jal_shuffle, bfd_byte *data)
2397
829
{
2398
829
  bfd_vma first, second, val;
2399
2400
829
  if (!needs_shuffle (r_type))
2401
752
    return;
2402
2403
  /* Pick up the first and second halfwords of the instruction.  */
2404
77
  first = bfd_get_16 (abfd, data);
2405
77
  second = bfd_get_16 (abfd, data + 2);
2406
77
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2407
36
    val = first << 16 | second;
2408
41
  else if (r_type != R_MIPS16_26)
2409
41
    val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2410
41
     | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2411
0
  else
2412
0
    val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2413
0
     | ((first & 0x1f) << 21) | second);
2414
77
  bfd_put_32 (abfd, val, data);
2415
77
}
2416
2417
void
2418
_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2419
           bool jal_shuffle, bfd_byte *data)
2420
829
{
2421
829
  bfd_vma first, second, val;
2422
2423
829
  if (!needs_shuffle (r_type))
2424
752
    return;
2425
2426
77
  val = bfd_get_32 (abfd, data);
2427
77
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2428
36
    {
2429
36
      second = val & 0xffff;
2430
36
      first = val >> 16;
2431
36
    }
2432
41
  else if (r_type != R_MIPS16_26)
2433
41
    {
2434
41
      second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2435
41
      first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2436
41
    }
2437
0
  else
2438
0
    {
2439
0
      second = val & 0xffff;
2440
0
      first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2441
0
         | ((val >> 21) & 0x1f);
2442
0
    }
2443
77
  bfd_put_16 (abfd, second, data + 2);
2444
77
  bfd_put_16 (abfd, first, data);
2445
77
}
2446
2447
/* Perform reloc offset checking.
2448
   We can only use bfd_reloc_offset_in_range, which takes into account
2449
   the size of the field being relocated, when section contents will
2450
   be accessed because mips object files may use relocations that seem
2451
   to access beyond section limits.
2452
   gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2453
   R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2454
   section.  The R_MIPS_SUB applies to the addend for the next reloc
2455
   rather than the section contents.
2456
2457
   CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2458
   CHECK_INPLACE to only check partial_inplace relocs, and
2459
   CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
2460
2461
bool
2462
_bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2463
         arelent *reloc_entry, enum reloc_check check)
2464
1.05k
{
2465
1.05k
  if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2466
36
    return true;
2467
1.01k
  if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2468
7
    return true;
2469
1.01k
  return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2470
1.01k
            input_section, reloc_entry->address);
2471
1.01k
}
2472
2473
bfd_reloc_status_type
2474
_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2475
             arelent *reloc_entry, asection *input_section,
2476
             bool relocatable, void *data, bfd_vma gp)
2477
89
{
2478
89
  bfd_vma relocation;
2479
89
  bfd_signed_vma val;
2480
89
  bfd_reloc_status_type status;
2481
2482
89
  if (bfd_is_com_section (symbol->section))
2483
1
    relocation = 0;
2484
88
  else
2485
88
    relocation = symbol->value;
2486
2487
89
  if (symbol->section->output_section != NULL)
2488
89
    {
2489
89
      relocation += symbol->section->output_section->vma;
2490
89
      relocation += symbol->section->output_offset;
2491
89
    }
2492
2493
  /* Set val to the offset into the section or symbol.  */
2494
89
  val = reloc_entry->addend;
2495
2496
89
  _bfd_mips_elf_sign_extend (val, 16);
2497
2498
  /* Adjust val for the final section location and GP value.  If we
2499
     are producing relocatable output, we don't want to do this for
2500
     an external symbol.  */
2501
89
  if (! relocatable
2502
0
      || (symbol->flags & BSF_SECTION_SYM) != 0)
2503
89
    val += relocation - gp;
2504
2505
89
  if (reloc_entry->howto->partial_inplace)
2506
5
    {
2507
5
      if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2508
5
              reloc_entry->address))
2509
5
  return bfd_reloc_outofrange;
2510
2511
0
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2512
0
               (bfd_byte *) data
2513
0
               + reloc_entry->address);
2514
0
      if (status != bfd_reloc_ok)
2515
0
  return status;
2516
0
    }
2517
84
  else
2518
84
    reloc_entry->addend = val;
2519
2520
84
  if (relocatable)
2521
0
    reloc_entry->address += input_section->output_offset;
2522
2523
84
  return bfd_reloc_ok;
2524
89
}
2525
2526
/* A howto special_function for REL *HI16 relocations.  We can only
2527
   calculate the correct value once we've seen the partnering
2528
   *LO16 relocation, so just save the information for later.
2529
2530
   The ABI requires that the *LO16 immediately follow the *HI16.
2531
   However, as a GNU extension, we permit an arbitrary number of
2532
   *HI16s to be associated with a single *LO16.  This significantly
2533
   simplies the relocation handling in gcc.  */
2534
2535
bfd_reloc_status_type
2536
_bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2537
        asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2538
        asection *input_section, bfd *output_bfd,
2539
        char **error_message ATTRIBUTE_UNUSED)
2540
17
{
2541
17
  struct _mips_elf_section_data *sdata;
2542
17
  struct mips_hi16 *n;
2543
2544
17
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2545
4
    return bfd_reloc_outofrange;
2546
2547
13
  n = bfd_malloc (sizeof *n);
2548
13
  if (n == NULL)
2549
0
    return bfd_reloc_outofrange;
2550
2551
13
  sdata = mips_elf_section_data (input_section);
2552
13
  n->next = sdata->mips_hi16_list;
2553
13
  n->data = data;
2554
13
  n->output_bfd = output_bfd;
2555
13
  n->rel = *reloc_entry;
2556
13
  sdata->mips_hi16_list = n;
2557
2558
13
  if (output_bfd != NULL)
2559
0
    reloc_entry->address += input_section->output_offset;
2560
2561
13
  return bfd_reloc_ok;
2562
13
}
2563
2564
/* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2565
   like any other 16-bit relocation when applied to global symbols, but is
2566
   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2567
2568
bfd_reloc_status_type
2569
_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2570
         void *data, asection *input_section,
2571
         bfd *output_bfd, char **error_message)
2572
9
{
2573
9
  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2574
9
      || bfd_is_und_section (bfd_asymbol_section (symbol))
2575
9
      || bfd_is_com_section (bfd_asymbol_section (symbol)))
2576
    /* The relocation is against a global symbol.  */
2577
0
    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2578
0
          input_section, output_bfd,
2579
0
          error_message);
2580
2581
9
  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2582
9
           input_section, output_bfd, error_message);
2583
9
}
2584
2585
/* A helper function for REL high-part relocations that takes into account
2586
   local R_MIPS*_GOT16 relocations, which are something of a special case.
2587
   We want to install the addend in the same way as for a R_MIPS*_HI16
2588
   relocation (with a rightshift of 16).  However, since GOT16 relocations
2589
   can also be used with global symbols, their howto has a rightshift of 0.  */
2590
2591
static bfd_reloc_status_type
2592
_bfd_mips_elf_shr16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2593
         void *data, asection *input_section,
2594
         bfd *output_bfd, char **error_message)
2595
7
{
2596
7
  reloc_howto_type **howto = &reloc_entry->howto;
2597
2598
7
  if ((*howto)->type == R_MIPS_GOT16)
2599
2
    *howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2600
5
  else if ((*howto)->type == R_MIPS16_GOT16)
2601
1
    *howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2602
4
  else if ((*howto)->type == R_MICROMIPS_GOT16)
2603
0
    *howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2604
2605
7
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2606
7
              input_section, output_bfd,
2607
7
              error_message);
2608
7
}
2609
2610
/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2611
   is a straightforward 16 bit inplace relocation, but we must deal with
2612
   any partnering high-part relocations as well.  */
2613
2614
bfd_reloc_status_type
2615
_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2616
        void *data, asection *input_section,
2617
        bfd *output_bfd, char **error_message)
2618
16
{
2619
16
  bfd_vma vallo;
2620
16
  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2621
16
  struct _mips_elf_section_data *sdata;
2622
2623
16
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2624
16
          reloc_entry->address))
2625
6
    return bfd_reloc_outofrange;
2626
2627
10
  _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2628
10
         location);
2629
  /* The high 16 bits of the addend are stored in the high insn, the
2630
     low 16 bits in the low insn, but there is a catch:  You can't
2631
     just concatenate the high and low parts.  The high part of the
2632
     addend is adjusted for the fact that the low part is sign
2633
     extended.  For example, an addend of 0x38000 would have 0x0004 in
2634
     the high part and 0x8000 (=0xff..f8000) in the low part.
2635
     To extract the actual addend, calculate
2636
     ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.  */
2637
10
  vallo = _bfd_mips_elf_sign_extend (bfd_get_32 (abfd, location) & 0xffff, 16);
2638
10
  _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2639
10
             location);
2640
  /* Add in the separate addend, if any.  Since we are REL here this
2641
     will have been set and the in-place addend cleared if we have
2642
     been called from GAS via `bfd_install_relocation'.  */
2643
10
  vallo += reloc_entry->addend;
2644
2645
10
  sdata = mips_elf_section_data (input_section);
2646
13
  while (sdata->mips_hi16_list != NULL)
2647
3
    {
2648
3
      bfd_reloc_status_type ret;
2649
3
      struct mips_hi16 *hi;
2650
3
      bfd_vma addhi;
2651
3
      bfd_vma addlo;
2652
2653
3
      hi = sdata->mips_hi16_list;
2654
2655
      /* We will be applying (symbol + addend) & 0xffff to the low insn,
2656
   and we want to apply (symbol + addend + 0x8000) >> 16 to the
2657
   high insn (the +0x8000 adjusting for when the applied low part is
2658
   negative).  */
2659
3
      addhi = (hi->rel.addend + 0x8000) & ~(bfd_vma) 0xffff;
2660
3
      addlo = vallo;
2661
2662
      /* For a PC-relative relocation the PCLO16 part of the addend
2663
   is relative to its PC and not ours, so we need to adjust it.  */
2664
3
      if (hi->rel.howto->type == R_MIPS_PCHI16)
2665
0
  addlo -= reloc_entry->address - hi->rel.address;
2666
2667
3
      hi->rel.addend = addhi + _bfd_mips_elf_sign_extend (addlo & 0xffff, 16);
2668
2669
3
      ret = _bfd_mips_elf_shr16_reloc (abfd, &hi->rel, symbol, hi->data,
2670
3
               input_section, hi->output_bfd,
2671
3
               error_message);
2672
2673
3
      sdata->mips_hi16_list = hi->next;
2674
3
      free (hi);
2675
2676
3
      if (ret != bfd_reloc_ok)
2677
0
  return ret;
2678
3
    }
2679
2680
10
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2681
10
              input_section, output_bfd,
2682
10
              error_message);
2683
10
}
2684
2685
/* A generic howto special_function.  This calculates and installs the
2686
   relocation itself, thus avoiding the oft-discussed problems in
2687
   bfd_perform_relocation and bfd_install_relocation.  */
2688
2689
bfd_reloc_status_type
2690
_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2691
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2692
           asection *input_section, bfd *output_bfd,
2693
           char **error_message ATTRIBUTE_UNUSED)
2694
1.00k
{
2695
1.00k
  bfd_signed_vma val;
2696
1.00k
  bfd_reloc_status_type status;
2697
1.00k
  bool relocatable;
2698
2699
1.00k
  relocatable = (output_bfd != NULL);
2700
2701
1.00k
  if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2702
1.00k
          (relocatable
2703
1.00k
           ? check_inplace : check_std)))
2704
191
    return bfd_reloc_outofrange;
2705
2706
  /* Build up the field adjustment in VAL.  */
2707
809
  val = 0;
2708
809
  if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2709
809
      && symbol->section->output_section != NULL)
2710
809
    {
2711
      /* Either we're calculating the final field value or we have a
2712
   relocation against a section symbol.  Add in the section's
2713
   offset or address.  */
2714
809
      val += symbol->section->output_section->vma;
2715
809
      val += symbol->section->output_offset;
2716
809
    }
2717
2718
809
  if (!relocatable)
2719
809
    {
2720
      /* We're calculating the final field value.  Add in the symbol's value
2721
   and, if pc-relative, subtract the address of the field itself.  */
2722
809
      val += symbol->value;
2723
809
      if (reloc_entry->howto->pc_relative)
2724
28
  {
2725
28
    val -= input_section->output_section->vma;
2726
28
    val -= input_section->output_offset;
2727
28
    val -= reloc_entry->address;
2728
28
  }
2729
809
    }
2730
2731
  /* VAL is now the final adjustment.  If we're keeping this relocation
2732
     in the output file, and if the relocation uses a separate addend,
2733
     we just need to add VAL to that addend.  Otherwise we need to add
2734
     VAL to the relocation field itself.  */
2735
809
  if (relocatable && !reloc_entry->howto->partial_inplace)
2736
0
    reloc_entry->addend += val;
2737
809
  else
2738
809
    {
2739
809
      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2740
2741
      /* Add in the separate addend, if any.  */
2742
809
      val += reloc_entry->addend;
2743
2744
      /* The high 16 bits of the addend are stored in the high insn, the
2745
   low 16 bits in the low insn, but there is a catch:  You can't
2746
   just concatenate the high and low parts.  The high part of the
2747
   addend is adjusted for the fact that the low part is sign
2748
   extended.  For example, an addend of 0x38000 would have 0x0004 in
2749
   the high part and 0x8000 (=0xff..f8000) in the low part.
2750
   We will be applying (symbol + addend) & 0xffff to the low insn,
2751
   and we want to apply (symbol + addend + 0x8000) >> 16 to the
2752
   high insn (the +0x8000 adjusting for when the applied low part is
2753
   negative).  Analogously for the higher parts of a 64-bit addend.  */
2754
809
      if (reloc_entry->howto->bitsize == 16
2755
123
    && reloc_entry->howto->rightshift % 16 == 0)
2756
115
#ifdef BFD64
2757
115
  val += 0x800080008000ULL >> (48 - reloc_entry->howto->rightshift);
2758
#else
2759
  {
2760
    if (reloc_entry->howto->rightshift <= 16)
2761
      val += 0x8000 >> (16 - reloc_entry->howto->rightshift);
2762
    else
2763
      abort ();
2764
  }
2765
#endif
2766
2767
      /* Add VAL to the relocation field.  */
2768
809
      _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2769
809
             location);
2770
809
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2771
809
               location);
2772
809
      _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2773
809
           location);
2774
2775
809
      if (status != bfd_reloc_ok)
2776
44
  return status;
2777
809
    }
2778
2779
765
  if (relocatable)
2780
0
    reloc_entry->address += input_section->output_offset;
2781
2782
765
  return bfd_reloc_ok;
2783
809
}
2784

2785
/* Swap an entry in a .gptab section.  Note that these routines rely
2786
   on the equivalence of the two elements of the union.  */
2787
2788
static void
2789
bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2790
            Elf32_gptab *in)
2791
0
{
2792
0
  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2793
0
  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2794
0
}
2795
2796
static void
2797
bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2798
             Elf32_External_gptab *ex)
2799
0
{
2800
0
  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2801
0
  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2802
0
}
2803
2804
static void
2805
bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2806
        Elf32_External_compact_rel *ex)
2807
0
{
2808
0
  H_PUT_32 (abfd, in->id1, ex->id1);
2809
0
  H_PUT_32 (abfd, in->num, ex->num);
2810
0
  H_PUT_32 (abfd, in->id2, ex->id2);
2811
0
  H_PUT_32 (abfd, in->offset, ex->offset);
2812
0
  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2813
0
  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2814
0
}
2815
2816
static void
2817
bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2818
         Elf32_External_crinfo *ex)
2819
0
{
2820
0
  unsigned long l;
2821
2822
0
  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2823
0
       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2824
0
       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2825
0
       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2826
0
  H_PUT_32 (abfd, l, ex->info);
2827
0
  H_PUT_32 (abfd, in->konst, ex->konst);
2828
0
  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2829
0
}
2830

2831
/* A .reginfo section holds a single Elf32_RegInfo structure.  These
2832
   routines swap this structure in and out.  They are used outside of
2833
   BFD, so they are globally visible.  */
2834
2835
void
2836
bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2837
        Elf32_RegInfo *in)
2838
0
{
2839
0
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2840
0
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2841
0
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2842
0
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2843
0
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2844
0
  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2845
0
}
2846
2847
void
2848
bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2849
         Elf32_External_RegInfo *ex)
2850
0
{
2851
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2852
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2853
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2854
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2855
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2856
0
  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2857
0
}
2858
2859
/* In the 64 bit ABI, the .MIPS.options section holds register
2860
   information in an Elf64_Reginfo structure.  These routines swap
2861
   them in and out.  They are globally visible because they are used
2862
   outside of BFD.  These routines are here so that gas can call them
2863
   without worrying about whether the 64 bit ABI has been included.  */
2864
2865
void
2866
bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2867
        Elf64_Internal_RegInfo *in)
2868
76
{
2869
76
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2870
76
  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2871
76
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2872
76
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2873
76
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2874
76
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2875
76
  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2876
76
}
2877
2878
void
2879
bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2880
         Elf64_External_RegInfo *ex)
2881
0
{
2882
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2883
0
  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2884
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2885
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2886
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2887
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2888
0
  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2889
0
}
2890
2891
/* Swap in an options header.  */
2892
2893
void
2894
bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2895
            Elf_Internal_Options *in)
2896
1.06k
{
2897
1.06k
  in->kind = H_GET_8 (abfd, ex->kind);
2898
1.06k
  in->size = H_GET_8 (abfd, ex->size);
2899
1.06k
  in->section = H_GET_16 (abfd, ex->section);
2900
1.06k
  in->info = H_GET_32 (abfd, ex->info);
2901
1.06k
}
2902
2903
/* Swap out an options header.  */
2904
2905
void
2906
bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2907
             Elf_External_Options *ex)
2908
0
{
2909
0
  H_PUT_8 (abfd, in->kind, ex->kind);
2910
0
  H_PUT_8 (abfd, in->size, ex->size);
2911
0
  H_PUT_16 (abfd, in->section, ex->section);
2912
0
  H_PUT_32 (abfd, in->info, ex->info);
2913
0
}
2914
2915
/* Swap in an abiflags structure.  */
2916
2917
void
2918
bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2919
          const Elf_External_ABIFlags_v0 *ex,
2920
          Elf_Internal_ABIFlags_v0 *in)
2921
0
{
2922
0
  in->version = H_GET_16 (abfd, ex->version);
2923
0
  in->isa_level = H_GET_8 (abfd, ex->isa_level);
2924
0
  in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2925
0
  in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2926
0
  in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2927
0
  in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2928
0
  in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2929
0
  in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2930
0
  in->ases = H_GET_32 (abfd, ex->ases);
2931
0
  in->flags1 = H_GET_32 (abfd, ex->flags1);
2932
0
  in->flags2 = H_GET_32 (abfd, ex->flags2);
2933
0
}
2934
2935
/* Swap out an abiflags structure.  */
2936
2937
void
2938
bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2939
           const Elf_Internal_ABIFlags_v0 *in,
2940
           Elf_External_ABIFlags_v0 *ex)
2941
0
{
2942
0
  H_PUT_16 (abfd, in->version, ex->version);
2943
0
  H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2944
0
  H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2945
0
  H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2946
0
  H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2947
0
  H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2948
0
  H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2949
0
  H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2950
0
  H_PUT_32 (abfd, in->ases, ex->ases);
2951
0
  H_PUT_32 (abfd, in->flags1, ex->flags1);
2952
0
  H_PUT_32 (abfd, in->flags2, ex->flags2);
2953
0
}
2954

2955
/* This function is called via qsort() to sort the dynamic relocation
2956
   entries by increasing r_symndx value.  */
2957
2958
static int
2959
sort_dynamic_relocs (const void *arg1, const void *arg2)
2960
0
{
2961
0
  Elf_Internal_Rela int_reloc1;
2962
0
  Elf_Internal_Rela int_reloc2;
2963
0
  int diff;
2964
2965
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2966
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2967
2968
0
  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2969
0
  if (diff != 0)
2970
0
    return diff;
2971
2972
0
  if (int_reloc1.r_offset < int_reloc2.r_offset)
2973
0
    return -1;
2974
0
  if (int_reloc1.r_offset > int_reloc2.r_offset)
2975
0
    return 1;
2976
0
  return 0;
2977
0
}
2978
2979
/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2980
2981
static int
2982
sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2983
      const void *arg2 ATTRIBUTE_UNUSED)
2984
0
{
2985
0
#ifdef BFD64
2986
0
  Elf_Internal_Rela int_reloc1[3];
2987
0
  Elf_Internal_Rela int_reloc2[3];
2988
2989
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2990
0
    (reldyn_sorting_bfd, arg1, int_reloc1);
2991
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2992
0
    (reldyn_sorting_bfd, arg2, int_reloc2);
2993
2994
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2995
0
    return -1;
2996
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2997
0
    return 1;
2998
2999
0
  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
3000
0
    return -1;
3001
0
  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
3002
0
    return 1;
3003
0
  return 0;
3004
#else
3005
  abort ();
3006
#endif
3007
0
}
3008
3009
3010
/* This routine is used to write out ECOFF debugging external symbol
3011
   information.  It is called via mips_elf_link_hash_traverse.  The
3012
   ECOFF external symbol information must match the ELF external
3013
   symbol information.  Unfortunately, at this point we don't know
3014
   whether a symbol is required by reloc information, so the two
3015
   tables may wind up being different.  We must sort out the external
3016
   symbol information before we can set the final size of the .mdebug
3017
   section, and we must set the size of the .mdebug section before we
3018
   can relocate any sections, and we can't know which symbols are
3019
   required by relocation until we relocate the sections.
3020
   Fortunately, it is relatively unlikely that any symbol will be
3021
   stripped but required by a reloc.  In particular, it can not happen
3022
   when generating a final executable.  */
3023
3024
static bool
3025
mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
3026
0
{
3027
0
  struct extsym_info *einfo = data;
3028
0
  bool strip;
3029
0
  asection *sec, *output_section;
3030
3031
0
  if (h->root.indx == -2)
3032
0
    strip = false;
3033
0
  else if ((h->root.def_dynamic
3034
0
      || h->root.ref_dynamic
3035
0
      || h->root.type == bfd_link_hash_new)
3036
0
     && !h->root.def_regular
3037
0
     && !h->root.ref_regular)
3038
0
    strip = true;
3039
0
  else if (einfo->info->strip == strip_all
3040
0
     || (einfo->info->strip == strip_some
3041
0
         && bfd_hash_lookup (einfo->info->keep_hash,
3042
0
           h->root.root.root.string,
3043
0
           false, false) == NULL))
3044
0
    strip = true;
3045
0
  else
3046
0
    strip = false;
3047
3048
0
  if (strip)
3049
0
    return true;
3050
3051
0
  if (h->esym.ifd == -2)
3052
0
    {
3053
0
      h->esym.jmptbl = 0;
3054
0
      h->esym.cobol_main = 0;
3055
0
      h->esym.weakext = 0;
3056
0
      h->esym.reserved = 0;
3057
0
      h->esym.ifd = ifdNil;
3058
0
      h->esym.asym.value = 0;
3059
0
      h->esym.asym.st = stGlobal;
3060
3061
0
      if (h->root.root.type == bfd_link_hash_undefined
3062
0
    || h->root.root.type == bfd_link_hash_undefweak)
3063
0
  {
3064
0
    const char *name;
3065
3066
    /* Use undefined class.  Also, set class and type for some
3067
       special symbols.  */
3068
0
    name = h->root.root.root.string;
3069
0
    if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3070
0
        || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3071
0
      {
3072
0
        h->esym.asym.sc = scData;
3073
0
        h->esym.asym.st = stLabel;
3074
0
        h->esym.asym.value = 0;
3075
0
      }
3076
0
    else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3077
0
      {
3078
0
        h->esym.asym.sc = scAbs;
3079
0
        h->esym.asym.st = stLabel;
3080
0
        h->esym.asym.value =
3081
0
    mips_elf_hash_table (einfo->info)->procedure_count;
3082
0
      }
3083
0
    else
3084
0
      h->esym.asym.sc = scUndefined;
3085
0
  }
3086
0
      else if (h->root.root.type != bfd_link_hash_defined
3087
0
    && h->root.root.type != bfd_link_hash_defweak)
3088
0
  h->esym.asym.sc = scAbs;
3089
0
      else
3090
0
  {
3091
0
    const char *name;
3092
3093
0
    sec = h->root.root.u.def.section;
3094
0
    output_section = sec->output_section;
3095
3096
    /* When making a shared library and symbol h is the one from
3097
       the another shared library, OUTPUT_SECTION may be null.  */
3098
0
    if (output_section == NULL)
3099
0
      h->esym.asym.sc = scUndefined;
3100
0
    else
3101
0
      {
3102
0
        name = bfd_section_name (output_section);
3103
3104
0
        if (strcmp (name, ".text") == 0)
3105
0
    h->esym.asym.sc = scText;
3106
0
        else if (strcmp (name, ".data") == 0)
3107
0
    h->esym.asym.sc = scData;
3108
0
        else if (strcmp (name, ".sdata") == 0)
3109
0
    h->esym.asym.sc = scSData;
3110
0
        else if (strcmp (name, ".rodata") == 0
3111
0
           || strcmp (name, ".rdata") == 0)
3112
0
    h->esym.asym.sc = scRData;
3113
0
        else if (strcmp (name, ".bss") == 0)
3114
0
    h->esym.asym.sc = scBss;
3115
0
        else if (strcmp (name, ".sbss") == 0)
3116
0
    h->esym.asym.sc = scSBss;
3117
0
        else if (strcmp (name, ".init") == 0)
3118
0
    h->esym.asym.sc = scInit;
3119
0
        else if (strcmp (name, ".fini") == 0)
3120
0
    h->esym.asym.sc = scFini;
3121
0
        else
3122
0
    h->esym.asym.sc = scAbs;
3123
0
      }
3124
0
  }
3125
3126
0
      h->esym.asym.reserved = 0;
3127
0
      h->esym.asym.index = indexNil;
3128
0
    }
3129
3130
0
  if (h->root.root.type == bfd_link_hash_common)
3131
0
    h->esym.asym.value = h->root.root.u.c.size;
3132
0
  else if (h->root.root.type == bfd_link_hash_defined
3133
0
     || h->root.root.type == bfd_link_hash_defweak)
3134
0
    {
3135
0
      if (h->esym.asym.sc == scCommon)
3136
0
  h->esym.asym.sc = scBss;
3137
0
      else if (h->esym.asym.sc == scSCommon)
3138
0
  h->esym.asym.sc = scSBss;
3139
3140
0
      sec = h->root.root.u.def.section;
3141
0
      output_section = sec->output_section;
3142
0
      if (output_section != NULL)
3143
0
  h->esym.asym.value = (h->root.root.u.def.value
3144
0
            + sec->output_offset
3145
0
            + output_section->vma);
3146
0
      else
3147
0
  h->esym.asym.value = 0;
3148
0
    }
3149
0
  else
3150
0
    {
3151
0
      struct mips_elf_link_hash_entry *hd = h;
3152
3153
0
      while (hd->root.root.type == bfd_link_hash_indirect)
3154
0
  hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3155
3156
0
      if (hd->needs_lazy_stub)
3157
0
  {
3158
0
    BFD_ASSERT (hd->root.plt.plist != NULL);
3159
0
    BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3160
    /* Set type and value for a symbol with a function stub.  */
3161
0
    h->esym.asym.st = stProc;
3162
0
    sec = hd->root.root.u.def.section;
3163
0
    if (sec == NULL)
3164
0
      h->esym.asym.value = 0;
3165
0
    else
3166
0
      {
3167
0
        output_section = sec->output_section;
3168
0
        if (output_section != NULL)
3169
0
    h->esym.asym.value = (hd->root.plt.plist->stub_offset
3170
0
              + sec->output_offset
3171
0
              + output_section->vma);
3172
0
        else
3173
0
    h->esym.asym.value = 0;
3174
0
      }
3175
0
  }
3176
0
    }
3177
3178
0
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3179
0
              h->root.root.root.string,
3180
0
              &h->esym))
3181
0
    {
3182
0
      einfo->failed = true;
3183
0
      return false;
3184
0
    }
3185
3186
0
  return true;
3187
0
}
3188
3189
/* A comparison routine used to sort .gptab entries.  */
3190
3191
static int
3192
gptab_compare (const void *p1, const void *p2)
3193
0
{
3194
0
  const Elf32_gptab *a1 = p1;
3195
0
  const Elf32_gptab *a2 = p2;
3196
3197
0
  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3198
0
}
3199

3200
/* Functions to manage the got entry hash table.  */
3201
3202
/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3203
   hash number.  */
3204
3205
static inline hashval_t
3206
mips_elf_hash_bfd_vma (bfd_vma addr)
3207
0
{
3208
0
#ifdef BFD64
3209
0
  return addr + (addr >> 32);
3210
#else
3211
  return addr;
3212
#endif
3213
0
}
3214
3215
static hashval_t
3216
mips_elf_got_entry_hash (const void *entry_)
3217
0
{
3218
0
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3219
3220
0
  return (entry->symndx
3221
0
    + ((entry->tls_type == GOT_TLS_LDM) << 18)
3222
0
    + (entry->tls_type == GOT_TLS_LDM ? 0
3223
0
       : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3224
0
       : entry->symndx >= 0 ? (entry->abfd->id
3225
0
             + mips_elf_hash_bfd_vma (entry->d.addend))
3226
0
       : entry->d.h->root.root.root.hash));
3227
0
}
3228
3229
static int
3230
mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3231
0
{
3232
0
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3233
0
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3234
3235
0
  return (e1->symndx == e2->symndx
3236
0
    && e1->tls_type == e2->tls_type
3237
0
    && (e1->tls_type == GOT_TLS_LDM ? true
3238
0
        : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3239
0
        : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3240
0
           && e1->d.addend == e2->d.addend)
3241
0
        : e2->abfd && e1->d.h == e2->d.h));
3242
0
}
3243
3244
static hashval_t
3245
mips_got_page_ref_hash (const void *ref_)
3246
0
{
3247
0
  const struct mips_got_page_ref *ref;
3248
3249
0
  ref = (const struct mips_got_page_ref *) ref_;
3250
0
  return ((ref->symndx >= 0
3251
0
     ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3252
0
     : ref->u.h->root.root.root.hash)
3253
0
    + mips_elf_hash_bfd_vma (ref->addend));
3254
0
}
3255
3256
static int
3257
mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3258
0
{
3259
0
  const struct mips_got_page_ref *ref1, *ref2;
3260
3261
0
  ref1 = (const struct mips_got_page_ref *) ref1_;
3262
0
  ref2 = (const struct mips_got_page_ref *) ref2_;
3263
0
  return (ref1->symndx == ref2->symndx
3264
0
    && (ref1->symndx < 0
3265
0
        ? ref1->u.h == ref2->u.h
3266
0
        : ref1->u.abfd == ref2->u.abfd)
3267
0
    && ref1->addend == ref2->addend);
3268
0
}
3269
3270
static hashval_t
3271
mips_got_page_entry_hash (const void *entry_)
3272
0
{
3273
0
  const struct mips_got_page_entry *entry;
3274
3275
0
  entry = (const struct mips_got_page_entry *) entry_;
3276
0
  return entry->sec->id;
3277
0
}
3278
3279
static int
3280
mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3281
0
{
3282
0
  const struct mips_got_page_entry *entry1, *entry2;
3283
3284
0
  entry1 = (const struct mips_got_page_entry *) entry1_;
3285
0
  entry2 = (const struct mips_got_page_entry *) entry2_;
3286
0
  return entry1->sec == entry2->sec;
3287
0
}
3288

3289
/* Create and return a new mips_got_info structure.  */
3290
3291
static struct mips_got_info *
3292
mips_elf_create_got_info (bfd *abfd)
3293
0
{
3294
0
  struct mips_got_info *g;
3295
3296
0
  g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3297
0
  if (g == NULL)
3298
0
    return NULL;
3299
3300
0
  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3301
0
            mips_elf_got_entry_eq, NULL);
3302
0
  if (g->got_entries == NULL)
3303
0
    return NULL;
3304
3305
0
  g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3306
0
              mips_got_page_ref_eq, NULL);
3307
0
  if (g->got_page_refs == NULL)
3308
0
    return NULL;
3309
3310
0
  return g;
3311
0
}
3312
3313
/* Return the GOT info for input bfd ABFD, trying to create a new one if
3314
   CREATE_P and if ABFD doesn't already have a GOT.  */
3315
3316
static struct mips_got_info *
3317
mips_elf_bfd_got (bfd *abfd, bool create_p)
3318
0
{
3319
0
  struct mips_elf_obj_tdata *tdata;
3320
3321
0
  if (!is_mips_elf (abfd))
3322
0
    return NULL;
3323
3324
0
  tdata = mips_elf_tdata (abfd);
3325
0
  if (!tdata->got && create_p)
3326
0
    tdata->got = mips_elf_create_got_info (abfd);
3327
0
  return tdata->got;
3328
0
}
3329
3330
/* Record that ABFD should use output GOT G.  */
3331
3332
static void
3333
mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3334
0
{
3335
0
  struct mips_elf_obj_tdata *tdata;
3336
3337
0
  BFD_ASSERT (is_mips_elf (abfd));
3338
0
  tdata = mips_elf_tdata (abfd);
3339
0
  if (tdata->got)
3340
0
    {
3341
      /* The GOT structure itself and the hash table entries are
3342
   allocated to a bfd, but the hash tables aren't.  */
3343
0
      htab_delete (tdata->got->got_entries);
3344
0
      htab_delete (tdata->got->got_page_refs);
3345
0
      if (tdata->got->got_page_entries)
3346
0
  htab_delete (tdata->got->got_page_entries);
3347
0
    }
3348
0
  tdata->got = g;
3349
0
}
3350
3351
/* Return the dynamic relocation section.  If it doesn't exist, try to
3352
   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
3353
   if creation fails.  */
3354
3355
static asection *
3356
mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3357
0
{
3358
0
  const char *dname;
3359
0
  asection *sreloc;
3360
0
  bfd *dynobj;
3361
3362
0
  dname = MIPS_ELF_REL_DYN_NAME (info);
3363
0
  dynobj = elf_hash_table (info)->dynobj;
3364
0
  sreloc = bfd_get_linker_section (dynobj, dname);
3365
0
  if (sreloc == NULL && create_p)
3366
0
    {
3367
0
      sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3368
0
               (SEC_ALLOC
3369
0
                | SEC_LOAD
3370
0
                | SEC_HAS_CONTENTS
3371
0
                | SEC_IN_MEMORY
3372
0
                | SEC_LINKER_CREATED
3373
0
                | SEC_READONLY));
3374
0
      if (sreloc == NULL
3375
0
    || !bfd_set_section_alignment (sreloc,
3376
0
           MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3377
0
  return NULL;
3378
0
    }
3379
0
  return sreloc;
3380
0
}
3381
3382
/* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
3383
3384
static int
3385
mips_elf_reloc_tls_type (unsigned int r_type)
3386
0
{
3387
0
  if (tls_gd_reloc_p (r_type))
3388
0
    return GOT_TLS_GD;
3389
3390
0
  if (tls_ldm_reloc_p (r_type))
3391
0
    return GOT_TLS_LDM;
3392
3393
0
  if (tls_gottprel_reloc_p (r_type))
3394
0
    return GOT_TLS_IE;
3395
3396
0
  return GOT_TLS_NONE;
3397
0
}
3398
3399
/* Return the number of GOT slots needed for GOT TLS type TYPE.  */
3400
3401
static int
3402
mips_tls_got_entries (unsigned int type)
3403
0
{
3404
0
  switch (type)
3405
0
    {
3406
0
    case GOT_TLS_GD:
3407
0
    case GOT_TLS_LDM:
3408
0
      return 2;
3409
3410
0
    case GOT_TLS_IE:
3411
0
      return 1;
3412
3413
0
    case GOT_TLS_NONE:
3414
0
      return 0;
3415
0
    }
3416
0
  abort ();
3417
0
}
3418
3419
/* Count the number of relocations needed for a TLS GOT entry, with
3420
   access types from TLS_TYPE, and symbol H (or a local symbol if H
3421
   is NULL).  */
3422
3423
static int
3424
mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3425
         struct elf_link_hash_entry *h)
3426
0
{
3427
0
  int indx = 0;
3428
0
  bool need_relocs = false;
3429
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3430
3431
0
  if (h != NULL
3432
0
      && h->dynindx != -1
3433
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3434
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3435
0
    indx = h->dynindx;
3436
3437
0
  if ((bfd_link_dll (info) || indx != 0)
3438
0
      && (h == NULL
3439
0
    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3440
0
    || h->root.type != bfd_link_hash_undefweak))
3441
0
    need_relocs = true;
3442
3443
0
  if (!need_relocs)
3444
0
    return 0;
3445
3446
0
  switch (tls_type)
3447
0
    {
3448
0
    case GOT_TLS_GD:
3449
0
      return indx != 0 ? 2 : 1;
3450
3451
0
    case GOT_TLS_IE:
3452
0
      return 1;
3453
3454
0
    case GOT_TLS_LDM:
3455
0
      return bfd_link_dll (info) ? 1 : 0;
3456
3457
0
    default:
3458
0
      return 0;
3459
0
    }
3460
0
}
3461
3462
/* Add the number of GOT entries and TLS relocations required by ENTRY
3463
   to G.  */
3464
3465
static void
3466
mips_elf_count_got_entry (struct bfd_link_info *info,
3467
        struct mips_got_info *g,
3468
        struct mips_got_entry *entry)
3469
0
{
3470
0
  if (entry->tls_type)
3471
0
    {
3472
0
      g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3473
0
      g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3474
0
          entry->symndx < 0
3475
0
          ? &entry->d.h->root : NULL);
3476
0
    }
3477
0
  else if (entry->symndx >= 0)
3478
0
    g->local_gotno += 1;
3479
0
  else if (entry->d.h->global_got_area == GGA_NONE)
3480
0
    {
3481
      /* On VxWorks, calls can refer directly to the .got.plt entry,
3482
   in which case they won't have an entry in the regular GOT.
3483
   This is arranged for in `mips_elf_count_got_symbols' and we
3484
   need to refrain from counting these entries for the regular
3485
   GOT here.  */
3486
0
      if (mips_elf_hash_table (info)->root.target_os != is_vxworks
3487
0
    || entry->d.h->root.dynindx == -1
3488
0
    || !entry->d.h->got_only_for_calls
3489
0
    || entry->d.h->root.plt.plist->mips_offset == MINUS_ONE)
3490
0
  g->local_gotno += 1;
3491
0
    }
3492
0
  else
3493
0
    g->global_gotno += 1;
3494
0
}
3495
3496
/* Output a simple dynamic relocation into SRELOC.  */
3497
3498
static void
3499
mips_elf_output_dynamic_relocation (bfd *output_bfd,
3500
            asection *sreloc,
3501
            unsigned long reloc_index,
3502
            unsigned long indx,
3503
            int r_type,
3504
            bfd_vma offset)
3505
0
{
3506
0
  Elf_Internal_Rela rel[3];
3507
3508
0
  memset (rel, 0, sizeof (rel));
3509
3510
0
  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3511
0
  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3512
3513
0
  if (ABI_64_P (output_bfd))
3514
0
    {
3515
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3516
0
  (output_bfd, &rel[0],
3517
0
   (sreloc->contents
3518
0
    + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3519
0
    }
3520
0
  else
3521
0
    bfd_elf32_swap_reloc_out
3522
0
      (output_bfd, &rel[0],
3523
0
       (sreloc->contents
3524
0
  + reloc_index * sizeof (Elf32_External_Rel)));
3525
0
}
3526
3527
/* Initialize a set of TLS GOT entries for one symbol.  */
3528
3529
static void
3530
mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3531
             struct mips_got_entry *entry,
3532
             struct mips_elf_link_hash_entry *h,
3533
             bfd_vma value)
3534
0
{
3535
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3536
0
  struct mips_elf_link_hash_table *htab;
3537
0
  int indx;
3538
0
  asection *sreloc, *sgot;
3539
0
  bfd_vma got_offset, got_offset2;
3540
0
  bool need_relocs = false;
3541
3542
0
  htab = mips_elf_hash_table (info);
3543
0
  if (htab == NULL)
3544
0
    return;
3545
3546
0
  sgot = htab->root.sgot;
3547
3548
0
  indx = 0;
3549
0
  if (h != NULL
3550
0
      && h->root.dynindx != -1
3551
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3552
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3553
0
    indx = h->root.dynindx;
3554
3555
0
  if (entry->tls_initialized)
3556
0
    return;
3557
3558
0
  if ((bfd_link_dll (info) || indx != 0)
3559
0
      && (h == NULL
3560
0
    || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3561
0
    || h->root.type != bfd_link_hash_undefweak))
3562
0
    need_relocs = true;
3563
3564
  /* MINUS_ONE means the symbol is not defined in this object.  It may not
3565
     be defined at all; assume that the value doesn't matter in that
3566
     case.  Otherwise complain if we would use the value.  */
3567
0
  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3568
0
        || h->root.root.type == bfd_link_hash_undefweak);
3569
3570
  /* Emit necessary relocations.  */
3571
0
  sreloc = mips_elf_rel_dyn_section (info, false);
3572
0
  got_offset = entry->gotidx;
3573
3574
0
  switch (entry->tls_type)
3575
0
    {
3576
0
    case GOT_TLS_GD:
3577
      /* General Dynamic.  */
3578
0
      got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3579
3580
0
      if (need_relocs)
3581
0
  {
3582
0
    mips_elf_output_dynamic_relocation
3583
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3584
0
       ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3585
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3586
3587
0
    if (indx)
3588
0
      mips_elf_output_dynamic_relocation
3589
0
        (abfd, sreloc, sreloc->reloc_count++, indx,
3590
0
         ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3591
0
         sgot->output_offset + sgot->output_section->vma + got_offset2);
3592
0
    else
3593
0
      MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3594
0
             sgot->contents + got_offset2);
3595
0
  }
3596
0
      else
3597
0
  {
3598
0
    MIPS_ELF_PUT_WORD (abfd, 1,
3599
0
           sgot->contents + got_offset);
3600
0
    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3601
0
           sgot->contents + got_offset2);
3602
0
  }
3603
0
      break;
3604
3605
0
    case GOT_TLS_IE:
3606
      /* Initial Exec model.  */
3607
0
      if (need_relocs)
3608
0
  {
3609
0
    if (indx == 0)
3610
0
      MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3611
0
             sgot->contents + got_offset);
3612
0
    else
3613
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3614
0
             sgot->contents + got_offset);
3615
3616
0
    mips_elf_output_dynamic_relocation
3617
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3618
0
       ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3619
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3620
0
  }
3621
0
      else
3622
0
  MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3623
0
         sgot->contents + got_offset);
3624
0
      break;
3625
3626
0
    case GOT_TLS_LDM:
3627
      /* The initial offset is zero, and the LD offsets will include the
3628
   bias by DTP_OFFSET.  */
3629
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3630
0
       sgot->contents + got_offset
3631
0
       + MIPS_ELF_GOT_SIZE (abfd));
3632
3633
0
      if (!bfd_link_dll (info))
3634
0
  MIPS_ELF_PUT_WORD (abfd, 1,
3635
0
         sgot->contents + got_offset);
3636
0
      else
3637
0
  mips_elf_output_dynamic_relocation
3638
0
    (abfd, sreloc, sreloc->reloc_count++, indx,
3639
0
     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3640
0
     sgot->output_offset + sgot->output_section->vma + got_offset);
3641
0
      break;
3642
3643
0
    default:
3644
0
      abort ();
3645
0
    }
3646
3647
0
  entry->tls_initialized = true;
3648
0
}
3649
3650
/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3651
   for global symbol H.  .got.plt comes before the GOT, so the offset
3652
   will be negative.  */
3653
3654
static bfd_vma
3655
mips_elf_gotplt_index (struct bfd_link_info *info,
3656
           struct elf_link_hash_entry *h)
3657
0
{
3658
0
  bfd_vma got_address, got_value;
3659
0
  struct mips_elf_link_hash_table *htab;
3660
3661
0
  htab = mips_elf_hash_table (info);
3662
0
  BFD_ASSERT (htab != NULL);
3663
3664
0
  BFD_ASSERT (h->plt.plist != NULL);
3665
0
  BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3666
3667
  /* Calculate the address of the associated .got.plt entry.  */
3668
0
  got_address = (htab->root.sgotplt->output_section->vma
3669
0
     + htab->root.sgotplt->output_offset
3670
0
     + (h->plt.plist->gotplt_index
3671
0
        * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3672
3673
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3674
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3675
0
         + htab->root.hgot->root.u.def.section->output_offset
3676
0
         + htab->root.hgot->root.u.def.value);
3677
3678
0
  return got_address - got_value;
3679
0
}
3680
3681
/* Return the GOT offset for address VALUE.   If there is not yet a GOT
3682
   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3683
   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3684
   offset can be found.  */
3685
3686
static bfd_vma
3687
mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3688
        bfd_vma value, unsigned long r_symndx,
3689
        struct mips_elf_link_hash_entry *h, int r_type)
3690
0
{
3691
0
  struct mips_elf_link_hash_table *htab;
3692
0
  struct mips_got_entry *entry;
3693
3694
0
  htab = mips_elf_hash_table (info);
3695
0
  BFD_ASSERT (htab != NULL);
3696
3697
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3698
0
             r_symndx, h, r_type);
3699
0
  if (!entry)
3700
0
    return MINUS_ONE;
3701
3702
0
  if (entry->tls_type)
3703
0
    mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3704
0
  return entry->gotidx;
3705
0
}
3706
3707
/* Return the GOT index of global symbol H in the primary GOT.  */
3708
3709
static bfd_vma
3710
mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3711
           struct elf_link_hash_entry *h)
3712
0
{
3713
0
  struct mips_elf_link_hash_table *htab;
3714
0
  long global_got_dynindx;
3715
0
  struct mips_got_info *g;
3716
0
  bfd_vma got_index;
3717
3718
0
  htab = mips_elf_hash_table (info);
3719
0
  BFD_ASSERT (htab != NULL);
3720
3721
0
  global_got_dynindx = 0;
3722
0
  if (htab->global_gotsym != NULL)
3723
0
    global_got_dynindx = htab->global_gotsym->dynindx;
3724
3725
  /* Once we determine the global GOT entry with the lowest dynamic
3726
     symbol table index, we must put all dynamic symbols with greater
3727
     indices into the primary GOT.  That makes it easy to calculate the
3728
     GOT offset.  */
3729
0
  BFD_ASSERT (h->dynindx >= global_got_dynindx);
3730
0
  g = mips_elf_bfd_got (obfd, false);
3731
0
  got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3732
0
         * MIPS_ELF_GOT_SIZE (obfd));
3733
0
  BFD_ASSERT (got_index < htab->root.sgot->size);
3734
3735
0
  return got_index;
3736
0
}
3737
3738
/* Return the GOT index for the global symbol indicated by H, which is
3739
   referenced by a relocation of type R_TYPE in IBFD.  */
3740
3741
static bfd_vma
3742
mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3743
         struct elf_link_hash_entry *h, int r_type)
3744
0
{
3745
0
  struct mips_elf_link_hash_table *htab;
3746
0
  struct mips_got_info *g;
3747
0
  struct mips_got_entry lookup, *entry;
3748
0
  bfd_vma gotidx;
3749
3750
0
  htab = mips_elf_hash_table (info);
3751
0
  BFD_ASSERT (htab != NULL);
3752
3753
0
  g = mips_elf_bfd_got (ibfd, false);
3754
0
  BFD_ASSERT (g);
3755
3756
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3757
0
  if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3758
0
    return mips_elf_primary_global_got_index (obfd, info, h);
3759
3760
0
  lookup.abfd = ibfd;
3761
0
  lookup.symndx = -1;
3762
0
  lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3763
0
  entry = htab_find (g->got_entries, &lookup);
3764
0
  BFD_ASSERT (entry);
3765
3766
0
  gotidx = entry->gotidx;
3767
0
  BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3768
3769
0
  if (lookup.tls_type)
3770
0
    {
3771
0
      bfd_vma value = MINUS_ONE;
3772
3773
0
      if ((h->root.type == bfd_link_hash_defined
3774
0
     || h->root.type == bfd_link_hash_defweak)
3775
0
    && h->root.u.def.section->output_section)
3776
0
  value = (h->root.u.def.value
3777
0
     + h->root.u.def.section->output_offset
3778
0
     + h->root.u.def.section->output_section->vma);
3779
3780
0
      mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3781
0
    }
3782
0
  return gotidx;
3783
0
}
3784
3785
/* Find a GOT page entry that points to within 32KB of VALUE.  These
3786
   entries are supposed to be placed at small offsets in the GOT, i.e.,
3787
   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3788
   entry could be created.  If OFFSETP is nonnull, use it to return the
3789
   offset of the GOT entry from VALUE.  */
3790
3791
static bfd_vma
3792
mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3793
       bfd_vma value, bfd_vma *offsetp)
3794
0
{
3795
0
  bfd_vma page, got_index;
3796
0
  struct mips_got_entry *entry;
3797
3798
0
  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3799
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3800
0
             NULL, R_MIPS_GOT_PAGE);
3801
3802
0
  if (!entry)
3803
0
    return MINUS_ONE;
3804
3805
0
  got_index = entry->gotidx;
3806
3807
0
  if (offsetp)
3808
0
    *offsetp = value - entry->d.address;
3809
3810
0
  return got_index;
3811
0
}
3812
3813
/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3814
   EXTERNAL is true if the relocation was originally against a global
3815
   symbol that binds locally.  */
3816
3817
static bfd_vma
3818
mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3819
          bfd_vma value, bool external)
3820
0
{
3821
0
  struct mips_got_entry *entry;
3822
3823
  /* GOT16 relocations against local symbols are followed by a LO16
3824
     relocation; those against global symbols are not.  Thus if the
3825
     symbol was originally local, the GOT16 relocation should load the
3826
     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3827
0
  if (! external)
3828
0
    value = mips_elf_high (value) << 16;
3829
3830
  /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3831
     R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3832
     same in all cases.  */
3833
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3834
0
             NULL, R_MIPS_GOT16);
3835
0
  if (entry)
3836
0
    return entry->gotidx;
3837
0
  else
3838
0
    return MINUS_ONE;
3839
0
}
3840
3841
/* Returns the offset for the entry at the INDEXth position
3842
   in the GOT.  */
3843
3844
static bfd_vma
3845
mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3846
        bfd *input_bfd, bfd_vma got_index)
3847
0
{
3848
0
  struct mips_elf_link_hash_table *htab;
3849
0
  asection *sgot;
3850
0
  bfd_vma gp;
3851
3852
0
  htab = mips_elf_hash_table (info);
3853
0
  BFD_ASSERT (htab != NULL);
3854
3855
0
  sgot = htab->root.sgot;
3856
0
  gp = _bfd_get_gp_value (output_bfd)
3857
0
    + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3858
3859
0
  return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3860
0
}
3861
3862
/* Create and return a local GOT entry for VALUE, which was calculated
3863
   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3864
   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3865
   instead.  */
3866
3867
static struct mips_got_entry *
3868
mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3869
         bfd *ibfd, bfd_vma value,
3870
         unsigned long r_symndx,
3871
         struct mips_elf_link_hash_entry *h,
3872
         int r_type)
3873
0
{
3874
0
  struct mips_got_entry lookup, *entry;
3875
0
  void **loc;
3876
0
  struct mips_got_info *g;
3877
0
  struct mips_elf_link_hash_table *htab;
3878
0
  bfd_vma gotidx;
3879
3880
0
  htab = mips_elf_hash_table (info);
3881
0
  BFD_ASSERT (htab != NULL);
3882
3883
0
  g = mips_elf_bfd_got (ibfd, false);
3884
0
  if (g == NULL)
3885
0
    {
3886
0
      g = mips_elf_bfd_got (abfd, false);
3887
0
      BFD_ASSERT (g != NULL);
3888
0
    }
3889
3890
  /* This function shouldn't be called for symbols that live in the global
3891
     area of the GOT.  */
3892
0
  BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3893
3894
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3895
0
  if (lookup.tls_type)
3896
0
    {
3897
0
      lookup.abfd = ibfd;
3898
0
      if (tls_ldm_reloc_p (r_type))
3899
0
  {
3900
0
    lookup.symndx = 0;
3901
0
    lookup.d.addend = 0;
3902
0
  }
3903
0
      else if (h == NULL)
3904
0
  {
3905
0
    lookup.symndx = r_symndx;
3906
0
    lookup.d.addend = 0;
3907
0
  }
3908
0
      else
3909
0
  {
3910
0
    lookup.symndx = -1;
3911
0
    lookup.d.h = h;
3912
0
  }
3913
3914
0
      entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3915
0
      BFD_ASSERT (entry);
3916
3917
0
      gotidx = entry->gotidx;
3918
0
      BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3919
3920
0
      return entry;
3921
0
    }
3922
3923
0
  lookup.abfd = NULL;
3924
0
  lookup.symndx = -1;
3925
0
  lookup.d.address = value;
3926
0
  loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3927
0
  if (!loc)
3928
0
    return NULL;
3929
3930
0
  entry = (struct mips_got_entry *) *loc;
3931
0
  if (entry)
3932
0
    return entry;
3933
3934
0
  if (g->assigned_low_gotno > g->assigned_high_gotno)
3935
0
    {
3936
      /* We didn't allocate enough space in the GOT.  */
3937
0
      _bfd_error_handler
3938
0
  (_("not enough GOT space for local GOT entries"));
3939
0
      bfd_set_error (bfd_error_bad_value);
3940
0
      return NULL;
3941
0
    }
3942
3943
0
  entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3944
0
  if (!entry)
3945
0
    return NULL;
3946
3947
0
  if (got16_reloc_p (r_type)
3948
0
      || call16_reloc_p (r_type)
3949
0
      || got_page_reloc_p (r_type)
3950
0
      || got_disp_reloc_p (r_type))
3951
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3952
0
  else
3953
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3954
3955
0
  *entry = lookup;
3956
0
  *loc = entry;
3957
3958
0
  MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3959
3960
  /* These GOT entries need a dynamic relocation on VxWorks.  */
3961
0
  if (htab->root.target_os == is_vxworks)
3962
0
    {
3963
0
      Elf_Internal_Rela outrel;
3964
0
      asection *s;
3965
0
      bfd_byte *rloc;
3966
0
      bfd_vma got_address;
3967
3968
0
      s = mips_elf_rel_dyn_section (info, false);
3969
0
      got_address = (htab->root.sgot->output_section->vma
3970
0
         + htab->root.sgot->output_offset
3971
0
         + entry->gotidx);
3972
3973
0
      rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3974
0
      outrel.r_offset = got_address;
3975
0
      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3976
0
      outrel.r_addend = value;
3977
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3978
0
    }
3979
3980
0
  return entry;
3981
0
}
3982
3983
/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3984
   The number might be exact or a worst-case estimate, depending on how
3985
   much information is available to elf_backend_omit_section_dynsym at
3986
   the current linking stage.  */
3987
3988
static bfd_size_type
3989
count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3990
0
{
3991
0
  bfd_size_type count;
3992
3993
0
  count = 0;
3994
0
  if (bfd_link_pic (info))
3995
0
    {
3996
0
      asection *p;
3997
0
      elf_backend_data *bed = get_elf_backend_data (output_bfd);
3998
3999
0
      for (p = output_bfd->sections; p ; p = p->next)
4000
0
  if ((p->flags & SEC_EXCLUDE) == 0
4001
0
      && (p->flags & SEC_ALLOC) != 0
4002
0
      && elf_hash_table (info)->dynamic_relocs
4003
0
      && !bed->elf_backend_omit_section_dynsym (info, p))
4004
0
    ++count;
4005
0
    }
4006
0
  return count;
4007
0
}
4008
4009
/* Sort the dynamic symbol table so that symbols that need GOT entries
4010
   appear towards the end.  */
4011
4012
static bool
4013
mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
4014
0
{
4015
0
  struct mips_elf_link_hash_table *htab;
4016
0
  struct mips_elf_hash_sort_data hsd;
4017
0
  struct mips_got_info *g;
4018
4019
0
  htab = mips_elf_hash_table (info);
4020
0
  BFD_ASSERT (htab != NULL);
4021
4022
0
  if (htab->root.dynsymcount <= 1)
4023
0
    return true;
4024
4025
0
  g = htab->got_info;
4026
0
  if (g == NULL)
4027
0
    return true;
4028
4029
0
  hsd.low = NULL;
4030
0
  hsd.max_unref_got_dynindx
4031
0
    = hsd.min_got_dynindx
4032
0
    = (htab->root.dynsymcount - g->reloc_only_gotno);
4033
  /* Add 1 to local symbol indices to account for the mandatory NULL entry
4034
     at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
4035
0
  hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
4036
0
  hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
4037
0
  hsd.output_bfd = abfd;
4038
0
  if (htab->root.dynobj != NULL
4039
0
      && htab->root.dynamic_sections_created
4040
0
      && info->emit_gnu_hash)
4041
0
    {
4042
0
      asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
4043
0
      BFD_ASSERT (s != NULL);
4044
0
      hsd.mipsxhash = s->contents;
4045
0
      BFD_ASSERT (hsd.mipsxhash != NULL);
4046
0
    }
4047
0
  else
4048
0
    hsd.mipsxhash = NULL;
4049
0
  mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
4050
4051
  /* There should have been enough room in the symbol table to
4052
     accommodate both the GOT and non-GOT symbols.  */
4053
0
  BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
4054
0
  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
4055
0
  BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
4056
0
  BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
4057
4058
  /* Now we know which dynamic symbol has the lowest dynamic symbol
4059
     table index in the GOT.  */
4060
0
  htab->global_gotsym = hsd.low;
4061
4062
0
  return true;
4063
0
}
4064
4065
/* If H needs a GOT entry, assign it the highest available dynamic
4066
   index.  Otherwise, assign it the lowest available dynamic
4067
   index.  */
4068
4069
static bool
4070
mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4071
0
{
4072
0
  struct mips_elf_hash_sort_data *hsd = data;
4073
4074
  /* Symbols without dynamic symbol table entries aren't interesting
4075
     at all.  */
4076
0
  if (h->root.dynindx == -1)
4077
0
    return true;
4078
4079
0
  switch (h->global_got_area)
4080
0
    {
4081
0
    case GGA_NONE:
4082
0
      if (h->root.forced_local)
4083
0
  h->root.dynindx = hsd->max_local_dynindx++;
4084
0
      else
4085
0
  h->root.dynindx = hsd->max_non_got_dynindx++;
4086
0
      break;
4087
4088
0
    case GGA_NORMAL:
4089
0
      h->root.dynindx = --hsd->min_got_dynindx;
4090
0
      hsd->low = (struct elf_link_hash_entry *) h;
4091
0
      break;
4092
4093
0
    case GGA_RELOC_ONLY:
4094
0
      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4095
0
  hsd->low = (struct elf_link_hash_entry *) h;
4096
0
      h->root.dynindx = hsd->max_unref_got_dynindx++;
4097
0
      break;
4098
0
    }
4099
4100
  /* Populate the .MIPS.xhash translation table entry with
4101
     the symbol dynindx.  */
4102
0
  if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4103
0
    bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4104
0
    hsd->mipsxhash + h->mipsxhash_loc);
4105
4106
0
  return true;
4107
0
}
4108
4109
/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4110
   (which is owned by the caller and shouldn't be added to the
4111
   hash table directly).  */
4112
4113
static bool
4114
mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4115
         struct mips_got_entry *lookup)
4116
0
{
4117
0
  struct mips_elf_link_hash_table *htab;
4118
0
  struct mips_got_entry *entry;
4119
0
  struct mips_got_info *g;
4120
0
  void **loc, **bfd_loc;
4121
4122
  /* Make sure there's a slot for this entry in the master GOT.  */
4123
0
  htab = mips_elf_hash_table (info);
4124
0
  g = htab->got_info;
4125
0
  loc = htab_find_slot (g->got_entries, lookup, INSERT);
4126
0
  if (!loc)
4127
0
    return false;
4128
4129
  /* Populate the entry if it isn't already.  */
4130
0
  entry = (struct mips_got_entry *) *loc;
4131
0
  if (!entry)
4132
0
    {
4133
0
      entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4134
0
      if (!entry)
4135
0
  return false;
4136
4137
0
      lookup->tls_initialized = false;
4138
0
      lookup->gotidx = -1;
4139
0
      *entry = *lookup;
4140
0
      *loc = entry;
4141
0
    }
4142
4143
  /* Reuse the same GOT entry for the BFD's GOT.  */
4144
0
  g = mips_elf_bfd_got (abfd, true);
4145
0
  if (!g)
4146
0
    return false;
4147
4148
0
  bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4149
0
  if (!bfd_loc)
4150
0
    return false;
4151
4152
0
  if (!*bfd_loc)
4153
0
    *bfd_loc = entry;
4154
0
  return true;
4155
0
}
4156
4157
/* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
4158
   entry for it.  FOR_CALL is true if the caller is only interested in
4159
   using the GOT entry for calls.  */
4160
4161
static bool
4162
mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4163
           bfd *abfd, struct bfd_link_info *info,
4164
           bool for_call, int r_type)
4165
0
{
4166
0
  struct mips_elf_link_hash_table *htab;
4167
0
  struct mips_elf_link_hash_entry *hmips;
4168
0
  struct mips_got_entry entry;
4169
0
  unsigned char tls_type;
4170
4171
0
  htab = mips_elf_hash_table (info);
4172
0
  BFD_ASSERT (htab != NULL);
4173
4174
0
  hmips = (struct mips_elf_link_hash_entry *) h;
4175
0
  if (!for_call)
4176
0
    hmips->got_only_for_calls = false;
4177
4178
  /* A global symbol in the GOT must also be in the dynamic symbol
4179
     table.  */
4180
0
  if (h->dynindx == -1)
4181
0
    {
4182
0
      switch (ELF_ST_VISIBILITY (h->other))
4183
0
  {
4184
0
  case STV_INTERNAL:
4185
0
  case STV_HIDDEN:
4186
0
    _bfd_mips_elf_hide_symbol (info, h, true);
4187
0
    break;
4188
0
  }
4189
0
      if (!bfd_elf_link_record_dynamic_symbol (info, h))
4190
0
  return false;
4191
0
    }
4192
4193
0
  tls_type = mips_elf_reloc_tls_type (r_type);
4194
0
  if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4195
0
    hmips->global_got_area = GGA_NORMAL;
4196
4197
0
  entry.abfd = abfd;
4198
0
  entry.symndx = -1;
4199
0
  entry.d.h = (struct mips_elf_link_hash_entry *) h;
4200
0
  entry.tls_type = tls_type;
4201
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4202
0
}
4203
4204
/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4205
   where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
4206
4207
static bool
4208
mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4209
          struct bfd_link_info *info, int r_type)
4210
0
{
4211
0
  struct mips_elf_link_hash_table *htab;
4212
0
  struct mips_got_info *g;
4213
0
  struct mips_got_entry entry;
4214
4215
0
  htab = mips_elf_hash_table (info);
4216
0
  BFD_ASSERT (htab != NULL);
4217
4218
0
  g = htab->got_info;
4219
0
  BFD_ASSERT (g != NULL);
4220
4221
0
  entry.abfd = abfd;
4222
0
  entry.symndx = symndx;
4223
0
  entry.d.addend = addend;
4224
0
  entry.tls_type = mips_elf_reloc_tls_type (r_type);
4225
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4226
0
}
4227
4228
/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4229
   H is the symbol's hash table entry, or null if SYMNDX is local
4230
   to ABFD.  */
4231
4232
static bool
4233
mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4234
            long symndx, struct elf_link_hash_entry *h,
4235
            bfd_signed_vma addend)
4236
0
{
4237
0
  struct mips_elf_link_hash_table *htab;
4238
0
  struct mips_got_info *g1, *g2;
4239
0
  struct mips_got_page_ref lookup, *entry;
4240
0
  void **loc, **bfd_loc;
4241
4242
0
  htab = mips_elf_hash_table (info);
4243
0
  BFD_ASSERT (htab != NULL);
4244
4245
0
  g1 = htab->got_info;
4246
0
  BFD_ASSERT (g1 != NULL);
4247
4248
0
  if (h)
4249
0
    {
4250
0
      lookup.symndx = -1;
4251
0
      lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4252
0
    }
4253
0
  else
4254
0
    {
4255
0
      lookup.symndx = symndx;
4256
0
      lookup.u.abfd = abfd;
4257
0
    }
4258
0
  lookup.addend = addend;
4259
0
  loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4260
0
  if (loc == NULL)
4261
0
    return false;
4262
4263
0
  entry = (struct mips_got_page_ref *) *loc;
4264
0
  if (!entry)
4265
0
    {
4266
0
      entry = bfd_alloc (abfd, sizeof (*entry));
4267
0
      if (!entry)
4268
0
  return false;
4269
4270
0
      *entry = lookup;
4271
0
      *loc = entry;
4272
0
    }
4273
4274
  /* Add the same entry to the BFD's GOT.  */
4275
0
  g2 = mips_elf_bfd_got (abfd, true);
4276
0
  if (!g2)
4277
0
    return false;
4278
4279
0
  bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4280
0
  if (!bfd_loc)
4281
0
    return false;
4282
4283
0
  if (!*bfd_loc)
4284
0
    *bfd_loc = entry;
4285
4286
0
  return true;
4287
0
}
4288
4289
/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4290
4291
static void
4292
mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4293
               unsigned int n)
4294
0
{
4295
0
  asection *s;
4296
0
  struct mips_elf_link_hash_table *htab;
4297
4298
0
  htab = mips_elf_hash_table (info);
4299
0
  BFD_ASSERT (htab != NULL);
4300
4301
0
  s = mips_elf_rel_dyn_section (info, false);
4302
0
  BFD_ASSERT (s != NULL);
4303
4304
0
  if (htab->root.target_os == is_vxworks)
4305
0
    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4306
0
  else
4307
0
    {
4308
0
      if (s->size == 0)
4309
0
  {
4310
    /* Make room for a null element.  */
4311
0
    s->size += MIPS_ELF_REL_SIZE (abfd);
4312
0
    ++s->reloc_count;
4313
0
  }
4314
0
      s->size += n * MIPS_ELF_REL_SIZE (abfd);
4315
0
    }
4316
0
}
4317

4318
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4319
   mips_elf_traverse_got_arg structure.  Count the number of GOT
4320
   entries and TLS relocs.  Set DATA->value to true if we need
4321
   to resolve indirect or warning symbols and then recreate the GOT.  */
4322
4323
static int
4324
mips_elf_check_recreate_got (void **entryp, void *data)
4325
0
{
4326
0
  struct mips_got_entry *entry;
4327
0
  struct mips_elf_traverse_got_arg *arg;
4328
4329
0
  entry = (struct mips_got_entry *) *entryp;
4330
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4331
0
  if (entry->abfd != NULL && entry->symndx == -1)
4332
0
    {
4333
0
      struct mips_elf_link_hash_entry *h;
4334
4335
0
      h = entry->d.h;
4336
0
      if (h->root.root.type == bfd_link_hash_indirect
4337
0
    || h->root.root.type == bfd_link_hash_warning)
4338
0
  {
4339
0
    arg->value = true;
4340
0
    return 0;
4341
0
  }
4342
0
    }
4343
0
  mips_elf_count_got_entry (arg->info, arg->g, entry);
4344
0
  return 1;
4345
0
}
4346
4347
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4348
   mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
4349
   converting entries for indirect and warning symbols into entries
4350
   for the target symbol.  Set DATA->g to null on error.  */
4351
4352
static int
4353
mips_elf_recreate_got (void **entryp, void *data)
4354
0
{
4355
0
  struct mips_got_entry new_entry, *entry;
4356
0
  struct mips_elf_traverse_got_arg *arg;
4357
0
  void **slot;
4358
4359
0
  entry = (struct mips_got_entry *) *entryp;
4360
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4361
0
  if (entry->abfd != NULL
4362
0
      && entry->symndx == -1
4363
0
      && (entry->d.h->root.root.type == bfd_link_hash_indirect
4364
0
    || entry->d.h->root.root.type == bfd_link_hash_warning))
4365
0
    {
4366
0
      struct mips_elf_link_hash_entry *h;
4367
4368
0
      new_entry = *entry;
4369
0
      entry = &new_entry;
4370
0
      h = entry->d.h;
4371
0
      do
4372
0
  {
4373
0
    BFD_ASSERT (h->global_got_area == GGA_NONE);
4374
0
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4375
0
  }
4376
0
      while (h->root.root.type == bfd_link_hash_indirect
4377
0
       || h->root.root.type == bfd_link_hash_warning);
4378
0
      entry->d.h = h;
4379
0
    }
4380
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4381
0
  if (slot == NULL)
4382
0
    {
4383
0
      arg->g = NULL;
4384
0
      return 0;
4385
0
    }
4386
0
  if (*slot == NULL)
4387
0
    {
4388
0
      if (entry == &new_entry)
4389
0
  {
4390
0
    entry = bfd_alloc (entry->abfd, sizeof (*entry));
4391
0
    if (!entry)
4392
0
      {
4393
0
        arg->g = NULL;
4394
0
        return 0;
4395
0
      }
4396
0
    *entry = new_entry;
4397
0
  }
4398
0
      *slot = entry;
4399
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4400
0
    }
4401
0
  return 1;
4402
0
}
4403
4404
/* Return the maximum number of GOT page entries required for RANGE.  */
4405
4406
static bfd_vma
4407
mips_elf_pages_for_range (const struct mips_got_page_range *range)
4408
0
{
4409
0
  return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4410
0
}
4411
4412
/* Record that G requires a page entry that can reach SEC + ADDEND.  */
4413
4414
static bool
4415
mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4416
        asection *sec, bfd_signed_vma addend)
4417
0
{
4418
0
  struct mips_got_info *g = arg->g;
4419
0
  struct mips_got_page_entry lookup, *entry;
4420
0
  struct mips_got_page_range **range_ptr, *range;
4421
0
  bfd_vma old_pages, new_pages;
4422
0
  void **loc;
4423
4424
  /* Find the mips_got_page_entry hash table entry for this section.  */
4425
0
  lookup.sec = sec;
4426
0
  loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4427
0
  if (loc == NULL)
4428
0
    return false;
4429
4430
  /* Create a mips_got_page_entry if this is the first time we've
4431
     seen the section.  */
4432
0
  entry = (struct mips_got_page_entry *) *loc;
4433
0
  if (!entry)
4434
0
    {
4435
0
      entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4436
0
      if (!entry)
4437
0
  return false;
4438
4439
0
      entry->sec = sec;
4440
0
      *loc = entry;
4441
0
    }
4442
4443
  /* Skip over ranges whose maximum extent cannot share a page entry
4444
     with ADDEND.  */
4445
0
  range_ptr = &entry->ranges;
4446
0
  while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4447
0
    range_ptr = &(*range_ptr)->next;
4448
4449
  /* If we scanned to the end of the list, or found a range whose
4450
     minimum extent cannot share a page entry with ADDEND, create
4451
     a new singleton range.  */
4452
0
  range = *range_ptr;
4453
0
  if (!range || addend < range->min_addend - 0xffff)
4454
0
    {
4455
0
      range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4456
0
      if (!range)
4457
0
  return false;
4458
4459
0
      range->next = *range_ptr;
4460
0
      range->min_addend = addend;
4461
0
      range->max_addend = addend;
4462
4463
0
      *range_ptr = range;
4464
0
      entry->num_pages++;
4465
0
      g->page_gotno++;
4466
0
      return true;
4467
0
    }
4468
4469
  /* Remember how many pages the old range contributed.  */
4470
0
  old_pages = mips_elf_pages_for_range (range);
4471
4472
  /* Update the ranges.  */
4473
0
  if (addend < range->min_addend)
4474
0
    range->min_addend = addend;
4475
0
  else if (addend > range->max_addend)
4476
0
    {
4477
0
      if (range->next && addend >= range->next->min_addend - 0xffff)
4478
0
  {
4479
0
    old_pages += mips_elf_pages_for_range (range->next);
4480
0
    range->max_addend = range->next->max_addend;
4481
0
    range->next = range->next->next;
4482
0
  }
4483
0
      else
4484
0
  range->max_addend = addend;
4485
0
    }
4486
4487
  /* Record any change in the total estimate.  */
4488
0
  new_pages = mips_elf_pages_for_range (range);
4489
0
  if (old_pages != new_pages)
4490
0
    {
4491
0
      entry->num_pages += new_pages - old_pages;
4492
0
      g->page_gotno += new_pages - old_pages;
4493
0
    }
4494
4495
0
  return true;
4496
0
}
4497
4498
/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4499
   and for which DATA points to a mips_elf_traverse_got_arg.  Work out
4500
   whether the page reference described by *REFP needs a GOT page entry,
4501
   and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
4502
4503
static int
4504
mips_elf_resolve_got_page_ref (void **refp, void *data)
4505
0
{
4506
0
  struct mips_got_page_ref *ref;
4507
0
  struct mips_elf_traverse_got_arg *arg;
4508
0
  struct mips_elf_link_hash_table *htab;
4509
0
  asection *sec;
4510
0
  bfd_vma addend;
4511
4512
0
  ref = (struct mips_got_page_ref *) *refp;
4513
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4514
0
  htab = mips_elf_hash_table (arg->info);
4515
4516
0
  if (ref->symndx < 0)
4517
0
    {
4518
0
      struct mips_elf_link_hash_entry *h;
4519
4520
      /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
4521
0
      h = ref->u.h;
4522
0
      if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4523
0
  return 1;
4524
4525
      /* Ignore undefined symbols; we'll issue an error later if
4526
   appropriate.  */
4527
0
      if (!((h->root.root.type == bfd_link_hash_defined
4528
0
       || h->root.root.type == bfd_link_hash_defweak)
4529
0
      && h->root.root.u.def.section))
4530
0
  return 1;
4531
4532
0
      sec = h->root.root.u.def.section;
4533
0
      addend = h->root.root.u.def.value + ref->addend;
4534
0
    }
4535
0
  else
4536
0
    {
4537
0
      Elf_Internal_Sym *isym;
4538
4539
      /* Read in the symbol.  */
4540
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4541
0
            ref->symndx);
4542
0
      if (isym == NULL)
4543
0
  {
4544
0
    arg->g = NULL;
4545
0
    return 0;
4546
0
  }
4547
4548
      /* Get the associated input section.  */
4549
0
      sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4550
0
      if (sec == NULL)
4551
0
  {
4552
0
    arg->g = NULL;
4553
0
    return 0;
4554
0
  }
4555
4556
      /* If this is a mergable section, work out the section and offset
4557
   of the merged data.  For section symbols, the addend specifies
4558
   of the offset _of_ the first byte in the data, otherwise it
4559
   specifies the offset _from_ the first byte.  */
4560
0
      if (sec->flags & SEC_MERGE)
4561
0
  {
4562
0
    if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4563
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec,
4564
0
             isym->st_value + ref->addend);
4565
0
    else
4566
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec,
4567
0
             isym->st_value) + ref->addend;
4568
0
  }
4569
0
      else
4570
0
  addend = isym->st_value + ref->addend;
4571
0
    }
4572
0
  if (!mips_elf_record_got_page_entry (arg, sec, addend))
4573
0
    {
4574
0
      arg->g = NULL;
4575
0
      return 0;
4576
0
    }
4577
0
  return 1;
4578
0
}
4579
4580
/* If any entries in G->got_entries are for indirect or warning symbols,
4581
   replace them with entries for the target symbol.  Convert g->got_page_refs
4582
   into got_page_entry structures and estimate the number of page entries
4583
   that they require.  */
4584
4585
static bool
4586
mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4587
            struct mips_got_info *g)
4588
0
{
4589
0
  struct mips_elf_traverse_got_arg tga;
4590
0
  struct mips_got_info oldg;
4591
4592
0
  oldg = *g;
4593
4594
0
  tga.info = info;
4595
0
  tga.g = g;
4596
0
  tga.value = false;
4597
0
  htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4598
0
  if (tga.value)
4599
0
    {
4600
0
      *g = oldg;
4601
0
      g->got_entries = htab_create (htab_size (oldg.got_entries),
4602
0
            mips_elf_got_entry_hash,
4603
0
            mips_elf_got_entry_eq, NULL);
4604
0
      if (!g->got_entries)
4605
0
  return false;
4606
4607
0
      htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4608
0
      if (!tga.g)
4609
0
  return false;
4610
4611
0
      htab_delete (oldg.got_entries);
4612
0
    }
4613
4614
0
  g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4615
0
           mips_got_page_entry_eq, NULL);
4616
0
  if (g->got_page_entries == NULL)
4617
0
    return false;
4618
4619
0
  tga.info = info;
4620
0
  tga.g = g;
4621
0
  htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4622
4623
0
  return true;
4624
0
}
4625
4626
/* Return true if a GOT entry for H should live in the local rather than
4627
   global GOT area.  */
4628
4629
static bool
4630
mips_use_local_got_p (struct bfd_link_info *info,
4631
          struct mips_elf_link_hash_entry *h)
4632
0
{
4633
  /* Symbols that aren't in the dynamic symbol table must live in the
4634
     local GOT.  This includes symbols that are completely undefined
4635
     and which therefore don't bind locally.  We'll report undefined
4636
     symbols later if appropriate.  */
4637
0
  if (h->root.dynindx == -1)
4638
0
    return true;
4639
4640
  /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4641
     to the local GOT, as they would be implicitly relocated by the
4642
     base address by the dynamic loader.  */
4643
0
  if (bfd_is_abs_symbol (&h->root.root))
4644
0
    return false;
4645
4646
  /* Symbols that bind locally can (and in the case of forced-local
4647
     symbols, must) live in the local GOT.  */
4648
0
  if (h->got_only_for_calls
4649
0
      ? SYMBOL_CALLS_LOCAL (info, &h->root)
4650
0
      : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4651
0
    return true;
4652
4653
  /* If this is an executable that must provide a definition of the symbol,
4654
     either though PLTs or copy relocations, then that address should go in
4655
     the local rather than global GOT.  */
4656
0
  if (bfd_link_executable (info) && h->has_static_relocs)
4657
0
    return true;
4658
4659
0
  return false;
4660
0
}
4661
4662
/* A mips_elf_link_hash_traverse callback for which DATA points to the
4663
   link_info structure.  Decide whether the hash entry needs an entry in
4664
   the global part of the primary GOT, setting global_got_area accordingly.
4665
   Count the number of global symbols that are in the primary GOT only
4666
   because they have relocations against them (reloc_only_gotno).  */
4667
4668
static bool
4669
mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4670
0
{
4671
0
  struct bfd_link_info *info;
4672
0
  struct mips_elf_link_hash_table *htab;
4673
0
  struct mips_got_info *g;
4674
4675
0
  info = (struct bfd_link_info *) data;
4676
0
  htab = mips_elf_hash_table (info);
4677
0
  g = htab->got_info;
4678
0
  if (h->global_got_area != GGA_NONE)
4679
0
    {
4680
      /* Make a final decision about whether the symbol belongs in the
4681
   local or global GOT.  */
4682
0
      if (mips_use_local_got_p (info, h))
4683
  /* The symbol belongs in the local GOT.  We no longer need this
4684
     entry if it was only used for relocations; those relocations
4685
     will be against the null or section symbol instead of H.  */
4686
0
  h->global_got_area = GGA_NONE;
4687
0
      else if (htab->root.target_os == is_vxworks
4688
0
         && h->got_only_for_calls
4689
0
         && h->root.plt.plist->mips_offset != MINUS_ONE)
4690
  /* On VxWorks, calls can refer directly to the .got.plt entry;
4691
     they don't need entries in the regular GOT.  .got.plt entries
4692
     will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
4693
0
  h->global_got_area = GGA_NONE;
4694
0
      else if (h->global_got_area == GGA_RELOC_ONLY)
4695
0
  {
4696
0
    g->reloc_only_gotno++;
4697
0
    g->global_gotno++;
4698
0
  }
4699
0
    }
4700
0
  return 1;
4701
0
}
4702

4703
/* A htab_traverse callback for GOT entries.  Add each one to the GOT
4704
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4705
4706
static int
4707
mips_elf_add_got_entry (void **entryp, void *data)
4708
0
{
4709
0
  struct mips_got_entry *entry;
4710
0
  struct mips_elf_traverse_got_arg *arg;
4711
0
  void **slot;
4712
4713
0
  entry = (struct mips_got_entry *) *entryp;
4714
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4715
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4716
0
  if (!slot)
4717
0
    {
4718
0
      arg->g = NULL;
4719
0
      return 0;
4720
0
    }
4721
0
  if (!*slot)
4722
0
    {
4723
0
      *slot = entry;
4724
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4725
0
    }
4726
0
  return 1;
4727
0
}
4728
4729
/* A htab_traverse callback for GOT page entries.  Add each one to the GOT
4730
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4731
4732
static int
4733
mips_elf_add_got_page_entry (void **entryp, void *data)
4734
0
{
4735
0
  struct mips_got_page_entry *entry;
4736
0
  struct mips_elf_traverse_got_arg *arg;
4737
0
  void **slot;
4738
4739
0
  entry = (struct mips_got_page_entry *) *entryp;
4740
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4741
0
  slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4742
0
  if (!slot)
4743
0
    {
4744
0
      arg->g = NULL;
4745
0
      return 0;
4746
0
    }
4747
0
  if (!*slot)
4748
0
    {
4749
0
      *slot = entry;
4750
0
      arg->g->page_gotno += entry->num_pages;
4751
0
    }
4752
0
  return 1;
4753
0
}
4754
4755
/* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
4756
   this would lead to overflow, 1 if they were merged successfully,
4757
   and 0 if a merge failed due to lack of memory.  (These values are chosen
4758
   so that nonnegative return values can be returned by a htab_traverse
4759
   callback.)  */
4760
4761
static int
4762
mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4763
       struct mips_got_info *to,
4764
       struct mips_elf_got_per_bfd_arg *arg)
4765
0
{
4766
0
  struct mips_elf_traverse_got_arg tga;
4767
0
  unsigned int estimate;
4768
4769
  /* Work out how many page entries we would need for the combined GOT.  */
4770
0
  estimate = arg->max_pages;
4771
0
  if (estimate >= from->page_gotno + to->page_gotno)
4772
0
    estimate = from->page_gotno + to->page_gotno;
4773
4774
  /* And conservatively estimate how many local and TLS entries
4775
     would be needed.  */
4776
0
  estimate += from->local_gotno + to->local_gotno;
4777
0
  estimate += from->tls_gotno + to->tls_gotno;
4778
4779
  /* If we're merging with the primary got, any TLS relocations will
4780
     come after the full set of global entries.  Otherwise estimate those
4781
     conservatively as well.  */
4782
0
  if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4783
0
    estimate += arg->global_count;
4784
0
  else
4785
0
    estimate += from->global_gotno + to->global_gotno;
4786
4787
  /* Bail out if the combined GOT might be too big.  */
4788
0
  if (estimate > arg->max_count)
4789
0
    return -1;
4790
4791
  /* Transfer the bfd's got information from FROM to TO.  */
4792
0
  tga.info = arg->info;
4793
0
  tga.g = to;
4794
0
  htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4795
0
  if (!tga.g)
4796
0
    return 0;
4797
4798
0
  htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4799
0
  if (!tga.g)
4800
0
    return 0;
4801
4802
0
  mips_elf_replace_bfd_got (abfd, to);
4803
0
  return 1;
4804
0
}
4805
4806
/* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
4807
   as possible of the primary got, since it doesn't require explicit
4808
   dynamic relocations, but don't use bfds that would reference global
4809
   symbols out of the addressable range.  Failing the primary got,
4810
   attempt to merge with the current got, or finish the current got
4811
   and then make make the new got current.  */
4812
4813
static bool
4814
mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4815
        struct mips_elf_got_per_bfd_arg *arg)
4816
0
{
4817
0
  unsigned int estimate;
4818
0
  int result;
4819
4820
0
  if (!mips_elf_resolve_final_got_entries (arg->info, g))
4821
0
    return false;
4822
4823
  /* Work out the number of page, local and TLS entries.  */
4824
0
  estimate = arg->max_pages;
4825
0
  if (estimate > g->page_gotno)
4826
0
    estimate = g->page_gotno;
4827
0
  estimate += g->local_gotno + g->tls_gotno;
4828
4829
  /* We place TLS GOT entries after both locals and globals.  The globals
4830
     for the primary GOT may overflow the normal GOT size limit, so be
4831
     sure not to merge a GOT which requires TLS with the primary GOT in that
4832
     case.  This doesn't affect non-primary GOTs.  */
4833
0
  estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4834
4835
0
  if (estimate <= arg->max_count)
4836
0
    {
4837
      /* If we don't have a primary GOT, use it as
4838
   a starting point for the primary GOT.  */
4839
0
      if (!arg->primary)
4840
0
  {
4841
0
    arg->primary = g;
4842
0
    return true;
4843
0
  }
4844
4845
      /* Try merging with the primary GOT.  */
4846
0
      result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4847
0
      if (result >= 0)
4848
0
  return result;
4849
0
    }
4850
4851
  /* If we can merge with the last-created got, do it.  */
4852
0
  if (arg->current)
4853
0
    {
4854
0
      result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4855
0
      if (result >= 0)
4856
0
  return result;
4857
0
    }
4858
4859
  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4860
     fits; if it turns out that it doesn't, we'll get relocation
4861
     overflows anyway.  */
4862
0
  g->next = arg->current;
4863
0
  arg->current = g;
4864
4865
0
  return true;
4866
0
}
4867
4868
/* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
4869
   to GOTIDX, duplicating the entry if it has already been assigned
4870
   an index in a different GOT.  */
4871
4872
static bool
4873
mips_elf_set_gotidx (void **entryp, long gotidx)
4874
0
{
4875
0
  struct mips_got_entry *entry;
4876
4877
0
  entry = (struct mips_got_entry *) *entryp;
4878
0
  if (entry->gotidx > 0)
4879
0
    {
4880
0
      struct mips_got_entry *new_entry;
4881
4882
0
      new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4883
0
      if (!new_entry)
4884
0
  return false;
4885
4886
0
      *new_entry = *entry;
4887
0
      *entryp = new_entry;
4888
0
      entry = new_entry;
4889
0
    }
4890
0
  entry->gotidx = gotidx;
4891
0
  return true;
4892
0
}
4893
4894
/* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
4895
   mips_elf_traverse_got_arg in which DATA->value is the size of one
4896
   GOT entry.  Set DATA->g to null on failure.  */
4897
4898
static int
4899
mips_elf_initialize_tls_index (void **entryp, void *data)
4900
0
{
4901
0
  struct mips_got_entry *entry;
4902
0
  struct mips_elf_traverse_got_arg *arg;
4903
4904
  /* We're only interested in TLS symbols.  */
4905
0
  entry = (struct mips_got_entry *) *entryp;
4906
0
  if (entry->tls_type == GOT_TLS_NONE)
4907
0
    return 1;
4908
4909
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4910
0
  if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4911
0
    {
4912
0
      arg->g = NULL;
4913
0
      return 0;
4914
0
    }
4915
4916
  /* Account for the entries we've just allocated.  */
4917
0
  arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4918
0
  return 1;
4919
0
}
4920
4921
/* A htab_traverse callback for GOT entries, where DATA points to a
4922
   mips_elf_traverse_got_arg.  Set the global_got_area of each global
4923
   symbol to DATA->value.  */
4924
4925
static int
4926
mips_elf_set_global_got_area (void **entryp, void *data)
4927
0
{
4928
0
  struct mips_got_entry *entry;
4929
0
  struct mips_elf_traverse_got_arg *arg;
4930
4931
0
  entry = (struct mips_got_entry *) *entryp;
4932
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4933
0
  if (entry->abfd != NULL
4934
0
      && entry->symndx == -1
4935
0
      && entry->d.h->global_got_area != GGA_NONE)
4936
0
    entry->d.h->global_got_area = arg->value;
4937
0
  return 1;
4938
0
}
4939
4940
/* A htab_traverse callback for secondary GOT entries, where DATA points
4941
   to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
4942
   and record the number of relocations they require.  DATA->value is
4943
   the size of one GOT entry.  Set DATA->g to null on failure.  */
4944
4945
static int
4946
mips_elf_set_global_gotidx (void **entryp, void *data)
4947
0
{
4948
0
  struct mips_got_entry *entry;
4949
0
  struct mips_elf_traverse_got_arg *arg;
4950
4951
0
  entry = (struct mips_got_entry *) *entryp;
4952
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4953
0
  if (entry->abfd != NULL
4954
0
      && entry->symndx == -1
4955
0
      && entry->d.h->global_got_area != GGA_NONE)
4956
0
    {
4957
0
      if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4958
0
  {
4959
0
    arg->g = NULL;
4960
0
    return 0;
4961
0
  }
4962
0
      arg->g->assigned_low_gotno += 1;
4963
4964
0
      if (bfd_link_pic (arg->info)
4965
0
    || (elf_hash_table (arg->info)->dynamic_sections_created
4966
0
        && entry->d.h->root.def_dynamic
4967
0
        && !entry->d.h->root.def_regular))
4968
0
  arg->g->relocs += 1;
4969
0
    }
4970
4971
0
  return 1;
4972
0
}
4973
4974
/* A htab_traverse callback for GOT entries for which DATA is the
4975
   bfd_link_info.  Forbid any global symbols from having traditional
4976
   lazy-binding stubs.  */
4977
4978
static int
4979
mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4980
0
{
4981
0
  struct bfd_link_info *info;
4982
0
  struct mips_elf_link_hash_table *htab;
4983
0
  struct mips_got_entry *entry;
4984
4985
0
  entry = (struct mips_got_entry *) *entryp;
4986
0
  info = (struct bfd_link_info *) data;
4987
0
  htab = mips_elf_hash_table (info);
4988
0
  BFD_ASSERT (htab != NULL);
4989
4990
0
  if (entry->abfd != NULL
4991
0
      && entry->symndx == -1
4992
0
      && entry->d.h->needs_lazy_stub)
4993
0
    {
4994
0
      entry->d.h->needs_lazy_stub = false;
4995
0
      htab->lazy_stub_count--;
4996
0
    }
4997
4998
0
  return 1;
4999
0
}
5000
5001
/* Return the offset of an input bfd IBFD's GOT from the beginning of
5002
   the primary GOT.  */
5003
static bfd_vma
5004
mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
5005
0
{
5006
0
  if (!g->next)
5007
0
    return 0;
5008
5009
0
  g = mips_elf_bfd_got (ibfd, false);
5010
0
  if (! g)
5011
0
    return 0;
5012
5013
0
  BFD_ASSERT (g->next);
5014
5015
0
  g = g->next;
5016
5017
0
  return (g->local_gotno + g->global_gotno + g->tls_gotno)
5018
0
    * MIPS_ELF_GOT_SIZE (abfd);
5019
0
}
5020
5021
/* Turn a single GOT that is too big for 16-bit addressing into
5022
   a sequence of GOTs, each one 16-bit addressable.  */
5023
5024
static bool
5025
mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
5026
        asection *got, bfd_size_type pages)
5027
0
{
5028
0
  struct mips_elf_link_hash_table *htab;
5029
0
  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
5030
0
  struct mips_elf_traverse_got_arg tga;
5031
0
  struct mips_got_info *g, *gg;
5032
0
  unsigned int assign, needed_relocs;
5033
0
  bfd *dynobj, *ibfd;
5034
5035
0
  dynobj = elf_hash_table (info)->dynobj;
5036
0
  htab = mips_elf_hash_table (info);
5037
0
  BFD_ASSERT (htab != NULL);
5038
5039
0
  g = htab->got_info;
5040
5041
0
  got_per_bfd_arg.obfd = abfd;
5042
0
  got_per_bfd_arg.info = info;
5043
0
  got_per_bfd_arg.current = NULL;
5044
0
  got_per_bfd_arg.primary = NULL;
5045
0
  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
5046
0
        / MIPS_ELF_GOT_SIZE (abfd))
5047
0
             - htab->reserved_gotno);
5048
0
  got_per_bfd_arg.max_pages = pages;
5049
  /* The number of globals that will be included in the primary GOT.
5050
     See the calls to mips_elf_set_global_got_area below for more
5051
     information.  */
5052
0
  got_per_bfd_arg.global_count = g->global_gotno;
5053
5054
  /* Try to merge the GOTs of input bfds together, as long as they
5055
     don't seem to exceed the maximum GOT size, choosing one of them
5056
     to be the primary GOT.  */
5057
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
5058
0
    {
5059
0
      gg = mips_elf_bfd_got (ibfd, false);
5060
0
      if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5061
0
  return false;
5062
0
    }
5063
5064
  /* If we do not find any suitable primary GOT, create an empty one.  */
5065
0
  if (got_per_bfd_arg.primary == NULL)
5066
0
    g->next = mips_elf_create_got_info (abfd);
5067
0
  else
5068
0
    g->next = got_per_bfd_arg.primary;
5069
0
  g->next->next = got_per_bfd_arg.current;
5070
5071
  /* GG is now the master GOT, and G is the primary GOT.  */
5072
0
  gg = g;
5073
0
  g = g->next;
5074
5075
  /* Map the output bfd to the primary got.  That's what we're going
5076
     to use for bfds that use GOT16 or GOT_PAGE relocations that we
5077
     didn't mark in check_relocs, and we want a quick way to find it.
5078
     We can't just use gg->next because we're going to reverse the
5079
     list.  */
5080
0
  mips_elf_replace_bfd_got (abfd, g);
5081
5082
  /* Every symbol that is referenced in a dynamic relocation must be
5083
     present in the primary GOT, so arrange for them to appear after
5084
     those that are actually referenced.  */
5085
0
  gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5086
0
  g->global_gotno = gg->global_gotno;
5087
5088
0
  tga.info = info;
5089
0
  tga.value = GGA_RELOC_ONLY;
5090
0
  htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5091
0
  tga.value = GGA_NORMAL;
5092
0
  htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5093
5094
  /* Now go through the GOTs assigning them offset ranges.
5095
     [assigned_low_gotno, local_gotno[ will be set to the range of local
5096
     entries in each GOT.  We can then compute the end of a GOT by
5097
     adding local_gotno to global_gotno.  We reverse the list and make
5098
     it circular since then we'll be able to quickly compute the
5099
     beginning of a GOT, by computing the end of its predecessor.  To
5100
     avoid special cases for the primary GOT, while still preserving
5101
     assertions that are valid for both single- and multi-got links,
5102
     we arrange for the main got struct to have the right number of
5103
     global entries, but set its local_gotno such that the initial
5104
     offset of the primary GOT is zero.  Remember that the primary GOT
5105
     will become the last item in the circular linked list, so it
5106
     points back to the master GOT.  */
5107
0
  gg->local_gotno = -g->global_gotno;
5108
0
  gg->global_gotno = g->global_gotno;
5109
0
  gg->tls_gotno = 0;
5110
0
  assign = 0;
5111
0
  gg->next = gg;
5112
5113
0
  do
5114
0
    {
5115
0
      struct mips_got_info *gn;
5116
5117
0
      assign += htab->reserved_gotno;
5118
0
      g->assigned_low_gotno = assign;
5119
0
      g->local_gotno += assign;
5120
0
      g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5121
0
      g->assigned_high_gotno = g->local_gotno - 1;
5122
0
      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5123
5124
      /* Take g out of the direct list, and push it onto the reversed
5125
   list that gg points to.  g->next is guaranteed to be nonnull after
5126
   this operation, as required by mips_elf_initialize_tls_index. */
5127
0
      gn = g->next;
5128
0
      g->next = gg->next;
5129
0
      gg->next = g;
5130
5131
      /* Set up any TLS entries.  We always place the TLS entries after
5132
   all non-TLS entries.  */
5133
0
      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5134
0
      tga.g = g;
5135
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5136
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5137
0
      if (!tga.g)
5138
0
  return false;
5139
0
      BFD_ASSERT (g->tls_assigned_gotno == assign);
5140
5141
      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
5142
0
      g = gn;
5143
5144
      /* Forbid global symbols in every non-primary GOT from having
5145
   lazy-binding stubs.  */
5146
0
      if (g)
5147
0
  htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5148
0
    }
5149
0
  while (g);
5150
5151
0
  got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5152
5153
0
  needed_relocs = 0;
5154
0
  for (g = gg->next; g && g->next != gg; g = g->next)
5155
0
    {
5156
0
      unsigned int save_assign;
5157
5158
      /* Assign offsets to global GOT entries and count how many
5159
   relocations they need.  */
5160
0
      save_assign = g->assigned_low_gotno;
5161
0
      g->assigned_low_gotno = g->local_gotno;
5162
0
      tga.info = info;
5163
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5164
0
      tga.g = g;
5165
0
      htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5166
0
      if (!tga.g)
5167
0
  return false;
5168
0
      BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5169
0
      g->assigned_low_gotno = save_assign;
5170
5171
0
      if (bfd_link_pic (info))
5172
0
  {
5173
0
    g->relocs += g->local_gotno - g->assigned_low_gotno;
5174
0
    BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5175
0
          + g->next->global_gotno
5176
0
          + g->next->tls_gotno
5177
0
          + htab->reserved_gotno);
5178
0
  }
5179
0
      needed_relocs += g->relocs;
5180
0
    }
5181
0
  needed_relocs += g->relocs;
5182
5183
0
  if (needed_relocs)
5184
0
    mips_elf_allocate_dynamic_relocations (dynobj, info,
5185
0
             needed_relocs);
5186
5187
0
  return true;
5188
0
}
5189
5190

5191
/* Returns the first relocation of type r_type found, beginning with
5192
   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5193
5194
static const Elf_Internal_Rela *
5195
mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5196
        const Elf_Internal_Rela *relocation,
5197
        const Elf_Internal_Rela *relend)
5198
0
{
5199
0
  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5200
5201
0
  while (relocation < relend)
5202
0
    {
5203
0
      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5204
0
    && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5205
0
  return relocation;
5206
5207
0
      ++relocation;
5208
0
    }
5209
5210
  /* We didn't find it.  */
5211
0
  return NULL;
5212
0
}
5213
5214
/* Return whether an input relocation is against a local symbol.  */
5215
5216
static bool
5217
mips_elf_local_relocation_p (bfd *input_bfd,
5218
           const Elf_Internal_Rela *relocation,
5219
           asection **local_sections)
5220
0
{
5221
0
  unsigned long r_symndx;
5222
0
  Elf_Internal_Shdr *symtab_hdr;
5223
0
  size_t extsymoff;
5224
5225
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5226
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5227
0
  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5228
5229
0
  if (r_symndx < extsymoff)
5230
0
    return true;
5231
0
  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5232
0
    return true;
5233
5234
0
  return false;
5235
0
}
5236

5237
/* Return non-zero if the indicated VALUE has overflowed the maximum
5238
   range expressible by a signed number with the indicated number of
5239
   BITS.  */
5240
5241
static bool
5242
mips_elf_overflow_p (bfd_vma value, int bits)
5243
0
{
5244
0
  bfd_signed_vma svalue = (bfd_signed_vma) value;
5245
5246
0
  if (svalue > (1 << (bits - 1)) - 1)
5247
    /* The value is too big.  */
5248
0
    return true;
5249
0
  else if (svalue < -(1 << (bits - 1)))
5250
    /* The value is too small.  */
5251
0
    return true;
5252
5253
  /* All is well.  */
5254
0
  return false;
5255
0
}
5256
5257
/* Calculate the %high function.  */
5258
5259
static bfd_vma
5260
mips_elf_high (bfd_vma value)
5261
0
{
5262
0
  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5263
0
}
5264
5265
/* Calculate the %higher function.  */
5266
5267
static bfd_vma
5268
mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5269
0
{
5270
0
#ifdef BFD64
5271
0
  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5272
#else
5273
  abort ();
5274
  return MINUS_ONE;
5275
#endif
5276
0
}
5277
5278
/* Calculate the %highest function.  */
5279
5280
static bfd_vma
5281
mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5282
0
{
5283
0
#ifdef BFD64
5284
0
  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5285
#else
5286
  abort ();
5287
  return MINUS_ONE;
5288
#endif
5289
0
}
5290

5291
/* Create the .compact_rel section.  */
5292
5293
static bool
5294
mips_elf_create_compact_rel_section
5295
  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5296
0
{
5297
0
  flagword flags;
5298
0
  register asection *s;
5299
5300
0
  if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5301
0
    {
5302
0
      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5303
0
         | SEC_READONLY);
5304
5305
0
      s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5306
0
      if (s == NULL
5307
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5308
0
  return false;
5309
5310
0
      s->size = sizeof (Elf32_External_compact_rel);
5311
0
    }
5312
5313
0
  return true;
5314
0
}
5315
5316
/* Create the .got section to hold the global offset table.  */
5317
5318
static bool
5319
mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5320
0
{
5321
0
  flagword flags;
5322
0
  register asection *s;
5323
0
  struct elf_link_hash_entry *h;
5324
0
  struct bfd_link_hash_entry *bh;
5325
0
  struct mips_elf_link_hash_table *htab;
5326
5327
0
  htab = mips_elf_hash_table (info);
5328
0
  BFD_ASSERT (htab != NULL);
5329
5330
  /* This function may be called more than once.  */
5331
0
  if (htab->root.sgot)
5332
0
    return true;
5333
5334
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5335
0
     | SEC_LINKER_CREATED);
5336
5337
  /* We have to use an alignment of 2**4 here because this is hardcoded
5338
     in the function stub generation and in the linker script.  */
5339
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5340
0
  if (s == NULL
5341
0
      || !bfd_set_section_alignment (s, 4))
5342
0
    return false;
5343
0
  htab->root.sgot = s;
5344
5345
  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5346
     linker script because we don't want to define the symbol if we
5347
     are not creating a global offset table.  */
5348
0
  bh = NULL;
5349
0
  if (! (_bfd_generic_link_add_one_symbol
5350
0
   (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5351
0
    0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5352
0
    return false;
5353
5354
0
  h = (struct elf_link_hash_entry *) bh;
5355
0
  h->non_elf = 0;
5356
0
  h->def_regular = 1;
5357
0
  h->type = STT_OBJECT;
5358
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5359
0
  elf_hash_table (info)->hgot = h;
5360
5361
0
  if (bfd_link_pic (info)
5362
0
      && ! bfd_elf_link_record_dynamic_symbol (info, h))
5363
0
    return false;
5364
5365
0
  htab->got_info = mips_elf_create_got_info (abfd);
5366
0
  mips_elf_section_data (s)->elf.this_hdr.sh_flags
5367
0
    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5368
5369
  /* We also need a .got.plt section when generating PLTs.  */
5370
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5371
0
            SEC_ALLOC | SEC_LOAD
5372
0
            | SEC_HAS_CONTENTS
5373
0
            | SEC_IN_MEMORY
5374
0
            | SEC_LINKER_CREATED);
5375
0
  if (s == NULL)
5376
0
    return false;
5377
0
  htab->root.sgotplt = s;
5378
5379
0
  return true;
5380
0
}
5381

5382
/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5383
   __GOTT_INDEX__ symbols.  These symbols are only special for
5384
   shared objects; they are not used in executables.  */
5385
5386
static bool
5387
is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5388
0
{
5389
0
  return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5390
0
    && bfd_link_pic (info)
5391
0
    && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5392
0
        || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5393
0
}
5394
5395
/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5396
   require an la25 stub.  See also mips_elf_local_pic_function_p,
5397
   which determines whether the destination function ever requires a
5398
   stub.  */
5399
5400
static bool
5401
mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5402
             bool target_is_16_bit_code_p)
5403
0
{
5404
  /* We specifically ignore branches and jumps from EF_PIC objects,
5405
     where the onus is on the compiler or programmer to perform any
5406
     necessary initialization of $25.  Sometimes such initialization
5407
     is unnecessary; for example, -mno-shared functions do not use
5408
     the incoming value of $25, and may therefore be called directly.  */
5409
0
  if (PIC_OBJECT_P (input_bfd))
5410
0
    return false;
5411
5412
0
  switch (r_type)
5413
0
    {
5414
0
    case R_MIPS_26:
5415
0
    case R_MIPS_PC16:
5416
0
    case R_MIPS_PC21_S2:
5417
0
    case R_MIPS_PC26_S2:
5418
0
    case R_MICROMIPS_26_S1:
5419
0
    case R_MICROMIPS_PC7_S1:
5420
0
    case R_MICROMIPS_PC10_S1:
5421
0
    case R_MICROMIPS_PC16_S1:
5422
0
    case R_MICROMIPS_PC23_S2:
5423
0
      return true;
5424
5425
0
    case R_MIPS16_26:
5426
0
      return !target_is_16_bit_code_p;
5427
5428
0
    default:
5429
0
      return false;
5430
0
    }
5431
0
}
5432

5433
/* Obtain the field relocated by RELOCATION.  */
5434
5435
static bfd_vma
5436
mips_elf_obtain_contents (reloc_howto_type *howto,
5437
        const Elf_Internal_Rela *relocation,
5438
        bfd *input_bfd, bfd_byte *contents)
5439
0
{
5440
0
  bfd_vma x = 0;
5441
0
  bfd_byte *location = contents + relocation->r_offset;
5442
0
  unsigned int size = bfd_get_reloc_size (howto);
5443
5444
  /* Obtain the bytes.  */
5445
0
  if (size != 0)
5446
0
    x = bfd_get (8 * size, input_bfd, location);
5447
5448
0
  return x;
5449
0
}
5450
5451
/* Store the field relocated by RELOCATION.  */
5452
5453
static void
5454
mips_elf_store_contents (reloc_howto_type *howto,
5455
       const Elf_Internal_Rela *relocation,
5456
       bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5457
0
{
5458
0
  bfd_byte *location = contents + relocation->r_offset;
5459
0
  unsigned int size = bfd_get_reloc_size (howto);
5460
5461
  /* Put the value into the output.  */
5462
0
  if (size != 0)
5463
0
    bfd_put (8 * size, input_bfd, x, location);
5464
0
}
5465
5466
/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5467
   RELOCATION described by HOWTO, with a move of 0 to the load target
5468
   register, returning TRUE if that is successful and FALSE otherwise.
5469
   If DOIT is FALSE, then only determine it patching is possible and
5470
   return status without actually changing CONTENTS.
5471
*/
5472
5473
static bool
5474
mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5475
         const Elf_Internal_Rela *relocation,
5476
         reloc_howto_type *howto, bool doit)
5477
0
{
5478
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5479
0
  bfd_byte *location = contents + relocation->r_offset;
5480
0
  bool nullified = true;
5481
0
  bfd_vma x;
5482
5483
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5484
5485
  /* Obtain the current value.  */
5486
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5487
5488
  /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5489
     while RY is at bits [18:16] of the combined 32-bit instruction word.  */
5490
0
  if (mips16_reloc_p (r_type)
5491
0
      && (((x >> 22) & 0x3ff) == 0x3d3        /* LW */
5492
0
    || ((x >> 22) & 0x3ff) == 0x3c7))      /* LD */
5493
0
    x = (0x3cdU << 22) | (x & (7 << 16)) << 3;     /* LI */
5494
0
  else if (micromips_reloc_p (r_type)
5495
0
     && ((x >> 26) & 0x37) == 0x37)     /* LW/LD */
5496
0
    x = (0xc << 26) | (x & (0x1f << 21));     /* ADDIU */
5497
0
  else if (((x >> 26) & 0x3f) == 0x23        /* LW */
5498
0
     || ((x >> 26) & 0x3f) == 0x37)     /* LD */
5499
0
    x = (0x9 << 26) | (x & (0x1f << 16));     /* ADDIU */
5500
0
  else
5501
0
    nullified = false;
5502
5503
  /* Put the value into the output.  */
5504
0
  if (doit && nullified)
5505
0
    mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5506
5507
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5508
5509
0
  return nullified;
5510
0
}
5511
5512
/* Calculate the value produced by the RELOCATION (which comes from
5513
   the INPUT_BFD).  The ADDEND is the addend to use for this
5514
   RELOCATION; RELOCATION->R_ADDEND is ignored.
5515
5516
   The result of the relocation calculation is stored in VALUEP.
5517
   On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5518
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5519
5520
   This function returns bfd_reloc_continue if the caller need take no
5521
   further action regarding this relocation, bfd_reloc_notsupported if
5522
   something goes dramatically wrong, bfd_reloc_overflow if an
5523
   overflow occurs, and bfd_reloc_ok to indicate success.  */
5524
5525
static bfd_reloc_status_type
5526
mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5527
             asection *input_section, bfd_byte *contents,
5528
             struct bfd_link_info *info,
5529
             const Elf_Internal_Rela *relocation,
5530
             bfd_vma addend, reloc_howto_type *howto,
5531
             Elf_Internal_Sym *local_syms,
5532
             asection **local_sections, bfd_vma *valuep,
5533
             const char **namep,
5534
             bool *cross_mode_jump_p,
5535
             bool save_addend)
5536
0
{
5537
  /* The eventual value we will return.  */
5538
0
  bfd_vma value;
5539
  /* The address of the symbol against which the relocation is
5540
     occurring.  */
5541
0
  bfd_vma symbol = 0;
5542
  /* The final GP value to be used for the relocatable, executable, or
5543
     shared object file being produced.  */
5544
0
  bfd_vma gp;
5545
  /* The place (section offset or address) of the storage unit being
5546
     relocated.  */
5547
0
  bfd_vma p;
5548
  /* The value of GP used to create the relocatable object.  */
5549
0
  bfd_vma gp0;
5550
  /* The offset into the global offset table at which the address of
5551
     the relocation entry symbol, adjusted by the addend, resides
5552
     during execution.  */
5553
0
  bfd_vma g = MINUS_ONE;
5554
  /* The section in which the symbol referenced by the relocation is
5555
     located.  */
5556
0
  asection *sec = NULL;
5557
0
  struct mips_elf_link_hash_entry *h = NULL;
5558
  /* TRUE if the symbol referred to by this relocation is a local
5559
     symbol.  */
5560
0
  bool local_p, was_local_p;
5561
  /* TRUE if the symbol referred to by this relocation is a section
5562
     symbol.  */
5563
0
  bool section_p = false;
5564
  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
5565
0
  bool gp_disp_p = false;
5566
  /* TRUE if the symbol referred to by this relocation is
5567
     "__gnu_local_gp".  */
5568
0
  bool gnu_local_gp_p = false;
5569
0
  Elf_Internal_Shdr *symtab_hdr;
5570
0
  size_t extsymoff;
5571
0
  unsigned long r_symndx;
5572
0
  int r_type;
5573
  /* TRUE if overflow occurred during the calculation of the
5574
     relocation value.  */
5575
0
  bool overflowed_p;
5576
  /* TRUE if this relocation refers to a MIPS16 function.  */
5577
0
  bool target_is_16_bit_code_p = false;
5578
0
  bool target_is_micromips_code_p = false;
5579
0
  struct mips_elf_link_hash_table *htab;
5580
0
  bfd *dynobj;
5581
0
  bool resolved_to_zero;
5582
5583
0
  dynobj = elf_hash_table (info)->dynobj;
5584
0
  htab = mips_elf_hash_table (info);
5585
0
  BFD_ASSERT (htab != NULL);
5586
5587
  /* Parse the relocation.  */
5588
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5589
0
  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5590
0
  p = (input_section->output_section->vma
5591
0
       + input_section->output_offset
5592
0
       + relocation->r_offset);
5593
5594
  /* Assume that there will be no overflow.  */
5595
0
  overflowed_p = false;
5596
5597
  /* Figure out whether or not the symbol is local, and get the offset
5598
     used in the array of hash table entries.  */
5599
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5600
0
  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5601
0
           local_sections);
5602
0
  was_local_p = local_p;
5603
0
  if (! elf_bad_symtab (input_bfd))
5604
0
    extsymoff = symtab_hdr->sh_info;
5605
0
  else
5606
0
    {
5607
      /* The symbol table does not follow the rule that local symbols
5608
   must come before globals.  */
5609
0
      extsymoff = 0;
5610
0
    }
5611
5612
  /* Figure out the value of the symbol.  */
5613
0
  if (local_p)
5614
0
    {
5615
0
      bool micromips_p = MICROMIPS_P (abfd);
5616
0
      Elf_Internal_Sym *sym;
5617
5618
0
      sym = local_syms + r_symndx;
5619
0
      sec = local_sections[r_symndx];
5620
5621
0
      section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5622
5623
0
      symbol = sec->output_section->vma + sec->output_offset;
5624
0
      if (!section_p || (sec->flags & SEC_MERGE))
5625
0
  symbol += sym->st_value;
5626
0
      if ((sec->flags & SEC_MERGE) && section_p)
5627
0
  {
5628
0
    addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5629
0
    addend -= symbol;
5630
0
    addend += sec->output_section->vma + sec->output_offset;
5631
0
  }
5632
5633
      /* MIPS16/microMIPS text labels should be treated as odd.  */
5634
0
      if (ELF_ST_IS_COMPRESSED (sym->st_other))
5635
0
  ++symbol;
5636
5637
      /* Record the name of this symbol, for our caller.  */
5638
0
      *namep = bfd_elf_string_from_elf_section (input_bfd,
5639
0
            symtab_hdr->sh_link,
5640
0
            sym->st_name);
5641
0
      if (*namep == NULL || **namep == '\0')
5642
0
  *namep = bfd_section_name (sec);
5643
5644
      /* For relocations against a section symbol and ones against no
5645
   symbol (absolute relocations) infer the ISA mode from the addend.  */
5646
0
      if (section_p || r_symndx == STN_UNDEF)
5647
0
  {
5648
0
    target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5649
0
    target_is_micromips_code_p = (addend & 1) && micromips_p;
5650
0
  }
5651
      /* For relocations against an absolute symbol infer the ISA mode
5652
   from the value of the symbol plus addend.  */
5653
0
      else if (bfd_is_abs_section (sec))
5654
0
  {
5655
0
    target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5656
0
    target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5657
0
  }
5658
      /* Otherwise just use the regular symbol annotation available.  */
5659
0
      else
5660
0
  {
5661
0
    target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5662
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5663
0
  }
5664
0
    }
5665
0
  else
5666
0
    {
5667
      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
5668
5669
      /* For global symbols we look up the symbol in the hash-table.  */
5670
0
      h = ((struct mips_elf_link_hash_entry *)
5671
0
     elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5672
      /* Find the real hash-table entry for this symbol.  */
5673
0
      while (h->root.root.type == bfd_link_hash_indirect
5674
0
       || h->root.root.type == bfd_link_hash_warning)
5675
0
  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5676
5677
      /* Record the name of this symbol, for our caller.  */
5678
0
      *namep = h->root.root.root.string;
5679
5680
      /* See if this is the special _gp_disp symbol.  Note that such a
5681
   symbol must always be a global symbol.  */
5682
0
      if (strcmp (*namep, "_gp_disp") == 0
5683
0
    && ! NEWABI_P (input_bfd))
5684
0
  {
5685
    /* Relocations against _gp_disp are permitted only with
5686
       R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5687
0
    if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5688
0
      return bfd_reloc_notsupported;
5689
5690
0
    gp_disp_p = true;
5691
0
  }
5692
      /* See if this is the special _gp symbol.  Note that such a
5693
   symbol must always be a global symbol.  */
5694
0
      else if (strcmp (*namep, "__gnu_local_gp") == 0)
5695
0
  gnu_local_gp_p = true;
5696
5697
5698
      /* If this symbol is defined, calculate its address.  Note that
5699
   _gp_disp is a magic symbol, always implicitly defined by the
5700
   linker, so it's inappropriate to check to see whether or not
5701
   its defined.  */
5702
0
      else if ((h->root.root.type == bfd_link_hash_defined
5703
0
    || h->root.root.type == bfd_link_hash_defweak)
5704
0
         && h->root.root.u.def.section)
5705
0
  {
5706
0
    sec = h->root.root.u.def.section;
5707
0
    if (sec->output_section)
5708
0
      symbol = (h->root.root.u.def.value
5709
0
          + sec->output_section->vma
5710
0
          + sec->output_offset);
5711
0
    else
5712
0
      symbol = h->root.root.u.def.value;
5713
0
  }
5714
0
      else if (h->root.root.type == bfd_link_hash_undefweak)
5715
  /* We allow relocations against undefined weak symbols, giving
5716
     it the value zero, so that you can undefined weak functions
5717
     and check to see if they exist by looking at their
5718
     addresses.  */
5719
0
  symbol = 0;
5720
0
      else if (info->unresolved_syms_in_objects == RM_IGNORE
5721
0
         && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5722
0
  symbol = 0;
5723
0
      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5724
0
           ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5725
0
  {
5726
    /* If this is a dynamic link, we should have created a
5727
       _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5728
       in _bfd_mips_elf_create_dynamic_sections.
5729
       Otherwise, we should define the symbol with a value of 0.
5730
       FIXME: It should probably get into the symbol table
5731
       somehow as well.  */
5732
0
    BFD_ASSERT (! bfd_link_pic (info));
5733
0
    BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5734
0
    symbol = 0;
5735
0
  }
5736
0
      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5737
0
  {
5738
    /* This is an optional symbol - an Irix specific extension to the
5739
       ELF spec.  Ignore it for now.
5740
       XXX - FIXME - there is more to the spec for OPTIONAL symbols
5741
       than simply ignoring them, but we do not handle this for now.
5742
       For information see the "64-bit ELF Object File Specification"
5743
       which is available from here:
5744
       http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5745
0
    symbol = 0;
5746
0
  }
5747
0
      else
5748
0
  {
5749
0
          bool reject_undefined
5750
0
      = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5751
0
    && !info->warn_unresolved_syms)
5752
0
         || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5753
5754
0
    info->callbacks->undefined_symbol
5755
0
      (info, h->root.root.root.string, input_bfd,
5756
0
       input_section, relocation->r_offset, reject_undefined);
5757
5758
0
    if (reject_undefined)
5759
0
      return bfd_reloc_undefined;
5760
5761
0
    symbol = 0;
5762
0
  }
5763
5764
0
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5765
0
      target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5766
0
    }
5767
5768
  /* If this is a reference to a 16-bit function with a stub, we need
5769
     to redirect the relocation to the stub unless:
5770
5771
     (a) the relocation is for a MIPS16 JAL;
5772
5773
     (b) the relocation is for a MIPS16 PIC call, and there are no
5774
   non-MIPS16 uses of the GOT slot; or
5775
5776
     (c) the section allows direct references to MIPS16 functions.  */
5777
0
  if (r_type != R_MIPS16_26
5778
0
      && !bfd_link_relocatable (info)
5779
0
      && ((h != NULL
5780
0
     && h->fn_stub != NULL
5781
0
     && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5782
0
    || (local_p
5783
0
        && mips_elf_tdata (input_bfd)->local_stubs != NULL
5784
0
        && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5785
0
      && !section_allows_mips16_refs_p (input_section))
5786
0
    {
5787
      /* This is a 32- or 64-bit call to a 16-bit function.  We should
5788
   have already noticed that we were going to need the
5789
   stub.  */
5790
0
      if (local_p)
5791
0
  {
5792
0
    sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5793
0
    value = 0;
5794
0
  }
5795
0
      else
5796
0
  {
5797
0
    BFD_ASSERT (h->need_fn_stub);
5798
0
    if (h->la25_stub)
5799
0
      {
5800
        /* If a LA25 header for the stub itself exists, point to the
5801
     prepended LUI/ADDIU sequence.  */
5802
0
        sec = h->la25_stub->stub_section;
5803
0
        value = h->la25_stub->offset;
5804
0
      }
5805
0
    else
5806
0
      {
5807
0
        sec = h->fn_stub;
5808
0
        value = 0;
5809
0
      }
5810
0
  }
5811
5812
0
      symbol = sec->output_section->vma + sec->output_offset + value;
5813
      /* The target is 16-bit, but the stub isn't.  */
5814
0
      target_is_16_bit_code_p = false;
5815
0
    }
5816
  /* If this is a MIPS16 call with a stub, that is made through the PLT or
5817
     to a standard MIPS function, we need to redirect the call to the stub.
5818
     Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5819
     indirect calls should use an indirect stub instead.  */
5820
0
  else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5821
0
     && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5822
0
         || (local_p
5823
0
       && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5824
0
       && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5825
0
     && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5826
0
    {
5827
0
      if (local_p)
5828
0
  sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5829
0
      else
5830
0
  {
5831
    /* If both call_stub and call_fp_stub are defined, we can figure
5832
       out which one to use by checking which one appears in the input
5833
       file.  */
5834
0
    if (h->call_stub != NULL && h->call_fp_stub != NULL)
5835
0
      {
5836
0
        asection *o;
5837
5838
0
        sec = NULL;
5839
0
        for (o = input_bfd->sections; o != NULL; o = o->next)
5840
0
    {
5841
0
      if (CALL_FP_STUB_P (bfd_section_name (o)))
5842
0
        {
5843
0
          sec = h->call_fp_stub;
5844
0
          break;
5845
0
        }
5846
0
    }
5847
0
        if (sec == NULL)
5848
0
    sec = h->call_stub;
5849
0
      }
5850
0
    else if (h->call_stub != NULL)
5851
0
      sec = h->call_stub;
5852
0
    else
5853
0
      sec = h->call_fp_stub;
5854
0
  }
5855
5856
0
      BFD_ASSERT (sec->size > 0);
5857
0
      symbol = sec->output_section->vma + sec->output_offset;
5858
0
    }
5859
  /* If this is a direct call to a PIC function, redirect to the
5860
     non-PIC stub.  */
5861
0
  else if (h != NULL && h->la25_stub
5862
0
     && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5863
0
               target_is_16_bit_code_p))
5864
0
    {
5865
0
  symbol = (h->la25_stub->stub_section->output_section->vma
5866
0
      + h->la25_stub->stub_section->output_offset
5867
0
      + h->la25_stub->offset);
5868
0
  if (ELF_ST_IS_MICROMIPS (h->root.other))
5869
0
    symbol |= 1;
5870
0
    }
5871
  /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5872
     entry is used if a standard PLT entry has also been made.  In this
5873
     case the symbol will have been set by mips_elf_set_plt_sym_value
5874
     to point to the standard PLT entry, so redirect to the compressed
5875
     one.  */
5876
0
  else if ((mips16_branch_reloc_p (r_type)
5877
0
      || micromips_branch_reloc_p (r_type))
5878
0
     && !bfd_link_relocatable (info)
5879
0
     && h != NULL
5880
0
     && h->use_plt_entry
5881
0
     && h->root.plt.plist->comp_offset != MINUS_ONE
5882
0
     && h->root.plt.plist->mips_offset != MINUS_ONE)
5883
0
    {
5884
0
      bool micromips_p = MICROMIPS_P (abfd);
5885
5886
0
      sec = htab->root.splt;
5887
0
      symbol = (sec->output_section->vma
5888
0
    + sec->output_offset
5889
0
    + htab->plt_header_size
5890
0
    + htab->plt_mips_offset
5891
0
    + h->root.plt.plist->comp_offset
5892
0
    + 1);
5893
5894
0
      target_is_16_bit_code_p = !micromips_p;
5895
0
      target_is_micromips_code_p = micromips_p;
5896
0
    }
5897
5898
  /* Make sure MIPS16 and microMIPS are not used together.  */
5899
0
  if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5900
0
      || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5901
0
   {
5902
0
      _bfd_error_handler
5903
0
  (_("MIPS16 and microMIPS functions cannot call each other"));
5904
0
      return bfd_reloc_notsupported;
5905
0
   }
5906
5907
  /* Calls from 16-bit code to 32-bit code and vice versa require the
5908
     mode change.  However, we can ignore calls to undefined weak symbols,
5909
     which should never be executed at runtime.  This exception is important
5910
     because the assembly writer may have "known" that any definition of the
5911
     symbol would be 16-bit code, and that direct jumps were therefore
5912
     acceptable.  */
5913
0
  *cross_mode_jump_p = (!bfd_link_relocatable (info)
5914
0
      && !(h && h->root.root.type == bfd_link_hash_undefweak)
5915
0
      && ((mips16_branch_reloc_p (r_type)
5916
0
           && !target_is_16_bit_code_p)
5917
0
          || (micromips_branch_reloc_p (r_type)
5918
0
        && !target_is_micromips_code_p)
5919
0
          || ((branch_reloc_p (r_type)
5920
0
         || r_type == R_MIPS_JALR)
5921
0
        && (target_is_16_bit_code_p
5922
0
            || target_is_micromips_code_p))));
5923
5924
0
  resolved_to_zero = (h != NULL
5925
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5926
5927
0
  switch (r_type)
5928
0
    {
5929
0
    case R_MIPS16_CALL16:
5930
0
    case R_MIPS16_GOT16:
5931
0
    case R_MIPS_CALL16:
5932
0
    case R_MIPS_GOT16:
5933
0
    case R_MIPS_GOT_PAGE:
5934
0
    case R_MIPS_GOT_DISP:
5935
0
    case R_MIPS_GOT_LO16:
5936
0
    case R_MIPS_CALL_LO16:
5937
0
    case R_MICROMIPS_CALL16:
5938
0
    case R_MICROMIPS_GOT16:
5939
0
    case R_MICROMIPS_GOT_PAGE:
5940
0
    case R_MICROMIPS_GOT_DISP:
5941
0
    case R_MICROMIPS_GOT_LO16:
5942
0
    case R_MICROMIPS_CALL_LO16:
5943
0
      if (resolved_to_zero
5944
0
    && !bfd_link_relocatable (info)
5945
0
    && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5946
0
          relocation->r_offset)
5947
0
    && mips_elf_nullify_got_load (input_bfd, contents,
5948
0
          relocation, howto, true))
5949
0
  return bfd_reloc_continue;
5950
5951
      /* Fall through.  */
5952
0
    case R_MIPS_GOT_HI16:
5953
0
    case R_MIPS_CALL_HI16:
5954
0
    case R_MICROMIPS_GOT_HI16:
5955
0
    case R_MICROMIPS_CALL_HI16:
5956
0
      if (resolved_to_zero
5957
0
    && htab->use_absolute_zero
5958
0
    && bfd_link_pic (info))
5959
0
  {
5960
    /* Redirect to the special `__gnu_absolute_zero' symbol.  */
5961
0
    h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5962
0
           false, false, false);
5963
0
    BFD_ASSERT (h != NULL);
5964
0
  }
5965
0
      break;
5966
0
    }
5967
5968
0
  local_p = (h == NULL || mips_use_local_got_p (info, h));
5969
5970
0
  gp0 = _bfd_get_gp_value (input_bfd);
5971
0
  gp = _bfd_get_gp_value (abfd);
5972
0
  if (htab->got_info)
5973
0
    gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5974
5975
0
  if (gnu_local_gp_p)
5976
0
    symbol = gp;
5977
5978
  /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5979
     to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
5980
     corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
5981
0
  if (got_page_reloc_p (r_type) && !local_p)
5982
0
    {
5983
0
      r_type = (micromips_reloc_p (r_type)
5984
0
    ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5985
0
      addend = 0;
5986
0
    }
5987
5988
  /* If we haven't already determined the GOT offset, and we're going
5989
     to need it, get it now.  */
5990
0
  switch (r_type)
5991
0
    {
5992
0
    case R_MIPS16_CALL16:
5993
0
    case R_MIPS16_GOT16:
5994
0
    case R_MIPS_CALL16:
5995
0
    case R_MIPS_GOT16:
5996
0
    case R_MIPS_GOT_DISP:
5997
0
    case R_MIPS_GOT_HI16:
5998
0
    case R_MIPS_CALL_HI16:
5999
0
    case R_MIPS_GOT_LO16:
6000
0
    case R_MIPS_CALL_LO16:
6001
0
    case R_MICROMIPS_CALL16:
6002
0
    case R_MICROMIPS_GOT16:
6003
0
    case R_MICROMIPS_GOT_DISP:
6004
0
    case R_MICROMIPS_GOT_HI16:
6005
0
    case R_MICROMIPS_CALL_HI16:
6006
0
    case R_MICROMIPS_GOT_LO16:
6007
0
    case R_MICROMIPS_CALL_LO16:
6008
0
    case R_MIPS_TLS_GD:
6009
0
    case R_MIPS_TLS_GOTTPREL:
6010
0
    case R_MIPS_TLS_LDM:
6011
0
    case R_MIPS16_TLS_GD:
6012
0
    case R_MIPS16_TLS_GOTTPREL:
6013
0
    case R_MIPS16_TLS_LDM:
6014
0
    case R_MICROMIPS_TLS_GD:
6015
0
    case R_MICROMIPS_TLS_GOTTPREL:
6016
0
    case R_MICROMIPS_TLS_LDM:
6017
      /* Find the index into the GOT where this value is located.  */
6018
0
      if (tls_ldm_reloc_p (r_type))
6019
0
  {
6020
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
6021
0
          0, 0, NULL, r_type);
6022
0
    if (g == MINUS_ONE)
6023
0
      return bfd_reloc_outofrange;
6024
0
  }
6025
0
      else if (!local_p)
6026
0
  {
6027
    /* On VxWorks, CALL relocations should refer to the .got.plt
6028
       entry, which is initialized to point at the PLT stub.  */
6029
0
    if (htab->root.target_os == is_vxworks
6030
0
        && (call_hi16_reloc_p (r_type)
6031
0
      || call_lo16_reloc_p (r_type)
6032
0
      || call16_reloc_p (r_type)))
6033
0
      {
6034
0
        BFD_ASSERT (addend == 0);
6035
0
        BFD_ASSERT (h->root.needs_plt);
6036
0
        g = mips_elf_gotplt_index (info, &h->root);
6037
0
      }
6038
0
    else
6039
0
      {
6040
0
        BFD_ASSERT (addend == 0);
6041
0
        g = mips_elf_global_got_index (abfd, info, input_bfd,
6042
0
               &h->root, r_type);
6043
0
        if (!TLS_RELOC_P (r_type)
6044
0
      && !elf_hash_table (info)->dynamic_sections_created)
6045
    /* This is a static link.  We must initialize the GOT entry.  */
6046
0
    MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
6047
0
      }
6048
0
  }
6049
0
      else if (htab->root.target_os != is_vxworks
6050
0
         && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6051
  /* The calculation below does not involve "g".  */
6052
0
  break;
6053
0
      else
6054
0
  {
6055
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
6056
0
          symbol + addend, r_symndx, h, r_type);
6057
0
    if (g == MINUS_ONE)
6058
0
      return bfd_reloc_outofrange;
6059
0
  }
6060
6061
      /* Convert GOT indices to actual offsets.  */
6062
0
      g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6063
0
      break;
6064
0
    }
6065
6066
  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6067
     symbols are resolved by the loader.  Add them to .rela.dyn.  */
6068
0
  if (h != NULL && is_gott_symbol (info, &h->root))
6069
0
    {
6070
0
      Elf_Internal_Rela outrel;
6071
0
      bfd_byte *loc;
6072
0
      asection *s;
6073
6074
0
      s = mips_elf_rel_dyn_section (info, false);
6075
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6076
6077
0
      outrel.r_offset = (input_section->output_section->vma
6078
0
       + input_section->output_offset
6079
0
       + relocation->r_offset);
6080
0
      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6081
0
      outrel.r_addend = addend;
6082
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6083
6084
      /* If we've written this relocation for a readonly section,
6085
   we need to set DF_TEXTREL again, so that we do not delete the
6086
   DT_TEXTREL tag.  */
6087
0
      if (MIPS_ELF_READONLY_SECTION (input_section))
6088
0
  info->flags |= DF_TEXTREL;
6089
6090
0
      *valuep = 0;
6091
0
      return bfd_reloc_ok;
6092
0
    }
6093
6094
  /* Figure out what kind of relocation is being performed.  */
6095
0
  switch (r_type)
6096
0
    {
6097
0
    case R_MIPS_NONE:
6098
0
      return bfd_reloc_continue;
6099
6100
0
    case R_MIPS_16:
6101
0
      if (howto->partial_inplace)
6102
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6103
0
      value = symbol + addend;
6104
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6105
0
      break;
6106
6107
0
    case R_MIPS_32:
6108
0
    case R_MIPS_REL32:
6109
0
    case R_MIPS_64:
6110
0
      if ((bfd_link_pic (info)
6111
0
     || (htab->root.dynamic_sections_created
6112
0
         && h != NULL
6113
0
         && h->root.def_dynamic
6114
0
         && !h->root.def_regular
6115
0
         && !h->has_static_relocs))
6116
0
    && r_symndx != STN_UNDEF
6117
0
    && (h == NULL
6118
0
        || h->root.root.type != bfd_link_hash_undefweak
6119
0
        || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6120
0
      && !resolved_to_zero))
6121
0
    && (input_section->flags & SEC_ALLOC) != 0)
6122
0
  {
6123
    /* If we're creating a shared library, then we can't know
6124
       where the symbol will end up.  So, we create a relocation
6125
       record in the output, and leave the job up to the dynamic
6126
       linker.  We must do the same for executable references to
6127
       shared library symbols, unless we've decided to use copy
6128
       relocs or PLTs instead.  */
6129
0
    value = addend;
6130
0
    if (!mips_elf_create_dynamic_relocation (abfd,
6131
0
               info,
6132
0
               relocation,
6133
0
               h,
6134
0
               sec,
6135
0
               symbol,
6136
0
               &value,
6137
0
               input_section))
6138
0
      return bfd_reloc_undefined;
6139
0
  }
6140
0
      else
6141
0
  {
6142
0
    if (r_type != R_MIPS_REL32)
6143
0
      value = symbol + addend;
6144
0
    else
6145
0
      value = addend;
6146
0
  }
6147
0
      value &= howto->dst_mask;
6148
0
      break;
6149
6150
0
    case R_MIPS_PC32:
6151
0
      value = symbol + addend - p;
6152
0
      value &= howto->dst_mask;
6153
0
      break;
6154
6155
0
    case R_MIPS16_26:
6156
      /* The calculation for R_MIPS16_26 is just the same as for an
6157
   R_MIPS_26.  It's only the storage of the relocated field into
6158
   the output file that's different.  That's handled in
6159
   mips_elf_perform_relocation.  So, we just fall through to the
6160
   R_MIPS_26 case here.  */
6161
0
    case R_MIPS_26:
6162
0
    case R_MICROMIPS_26_S1:
6163
0
      {
6164
0
  unsigned int shift;
6165
6166
  /* Shift is 2, unusually, for microMIPS JALX.  */
6167
0
  shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6168
6169
0
  if (howto->partial_inplace && !section_p)
6170
0
    value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6171
0
  else
6172
0
    value = addend;
6173
0
  value += symbol;
6174
6175
  /* Make sure the target of a jump is suitably aligned.  Bit 0 must
6176
     be the correct ISA mode selector except for weak undefined
6177
     symbols.  */
6178
0
  if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6179
0
      && (*cross_mode_jump_p
6180
0
    ? (value & 3) != (r_type == R_MIPS_26)
6181
0
    : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6182
0
    return bfd_reloc_outofrange;
6183
6184
0
  value >>= shift;
6185
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6186
0
    overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6187
0
  value &= howto->dst_mask;
6188
0
      }
6189
0
      break;
6190
6191
0
    case R_MIPS_TLS_DTPREL_HI16:
6192
0
    case R_MIPS16_TLS_DTPREL_HI16:
6193
0
    case R_MICROMIPS_TLS_DTPREL_HI16:
6194
0
      value = (mips_elf_high (addend + symbol - dtprel_base (info))
6195
0
         & howto->dst_mask);
6196
0
      break;
6197
6198
0
    case R_MIPS_TLS_DTPREL_LO16:
6199
0
    case R_MIPS_TLS_DTPREL32:
6200
0
    case R_MIPS_TLS_DTPREL64:
6201
0
    case R_MIPS16_TLS_DTPREL_LO16:
6202
0
    case R_MICROMIPS_TLS_DTPREL_LO16:
6203
0
      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6204
0
      break;
6205
6206
0
    case R_MIPS_TLS_TPREL_HI16:
6207
0
    case R_MIPS16_TLS_TPREL_HI16:
6208
0
    case R_MICROMIPS_TLS_TPREL_HI16:
6209
0
      value = (mips_elf_high (addend + symbol - tprel_base (info))
6210
0
         & howto->dst_mask);
6211
0
      break;
6212
6213
0
    case R_MIPS_TLS_TPREL_LO16:
6214
0
    case R_MIPS_TLS_TPREL32:
6215
0
    case R_MIPS_TLS_TPREL64:
6216
0
    case R_MIPS16_TLS_TPREL_LO16:
6217
0
    case R_MICROMIPS_TLS_TPREL_LO16:
6218
0
      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6219
0
      break;
6220
6221
0
    case R_MIPS_HI16:
6222
0
    case R_MIPS16_HI16:
6223
0
    case R_MICROMIPS_HI16:
6224
0
      if (!gp_disp_p)
6225
0
  {
6226
0
    value = mips_elf_high (addend + symbol);
6227
0
    value &= howto->dst_mask;
6228
0
  }
6229
0
      else
6230
0
  {
6231
    /* For MIPS16 ABI code we generate this sequence
6232
    0: li      $v0,%hi(_gp_disp)
6233
    4: addiupc $v1,%lo(_gp_disp)
6234
    8: sll     $v0,16
6235
         12: addu    $v0,$v1
6236
         14: move    $gp,$v0
6237
       So the offsets of hi and lo relocs are the same, but the
6238
       base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6239
       ADDIUPC clears the low two bits of the instruction address,
6240
       so the base is ($t9 + 4) & ~3.  */
6241
0
    if (r_type == R_MIPS16_HI16)
6242
0
      value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6243
    /* The microMIPS .cpload sequence uses the same assembly
6244
       instructions as the traditional psABI version, but the
6245
       incoming $t9 has the low bit set.  */
6246
0
    else if (r_type == R_MICROMIPS_HI16)
6247
0
      value = mips_elf_high (addend + gp - p - 1);
6248
0
    else
6249
0
      value = mips_elf_high (addend + gp - p);
6250
0
  }
6251
0
      break;
6252
6253
0
    case R_MIPS_LO16:
6254
0
    case R_MIPS16_LO16:
6255
0
    case R_MICROMIPS_LO16:
6256
0
    case R_MICROMIPS_HI0_LO16:
6257
0
      if (!gp_disp_p)
6258
0
  value = (symbol + addend) & howto->dst_mask;
6259
0
      else
6260
0
  {
6261
    /* See the comment for R_MIPS16_HI16 above for the reason
6262
       for this conditional.  */
6263
0
    if (r_type == R_MIPS16_LO16)
6264
0
      value = addend + gp - (p & ~(bfd_vma) 0x3);
6265
0
    else if (r_type == R_MICROMIPS_LO16
6266
0
       || r_type == R_MICROMIPS_HI0_LO16)
6267
0
      value = addend + gp - p + 3;
6268
0
    else
6269
0
      value = addend + gp - p + 4;
6270
    /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6271
       for overflow.  But, on, say, IRIX5, relocations against
6272
       _gp_disp are normally generated from the .cpload
6273
       pseudo-op.  It generates code that normally looks like
6274
       this:
6275
6276
         lui    $gp,%hi(_gp_disp)
6277
         addiu  $gp,$gp,%lo(_gp_disp)
6278
         addu   $gp,$gp,$t9
6279
6280
       Here $t9 holds the address of the function being called,
6281
       as required by the MIPS ELF ABI.  The R_MIPS_LO16
6282
       relocation can easily overflow in this situation, but the
6283
       R_MIPS_HI16 relocation will handle the overflow.
6284
       Therefore, we consider this a bug in the MIPS ABI, and do
6285
       not check for overflow here.  */
6286
0
  }
6287
0
      break;
6288
6289
0
    case R_MIPS_LITERAL:
6290
0
    case R_MICROMIPS_LITERAL:
6291
      /* Because we don't merge literal sections, we can handle this
6292
   just like R_MIPS_GPREL16.  In the long run, we should merge
6293
   shared literals, and then we will need to additional work
6294
   here.  */
6295
6296
      /* Fall through.  */
6297
6298
0
    case R_MIPS16_GPREL:
6299
      /* The R_MIPS16_GPREL performs the same calculation as
6300
   R_MIPS_GPREL16, but stores the relocated bits in a different
6301
   order.  We don't need to do anything special here; the
6302
   differences are handled in mips_elf_perform_relocation.  */
6303
0
    case R_MIPS_GPREL16:
6304
0
    case R_MICROMIPS_GPREL7_S2:
6305
0
    case R_MICROMIPS_GPREL16:
6306
0
      {
6307
0
  int bits = howto->bitsize + howto->rightshift;
6308
  /* Only sign-extend the addend if it was extracted from the
6309
     instruction.  If the addend was separate, leave it alone,
6310
     otherwise we may lose significant bits.  */
6311
0
  if (howto->partial_inplace)
6312
0
    addend = _bfd_mips_elf_sign_extend (addend, bits);
6313
0
  value = symbol + addend - gp;
6314
  /* If the symbol was local, any earlier relocatable links will
6315
     have adjusted its addend with the gp offset, so compensate
6316
     for that now.  Don't do it for symbols forced local in this
6317
     link, though, since they won't have had the gp offset applied
6318
     to them before.  */
6319
0
  if (was_local_p)
6320
0
    value += gp0;
6321
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6322
0
    overflowed_p = mips_elf_overflow_p (value, bits);
6323
0
      }
6324
0
      break;
6325
6326
0
    case R_MIPS16_GOT16:
6327
0
    case R_MIPS16_CALL16:
6328
0
    case R_MIPS_GOT16:
6329
0
    case R_MIPS_CALL16:
6330
0
    case R_MICROMIPS_GOT16:
6331
0
    case R_MICROMIPS_CALL16:
6332
      /* VxWorks does not have separate local and global semantics for
6333
   R_MIPS*_GOT16; every relocation evaluates to "G".  */
6334
0
      if (htab->root.target_os != is_vxworks && local_p)
6335
0
  {
6336
0
    value = mips_elf_got16_entry (abfd, input_bfd, info,
6337
0
          symbol + addend, !was_local_p);
6338
0
    if (value == MINUS_ONE)
6339
0
      return bfd_reloc_outofrange;
6340
0
    value
6341
0
      = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6342
0
    overflowed_p = mips_elf_overflow_p (value, 16);
6343
0
    break;
6344
0
  }
6345
6346
      /* Fall through.  */
6347
6348
0
    case R_MIPS_TLS_GD:
6349
0
    case R_MIPS_TLS_GOTTPREL:
6350
0
    case R_MIPS_TLS_LDM:
6351
0
    case R_MIPS_GOT_DISP:
6352
0
    case R_MIPS16_TLS_GD:
6353
0
    case R_MIPS16_TLS_GOTTPREL:
6354
0
    case R_MIPS16_TLS_LDM:
6355
0
    case R_MICROMIPS_TLS_GD:
6356
0
    case R_MICROMIPS_TLS_GOTTPREL:
6357
0
    case R_MICROMIPS_TLS_LDM:
6358
0
    case R_MICROMIPS_GOT_DISP:
6359
0
      value = g;
6360
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6361
0
      break;
6362
6363
0
    case R_MIPS_GPREL32:
6364
0
      value = (addend + symbol + gp0 - gp);
6365
0
      if (!save_addend)
6366
0
  value &= howto->dst_mask;
6367
0
      break;
6368
6369
0
    case R_MIPS_PC16:
6370
0
    case R_MIPS_GNU_REL16_S2:
6371
0
      if (howto->partial_inplace)
6372
0
  addend = _bfd_mips_elf_sign_extend (addend, 18);
6373
6374
      /* No need to exclude weak undefined symbols here as they resolve
6375
   to 0 and never set `*cross_mode_jump_p', so this alignment check
6376
   will never trigger for them.  */
6377
0
      if (*cross_mode_jump_p
6378
0
    ? ((symbol + addend) & 3) != 1
6379
0
    : ((symbol + addend) & 3) != 0)
6380
0
  return bfd_reloc_outofrange;
6381
6382
0
      value = symbol + addend - p;
6383
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6384
0
  overflowed_p = mips_elf_overflow_p (value, 18);
6385
0
      value >>= howto->rightshift;
6386
0
      value &= howto->dst_mask;
6387
0
      break;
6388
6389
0
    case R_MIPS16_PC16_S1:
6390
0
      if (howto->partial_inplace)
6391
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6392
6393
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6394
0
    && (*cross_mode_jump_p
6395
0
        ? ((symbol + addend) & 3) != 0
6396
0
        : ((symbol + addend) & 1) == 0))
6397
0
  return bfd_reloc_outofrange;
6398
6399
0
      value = symbol + addend - p;
6400
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6401
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6402
0
      value >>= howto->rightshift;
6403
0
      value &= howto->dst_mask;
6404
0
      break;
6405
6406
0
    case R_MIPS_PC21_S2:
6407
0
      if (howto->partial_inplace)
6408
0
  addend = _bfd_mips_elf_sign_extend (addend, 23);
6409
6410
0
      if ((symbol + addend) & 3)
6411
0
  return bfd_reloc_outofrange;
6412
6413
0
      value = symbol + addend - p;
6414
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6415
0
  overflowed_p = mips_elf_overflow_p (value, 23);
6416
0
      value >>= howto->rightshift;
6417
0
      value &= howto->dst_mask;
6418
0
      break;
6419
6420
0
    case R_MIPS_PC26_S2:
6421
0
      if (howto->partial_inplace)
6422
0
  addend = _bfd_mips_elf_sign_extend (addend, 28);
6423
6424
0
      if ((symbol + addend) & 3)
6425
0
  return bfd_reloc_outofrange;
6426
6427
0
      value = symbol + addend - p;
6428
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6429
0
  overflowed_p = mips_elf_overflow_p (value, 28);
6430
0
      value >>= howto->rightshift;
6431
0
      value &= howto->dst_mask;
6432
0
      break;
6433
6434
0
    case R_MIPS_PC18_S3:
6435
0
      if (howto->partial_inplace)
6436
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6437
6438
0
      if ((symbol + addend) & 7)
6439
0
  return bfd_reloc_outofrange;
6440
6441
0
      value = symbol + addend - ((p | 7) ^ 7);
6442
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6443
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6444
0
      value >>= howto->rightshift;
6445
0
      value &= howto->dst_mask;
6446
0
      break;
6447
6448
0
    case R_MIPS_PC19_S2:
6449
0
      if (howto->partial_inplace)
6450
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6451
6452
0
      if ((symbol + addend) & 3)
6453
0
  return bfd_reloc_outofrange;
6454
6455
0
      value = symbol + addend - p;
6456
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6457
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6458
0
      value >>= howto->rightshift;
6459
0
      value &= howto->dst_mask;
6460
0
      break;
6461
6462
0
    case R_MIPS_PCHI16:
6463
0
      value = mips_elf_high (symbol + addend - p);
6464
0
      value &= howto->dst_mask;
6465
0
      break;
6466
6467
0
    case R_MIPS_PCLO16:
6468
0
      if (howto->partial_inplace)
6469
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6470
0
      value = symbol + addend - p;
6471
0
      value &= howto->dst_mask;
6472
0
      break;
6473
6474
0
    case R_MICROMIPS_PC7_S1:
6475
0
      if (howto->partial_inplace)
6476
0
  addend = _bfd_mips_elf_sign_extend (addend, 8);
6477
6478
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6479
0
    && (*cross_mode_jump_p
6480
0
        ? ((symbol + addend + 2) & 3) != 0
6481
0
        : ((symbol + addend + 2) & 1) == 0))
6482
0
  return bfd_reloc_outofrange;
6483
6484
0
      value = symbol + addend - p;
6485
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6486
0
  overflowed_p = mips_elf_overflow_p (value, 8);
6487
0
      value >>= howto->rightshift;
6488
0
      value &= howto->dst_mask;
6489
0
      break;
6490
6491
0
    case R_MICROMIPS_PC10_S1:
6492
0
      if (howto->partial_inplace)
6493
0
  addend = _bfd_mips_elf_sign_extend (addend, 11);
6494
6495
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6496
0
    && (*cross_mode_jump_p
6497
0
        ? ((symbol + addend + 2) & 3) != 0
6498
0
        : ((symbol + addend + 2) & 1) == 0))
6499
0
  return bfd_reloc_outofrange;
6500
6501
0
      value = symbol + addend - p;
6502
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6503
0
  overflowed_p = mips_elf_overflow_p (value, 11);
6504
0
      value >>= howto->rightshift;
6505
0
      value &= howto->dst_mask;
6506
0
      break;
6507
6508
0
    case R_MICROMIPS_PC16_S1:
6509
0
      if (howto->partial_inplace)
6510
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6511
6512
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6513
0
    && (*cross_mode_jump_p
6514
0
        ? ((symbol + addend) & 3) != 0
6515
0
        : ((symbol + addend) & 1) == 0))
6516
0
  return bfd_reloc_outofrange;
6517
6518
0
      value = symbol + addend - p;
6519
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6520
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6521
0
      value >>= howto->rightshift;
6522
0
      value &= howto->dst_mask;
6523
0
      break;
6524
6525
0
    case R_MICROMIPS_PC23_S2:
6526
0
      if (howto->partial_inplace)
6527
0
  addend = _bfd_mips_elf_sign_extend (addend, 25);
6528
0
      value = symbol + addend - ((p | 3) ^ 3);
6529
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6530
0
  overflowed_p = mips_elf_overflow_p (value, 25);
6531
0
      value >>= howto->rightshift;
6532
0
      value &= howto->dst_mask;
6533
0
      break;
6534
6535
0
    case R_MIPS_GOT_HI16:
6536
0
    case R_MIPS_CALL_HI16:
6537
0
    case R_MICROMIPS_GOT_HI16:
6538
0
    case R_MICROMIPS_CALL_HI16:
6539
      /* We're allowed to handle these two relocations identically.
6540
   The dynamic linker is allowed to handle the CALL relocations
6541
   differently by creating a lazy evaluation stub.  */
6542
0
      value = g;
6543
0
      value = mips_elf_high (value);
6544
0
      value &= howto->dst_mask;
6545
0
      break;
6546
6547
0
    case R_MIPS_GOT_LO16:
6548
0
    case R_MIPS_CALL_LO16:
6549
0
    case R_MICROMIPS_GOT_LO16:
6550
0
    case R_MICROMIPS_CALL_LO16:
6551
0
      value = g & howto->dst_mask;
6552
0
      break;
6553
6554
0
    case R_MIPS_GOT_PAGE:
6555
0
    case R_MICROMIPS_GOT_PAGE:
6556
0
      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6557
0
      if (value == MINUS_ONE)
6558
0
  return bfd_reloc_outofrange;
6559
0
      value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6560
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6561
0
      break;
6562
6563
0
    case R_MIPS_GOT_OFST:
6564
0
    case R_MICROMIPS_GOT_OFST:
6565
0
      if (local_p)
6566
0
  mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6567
0
      else
6568
0
  value = addend;
6569
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6570
0
      break;
6571
6572
0
    case R_MIPS_SUB:
6573
0
    case R_MICROMIPS_SUB:
6574
0
      value = symbol - addend;
6575
0
      value &= howto->dst_mask;
6576
0
      break;
6577
6578
0
    case R_MIPS_HIGHER:
6579
0
    case R_MICROMIPS_HIGHER:
6580
0
      value = mips_elf_higher (addend + symbol);
6581
0
      value &= howto->dst_mask;
6582
0
      break;
6583
6584
0
    case R_MIPS_HIGHEST:
6585
0
    case R_MICROMIPS_HIGHEST:
6586
0
      value = mips_elf_highest (addend + symbol);
6587
0
      value &= howto->dst_mask;
6588
0
      break;
6589
6590
0
    case R_MIPS_SCN_DISP:
6591
0
    case R_MICROMIPS_SCN_DISP:
6592
0
      value = symbol + addend - sec->output_offset;
6593
0
      value &= howto->dst_mask;
6594
0
      break;
6595
6596
0
    case R_MIPS_JALR:
6597
0
    case R_MICROMIPS_JALR:
6598
      /* This relocation is only a hint.  In some cases, we optimize
6599
   it into a bal instruction.  But we don't try to optimize
6600
   when the symbol does not resolve locally.  */
6601
0
      if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6602
0
  return bfd_reloc_continue;
6603
      /* We can't optimize cross-mode jumps either.  */
6604
0
      if (*cross_mode_jump_p)
6605
0
  return bfd_reloc_continue;
6606
0
      value = symbol + addend;
6607
      /* Neither we can non-instruction-aligned targets.  */
6608
0
      if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6609
0
  return bfd_reloc_continue;
6610
0
      break;
6611
6612
0
    case R_MIPS_PJUMP:
6613
0
    case R_MIPS_GNU_VTINHERIT:
6614
0
    case R_MIPS_GNU_VTENTRY:
6615
      /* We don't do anything with these at present.  */
6616
0
      return bfd_reloc_continue;
6617
6618
0
    default:
6619
      /* An unrecognized relocation type.  */
6620
0
      return bfd_reloc_notsupported;
6621
0
    }
6622
6623
  /* Store the VALUE for our caller.  */
6624
0
  *valuep = value;
6625
0
  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6626
0
}
6627
6628
/* It has been determined that the result of the RELOCATION is the
6629
   VALUE.  Use HOWTO to place VALUE into the output file at the
6630
   appropriate position.  The SECTION is the section to which the
6631
   relocation applies.
6632
   CROSS_MODE_JUMP_P is true if the relocation field
6633
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6634
6635
   Returns FALSE if anything goes wrong.  */
6636
6637
static bool
6638
mips_elf_perform_relocation (struct bfd_link_info *info,
6639
           reloc_howto_type *howto,
6640
           const Elf_Internal_Rela *relocation,
6641
           bfd_vma value, bfd *input_bfd,
6642
           asection *input_section, bfd_byte *contents,
6643
           bool cross_mode_jump_p)
6644
0
{
6645
0
  bfd_vma x;
6646
0
  bfd_byte *location;
6647
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6648
6649
  /* Figure out where the relocation is occurring.  */
6650
0
  location = contents + relocation->r_offset;
6651
6652
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6653
6654
  /* Obtain the current value.  */
6655
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6656
6657
  /* Clear the field we are setting.  */
6658
0
  x &= ~howto->dst_mask;
6659
6660
  /* Set the field.  */
6661
0
  x |= (value & howto->dst_mask);
6662
6663
  /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
6664
0
  if (!cross_mode_jump_p && jal_reloc_p (r_type))
6665
0
    {
6666
0
      bfd_vma opcode = x >> 26;
6667
6668
0
      if (r_type == R_MIPS16_26 ? opcode == 0x7
6669
0
    : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6670
0
    : opcode == 0x1d)
6671
0
  {
6672
0
    info->callbacks->einfo
6673
0
      (_("%X%H: unsupported JALX to the same ISA mode\n"),
6674
0
       input_bfd, input_section, relocation->r_offset);
6675
0
    return true;
6676
0
  }
6677
0
    }
6678
0
  if (cross_mode_jump_p && jal_reloc_p (r_type))
6679
0
    {
6680
0
      bool ok;
6681
0
      bfd_vma opcode = x >> 26;
6682
0
      bfd_vma jalx_opcode;
6683
6684
      /* Check to see if the opcode is already JAL or JALX.  */
6685
0
      if (r_type == R_MIPS16_26)
6686
0
  {
6687
0
    ok = ((opcode == 0x6) || (opcode == 0x7));
6688
0
    jalx_opcode = 0x7;
6689
0
  }
6690
0
      else if (r_type == R_MICROMIPS_26_S1)
6691
0
  {
6692
0
    ok = ((opcode == 0x3d) || (opcode == 0x3c));
6693
0
    jalx_opcode = 0x3c;
6694
0
  }
6695
0
      else
6696
0
  {
6697
0
    ok = ((opcode == 0x3) || (opcode == 0x1d));
6698
0
    jalx_opcode = 0x1d;
6699
0
  }
6700
6701
      /* If the opcode is not JAL or JALX, there's a problem.  We cannot
6702
   convert J or JALS to JALX.  */
6703
0
      if (!ok)
6704
0
  {
6705
0
    info->callbacks->einfo
6706
0
      (_("%X%H: unsupported jump between ISA modes; "
6707
0
         "consider recompiling with interlinking enabled\n"),
6708
0
       input_bfd, input_section, relocation->r_offset);
6709
0
    return true;
6710
0
  }
6711
6712
      /* Make this the JALX opcode.  */
6713
0
      x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6714
0
    }
6715
0
  else if (cross_mode_jump_p && b_reloc_p (r_type))
6716
0
    {
6717
0
      bool ok = false;
6718
0
      bfd_vma opcode = x >> 16;
6719
0
      bfd_vma jalx_opcode = 0;
6720
0
      bfd_vma sign_bit = 0;
6721
0
      bfd_vma addr;
6722
0
      bfd_vma dest;
6723
6724
0
      if (r_type == R_MICROMIPS_PC16_S1)
6725
0
  {
6726
0
    ok = opcode == 0x4060;
6727
0
    jalx_opcode = 0x3c;
6728
0
    sign_bit = 0x10000;
6729
0
    value <<= 1;
6730
0
  }
6731
0
      else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6732
0
  {
6733
0
    ok = opcode == 0x411;
6734
0
    jalx_opcode = 0x1d;
6735
0
    sign_bit = 0x20000;
6736
0
    value <<= 2;
6737
0
  }
6738
6739
0
      if (ok && !bfd_link_pic (info))
6740
0
  {
6741
0
    addr = (input_section->output_section->vma
6742
0
      + input_section->output_offset
6743
0
      + relocation->r_offset
6744
0
      + 4);
6745
0
    dest = (addr
6746
0
      + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6747
6748
0
    if ((addr >> 28) << 28 != (dest >> 28) << 28)
6749
0
      {
6750
0
        info->callbacks->einfo
6751
0
    (_("%X%H: cannot convert branch between ISA modes "
6752
0
       "to JALX: relocation out of range\n"),
6753
0
     input_bfd, input_section, relocation->r_offset);
6754
0
        return true;
6755
0
      }
6756
6757
    /* Make this the JALX opcode.  */
6758
0
    x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6759
0
  }
6760
0
      else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6761
0
  {
6762
0
    info->callbacks->einfo
6763
0
      (_("%X%H: unsupported branch between ISA modes\n"),
6764
0
       input_bfd, input_section, relocation->r_offset);
6765
0
    return true;
6766
0
  }
6767
0
    }
6768
6769
  /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6770
     range.  */
6771
0
  if (!bfd_link_relocatable (info)
6772
0
      && !cross_mode_jump_p
6773
0
      && ((JAL_TO_BAL_P (input_bfd)
6774
0
     && r_type == R_MIPS_26
6775
0
     && (x >> 26) == 0x3)     /* jal addr */
6776
0
    || (JALR_TO_BAL_P (input_bfd)
6777
0
        && r_type == R_MIPS_JALR
6778
0
        && x == 0x0320f809)   /* jalr t9 */
6779
0
    || (JR_TO_B_P (input_bfd)
6780
0
        && r_type == R_MIPS_JALR
6781
0
        && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6782
0
    {
6783
0
      bfd_vma addr;
6784
0
      bfd_vma dest;
6785
0
      bfd_signed_vma off;
6786
6787
0
      addr = (input_section->output_section->vma
6788
0
        + input_section->output_offset
6789
0
        + relocation->r_offset
6790
0
        + 4);
6791
0
      if (r_type == R_MIPS_26)
6792
0
  dest = (value << 2) | ((addr >> 28) << 28);
6793
0
      else
6794
0
  dest = value;
6795
0
      off = dest - addr;
6796
0
      if (off <= 0x1ffff && off >= -0x20000)
6797
0
  {
6798
0
    if ((x & ~1) == 0x03200008)   /* jr t9 / jalr zero, t9 */
6799
0
      x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
6800
0
    else
6801
0
      x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
6802
0
  }
6803
0
    }
6804
6805
  /* Put the value into the output.  */
6806
0
  mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6807
6808
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6809
0
             location);
6810
6811
0
  return true;
6812
0
}
6813

6814
/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
6815
   is the original relocation, which is now being transformed into a
6816
   dynamic relocation.  The ADDENDP is adjusted if necessary; the
6817
   caller should store the result in place of the original addend.  */
6818
6819
static bool
6820
mips_elf_create_dynamic_relocation (bfd *output_bfd,
6821
            struct bfd_link_info *info,
6822
            const Elf_Internal_Rela *rel,
6823
            struct mips_elf_link_hash_entry *h,
6824
            asection *sec, bfd_vma symbol,
6825
            bfd_vma *addendp, asection *input_section)
6826
0
{
6827
0
  Elf_Internal_Rela outrel[3];
6828
0
  asection *sreloc;
6829
0
  bfd *dynobj;
6830
0
  int r_type;
6831
0
  long indx;
6832
0
  bool defined_p;
6833
0
  struct mips_elf_link_hash_table *htab;
6834
6835
0
  htab = mips_elf_hash_table (info);
6836
0
  BFD_ASSERT (htab != NULL);
6837
6838
0
  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6839
0
  dynobj = elf_hash_table (info)->dynobj;
6840
0
  sreloc = mips_elf_rel_dyn_section (info, false);
6841
0
  BFD_ASSERT (sreloc != NULL);
6842
0
  BFD_ASSERT (sreloc->contents != NULL);
6843
0
  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6844
0
        < sreloc->size);
6845
6846
0
  outrel[0].r_offset =
6847
0
    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6848
0
  if (ABI_64_P (output_bfd))
6849
0
    {
6850
0
      outrel[1].r_offset =
6851
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6852
0
      outrel[2].r_offset =
6853
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6854
0
    }
6855
6856
0
  if (outrel[0].r_offset == MINUS_ONE)
6857
    /* The relocation field has been deleted.  */
6858
0
    return true;
6859
6860
0
  if (outrel[0].r_offset == MINUS_TWO)
6861
0
    {
6862
      /* The relocation field has been converted into a relative value of
6863
   some sort.  Functions like _bfd_elf_write_section_eh_frame expect
6864
   the field to be fully relocated, so add in the symbol's value.  */
6865
0
      *addendp += symbol;
6866
0
      return true;
6867
0
    }
6868
6869
  /* We must now calculate the dynamic symbol table index to use
6870
     in the relocation.  */
6871
0
  if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6872
0
    {
6873
0
      BFD_ASSERT (htab->root.target_os == is_vxworks
6874
0
      || h->global_got_area != GGA_NONE);
6875
0
      indx = h->root.dynindx;
6876
0
      if (SGI_COMPAT (output_bfd))
6877
0
  defined_p = h->root.def_regular;
6878
0
      else
6879
  /* ??? glibc's ld.so just adds the final GOT entry to the
6880
     relocation field.  It therefore treats relocs against
6881
     defined symbols in the same way as relocs against
6882
     undefined symbols.  */
6883
0
  defined_p = false;
6884
0
    }
6885
0
  else
6886
0
    {
6887
0
      if (sec != NULL && bfd_is_abs_section (sec))
6888
0
  indx = 0;
6889
0
      else if (sec == NULL || sec->owner == NULL)
6890
0
  {
6891
0
    BFD_ASSERT (0);
6892
0
    bfd_set_error (bfd_error_bad_value);
6893
0
    return false;
6894
0
  }
6895
0
      else
6896
0
  {
6897
0
    indx = elf_section_data (sec->output_section)->dynindx;
6898
0
    if (indx == 0)
6899
0
      {
6900
0
        asection *osec = htab->root.text_index_section;
6901
0
        indx = elf_section_data (osec)->dynindx;
6902
0
      }
6903
0
    if (indx == 0)
6904
0
      abort ();
6905
0
  }
6906
6907
      /* Instead of generating a relocation using the section
6908
   symbol, we may as well make it a fully relative
6909
   relocation.  We want to avoid generating relocations to
6910
   local symbols because we used to generate them
6911
   incorrectly, without adding the original symbol value,
6912
   which is mandated by the ABI for section symbols.  In
6913
   order to give dynamic loaders and applications time to
6914
   phase out the incorrect use, we refrain from emitting
6915
   section-relative relocations.  It's not like they're
6916
   useful, after all.  This should be a bit more efficient
6917
   as well.  */
6918
      /* ??? Although this behavior is compatible with glibc's ld.so,
6919
   the ABI says that relocations against STN_UNDEF should have
6920
   a symbol value of 0.  Irix rld honors this, so relocations
6921
   against STN_UNDEF have no effect.  */
6922
0
      if (!SGI_COMPAT (output_bfd))
6923
0
  indx = 0;
6924
0
      defined_p = true;
6925
0
    }
6926
6927
  /* If the relocation was previously an absolute relocation and
6928
     this symbol will not be referred to by the relocation, we must
6929
     adjust it by the value we give it in the dynamic symbol table.
6930
     Otherwise leave the job up to the dynamic linker.  */
6931
0
  if (defined_p && r_type != R_MIPS_REL32)
6932
0
    *addendp += symbol;
6933
6934
0
  if (htab->root.target_os == is_vxworks)
6935
    /* VxWorks uses non-relative relocations for this.  */
6936
0
    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6937
0
  else
6938
    /* The relocation is always an REL32 relocation because we don't
6939
       know where the shared library will wind up at load-time.  */
6940
0
    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6941
0
           R_MIPS_REL32);
6942
6943
  /* For strict adherence to the ABI specification, we should
6944
     generate a R_MIPS_64 relocation record by itself before the
6945
     _REL32/_64 record as well, such that the addend is read in as
6946
     a 64-bit value (REL32 is a 32-bit relocation, after all).
6947
     However, since none of the existing ELF64 MIPS dynamic
6948
     loaders seems to care, we don't waste space with these
6949
     artificial relocations.  If this turns out to not be true,
6950
     mips_elf_allocate_dynamic_relocation() should be tweaked so
6951
     as to make room for a pair of dynamic relocations per
6952
     invocation if ABI_64_P, and here we should generate an
6953
     additional relocation record with R_MIPS_64 by itself for a
6954
     NULL symbol before this relocation record.  */
6955
0
  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6956
0
         ABI_64_P (output_bfd)
6957
0
         ? R_MIPS_64
6958
0
         : R_MIPS_NONE);
6959
0
  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6960
6961
  /* Adjust the output offset of the relocation to reference the
6962
     correct location in the output file.  */
6963
0
  outrel[0].r_offset += (input_section->output_section->vma
6964
0
       + input_section->output_offset);
6965
0
  outrel[1].r_offset += (input_section->output_section->vma
6966
0
       + input_section->output_offset);
6967
0
  outrel[2].r_offset += (input_section->output_section->vma
6968
0
       + input_section->output_offset);
6969
6970
  /* Put the relocation back out.  We have to use the special
6971
     relocation outputter in the 64-bit case since the 64-bit
6972
     relocation format is non-standard.  */
6973
0
  if (ABI_64_P (output_bfd))
6974
0
    {
6975
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6976
0
  (output_bfd, &outrel[0],
6977
0
   (sreloc->contents
6978
0
    + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6979
0
    }
6980
0
  else if (htab->root.target_os == is_vxworks)
6981
0
    {
6982
      /* VxWorks uses RELA rather than REL dynamic relocations.  */
6983
0
      outrel[0].r_addend = *addendp;
6984
0
      bfd_elf32_swap_reloca_out
6985
0
  (output_bfd, &outrel[0],
6986
0
   (sreloc->contents
6987
0
    + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6988
0
    }
6989
0
  else
6990
0
    bfd_elf32_swap_reloc_out
6991
0
      (output_bfd, &outrel[0],
6992
0
       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6993
6994
  /* We've now added another relocation.  */
6995
0
  ++sreloc->reloc_count;
6996
6997
  /* Make sure the output section is writable.  The dynamic linker
6998
     will be writing to it.  */
6999
0
  elf_section_data (input_section->output_section)->this_hdr.sh_flags
7000
0
    |= SHF_WRITE;
7001
7002
  /* On IRIX5, make an entry of compact relocation info.  */
7003
0
  if (IRIX_COMPAT (output_bfd) == ict_irix5)
7004
0
    {
7005
0
      asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
7006
0
      bfd_byte *cr;
7007
7008
0
      if (scpt)
7009
0
  {
7010
0
    Elf32_crinfo cptrel;
7011
7012
0
    mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
7013
0
    cptrel.vaddr = (rel->r_offset
7014
0
        + input_section->output_section->vma
7015
0
        + input_section->output_offset);
7016
0
    if (r_type == R_MIPS_REL32)
7017
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
7018
0
    else
7019
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
7020
0
    mips_elf_set_cr_dist2to (cptrel, 0);
7021
0
    cptrel.konst = *addendp;
7022
7023
0
    cr = (scpt->contents
7024
0
    + sizeof (Elf32_External_compact_rel));
7025
0
    mips_elf_set_cr_relvaddr (cptrel, 0);
7026
0
    bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
7027
0
             ((Elf32_External_crinfo *) cr
7028
0
              + scpt->reloc_count));
7029
0
    ++scpt->reloc_count;
7030
0
  }
7031
0
    }
7032
7033
  /* If we've written this relocation for a readonly section,
7034
     we need to set DF_TEXTREL again, so that we do not delete the
7035
     DT_TEXTREL tag.  */
7036
0
  if (MIPS_ELF_READONLY_SECTION (input_section))
7037
0
    info->flags |= DF_TEXTREL;
7038
7039
0
  return true;
7040
0
}
7041

7042
/* Return the MACH for a MIPS e_flags value.  */
7043
7044
unsigned long
7045
_bfd_elf_mips_mach (flagword flags)
7046
11.9k
{
7047
11.9k
  switch (flags & EF_MIPS_MACH)
7048
11.9k
    {
7049
34
    case EF_MIPS_MACH_3900:
7050
34
      return bfd_mach_mips3900;
7051
7052
0
    case EF_MIPS_MACH_4010:
7053
0
      return bfd_mach_mips4010;
7054
7055
3
    case EF_MIPS_MACH_ALLEGREX:
7056
3
      return bfd_mach_mips_allegrex;
7057
7058
21
    case EF_MIPS_MACH_4100:
7059
21
      return bfd_mach_mips4100;
7060
7061
29
    case EF_MIPS_MACH_4111:
7062
29
      return bfd_mach_mips4111;
7063
7064
109
    case EF_MIPS_MACH_4120:
7065
109
      return bfd_mach_mips4120;
7066
7067
55
    case EF_MIPS_MACH_4650:
7068
55
      return bfd_mach_mips4650;
7069
7070
20
    case EF_MIPS_MACH_TX49:
7071
20
      return bfd_mach_mips_tx49;
7072
7073
0
    case EF_MIPS_MACH_5400:
7074
0
      return bfd_mach_mips5400;
7075
7076
11
    case EF_MIPS_MACH_5500:
7077
11
      return bfd_mach_mips5500;
7078
7079
122
    case EF_MIPS_MACH_5900:
7080
122
      return bfd_mach_mips5900;
7081
7082
17
    case EF_MIPS_MACH_9000:
7083
17
      return bfd_mach_mips9000;
7084
7085
26
    case EF_MIPS_MACH_SB1:
7086
26
      return bfd_mach_mips_sb1;
7087
7088
7
    case EF_MIPS_MACH_LS2E:
7089
7
      return bfd_mach_mips_loongson_2e;
7090
7091
51
    case EF_MIPS_MACH_LS2F:
7092
51
      return bfd_mach_mips_loongson_2f;
7093
7094
17
    case EF_MIPS_MACH_GS464:
7095
17
      return bfd_mach_mips_gs464;
7096
7097
0
    case EF_MIPS_MACH_GS464E:
7098
0
      return bfd_mach_mips_gs464e;
7099
7100
32
    case EF_MIPS_MACH_GS264E:
7101
32
      return bfd_mach_mips_gs264e;
7102
7103
0
    case EF_MIPS_MACH_OCTEON3:
7104
0
      return bfd_mach_mips_octeon3;
7105
7106
37
    case EF_MIPS_MACH_OCTEON2:
7107
37
      return bfd_mach_mips_octeon2;
7108
7109
10
    case EF_MIPS_MACH_OCTEON:
7110
10
      return bfd_mach_mips_octeon;
7111
7112
0
    case EF_MIPS_MACH_XLR:
7113
0
      return bfd_mach_mips_xlr;
7114
7115
3
    case EF_MIPS_MACH_IAMR2:
7116
3
      return bfd_mach_mips_interaptiv_mr2;
7117
7118
11.2k
    default:
7119
11.2k
      switch (flags & EF_MIPS_ARCH)
7120
11.2k
  {
7121
694
  default:
7122
8.49k
  case EF_MIPS_ARCH_1:
7123
8.49k
    return bfd_mach_mips3000;
7124
7125
113
  case EF_MIPS_ARCH_2:
7126
113
    return bfd_mach_mips6000;
7127
7128
623
  case EF_MIPS_ARCH_3:
7129
623
    return bfd_mach_mips4000;
7130
7131
491
  case EF_MIPS_ARCH_4:
7132
491
    return bfd_mach_mips8000;
7133
7134
71
  case EF_MIPS_ARCH_5:
7135
71
    return bfd_mach_mips5;
7136
7137
129
  case EF_MIPS_ARCH_32:
7138
129
    return bfd_mach_mipsisa32;
7139
7140
264
  case EF_MIPS_ARCH_64:
7141
264
    return bfd_mach_mipsisa64;
7142
7143
117
  case EF_MIPS_ARCH_32R2:
7144
117
    return bfd_mach_mipsisa32r2;
7145
7146
72
  case EF_MIPS_ARCH_64R2:
7147
72
    return bfd_mach_mipsisa64r2;
7148
7149
609
  case EF_MIPS_ARCH_32R6:
7150
609
    return bfd_mach_mipsisa32r6;
7151
7152
315
  case EF_MIPS_ARCH_64R6:
7153
315
    return bfd_mach_mipsisa64r6;
7154
11.2k
  }
7155
11.9k
    }
7156
7157
0
  return 0;
7158
11.9k
}
7159
7160
/* Return printable name for ABI.  */
7161
7162
static inline char *
7163
elf_mips_abi_name (bfd *abfd)
7164
0
{
7165
0
  flagword flags;
7166
7167
0
  flags = elf_elfheader (abfd)->e_flags;
7168
0
  switch (flags & EF_MIPS_ABI)
7169
0
    {
7170
0
    case 0:
7171
0
      if (ABI_N32_P (abfd))
7172
0
  return "N32";
7173
0
      else if (ABI_64_P (abfd))
7174
0
  return "64";
7175
0
      else
7176
0
  return "none";
7177
0
    case EF_MIPS_ABI_O32:
7178
0
      return "O32";
7179
0
    case EF_MIPS_ABI_O64:
7180
0
      return "O64";
7181
0
    case EF_MIPS_ABI_EABI32:
7182
0
      return "EABI32";
7183
0
    case EF_MIPS_ABI_EABI64:
7184
0
      return "EABI64";
7185
0
    default:
7186
0
      return "unknown abi";
7187
0
    }
7188
0
}
7189

7190
/* MIPS ELF uses two common sections.  One is the usual one, and the
7191
   other is for small objects.  All the small objects are kept
7192
   together, and then referenced via the gp pointer, which yields
7193
   faster assembler code.  This is what we use for the small common
7194
   section.  This approach is copied from ecoff.c.  */
7195
static asection mips_elf_scom_section;
7196
static const asymbol mips_elf_scom_symbol =
7197
  GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7198
static asection mips_elf_scom_section =
7199
  BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7200
        ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7201
7202
/* MIPS ELF also uses an acommon section, which represents an
7203
   allocated common symbol which may be overridden by a
7204
   definition in a shared library.  */
7205
static asection mips_elf_acom_section;
7206
static const asymbol mips_elf_acom_symbol =
7207
  GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7208
static asection mips_elf_acom_section =
7209
  BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7210
        ".acommon", 0, SEC_ALLOC);
7211
7212
/* This is used for both the 32-bit and the 64-bit ABI.  */
7213
7214
void
7215
_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7216
18.6k
{
7217
18.6k
  elf_symbol_type *elfsym;
7218
7219
  /* Handle the special MIPS section numbers that a symbol may use.  */
7220
18.6k
  elfsym = (elf_symbol_type *) asym;
7221
18.6k
  switch (elfsym->internal_elf_sym.st_shndx)
7222
18.6k
    {
7223
62
    case SHN_MIPS_ACOMMON:
7224
      /* This section is used in a dynamically linked executable file.
7225
   It is an allocated common section.  The dynamic linker can
7226
   either resolve these symbols to something in a shared
7227
   library, or it can just leave them here.  For our purposes,
7228
   we can consider these symbols to be in a new section.  */
7229
62
      asym->section = &mips_elf_acom_section;
7230
62
      break;
7231
7232
31
    case SHN_COMMON:
7233
      /* Common symbols less than the GP size are automatically
7234
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7235
31
      if (asym->value > elf_gp_size (abfd)
7236
16
    || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7237
16
    || IRIX_COMPAT (abfd) == ict_irix6
7238
2
    || strcmp (asym->name, "__gnu_lto_slim") == 0)
7239
29
  break;
7240
      /* Fall through.  */
7241
67
    case SHN_MIPS_SCOMMON:
7242
67
      asym->section = &mips_elf_scom_section;
7243
67
      asym->value = elfsym->internal_elf_sym.st_size;
7244
67
      break;
7245
7246
6
    case SHN_MIPS_SUNDEFINED:
7247
6
      asym->section = bfd_und_section_ptr;
7248
6
      break;
7249
7250
64
    case SHN_MIPS_TEXT:
7251
64
      {
7252
64
  asection *section = bfd_get_section_by_name (abfd, ".text");
7253
7254
64
  if (section != NULL)
7255
28
    {
7256
28
      asym->section = section;
7257
      /* MIPS_TEXT is a bit special, the address is not an offset
7258
         to the base of the .text section.  So subtract the section
7259
         base address to make it an offset.  */
7260
28
      asym->value -= section->vma;
7261
28
    }
7262
64
      }
7263
64
      break;
7264
7265
20
    case SHN_MIPS_DATA:
7266
20
      {
7267
20
  asection *section = bfd_get_section_by_name (abfd, ".data");
7268
7269
20
  if (section != NULL)
7270
5
    {
7271
5
      asym->section = section;
7272
      /* MIPS_DATA is a bit special, the address is not an offset
7273
         to the base of the .data section.  So subtract the section
7274
         base address to make it an offset.  */
7275
5
      asym->value -= section->vma;
7276
5
    }
7277
20
      }
7278
20
      break;
7279
18.6k
    }
7280
7281
  /* If this is an odd-valued function symbol, assume it's a MIPS16
7282
     or microMIPS one.  */
7283
18.6k
  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7284
1.10k
      && (asym->value & 1) != 0)
7285
143
    {
7286
143
      asym->value--;
7287
143
      if (MICROMIPS_P (abfd))
7288
19
  elfsym->internal_elf_sym.st_other
7289
19
    = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7290
124
      else
7291
124
  elfsym->internal_elf_sym.st_other
7292
124
    = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7293
143
    }
7294
18.6k
}
7295

7296
/* Implement elf_backend_eh_frame_address_size.  This differs from
7297
   the default in the way it handles EABI64.
7298
7299
   EABI64 was originally specified as an LP64 ABI, and that is what
7300
   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
7301
   historically accepted the combination of -mabi=eabi and -mlong32,
7302
   and this ILP32 variation has become semi-official over time.
7303
   Both forms use elf32 and have pointer-sized FDE addresses.
7304
7305
   If an EABI object was generated by GCC 4.0 or above, it will have
7306
   an empty .gcc_compiled_longXX section, where XX is the size of longs
7307
   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
7308
   have no special marking to distinguish them from LP64 objects.
7309
7310
   We don't want users of the official LP64 ABI to be punished for the
7311
   existence of the ILP32 variant, but at the same time, we don't want
7312
   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7313
   We therefore take the following approach:
7314
7315
      - If ABFD contains a .gcc_compiled_longXX section, use it to
7316
  determine the pointer size.
7317
7318
      - Otherwise check the type of the first relocation.  Assume that
7319
  the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7320
7321
      - Otherwise punt.
7322
7323
   The second check is enough to detect LP64 objects generated by pre-4.0
7324
   compilers because, in the kind of output generated by those compilers,
7325
   the first relocation will be associated with either a CIE personality
7326
   routine or an FDE start address.  Furthermore, the compilers never
7327
   used a special (non-pointer) encoding for this ABI.
7328
7329
   Checking the relocation type should also be safe because there is no
7330
   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
7331
   did so.  */
7332
7333
unsigned int
7334
_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7335
0
{
7336
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7337
0
    return 8;
7338
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
7339
0
    {
7340
0
      bool long32_p, long64_p;
7341
7342
0
      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7343
0
      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7344
0
      if (long32_p && long64_p)
7345
0
  return 0;
7346
0
      if (long32_p)
7347
0
  return 4;
7348
0
      if (long64_p)
7349
0
  return 8;
7350
7351
0
      if (sec->reloc_count > 0)
7352
0
  {
7353
    /* Load the relocations for this section.  */
7354
0
    Elf_Internal_Rela *internal_relocs
7355
0
      = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
7356
0
    if (internal_relocs == NULL)
7357
0
      return 0;
7358
7359
0
    unsigned int size = 0;
7360
0
    if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
7361
0
      size = 8;
7362
7363
0
    if (elf_section_data (sec)->relocs != internal_relocs)
7364
0
      free (internal_relocs);
7365
7366
0
    return size;
7367
0
  }
7368
7369
0
      return 0;
7370
0
    }
7371
0
  return 4;
7372
0
}
7373

7374
/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7375
   relocations against two unnamed section symbols to resolve to the
7376
   same address.  For example, if we have code like:
7377
7378
  lw  $4,%got_disp(.data)($gp)
7379
  lw  $25,%got_disp(.text)($gp)
7380
  jalr  $25
7381
7382
   then the linker will resolve both relocations to .data and the program
7383
   will jump there rather than to .text.
7384
7385
   We can work around this problem by giving names to local section symbols.
7386
   This is also what the MIPSpro tools do.  */
7387
7388
bool
7389
_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7390
9
{
7391
9
  return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7392
9
}
7393

7394
/* Work over a section just before writing it out.  This routine is
7395
   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
7396
   sections that need the SHF_MIPS_GPREL flag by name; there has to be
7397
   a better way.  */
7398
7399
bool
7400
_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7401
316
{
7402
316
  if (hdr->sh_type == SHT_MIPS_REGINFO
7403
0
      && hdr->sh_size > 0)
7404
0
    {
7405
0
      bfd_byte buf[4];
7406
7407
0
      BFD_ASSERT (hdr->contents == NULL);
7408
7409
0
      if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7410
0
  {
7411
0
    _bfd_error_handler
7412
0
      (_("%pB: incorrect `.reginfo' section size; "
7413
0
         "expected %" PRIu64 ", got %" PRIu64),
7414
0
       abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7415
0
       (uint64_t) hdr->sh_size);
7416
0
    bfd_set_error (bfd_error_bad_value);
7417
0
    return false;
7418
0
  }
7419
7420
0
      if (bfd_seek (abfd,
7421
0
        hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7422
0
        SEEK_SET) != 0)
7423
0
  return false;
7424
0
      H_PUT_32 (abfd, elf_gp (abfd), buf);
7425
0
      if (bfd_write (buf, 4, abfd) != 4)
7426
0
  return false;
7427
0
    }
7428
7429
316
  if (hdr->sh_type == SHT_MIPS_OPTIONS
7430
0
      && hdr->bfd_section != NULL
7431
0
      && mips_elf_section_data (hdr->bfd_section) != NULL
7432
0
      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7433
0
    {
7434
0
      bfd_byte *contents, *l, *lend;
7435
7436
      /* We stored the section contents in the tdata field in the
7437
   set_section_contents routine.  We save the section contents
7438
   so that we don't have to read them again.
7439
   At this point we know that elf_gp is set, so we can look
7440
   through the section contents to see if there is an
7441
   ODK_REGINFO structure.  */
7442
7443
0
      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7444
0
      l = contents;
7445
0
      lend = contents + hdr->sh_size;
7446
0
      while (l + sizeof (Elf_External_Options) <= lend)
7447
0
  {
7448
0
    Elf_Internal_Options intopt;
7449
7450
0
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7451
0
          &intopt);
7452
0
    if (intopt.size < sizeof (Elf_External_Options))
7453
0
      {
7454
0
        _bfd_error_handler
7455
    /* xgettext:c-format */
7456
0
    (_("%pB: warning: bad `%s' option size %u smaller than"
7457
0
       " its header"),
7458
0
    abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7459
0
        break;
7460
0
      }
7461
0
    if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7462
0
      {
7463
0
        bfd_byte buf[8];
7464
7465
0
        if (bfd_seek (abfd,
7466
0
          (hdr->sh_offset
7467
0
           + (l - contents)
7468
0
           + sizeof (Elf_External_Options)
7469
0
           + (sizeof (Elf64_External_RegInfo) - 8)),
7470
0
           SEEK_SET) != 0)
7471
0
    return false;
7472
0
        H_PUT_64 (abfd, elf_gp (abfd), buf);
7473
0
        if (bfd_write (buf, 8, abfd) != 8)
7474
0
    return false;
7475
0
      }
7476
0
    else if (intopt.kind == ODK_REGINFO)
7477
0
      {
7478
0
        bfd_byte buf[4];
7479
7480
0
        if (bfd_seek (abfd,
7481
0
          (hdr->sh_offset
7482
0
           + (l - contents)
7483
0
           + sizeof (Elf_External_Options)
7484
0
           + (sizeof (Elf32_External_RegInfo) - 4)),
7485
0
          SEEK_SET) != 0)
7486
0
    return false;
7487
0
        H_PUT_32 (abfd, elf_gp (abfd), buf);
7488
0
        if (bfd_write (buf, 4, abfd) != 4)
7489
0
    return false;
7490
0
      }
7491
0
    l += intopt.size;
7492
0
  }
7493
0
    }
7494
7495
316
  if (hdr->bfd_section != NULL)
7496
203
    {
7497
203
      const char *name = bfd_section_name (hdr->bfd_section);
7498
7499
      /* .sbss is not handled specially here because the GNU/Linux
7500
   prelinker can convert .sbss from NOBITS to PROGBITS and
7501
   changing it back to NOBITS breaks the binary.  The entry in
7502
   _bfd_mips_elf_special_sections will ensure the correct flags
7503
   are set on .sbss if BFD creates it without reading it from an
7504
   input file, and without special handling here the flags set
7505
   on it in an input file will be followed.  */
7506
203
      if (strcmp (name, ".sdata") == 0
7507
203
    || strcmp (name, ".lit8") == 0
7508
203
    || strcmp (name, ".lit4") == 0)
7509
1
  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7510
202
      else if (strcmp (name, ".srdata") == 0)
7511
0
  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7512
202
      else if (strcmp (name, ".compact_rel") == 0)
7513
0
  hdr->sh_flags = 0;
7514
202
      else if (strcmp (name, ".rtproc") == 0)
7515
0
  {
7516
0
    if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7517
0
      {
7518
0
        unsigned int adjust;
7519
7520
0
        adjust = hdr->sh_size % hdr->sh_addralign;
7521
0
        if (adjust != 0)
7522
0
    hdr->sh_size += hdr->sh_addralign - adjust;
7523
0
      }
7524
0
  }
7525
203
    }
7526
7527
316
  return true;
7528
316
}
7529
7530
/* Handle a MIPS specific section when reading an object file.  This
7531
   is called when elfcode.h finds a section with an unknown type.
7532
   This routine supports both the 32-bit and 64-bit ELF ABI.  */
7533
7534
bool
7535
_bfd_mips_elf_section_from_shdr (bfd *abfd,
7536
         Elf_Internal_Shdr *hdr,
7537
         const char *name,
7538
         int shindex)
7539
77.9k
{
7540
77.9k
  flagword flags = 0;
7541
7542
  /* There ought to be a place to keep ELF backend specific flags, but
7543
     at the moment there isn't one.  We just keep track of the
7544
     sections by their name, instead.  Fortunately, the ABI gives
7545
     suggested names for all the MIPS specific sections, so we will
7546
     probably get away with this.  */
7547
77.9k
  switch (hdr->sh_type)
7548
77.9k
    {
7549
73
    case SHT_MIPS_LIBLIST:
7550
73
      if (strcmp (name, ".liblist") != 0)
7551
73
  return false;
7552
0
      break;
7553
91
    case SHT_MIPS_MSYM:
7554
91
      if (strcmp (name, ".msym") != 0)
7555
91
  return false;
7556
0
      break;
7557
51
    case SHT_MIPS_CONFLICT:
7558
51
      if (strcmp (name, ".conflict") != 0)
7559
51
  return false;
7560
0
      break;
7561
53
    case SHT_MIPS_GPTAB:
7562
53
      if (! startswith (name, ".gptab."))
7563
39
  return false;
7564
14
      break;
7565
45
    case SHT_MIPS_UCODE:
7566
45
      if (strcmp (name, ".ucode") != 0)
7567
45
  return false;
7568
0
      break;
7569
69
    case SHT_MIPS_DEBUG:
7570
69
      if (strcmp (name, ".mdebug") != 0)
7571
55
  return false;
7572
14
      flags = SEC_DEBUGGING;
7573
14
      break;
7574
58
    case SHT_MIPS_REGINFO:
7575
58
      if (strcmp (name, ".reginfo") != 0
7576
0
    || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7577
58
  return false;
7578
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7579
0
      break;
7580
50
    case SHT_MIPS_IFACE:
7581
50
      if (strcmp (name, ".MIPS.interfaces") != 0)
7582
50
  return false;
7583
0
      break;
7584
64
    case SHT_MIPS_CONTENT:
7585
64
      if (! startswith (name, ".MIPS.content"))
7586
52
  return false;
7587
12
      break;
7588
304
    case SHT_MIPS_OPTIONS:
7589
304
      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7590
99
  return false;
7591
205
      break;
7592
205
    case SHT_MIPS_ABIFLAGS:
7593
49
      if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7594
49
  return false;
7595
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7596
0
      break;
7597
423
    case SHT_MIPS_DWARF:
7598
423
      if (! startswith (name, ".debug_")
7599
386
         && ! startswith (name, ".gnu.debuglto_.debug_")
7600
357
         && ! startswith (name, ".zdebug_")
7601
323
         && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7602
294
  return false;
7603
129
      break;
7604
129
    case SHT_MIPS_SYMBOL_LIB:
7605
37
      if (strcmp (name, ".MIPS.symlib") != 0)
7606
37
  return false;
7607
0
      break;
7608
70
    case SHT_MIPS_EVENTS:
7609
70
      if (! startswith (name, ".MIPS.events")
7610
55
    && ! startswith (name, ".MIPS.post_rel"))
7611
45
  return false;
7612
25
      break;
7613
44
    case SHT_MIPS_XHASH:
7614
44
      if (strcmp (name, ".MIPS.xhash") != 0)
7615
44
  return false;
7616
76.4k
    default:
7617
76.4k
      break;
7618
77.9k
    }
7619
7620
76.8k
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7621
157
    return false;
7622
7623
76.6k
  if (hdr->sh_flags & SHF_MIPS_GPREL)
7624
16.0k
    flags |= SEC_SMALL_DATA;
7625
7626
76.6k
  if (flags)
7627
16.1k
    {
7628
16.1k
      if (!bfd_set_section_flags (hdr->bfd_section,
7629
16.1k
          (bfd_section_flags (hdr->bfd_section)
7630
16.1k
           | flags)))
7631
0
  return false;
7632
16.1k
    }
7633
7634
76.6k
  if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7635
0
    {
7636
0
      Elf_External_ABIFlags_v0 ext;
7637
7638
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7639
0
              &ext, 0, sizeof ext))
7640
0
  return false;
7641
0
      bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7642
0
          &mips_elf_tdata (abfd)->abiflags);
7643
0
      if (mips_elf_tdata (abfd)->abiflags.version != 0)
7644
0
  return false;
7645
0
      mips_elf_tdata (abfd)->abiflags_valid = true;
7646
0
    }
7647
7648
  /* FIXME: We should record sh_info for a .gptab section.  */
7649
7650
  /* For a .reginfo section, set the gp value in the tdata information
7651
     from the contents of this section.  We need the gp value while
7652
     processing relocs, so we just get it now.  The .reginfo section
7653
     is not used in the 64-bit MIPS ELF ABI.  */
7654
76.6k
  if (hdr->sh_type == SHT_MIPS_REGINFO)
7655
0
    {
7656
0
      Elf32_External_RegInfo ext;
7657
0
      Elf32_RegInfo s;
7658
7659
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7660
0
              &ext, 0, sizeof ext))
7661
0
  return false;
7662
0
      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7663
0
      elf_gp (abfd) = s.ri_gp_value;
7664
0
    }
7665
7666
  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7667
     set the gp value based on what we find.  We may see both
7668
     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7669
     they should agree.  */
7670
76.6k
  if (hdr->sh_type == SHT_MIPS_OPTIONS)
7671
205
    {
7672
205
      bfd_byte *contents, *l, *lend;
7673
7674
205
      if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7675
29
  {
7676
29
    free (contents);
7677
29
    return false;
7678
29
  }
7679
176
      l = contents;
7680
176
      lend = contents + hdr->sh_size;
7681
1.12k
      while (l + sizeof (Elf_External_Options) <= lend)
7682
1.06k
  {
7683
1.06k
    Elf_Internal_Options intopt;
7684
7685
1.06k
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7686
1.06k
          &intopt);
7687
1.06k
    if (intopt.size < sizeof (Elf_External_Options))
7688
67
      {
7689
109
      bad_opt:
7690
109
        _bfd_error_handler
7691
    /* xgettext:c-format */
7692
109
    (_("%pB: warning: truncated `%s' option"),
7693
109
     abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7694
109
        break;
7695
67
      }
7696
993
    if (intopt.kind == ODK_REGINFO)
7697
118
      {
7698
118
        if (ABI_64_P (abfd))
7699
118
    {
7700
118
      Elf64_Internal_RegInfo intreg;
7701
118
      size_t needed = (sizeof (Elf_External_Options)
7702
118
           + sizeof (Elf64_External_RegInfo));
7703
118
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7704
42
        goto bad_opt;
7705
76
      bfd_mips_elf64_swap_reginfo_in
7706
76
        (abfd,
7707
76
         ((Elf64_External_RegInfo *)
7708
76
          (l + sizeof (Elf_External_Options))),
7709
76
         &intreg);
7710
76
      elf_gp (abfd) = intreg.ri_gp_value;
7711
76
    }
7712
0
        else
7713
0
    {
7714
0
      Elf32_RegInfo intreg;
7715
0
      size_t needed = (sizeof (Elf_External_Options)
7716
0
           + sizeof (Elf32_External_RegInfo));
7717
0
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7718
0
        goto bad_opt;
7719
0
      bfd_mips_elf32_swap_reginfo_in
7720
0
        (abfd,
7721
0
         ((Elf32_External_RegInfo *)
7722
0
          (l + sizeof (Elf_External_Options))),
7723
0
         &intreg);
7724
0
      elf_gp (abfd) = intreg.ri_gp_value;
7725
0
    }
7726
118
      }
7727
951
    l += intopt.size;
7728
951
  }
7729
176
      free (contents);
7730
176
    }
7731
7732
76.6k
  return true;
7733
76.6k
}
7734
7735
/* Set the correct type for a MIPS ELF section.  We do this by the
7736
   section name, which is a hack, but ought to work.  This routine is
7737
   used by both the 32-bit and the 64-bit ABI.  */
7738
7739
bool
7740
_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7741
209
{
7742
209
  const char *name = bfd_section_name (sec);
7743
7744
209
  if (strcmp (name, ".liblist") == 0)
7745
0
    {
7746
0
      hdr->sh_type = SHT_MIPS_LIBLIST;
7747
0
      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7748
      /* The sh_link field is set in final_write_processing.  */
7749
0
    }
7750
209
  else if (strcmp (name, ".conflict") == 0)
7751
0
    hdr->sh_type = SHT_MIPS_CONFLICT;
7752
209
  else if (startswith (name, ".gptab."))
7753
0
    {
7754
0
      hdr->sh_type = SHT_MIPS_GPTAB;
7755
0
      hdr->sh_entsize = sizeof (Elf32_External_gptab);
7756
      /* The sh_info field is set in final_write_processing.  */
7757
0
    }
7758
209
  else if (strcmp (name, ".ucode") == 0)
7759
0
    hdr->sh_type = SHT_MIPS_UCODE;
7760
209
  else if (strcmp (name, ".mdebug") == 0)
7761
0
    {
7762
0
      hdr->sh_type = SHT_MIPS_DEBUG;
7763
      /* In a shared object on IRIX 5.3, the .mdebug section has an
7764
   entsize of 0.  FIXME: Does this matter?  */
7765
0
      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7766
0
  hdr->sh_entsize = 0;
7767
0
      else
7768
0
  hdr->sh_entsize = 1;
7769
0
    }
7770
209
  else if (strcmp (name, ".reginfo") == 0)
7771
0
    {
7772
0
      hdr->sh_type = SHT_MIPS_REGINFO;
7773
      /* In a shared object on IRIX 5.3, the .reginfo section has an
7774
   entsize of 0x18.  FIXME: Does this matter?  */
7775
0
      if (SGI_COMPAT (abfd))
7776
0
  {
7777
0
    if ((abfd->flags & DYNAMIC) != 0)
7778
0
      hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7779
0
    else
7780
0
      hdr->sh_entsize = 1;
7781
0
  }
7782
0
      else
7783
0
  hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7784
0
    }
7785
209
  else if (SGI_COMPAT (abfd)
7786
160
     && (strcmp (name, ".hash") == 0
7787
160
         || strcmp (name, ".dynamic") == 0
7788
160
         || strcmp (name, ".dynstr") == 0))
7789
0
    {
7790
0
      if (SGI_COMPAT (abfd))
7791
0
  hdr->sh_entsize = 0;
7792
#if 0
7793
      /* This isn't how the IRIX6 linker behaves.  */
7794
      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7795
#endif
7796
0
    }
7797
209
  else if (strcmp (name, ".got") == 0
7798
209
     || strcmp (name, ".srdata") == 0
7799
209
     || strcmp (name, ".sdata") == 0
7800
209
     || strcmp (name, ".sbss") == 0
7801
209
     || strcmp (name, ".lit4") == 0
7802
208
     || strcmp (name, ".lit8") == 0)
7803
1
    hdr->sh_flags |= SHF_MIPS_GPREL;
7804
208
  else if (strcmp (name, ".MIPS.interfaces") == 0)
7805
0
    {
7806
0
      hdr->sh_type = SHT_MIPS_IFACE;
7807
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7808
0
    }
7809
208
  else if (startswith (name, ".MIPS.content"))
7810
0
    {
7811
0
      hdr->sh_type = SHT_MIPS_CONTENT;
7812
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7813
      /* The sh_info field is set in final_write_processing.  */
7814
0
    }
7815
208
  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7816
0
    {
7817
0
      hdr->sh_type = SHT_MIPS_OPTIONS;
7818
0
      hdr->sh_entsize = 1;
7819
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7820
0
    }
7821
208
  else if (startswith (name, ".MIPS.abiflags"))
7822
0
    {
7823
0
      hdr->sh_type = SHT_MIPS_ABIFLAGS;
7824
0
      hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7825
0
    }
7826
208
  else if (startswith (name, ".debug_")
7827
204
     || startswith (name, ".gnu.debuglto_.debug_")
7828
204
     || startswith (name, ".zdebug_")
7829
204
     || startswith (name, ".gnu.debuglto_.zdebug_"))
7830
4
    {
7831
4
      hdr->sh_type = SHT_MIPS_DWARF;
7832
7833
      /* Irix facilities such as libexc expect a single .debug_frame
7834
   per executable, the system ones have NOSTRIP set and the linker
7835
   doesn't merge sections with different flags so ...  */
7836
4
      if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7837
0
  hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7838
4
    }
7839
204
  else if (strcmp (name, ".MIPS.symlib") == 0)
7840
0
    {
7841
0
      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7842
      /* The sh_link and sh_info fields are set in
7843
   final_write_processing.  */
7844
0
    }
7845
204
  else if (startswith (name, ".MIPS.events")
7846
204
     || startswith (name, ".MIPS.post_rel"))
7847
0
    {
7848
0
      hdr->sh_type = SHT_MIPS_EVENTS;
7849
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7850
      /* The sh_link field is set in final_write_processing.  */
7851
0
    }
7852
204
  else if (strcmp (name, ".msym") == 0)
7853
0
    {
7854
0
      hdr->sh_type = SHT_MIPS_MSYM;
7855
0
      hdr->sh_flags |= SHF_ALLOC;
7856
0
      hdr->sh_entsize = 8;
7857
0
    }
7858
204
  else if (strcmp (name, ".MIPS.xhash") == 0)
7859
0
    {
7860
0
      hdr->sh_type = SHT_MIPS_XHASH;
7861
0
      hdr->sh_flags |= SHF_ALLOC;
7862
0
      hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7863
0
    }
7864
7865
  /* The generic elf_fake_sections will set up REL_HDR using the default
7866
   kind of relocations.  We used to set up a second header for the
7867
   non-default kind of relocations here, but only NewABI would use
7868
   these, and the IRIX ld doesn't like resulting empty RELA sections.
7869
   Thus we create those header only on demand now.  */
7870
7871
209
  return true;
7872
209
}
7873
7874
/* Given a BFD section, try to locate the corresponding ELF section
7875
   index.  This is used by both the 32-bit and the 64-bit ABI.
7876
   Actually, it's not clear to me that the 64-bit ABI supports these,
7877
   but for non-PIC objects we will certainly want support for at least
7878
   the .scommon section.  */
7879
7880
bool
7881
_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7882
          asection *sec, int *retval)
7883
153
{
7884
153
  if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7885
1
    {
7886
1
      *retval = SHN_MIPS_SCOMMON;
7887
1
      return true;
7888
1
    }
7889
152
  if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7890
1
    {
7891
1
      *retval = SHN_MIPS_ACOMMON;
7892
1
      return true;
7893
1
    }
7894
151
  return false;
7895
152
}
7896

7897
/* Hook called by the linker routine which adds symbols from an object
7898
   file.  We must handle the special MIPS section numbers here.  */
7899
7900
bool
7901
_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7902
             Elf_Internal_Sym *sym, const char **namep,
7903
             flagword *flagsp ATTRIBUTE_UNUSED,
7904
             asection **secp, bfd_vma *valp)
7905
0
{
7906
0
  if (SGI_COMPAT (abfd)
7907
0
      && (abfd->flags & DYNAMIC) != 0
7908
0
      && strcmp (*namep, "_rld_new_interface") == 0)
7909
0
    {
7910
      /* Skip IRIX5 rld entry name.  */
7911
0
      *namep = NULL;
7912
0
      return true;
7913
0
    }
7914
7915
  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7916
     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
7917
     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
7918
     a magic symbol resolved by the linker, we ignore this bogus definition
7919
     of _gp_disp.  New ABI objects do not suffer from this problem so this
7920
     is not done for them. */
7921
0
  if (!NEWABI_P(abfd)
7922
0
      && (sym->st_shndx == SHN_ABS)
7923
0
      && (strcmp (*namep, "_gp_disp") == 0))
7924
0
    {
7925
0
      *namep = NULL;
7926
0
      return true;
7927
0
    }
7928
7929
0
  switch (sym->st_shndx)
7930
0
    {
7931
0
    case SHN_COMMON:
7932
      /* Common symbols less than the GP size are automatically
7933
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7934
0
      if (sym->st_size > elf_gp_size (abfd)
7935
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS
7936
0
    || IRIX_COMPAT (abfd) == ict_irix6
7937
0
    || strcmp (*namep, "__gnu_lto_slim") == 0)
7938
0
  break;
7939
      /* Fall through.  */
7940
0
    case SHN_MIPS_SCOMMON:
7941
0
      *secp = bfd_make_section_old_way (abfd, ".scommon");
7942
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7943
0
      *valp = sym->st_size;
7944
0
      break;
7945
7946
0
    case SHN_MIPS_TEXT:
7947
      /* This section is used in a shared object.  */
7948
0
      if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7949
0
  {
7950
0
    asymbol *elf_text_symbol;
7951
0
    asection *elf_text_section;
7952
0
    size_t amt = sizeof (asection);
7953
7954
0
    elf_text_section = bfd_zalloc (abfd, amt);
7955
0
    if (elf_text_section == NULL)
7956
0
      return false;
7957
7958
0
    amt = sizeof (asymbol);
7959
0
    elf_text_symbol = bfd_zalloc (abfd, amt);
7960
0
    if (elf_text_symbol == NULL)
7961
0
      return false;
7962
7963
    /* Initialize the section.  */
7964
7965
0
    mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7966
7967
0
    elf_text_section->symbol = elf_text_symbol;
7968
0
    elf_text_section->name = ".text";
7969
0
    elf_text_section->flags = SEC_NO_FLAGS;
7970
0
    elf_text_section->output_section = NULL;
7971
0
    elf_text_section->owner = abfd;
7972
0
    elf_text_symbol->name = ".text";
7973
0
    elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7974
0
    elf_text_symbol->section = elf_text_section;
7975
0
  }
7976
      /* This code used to do *secp = bfd_und_section_ptr if
7977
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7978
   so I took it out.  */
7979
0
      *secp = mips_elf_tdata (abfd)->elf_text_section;
7980
0
      break;
7981
7982
0
    case SHN_MIPS_ACOMMON:
7983
      /* Fall through. XXX Can we treat this as allocated data?  */
7984
0
    case SHN_MIPS_DATA:
7985
      /* This section is used in a shared object.  */
7986
0
      if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7987
0
  {
7988
0
    asymbol *elf_data_symbol;
7989
0
    asection *elf_data_section;
7990
0
    size_t amt = sizeof (asection);
7991
7992
0
    elf_data_section = bfd_zalloc (abfd, amt);
7993
0
    if (elf_data_section == NULL)
7994
0
      return false;
7995
7996
0
    amt = sizeof (asymbol);
7997
0
    elf_data_symbol = bfd_zalloc (abfd, amt);
7998
0
    if (elf_data_symbol == NULL)
7999
0
      return false;
8000
8001
    /* Initialize the section.  */
8002
8003
0
    mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
8004
8005
0
    elf_data_section->symbol = elf_data_symbol;
8006
0
    elf_data_section->name = ".data";
8007
0
    elf_data_section->flags = SEC_NO_FLAGS;
8008
0
    elf_data_section->output_section = NULL;
8009
0
    elf_data_section->owner = abfd;
8010
0
    elf_data_symbol->name = ".data";
8011
0
    elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
8012
0
    elf_data_symbol->section = elf_data_section;
8013
0
  }
8014
      /* This code used to do *secp = bfd_und_section_ptr if
8015
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
8016
   so I took it out.  */
8017
0
      *secp = mips_elf_tdata (abfd)->elf_data_section;
8018
0
      break;
8019
8020
0
    case SHN_MIPS_SUNDEFINED:
8021
0
      *secp = bfd_und_section_ptr;
8022
0
      break;
8023
0
    }
8024
8025
0
  if (SGI_COMPAT (abfd)
8026
0
      && ! bfd_link_pic (info)
8027
0
      && info->output_bfd->xvec == abfd->xvec
8028
0
      && strcmp (*namep, "__rld_obj_head") == 0)
8029
0
    {
8030
0
      struct elf_link_hash_entry *h;
8031
0
      struct bfd_link_hash_entry *bh;
8032
8033
      /* Mark __rld_obj_head as dynamic.  */
8034
0
      bh = NULL;
8035
0
      if (! (_bfd_generic_link_add_one_symbol
8036
0
       (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
8037
0
        get_elf_backend_data (abfd)->collect, &bh)))
8038
0
  return false;
8039
8040
0
      h = (struct elf_link_hash_entry *) bh;
8041
0
      h->non_elf = 0;
8042
0
      h->def_regular = 1;
8043
0
      h->type = STT_OBJECT;
8044
8045
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8046
0
  return false;
8047
8048
0
      mips_elf_hash_table (info)->use_rld_obj_head = true;
8049
0
      mips_elf_hash_table (info)->rld_symbol = h;
8050
0
    }
8051
8052
  /* If this is a mips16 text symbol, add 1 to the value to make it
8053
     odd.  This will cause something like .word SYM to come up with
8054
     the right value when it is loaded into the PC.  */
8055
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8056
0
    ++*valp;
8057
8058
0
  return true;
8059
0
}
8060
8061
/* This hook function is called before the linker writes out a global
8062
   symbol.  We mark symbols as small common if appropriate.  This is
8063
   also where we undo the increment of the value for a mips16 symbol.  */
8064
8065
int
8066
_bfd_mips_elf_link_output_symbol_hook
8067
  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8068
   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8069
   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8070
0
{
8071
  /* If we see a common symbol, which implies a relocatable link, then
8072
     if a symbol was small common in an input file, mark it as small
8073
     common in the output file.  */
8074
0
  if (sym->st_shndx == SHN_COMMON
8075
0
      && strcmp (input_sec->name, ".scommon") == 0)
8076
0
    sym->st_shndx = SHN_MIPS_SCOMMON;
8077
8078
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8079
0
    sym->st_value &= ~1;
8080
8081
0
  return 1;
8082
0
}
8083

8084
/* Functions for the dynamic linker.  */
8085
8086
/* Create dynamic sections when linking against a dynamic object.  */
8087
8088
bool
8089
_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8090
0
{
8091
0
  struct elf_link_hash_entry *h;
8092
0
  struct bfd_link_hash_entry *bh;
8093
0
  flagword flags;
8094
0
  register asection *s;
8095
0
  const char * const *namep;
8096
0
  struct mips_elf_link_hash_table *htab;
8097
8098
0
  htab = mips_elf_hash_table (info);
8099
0
  BFD_ASSERT (htab != NULL);
8100
8101
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8102
0
     | SEC_LINKER_CREATED | SEC_READONLY);
8103
8104
  /* The psABI requires a read-only .dynamic section, but the VxWorks
8105
     EABI doesn't.  */
8106
0
  if (htab->root.target_os != is_vxworks)
8107
0
    {
8108
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8109
0
      if (s != NULL)
8110
0
  {
8111
0
    if (!bfd_set_section_flags (s, flags))
8112
0
      return false;
8113
0
  }
8114
0
    }
8115
8116
  /* We need to create .got section.  */
8117
0
  if (!mips_elf_create_got_section (abfd, info))
8118
0
    return false;
8119
8120
0
  if (! mips_elf_rel_dyn_section (info, true))
8121
0
    return false;
8122
8123
  /* Create .stub section.  */
8124
0
  s = bfd_make_section_anyway_with_flags (abfd,
8125
0
            MIPS_ELF_STUB_SECTION_NAME (abfd),
8126
0
            flags | SEC_CODE);
8127
0
  if (s == NULL
8128
0
      || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8129
0
    return false;
8130
0
  htab->sstubs = s;
8131
8132
0
  if (!mips_elf_hash_table (info)->use_rld_obj_head
8133
0
      && bfd_link_executable (info)
8134
0
      && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8135
0
    {
8136
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8137
0
                flags &~ (flagword) SEC_READONLY);
8138
0
      if (s == NULL
8139
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8140
0
  return false;
8141
0
    }
8142
8143
  /* Create .MIPS.xhash section.  */
8144
0
  if (info->emit_gnu_hash)
8145
0
    s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8146
0
              flags | SEC_READONLY);
8147
8148
  /* On IRIX5, we adjust add some additional symbols and change the
8149
     alignments of several sections.  There is no ABI documentation
8150
     indicating that this is necessary on IRIX6, nor any evidence that
8151
     the linker takes such action.  */
8152
0
  if (IRIX_COMPAT (abfd) == ict_irix5)
8153
0
    {
8154
0
      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8155
0
  {
8156
0
    bh = NULL;
8157
0
    if (! (_bfd_generic_link_add_one_symbol
8158
0
     (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8159
0
      NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8160
0
      return false;
8161
8162
0
    h = (struct elf_link_hash_entry *) bh;
8163
0
    h->mark = 1;
8164
0
    h->non_elf = 0;
8165
0
    h->def_regular = 1;
8166
0
    h->type = STT_SECTION;
8167
8168
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8169
0
      return false;
8170
0
  }
8171
8172
      /* We need to create a .compact_rel section.  */
8173
0
      if (SGI_COMPAT (abfd))
8174
0
  {
8175
0
    if (!mips_elf_create_compact_rel_section (abfd, info))
8176
0
      return false;
8177
0
  }
8178
8179
      /* Change alignments of some sections.  */
8180
0
      s = bfd_get_linker_section (abfd, ".hash");
8181
0
      if (s != NULL)
8182
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8183
8184
0
      s = bfd_get_linker_section (abfd, ".dynsym");
8185
0
      if (s != NULL)
8186
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8187
8188
0
      s = bfd_get_linker_section (abfd, ".dynstr");
8189
0
      if (s != NULL)
8190
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8191
8192
      /* ??? */
8193
0
      s = bfd_get_section_by_name (abfd, ".reginfo");
8194
0
      if (s != NULL)
8195
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8196
8197
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8198
0
      if (s != NULL)
8199
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8200
0
    }
8201
8202
0
  if (bfd_link_executable (info))
8203
0
    {
8204
0
      const char *name;
8205
8206
0
      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8207
0
      bh = NULL;
8208
0
      if (!(_bfd_generic_link_add_one_symbol
8209
0
      (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8210
0
       NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8211
0
  return false;
8212
8213
0
      h = (struct elf_link_hash_entry *) bh;
8214
0
      h->non_elf = 0;
8215
0
      h->def_regular = 1;
8216
0
      h->type = STT_SECTION;
8217
8218
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8219
0
  return false;
8220
8221
0
      if (! mips_elf_hash_table (info)->use_rld_obj_head)
8222
0
  {
8223
    /* __rld_map is a four byte word located in the .data section
8224
       and is filled in by the rtld to contain a pointer to
8225
       the _r_debug structure. Its symbol value will be set in
8226
       _bfd_mips_elf_finish_dynamic_symbol.  */
8227
0
    s = bfd_get_linker_section (abfd, ".rld_map");
8228
0
    BFD_ASSERT (s != NULL);
8229
8230
0
    name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8231
0
    bh = NULL;
8232
0
    if (!(_bfd_generic_link_add_one_symbol
8233
0
    (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8234
0
     get_elf_backend_data (abfd)->collect, &bh)))
8235
0
      return false;
8236
8237
0
    h = (struct elf_link_hash_entry *) bh;
8238
0
    h->non_elf = 0;
8239
0
    h->def_regular = 1;
8240
0
    h->type = STT_OBJECT;
8241
8242
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8243
0
      return false;
8244
0
    mips_elf_hash_table (info)->rld_symbol = h;
8245
0
  }
8246
0
    }
8247
8248
  /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8249
     Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
8250
0
  if (!_bfd_elf_create_dynamic_sections (abfd, info))
8251
0
    return false;
8252
8253
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
8254
  /* Do the usual VxWorks handling.  */
8255
0
  if (htab->root.target_os == is_vxworks
8256
0
      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8257
0
    return false;
8258
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
8259
8260
0
  return true;
8261
0
}
8262

8263
/* Return true if relocation REL against section SEC is a REL rather than
8264
   RELA relocation.  RELOCS is the first relocation in the section and
8265
   ABFD is the bfd that contains SEC.  */
8266
8267
static bool
8268
mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8269
         const Elf_Internal_Rela *relocs,
8270
         const Elf_Internal_Rela *rel)
8271
0
{
8272
0
  Elf_Internal_Shdr *rel_hdr;
8273
0
  elf_backend_data *bed;
8274
8275
  /* To determine which flavor of relocation this is, we depend on the
8276
     fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
8277
0
  rel_hdr = elf_section_data (sec)->rel.hdr;
8278
0
  if (rel_hdr == NULL)
8279
0
    return false;
8280
0
  bed = get_elf_backend_data (abfd);
8281
0
  return ((size_t) (rel - relocs)
8282
0
    < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8283
0
}
8284
8285
/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8286
   HOWTO is the relocation's howto and CONTENTS points to the contents
8287
   of the section that REL is against.  */
8288
8289
static bfd_vma
8290
mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8291
        const Elf_Internal_Rela *rel,
8292
        reloc_howto_type *howto, bfd_byte *contents)
8293
0
{
8294
0
  bfd_byte *location;
8295
0
  unsigned int r_type;
8296
0
  bfd_vma addend;
8297
0
  bfd_vma bytes;
8298
8299
0
  if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8300
0
    return 0;
8301
8302
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8303
0
  location = contents + rel->r_offset;
8304
8305
  /* Get the addend, which is stored in the input file.  */
8306
0
  _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8307
0
  bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8308
0
  _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8309
8310
0
  addend = bytes & howto->src_mask;
8311
8312
  /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
8313
     accordingly.  */
8314
0
  if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8315
0
    addend <<= 1;
8316
8317
0
  return addend;
8318
0
}
8319
8320
/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8321
   and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
8322
   and update *ADDEND with the final addend.  Return true on success
8323
   or false if the LO16 could not be found.  RELEND is the exclusive
8324
   upper bound on the relocations for REL's section.  */
8325
8326
static bool
8327
mips_elf_add_lo16_rel_addend (bfd *abfd,
8328
            asection *sec,
8329
            const Elf_Internal_Rela *rel,
8330
            const Elf_Internal_Rela *relend,
8331
            bfd_byte *contents, bfd_vma *addend)
8332
0
{
8333
0
  unsigned int r_type, lo16_type;
8334
0
  const Elf_Internal_Rela *lo16_relocation;
8335
0
  reloc_howto_type *lo16_howto;
8336
0
  bfd_vma l;
8337
8338
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8339
0
  switch (r_type)
8340
0
    {
8341
0
    case R_MIPS_HI16:
8342
0
    case R_MIPS_GOT16:
8343
0
      lo16_type = R_MIPS_LO16;
8344
0
      break;
8345
0
    case R_MIPS_PCHI16:
8346
0
      lo16_type = R_MIPS_PCLO16;
8347
0
      break;
8348
0
    case R_MIPS_TLS_DTPREL_HI16:
8349
0
      lo16_type = R_MIPS_TLS_DTPREL_LO16;
8350
0
      break;
8351
0
    case R_MIPS_TLS_TPREL_HI16:
8352
0
      lo16_type = R_MIPS_TLS_TPREL_LO16;
8353
0
      break;
8354
0
    case R_MIPS16_HI16:
8355
0
    case R_MIPS16_GOT16:
8356
0
      lo16_type = R_MIPS16_LO16;
8357
0
      break;
8358
0
    case R_MIPS16_TLS_DTPREL_HI16:
8359
0
      lo16_type = R_MIPS16_TLS_DTPREL_LO16;
8360
0
      break;
8361
0
    case R_MIPS16_TLS_TPREL_HI16:
8362
0
      lo16_type = R_MIPS16_TLS_TPREL_LO16;
8363
0
      break;
8364
0
    case R_MICROMIPS_HI16:
8365
0
    case R_MICROMIPS_GOT16:
8366
0
      lo16_type = R_MICROMIPS_LO16;
8367
0
      break;
8368
0
    case R_MICROMIPS_TLS_DTPREL_HI16:
8369
0
      lo16_type = R_MICROMIPS_TLS_DTPREL_LO16;
8370
0
      break;
8371
0
    case R_MICROMIPS_TLS_TPREL_HI16:
8372
0
      lo16_type = R_MICROMIPS_TLS_TPREL_LO16;
8373
0
      break;
8374
0
    default:
8375
0
      abort ();
8376
0
    }
8377
8378
  /* The combined value is the sum of the HI16 addend, left-shifted by
8379
     sixteen bits, and the LO16 addend, sign extended.  (Usually, the
8380
     code does a `lui' of the HI16 value, and then an `addiu' of the
8381
     LO16 value.)  */
8382
0
  *addend <<= 16;
8383
8384
  /* Scan ahead to find a matching LO16 relocation.
8385
8386
     According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8387
     be immediately following.  However, for the IRIX6 ABI, the next
8388
     relocation may be a composed relocation consisting of several
8389
     relocations for the same address.  In that case, the R_MIPS_LO16
8390
     relocation may occur as one of these.  We permit a similar
8391
     extension in general, as that is useful for GCC.
8392
8393
     In some cases GCC dead code elimination removes the LO16 but keeps
8394
     the corresponding HI16.  This is strictly speaking a violation of
8395
     the ABI but not immediately harmful.  */
8396
0
  lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8397
0
  if (lo16_relocation == NULL)
8398
0
    return false;
8399
8400
  /* Obtain the addend kept there.  */
8401
0
  lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8402
0
  l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8403
0
        contents);
8404
8405
0
  l <<= lo16_howto->rightshift;
8406
  /* For a PC-relative relocation the PCLO16 part of the addend
8407
     is relative to its PC and not ours, so we need to adjust it.  */
8408
0
  if (r_type == R_MIPS_PCHI16)
8409
0
    l = (l - (lo16_relocation->r_offset - rel->r_offset)) & 0xffff;
8410
0
  l = _bfd_mips_elf_sign_extend (l, 16);
8411
8412
0
  *addend += l;
8413
0
  return true;
8414
0
}
8415
8416
/* Try to read the contents of section SEC in bfd ABFD.  Return true and
8417
   store the contents in *CONTENTS on success.  Assume that *CONTENTS
8418
   already holds the contents if it is nonull on entry.  */
8419
8420
static bool
8421
mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8422
0
{
8423
0
  if (*contents)
8424
0
    return true;
8425
8426
  /* Get cached copy if it exists.  */
8427
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
8428
0
    {
8429
0
      *contents = elf_section_data (sec)->this_hdr.contents;
8430
0
      return true;
8431
0
    }
8432
8433
0
  return bfd_malloc_and_get_section (abfd, sec, contents);
8434
0
}
8435
8436
/* Make a new PLT record to keep internal data.  */
8437
8438
static struct plt_entry *
8439
mips_elf_make_plt_record (bfd *abfd)
8440
0
{
8441
0
  struct plt_entry *entry;
8442
8443
0
  entry = bfd_zalloc (abfd, sizeof (*entry));
8444
0
  if (entry == NULL)
8445
0
    return NULL;
8446
8447
0
  entry->stub_offset = MINUS_ONE;
8448
0
  entry->mips_offset = MINUS_ONE;
8449
0
  entry->comp_offset = MINUS_ONE;
8450
0
  entry->gotplt_index = MINUS_ONE;
8451
0
  return entry;
8452
0
}
8453
8454
/* Define the special `__gnu_absolute_zero' symbol.  We only need this
8455
   for PIC code, as otherwise there is no load-time relocation involved
8456
   and local GOT entries whose value is zero at static link time will
8457
   retain their value at load time.  */
8458
8459
static bool
8460
mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8461
             struct mips_elf_link_hash_table *htab,
8462
             unsigned int r_type)
8463
0
{
8464
0
  union
8465
0
    {
8466
0
      struct elf_link_hash_entry *eh;
8467
0
      struct bfd_link_hash_entry *bh;
8468
0
    }
8469
0
  hzero;
8470
8471
0
  BFD_ASSERT (!htab->use_absolute_zero);
8472
0
  BFD_ASSERT (bfd_link_pic (info));
8473
8474
0
  hzero.bh = NULL;
8475
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8476
0
           BSF_GLOBAL, bfd_abs_section_ptr, 0,
8477
0
           NULL, false, false, &hzero.bh))
8478
0
    return false;
8479
8480
0
  BFD_ASSERT (hzero.bh != NULL);
8481
0
  hzero.eh->size = 0;
8482
0
  hzero.eh->type = STT_NOTYPE;
8483
0
  hzero.eh->other = STV_PROTECTED;
8484
0
  hzero.eh->def_regular = 1;
8485
0
  hzero.eh->non_elf = 0;
8486
8487
0
  if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8488
0
    return false;
8489
8490
0
  htab->use_absolute_zero = true;
8491
8492
0
  return true;
8493
0
}
8494
8495
/* Look through the relocs for a section during the first phase, and
8496
   allocate space in the global offset table and record the need for
8497
   standard MIPS and compressed procedure linkage table entries.  */
8498
8499
bool
8500
_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8501
          asection *sec, const Elf_Internal_Rela *relocs)
8502
0
{
8503
0
  const char *name;
8504
0
  bfd *dynobj;
8505
0
  Elf_Internal_Shdr *symtab_hdr;
8506
0
  struct elf_link_hash_entry **sym_hashes;
8507
0
  size_t extsymoff;
8508
0
  const Elf_Internal_Rela *rel;
8509
0
  const Elf_Internal_Rela *rel_end;
8510
0
  asection *sreloc;
8511
0
  elf_backend_data *bed;
8512
0
  struct mips_elf_link_hash_table *htab;
8513
0
  bfd_byte *contents;
8514
0
  bfd_vma addend;
8515
0
  reloc_howto_type *howto;
8516
8517
0
  if (bfd_link_relocatable (info))
8518
0
    return true;
8519
8520
0
  htab = mips_elf_hash_table (info);
8521
0
  BFD_ASSERT (htab != NULL);
8522
8523
0
  dynobj = elf_hash_table (info)->dynobj;
8524
0
  symtab_hdr = &elf_symtab_hdr (abfd);
8525
0
  sym_hashes = elf_sym_hashes (abfd);
8526
0
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8527
8528
0
  bed = get_elf_backend_data (abfd);
8529
0
  rel_end = relocs + sec->reloc_count;
8530
8531
  /* Check for the mips16 stub sections.  */
8532
8533
0
  name = bfd_section_name (sec);
8534
0
  if (FN_STUB_P (name))
8535
0
    {
8536
0
      unsigned long r_symndx;
8537
8538
      /* Look at the relocation information to figure out which symbol
8539
   this is for.  */
8540
8541
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8542
0
      if (r_symndx == 0)
8543
0
  {
8544
0
    _bfd_error_handler
8545
      /* xgettext:c-format */
8546
0
      (_("%pB: warning: cannot determine the target function for"
8547
0
         " stub section `%s'"),
8548
0
       abfd, name);
8549
0
    bfd_set_error (bfd_error_bad_value);
8550
0
    return false;
8551
0
  }
8552
8553
0
      if (r_symndx < extsymoff
8554
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8555
0
  {
8556
0
    asection *o;
8557
8558
    /* This stub is for a local symbol.  This stub will only be
8559
       needed if there is some relocation in this BFD, other
8560
       than a 16 bit function call, which refers to this symbol.  */
8561
0
    for (o = abfd->sections; o != NULL; o = o->next)
8562
0
      {
8563
0
        Elf_Internal_Rela *sec_relocs;
8564
0
        const Elf_Internal_Rela *r, *rend;
8565
8566
        /* We can ignore stub sections when looking for relocs.  */
8567
0
        if ((o->flags & SEC_RELOC) == 0
8568
0
      || o->reloc_count == 0
8569
0
      || section_allows_mips16_refs_p (o))
8570
0
    continue;
8571
8572
0
        sec_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8573
0
                  info->keep_memory);
8574
0
        if (sec_relocs == NULL)
8575
0
    return false;
8576
8577
0
        rend = sec_relocs + o->reloc_count;
8578
0
        for (r = sec_relocs; r < rend; r++)
8579
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8580
0
        && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8581
0
      break;
8582
8583
0
        if (elf_section_data (o)->relocs != sec_relocs)
8584
0
    free (sec_relocs);
8585
8586
0
        if (r < rend)
8587
0
    break;
8588
0
      }
8589
8590
0
    if (o == NULL)
8591
0
      {
8592
        /* There is no non-call reloc for this stub, so we do
8593
     not need it.  Since this function is called before
8594
     the linker maps input sections to output sections, we
8595
     can easily discard it by setting the SEC_EXCLUDE
8596
     flag.  */
8597
0
        sec->flags |= SEC_EXCLUDE;
8598
0
        return true;
8599
0
      }
8600
8601
    /* Record this stub in an array of local symbol stubs for
8602
       this BFD.  */
8603
0
    if (mips_elf_tdata (abfd)->local_stubs == NULL)
8604
0
      {
8605
0
        unsigned long symcount;
8606
0
        asection **n;
8607
0
        bfd_size_type amt;
8608
8609
0
        if (elf_bad_symtab (abfd))
8610
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8611
0
        else
8612
0
    symcount = symtab_hdr->sh_info;
8613
0
        amt = symcount * sizeof (asection *);
8614
0
        n = bfd_zalloc (abfd, amt);
8615
0
        if (n == NULL)
8616
0
    return false;
8617
0
        mips_elf_tdata (abfd)->local_stubs = n;
8618
0
      }
8619
8620
0
    sec->flags |= SEC_KEEP;
8621
0
    mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8622
8623
    /* We don't need to set mips16_stubs_seen in this case.
8624
       That flag is used to see whether we need to look through
8625
       the global symbol table for stubs.  We don't need to set
8626
       it here, because we just have a local stub.  */
8627
0
  }
8628
0
      else
8629
0
  {
8630
0
    struct mips_elf_link_hash_entry *h;
8631
8632
0
    h = ((struct mips_elf_link_hash_entry *)
8633
0
         sym_hashes[r_symndx - extsymoff]);
8634
8635
0
    while (h->root.root.type == bfd_link_hash_indirect
8636
0
     || h->root.root.type == bfd_link_hash_warning)
8637
0
      h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8638
8639
    /* H is the symbol this stub is for.  */
8640
8641
    /* If we already have an appropriate stub for this function, we
8642
       don't need another one, so we can discard this one.  Since
8643
       this function is called before the linker maps input sections
8644
       to output sections, we can easily discard it by setting the
8645
       SEC_EXCLUDE flag.  */
8646
0
    if (h->fn_stub != NULL)
8647
0
      {
8648
0
        sec->flags |= SEC_EXCLUDE;
8649
0
        return true;
8650
0
      }
8651
8652
0
    sec->flags |= SEC_KEEP;
8653
0
    h->fn_stub = sec;
8654
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8655
0
  }
8656
0
    }
8657
0
  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8658
0
    {
8659
0
      unsigned long r_symndx;
8660
0
      struct mips_elf_link_hash_entry *h;
8661
0
      asection **loc;
8662
8663
      /* Look at the relocation information to figure out which symbol
8664
   this is for.  */
8665
8666
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8667
0
      if (r_symndx == 0)
8668
0
  {
8669
0
    _bfd_error_handler
8670
      /* xgettext:c-format */
8671
0
      (_("%pB: warning: cannot determine the target function for"
8672
0
         " stub section `%s'"),
8673
0
       abfd, name);
8674
0
    bfd_set_error (bfd_error_bad_value);
8675
0
    return false;
8676
0
  }
8677
8678
0
      if (r_symndx < extsymoff
8679
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8680
0
  {
8681
0
    asection *o;
8682
8683
    /* This stub is for a local symbol.  This stub will only be
8684
       needed if there is some relocation (R_MIPS16_26) in this BFD
8685
       that refers to this symbol.  */
8686
0
    for (o = abfd->sections; o != NULL; o = o->next)
8687
0
      {
8688
0
        Elf_Internal_Rela *sec_relocs;
8689
0
        const Elf_Internal_Rela *r, *rend;
8690
8691
        /* We can ignore stub sections when looking for relocs.  */
8692
0
        if ((o->flags & SEC_RELOC) == 0
8693
0
      || o->reloc_count == 0
8694
0
      || section_allows_mips16_refs_p (o))
8695
0
    continue;
8696
8697
0
        sec_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8698
0
                  info->keep_memory);
8699
0
        if (sec_relocs == NULL)
8700
0
    return false;
8701
8702
0
        rend = sec_relocs + o->reloc_count;
8703
0
        for (r = sec_relocs; r < rend; r++)
8704
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8705
0
        && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8706
0
        break;
8707
8708
0
        if (elf_section_data (o)->relocs != sec_relocs)
8709
0
    free (sec_relocs);
8710
8711
0
        if (r < rend)
8712
0
    break;
8713
0
      }
8714
8715
0
    if (o == NULL)
8716
0
      {
8717
        /* There is no non-call reloc for this stub, so we do
8718
     not need it.  Since this function is called before
8719
     the linker maps input sections to output sections, we
8720
     can easily discard it by setting the SEC_EXCLUDE
8721
     flag.  */
8722
0
        sec->flags |= SEC_EXCLUDE;
8723
0
        return true;
8724
0
      }
8725
8726
    /* Record this stub in an array of local symbol call_stubs for
8727
       this BFD.  */
8728
0
    if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8729
0
      {
8730
0
        unsigned long symcount;
8731
0
        asection **n;
8732
0
        bfd_size_type amt;
8733
8734
0
        if (elf_bad_symtab (abfd))
8735
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8736
0
        else
8737
0
    symcount = symtab_hdr->sh_info;
8738
0
        amt = symcount * sizeof (asection *);
8739
0
        n = bfd_zalloc (abfd, amt);
8740
0
        if (n == NULL)
8741
0
    return false;
8742
0
        mips_elf_tdata (abfd)->local_call_stubs = n;
8743
0
      }
8744
8745
0
    sec->flags |= SEC_KEEP;
8746
0
    mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8747
8748
    /* We don't need to set mips16_stubs_seen in this case.
8749
       That flag is used to see whether we need to look through
8750
       the global symbol table for stubs.  We don't need to set
8751
       it here, because we just have a local stub.  */
8752
0
  }
8753
0
      else
8754
0
  {
8755
0
    h = ((struct mips_elf_link_hash_entry *)
8756
0
         sym_hashes[r_symndx - extsymoff]);
8757
8758
    /* H is the symbol this stub is for.  */
8759
8760
0
    if (CALL_FP_STUB_P (name))
8761
0
      loc = &h->call_fp_stub;
8762
0
    else
8763
0
      loc = &h->call_stub;
8764
8765
    /* If we already have an appropriate stub for this function, we
8766
       don't need another one, so we can discard this one.  Since
8767
       this function is called before the linker maps input sections
8768
       to output sections, we can easily discard it by setting the
8769
       SEC_EXCLUDE flag.  */
8770
0
    if (*loc != NULL)
8771
0
      {
8772
0
        sec->flags |= SEC_EXCLUDE;
8773
0
        return true;
8774
0
      }
8775
8776
0
    sec->flags |= SEC_KEEP;
8777
0
    *loc = sec;
8778
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8779
0
  }
8780
0
    }
8781
8782
0
  sreloc = NULL;
8783
0
  contents = NULL;
8784
0
  for (rel = relocs; rel < rel_end; ++rel)
8785
0
    {
8786
0
      unsigned long r_symndx;
8787
0
      unsigned int r_type;
8788
0
      struct elf_link_hash_entry *h;
8789
0
      bool can_make_dynamic_p;
8790
0
      bool call_reloc_p;
8791
0
      bool constrain_symbol_p;
8792
8793
0
      r_symndx = ELF_R_SYM (abfd, rel->r_info);
8794
0
      r_type = ELF_R_TYPE (abfd, rel->r_info);
8795
8796
0
      if (r_symndx < extsymoff)
8797
0
  h = NULL;
8798
0
      else
8799
0
  {
8800
0
    h = sym_hashes[r_symndx - extsymoff];
8801
0
    if (h != NULL)
8802
0
      {
8803
0
        while (h->root.type == bfd_link_hash_indirect
8804
0
         || h->root.type == bfd_link_hash_warning)
8805
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8806
0
      }
8807
0
  }
8808
8809
      /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8810
   relocation into a dynamic one.  */
8811
0
      can_make_dynamic_p = false;
8812
8813
      /* Set CALL_RELOC_P to true if the relocation is for a call,
8814
   and if pointer equality therefore doesn't matter.  */
8815
0
      call_reloc_p = false;
8816
8817
      /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8818
   into account when deciding how to define the symbol.  */
8819
0
      constrain_symbol_p = true;
8820
8821
0
      switch (r_type)
8822
0
  {
8823
0
  case R_MIPS_CALL16:
8824
0
  case R_MIPS_CALL_HI16:
8825
0
  case R_MIPS_CALL_LO16:
8826
0
  case R_MIPS16_CALL16:
8827
0
  case R_MICROMIPS_CALL16:
8828
0
  case R_MICROMIPS_CALL_HI16:
8829
0
  case R_MICROMIPS_CALL_LO16:
8830
0
    call_reloc_p = true;
8831
    /* Fall through.  */
8832
8833
0
  case R_MIPS_GOT16:
8834
0
  case R_MIPS_GOT_LO16:
8835
0
  case R_MIPS_GOT_PAGE:
8836
0
  case R_MIPS_GOT_DISP:
8837
0
  case R_MIPS16_GOT16:
8838
0
  case R_MICROMIPS_GOT16:
8839
0
  case R_MICROMIPS_GOT_LO16:
8840
0
  case R_MICROMIPS_GOT_PAGE:
8841
0
  case R_MICROMIPS_GOT_DISP:
8842
    /* If we have a symbol that will resolve to zero at static link
8843
       time and it is used by a GOT relocation applied to code we
8844
       cannot relax to an immediate zero load, then we will be using
8845
       the special `__gnu_absolute_zero' symbol whose value is zero
8846
       at dynamic load time.  We ignore HI16-type GOT relocations at
8847
       this stage, because their handling will depend entirely on
8848
       the corresponding LO16-type GOT relocation.  */
8849
0
    if (!call_hi16_reloc_p (r_type)
8850
0
        && h != NULL
8851
0
        && bfd_link_pic (info)
8852
0
        && !htab->use_absolute_zero
8853
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8854
0
      {
8855
0
        bool rel_reloc;
8856
8857
0
        if (!mips_elf_get_section_contents (abfd, sec, &contents))
8858
0
    return false;
8859
8860
0
        rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8861
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8862
0
        if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8863
0
    if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8864
0
            false))
8865
0
      if (!mips_elf_define_absolute_zero (abfd, info, htab,
8866
0
                  r_type))
8867
0
        return false;
8868
0
      }
8869
8870
    /* Fall through.  */
8871
0
  case R_MIPS_GOT_HI16:
8872
0
  case R_MIPS_GOT_OFST:
8873
0
  case R_MIPS_TLS_GOTTPREL:
8874
0
  case R_MIPS_TLS_GD:
8875
0
  case R_MIPS_TLS_LDM:
8876
0
  case R_MIPS16_TLS_GOTTPREL:
8877
0
  case R_MIPS16_TLS_GD:
8878
0
  case R_MIPS16_TLS_LDM:
8879
0
  case R_MICROMIPS_GOT_HI16:
8880
0
  case R_MICROMIPS_GOT_OFST:
8881
0
  case R_MICROMIPS_TLS_GOTTPREL:
8882
0
  case R_MICROMIPS_TLS_GD:
8883
0
  case R_MICROMIPS_TLS_LDM:
8884
0
    if (dynobj == NULL)
8885
0
      {
8886
0
        dynobj = _bfd_elf_link_dynobj (info);
8887
0
        if (dynobj == NULL)
8888
0
    return false;
8889
0
      }
8890
0
    if (!mips_elf_create_got_section (dynobj, info))
8891
0
      return false;
8892
0
    if (htab->root.target_os == is_vxworks
8893
0
        && !bfd_link_pic (info))
8894
0
      {
8895
0
        _bfd_error_handler
8896
    /* xgettext:c-format */
8897
0
    (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8898
0
     abfd, (uint64_t) rel->r_offset);
8899
0
        bfd_set_error (bfd_error_bad_value);
8900
0
        return false;
8901
0
      }
8902
0
    can_make_dynamic_p = true;
8903
0
    break;
8904
8905
0
  case R_MIPS_NONE:
8906
0
  case R_MIPS_JALR:
8907
0
  case R_MICROMIPS_JALR:
8908
    /* These relocations have empty fields and are purely there to
8909
       provide link information.  The symbol value doesn't matter.  */
8910
0
    constrain_symbol_p = false;
8911
0
    break;
8912
8913
0
  case R_MIPS_GPREL16:
8914
0
  case R_MIPS_GPREL32:
8915
0
  case R_MIPS16_GPREL:
8916
0
  case R_MICROMIPS_GPREL16:
8917
    /* GP-relative relocations always resolve to a definition in a
8918
       regular input file, ignoring the one-definition rule.  This is
8919
       important for the GP setup sequence in NewABI code, which
8920
       always resolves to a local function even if other relocations
8921
       against the symbol wouldn't.  */
8922
0
    constrain_symbol_p = false;
8923
0
    break;
8924
8925
0
  case R_MIPS_32:
8926
0
  case R_MIPS_REL32:
8927
0
  case R_MIPS_64:
8928
    /* In VxWorks executables, references to external symbols
8929
       must be handled using copy relocs or PLT entries; it is not
8930
       possible to convert this relocation into a dynamic one.
8931
8932
       For executables that use PLTs and copy-relocs, we have a
8933
       choice between converting the relocation into a dynamic
8934
       one or using copy relocations or PLT entries.  It is
8935
       usually better to do the former, unless the relocation is
8936
       against a read-only section.  */
8937
0
    if ((bfd_link_pic (info)
8938
0
         || (h != NULL
8939
0
       && htab->root.target_os != is_vxworks
8940
0
       && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8941
0
       && !(!info->nocopyreloc
8942
0
      && !PIC_OBJECT_P (abfd)
8943
0
      && MIPS_ELF_READONLY_SECTION (sec))))
8944
0
        && (sec->flags & SEC_ALLOC) != 0)
8945
0
      {
8946
0
        can_make_dynamic_p = true;
8947
0
        if (dynobj == NULL)
8948
0
    {
8949
0
      dynobj = _bfd_elf_link_dynobj (info);
8950
0
      if (dynobj == NULL)
8951
0
        return false;
8952
0
    }
8953
0
      }
8954
0
    break;
8955
8956
0
  case R_MIPS_26:
8957
0
  case R_MIPS_PC16:
8958
0
  case R_MIPS_PC21_S2:
8959
0
  case R_MIPS_PC26_S2:
8960
0
  case R_MIPS16_26:
8961
0
  case R_MIPS16_PC16_S1:
8962
0
  case R_MICROMIPS_26_S1:
8963
0
  case R_MICROMIPS_PC7_S1:
8964
0
  case R_MICROMIPS_PC10_S1:
8965
0
  case R_MICROMIPS_PC16_S1:
8966
0
  case R_MICROMIPS_PC23_S2:
8967
0
    call_reloc_p = true;
8968
0
    break;
8969
0
  }
8970
8971
0
      if (h)
8972
0
  {
8973
0
    if (constrain_symbol_p)
8974
0
      {
8975
0
        if (!can_make_dynamic_p)
8976
0
    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8977
8978
0
        if (!call_reloc_p)
8979
0
    h->pointer_equality_needed = 1;
8980
8981
        /* We must not create a stub for a symbol that has
8982
     relocations related to taking the function's address.
8983
     This doesn't apply to VxWorks, where CALL relocs refer
8984
     to a .got.plt entry instead of a normal .got entry.  */
8985
0
        if (htab->root.target_os != is_vxworks
8986
0
      && (!can_make_dynamic_p || !call_reloc_p))
8987
0
    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8988
0
      }
8989
8990
    /* Relocations against the special VxWorks __GOTT_BASE__ and
8991
       __GOTT_INDEX__ symbols must be left to the loader.  Allocate
8992
       room for them in .rela.dyn.  */
8993
0
    if (is_gott_symbol (info, h))
8994
0
      {
8995
0
        if (sreloc == NULL)
8996
0
    {
8997
0
      sreloc = mips_elf_rel_dyn_section (info, true);
8998
0
      if (sreloc == NULL)
8999
0
        return false;
9000
0
    }
9001
0
        mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9002
0
        if (MIPS_ELF_READONLY_SECTION (sec))
9003
    /* We tell the dynamic linker that there are
9004
       relocations against the text segment.  */
9005
0
    info->flags |= DF_TEXTREL;
9006
0
      }
9007
0
  }
9008
0
      else if (call_lo16_reloc_p (r_type)
9009
0
         || got_lo16_reloc_p (r_type)
9010
0
         || got_disp_reloc_p (r_type)
9011
0
         || (got16_reloc_p (r_type)
9012
0
       && htab->root.target_os == is_vxworks))
9013
0
  {
9014
    /* We may need a local GOT entry for this relocation.  We
9015
       don't count R_MIPS_GOT_PAGE because we can estimate the
9016
       maximum number of pages needed by looking at the size of
9017
       the segment.  Similar comments apply to R_MIPS*_GOT16 and
9018
       R_MIPS*_CALL16, except on VxWorks, where GOT relocations
9019
       always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
9020
       R_MIPS_CALL_HI16 because these are always followed by an
9021
       R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
9022
0
    if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9023
0
             rel->r_addend, info, r_type))
9024
0
      return false;
9025
0
  }
9026
9027
0
      if (h != NULL
9028
0
    && mips_elf_relocation_needs_la25_stub (abfd, r_type,
9029
0
              ELF_ST_IS_MIPS16 (h->other)))
9030
0
  ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
9031
9032
0
      switch (r_type)
9033
0
  {
9034
0
  case R_MIPS_CALL16:
9035
0
  case R_MIPS16_CALL16:
9036
0
  case R_MICROMIPS_CALL16:
9037
0
    if (h == NULL)
9038
0
      {
9039
0
        _bfd_error_handler
9040
    /* xgettext:c-format */
9041
0
    (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
9042
0
     abfd, (uint64_t) rel->r_offset);
9043
0
        bfd_set_error (bfd_error_bad_value);
9044
0
        return false;
9045
0
      }
9046
    /* Fall through.  */
9047
9048
0
  case R_MIPS_CALL_HI16:
9049
0
  case R_MIPS_CALL_LO16:
9050
0
  case R_MICROMIPS_CALL_HI16:
9051
0
  case R_MICROMIPS_CALL_LO16:
9052
0
    if (h != NULL)
9053
0
      {
9054
        /* Make sure there is room in the regular GOT to hold the
9055
     function's address.  We may eliminate it in favour of
9056
     a .got.plt entry later; see mips_elf_count_got_symbols.  */
9057
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
9058
0
                  r_type))
9059
0
    return false;
9060
9061
        /* We need a stub, not a plt entry for the undefined
9062
     function.  But we record it as if it needs plt.  See
9063
     _bfd_elf_adjust_dynamic_symbol.  */
9064
0
        h->needs_plt = 1;
9065
0
        h->type = STT_FUNC;
9066
0
      }
9067
0
    break;
9068
9069
0
  case R_MIPS_GOT_PAGE:
9070
0
  case R_MICROMIPS_GOT_PAGE:
9071
0
  case R_MIPS16_GOT16:
9072
0
  case R_MIPS_GOT16:
9073
0
  case R_MIPS_GOT_HI16:
9074
0
  case R_MIPS_GOT_LO16:
9075
0
  case R_MICROMIPS_GOT16:
9076
0
  case R_MICROMIPS_GOT_HI16:
9077
0
  case R_MICROMIPS_GOT_LO16:
9078
0
    if (!h || got_page_reloc_p (r_type))
9079
0
      {
9080
        /* This relocation needs (or may need, if h != NULL) a
9081
     page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
9082
     know for sure until we know whether the symbol is
9083
     preemptible.  */
9084
0
        if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
9085
0
    {
9086
0
      if (!mips_elf_get_section_contents (abfd, sec, &contents))
9087
0
        return false;
9088
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
9089
0
      addend = mips_elf_read_rel_addend (abfd, sec, rel,
9090
0
                 howto, contents);
9091
0
      if (got16_reloc_p (r_type))
9092
0
        mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
9093
0
              contents, &addend);
9094
0
      else
9095
0
        addend <<= howto->rightshift;
9096
0
    }
9097
0
        else
9098
0
    addend = rel->r_addend;
9099
0
        if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9100
0
             h, addend))
9101
0
    return false;
9102
9103
0
        if (h)
9104
0
    {
9105
0
      struct mips_elf_link_hash_entry *hmips =
9106
0
        (struct mips_elf_link_hash_entry *) h;
9107
9108
      /* This symbol is definitely not overridable.  */
9109
0
      if (hmips->root.def_regular
9110
0
          && ! (bfd_link_pic (info) && ! info->symbolic
9111
0
          && ! hmips->root.forced_local))
9112
0
        h = NULL;
9113
0
    }
9114
0
      }
9115
    /* If this is a global, overridable symbol, GOT_PAGE will
9116
       decay to GOT_DISP, so we'll need a GOT entry for it.  */
9117
    /* Fall through.  */
9118
9119
0
  case R_MIPS_GOT_DISP:
9120
0
  case R_MICROMIPS_GOT_DISP:
9121
0
    if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9122
0
                   false, r_type))
9123
0
      return false;
9124
0
    break;
9125
9126
0
  case R_MIPS_TLS_GOTTPREL:
9127
0
  case R_MIPS16_TLS_GOTTPREL:
9128
0
  case R_MICROMIPS_TLS_GOTTPREL:
9129
0
    if (bfd_link_pic (info))
9130
0
      info->flags |= DF_STATIC_TLS;
9131
    /* Fall through */
9132
9133
0
  case R_MIPS_TLS_LDM:
9134
0
  case R_MIPS16_TLS_LDM:
9135
0
  case R_MICROMIPS_TLS_LDM:
9136
0
    if (tls_ldm_reloc_p (r_type))
9137
0
      {
9138
0
        r_symndx = STN_UNDEF;
9139
0
        h = NULL;
9140
0
      }
9141
    /* Fall through */
9142
9143
0
  case R_MIPS_TLS_GD:
9144
0
  case R_MIPS16_TLS_GD:
9145
0
  case R_MICROMIPS_TLS_GD:
9146
    /* This symbol requires a global offset table entry, or two
9147
       for TLS GD relocations.  */
9148
0
    if (h != NULL)
9149
0
      {
9150
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info,
9151
0
                  false, r_type))
9152
0
    return false;
9153
0
      }
9154
0
    else
9155
0
      {
9156
0
        if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9157
0
                 rel->r_addend,
9158
0
                 info, r_type))
9159
0
    return false;
9160
0
      }
9161
0
    break;
9162
9163
0
  case R_MIPS_32:
9164
0
  case R_MIPS_REL32:
9165
0
  case R_MIPS_64:
9166
    /* In VxWorks executables, references to external symbols
9167
       are handled using copy relocs or PLT stubs, so there's
9168
       no need to add a .rela.dyn entry for this relocation.  */
9169
0
    if (can_make_dynamic_p)
9170
0
      {
9171
0
        if (sreloc == NULL)
9172
0
    {
9173
0
      sreloc = mips_elf_rel_dyn_section (info, true);
9174
0
      if (sreloc == NULL)
9175
0
        return false;
9176
0
    }
9177
0
        if (bfd_link_pic (info) && h == NULL)
9178
0
    {
9179
      /* When creating a shared object, we must copy these
9180
         reloc types into the output file as R_MIPS_REL32
9181
         relocs.  Make room for this reloc in .rel(a).dyn.  */
9182
0
      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9183
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9184
        /* We tell the dynamic linker that there are
9185
           relocations against the text segment.  */
9186
0
        info->flags |= DF_TEXTREL;
9187
0
    }
9188
0
        else
9189
0
    {
9190
0
      struct mips_elf_link_hash_entry *hmips;
9191
9192
      /* For a shared object, we must copy this relocation
9193
         unless the symbol turns out to be undefined and
9194
         weak with non-default visibility, in which case
9195
         it will be left as zero.
9196
9197
         We could elide R_MIPS_REL32 for locally binding symbols
9198
         in shared libraries, but do not yet do so.
9199
9200
         For an executable, we only need to copy this
9201
         reloc if the symbol is defined in a dynamic
9202
         object.  */
9203
0
      hmips = (struct mips_elf_link_hash_entry *) h;
9204
0
      ++hmips->possibly_dynamic_relocs;
9205
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9206
        /* We need it to tell the dynamic linker if there
9207
           are relocations against the text segment.  */
9208
0
        hmips->readonly_reloc = true;
9209
0
    }
9210
0
      }
9211
9212
0
    if (SGI_COMPAT (abfd))
9213
0
      mips_elf_hash_table (info)->compact_rel_size +=
9214
0
        sizeof (Elf32_External_crinfo);
9215
0
    break;
9216
9217
0
  case R_MIPS_26:
9218
0
  case R_MIPS_GPREL16:
9219
0
  case R_MIPS_LITERAL:
9220
0
  case R_MIPS_GPREL32:
9221
0
  case R_MICROMIPS_26_S1:
9222
0
  case R_MICROMIPS_GPREL16:
9223
0
  case R_MICROMIPS_LITERAL:
9224
0
  case R_MICROMIPS_GPREL7_S2:
9225
0
    if (SGI_COMPAT (abfd))
9226
0
      mips_elf_hash_table (info)->compact_rel_size +=
9227
0
        sizeof (Elf32_External_crinfo);
9228
0
    break;
9229
9230
    /* This relocation describes the C++ object vtable hierarchy.
9231
       Reconstruct it for later use during GC.  */
9232
0
  case R_MIPS_GNU_VTINHERIT:
9233
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9234
0
      return false;
9235
0
    break;
9236
9237
    /* This relocation describes which C++ vtable entries are actually
9238
       used.  Record for later use during GC.  */
9239
0
  case R_MIPS_GNU_VTENTRY:
9240
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9241
0
      return false;
9242
0
    break;
9243
9244
0
  default:
9245
0
    break;
9246
0
  }
9247
9248
      /* Record the need for a PLT entry.  At this point we don't know
9249
   yet if we are going to create a PLT in the first place, but
9250
   we only record whether the relocation requires a standard MIPS
9251
   or a compressed code entry anyway.  If we don't make a PLT after
9252
   all, then we'll just ignore these arrangements.  Likewise if
9253
   a PLT entry is not created because the symbol is satisfied
9254
   locally.  */
9255
0
      if (h != NULL
9256
0
    && (branch_reloc_p (r_type)
9257
0
        || mips16_branch_reloc_p (r_type)
9258
0
        || micromips_branch_reloc_p (r_type))
9259
0
    && !SYMBOL_CALLS_LOCAL (info, h))
9260
0
  {
9261
0
    if (h->plt.plist == NULL)
9262
0
      h->plt.plist = mips_elf_make_plt_record (abfd);
9263
0
    if (h->plt.plist == NULL)
9264
0
      return false;
9265
9266
0
    if (branch_reloc_p (r_type))
9267
0
      h->plt.plist->need_mips = true;
9268
0
    else
9269
0
      h->plt.plist->need_comp = true;
9270
0
  }
9271
9272
      /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9273
   if there is one.  We only need to handle global symbols here;
9274
   we decide whether to keep or delete stubs for local symbols
9275
   when processing the stub's relocations.  */
9276
0
      if (h != NULL
9277
0
    && !mips16_call_reloc_p (r_type)
9278
0
    && !section_allows_mips16_refs_p (sec))
9279
0
  {
9280
0
    struct mips_elf_link_hash_entry *mh;
9281
9282
0
    mh = (struct mips_elf_link_hash_entry *) h;
9283
0
    mh->need_fn_stub = true;
9284
0
  }
9285
9286
      /* Refuse some position-dependent relocations when creating a
9287
   shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
9288
   not PIC, but we can create dynamic relocations and the result
9289
   will be fine.  Also do not refuse R_MIPS_LO16, which can be
9290
   combined with R_MIPS_GOT16.  */
9291
0
      if (bfd_link_pic (info))
9292
0
  {
9293
0
    switch (r_type)
9294
0
      {
9295
0
      case R_MIPS_TLS_TPREL_HI16:
9296
0
      case R_MIPS16_TLS_TPREL_HI16:
9297
0
      case R_MICROMIPS_TLS_TPREL_HI16:
9298
0
      case R_MIPS_TLS_TPREL_LO16:
9299
0
      case R_MIPS16_TLS_TPREL_LO16:
9300
0
      case R_MICROMIPS_TLS_TPREL_LO16:
9301
        /* These are okay in PIE, but not in a shared library.  */
9302
0
        if (bfd_link_executable (info))
9303
0
    break;
9304
9305
        /* FALLTHROUGH */
9306
9307
0
      case R_MIPS16_HI16:
9308
0
      case R_MIPS_HI16:
9309
0
      case R_MIPS_HIGHER:
9310
0
      case R_MIPS_HIGHEST:
9311
0
      case R_MICROMIPS_HI16:
9312
0
      case R_MICROMIPS_HIGHER:
9313
0
      case R_MICROMIPS_HIGHEST:
9314
        /* Don't refuse a high part relocation if it's against
9315
     no symbol (e.g. part of a compound relocation).  */
9316
0
        if (r_symndx == STN_UNDEF)
9317
0
    break;
9318
9319
        /* Likewise an absolute symbol.  */
9320
0
        if (h != NULL && bfd_is_abs_symbol (&h->root))
9321
0
    break;
9322
9323
        /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9324
     and has a special meaning.  */
9325
0
        if (!NEWABI_P (abfd) && h != NULL
9326
0
      && strcmp (h->root.root.string, "_gp_disp") == 0)
9327
0
    break;
9328
9329
        /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
9330
0
        if (is_gott_symbol (info, h))
9331
0
    break;
9332
9333
        /* FALLTHROUGH */
9334
9335
0
      case R_MIPS16_26:
9336
0
      case R_MIPS_26:
9337
0
      case R_MICROMIPS_26_S1:
9338
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9339
        /* An error for unsupported relocations is raised as part
9340
     of the above search, so we can skip the following.  */
9341
0
        if (howto != NULL)
9342
0
    info->callbacks->einfo
9343
      /* xgettext:c-format */
9344
0
      (_("%X%H: relocation %s against `%s' cannot be used"
9345
0
         " when making a shared object; recompile with -fPIC\n"),
9346
0
       abfd, sec, rel->r_offset, howto->name,
9347
0
       (h) ? h->root.root.string : "a local symbol");
9348
0
        break;
9349
0
      default:
9350
0
        break;
9351
0
      }
9352
0
  }
9353
0
    }
9354
9355
0
  return true;
9356
0
}
9357

9358
/* Allocate space for global sym dynamic relocs.  */
9359
9360
static bool
9361
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9362
0
{
9363
0
  struct bfd_link_info *info = inf;
9364
0
  bfd *dynobj;
9365
0
  struct mips_elf_link_hash_entry *hmips;
9366
0
  struct mips_elf_link_hash_table *htab;
9367
9368
0
  htab = mips_elf_hash_table (info);
9369
0
  BFD_ASSERT (htab != NULL);
9370
9371
0
  dynobj = elf_hash_table (info)->dynobj;
9372
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9373
9374
  /* VxWorks executables are handled elsewhere; we only need to
9375
     allocate relocations in shared objects.  */
9376
0
  if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9377
0
    return true;
9378
9379
  /* Ignore indirect symbols.  All relocations against such symbols
9380
     will be redirected to the target symbol.  */
9381
0
  if (h->root.type == bfd_link_hash_indirect)
9382
0
    return true;
9383
9384
  /* If this symbol is defined in a dynamic object, or we are creating
9385
     a shared library, we will need to copy any R_MIPS_32 or
9386
     R_MIPS_REL32 relocs against it into the output file.  */
9387
0
  if (! bfd_link_relocatable (info)
9388
0
      && hmips->possibly_dynamic_relocs != 0
9389
0
      && (h->root.type == bfd_link_hash_defweak
9390
0
    || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9391
0
    || bfd_link_pic (info)))
9392
0
    {
9393
0
      bool do_copy = true;
9394
9395
0
      if (h->root.type == bfd_link_hash_undefweak)
9396
0
  {
9397
    /* Do not copy relocations for undefined weak symbols that
9398
       we are not going to export.  */
9399
0
    if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9400
0
      do_copy = false;
9401
9402
    /* Make sure undefined weak symbols are output as a dynamic
9403
       symbol in PIEs.  */
9404
0
    else if (h->dynindx == -1 && !h->forced_local)
9405
0
      {
9406
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
9407
0
    return false;
9408
0
      }
9409
0
  }
9410
9411
0
      if (do_copy)
9412
0
  {
9413
    /* Even though we don't directly need a GOT entry for this symbol,
9414
       the SVR4 psABI requires it to have a dynamic symbol table
9415
       index greater that DT_MIPS_GOTSYM if there are dynamic
9416
       relocations against it.
9417
9418
       VxWorks does not enforce the same mapping between the GOT
9419
       and the symbol table, so the same requirement does not
9420
       apply there.  */
9421
0
    if (htab->root.target_os != is_vxworks)
9422
0
      {
9423
0
        if (hmips->global_got_area > GGA_RELOC_ONLY)
9424
0
    hmips->global_got_area = GGA_RELOC_ONLY;
9425
0
        hmips->got_only_for_calls = false;
9426
0
      }
9427
9428
0
    mips_elf_allocate_dynamic_relocations
9429
0
      (dynobj, info, hmips->possibly_dynamic_relocs);
9430
0
    if (hmips->readonly_reloc)
9431
      /* We tell the dynamic linker that there are relocations
9432
         against the text segment.  */
9433
0
      info->flags |= DF_TEXTREL;
9434
0
  }
9435
0
    }
9436
9437
0
  return true;
9438
0
}
9439
9440
/* Adjust a symbol defined by a dynamic object and referenced by a
9441
   regular object.  The current definition is in some section of the
9442
   dynamic object, but we're not including those sections.  We have to
9443
   change the definition to something the rest of the link can
9444
   understand.  */
9445
9446
bool
9447
_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9448
             struct elf_link_hash_entry *h)
9449
0
{
9450
0
  bfd *dynobj;
9451
0
  struct mips_elf_link_hash_entry *hmips;
9452
0
  struct mips_elf_link_hash_table *htab;
9453
0
  asection *s, *srel;
9454
9455
0
  htab = mips_elf_hash_table (info);
9456
0
  BFD_ASSERT (htab != NULL);
9457
9458
0
  dynobj = elf_hash_table (info)->dynobj;
9459
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9460
9461
  /* Make sure we know what is going on here.  */
9462
0
  if (dynobj == NULL
9463
0
      || (! h->needs_plt
9464
0
    && ! h->is_weakalias
9465
0
    && (! h->def_dynamic
9466
0
        || ! h->ref_regular
9467
0
        || h->def_regular)))
9468
0
    {
9469
0
      if (h->type == STT_GNU_IFUNC)
9470
0
  _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9471
0
          h->root.root.string);
9472
0
      else
9473
0
  _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9474
0
          h->root.root.string);
9475
0
      return true;
9476
0
    }
9477
9478
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9479
9480
  /* If there are call relocations against an externally-defined symbol,
9481
     see whether we can create a MIPS lazy-binding stub for it.  We can
9482
     only do this if all references to the function are through call
9483
     relocations, and in that case, the traditional lazy-binding stubs
9484
     are much more efficient than PLT entries.
9485
9486
     Traditional stubs are only available on SVR4 psABI-based systems;
9487
     VxWorks always uses PLTs instead.  */
9488
0
  if (htab->root.target_os != is_vxworks
9489
0
      && h->needs_plt
9490
0
      && !hmips->no_fn_stub)
9491
0
    {
9492
0
      if (! elf_hash_table (info)->dynamic_sections_created)
9493
0
  return true;
9494
9495
      /* If this symbol is not defined in a regular file, then set
9496
   the symbol to the stub location.  This is required to make
9497
   function pointers compare as equal between the normal
9498
   executable and the shared library.  */
9499
0
      if (!h->def_regular
9500
0
    && !bfd_is_abs_section (htab->sstubs->output_section))
9501
0
  {
9502
0
    hmips->needs_lazy_stub = true;
9503
0
    htab->lazy_stub_count++;
9504
0
    return true;
9505
0
  }
9506
0
    }
9507
  /* As above, VxWorks requires PLT entries for externally-defined
9508
     functions that are only accessed through call relocations.
9509
9510
     Both VxWorks and non-VxWorks targets also need PLT entries if there
9511
     are static-only relocations against an externally-defined function.
9512
     This can technically occur for shared libraries if there are
9513
     branches to the symbol, although it is unlikely that this will be
9514
     used in practice due to the short ranges involved.  It can occur
9515
     for any relative or absolute relocation in executables; in that
9516
     case, the PLT entry becomes the function's canonical address.  */
9517
0
  else if (((h->needs_plt && !hmips->no_fn_stub)
9518
0
      || (h->type == STT_FUNC && hmips->has_static_relocs))
9519
0
     && htab->use_plts_and_copy_relocs
9520
0
     && !SYMBOL_CALLS_LOCAL (info, h)
9521
0
     && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9522
0
    && h->root.type == bfd_link_hash_undefweak))
9523
0
    {
9524
0
      bool micromips_p = MICROMIPS_P (info->output_bfd);
9525
0
      bool newabi_p = NEWABI_P (info->output_bfd);
9526
9527
      /* If this is the first symbol to need a PLT entry, then make some
9528
   basic setup.  Also work out PLT entry sizes.  We'll need them
9529
   for PLT offset calculations.  */
9530
0
      if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9531
0
  {
9532
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
9533
0
    BFD_ASSERT (htab->plt_got_index == 0);
9534
9535
    /* If we're using the PLT additions to the psABI, each PLT
9536
       entry is 16 bytes and the PLT0 entry is 32 bytes.
9537
       Encourage better cache usage by aligning.  We do this
9538
       lazily to avoid pessimizing traditional objects.  */
9539
0
    if (htab->root.target_os != is_vxworks
9540
0
        && !bfd_link_align_section (htab->root.splt, 5))
9541
0
      return false;
9542
9543
    /* Make sure that .got.plt is word-aligned.  We do this lazily
9544
       for the same reason as above.  */
9545
0
    if (!bfd_link_align_section (htab->root.sgotplt,
9546
0
               MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9547
0
      return false;
9548
9549
    /* On non-VxWorks targets, the first two entries in .got.plt
9550
       are reserved.  */
9551
0
    if (htab->root.target_os != is_vxworks)
9552
0
      htab->plt_got_index
9553
0
        += (get_elf_backend_data (dynobj)->got_header_size
9554
0
      / MIPS_ELF_GOT_SIZE (dynobj));
9555
9556
    /* On VxWorks, also allocate room for the header's
9557
       .rela.plt.unloaded entries.  */
9558
0
    if (htab->root.target_os == is_vxworks
9559
0
        && !bfd_link_pic (info))
9560
0
      htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9561
9562
    /* Now work out the sizes of individual PLT entries.  */
9563
0
    if (htab->root.target_os == is_vxworks
9564
0
        && bfd_link_pic (info))
9565
0
      htab->plt_mips_entry_size
9566
0
        = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9567
0
    else if (htab->root.target_os == is_vxworks)
9568
0
      htab->plt_mips_entry_size
9569
0
        = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9570
0
    else if (newabi_p)
9571
0
      htab->plt_mips_entry_size
9572
0
        = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9573
0
    else if (!micromips_p)
9574
0
      {
9575
0
        htab->plt_mips_entry_size
9576
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9577
0
        htab->plt_comp_entry_size
9578
0
    = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9579
0
      }
9580
0
    else if (htab->insn32)
9581
0
      {
9582
0
        htab->plt_mips_entry_size
9583
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9584
0
        htab->plt_comp_entry_size
9585
0
    = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9586
0
      }
9587
0
    else
9588
0
      {
9589
0
        htab->plt_mips_entry_size
9590
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9591
0
        htab->plt_comp_entry_size
9592
0
    = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9593
0
      }
9594
0
  }
9595
9596
0
      if (h->plt.plist == NULL)
9597
0
  h->plt.plist = mips_elf_make_plt_record (dynobj);
9598
0
      if (h->plt.plist == NULL)
9599
0
  return false;
9600
9601
      /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9602
   n32 or n64, so always use a standard entry there.
9603
9604
   If the symbol has a MIPS16 call stub and gets a PLT entry, then
9605
   all MIPS16 calls will go via that stub, and there is no benefit
9606
   to having a MIPS16 entry.  And in the case of call_stub a
9607
   standard entry actually has to be used as the stub ends with a J
9608
   instruction.  */
9609
0
      if (newabi_p
9610
0
    || htab->root.target_os == is_vxworks
9611
0
    || hmips->call_stub
9612
0
    || hmips->call_fp_stub)
9613
0
  {
9614
0
    h->plt.plist->need_mips = true;
9615
0
    h->plt.plist->need_comp = false;
9616
0
  }
9617
9618
      /* Otherwise, if there are no direct calls to the function, we
9619
   have a free choice of whether to use standard or compressed
9620
   entries.  Prefer microMIPS entries if the object is known to
9621
   contain microMIPS code, so that it becomes possible to create
9622
   pure microMIPS binaries.  Prefer standard entries otherwise,
9623
   because MIPS16 ones are no smaller and are usually slower.  */
9624
0
      if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9625
0
  {
9626
0
    if (micromips_p)
9627
0
      h->plt.plist->need_comp = true;
9628
0
    else
9629
0
      h->plt.plist->need_mips = true;
9630
0
  }
9631
9632
0
      if (h->plt.plist->need_mips)
9633
0
  {
9634
0
    h->plt.plist->mips_offset = htab->plt_mips_offset;
9635
0
    htab->plt_mips_offset += htab->plt_mips_entry_size;
9636
0
  }
9637
0
      if (h->plt.plist->need_comp)
9638
0
  {
9639
0
    h->plt.plist->comp_offset = htab->plt_comp_offset;
9640
0
    htab->plt_comp_offset += htab->plt_comp_entry_size;
9641
0
  }
9642
9643
      /* Reserve the corresponding .got.plt entry now too.  */
9644
0
      h->plt.plist->gotplt_index = htab->plt_got_index++;
9645
9646
      /* If the output file has no definition of the symbol, set the
9647
   symbol's value to the address of the stub.  */
9648
0
      if (!bfd_link_pic (info) && !h->def_regular)
9649
0
  hmips->use_plt_entry = true;
9650
9651
      /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
9652
0
      htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9653
0
           ? MIPS_ELF_RELA_SIZE (dynobj)
9654
0
           : MIPS_ELF_REL_SIZE (dynobj));
9655
9656
      /* Make room for the .rela.plt.unloaded relocations.  */
9657
0
      if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9658
0
  htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9659
9660
      /* All relocations against this symbol that could have been made
9661
   dynamic will now refer to the PLT entry instead.  */
9662
0
      hmips->possibly_dynamic_relocs = 0;
9663
9664
0
      return true;
9665
0
    }
9666
9667
  /* If this is a weak symbol, and there is a real definition, the
9668
     processor independent code will have arranged for us to see the
9669
     real definition first, and we can just use the same value.  */
9670
0
  if (h->is_weakalias)
9671
0
    {
9672
0
      struct elf_link_hash_entry *def = weakdef (h);
9673
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9674
0
      h->root.u.def.section = def->root.u.def.section;
9675
0
      h->root.u.def.value = def->root.u.def.value;
9676
0
      return true;
9677
0
    }
9678
9679
  /* Otherwise, there is nothing further to do for symbols defined
9680
     in regular objects.  */
9681
0
  if (h->def_regular)
9682
0
    return true;
9683
9684
  /* There's also nothing more to do if we'll convert all relocations
9685
     against this symbol into dynamic relocations.  */
9686
0
  if (!hmips->has_static_relocs)
9687
0
    return true;
9688
9689
  /* We're now relying on copy relocations.  Complain if we have
9690
     some that we can't convert.  */
9691
0
  if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9692
0
    {
9693
0
      _bfd_error_handler (_("non-dynamic relocations refer to "
9694
0
          "dynamic symbol %s"),
9695
0
        h->root.root.string);
9696
0
      bfd_set_error (bfd_error_bad_value);
9697
0
      return false;
9698
0
    }
9699
9700
  /* We must allocate the symbol in our .dynbss section, which will
9701
     become part of the .bss section of the executable.  There will be
9702
     an entry for this symbol in the .dynsym section.  The dynamic
9703
     object will contain position independent code, so all references
9704
     from the dynamic object to this symbol will go through the global
9705
     offset table.  The dynamic linker will use the .dynsym entry to
9706
     determine the address it must put in the global offset table, so
9707
     both the dynamic object and the regular object will refer to the
9708
     same memory location for the variable.  */
9709
9710
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9711
0
    {
9712
0
      s = htab->root.sdynrelro;
9713
0
      srel = htab->root.sreldynrelro;
9714
0
    }
9715
0
  else
9716
0
    {
9717
0
      s = htab->root.sdynbss;
9718
0
      srel = htab->root.srelbss;
9719
0
    }
9720
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9721
0
    {
9722
0
      if (htab->root.target_os == is_vxworks)
9723
0
  srel->size += sizeof (Elf32_External_Rela);
9724
0
      else
9725
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9726
0
      h->needs_copy = 1;
9727
0
    }
9728
9729
  /* All relocations against this symbol that could have been made
9730
     dynamic will now refer to the local copy instead.  */
9731
0
  hmips->possibly_dynamic_relocs = 0;
9732
9733
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
9734
0
}
9735

9736
/* If the link uses a GOT, lay it out and work out its size.  */
9737
9738
static bool
9739
mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9740
0
{
9741
0
  bfd *dynobj;
9742
0
  asection *s;
9743
0
  struct mips_got_info *g;
9744
0
  bfd_size_type loadable_size = 0;
9745
0
  bfd_size_type page_gotno;
9746
0
  bfd *ibfd;
9747
0
  struct mips_elf_traverse_got_arg tga;
9748
0
  struct mips_elf_link_hash_table *htab;
9749
9750
0
  htab = mips_elf_hash_table (info);
9751
0
  BFD_ASSERT (htab != NULL);
9752
9753
0
  s = htab->root.sgot;
9754
0
  if (s == NULL)
9755
0
    return true;
9756
9757
0
  dynobj = elf_hash_table (info)->dynobj;
9758
0
  g = htab->got_info;
9759
9760
  /* Allocate room for the reserved entries.  VxWorks always reserves
9761
     3 entries; other objects only reserve 2 entries.  */
9762
0
  BFD_ASSERT (g->assigned_low_gotno == 0);
9763
0
  if (htab->root.target_os == is_vxworks)
9764
0
    htab->reserved_gotno = 3;
9765
0
  else
9766
0
    htab->reserved_gotno = 2;
9767
0
  g->local_gotno += htab->reserved_gotno;
9768
0
  g->assigned_low_gotno = htab->reserved_gotno;
9769
9770
  /* Decide which symbols need to go in the global part of the GOT and
9771
     count the number of reloc-only GOT symbols.  */
9772
0
  mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9773
9774
0
  if (!mips_elf_resolve_final_got_entries (info, g))
9775
0
    return false;
9776
9777
  /* Calculate the total loadable size of the output.  That
9778
     will give us the maximum number of GOT_PAGE entries
9779
     required.  */
9780
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9781
0
    {
9782
0
      asection *subsection;
9783
9784
0
      for (subsection = ibfd->sections;
9785
0
     subsection;
9786
0
     subsection = subsection->next)
9787
0
  {
9788
0
    if ((subsection->flags & SEC_ALLOC) == 0)
9789
0
      continue;
9790
0
    loadable_size += ((subsection->size + 0xf)
9791
0
          &~ (bfd_size_type) 0xf);
9792
0
  }
9793
0
    }
9794
9795
0
  if (htab->root.target_os == is_vxworks)
9796
    /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9797
       relocations against local symbols evaluate to "G", and the EABI does
9798
       not include R_MIPS_GOT_PAGE.  */
9799
0
    page_gotno = 0;
9800
0
  else
9801
    /* Assume there are two loadable segments consisting of contiguous
9802
       sections.  Is 5 enough?  */
9803
0
    page_gotno = (loadable_size >> 16) + 5;
9804
9805
  /* Choose the smaller of the two page estimates; both are intended to be
9806
     conservative.  */
9807
0
  if (page_gotno > g->page_gotno)
9808
0
    page_gotno = g->page_gotno;
9809
9810
0
  g->local_gotno += page_gotno;
9811
0
  g->assigned_high_gotno = g->local_gotno - 1;
9812
9813
0
  s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9814
0
  s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9815
0
  s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9816
9817
  /* VxWorks does not support multiple GOTs.  It initializes $gp to
9818
     __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9819
     dynamic loader.  */
9820
0
  if (htab->root.target_os != is_vxworks
9821
0
      && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9822
0
    {
9823
0
      if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9824
0
  return false;
9825
0
    }
9826
0
  else
9827
0
    {
9828
      /* Record that all bfds use G.  This also has the effect of freeing
9829
   the per-bfd GOTs, which we no longer need.  */
9830
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9831
0
  if (mips_elf_bfd_got (ibfd, false))
9832
0
    mips_elf_replace_bfd_got (ibfd, g);
9833
0
      mips_elf_replace_bfd_got (output_bfd, g);
9834
9835
      /* Set up TLS entries.  */
9836
0
      g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9837
0
      tga.info = info;
9838
0
      tga.g = g;
9839
0
      tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9840
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9841
0
      if (!tga.g)
9842
0
  return false;
9843
0
      BFD_ASSERT (g->tls_assigned_gotno
9844
0
      == g->global_gotno + g->local_gotno + g->tls_gotno);
9845
9846
      /* Each VxWorks GOT entry needs an explicit relocation.  */
9847
0
      if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9848
0
  g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9849
9850
      /* Allocate room for the TLS relocations.  */
9851
0
      if (g->relocs)
9852
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9853
0
    }
9854
9855
0
  return true;
9856
0
}
9857
9858
/* Estimate the size of the .MIPS.stubs section.  */
9859
9860
static void
9861
mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9862
0
{
9863
0
  struct mips_elf_link_hash_table *htab;
9864
0
  bfd_size_type dynsymcount;
9865
9866
0
  htab = mips_elf_hash_table (info);
9867
0
  BFD_ASSERT (htab != NULL);
9868
9869
0
  if (htab->lazy_stub_count == 0)
9870
0
    return;
9871
9872
  /* IRIX rld assumes that a function stub isn't at the end of the .text
9873
     section, so add a dummy entry to the end.  */
9874
0
  htab->lazy_stub_count++;
9875
9876
  /* Get a worst-case estimate of the number of dynamic symbols needed.
9877
     At this point, dynsymcount does not account for section symbols
9878
     and count_section_dynsyms may overestimate the number that will
9879
     be needed.  */
9880
0
  dynsymcount = (elf_hash_table (info)->dynsymcount
9881
0
     + count_section_dynsyms (output_bfd, info));
9882
9883
  /* Determine the size of one stub entry.  There's no disadvantage
9884
     from using microMIPS code here, so for the sake of pure-microMIPS
9885
     binaries we prefer it whenever there's any microMIPS code in
9886
     output produced at all.  This has a benefit of stubs being
9887
     shorter by 4 bytes each too, unless in the insn32 mode.  */
9888
0
  if (!MICROMIPS_P (output_bfd))
9889
0
    htab->function_stub_size = (dynsymcount > 0x10000
9890
0
        ? MIPS_FUNCTION_STUB_BIG_SIZE
9891
0
        : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9892
0
  else if (htab->insn32)
9893
0
    htab->function_stub_size = (dynsymcount > 0x10000
9894
0
        ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9895
0
        : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9896
0
  else
9897
0
    htab->function_stub_size = (dynsymcount > 0x10000
9898
0
        ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9899
0
        : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9900
9901
0
  htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9902
0
}
9903
9904
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9905
   mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
9906
   stub, allocate an entry in the stubs section.  */
9907
9908
static bool
9909
mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9910
0
{
9911
0
  struct mips_htab_traverse_info *hti = data;
9912
0
  struct mips_elf_link_hash_table *htab;
9913
0
  struct bfd_link_info *info;
9914
0
  bfd *output_bfd;
9915
9916
0
  info = hti->info;
9917
0
  output_bfd = hti->output_bfd;
9918
0
  htab = mips_elf_hash_table (info);
9919
0
  BFD_ASSERT (htab != NULL);
9920
9921
0
  if (h->needs_lazy_stub)
9922
0
    {
9923
0
      bool micromips_p = MICROMIPS_P (output_bfd);
9924
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9925
0
      bfd_vma isa_bit = micromips_p;
9926
9927
0
      BFD_ASSERT (htab->root.dynobj != NULL);
9928
0
      if (h->root.plt.plist == NULL)
9929
0
  h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9930
0
      if (h->root.plt.plist == NULL)
9931
0
  {
9932
0
    hti->error = true;
9933
0
    return false;
9934
0
  }
9935
0
      h->root.root.u.def.section = htab->sstubs;
9936
0
      h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9937
0
      h->root.plt.plist->stub_offset = htab->sstubs->size;
9938
0
      h->root.other = other;
9939
0
      htab->sstubs->size += htab->function_stub_size;
9940
0
    }
9941
0
  return true;
9942
0
}
9943
9944
/* Allocate offsets in the stubs section to each symbol that needs one.
9945
   Set the final size of the .MIPS.stub section.  */
9946
9947
static bool
9948
mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9949
0
{
9950
0
  bfd *output_bfd = info->output_bfd;
9951
0
  bool micromips_p = MICROMIPS_P (output_bfd);
9952
0
  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9953
0
  bfd_vma isa_bit = micromips_p;
9954
0
  struct mips_elf_link_hash_table *htab;
9955
0
  struct mips_htab_traverse_info hti;
9956
0
  struct elf_link_hash_entry *h;
9957
0
  bfd *dynobj;
9958
9959
0
  htab = mips_elf_hash_table (info);
9960
0
  BFD_ASSERT (htab != NULL);
9961
9962
0
  if (htab->lazy_stub_count == 0)
9963
0
    return true;
9964
9965
0
  htab->sstubs->size = 0;
9966
0
  hti.info = info;
9967
0
  hti.output_bfd = output_bfd;
9968
0
  hti.error = false;
9969
0
  mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9970
0
  if (hti.error)
9971
0
    return false;
9972
0
  htab->sstubs->size += htab->function_stub_size;
9973
0
  BFD_ASSERT (htab->sstubs->size
9974
0
        == htab->lazy_stub_count * htab->function_stub_size);
9975
9976
0
  dynobj = elf_hash_table (info)->dynobj;
9977
0
  BFD_ASSERT (dynobj != NULL);
9978
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9979
0
  if (h == NULL)
9980
0
    return false;
9981
0
  h->root.u.def.value = isa_bit;
9982
0
  h->other = other;
9983
0
  h->type = STT_FUNC;
9984
9985
0
  return true;
9986
0
}
9987
9988
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9989
   bfd_link_info.  If H uses the address of a PLT entry as the value
9990
   of the symbol, then set the entry in the symbol table now.  Prefer
9991
   a standard MIPS PLT entry.  */
9992
9993
static bool
9994
mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9995
0
{
9996
0
  struct bfd_link_info *info = data;
9997
0
  bool micromips_p = MICROMIPS_P (info->output_bfd);
9998
0
  struct mips_elf_link_hash_table *htab;
9999
0
  unsigned int other;
10000
0
  bfd_vma isa_bit;
10001
0
  bfd_vma val;
10002
10003
0
  htab = mips_elf_hash_table (info);
10004
0
  BFD_ASSERT (htab != NULL);
10005
10006
0
  if (h->use_plt_entry)
10007
0
    {
10008
0
      BFD_ASSERT (h->root.plt.plist != NULL);
10009
0
      BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
10010
0
      || h->root.plt.plist->comp_offset != MINUS_ONE);
10011
10012
0
      val = htab->plt_header_size;
10013
0
      if (h->root.plt.plist->mips_offset != MINUS_ONE)
10014
0
  {
10015
0
    isa_bit = 0;
10016
0
    val += h->root.plt.plist->mips_offset;
10017
0
    other = 0;
10018
0
  }
10019
0
      else
10020
0
  {
10021
0
    isa_bit = 1;
10022
0
    val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
10023
0
    other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
10024
0
  }
10025
0
      val += isa_bit;
10026
      /* For VxWorks, point at the PLT load stub rather than the lazy
10027
   resolution stub; this stub will become the canonical function
10028
   address.  */
10029
0
      if (htab->root.target_os == is_vxworks)
10030
0
  val += 8;
10031
10032
0
      h->root.root.u.def.section = htab->root.splt;
10033
0
      h->root.root.u.def.value = val;
10034
0
      h->root.other = other;
10035
0
    }
10036
10037
0
  return true;
10038
0
}
10039
10040
/* Set the sizes of the dynamic sections, some mips non-dynamic sections,
10041
   and check for any mips16 stub sections that we can discard.  */
10042
10043
bool
10044
_bfd_mips_elf_late_size_sections (struct bfd_link_info *info)
10045
0
{
10046
0
  bfd *dynobj;
10047
0
  asection *s, *sreldyn;
10048
0
  bool reltext;
10049
0
  struct mips_elf_link_hash_table *htab;
10050
0
  struct mips_htab_traverse_info hti;
10051
10052
0
  htab = mips_elf_hash_table (info);
10053
0
  BFD_ASSERT (htab != NULL);
10054
10055
  /* The .reginfo section has a fixed size.  */
10056
0
  s = bfd_get_section_by_name (info->output_bfd, ".reginfo");
10057
0
  if (s != NULL)
10058
0
    {
10059
0
      bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
10060
0
      s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
10061
0
    }
10062
10063
  /* The .MIPS.abiflags section has a fixed size.  */
10064
0
  s = bfd_get_section_by_name (info->output_bfd, ".MIPS.abiflags");
10065
0
  if (s != NULL)
10066
0
    {
10067
0
      bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
10068
0
      s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
10069
0
    }
10070
10071
0
  hti.info = info;
10072
0
  hti.output_bfd = info->output_bfd;
10073
0
  hti.error = false;
10074
0
  mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
10075
0
  if (hti.error)
10076
0
    return false;
10077
10078
0
  dynobj = htab->root.dynobj;
10079
0
  if (dynobj == NULL)
10080
0
    return true;
10081
10082
0
  if (htab->root.dynamic_sections_created)
10083
0
    {
10084
      /* Set the contents of the .interp section to the interpreter.  */
10085
0
      if (bfd_link_executable (info) && !info->nointerp)
10086
0
  {
10087
0
    s = htab->root.interp;
10088
0
    BFD_ASSERT (s != NULL);
10089
0
    s->size
10090
0
      = strlen (ELF_DYNAMIC_INTERPRETER (info->output_bfd)) + 1;
10091
0
    s->contents
10092
0
      = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (info->output_bfd);
10093
0
    s->alloced = 1;
10094
0
  }
10095
10096
      /* Figure out the size of the PLT header if we know that we
10097
   are using it.  For the sake of cache alignment always use
10098
   a standard header whenever any standard entries are present
10099
   even if microMIPS entries are present as well.  This also
10100
   lets the microMIPS header rely on the value of $v0 only set
10101
   by microMIPS entries, for a small size reduction.
10102
10103
   Set symbol table entry values for symbols that use the
10104
   address of their PLT entry now that we can calculate it.
10105
10106
   Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10107
   haven't already in _bfd_elf_create_dynamic_sections.  */
10108
0
      if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10109
0
  {
10110
0
    bool micromips_p = (MICROMIPS_P (info->output_bfd)
10111
0
             && !htab->plt_mips_offset);
10112
0
    unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10113
0
    bfd_vma isa_bit = micromips_p;
10114
0
    struct elf_link_hash_entry *h;
10115
0
    bfd_vma size;
10116
10117
0
    BFD_ASSERT (htab->use_plts_and_copy_relocs);
10118
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
10119
0
    BFD_ASSERT (htab->root.splt->size == 0);
10120
10121
0
    if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10122
0
      size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10123
0
    else if (htab->root.target_os == is_vxworks)
10124
0
      size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10125
0
    else if (ABI_64_P (info->output_bfd))
10126
0
      size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10127
0
    else if (ABI_N32_P (info->output_bfd))
10128
0
      size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10129
0
    else if (!micromips_p)
10130
0
      size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10131
0
    else if (htab->insn32)
10132
0
      size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10133
0
    else
10134
0
      size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10135
10136
0
    htab->plt_header_is_comp = micromips_p;
10137
0
    htab->plt_header_size = size;
10138
0
    htab->root.splt->size = (size
10139
0
           + htab->plt_mips_offset
10140
0
           + htab->plt_comp_offset);
10141
0
    htab->root.sgotplt->size = (htab->plt_got_index
10142
0
              * MIPS_ELF_GOT_SIZE (dynobj));
10143
10144
0
    mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10145
10146
0
    if (htab->root.hplt == NULL)
10147
0
      {
10148
0
        h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10149
0
                 "_PROCEDURE_LINKAGE_TABLE_");
10150
0
        htab->root.hplt = h;
10151
0
        if (h == NULL)
10152
0
    return false;
10153
0
      }
10154
10155
0
    h = htab->root.hplt;
10156
0
    h->root.u.def.value = isa_bit;
10157
0
    h->other = other;
10158
0
    h->type = STT_FUNC;
10159
0
  }
10160
0
    }
10161
10162
  /* Allocate space for global sym dynamic relocs.  */
10163
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10164
10165
0
  mips_elf_estimate_stub_size (info->output_bfd, info);
10166
10167
0
  if (!mips_elf_lay_out_got (info->output_bfd, info))
10168
0
    return false;
10169
10170
0
  mips_elf_lay_out_lazy_stubs (info);
10171
10172
  /* The check_relocs and adjust_dynamic_symbol entry points have
10173
     determined the sizes of the various dynamic sections.  Allocate
10174
     memory for them.  */
10175
0
  reltext = false;
10176
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10177
0
    {
10178
0
      const char *name;
10179
10180
      /* It's OK to base decisions on the section name, because none
10181
   of the dynobj section names depend upon the input files.  */
10182
0
      name = bfd_section_name (s);
10183
10184
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10185
0
  continue;
10186
10187
0
      if (startswith (name, ".rel"))
10188
0
  {
10189
0
    if (s->size != 0)
10190
0
      {
10191
0
        const char *outname;
10192
0
        asection *target;
10193
10194
        /* If this relocation section applies to a read only
10195
     section, then we probably need a DT_TEXTREL entry.
10196
     If the relocation section is .rel(a).dyn, we always
10197
     assert a DT_TEXTREL entry rather than testing whether
10198
     there exists a relocation to a read only section or
10199
     not.  */
10200
0
        outname = bfd_section_name (s->output_section);
10201
0
        target = bfd_get_section_by_name (info->output_bfd, outname + 4);
10202
0
        if ((target != NULL
10203
0
       && (target->flags & SEC_READONLY) != 0
10204
0
       && (target->flags & SEC_ALLOC) != 0)
10205
0
      || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10206
0
    reltext = true;
10207
10208
        /* We use the reloc_count field as a counter if we need
10209
     to copy relocs into the output file.  */
10210
0
        if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10211
0
    s->reloc_count = 0;
10212
10213
        /* If combreloc is enabled, elf_link_sort_relocs() will
10214
     sort relocations, but in a different way than we do,
10215
     and before we're done creating relocations.  Also, it
10216
     will move them around between input sections'
10217
     relocation's contents, so our sorting would be
10218
     broken, so don't let it run.  */
10219
0
        info->combreloc = 0;
10220
0
      }
10221
0
  }
10222
0
      else if (bfd_link_executable (info)
10223
0
         && !htab->use_rld_obj_head
10224
0
         && startswith (name, ".rld_map"))
10225
0
  {
10226
    /* We add a room for __rld_map.  It will be filled in by the
10227
       rtld to contain a pointer to the _r_debug structure.  */
10228
0
    s->size += MIPS_ELF_RLD_MAP_SIZE (info->output_bfd);
10229
0
  }
10230
0
      else if (SGI_COMPAT (info->output_bfd)
10231
0
         && startswith (name, ".compact_rel"))
10232
0
  s->size += htab->compact_rel_size;
10233
0
      else if (s == htab->root.splt)
10234
0
  {
10235
    /* If the last PLT entry has a branch delay slot, allocate
10236
       room for an extra nop to fill the delay slot.  This is
10237
       for CPUs without load interlocking.  */
10238
0
    if (! LOAD_INTERLOCKS_P (info->output_bfd)
10239
0
        && htab->root.target_os != is_vxworks
10240
0
        && s->size > 0)
10241
0
      s->size += 4;
10242
0
  }
10243
0
      else if (! startswith (name, ".init")
10244
0
         && s != htab->root.sgot
10245
0
         && s != htab->root.sgotplt
10246
0
         && s != htab->sstubs
10247
0
         && s != htab->root.sdynbss
10248
0
         && s != htab->root.sdynrelro)
10249
0
  {
10250
    /* It's not one of our sections, so don't allocate space.  */
10251
0
    continue;
10252
0
  }
10253
10254
0
      if (s->size == 0)
10255
0
  {
10256
0
    s->flags |= SEC_EXCLUDE;
10257
0
    continue;
10258
0
  }
10259
10260
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10261
0
  continue;
10262
10263
      /* Allocate memory for the section contents.  */
10264
0
      s->contents = bfd_zalloc (dynobj, s->size);
10265
0
      if (s->contents == NULL)
10266
0
  {
10267
0
    bfd_set_error (bfd_error_no_memory);
10268
0
    return false;
10269
0
  }
10270
0
      s->alloced = 1;
10271
0
    }
10272
10273
0
  if (htab->root.dynamic_sections_created)
10274
0
    {
10275
      /* Add some entries to the .dynamic section.  We fill in the
10276
   values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10277
   must add the entries now so that we get the correct size for
10278
   the .dynamic section.  */
10279
10280
      /* SGI object has the equivalence of DT_DEBUG in the
10281
   DT_MIPS_RLD_MAP entry.  This must come first because glibc
10282
   only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10283
   may only look at the first one they see.  */
10284
0
      if (!bfd_link_pic (info)
10285
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10286
0
  return false;
10287
10288
0
      if (bfd_link_executable (info)
10289
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10290
0
  return false;
10291
10292
      /* The DT_DEBUG entry may be filled in by the dynamic linker and
10293
   used by the debugger.  */
10294
0
      if (bfd_link_executable (info)
10295
0
    && !SGI_COMPAT (info->output_bfd)
10296
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10297
0
  return false;
10298
10299
0
      if (reltext
10300
0
    && (SGI_COMPAT (info->output_bfd)
10301
0
        || htab->root.target_os == is_vxworks))
10302
0
  info->flags |= DF_TEXTREL;
10303
10304
0
      if ((info->flags & DF_TEXTREL) != 0)
10305
0
  {
10306
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10307
0
      return false;
10308
10309
    /* Clear the DF_TEXTREL flag.  It will be set again if we
10310
       write out an actual text relocation; we may not, because
10311
       at this point we do not know whether e.g. any .eh_frame
10312
       absolute relocations have been converted to PC-relative.  */
10313
0
    info->flags &= ~DF_TEXTREL;
10314
0
  }
10315
10316
0
      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10317
0
  return false;
10318
10319
0
      sreldyn = mips_elf_rel_dyn_section (info, false);
10320
0
      if (htab->root.target_os == is_vxworks)
10321
0
  {
10322
    /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
10323
       use any of the DT_MIPS_* tags.  */
10324
0
    if (sreldyn && sreldyn->size > 0)
10325
0
      {
10326
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10327
0
    return false;
10328
10329
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10330
0
    return false;
10331
10332
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10333
0
    return false;
10334
0
      }
10335
0
  }
10336
0
      else
10337
0
  {
10338
0
    if (sreldyn && sreldyn->size > 0
10339
0
        && !bfd_is_abs_section (sreldyn->output_section))
10340
0
      {
10341
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10342
0
    return false;
10343
10344
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10345
0
    return false;
10346
10347
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10348
0
    return false;
10349
0
      }
10350
10351
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10352
0
      return false;
10353
10354
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10355
0
      return false;
10356
10357
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10358
0
      return false;
10359
10360
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10361
0
      return false;
10362
10363
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10364
0
      return false;
10365
10366
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10367
0
      return false;
10368
10369
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10370
0
      return false;
10371
10372
0
    if (info->emit_gnu_hash
10373
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10374
0
      return false;
10375
10376
0
    if (IRIX_COMPAT (dynobj) == ict_irix5
10377
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10378
0
      return false;
10379
10380
0
    if (IRIX_COMPAT (dynobj) == ict_irix6
10381
0
        && (bfd_get_section_by_name
10382
0
      (info->output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10383
0
        && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10384
0
      return false;
10385
0
  }
10386
0
      if (htab->root.splt->size > 0)
10387
0
  {
10388
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10389
0
      return false;
10390
10391
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10392
0
      return false;
10393
10394
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10395
0
      return false;
10396
10397
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10398
0
      return false;
10399
0
  }
10400
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
10401
0
      if (htab->root.target_os == is_vxworks
10402
0
    && !elf_vxworks_add_dynamic_entries (info))
10403
0
  return false;
10404
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
10405
0
    }
10406
10407
0
  return true;
10408
0
}
10409

10410
/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10411
   Adjust its R_ADDEND field so that it is correct for the output file.
10412
   LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10413
   and sections respectively; both use symbol indexes.  */
10414
10415
static void
10416
mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10417
      bfd *input_bfd, Elf_Internal_Sym *local_syms,
10418
      asection **local_sections, Elf_Internal_Rela *rel)
10419
0
{
10420
0
  unsigned int r_type, r_symndx;
10421
0
  Elf_Internal_Sym *sym;
10422
0
  asection *sec;
10423
10424
0
  if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10425
0
    {
10426
0
      r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10427
0
      if (gprel16_reloc_p (r_type)
10428
0
    || r_type == R_MIPS_GPREL32
10429
0
    || literal_reloc_p (r_type))
10430
0
  {
10431
0
    rel->r_addend += _bfd_get_gp_value (input_bfd);
10432
0
    rel->r_addend -= _bfd_get_gp_value (output_bfd);
10433
0
  }
10434
10435
0
      r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10436
0
      sym = local_syms + r_symndx;
10437
10438
      /* Adjust REL's addend to account for section merging.  */
10439
0
      if (!bfd_link_relocatable (info))
10440
0
  {
10441
0
    sec = local_sections[r_symndx];
10442
0
    _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10443
0
  }
10444
10445
      /* This would normally be done by the rela_normal code in elflink.c.  */
10446
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10447
0
  rel->r_addend += local_sections[r_symndx]->output_offset;
10448
0
    }
10449
0
}
10450
10451
/* Handle relocations against symbols from removed linkonce sections,
10452
   or sections discarded by a linker script.  We use this wrapper around
10453
   RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10454
   on 64-bit ELF targets.  In this case for any relocation handled, which
10455
   always be the first in a triplet, the remaining two have to be processed
10456
   together with the first, even if they are R_MIPS_NONE.  It is the symbol
10457
   index referred by the first reloc that applies to all the three and the
10458
   remaining two never refer to an object symbol.  And it is the final
10459
   relocation (the last non-null one) that determines the output field of
10460
   the whole relocation so retrieve the corresponding howto structure for
10461
   the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10462
10463
   Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10464
   and therefore requires to be pasted in a loop.  It also defines a block
10465
   and does not protect any of its arguments, hence the extra brackets.  */
10466
10467
static void
10468
mips_reloc_against_discarded_section (bfd *output_bfd,
10469
              struct bfd_link_info *info,
10470
              bfd *input_bfd, asection *input_section,
10471
              Elf_Internal_Rela **rel,
10472
              const Elf_Internal_Rela **relend,
10473
              bool rel_reloc,
10474
              reloc_howto_type *howto,
10475
              bfd_byte *contents)
10476
0
{
10477
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
10478
0
  int count = bed->s->int_rels_per_ext_rel;
10479
0
  unsigned int r_type;
10480
0
  int i;
10481
10482
0
  for (i = count - 1; i > 0; i--)
10483
0
    {
10484
0
      r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10485
0
      if (r_type != R_MIPS_NONE)
10486
0
  {
10487
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10488
0
    break;
10489
0
  }
10490
0
    }
10491
0
  do
10492
0
    {
10493
0
       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10494
0
          (*rel), count, (*relend), R_MIPS_NONE,
10495
0
          howto, i, contents);
10496
0
    }
10497
0
  while (0);
10498
0
}
10499
10500
/* Relocate a MIPS ELF section.  */
10501
10502
int
10503
_bfd_mips_elf_relocate_section (struct bfd_link_info *info,
10504
        bfd *input_bfd, asection *input_section,
10505
        bfd_byte *contents, Elf_Internal_Rela *relocs,
10506
        Elf_Internal_Sym *local_syms,
10507
        asection **local_sections)
10508
0
{
10509
0
  Elf_Internal_Rela *rel;
10510
0
  const Elf_Internal_Rela *relend;
10511
0
  bfd_vma addend = 0;
10512
0
  bool use_saved_addend_p = false;
10513
10514
0
  relend = relocs + input_section->reloc_count;
10515
0
  for (rel = relocs; rel < relend; ++rel)
10516
0
    {
10517
0
      const char *name;
10518
0
      bfd_vma value = 0;
10519
0
      reloc_howto_type *howto;
10520
0
      bool cross_mode_jump_p = false;
10521
      /* TRUE if the relocation is a RELA relocation, rather than a
10522
   REL relocation.  */
10523
0
      bool rela_relocation_p = true;
10524
0
      unsigned int r_type = ELF_R_TYPE (info->output_bfd, rel->r_info);
10525
0
      const char *msg;
10526
0
      unsigned long r_symndx;
10527
0
      asection *sec;
10528
0
      Elf_Internal_Shdr *symtab_hdr;
10529
0
      struct elf_link_hash_entry *h;
10530
0
      bool rel_reloc;
10531
10532
0
      rel_reloc = mips_elf_rel_relocation_p (input_bfd, input_section,
10533
0
               relocs, rel);
10534
      /* Find the relocation howto for this relocation.  */
10535
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10536
10537
0
      r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10538
0
      symtab_hdr = &elf_symtab_hdr (input_bfd);
10539
0
      if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10540
0
  {
10541
0
    sec = local_sections[r_symndx];
10542
0
    h = NULL;
10543
0
  }
10544
0
      else
10545
0
  {
10546
0
    unsigned long extsymoff;
10547
10548
0
    extsymoff = 0;
10549
0
    if (!elf_bad_symtab (input_bfd))
10550
0
      extsymoff = symtab_hdr->sh_info;
10551
0
    h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10552
0
    while (h->root.type == bfd_link_hash_indirect
10553
0
     || h->root.type == bfd_link_hash_warning)
10554
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10555
10556
0
    sec = NULL;
10557
0
    if (h->root.type == bfd_link_hash_defined
10558
0
        || h->root.type == bfd_link_hash_defweak)
10559
0
      sec = h->root.u.def.section;
10560
0
  }
10561
10562
0
      if (sec != NULL && discarded_section (sec))
10563
0
  {
10564
0
    mips_reloc_against_discarded_section (info->output_bfd, info,
10565
0
            input_bfd,
10566
0
            input_section, &rel, &relend,
10567
0
            rel_reloc, howto, contents);
10568
0
    continue;
10569
0
  }
10570
10571
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10572
0
  {
10573
    /* Some 32-bit code uses R_MIPS_64.  In particular, people use
10574
       64-bit code, but make sure all their addresses are in the
10575
       lowermost or uppermost 32-bit section of the 64-bit address
10576
       space.  Thus, when they use an R_MIPS_64 they mean what is
10577
       usually meant by R_MIPS_32, with the exception that the
10578
       stored value is sign-extended to 64 bits.  */
10579
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10580
10581
    /* On big-endian systems, we need to lie about the position
10582
       of the reloc.  */
10583
0
    if (bfd_big_endian (input_bfd))
10584
0
      rel->r_offset += 4;
10585
0
  }
10586
10587
0
      if (!use_saved_addend_p)
10588
0
  {
10589
    /* If these relocations were originally of the REL variety,
10590
       we must pull the addend out of the field that will be
10591
       relocated.  Otherwise, we simply use the contents of the
10592
       RELA relocation.  */
10593
0
    if (mips_elf_rel_relocation_p (input_bfd, input_section,
10594
0
           relocs, rel))
10595
0
      {
10596
0
        rela_relocation_p = false;
10597
0
        addend = mips_elf_read_rel_addend (input_bfd, input_section,
10598
0
             rel, howto, contents);
10599
0
        if (hi16_reloc_p (r_type)
10600
0
      || (got16_reloc_p (r_type)
10601
0
          && mips_elf_local_relocation_p (input_bfd, rel,
10602
0
                  local_sections))
10603
0
      || tls_hi16_reloc_p (r_type))
10604
0
    {
10605
0
      if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10606
0
                 rel, relend,
10607
0
                 contents, &addend))
10608
0
        {
10609
0
          if (h)
10610
0
      name = h->root.root.string;
10611
0
          else
10612
0
      name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10613
0
             local_syms + r_symndx,
10614
0
             sec);
10615
          /* xgettext:c-format */
10616
0
          msg = bfd_asprintf (_("can't find matching LO16 reloc"
10617
0
              " against `%s' for %s at %#" PRIx64
10618
0
              " in section `%s'"),
10619
0
            name, howto->name,
10620
0
            (uint64_t) rel->r_offset,
10621
0
            input_section->name);
10622
0
          if (msg == NULL)
10623
0
      return false;
10624
0
          info->callbacks->warning
10625
0
      (info, msg, name, input_bfd, input_section,
10626
0
       rel->r_offset);
10627
0
        }
10628
0
    }
10629
0
        else
10630
0
    addend <<= howto->rightshift;
10631
0
      }
10632
0
    else
10633
0
      addend = rel->r_addend;
10634
0
    mips_elf_adjust_addend (info->output_bfd, info, input_bfd,
10635
0
          local_syms, local_sections, rel);
10636
0
  }
10637
10638
0
      if (bfd_link_relocatable (info))
10639
0
  {
10640
0
    if (r_type == R_MIPS_64 && ! NEWABI_P (info->output_bfd)
10641
0
        && bfd_big_endian (input_bfd))
10642
0
      rel->r_offset -= 4;
10643
10644
0
    if (!rela_relocation_p && rel->r_addend)
10645
0
      {
10646
0
        addend += rel->r_addend;
10647
0
        if (hi16_reloc_p (r_type)
10648
0
      || got16_reloc_p (r_type)
10649
0
      || tls_hi16_reloc_p (r_type))
10650
0
    addend = mips_elf_high (addend);
10651
0
        else if (r_type == R_MIPS_HIGHER)
10652
0
    addend = mips_elf_higher (addend);
10653
0
        else if (r_type == R_MIPS_HIGHEST)
10654
0
    addend = mips_elf_highest (addend);
10655
0
        else
10656
0
    addend >>= howto->rightshift;
10657
10658
        /* We use the source mask, rather than the destination
10659
     mask because the place to which we are writing will be
10660
     source of the addend in the final link.  */
10661
0
        addend &= howto->src_mask;
10662
10663
0
        if (r_type == R_MIPS_64 && ! NEWABI_P (info->output_bfd))
10664
    /* See the comment above about using R_MIPS_64 in the 32-bit
10665
       ABI.  Here, we need to update the addend.  It would be
10666
       possible to get away with just using the R_MIPS_32 reloc
10667
       but for endianness.  */
10668
0
    {
10669
0
      bfd_vma sign_bits;
10670
0
      bfd_vma low_bits;
10671
0
      bfd_vma high_bits;
10672
10673
0
      if (addend & ((bfd_vma) 1 << 31))
10674
0
#ifdef BFD64
10675
0
        sign_bits = ((bfd_vma) 1 << 32) - 1;
10676
#else
10677
        sign_bits = -1;
10678
#endif
10679
0
      else
10680
0
        sign_bits = 0;
10681
10682
      /* If we don't know that we have a 64-bit type,
10683
         do two separate stores.  */
10684
0
      if (bfd_big_endian (input_bfd))
10685
0
        {
10686
          /* Store the sign-bits (which are most significant)
10687
       first.  */
10688
0
          low_bits = sign_bits;
10689
0
          high_bits = addend;
10690
0
        }
10691
0
      else
10692
0
        {
10693
0
          low_bits = addend;
10694
0
          high_bits = sign_bits;
10695
0
        }
10696
0
      bfd_put_32 (input_bfd, low_bits,
10697
0
            contents + rel->r_offset);
10698
0
      bfd_put_32 (input_bfd, high_bits,
10699
0
            contents + rel->r_offset + 4);
10700
0
      continue;
10701
0
    }
10702
10703
0
        if (! mips_elf_perform_relocation (info, howto, rel, addend,
10704
0
             input_bfd, input_section,
10705
0
             contents, false))
10706
0
    return false;
10707
0
      }
10708
10709
    /* Go on to the next relocation.  */
10710
0
    continue;
10711
0
  }
10712
10713
      /* In the N32 and 64-bit ABIs there may be multiple consecutive
10714
   relocations for the same offset.  In that case we are
10715
   supposed to treat the output of each relocation as the addend
10716
   for the next.  */
10717
0
      if (rel + 1 < relend
10718
0
    && rel->r_offset == rel[1].r_offset
10719
0
    && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10720
0
  use_saved_addend_p = true;
10721
0
      else
10722
0
  use_saved_addend_p = false;
10723
10724
      /* Figure out what value we are supposed to relocate.  */
10725
0
      switch (mips_elf_calculate_relocation (info->output_bfd, input_bfd,
10726
0
               input_section, contents,
10727
0
               info, rel, addend, howto,
10728
0
               local_syms, local_sections,
10729
0
               &value, &name, &cross_mode_jump_p,
10730
0
               use_saved_addend_p))
10731
0
  {
10732
0
  case bfd_reloc_continue:
10733
    /* There's nothing to do.  */
10734
0
    continue;
10735
10736
0
  case bfd_reloc_undefined:
10737
    /* mips_elf_calculate_relocation already called the
10738
       undefined_symbol callback.  There's no real point in
10739
       trying to perform the relocation at this point, so we
10740
       just skip ahead to the next relocation.  */
10741
0
    continue;
10742
10743
0
  case bfd_reloc_notsupported:
10744
0
    msg = _("internal error: unsupported relocation error");
10745
0
    info->callbacks->warning
10746
0
      (info, msg, name, input_bfd, input_section, rel->r_offset);
10747
0
    return false;
10748
10749
0
  case bfd_reloc_overflow:
10750
0
    if (use_saved_addend_p)
10751
      /* Ignore overflow until we reach the last relocation for
10752
         a given location.  */
10753
0
      ;
10754
0
    else
10755
0
      {
10756
0
        struct mips_elf_link_hash_table *htab;
10757
10758
0
        htab = mips_elf_hash_table (info);
10759
0
        BFD_ASSERT (htab != NULL);
10760
0
        BFD_ASSERT (name != NULL);
10761
0
        if (!htab->small_data_overflow_reported
10762
0
      && (gprel16_reloc_p (howto->type)
10763
0
          || literal_reloc_p (howto->type)))
10764
0
    {
10765
0
      msg = _("small-data section too large;"
10766
0
        " lower small-data size limit (see option -G)");
10767
10768
0
      htab->small_data_overflow_reported = true;
10769
0
      (*info->callbacks->einfo) ("%P: %s\n", msg);
10770
0
    }
10771
0
        (*info->callbacks->reloc_overflow)
10772
0
    (info, NULL, name, howto->name, (bfd_vma) 0,
10773
0
     input_bfd, input_section, rel->r_offset);
10774
0
      }
10775
0
    break;
10776
10777
0
  case bfd_reloc_ok:
10778
0
    break;
10779
10780
0
  case bfd_reloc_outofrange:
10781
0
    msg = NULL;
10782
0
    if (jal_reloc_p (howto->type))
10783
0
      msg = (cross_mode_jump_p
10784
0
       ? _("cannot convert a jump to JALX "
10785
0
           "for a non-word-aligned address")
10786
0
       : (howto->type == R_MIPS16_26
10787
0
          ? _("jump to a non-word-aligned address")
10788
0
          : _("jump to a non-instruction-aligned address")));
10789
0
    else if (b_reloc_p (howto->type))
10790
0
      msg = (cross_mode_jump_p
10791
0
       ? _("cannot convert a branch to JALX "
10792
0
           "for a non-word-aligned address")
10793
0
       : _("branch to a non-instruction-aligned address"));
10794
0
    else if (aligned_pcrel_reloc_p (howto->type))
10795
0
      msg = _("PC-relative load from unaligned address");
10796
0
    if (msg)
10797
0
      {
10798
0
        info->callbacks->einfo
10799
0
    ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10800
0
        break;
10801
0
      }
10802
    /* Fall through.  */
10803
10804
0
  default:
10805
0
    abort ();
10806
0
    break;
10807
0
  }
10808
10809
      /* If we've got another relocation for the address, keep going
10810
   until we reach the last one.  */
10811
0
      if (use_saved_addend_p)
10812
0
  {
10813
0
    addend = value;
10814
0
    continue;
10815
0
  }
10816
10817
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (info->output_bfd))
10818
  /* See the comment above about using R_MIPS_64 in the 32-bit
10819
     ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
10820
     that calculated the right value.  Now, however, we
10821
     sign-extend the 32-bit result to 64-bits, and store it as a
10822
     64-bit value.  We are especially generous here in that we
10823
     go to extreme lengths to support this usage on systems with
10824
     only a 32-bit VMA.  */
10825
0
  {
10826
0
    bfd_vma sign_bits;
10827
0
    bfd_vma low_bits;
10828
0
    bfd_vma high_bits;
10829
10830
0
    if (value & ((bfd_vma) 1 << 31))
10831
0
#ifdef BFD64
10832
0
      sign_bits = ((bfd_vma) 1 << 32) - 1;
10833
#else
10834
      sign_bits = -1;
10835
#endif
10836
0
    else
10837
0
      sign_bits = 0;
10838
10839
    /* If we don't know that we have a 64-bit type,
10840
       do two separate stores.  */
10841
0
    if (bfd_big_endian (input_bfd))
10842
0
      {
10843
        /* Undo what we did above.  */
10844
0
        rel->r_offset -= 4;
10845
        /* Store the sign-bits (which are most significant)
10846
     first.  */
10847
0
        low_bits = sign_bits;
10848
0
        high_bits = value;
10849
0
      }
10850
0
    else
10851
0
      {
10852
0
        low_bits = value;
10853
0
        high_bits = sign_bits;
10854
0
      }
10855
0
    bfd_put_32 (input_bfd, low_bits,
10856
0
          contents + rel->r_offset);
10857
0
    bfd_put_32 (input_bfd, high_bits,
10858
0
          contents + rel->r_offset + 4);
10859
0
    continue;
10860
0
  }
10861
10862
      /* Actually perform the relocation.  */
10863
0
      if (! mips_elf_perform_relocation (info, howto, rel, value,
10864
0
           input_bfd, input_section,
10865
0
           contents, cross_mode_jump_p))
10866
0
  return false;
10867
0
    }
10868
10869
0
  return true;
10870
0
}
10871

10872
/* A function that iterates over each entry in la25_stubs and fills
10873
   in the code for each one.  DATA points to a mips_htab_traverse_info.  */
10874
10875
static int
10876
mips_elf_create_la25_stub (void **slot, void *data)
10877
0
{
10878
0
  struct mips_htab_traverse_info *hti;
10879
0
  struct mips_elf_link_hash_table *htab;
10880
0
  struct mips_elf_la25_stub *stub;
10881
0
  asection *s;
10882
0
  bfd_byte *loc;
10883
0
  bfd_vma offset, target, target_high, target_low;
10884
0
  bfd_vma branch_pc;
10885
0
  bfd_signed_vma pcrel_offset = 0;
10886
10887
0
  stub = (struct mips_elf_la25_stub *) *slot;
10888
0
  hti = (struct mips_htab_traverse_info *) data;
10889
0
  htab = mips_elf_hash_table (hti->info);
10890
0
  BFD_ASSERT (htab != NULL);
10891
10892
  /* Create the section contents, if we haven't already.  */
10893
0
  s = stub->stub_section;
10894
0
  loc = s->contents;
10895
0
  if (loc == NULL)
10896
0
    {
10897
0
      loc = bfd_malloc (s->size);
10898
0
      if (loc == NULL)
10899
0
  {
10900
0
    hti->error = true;
10901
0
    return false;
10902
0
  }
10903
0
      s->contents = loc;
10904
0
    }
10905
10906
  /* Work out where in the section this stub should go.  */
10907
0
  offset = stub->offset;
10908
10909
  /* We add 8 here to account for the LUI/ADDIU instructions
10910
     before the branch instruction.  This cannot be moved down to
10911
     where pcrel_offset is calculated as 's' is updated in
10912
     mips_elf_get_la25_target.  */
10913
0
  branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10914
10915
  /* Work out the target address.  */
10916
0
  target = mips_elf_get_la25_target (stub, &s);
10917
0
  target += s->output_section->vma + s->output_offset;
10918
10919
0
  target_high = ((target + 0x8000) >> 16) & 0xffff;
10920
0
  target_low = (target & 0xffff);
10921
10922
  /* Calculate the PC of the compact branch instruction (for the case where
10923
     compact branches are used for either microMIPSR6 or MIPSR6 with
10924
     compact branches.  Add 4-bytes to account for BC using the PC of the
10925
     next instruction as the base.  */
10926
0
  pcrel_offset = target - (branch_pc + 4);
10927
10928
0
  if (stub->stub_section != htab->strampoline)
10929
0
    {
10930
      /* This is a simple LUI/ADDIU stub.  Zero out the beginning
10931
   of the section and write the two instructions at the end.  */
10932
0
      memset (loc, 0, offset);
10933
0
      loc += offset;
10934
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10935
0
  {
10936
0
    bfd_put_micromips_32 (hti->output_bfd,
10937
0
        LA25_LUI_MICROMIPS (target_high),
10938
0
        loc);
10939
0
    bfd_put_micromips_32 (hti->output_bfd,
10940
0
        LA25_ADDIU_MICROMIPS (target_low),
10941
0
        loc + 4);
10942
0
  }
10943
0
      else
10944
0
  {
10945
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10946
0
    bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10947
0
  }
10948
0
    }
10949
0
  else
10950
0
    {
10951
      /* This is trampoline.  */
10952
0
      loc += offset;
10953
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10954
0
  {
10955
0
    bfd_put_micromips_32 (hti->output_bfd,
10956
0
        LA25_LUI_MICROMIPS (target_high), loc);
10957
0
    bfd_put_micromips_32 (hti->output_bfd,
10958
0
        LA25_J_MICROMIPS (target), loc + 4);
10959
0
    bfd_put_micromips_32 (hti->output_bfd,
10960
0
        LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10961
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10962
0
  }
10963
0
      else
10964
0
  {
10965
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10966
0
    if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10967
0
      {
10968
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10969
0
        bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10970
0
      }
10971
0
    else
10972
0
      {
10973
0
        bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10974
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10975
0
      }
10976
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10977
0
  }
10978
0
    }
10979
0
  return true;
10980
0
}
10981
10982
/* If NAME is one of the special IRIX6 symbols defined by the linker,
10983
   adjust it appropriately now.  */
10984
10985
static void
10986
mips_elf_irix6_finish_dynamic_symbol (const char *name, Elf_Internal_Sym *sym)
10987
0
{
10988
  /* The linker script takes care of providing names and values for
10989
     these, but we must place them into the right sections.  */
10990
0
  static const char* const text_section_symbols[] = {
10991
0
    "_ftext",
10992
0
    "_etext",
10993
0
    "__dso_displacement",
10994
0
    "__elf_header",
10995
0
    "__program_header_table",
10996
0
    NULL
10997
0
  };
10998
10999
0
  static const char* const data_section_symbols[] = {
11000
0
    "_fdata",
11001
0
    "_edata",
11002
0
    "_end",
11003
0
    "_fbss",
11004
0
    NULL
11005
0
  };
11006
11007
0
  const char* const *p;
11008
0
  int i;
11009
11010
0
  for (i = 0; i < 2; ++i)
11011
0
    for (p = (i == 0) ? text_section_symbols : data_section_symbols;
11012
0
   *p;
11013
0
   ++p)
11014
0
      if (strcmp (*p, name) == 0)
11015
0
  {
11016
    /* All of these symbols are given type STT_SECTION by the
11017
       IRIX6 linker.  */
11018
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11019
0
    sym->st_other = STO_PROTECTED;
11020
11021
    /* The IRIX linker puts these symbols in special sections.  */
11022
0
    if (i == 0)
11023
0
      sym->st_shndx = SHN_MIPS_TEXT;
11024
0
    else
11025
0
      sym->st_shndx = SHN_MIPS_DATA;
11026
11027
0
    break;
11028
0
  }
11029
0
}
11030
11031
/* Finish up dynamic symbol handling.  We set the contents of various
11032
   dynamic sections here.  */
11033
11034
bool
11035
_bfd_mips_elf_finish_dynamic_symbol (struct bfd_link_info *info,
11036
             struct elf_link_hash_entry *h,
11037
             Elf_Internal_Sym *sym)
11038
0
{
11039
0
  bfd *dynobj;
11040
0
  asection *sgot;
11041
0
  struct mips_got_info *g, *gg;
11042
0
  const char *name;
11043
0
  int idx;
11044
0
  struct mips_elf_link_hash_table *htab;
11045
0
  struct mips_elf_link_hash_entry *hmips;
11046
11047
0
  htab = mips_elf_hash_table (info);
11048
0
  BFD_ASSERT (htab != NULL);
11049
0
  dynobj = elf_hash_table (info)->dynobj;
11050
0
  hmips = (struct mips_elf_link_hash_entry *) h;
11051
11052
0
  BFD_ASSERT (htab->root.target_os != is_vxworks);
11053
11054
0
  if (h->plt.plist != NULL
11055
0
      && (h->plt.plist->mips_offset != MINUS_ONE
11056
0
    || h->plt.plist->comp_offset != MINUS_ONE))
11057
0
    {
11058
      /* We've decided to create a PLT entry for this symbol.  */
11059
0
      bfd_byte *loc;
11060
0
      bfd_vma header_address, got_address;
11061
0
      bfd_vma got_address_high, got_address_low, load;
11062
0
      bfd_vma got_index;
11063
0
      bfd_vma isa_bit;
11064
11065
0
      got_index = h->plt.plist->gotplt_index;
11066
11067
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
11068
0
      BFD_ASSERT (h->dynindx != -1);
11069
0
      BFD_ASSERT (htab->root.splt != NULL);
11070
0
      BFD_ASSERT (got_index != MINUS_ONE);
11071
0
      BFD_ASSERT (!h->def_regular);
11072
11073
      /* Calculate the address of the PLT header.  */
11074
0
      isa_bit = htab->plt_header_is_comp;
11075
0
      header_address = (htab->root.splt->output_section->vma
11076
0
      + htab->root.splt->output_offset + isa_bit);
11077
11078
      /* Calculate the address of the .got.plt entry.  */
11079
0
      got_address = (htab->root.sgotplt->output_section->vma
11080
0
         + htab->root.sgotplt->output_offset
11081
0
         + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11082
11083
0
      got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11084
0
      got_address_low = got_address & 0xffff;
11085
11086
      /* The PLT sequence is not safe for N64 if .got.plt entry's address
11087
   cannot be loaded in two instructions.  */
11088
0
      if (ABI_64_P (info->output_bfd)
11089
0
    && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11090
0
  {
11091
0
    _bfd_error_handler
11092
      /* xgettext:c-format */
11093
0
      (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11094
0
         "supported; consider using `-Ttext-segment=...'"),
11095
0
       info->output_bfd,
11096
0
       htab->root.sgotplt->output_section,
11097
0
       (int64_t) got_address);
11098
0
    bfd_set_error (bfd_error_no_error);
11099
0
    return false;
11100
0
  }
11101
11102
      /* Initially point the .got.plt entry at the PLT header.  */
11103
0
      loc = (htab->root.sgotplt->contents
11104
0
       + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11105
0
      if (ABI_64_P (info->output_bfd))
11106
0
  bfd_put_64 (info->output_bfd, header_address, loc);
11107
0
      else
11108
0
  bfd_put_32 (info->output_bfd, header_address, loc);
11109
11110
      /* Now handle the PLT itself.  First the standard entry (the order
11111
   does not matter, we just have to pick one).  */
11112
0
      if (h->plt.plist->mips_offset != MINUS_ONE)
11113
0
  {
11114
0
    const bfd_vma *plt_entry;
11115
0
    bfd_vma plt_offset;
11116
11117
0
    plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11118
11119
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11120
11121
    /* Find out where the .plt entry should go.  */
11122
0
    loc = htab->root.splt->contents + plt_offset;
11123
11124
    /* Pick the load opcode.  */
11125
0
    load = MIPS_ELF_LOAD_WORD (info->output_bfd);
11126
11127
    /* Fill in the PLT entry itself.  */
11128
11129
0
    if (MIPSR6_P (info->output_bfd))
11130
0
      plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11131
0
                 : mipsr6_exec_plt_entry;
11132
0
    else
11133
0
      plt_entry = mips_exec_plt_entry;
11134
0
    bfd_put_32 (info->output_bfd, plt_entry[0] | got_address_high, loc);
11135
0
    bfd_put_32 (info->output_bfd, plt_entry[1] | got_address_low | load,
11136
0
          loc + 4);
11137
11138
0
    if (! LOAD_INTERLOCKS_P (info->output_bfd)
11139
0
        || (MIPSR6_P (info->output_bfd) && htab->compact_branches))
11140
0
      {
11141
0
        bfd_put_32 (info->output_bfd, plt_entry[2] | got_address_low,
11142
0
        loc + 8);
11143
0
        bfd_put_32 (info->output_bfd, plt_entry[3], loc + 12);
11144
0
      }
11145
0
    else
11146
0
      {
11147
0
        bfd_put_32 (info->output_bfd, plt_entry[3], loc + 8);
11148
0
        bfd_put_32 (info->output_bfd, plt_entry[2] | got_address_low,
11149
0
        loc + 12);
11150
0
      }
11151
0
  }
11152
11153
      /* Now the compressed entry.  They come after any standard ones.  */
11154
0
      if (h->plt.plist->comp_offset != MINUS_ONE)
11155
0
  {
11156
0
    bfd_vma plt_offset;
11157
11158
0
    plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11159
0
      + h->plt.plist->comp_offset);
11160
11161
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11162
11163
    /* Find out where the .plt entry should go.  */
11164
0
    loc = htab->root.splt->contents + plt_offset;
11165
11166
    /* Fill in the PLT entry itself.  */
11167
0
    if (!MICROMIPS_P (info->output_bfd))
11168
0
      {
11169
0
        const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11170
11171
0
        bfd_put_16 (info->output_bfd, plt_entry[0], loc);
11172
0
        bfd_put_16 (info->output_bfd, plt_entry[1], loc + 2);
11173
0
        bfd_put_16 (info->output_bfd, plt_entry[2], loc + 4);
11174
0
        bfd_put_16 (info->output_bfd, plt_entry[3], loc + 6);
11175
0
        bfd_put_16 (info->output_bfd, plt_entry[4], loc + 8);
11176
0
        bfd_put_16 (info->output_bfd, plt_entry[5], loc + 10);
11177
0
        bfd_put_32 (info->output_bfd, got_address, loc + 12);
11178
0
      }
11179
0
    else if (htab->insn32)
11180
0
      {
11181
0
        const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11182
11183
0
        bfd_put_16 (info->output_bfd, plt_entry[0], loc);
11184
0
        bfd_put_16 (info->output_bfd, got_address_high, loc + 2);
11185
0
        bfd_put_16 (info->output_bfd, plt_entry[2], loc + 4);
11186
0
        bfd_put_16 (info->output_bfd, got_address_low, loc + 6);
11187
0
        bfd_put_16 (info->output_bfd, plt_entry[4], loc + 8);
11188
0
        bfd_put_16 (info->output_bfd, plt_entry[5], loc + 10);
11189
0
        bfd_put_16 (info->output_bfd, plt_entry[6], loc + 12);
11190
0
        bfd_put_16 (info->output_bfd, got_address_low, loc + 14);
11191
0
      }
11192
0
    else
11193
0
      {
11194
0
        const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11195
0
        bfd_signed_vma gotpc_offset;
11196
0
        bfd_vma loc_address;
11197
11198
0
        BFD_ASSERT (got_address % 4 == 0);
11199
11200
0
        loc_address = (htab->root.splt->output_section->vma
11201
0
           + htab->root.splt->output_offset + plt_offset);
11202
0
        gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11203
11204
        /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11205
0
        if (gotpc_offset + 0x1000000 >= 0x2000000)
11206
0
    {
11207
0
      _bfd_error_handler
11208
        /* xgettext:c-format */
11209
0
        (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11210
0
           "beyond the range of ADDIUPC"),
11211
0
         info->output_bfd,
11212
0
         htab->root.sgotplt->output_section,
11213
0
         (int64_t) gotpc_offset,
11214
0
         htab->root.splt->output_section);
11215
0
      bfd_set_error (bfd_error_no_error);
11216
0
      return false;
11217
0
    }
11218
0
        bfd_put_16 (info->output_bfd,
11219
0
        plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11220
0
        bfd_put_16 (info->output_bfd, (gotpc_offset >> 2) & 0xffff,
11221
0
        loc + 2);
11222
0
        bfd_put_16 (info->output_bfd, plt_entry[2], loc + 4);
11223
0
        bfd_put_16 (info->output_bfd, plt_entry[3], loc + 6);
11224
0
        bfd_put_16 (info->output_bfd, plt_entry[4], loc + 8);
11225
0
        bfd_put_16 (info->output_bfd, plt_entry[5], loc + 10);
11226
0
      }
11227
0
  }
11228
11229
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11230
0
      mips_elf_output_dynamic_relocation (info->output_bfd, htab->root.srelplt,
11231
0
            got_index - 2, h->dynindx,
11232
0
            R_MIPS_JUMP_SLOT, got_address);
11233
11234
      /* We distinguish between PLT entries and lazy-binding stubs by
11235
   giving the former an st_other value of STO_MIPS_PLT.  Set the
11236
   flag and leave the value if there are any relocations in the
11237
   binary where pointer equality matters.  */
11238
0
      sym->st_shndx = SHN_UNDEF;
11239
0
      if (h->pointer_equality_needed)
11240
0
  sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11241
0
      else
11242
0
  {
11243
0
    sym->st_value = 0;
11244
0
    sym->st_other = 0;
11245
0
  }
11246
0
    }
11247
11248
0
  if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11249
0
    {
11250
      /* We've decided to create a lazy-binding stub.  */
11251
0
      bool micromips_p = MICROMIPS_P (info->output_bfd);
11252
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11253
0
      bfd_vma stub_size = htab->function_stub_size;
11254
0
      bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11255
0
      bfd_vma isa_bit = micromips_p;
11256
0
      bfd_vma stub_big_size;
11257
11258
0
      if (!micromips_p)
11259
0
  stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11260
0
      else if (htab->insn32)
11261
0
  stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11262
0
      else
11263
0
  stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11264
11265
      /* This symbol has a stub.  Set it up.  */
11266
11267
0
      BFD_ASSERT (h->dynindx != -1);
11268
11269
0
      BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11270
11271
      /* Values up to 2^31 - 1 are allowed.  Larger values would cause
11272
   sign extension at runtime in the stub, resulting in a negative
11273
   index value.  */
11274
0
      if (h->dynindx & ~0x7fffffff)
11275
0
  {
11276
0
    _bfd_error_handler
11277
0
      (_("%pB: cannot handle more than %d dynamic symbols"),
11278
0
       info->output_bfd, 0x7fffffff);
11279
0
    bfd_set_error (bfd_error_bad_value);
11280
0
    return false;
11281
0
  }
11282
11283
      /* Fill the stub.  */
11284
0
      if (micromips_p)
11285
0
  {
11286
0
    idx = 0;
11287
0
    bfd_put_micromips_32 (info->output_bfd,
11288
0
        STUB_LW_MICROMIPS (info->output_bfd),
11289
0
        stub + idx);
11290
0
    idx += 4;
11291
0
    if (htab->insn32)
11292
0
      {
11293
0
        bfd_put_micromips_32 (info->output_bfd,
11294
0
            STUB_MOVE32_MICROMIPS, stub + idx);
11295
0
        idx += 4;
11296
0
      }
11297
0
    else
11298
0
      {
11299
0
        bfd_put_16 (info->output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11300
0
        idx += 2;
11301
0
      }
11302
0
    if (stub_size == stub_big_size)
11303
0
      {
11304
0
        long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11305
11306
0
        bfd_put_micromips_32 (info->output_bfd,
11307
0
            STUB_LUI_MICROMIPS (dynindx_hi),
11308
0
            stub + idx);
11309
0
        idx += 4;
11310
0
      }
11311
0
    if (htab->insn32)
11312
0
      {
11313
0
        bfd_put_micromips_32 (info->output_bfd, STUB_JALR32_MICROMIPS,
11314
0
            stub + idx);
11315
0
        idx += 4;
11316
0
      }
11317
0
    else
11318
0
      {
11319
0
        bfd_put_16 (info->output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11320
0
        idx += 2;
11321
0
      }
11322
11323
    /* If a large stub is not required and sign extension is not a
11324
       problem, then use legacy code in the stub.  */
11325
0
    if (stub_size == stub_big_size)
11326
0
      bfd_put_micromips_32 (info->output_bfd,
11327
0
          STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11328
0
          stub + idx);
11329
0
    else if (h->dynindx & ~0x7fff)
11330
0
      bfd_put_micromips_32 (info->output_bfd,
11331
0
          STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11332
0
          stub + idx);
11333
0
    else
11334
0
      bfd_put_micromips_32 (info->output_bfd,
11335
0
          STUB_LI16S_MICROMIPS (info->output_bfd,
11336
0
              h->dynindx),
11337
0
          stub + idx);
11338
0
  }
11339
0
      else
11340
0
  {
11341
0
    idx = 0;
11342
0
    bfd_put_32 (info->output_bfd, STUB_LW (info->output_bfd), stub + idx);
11343
0
    idx += 4;
11344
0
    bfd_put_32 (info->output_bfd, STUB_MOVE, stub + idx);
11345
0
    idx += 4;
11346
0
    if (stub_size == stub_big_size)
11347
0
      {
11348
0
        bfd_put_32 (info->output_bfd,
11349
0
        STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11350
0
        stub + idx);
11351
0
        idx += 4;
11352
0
      }
11353
11354
0
    if (!(MIPSR6_P (info->output_bfd) && htab->compact_branches))
11355
0
      {
11356
0
        bfd_put_32 (info->output_bfd, STUB_JALR, stub + idx);
11357
0
        idx += 4;
11358
0
      }
11359
11360
    /* If a large stub is not required and sign extension is not a
11361
       problem, then use legacy code in the stub.  */
11362
0
    if (stub_size == stub_big_size)
11363
0
      bfd_put_32 (info->output_bfd, STUB_ORI (h->dynindx & 0xffff),
11364
0
      stub + idx);
11365
0
    else if (h->dynindx & ~0x7fff)
11366
0
      bfd_put_32 (info->output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11367
0
      stub + idx);
11368
0
    else
11369
0
      bfd_put_32 (info->output_bfd,
11370
0
      STUB_LI16S (info->output_bfd, h->dynindx),
11371
0
      stub + idx);
11372
0
    idx += 4;
11373
11374
0
    if (MIPSR6_P (info->output_bfd) && htab->compact_branches)
11375
0
      bfd_put_32 (info->output_bfd, STUB_JALRC, stub + idx);
11376
0
  }
11377
11378
0
      BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11379
0
      memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11380
0
        stub, stub_size);
11381
11382
      /* Mark the symbol as undefined.  stub_offset != -1 occurs
11383
   only for the referenced symbol.  */
11384
0
      sym->st_shndx = SHN_UNDEF;
11385
11386
      /* The run-time linker uses the st_value field of the symbol
11387
   to reset the global offset table entry for this external
11388
   to its stub address when unlinking a shared object.  */
11389
0
      sym->st_value = (htab->sstubs->output_section->vma
11390
0
           + htab->sstubs->output_offset
11391
0
           + h->plt.plist->stub_offset
11392
0
           + isa_bit);
11393
0
      sym->st_other = other;
11394
0
    }
11395
11396
  /* If we have a MIPS16 function with a stub, the dynamic symbol must
11397
     refer to the stub, since only the stub uses the standard calling
11398
     conventions.  */
11399
0
  if (h->dynindx != -1 && hmips->fn_stub != NULL)
11400
0
    {
11401
0
      BFD_ASSERT (hmips->need_fn_stub);
11402
0
      sym->st_value = (hmips->fn_stub->output_section->vma
11403
0
           + hmips->fn_stub->output_offset);
11404
0
      sym->st_size = hmips->fn_stub->size;
11405
0
      sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11406
0
    }
11407
11408
0
  BFD_ASSERT (h->dynindx != -1
11409
0
        || h->forced_local);
11410
11411
0
  sgot = htab->root.sgot;
11412
0
  g = htab->got_info;
11413
0
  BFD_ASSERT (g != NULL);
11414
11415
  /* Run through the global symbol table, creating GOT entries for all
11416
     the symbols that need them.  */
11417
0
  if (hmips->global_got_area != GGA_NONE)
11418
0
    {
11419
0
      bfd_vma offset;
11420
0
      bfd_vma value;
11421
11422
0
      value = sym->st_value;
11423
0
      offset = mips_elf_primary_global_got_index (info->output_bfd, info, h);
11424
0
      MIPS_ELF_PUT_WORD (info->output_bfd, value, sgot->contents + offset);
11425
0
    }
11426
11427
0
  if (hmips->global_got_area != GGA_NONE && g->next)
11428
0
    {
11429
0
      struct mips_got_entry e, *p;
11430
0
      bfd_vma entry;
11431
0
      bfd_vma offset;
11432
11433
0
      gg = g;
11434
11435
0
      e.abfd = info->output_bfd;
11436
0
      e.symndx = -1;
11437
0
      e.d.h = hmips;
11438
0
      e.tls_type = GOT_TLS_NONE;
11439
11440
0
      for (g = g->next; g->next != gg; g = g->next)
11441
0
  {
11442
0
    if (g->got_entries
11443
0
        && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11444
0
                 &e)))
11445
0
      {
11446
0
        offset = p->gotidx;
11447
0
        BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11448
0
        if (bfd_link_pic (info)
11449
0
      || (elf_hash_table (info)->dynamic_sections_created
11450
0
          && p->d.h != NULL
11451
0
          && p->d.h->root.def_dynamic
11452
0
          && !p->d.h->root.def_regular))
11453
0
    {
11454
      /* Create an R_MIPS_REL32 relocation for this entry.  Due to
11455
         the various compatibility problems, it's easier to mock
11456
         up an R_MIPS_32 or R_MIPS_64 relocation and leave
11457
         mips_elf_create_dynamic_relocation to calculate the
11458
         appropriate addend.  */
11459
0
      Elf_Internal_Rela rel[3];
11460
11461
0
      memset (rel, 0, sizeof (rel));
11462
0
      if (ABI_64_P (info->output_bfd))
11463
0
        rel[0].r_info = ELF_R_INFO (info->output_bfd, 0, R_MIPS_64);
11464
0
      else
11465
0
        rel[0].r_info = ELF_R_INFO (info->output_bfd, 0, R_MIPS_32);
11466
0
      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11467
11468
0
      entry = 0;
11469
0
      if (! (mips_elf_create_dynamic_relocation
11470
0
       (info->output_bfd, info, rel,
11471
0
        e.d.h, NULL, sym->st_value, &entry, sgot)))
11472
0
        return false;
11473
0
    }
11474
0
        else
11475
0
    entry = sym->st_value;
11476
0
        MIPS_ELF_PUT_WORD (info->output_bfd, entry,
11477
0
         sgot->contents + offset);
11478
0
      }
11479
0
  }
11480
0
    }
11481
11482
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
11483
0
  name = h->root.root.string;
11484
0
  if (h == elf_hash_table (info)->hdynamic
11485
0
      || h == elf_hash_table (info)->hgot)
11486
0
    sym->st_shndx = SHN_ABS;
11487
0
  else if (strcmp (name, "_DYNAMIC_LINK") == 0
11488
0
     || strcmp (name, "_DYNAMIC_LINKING") == 0)
11489
0
    {
11490
0
      sym->st_shndx = SHN_ABS;
11491
0
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11492
0
      sym->st_value = 1;
11493
0
    }
11494
0
  else if (SGI_COMPAT (info->output_bfd))
11495
0
    {
11496
0
      if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11497
0
    || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11498
0
  {
11499
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11500
0
    sym->st_other = STO_PROTECTED;
11501
0
    sym->st_value = 0;
11502
0
    sym->st_shndx = SHN_MIPS_DATA;
11503
0
  }
11504
0
      else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11505
0
  {
11506
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11507
0
    sym->st_other = STO_PROTECTED;
11508
0
    sym->st_value = mips_elf_hash_table (info)->procedure_count;
11509
0
    sym->st_shndx = SHN_ABS;
11510
0
  }
11511
0
      else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11512
0
  {
11513
0
    if (h->type == STT_FUNC)
11514
0
      sym->st_shndx = SHN_MIPS_TEXT;
11515
0
    else if (h->type == STT_OBJECT)
11516
0
      sym->st_shndx = SHN_MIPS_DATA;
11517
0
  }
11518
0
    }
11519
11520
  /* Emit a copy reloc, if needed.  */
11521
0
  if (h->needs_copy)
11522
0
    {
11523
0
      asection *s;
11524
0
      bfd_vma symval;
11525
11526
0
      BFD_ASSERT (h->dynindx != -1);
11527
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
11528
11529
0
      s = mips_elf_rel_dyn_section (info, false);
11530
0
      symval = (h->root.u.def.section->output_section->vma
11531
0
    + h->root.u.def.section->output_offset
11532
0
    + h->root.u.def.value);
11533
0
      mips_elf_output_dynamic_relocation (info->output_bfd, s, s->reloc_count++,
11534
0
            h->dynindx, R_MIPS_COPY, symval);
11535
0
    }
11536
11537
  /* Handle the IRIX6-specific symbols.  */
11538
0
  if (IRIX_COMPAT (info->output_bfd) == ict_irix6)
11539
0
    mips_elf_irix6_finish_dynamic_symbol (name, sym);
11540
11541
  /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
11542
     to treat compressed symbols like any other.  */
11543
0
  if (ELF_ST_IS_MIPS16 (sym->st_other))
11544
0
    {
11545
0
      BFD_ASSERT (sym->st_value & 1);
11546
0
      sym->st_other -= STO_MIPS16;
11547
0
    }
11548
0
  else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11549
0
    {
11550
0
      BFD_ASSERT (sym->st_value & 1);
11551
0
      sym->st_other -= STO_MICROMIPS;
11552
0
    }
11553
11554
0
  return true;
11555
0
}
11556
11557
/* Likewise, for VxWorks.  */
11558
11559
bool
11560
_bfd_mips_vxworks_finish_dynamic_symbol (struct bfd_link_info *info,
11561
           struct elf_link_hash_entry *h,
11562
           Elf_Internal_Sym *sym)
11563
0
{
11564
0
  bfd *dynobj;
11565
0
  asection *sgot;
11566
0
  struct mips_got_info *g;
11567
0
  struct mips_elf_link_hash_table *htab;
11568
0
  struct mips_elf_link_hash_entry *hmips;
11569
11570
0
  htab = mips_elf_hash_table (info);
11571
0
  BFD_ASSERT (htab != NULL);
11572
0
  dynobj = elf_hash_table (info)->dynobj;
11573
0
  hmips = (struct mips_elf_link_hash_entry *) h;
11574
11575
0
  if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11576
0
    {
11577
0
      bfd_byte *loc;
11578
0
      bfd_vma plt_address, got_address, got_offset, branch_offset;
11579
0
      Elf_Internal_Rela rel;
11580
0
      static const bfd_vma *plt_entry;
11581
0
      bfd_vma gotplt_index;
11582
0
      bfd_vma plt_offset;
11583
11584
0
      plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11585
0
      gotplt_index = h->plt.plist->gotplt_index;
11586
11587
0
      BFD_ASSERT (h->dynindx != -1);
11588
0
      BFD_ASSERT (htab->root.splt != NULL);
11589
0
      BFD_ASSERT (gotplt_index != MINUS_ONE);
11590
0
      BFD_ASSERT (plt_offset <= htab->root.splt->size);
11591
11592
      /* Calculate the address of the .plt entry.  */
11593
0
      plt_address = (htab->root.splt->output_section->vma
11594
0
         + htab->root.splt->output_offset
11595
0
         + plt_offset);
11596
11597
      /* Calculate the address of the .got.plt entry.  */
11598
0
      got_address = (htab->root.sgotplt->output_section->vma
11599
0
         + htab->root.sgotplt->output_offset
11600
0
         + gotplt_index * MIPS_ELF_GOT_SIZE (info->output_bfd));
11601
11602
      /* Calculate the offset of the .got.plt entry from
11603
   _GLOBAL_OFFSET_TABLE_.  */
11604
0
      got_offset = mips_elf_gotplt_index (info, h);
11605
11606
      /* Calculate the offset for the branch at the start of the PLT
11607
   entry.  The branch jumps to the beginning of .plt.  */
11608
0
      branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11609
11610
      /* Fill in the initial value of the .got.plt entry.  */
11611
0
      bfd_put_32 (info->output_bfd, plt_address,
11612
0
      (htab->root.sgotplt->contents
11613
0
       + gotplt_index * MIPS_ELF_GOT_SIZE (info->output_bfd)));
11614
11615
      /* Find out where the .plt entry should go.  */
11616
0
      loc = htab->root.splt->contents + plt_offset;
11617
11618
0
      if (bfd_link_pic (info))
11619
0
  {
11620
0
    plt_entry = mips_vxworks_shared_plt_entry;
11621
0
    bfd_put_32 (info->output_bfd, plt_entry[0] | branch_offset, loc);
11622
0
    bfd_put_32 (info->output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11623
0
  }
11624
0
      else
11625
0
  {
11626
0
    bfd_vma got_address_high, got_address_low;
11627
11628
0
    plt_entry = mips_vxworks_exec_plt_entry;
11629
0
    got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11630
0
    got_address_low = got_address & 0xffff;
11631
11632
0
    bfd_put_32 (info->output_bfd, plt_entry[0] | branch_offset, loc);
11633
0
    bfd_put_32 (info->output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11634
0
    bfd_put_32 (info->output_bfd, plt_entry[2] | got_address_high,
11635
0
          loc + 8);
11636
0
    bfd_put_32 (info->output_bfd, plt_entry[3] | got_address_low,
11637
0
          loc + 12);
11638
0
    bfd_put_32 (info->output_bfd, plt_entry[4], loc + 16);
11639
0
    bfd_put_32 (info->output_bfd, plt_entry[5], loc + 20);
11640
0
    bfd_put_32 (info->output_bfd, plt_entry[6], loc + 24);
11641
0
    bfd_put_32 (info->output_bfd, plt_entry[7], loc + 28);
11642
11643
0
    loc = (htab->srelplt2->contents
11644
0
     + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11645
11646
    /* Emit a relocation for the .got.plt entry.  */
11647
0
    rel.r_offset = got_address;
11648
0
    rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11649
0
    rel.r_addend = plt_offset;
11650
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &rel, loc);
11651
11652
    /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
11653
0
    loc += sizeof (Elf32_External_Rela);
11654
0
    rel.r_offset = plt_address + 8;
11655
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11656
0
    rel.r_addend = got_offset;
11657
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &rel, loc);
11658
11659
    /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
11660
0
    loc += sizeof (Elf32_External_Rela);
11661
0
    rel.r_offset += 4;
11662
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11663
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &rel, loc);
11664
0
  }
11665
11666
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11667
0
      loc = (htab->root.srelplt->contents
11668
0
       + gotplt_index * sizeof (Elf32_External_Rela));
11669
0
      rel.r_offset = got_address;
11670
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11671
0
      rel.r_addend = 0;
11672
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rel, loc);
11673
11674
0
      if (!h->def_regular)
11675
0
  sym->st_shndx = SHN_UNDEF;
11676
0
    }
11677
11678
0
  BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11679
11680
0
  sgot = htab->root.sgot;
11681
0
  g = htab->got_info;
11682
0
  BFD_ASSERT (g != NULL);
11683
11684
  /* See if this symbol has an entry in the GOT.  */
11685
0
  if (hmips->global_got_area != GGA_NONE)
11686
0
    {
11687
0
      bfd_vma offset;
11688
0
      Elf_Internal_Rela outrel;
11689
0
      bfd_byte *loc;
11690
0
      asection *s;
11691
11692
      /* Install the symbol value in the GOT.   */
11693
0
      offset = mips_elf_primary_global_got_index (info->output_bfd, info, h);
11694
0
      MIPS_ELF_PUT_WORD (info->output_bfd, sym->st_value,
11695
0
       sgot->contents + offset);
11696
11697
      /* Add a dynamic relocation for it.  */
11698
0
      s = mips_elf_rel_dyn_section (info, false);
11699
0
      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11700
0
      outrel.r_offset = (sgot->output_section->vma
11701
0
       + sgot->output_offset
11702
0
       + offset);
11703
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11704
0
      outrel.r_addend = 0;
11705
0
      bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11706
0
    }
11707
11708
  /* Emit a copy reloc, if needed.  */
11709
0
  if (h->needs_copy)
11710
0
    {
11711
0
      Elf_Internal_Rela rel;
11712
0
      asection *srel;
11713
0
      bfd_byte *loc;
11714
11715
0
      BFD_ASSERT (h->dynindx != -1);
11716
11717
0
      rel.r_offset = (h->root.u.def.section->output_section->vma
11718
0
          + h->root.u.def.section->output_offset
11719
0
          + h->root.u.def.value);
11720
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11721
0
      rel.r_addend = 0;
11722
0
      if (h->root.u.def.section == htab->root.sdynrelro)
11723
0
  srel = htab->root.sreldynrelro;
11724
0
      else
11725
0
  srel = htab->root.srelbss;
11726
0
      loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11727
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rel, loc);
11728
0
      ++srel->reloc_count;
11729
0
    }
11730
11731
  /* If this is a mips16/microMIPS symbol, force the value to be even.  */
11732
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
11733
0
    sym->st_value &= ~1;
11734
11735
0
  return true;
11736
0
}
11737
11738
/* Write out a plt0 entry to the beginning of .plt.  */
11739
11740
static bool
11741
mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11742
0
{
11743
0
  bfd_byte *loc;
11744
0
  bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11745
0
  static const bfd_vma *plt_entry;
11746
0
  struct mips_elf_link_hash_table *htab;
11747
11748
0
  htab = mips_elf_hash_table (info);
11749
0
  BFD_ASSERT (htab != NULL);
11750
11751
0
  if (ABI_64_P (output_bfd))
11752
0
    plt_entry = (htab->compact_branches
11753
0
     ? mipsr6_n64_exec_plt0_entry_compact
11754
0
     : mips_n64_exec_plt0_entry);
11755
0
  else if (ABI_N32_P (output_bfd))
11756
0
    plt_entry = (htab->compact_branches
11757
0
     ? mipsr6_n32_exec_plt0_entry_compact
11758
0
     : mips_n32_exec_plt0_entry);
11759
0
  else if (!htab->plt_header_is_comp)
11760
0
    plt_entry = (htab->compact_branches
11761
0
     ? mipsr6_o32_exec_plt0_entry_compact
11762
0
     : mips_o32_exec_plt0_entry);
11763
0
  else if (htab->insn32)
11764
0
    plt_entry = micromips_insn32_o32_exec_plt0_entry;
11765
0
  else
11766
0
    plt_entry = micromips_o32_exec_plt0_entry;
11767
11768
  /* Calculate the value of .got.plt.  */
11769
0
  gotplt_value = (htab->root.sgotplt->output_section->vma
11770
0
      + htab->root.sgotplt->output_offset);
11771
0
  gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11772
0
  gotplt_value_low = gotplt_value & 0xffff;
11773
11774
  /* The PLT sequence is not safe for N64 if .got.plt's address can
11775
     not be loaded in two instructions.  */
11776
0
  if (ABI_64_P (output_bfd)
11777
0
      && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11778
0
    {
11779
0
      _bfd_error_handler
11780
  /* xgettext:c-format */
11781
0
  (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11782
0
     "supported; consider using `-Ttext-segment=...'"),
11783
0
   output_bfd,
11784
0
   htab->root.sgotplt->output_section,
11785
0
   (int64_t) gotplt_value);
11786
0
      bfd_set_error (bfd_error_no_error);
11787
0
      return false;
11788
0
    }
11789
11790
  /* Install the PLT header.  */
11791
0
  loc = htab->root.splt->contents;
11792
0
  if (plt_entry == micromips_o32_exec_plt0_entry)
11793
0
    {
11794
0
      bfd_vma gotpc_offset;
11795
0
      bfd_vma loc_address;
11796
0
      size_t i;
11797
11798
0
      BFD_ASSERT (gotplt_value % 4 == 0);
11799
11800
0
      loc_address = (htab->root.splt->output_section->vma
11801
0
         + htab->root.splt->output_offset);
11802
0
      gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11803
11804
      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11805
0
      if (gotpc_offset + 0x1000000 >= 0x2000000)
11806
0
  {
11807
0
    _bfd_error_handler
11808
      /* xgettext:c-format */
11809
0
      (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11810
0
         "beyond the range of ADDIUPC"),
11811
0
       output_bfd,
11812
0
       htab->root.sgotplt->output_section,
11813
0
       (int64_t) gotpc_offset,
11814
0
       htab->root.splt->output_section);
11815
0
    bfd_set_error (bfd_error_no_error);
11816
0
    return false;
11817
0
  }
11818
0
      bfd_put_16 (output_bfd,
11819
0
      plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11820
0
      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11821
0
      for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11822
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11823
0
    }
11824
0
  else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11825
0
    {
11826
0
      size_t i;
11827
11828
0
      bfd_put_16 (output_bfd, plt_entry[0], loc);
11829
0
      bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11830
0
      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11831
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11832
0
      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11833
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11834
0
      for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11835
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11836
0
    }
11837
0
  else
11838
0
    {
11839
0
      bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11840
0
      bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11841
0
      bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11842
0
      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11843
0
      bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11844
0
      bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11845
0
      bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11846
0
      bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11847
0
    }
11848
11849
0
  return true;
11850
0
}
11851
11852
/* Install the PLT header for a VxWorks executable and finalize the
11853
   contents of .rela.plt.unloaded.  */
11854
11855
static void
11856
mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11857
0
{
11858
0
  Elf_Internal_Rela rela;
11859
0
  bfd_byte *loc;
11860
0
  bfd_vma got_value, got_value_high, got_value_low, plt_address;
11861
0
  static const bfd_vma *plt_entry;
11862
0
  struct mips_elf_link_hash_table *htab;
11863
11864
0
  htab = mips_elf_hash_table (info);
11865
0
  BFD_ASSERT (htab != NULL);
11866
11867
0
  plt_entry = mips_vxworks_exec_plt0_entry;
11868
11869
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
11870
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11871
0
         + htab->root.hgot->root.u.def.section->output_offset
11872
0
         + htab->root.hgot->root.u.def.value);
11873
11874
0
  got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11875
0
  got_value_low = got_value & 0xffff;
11876
11877
  /* Calculate the address of the PLT header.  */
11878
0
  plt_address = (htab->root.splt->output_section->vma
11879
0
     + htab->root.splt->output_offset);
11880
11881
  /* Install the PLT header.  */
11882
0
  loc = htab->root.splt->contents;
11883
0
  bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11884
0
  bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11885
0
  bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11886
0
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11887
0
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11888
0
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11889
11890
  /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
11891
0
  loc = htab->srelplt2->contents;
11892
0
  rela.r_offset = plt_address;
11893
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11894
0
  rela.r_addend = 0;
11895
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11896
0
  loc += sizeof (Elf32_External_Rela);
11897
11898
  /* Output the relocation for the following addiu of
11899
     %lo(_GLOBAL_OFFSET_TABLE_).  */
11900
0
  rela.r_offset += 4;
11901
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11902
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11903
0
  loc += sizeof (Elf32_External_Rela);
11904
11905
  /* Fix up the remaining relocations.  They may have the wrong
11906
     symbol index for _G_O_T_ or _P_L_T_ depending on the order
11907
     in which symbols were output.  */
11908
0
  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11909
0
    {
11910
0
      Elf_Internal_Rela rel;
11911
11912
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11913
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11914
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11915
0
      loc += sizeof (Elf32_External_Rela);
11916
11917
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11918
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11919
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11920
0
      loc += sizeof (Elf32_External_Rela);
11921
11922
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11923
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11924
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11925
0
      loc += sizeof (Elf32_External_Rela);
11926
0
    }
11927
0
}
11928
11929
/* Install the PLT header for a VxWorks shared library.  */
11930
11931
static void
11932
mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11933
0
{
11934
0
  unsigned int i;
11935
0
  struct mips_elf_link_hash_table *htab;
11936
11937
0
  htab = mips_elf_hash_table (info);
11938
0
  BFD_ASSERT (htab != NULL);
11939
11940
  /* We just need to copy the entry byte-by-byte.  */
11941
0
  for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11942
0
    bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11943
0
    htab->root.splt->contents + i * 4);
11944
0
}
11945
11946
/* Finish up the dynamic sections.  */
11947
11948
bool
11949
_bfd_mips_elf_finish_dynamic_sections (struct bfd_link_info *info,
11950
               bfd_byte *buf ATTRIBUTE_UNUSED)
11951
0
{
11952
0
  bfd *dynobj;
11953
0
  asection *sdyn;
11954
0
  asection *sgot;
11955
0
  struct mips_got_info *gg, *g;
11956
0
  struct mips_elf_link_hash_table *htab;
11957
11958
0
  htab = mips_elf_hash_table (info);
11959
0
  BFD_ASSERT (htab != NULL);
11960
11961
0
  dynobj = elf_hash_table (info)->dynobj;
11962
11963
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11964
11965
0
  sgot = htab->root.sgot;
11966
0
  gg = htab->got_info;
11967
11968
0
  if (elf_hash_table (info)->dynamic_sections_created)
11969
0
    {
11970
0
      bfd_byte *b;
11971
0
      int dyn_to_skip = 0, dyn_skipped = 0;
11972
11973
0
      BFD_ASSERT (sdyn != NULL);
11974
0
      BFD_ASSERT (gg != NULL);
11975
11976
0
      g = mips_elf_bfd_got (info->output_bfd, false);
11977
0
      BFD_ASSERT (g != NULL);
11978
11979
0
      for (b = sdyn->contents;
11980
0
     b < sdyn->contents + sdyn->size;
11981
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
11982
0
  {
11983
0
    Elf_Internal_Dyn dyn;
11984
0
    const char *name;
11985
0
    size_t elemsize;
11986
0
    asection *s;
11987
0
    bool swap_out_p;
11988
11989
    /* Read in the current dynamic entry.  */
11990
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11991
11992
    /* Assume that we're going to modify it and write it out.  */
11993
0
    swap_out_p = true;
11994
11995
0
    switch (dyn.d_tag)
11996
0
      {
11997
0
      case DT_RELENT:
11998
0
        dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11999
0
        break;
12000
12001
0
      case DT_RELAENT:
12002
0
        BFD_ASSERT (htab->root.target_os == is_vxworks);
12003
0
        dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
12004
0
        break;
12005
12006
0
      case DT_STRSZ:
12007
        /* Rewrite DT_STRSZ.  */
12008
0
        dyn.d_un.d_val =
12009
0
    _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
12010
0
        break;
12011
12012
0
      case DT_PLTGOT:
12013
0
        s = htab->root.sgot;
12014
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12015
0
        break;
12016
12017
0
      case DT_MIPS_PLTGOT:
12018
0
        s = htab->root.sgotplt;
12019
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12020
0
        break;
12021
12022
0
      case DT_MIPS_RLD_VERSION:
12023
0
        dyn.d_un.d_val = 1; /* XXX */
12024
0
        break;
12025
12026
0
      case DT_MIPS_FLAGS:
12027
0
        dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
12028
0
        break;
12029
12030
0
      case DT_MIPS_TIME_STAMP:
12031
0
        {
12032
0
    time_t t;
12033
0
    time (&t);
12034
0
    dyn.d_un.d_val = t;
12035
0
        }
12036
0
        break;
12037
12038
0
      case DT_MIPS_ICHECKSUM:
12039
        /* XXX FIXME: */
12040
0
        swap_out_p = false;
12041
0
        break;
12042
12043
0
      case DT_MIPS_IVERSION:
12044
        /* XXX FIXME: */
12045
0
        swap_out_p = false;
12046
0
        break;
12047
12048
0
      case DT_MIPS_BASE_ADDRESS:
12049
0
        s = info->output_bfd->sections;
12050
0
        BFD_ASSERT (s != NULL);
12051
0
        dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
12052
0
        break;
12053
12054
0
      case DT_MIPS_LOCAL_GOTNO:
12055
0
        dyn.d_un.d_val = g->local_gotno;
12056
0
        break;
12057
12058
0
      case DT_MIPS_UNREFEXTNO:
12059
        /* The index into the dynamic symbol table which is the
12060
     entry of the first external symbol that is not
12061
     referenced within the same object.  */
12062
0
        dyn.d_un.d_val = bfd_count_sections (info->output_bfd) + 1;
12063
0
        break;
12064
12065
0
      case DT_MIPS_GOTSYM:
12066
0
        if (htab->global_gotsym)
12067
0
    {
12068
0
      dyn.d_un.d_val = htab->global_gotsym->dynindx;
12069
0
      break;
12070
0
    }
12071
        /* In case if we don't have global got symbols we default
12072
     to setting DT_MIPS_GOTSYM to the same value as
12073
     DT_MIPS_SYMTABNO.  */
12074
        /* Fall through.  */
12075
12076
0
      case DT_MIPS_SYMTABNO:
12077
0
        name = ".dynsym";
12078
0
        elemsize = MIPS_ELF_SYM_SIZE (info->output_bfd);
12079
0
        s = bfd_get_linker_section (dynobj, name);
12080
12081
0
        if (s != NULL)
12082
0
    dyn.d_un.d_val = s->size / elemsize;
12083
0
        else
12084
0
    dyn.d_un.d_val = 0;
12085
0
        break;
12086
12087
0
      case DT_MIPS_HIPAGENO:
12088
0
        dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
12089
0
        break;
12090
12091
0
      case DT_MIPS_RLD_MAP:
12092
0
        {
12093
0
    struct elf_link_hash_entry *h;
12094
0
    h = mips_elf_hash_table (info)->rld_symbol;
12095
0
    if (!h)
12096
0
      {
12097
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12098
0
        swap_out_p = false;
12099
0
        break;
12100
0
      }
12101
0
    s = h->root.u.def.section;
12102
12103
    /* The MIPS_RLD_MAP tag stores the absolute address of the
12104
       debug pointer.  */
12105
0
    dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12106
0
          + h->root.u.def.value);
12107
0
        }
12108
0
        break;
12109
12110
0
      case DT_MIPS_RLD_MAP_REL:
12111
0
        {
12112
0
    struct elf_link_hash_entry *h;
12113
0
    bfd_vma dt_addr, rld_addr;
12114
0
    h = mips_elf_hash_table (info)->rld_symbol;
12115
0
    if (!h)
12116
0
      {
12117
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12118
0
        swap_out_p = false;
12119
0
        break;
12120
0
      }
12121
0
    s = h->root.u.def.section;
12122
12123
    /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12124
       pointer, relative to the address of the tag.  */
12125
0
    dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12126
0
         + (b - sdyn->contents));
12127
0
    rld_addr = (s->output_section->vma + s->output_offset
12128
0
          + h->root.u.def.value);
12129
0
    dyn.d_un.d_ptr = rld_addr - dt_addr;
12130
0
        }
12131
0
        break;
12132
12133
0
      case DT_MIPS_OPTIONS:
12134
0
        s = (bfd_get_section_by_name
12135
0
       (info->output_bfd,
12136
0
        MIPS_ELF_OPTIONS_SECTION_NAME (info->output_bfd)));
12137
0
        dyn.d_un.d_ptr = s->vma;
12138
0
        break;
12139
12140
0
      case DT_PLTREL:
12141
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12142
0
        if (htab->root.target_os == is_vxworks)
12143
0
    dyn.d_un.d_val = DT_RELA;
12144
0
        else
12145
0
    dyn.d_un.d_val = DT_REL;
12146
0
        break;
12147
12148
0
      case DT_PLTRELSZ:
12149
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12150
0
        dyn.d_un.d_val = htab->root.srelplt->size;
12151
0
        break;
12152
12153
0
      case DT_JMPREL:
12154
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12155
0
        dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12156
0
        + htab->root.srelplt->output_offset);
12157
0
        break;
12158
12159
0
      case DT_TEXTREL:
12160
        /* If we didn't need any text relocations after all, delete
12161
     the dynamic tag.  */
12162
0
        if (!(info->flags & DF_TEXTREL))
12163
0
    {
12164
0
      dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12165
0
      swap_out_p = false;
12166
0
    }
12167
0
        break;
12168
12169
0
      case DT_FLAGS:
12170
        /* If we didn't need any text relocations after all, clear
12171
     DF_TEXTREL from DT_FLAGS.  */
12172
0
        if (!(info->flags & DF_TEXTREL))
12173
0
    dyn.d_un.d_val &= ~DF_TEXTREL;
12174
0
        else
12175
0
    swap_out_p = false;
12176
0
        break;
12177
12178
0
      case DT_MIPS_XHASH:
12179
0
        name = ".MIPS.xhash";
12180
0
        s = bfd_get_linker_section (dynobj, name);
12181
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12182
0
        break;
12183
12184
0
      default:
12185
0
        swap_out_p = false;
12186
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
12187
0
        if (htab->root.target_os == is_vxworks
12188
0
      && elf_vxworks_finish_dynamic_entry (info->output_bfd, &dyn))
12189
0
    swap_out_p = true;
12190
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
12191
0
        break;
12192
0
      }
12193
12194
0
    if (swap_out_p || dyn_skipped)
12195
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12196
0
        (dynobj, &dyn, b - dyn_skipped);
12197
12198
0
    if (dyn_to_skip)
12199
0
      {
12200
0
        dyn_skipped += dyn_to_skip;
12201
0
        dyn_to_skip = 0;
12202
0
      }
12203
0
  }
12204
12205
      /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
12206
0
      if (dyn_skipped > 0)
12207
0
  memset (b - dyn_skipped, 0, dyn_skipped);
12208
0
    }
12209
12210
0
  if (sgot != NULL && sgot->size > 0
12211
0
      && !bfd_is_abs_section (sgot->output_section))
12212
0
    {
12213
0
      if (htab->root.target_os == is_vxworks)
12214
0
  {
12215
    /* The first entry of the global offset table points to the
12216
       ".dynamic" section.  The second is initialized by the
12217
       loader and contains the shared library identifier.
12218
       The third is also initialized by the loader and points
12219
       to the lazy resolution stub.  */
12220
0
    MIPS_ELF_PUT_WORD (info->output_bfd,
12221
0
           sdyn->output_offset + sdyn->output_section->vma,
12222
0
           sgot->contents);
12223
0
    MIPS_ELF_PUT_WORD (info->output_bfd, 0,
12224
0
           sgot->contents + MIPS_ELF_GOT_SIZE (info->output_bfd));
12225
0
    MIPS_ELF_PUT_WORD (info->output_bfd, 0,
12226
0
           sgot->contents
12227
0
           + 2 * MIPS_ELF_GOT_SIZE (info->output_bfd));
12228
0
  }
12229
0
      else
12230
0
  {
12231
    /* The first entry of the global offset table will be filled at
12232
       runtime. The second entry will be used by some runtime loaders.
12233
       This isn't the case of IRIX rld.  */
12234
0
    MIPS_ELF_PUT_WORD (info->output_bfd, 0, sgot->contents);
12235
0
    MIPS_ELF_PUT_WORD (info->output_bfd,
12236
0
           MIPS_ELF_GNU_GOT1_MASK (info->output_bfd),
12237
0
           sgot->contents + MIPS_ELF_GOT_SIZE (info->output_bfd));
12238
0
  }
12239
12240
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12241
0
   = MIPS_ELF_GOT_SIZE (info->output_bfd);
12242
0
    }
12243
12244
  /* Generate dynamic relocations for the non-primary gots.  */
12245
0
  if (gg != NULL && gg->next)
12246
0
    {
12247
0
      Elf_Internal_Rela rel[3];
12248
0
      bfd_vma addend = 0;
12249
12250
0
      memset (rel, 0, sizeof (rel));
12251
0
      rel[0].r_info = ELF_R_INFO (info->output_bfd, 0, R_MIPS_REL32);
12252
12253
0
      for (g = gg->next; g->next != gg; g = g->next)
12254
0
  {
12255
0
    bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12256
0
      + g->next->tls_gotno;
12257
12258
0
    MIPS_ELF_PUT_WORD (info->output_bfd, 0, sgot->contents
12259
0
           + got_index++ * MIPS_ELF_GOT_SIZE (info->output_bfd));
12260
0
    MIPS_ELF_PUT_WORD (info->output_bfd,
12261
0
           MIPS_ELF_GNU_GOT1_MASK (info->output_bfd),
12262
0
           sgot->contents
12263
0
           + got_index++ * MIPS_ELF_GOT_SIZE (info->output_bfd));
12264
12265
0
    if (! bfd_link_pic (info))
12266
0
      continue;
12267
12268
0
    for (; got_index < g->local_gotno; got_index++)
12269
0
      {
12270
0
        if (got_index >= g->assigned_low_gotno
12271
0
      && got_index <= g->assigned_high_gotno)
12272
0
    continue;
12273
12274
0
        rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12275
0
    = got_index * MIPS_ELF_GOT_SIZE (info->output_bfd);
12276
0
        if (!(mips_elf_create_dynamic_relocation
12277
0
        (info->output_bfd, info, rel, NULL,
12278
0
         bfd_abs_section_ptr,
12279
0
         0, &addend, sgot)))
12280
0
    return false;
12281
0
        BFD_ASSERT (addend == 0);
12282
0
      }
12283
0
  }
12284
0
    }
12285
12286
  /* The generation of dynamic relocations for the non-primary gots
12287
     adds more dynamic relocations.  We cannot count them until
12288
     here.  */
12289
12290
0
  if (elf_hash_table (info)->dynamic_sections_created)
12291
0
    {
12292
0
      bfd_byte *b;
12293
0
      bool swap_out_p;
12294
12295
0
      BFD_ASSERT (sdyn != NULL);
12296
12297
0
      for (b = sdyn->contents;
12298
0
     b < sdyn->contents + sdyn->size;
12299
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
12300
0
  {
12301
0
    Elf_Internal_Dyn dyn;
12302
0
    asection *s;
12303
12304
    /* Read in the current dynamic entry.  */
12305
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12306
12307
    /* Assume that we're going to modify it and write it out.  */
12308
0
    swap_out_p = true;
12309
12310
0
    switch (dyn.d_tag)
12311
0
      {
12312
0
      case DT_RELSZ:
12313
        /* Reduce DT_RELSZ to account for any relocations we
12314
     decided not to make.  This is for the n64 irix rld,
12315
     which doesn't seem to apply any relocations if there
12316
     are trailing null entries.  */
12317
0
        s = mips_elf_rel_dyn_section (info, false);
12318
0
        dyn.d_un.d_val = (s->reloc_count
12319
0
        * (ABI_64_P (info->output_bfd)
12320
0
           ? sizeof (Elf64_Mips_External_Rel)
12321
0
           : sizeof (Elf32_External_Rel)));
12322
        /* Adjust the section size too.  Tools like the prelinker
12323
     can reasonably expect the values to the same.  */
12324
0
        BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12325
0
        elf_section_data (s->output_section)->this_hdr.sh_size
12326
0
    = dyn.d_un.d_val;
12327
0
        break;
12328
12329
0
      default:
12330
0
        swap_out_p = false;
12331
0
        break;
12332
0
      }
12333
12334
0
    if (swap_out_p)
12335
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12336
0
        (dynobj, &dyn, b);
12337
0
  }
12338
0
    }
12339
12340
0
  {
12341
0
    asection *s;
12342
0
    Elf32_compact_rel cpt;
12343
12344
0
    if (SGI_COMPAT (info->output_bfd))
12345
0
      {
12346
  /* Write .compact_rel section out.  */
12347
0
  s = bfd_get_linker_section (dynobj, ".compact_rel");
12348
0
  if (s != NULL)
12349
0
    {
12350
0
      cpt.id1 = 1;
12351
0
      cpt.num = s->reloc_count;
12352
0
      cpt.id2 = 2;
12353
0
      cpt.offset = (s->output_section->filepos
12354
0
        + sizeof (Elf32_External_compact_rel));
12355
0
      cpt.reserved0 = 0;
12356
0
      cpt.reserved1 = 0;
12357
0
      bfd_elf32_swap_compact_rel_out (info->output_bfd, &cpt,
12358
0
              ((Elf32_External_compact_rel *)
12359
0
               s->contents));
12360
12361
      /* Clean up a dummy stub function entry in .text.  */
12362
0
      if (htab->sstubs != NULL
12363
0
    && htab->sstubs->contents != NULL)
12364
0
        {
12365
0
    file_ptr dummy_offset;
12366
12367
0
    BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12368
0
    dummy_offset = htab->sstubs->size - htab->function_stub_size;
12369
0
    memset (htab->sstubs->contents + dummy_offset, 0,
12370
0
      htab->function_stub_size);
12371
0
        }
12372
0
    }
12373
0
      }
12374
12375
    /* The psABI says that the dynamic relocations must be sorted in
12376
       increasing order of r_symndx.  The VxWorks EABI doesn't require
12377
       this, and because the code below handles REL rather than RELA
12378
       relocations, using it for VxWorks would be outright harmful.  */
12379
0
    if (htab->root.target_os != is_vxworks)
12380
0
      {
12381
0
  s = mips_elf_rel_dyn_section (info, false);
12382
0
  if (s != NULL
12383
0
      && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (info->output_bfd))
12384
0
    {
12385
0
      reldyn_sorting_bfd = info->output_bfd;
12386
12387
0
      if (ABI_64_P (info->output_bfd))
12388
0
        qsort ((Elf64_External_Rel *) s->contents + 1,
12389
0
         s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12390
0
         sort_dynamic_relocs_64);
12391
0
      else
12392
0
        qsort ((Elf32_External_Rel *) s->contents + 1,
12393
0
         s->reloc_count - 1, sizeof (Elf32_External_Rel),
12394
0
         sort_dynamic_relocs);
12395
0
    }
12396
0
      }
12397
0
  }
12398
12399
0
  if (htab->root.splt && htab->root.splt->size > 0)
12400
0
    {
12401
0
      if (htab->root.target_os == is_vxworks)
12402
0
  {
12403
0
    if (bfd_link_pic (info))
12404
0
      mips_vxworks_finish_shared_plt (info->output_bfd, info);
12405
0
    else
12406
0
      mips_vxworks_finish_exec_plt (info->output_bfd, info);
12407
0
  }
12408
0
      else
12409
0
  {
12410
0
    BFD_ASSERT (!bfd_link_pic (info));
12411
0
    if (!mips_finish_exec_plt (info->output_bfd, info))
12412
0
      return false;
12413
0
  }
12414
0
    }
12415
0
  return true;
12416
0
}
12417
12418
12419
/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
12420
12421
static void
12422
mips_set_isa_flags (bfd *abfd)
12423
62
{
12424
62
  flagword val;
12425
12426
62
  switch (bfd_get_mach (abfd))
12427
62
    {
12428
0
    default:
12429
0
      if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12430
0
        val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
12431
0
      else
12432
0
        val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
12433
0
      break;
12434
12435
43
    case bfd_mach_mips3000:
12436
43
      val = EF_MIPS_ARCH_1;
12437
43
      break;
12438
12439
0
    case bfd_mach_mips3900:
12440
0
      val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
12441
0
      break;
12442
12443
6
    case bfd_mach_mips6000:
12444
6
      val = EF_MIPS_ARCH_2;
12445
6
      break;
12446
12447
0
    case bfd_mach_mips4010:
12448
0
      val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
12449
0
      break;
12450
12451
0
    case bfd_mach_mips_allegrex:
12452
0
      val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
12453
0
      break;
12454
12455
1
    case bfd_mach_mips4000:
12456
1
    case bfd_mach_mips4300:
12457
1
    case bfd_mach_mips4400:
12458
1
    case bfd_mach_mips4600:
12459
1
      val = EF_MIPS_ARCH_3;
12460
1
      break;
12461
12462
0
    case bfd_mach_mips4100:
12463
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
12464
0
      break;
12465
12466
0
    case bfd_mach_mips4111:
12467
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
12468
0
      break;
12469
12470
0
    case bfd_mach_mips4120:
12471
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
12472
0
      break;
12473
12474
0
    case bfd_mach_mips4650:
12475
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
12476
0
      break;
12477
12478
0
    case bfd_mach_mips_tx49:
12479
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_TX49;
12480
0
      break;
12481
12482
0
    case bfd_mach_mips5400:
12483
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
12484
0
      break;
12485
12486
0
    case bfd_mach_mips5500:
12487
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
12488
0
      break;
12489
12490
0
    case bfd_mach_mips5900:
12491
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
12492
0
      break;
12493
12494
0
    case bfd_mach_mips9000:
12495
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
12496
0
      break;
12497
12498
0
    case bfd_mach_mips5000:
12499
0
    case bfd_mach_mips7000:
12500
1
    case bfd_mach_mips8000:
12501
1
    case bfd_mach_mips10000:
12502
1
    case bfd_mach_mips12000:
12503
1
    case bfd_mach_mips14000:
12504
1
    case bfd_mach_mips16000:
12505
1
      val = EF_MIPS_ARCH_4;
12506
1
      break;
12507
12508
1
    case bfd_mach_mips5:
12509
1
      val = EF_MIPS_ARCH_5;
12510
1
      break;
12511
12512
0
    case bfd_mach_mips_loongson_2e:
12513
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
12514
0
      break;
12515
12516
0
    case bfd_mach_mips_loongson_2f:
12517
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
12518
0
      break;
12519
12520
0
    case bfd_mach_mips_sb1:
12521
0
      val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
12522
0
      break;
12523
12524
0
    case bfd_mach_mips_gs464:
12525
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
12526
0
      break;
12527
12528
0
    case bfd_mach_mips_gs464e:
12529
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
12530
0
      break;
12531
12532
0
    case bfd_mach_mips_gs264e:
12533
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
12534
0
      break;
12535
12536
0
    case bfd_mach_mips_octeon:
12537
0
    case bfd_mach_mips_octeonp:
12538
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
12539
0
      break;
12540
12541
0
    case bfd_mach_mips_octeon3:
12542
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
12543
0
      break;
12544
12545
0
    case bfd_mach_mips_xlr:
12546
0
      val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
12547
0
      break;
12548
12549
0
    case bfd_mach_mips_octeon2:
12550
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
12551
0
      break;
12552
12553
2
    case bfd_mach_mipsisa32:
12554
2
      val = EF_MIPS_ARCH_32;
12555
2
      break;
12556
12557
3
    case bfd_mach_mipsisa64:
12558
3
      val = EF_MIPS_ARCH_64;
12559
3
      break;
12560
12561
1
    case bfd_mach_mipsisa32r2:
12562
1
    case bfd_mach_mipsisa32r3:
12563
1
    case bfd_mach_mipsisa32r5:
12564
1
      val = EF_MIPS_ARCH_32R2;
12565
1
      break;
12566
12567
0
    case bfd_mach_mips_interaptiv_mr2:
12568
0
      val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
12569
0
      break;
12570
12571
2
    case bfd_mach_mipsisa64r2:
12572
2
    case bfd_mach_mipsisa64r3:
12573
2
    case bfd_mach_mipsisa64r5:
12574
2
      val = EF_MIPS_ARCH_64R2;
12575
2
      break;
12576
12577
1
    case bfd_mach_mipsisa32r6:
12578
1
      val = EF_MIPS_ARCH_32R6;
12579
1
      break;
12580
12581
1
    case bfd_mach_mipsisa64r6:
12582
1
      val = EF_MIPS_ARCH_64R6;
12583
1
      break;
12584
62
    }
12585
62
  elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12586
62
  elf_elfheader (abfd)->e_flags |= val;
12587
12588
62
}
12589
12590
12591
/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12592
   Don't do so for code sections.  We want to keep ordering of HI16/LO16
12593
   as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
12594
   relocs to be sorted.  */
12595
12596
bool
12597
_bfd_mips_elf_sort_relocs_p (asection *sec)
12598
0
{
12599
0
  return (sec->flags & SEC_CODE) == 0;
12600
0
}
12601
12602
12603
/* The final processing done just before writing out a MIPS ELF object
12604
   file.  This gets the MIPS architecture right based on the machine
12605
   number.  This is used by both the 32-bit and the 64-bit ABI.  */
12606
12607
void
12608
_bfd_mips_final_write_processing (bfd *abfd)
12609
78
{
12610
78
  unsigned int i;
12611
78
  Elf_Internal_Shdr **hdrpp;
12612
78
  const char *name;
12613
78
  asection *sec;
12614
12615
  /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12616
     is nonzero.  This is for compatibility with old objects, which used
12617
     a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
12618
78
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12619
62
    mips_set_isa_flags (abfd);
12620
12621
  /* Set the sh_info field for .gptab sections and other appropriate
12622
     info for each special section.  */
12623
78
  for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12624
371
       i < elf_numsections (abfd);
12625
293
       i++, hdrpp++)
12626
293
    {
12627
293
      switch ((*hdrpp)->sh_type)
12628
293
  {
12629
0
  case SHT_MIPS_MSYM:
12630
0
  case SHT_MIPS_LIBLIST:
12631
0
    sec = bfd_get_section_by_name (abfd, ".dynstr");
12632
0
    if (sec != NULL)
12633
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12634
0
    break;
12635
12636
0
  case SHT_MIPS_GPTAB:
12637
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12638
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12639
0
    if (startswith (name, ".gptab."))
12640
0
      {
12641
0
        sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12642
0
        if (sec != NULL)
12643
0
    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12644
0
      }
12645
0
    break;
12646
12647
0
  case SHT_MIPS_CONTENT:
12648
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12649
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12650
0
    if (startswith (name, ".MIPS.content"))
12651
0
      {
12652
0
        sec = bfd_get_section_by_name (abfd,
12653
0
               name + sizeof ".MIPS.content" - 1);
12654
0
        if (sec != NULL)
12655
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12656
0
      }
12657
0
    break;
12658
12659
0
  case SHT_MIPS_SYMBOL_LIB:
12660
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12661
0
    if (sec != NULL)
12662
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12663
0
    sec = bfd_get_section_by_name (abfd, ".liblist");
12664
0
    if (sec != NULL)
12665
0
      (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12666
0
    break;
12667
12668
0
  case SHT_MIPS_EVENTS:
12669
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12670
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12671
0
    if (startswith (name, ".MIPS.events"))
12672
0
      sec = bfd_get_section_by_name (abfd,
12673
0
             name + sizeof ".MIPS.events" - 1);
12674
0
    else if (startswith (name, ".MIPS.post_rel"))
12675
0
      sec = bfd_get_section_by_name (abfd,
12676
0
             name + sizeof ".MIPS.post_rel" - 1);
12677
0
    else
12678
0
      sec = NULL;
12679
0
    if (sec != NULL)
12680
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12681
0
    break;
12682
12683
0
  case SHT_MIPS_XHASH:
12684
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12685
0
    if (sec != NULL)
12686
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12687
293
  }
12688
293
    }
12689
78
}
12690
12691
bool
12692
_bfd_mips_elf_final_write_processing (bfd *abfd)
12693
78
{
12694
78
  _bfd_mips_final_write_processing (abfd);
12695
78
  return _bfd_elf_final_write_processing (abfd);
12696
78
}
12697

12698
/* When creating an IRIX5 executable, we need REGINFO and RTPROC
12699
   segments.  */
12700
12701
int
12702
_bfd_mips_elf_additional_program_headers (bfd *abfd,
12703
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
12704
3
{
12705
3
  asection *s;
12706
3
  int ret = 0;
12707
12708
  /* See if we need a PT_MIPS_REGINFO segment.  */
12709
3
  s = bfd_get_section_by_name (abfd, ".reginfo");
12710
3
  if (s && (s->flags & SEC_LOAD))
12711
0
    ++ret;
12712
12713
  /* See if we need a PT_MIPS_ABIFLAGS segment.  */
12714
3
  if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12715
0
    ++ret;
12716
12717
  /* See if we need a PT_MIPS_OPTIONS segment.  */
12718
3
  if (IRIX_COMPAT (abfd) == ict_irix6
12719
2
      && bfd_get_section_by_name (abfd,
12720
2
          MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12721
0
    ++ret;
12722
12723
  /* See if we need a PT_MIPS_RTPROC segment.  */
12724
3
  if (IRIX_COMPAT (abfd) == ict_irix5
12725
1
      && bfd_get_section_by_name (abfd, ".dynamic")
12726
0
      && bfd_get_section_by_name (abfd, ".mdebug"))
12727
0
    ++ret;
12728
12729
  /* Allocate a PT_NULL header in dynamic objects.  See
12730
     _bfd_mips_elf_modify_segment_map for details.  */
12731
3
  if (!SGI_COMPAT (abfd)
12732
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12733
0
    ++ret;
12734
12735
3
  return ret;
12736
3
}
12737
12738
/* Modify the segment map for an IRIX5 executable.  */
12739
12740
bool
12741
_bfd_mips_elf_modify_segment_map (bfd *abfd,
12742
          struct bfd_link_info *info)
12743
56
{
12744
56
  asection *s;
12745
56
  struct elf_segment_map *m, **pm;
12746
56
  size_t amt;
12747
12748
  /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12749
     segment.  */
12750
56
  s = bfd_get_section_by_name (abfd, ".reginfo");
12751
56
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12752
0
    {
12753
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12754
0
  if (m->p_type == PT_MIPS_REGINFO)
12755
0
    break;
12756
0
      if (m == NULL)
12757
0
  {
12758
0
    amt = sizeof *m;
12759
0
    m = bfd_zalloc (abfd, amt);
12760
0
    if (m == NULL)
12761
0
      return false;
12762
12763
0
    m->p_type = PT_MIPS_REGINFO;
12764
0
    m->count = 1;
12765
0
    m->sections[0] = s;
12766
12767
    /* We want to put it after the PHDR and INTERP segments.  */
12768
0
    pm = &elf_seg_map (abfd);
12769
0
    while (*pm != NULL
12770
0
     && ((*pm)->p_type == PT_PHDR
12771
0
         || (*pm)->p_type == PT_INTERP))
12772
0
      pm = &(*pm)->next;
12773
12774
0
    m->next = *pm;
12775
0
    *pm = m;
12776
0
  }
12777
0
    }
12778
12779
  /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12780
     segment.  */
12781
56
  s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12782
56
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12783
0
    {
12784
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12785
0
  if (m->p_type == PT_MIPS_ABIFLAGS)
12786
0
    break;
12787
0
      if (m == NULL)
12788
0
  {
12789
0
    amt = sizeof *m;
12790
0
    m = bfd_zalloc (abfd, amt);
12791
0
    if (m == NULL)
12792
0
      return false;
12793
12794
0
    m->p_type = PT_MIPS_ABIFLAGS;
12795
0
    m->count = 1;
12796
0
    m->sections[0] = s;
12797
12798
    /* We want to put it after the PHDR and INTERP segments.  */
12799
0
    pm = &elf_seg_map (abfd);
12800
0
    while (*pm != NULL
12801
0
     && ((*pm)->p_type == PT_PHDR
12802
0
         || (*pm)->p_type == PT_INTERP))
12803
0
      pm = &(*pm)->next;
12804
12805
0
    m->next = *pm;
12806
0
    *pm = m;
12807
0
  }
12808
0
    }
12809
12810
  /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12811
     .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
12812
     PT_MIPS_OPTIONS segment immediately following the program header
12813
     table.  */
12814
56
  if (NEWABI_P (abfd)
12815
      /* On non-IRIX6 new abi, we'll have already created a segment
12816
   for this section, so don't create another.  I'm not sure this
12817
   is not also the case for IRIX 6, but I can't test it right
12818
   now.  */
12819
39
      && IRIX_COMPAT (abfd) == ict_irix6)
12820
31
    {
12821
62
      for (s = abfd->sections; s; s = s->next)
12822
31
  if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12823
0
    break;
12824
12825
31
      if (s)
12826
0
  {
12827
0
    struct elf_segment_map *options_segment;
12828
12829
0
    pm = &elf_seg_map (abfd);
12830
0
    while (*pm != NULL
12831
0
     && ((*pm)->p_type == PT_PHDR
12832
0
         || (*pm)->p_type == PT_INTERP))
12833
0
      pm = &(*pm)->next;
12834
12835
0
    if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12836
0
      {
12837
0
        amt = sizeof (struct elf_segment_map);
12838
0
        options_segment = bfd_zalloc (abfd, amt);
12839
0
        options_segment->next = *pm;
12840
0
        options_segment->p_type = PT_MIPS_OPTIONS;
12841
0
        options_segment->p_flags = PF_R;
12842
0
        options_segment->p_flags_valid = true;
12843
0
        options_segment->count = 1;
12844
0
        options_segment->sections[0] = s;
12845
0
        *pm = options_segment;
12846
0
      }
12847
0
  }
12848
31
    }
12849
25
  else
12850
25
    {
12851
25
      if (IRIX_COMPAT (abfd) == ict_irix5)
12852
11
  {
12853
    /* If there are .dynamic and .mdebug sections, we make a room
12854
       for the RTPROC header.  FIXME: Rewrite without section names.  */
12855
11
    if (bfd_get_section_by_name (abfd, ".interp") == NULL
12856
11
        && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12857
0
        && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12858
0
      {
12859
0
        for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12860
0
    if (m->p_type == PT_MIPS_RTPROC)
12861
0
      break;
12862
0
        if (m == NULL)
12863
0
    {
12864
0
      amt = sizeof *m;
12865
0
      m = bfd_zalloc (abfd, amt);
12866
0
      if (m == NULL)
12867
0
        return false;
12868
12869
0
      m->p_type = PT_MIPS_RTPROC;
12870
12871
0
      s = bfd_get_section_by_name (abfd, ".rtproc");
12872
0
      if (s == NULL)
12873
0
        {
12874
0
          m->count = 0;
12875
0
          m->p_flags = 0;
12876
0
          m->p_flags_valid = 1;
12877
0
        }
12878
0
      else
12879
0
        {
12880
0
          m->count = 1;
12881
0
          m->sections[0] = s;
12882
0
        }
12883
12884
      /* We want to put it after the DYNAMIC segment.  */
12885
0
      pm = &elf_seg_map (abfd);
12886
0
      while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12887
0
        pm = &(*pm)->next;
12888
0
      if (*pm != NULL)
12889
0
        pm = &(*pm)->next;
12890
12891
0
      m->next = *pm;
12892
0
      *pm = m;
12893
0
    }
12894
0
      }
12895
11
  }
12896
      /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12897
   .dynstr, .dynsym, and .hash sections, and everything in
12898
   between.  We avoid any sections though ahead of .dynamic,
12899
   which needs to be first to be usable.  This can only ever
12900
   happen with a non-standard linker script.  */
12901
53
      for (pm = &elf_seg_map (abfd); *pm != NULL;
12902
28
     pm = &(*pm)->next)
12903
28
  if ((*pm)->p_type == PT_DYNAMIC)
12904
0
    break;
12905
25
      m = *pm;
12906
      /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12907
   glibc's dynamic linker has traditionally derived the number of
12908
   tags from the p_filesz field, and sometimes allocates stack
12909
   arrays of that size.  An overly-big PT_DYNAMIC segment can
12910
   be actively harmful in such cases.  Making PT_DYNAMIC contain
12911
   other sections can also make life hard for the prelinker,
12912
   which might move one of the other sections to a different
12913
   PT_LOAD segment.  */
12914
25
      if (SGI_COMPAT (abfd)
12915
11
    && m != NULL
12916
0
    && m->count == 1
12917
0
    && strcmp (m->sections[0]->name, ".dynamic") == 0
12918
0
    && (m->sections[0]->flags & SEC_LOAD) != 0)
12919
0
  {
12920
0
    static const char *sec_names[] =
12921
0
    {
12922
0
      ".dynstr", ".dynsym", ".hash"
12923
0
    };
12924
0
    bfd_vma low, high;
12925
0
    unsigned int i, c;
12926
0
    struct elf_segment_map *n;
12927
12928
0
    low = m->sections[0]->vma;
12929
0
    high = low + m->sections[0]->size;
12930
0
    for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12931
0
      {
12932
0
        s = bfd_get_section_by_name (abfd, sec_names[i]);
12933
0
        if (s != NULL && (s->flags & SEC_LOAD) != 0)
12934
0
    {
12935
0
      bfd_size_type sz;
12936
12937
0
      if (low > s->vma)
12938
0
        continue;
12939
0
      sz = s->size;
12940
0
      if (high < s->vma + sz)
12941
0
        high = s->vma + sz;
12942
0
    }
12943
0
      }
12944
12945
0
    c = 0;
12946
0
    for (s = abfd->sections; s != NULL; s = s->next)
12947
0
      if ((s->flags & SEC_LOAD) != 0
12948
0
    && s->vma >= low
12949
0
    && s->vma + s->size <= high)
12950
0
        ++c;
12951
12952
0
    amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12953
0
    n = bfd_zalloc (abfd, amt);
12954
0
    if (n == NULL)
12955
0
      return false;
12956
0
    *n = *m;
12957
0
    n->count = c;
12958
12959
0
    i = 0;
12960
0
    for (s = abfd->sections; s != NULL; s = s->next)
12961
0
      {
12962
0
        if ((s->flags & SEC_LOAD) != 0
12963
0
      && s->vma >= low
12964
0
      && s->vma + s->size <= high)
12965
0
    {
12966
0
      n->sections[i] = s;
12967
0
      ++i;
12968
0
    }
12969
0
      }
12970
12971
0
    *pm = n;
12972
0
  }
12973
25
    }
12974
12975
  /* Allocate a spare program header in dynamic objects so that tools
12976
     like the prelinker can add an extra PT_LOAD entry.
12977
12978
     If the prelinker needs to make room for a new PT_LOAD entry, its
12979
     standard procedure is to move the first (read-only) sections into
12980
     the new (writable) segment.  However, the MIPS ABI requires
12981
     .dynamic to be in a read-only segment, and the section will often
12982
     start within sizeof (ElfNN_Phdr) bytes of the last program header.
12983
12984
     Although the prelinker could in principle move .dynamic to a
12985
     writable segment, it seems better to allocate a spare program
12986
     header instead, and avoid the need to move any sections.
12987
     There is a long tradition of allocating spare dynamic tags,
12988
     so allocating a spare program header seems like a natural
12989
     extension.
12990
12991
     If INFO is NULL, we may be copying an already prelinked binary
12992
     with objcopy or strip, so do not add this header.  */
12993
56
  if (info != NULL
12994
0
      && !SGI_COMPAT (abfd)
12995
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12996
0
    {
12997
0
      for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12998
0
  if ((*pm)->p_type == PT_NULL)
12999
0
    break;
13000
0
      if (*pm == NULL)
13001
0
  {
13002
0
    m = bfd_zalloc (abfd, sizeof (*m));
13003
0
    if (m == NULL)
13004
0
      return false;
13005
13006
0
    m->p_type = PT_NULL;
13007
0
    *pm = m;
13008
0
  }
13009
0
    }
13010
13011
56
  return true;
13012
56
}
13013

13014
/* Return the section that should be marked against GC for a given
13015
   relocation.  */
13016
13017
asection *
13018
_bfd_mips_elf_gc_mark_hook (asection *sec,
13019
          struct bfd_link_info *info,
13020
          struct elf_reloc_cookie *cookie,
13021
          struct elf_link_hash_entry *h,
13022
          unsigned int symndx)
13023
0
{
13024
  /* ??? Do mips16 stub sections need to be handled special?  */
13025
13026
0
  if (h != NULL)
13027
0
    switch (ELF_R_TYPE (sec->owner, cookie->rel->r_info))
13028
0
      {
13029
0
      case R_MIPS_GNU_VTINHERIT:
13030
0
      case R_MIPS_GNU_VTENTRY:
13031
0
  return NULL;
13032
0
      }
13033
13034
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
13035
0
}
13036
13037
/* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
13038
13039
bool
13040
_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13041
              elf_gc_mark_hook_fn gc_mark_hook)
13042
0
{
13043
0
  bfd *sub;
13044
13045
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
13046
13047
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13048
0
    {
13049
0
      asection *o;
13050
13051
0
      if (! is_mips_elf (sub))
13052
0
  continue;
13053
13054
0
      for (o = sub->sections; o != NULL; o = o->next)
13055
0
  if (!o->gc_mark
13056
0
      && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
13057
0
    {
13058
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13059
0
        return false;
13060
0
    }
13061
0
    }
13062
13063
0
  return true;
13064
0
}
13065

13066
/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
13067
   hiding the old indirect symbol.  Process additional relocation
13068
   information.  Also called for weakdefs, in which case we just let
13069
   _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
13070
13071
void
13072
_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
13073
            struct elf_link_hash_entry *dir,
13074
            struct elf_link_hash_entry *ind)
13075
0
{
13076
0
  struct mips_elf_link_hash_entry *dirmips, *indmips;
13077
13078
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
13079
13080
0
  dirmips = (struct mips_elf_link_hash_entry *) dir;
13081
0
  indmips = (struct mips_elf_link_hash_entry *) ind;
13082
  /* Any absolute non-dynamic relocations against an indirect or weak
13083
     definition will be against the target symbol.  */
13084
0
  if (indmips->has_static_relocs)
13085
0
    dirmips->has_static_relocs = true;
13086
13087
0
  if (ind->root.type != bfd_link_hash_indirect)
13088
0
    return;
13089
13090
0
  dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
13091
0
  if (indmips->readonly_reloc)
13092
0
    dirmips->readonly_reloc = true;
13093
0
  if (indmips->no_fn_stub)
13094
0
    dirmips->no_fn_stub = true;
13095
0
  if (indmips->fn_stub)
13096
0
    {
13097
0
      dirmips->fn_stub = indmips->fn_stub;
13098
0
      indmips->fn_stub = NULL;
13099
0
    }
13100
0
  if (indmips->need_fn_stub)
13101
0
    {
13102
0
      dirmips->need_fn_stub = true;
13103
0
      indmips->need_fn_stub = false;
13104
0
    }
13105
0
  if (indmips->call_stub)
13106
0
    {
13107
0
      dirmips->call_stub = indmips->call_stub;
13108
0
      indmips->call_stub = NULL;
13109
0
    }
13110
0
  if (indmips->call_fp_stub)
13111
0
    {
13112
0
      dirmips->call_fp_stub = indmips->call_fp_stub;
13113
0
      indmips->call_fp_stub = NULL;
13114
0
    }
13115
0
  if (indmips->global_got_area < dirmips->global_got_area)
13116
0
    dirmips->global_got_area = indmips->global_got_area;
13117
0
  if (indmips->global_got_area < GGA_NONE)
13118
0
    indmips->global_got_area = GGA_NONE;
13119
0
  if (indmips->has_nonpic_branches)
13120
0
    dirmips->has_nonpic_branches = true;
13121
0
}
13122
13123
/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13124
   to hide it.  It has to remain global (it will also be protected) so as to
13125
   be assigned a global GOT entry, which will then remain unchanged at load
13126
   time.  */
13127
13128
void
13129
_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13130
         struct elf_link_hash_entry *entry,
13131
         bool force_local)
13132
0
{
13133
0
  struct mips_elf_link_hash_table *htab;
13134
13135
0
  htab = mips_elf_hash_table (info);
13136
0
  BFD_ASSERT (htab != NULL);
13137
0
  if (htab->use_absolute_zero
13138
0
      && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13139
0
    return;
13140
13141
0
  _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13142
0
}
13143

13144
0
#define PDR_SIZE 32
13145
13146
bool
13147
_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13148
          struct bfd_link_info *info)
13149
0
{
13150
0
  asection *o;
13151
0
  bool ret = false;
13152
0
  unsigned char *tdata;
13153
0
  size_t i, skip;
13154
13155
0
  o = bfd_get_section_by_name (abfd, ".pdr");
13156
0
  if (! o)
13157
0
    return false;
13158
0
  if (o->size == 0)
13159
0
    return false;
13160
0
  if (o->size % PDR_SIZE != 0)
13161
0
    return false;
13162
0
  if (o->output_section != NULL
13163
0
      && bfd_is_abs_section (o->output_section))
13164
0
    return false;
13165
13166
0
  tdata = bfd_zmalloc (o->size / PDR_SIZE);
13167
0
  if (! tdata)
13168
0
    return false;
13169
13170
0
  cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13171
0
              info->keep_memory);
13172
0
  if (!cookie->rels)
13173
0
    {
13174
0
      free (tdata);
13175
0
      return false;
13176
0
    }
13177
13178
0
  cookie->rel = cookie->rels;
13179
0
  cookie->relend = cookie->rels + o->reloc_count;
13180
13181
0
  for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13182
0
    {
13183
0
      if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13184
0
  {
13185
0
    tdata[i] = 1;
13186
0
    skip ++;
13187
0
  }
13188
0
    }
13189
13190
0
  if (skip != 0)
13191
0
    {
13192
0
      mips_elf_section_data (o)->u.tdata = tdata;
13193
0
      if (o->rawsize == 0)
13194
0
  o->rawsize = o->size;
13195
0
      o->size -= skip * PDR_SIZE;
13196
0
      ret = true;
13197
0
    }
13198
0
  else
13199
0
    free (tdata);
13200
13201
0
  if (! info->keep_memory)
13202
0
    free (cookie->rels);
13203
13204
0
  return ret;
13205
0
}
13206
13207
bool
13208
_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13209
0
{
13210
0
  if (strcmp (sec->name, ".pdr") == 0)
13211
0
    return true;
13212
0
  return false;
13213
0
}
13214
13215
bool
13216
_bfd_mips_elf_write_section (bfd *output_bfd,
13217
           struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13218
           asection *sec, bfd_byte *contents)
13219
0
{
13220
0
  bfd_byte *to, *from, *end;
13221
0
  int i;
13222
13223
0
  if (strcmp (sec->name, ".pdr") != 0)
13224
0
    return false;
13225
13226
0
  if (mips_elf_section_data (sec)->u.tdata == NULL)
13227
0
    return false;
13228
13229
0
  to = contents;
13230
0
  end = contents + sec->size;
13231
0
  for (from = contents, i = 0;
13232
0
       from < end;
13233
0
       from += PDR_SIZE, i++)
13234
0
    {
13235
0
      if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13236
0
  continue;
13237
0
      if (to != from)
13238
0
  memcpy (to, from, PDR_SIZE);
13239
0
      to += PDR_SIZE;
13240
0
    }
13241
0
  bfd_set_section_contents (output_bfd, sec->output_section, contents,
13242
0
          sec->output_offset, sec->size);
13243
0
  return true;
13244
0
}
13245

13246
/* microMIPS code retains local labels for linker relaxation.  Omit them
13247
   from output by default for clarity.  */
13248
13249
bool
13250
_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13251
4.53k
{
13252
4.53k
  return _bfd_elf_is_local_label_name (abfd, sym->name);
13253
4.53k
}
13254
13255
/* Helper for `mips_elf_free_hi16_list'.  Resolve an orphan REL high-part
13256
   relocation according to HI.  Return TRUE if succeeded, otherwise FALSE.  */
13257
13258
static bool
13259
_bfd_mips_elf_orphan_shr16_reloc (bfd *abfd, asection *sec,
13260
          struct mips_hi16 *hi,
13261
          struct bfd_link_info *info)
13262
4
{
13263
4
  asymbol *symbol = *hi->rel.sym_ptr_ptr;
13264
4
  const char *name = hi->rel.howto->name;
13265
4
  char *error_message = NULL;
13266
4
  bfd_reloc_status_type r;
13267
13268
4
  r = _bfd_mips_elf_shr16_reloc (abfd, &hi->rel, symbol, hi->data,
13269
4
         sec, hi->output_bfd, &error_message);
13270
4
  if (r == bfd_reloc_ok)
13271
4
    {
13272
4
      if (info != NULL)
13273
4
  {
13274
    /* xgettext:c-format */
13275
4
    error_message = bfd_asprintf (_("can't find matching LO16 reloc"
13276
4
            " against `%s' for %s at %#" PRIx64
13277
4
            " in section `%s'"),
13278
4
          symbol->name, name,
13279
4
          (uint64_t) hi->rel.address, sec->name);
13280
4
    if (error_message == NULL)
13281
0
      return false;
13282
4
    info->callbacks->warning
13283
4
      (info, error_message, symbol->name, sec->owner, sec,
13284
4
       hi->rel.address);
13285
4
  }
13286
4
      return true;
13287
4
    }
13288
13289
0
  if (info != NULL)
13290
0
    _bfd_link_reloc_status_error (abfd, info, sec, &hi->rel, error_message, r);
13291
0
  else
13292
0
    _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): %s relocation error"),
13293
0
      abfd, sec, (uint64_t) hi->rel.address,
13294
0
      hi->rel.howto->name);
13295
0
  bfd_set_error (bfd_error_bad_value);
13296
0
  return false;
13297
4
}
13298
13299
/* Resolve any outstanding orphan REL high-part relocations if INSTALL
13300
   is TRUE, and release their occupied memory.  */
13301
13302
static bool
13303
mips_elf_free_hi16_list (bfd *abfd, asection *sec, bool install,
13304
       struct bfd_link_info *info)
13305
48.8k
{
13306
48.8k
  bfd_error_type error_tag = bfd_get_error ();
13307
48.8k
  struct _mips_elf_section_data *sdata;
13308
48.8k
  bool status = true;
13309
13310
48.8k
  BFD_ASSERT (is_mips_elf (abfd));
13311
48.8k
  sdata = mips_elf_section_data (sec);
13312
48.8k
  while (sdata->mips_hi16_list != NULL)
13313
10
    {
13314
10
      struct mips_hi16 *hi = sdata->mips_hi16_list;
13315
13316
10
      if (install)
13317
4
  status &= _bfd_mips_elf_orphan_shr16_reloc (abfd, sec, hi, info);
13318
10
      if (!status && error_tag == bfd_error_no_error)
13319
0
  error_tag = bfd_get_error ();
13320
13321
10
      sdata->mips_hi16_list = hi->next;
13322
10
      free (hi);
13323
10
    }
13324
13325
48.8k
  bfd_set_error (error_tag);
13326
48.8k
  return status;
13327
48.8k
}
13328
13329
/* Resolve any outstanding orphan REL high-part relocations before
13330
   calling the generic BFD handler.  */
13331
13332
bool
13333
_bfd_mips_elf_finalize_section_relocs (bfd *abfd, asection *asect,
13334
               arelent **location, unsigned int count)
13335
210
{
13336
210
  if (!mips_elf_free_hi16_list (abfd, asect, true, NULL))
13337
0
    return false;
13338
210
  return _bfd_generic_finalize_section_relocs (abfd, asect, location, count);
13339
210
}
13340
13341
bool
13342
_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13343
         asection *section, bfd_vma offset,
13344
         const char **filename_ptr,
13345
         const char **functionname_ptr,
13346
         unsigned int *line_ptr,
13347
         unsigned int *discriminator_ptr)
13348
5.90k
{
13349
5.90k
  asection *msec;
13350
13351
5.90k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13352
5.90k
             filename_ptr, functionname_ptr,
13353
5.90k
             line_ptr, discriminator_ptr,
13354
5.90k
             dwarf_debug_sections,
13355
5.90k
             &elf_tdata (abfd)->dwarf2_find_line_info)
13356
5.90k
      == 1)
13357
26
    return true;
13358
13359
5.88k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13360
5.88k
             filename_ptr, functionname_ptr,
13361
5.88k
             line_ptr))
13362
0
    {
13363
0
      if (!*functionname_ptr)
13364
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
13365
0
        *filename_ptr ? NULL : filename_ptr,
13366
0
        functionname_ptr);
13367
0
      return true;
13368
0
    }
13369
13370
5.88k
  msec = bfd_get_section_by_name (abfd, ".mdebug");
13371
5.88k
  if (msec != NULL)
13372
3.45k
    {
13373
3.45k
      flagword origflags;
13374
3.45k
      struct mips_elf_find_line *fi;
13375
3.45k
      const struct ecoff_debug_swap * const swap =
13376
3.45k
  get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13377
13378
      /* If we are called during a link, mips_elf_final_link may have
13379
   cleared the SEC_HAS_CONTENTS field.  We force it back on here
13380
   if appropriate (which it normally will be).  */
13381
3.45k
      origflags = msec->flags;
13382
3.45k
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13383
3.40k
  msec->flags |= SEC_HAS_CONTENTS;
13384
13385
3.45k
      fi = mips_elf_tdata (abfd)->find_line_info;
13386
3.45k
      if (fi == NULL)
13387
2.57k
  {
13388
2.57k
    bfd_size_type external_fdr_size;
13389
2.57k
    char *fraw_src;
13390
2.57k
    char *fraw_end;
13391
2.57k
    struct fdr *fdr_ptr;
13392
2.57k
    bfd_size_type amt = sizeof (struct mips_elf_find_line);
13393
13394
2.57k
    fi = bfd_zalloc (abfd, amt);
13395
2.57k
    if (fi == NULL)
13396
0
      {
13397
0
        msec->flags = origflags;
13398
0
        return false;
13399
0
      }
13400
13401
2.57k
    if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13402
2.41k
      {
13403
2.41k
        msec->flags = origflags;
13404
2.41k
        return false;
13405
2.41k
      }
13406
13407
    /* Swap in the FDR information.  */
13408
160
    amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13409
160
    fi->d.fdr = bfd_alloc (abfd, amt);
13410
160
    if (fi->d.fdr == NULL)
13411
0
      {
13412
0
        _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13413
0
        msec->flags = origflags;
13414
0
        return false;
13415
0
      }
13416
160
    external_fdr_size = swap->external_fdr_size;
13417
160
    fdr_ptr = fi->d.fdr;
13418
160
    fraw_src = (char *) fi->d.external_fdr;
13419
160
    fraw_end = (fraw_src
13420
160
          + fi->d.symbolic_header.ifdMax * external_fdr_size);
13421
14.1k
    for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13422
14.0k
      (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13423
13424
160
    mips_elf_tdata (abfd)->find_line_info = fi;
13425
160
  }
13426
13427
1.04k
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13428
1.04k
          &fi->i, filename_ptr, functionname_ptr,
13429
1.04k
          line_ptr))
13430
572
  {
13431
572
    msec->flags = origflags;
13432
572
    return true;
13433
572
  }
13434
13435
475
      msec->flags = origflags;
13436
475
    }
13437
13438
  /* Fall back on the generic ELF find_nearest_line routine.  */
13439
13440
2.89k
  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13441
2.89k
             filename_ptr, functionname_ptr,
13442
2.89k
             line_ptr, discriminator_ptr);
13443
5.88k
}
13444
13445
bool
13446
_bfd_mips_elf_find_inliner_info (bfd *abfd,
13447
         const char **filename_ptr,
13448
         const char **functionname_ptr,
13449
         unsigned int *line_ptr)
13450
0
{
13451
0
  bool found;
13452
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13453
0
           functionname_ptr, line_ptr,
13454
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
13455
0
  return found;
13456
0
}
13457
13458

13459
/* When are writing out the .options or .MIPS.options section,
13460
   remember the bytes we are writing out, so that we can install the
13461
   GP value in the section_processing routine.  */
13462
13463
bool
13464
_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13465
            const void *location,
13466
            file_ptr offset, bfd_size_type count)
13467
145
{
13468
145
  if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13469
0
    {
13470
0
      bfd_byte *c;
13471
13472
0
      if (elf_section_data (section) == NULL)
13473
0
  {
13474
0
    size_t amt = sizeof (struct bfd_elf_section_data);
13475
0
    section->used_by_bfd = bfd_zalloc (abfd, amt);
13476
0
    if (elf_section_data (section) == NULL)
13477
0
      return false;
13478
0
  }
13479
0
      c = mips_elf_section_data (section)->u.tdata;
13480
0
      if (c == NULL)
13481
0
  {
13482
0
    c = bfd_zalloc (abfd, section->size);
13483
0
    if (c == NULL)
13484
0
      return false;
13485
0
    mips_elf_section_data (section)->u.tdata = c;
13486
0
  }
13487
13488
0
      memcpy (c + offset, location, count);
13489
0
    }
13490
13491
145
  return _bfd_elf_set_section_contents (abfd, section, location, offset,
13492
145
          count);
13493
145
}
13494
13495
/* This is almost identical to bfd_generic_get_... except that some
13496
   MIPS relocations need to be handled specially.  Sigh.  */
13497
13498
bfd_byte *
13499
_bfd_elf_mips_get_relocated_section_contents
13500
  (bfd *abfd,
13501
   struct bfd_link_info *link_info,
13502
   const struct bfd_link_order *link_order,
13503
   bfd_byte *data,
13504
   bool relocatable,
13505
   asymbol **symbols)
13506
1.37k
{
13507
1.37k
  bfd *input_bfd = link_order->u.indirect.section->owner;
13508
1.37k
  asection *input_section = link_order->u.indirect.section;
13509
1.37k
  long reloc_size;
13510
1.37k
  arelent **reloc_vector;
13511
1.37k
  long reloc_count = 0;
13512
1.37k
  bool install = true;
13513
13514
1.37k
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13515
1.37k
  if (reloc_size < 0)
13516
53
    return NULL;
13517
13518
  /* Read in the section.  */
13519
1.32k
  bfd_byte *orig_data = data;
13520
1.32k
  if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13521
7
    return NULL;
13522
13523
1.31k
  if (data == NULL)
13524
21
    return NULL;
13525
13526
1.29k
  if (reloc_size == 0)
13527
0
    return data;
13528
13529
1.29k
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13530
1.29k
  if (reloc_vector != NULL)
13531
1.29k
    reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13532
1.29k
            reloc_vector, symbols);
13533
13534
1.29k
  if (reloc_vector == NULL || reloc_count < 0)
13535
998
    {
13536
998
      install = false;
13537
998
      if (orig_data == NULL)
13538
5
  free (data);
13539
998
      data = NULL;
13540
998
      goto out;
13541
998
    }
13542
13543
295
  if (reloc_count > 0)
13544
280
    {
13545
280
      arelent **parent;
13546
      /* for mips */
13547
280
      int gp_found;
13548
280
      bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
13549
13550
280
      {
13551
280
  struct bfd_hash_entry *h;
13552
280
  struct bfd_link_hash_entry *lh;
13553
  /* Skip all this stuff if we aren't mixing formats.  */
13554
280
  if (abfd && input_bfd
13555
280
      && abfd->xvec == input_bfd->xvec)
13556
280
    lh = 0;
13557
0
  else
13558
0
    {
13559
0
      h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13560
0
      lh = (struct bfd_link_hash_entry *) h;
13561
0
    }
13562
280
      lookup:
13563
280
  if (lh)
13564
0
    {
13565
0
      switch (lh->type)
13566
0
        {
13567
0
        case bfd_link_hash_undefined:
13568
0
        case bfd_link_hash_undefweak:
13569
0
        case bfd_link_hash_common:
13570
0
    gp_found = 0;
13571
0
    break;
13572
0
        case bfd_link_hash_defined:
13573
0
        case bfd_link_hash_defweak:
13574
0
    gp_found = 1;
13575
0
    gp = lh->u.def.value;
13576
0
    break;
13577
0
        case bfd_link_hash_indirect:
13578
0
        case bfd_link_hash_warning:
13579
0
    lh = lh->u.i.link;
13580
    /* @@FIXME  ignoring warning for now */
13581
0
    goto lookup;
13582
0
        case bfd_link_hash_new:
13583
0
        default:
13584
0
    abort ();
13585
0
        }
13586
0
    }
13587
280
  else
13588
280
    gp_found = 0;
13589
280
      }
13590
      /* end mips */
13591
13592
1.34k
      for (parent = reloc_vector; *parent != NULL; parent++)
13593
1.27k
  {
13594
1.27k
    char *error_message = NULL;
13595
1.27k
    asymbol *symbol;
13596
1.27k
    bfd_reloc_status_type r;
13597
13598
1.27k
    symbol = *(*parent)->sym_ptr_ptr;
13599
    /* PR ld/19628: A specially crafted input file
13600
       can result in a NULL symbol pointer here.  */
13601
1.27k
    if (symbol == NULL)
13602
0
      {
13603
0
        link_info->callbacks->einfo
13604
    /* xgettext:c-format */
13605
0
    (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13606
0
     abfd, input_section, (* parent)->address);
13607
0
        install = false;
13608
0
        goto out;
13609
0
      }
13610
13611
    /* Zap reloc field when the symbol is from a discarded
13612
       section, ignoring any addend.  Do the same when called
13613
       from bfd_simple_get_relocated_section_contents for
13614
       undefined symbols in debug sections.  This is to keep
13615
       debug info reasonably sane, in particular so that
13616
       DW_FORM_ref_addr to another file's .debug_info isn't
13617
       confused with an offset into the current file's
13618
       .debug_info.  */
13619
1.27k
    if ((symbol->section != NULL && discarded_section (symbol->section))
13620
1.27k
        || (symbol->section == bfd_und_section_ptr
13621
46
      && (input_section->flags & SEC_DEBUGGING) != 0
13622
36
      && link_info->input_bfds == link_info->output_bfd))
13623
36
      {
13624
36
        bfd_vma off;
13625
36
        static reloc_howto_type none_howto
13626
36
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13627
36
       "unused", false, 0, 0, false);
13628
13629
36
        off = ((*parent)->address
13630
36
         * bfd_octets_per_byte (input_bfd, input_section));
13631
36
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13632
36
           input_section, data, off);
13633
36
        (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13634
36
        (*parent)->addend = 0;
13635
36
        (*parent)->howto = &none_howto;
13636
36
        r = bfd_reloc_ok;
13637
36
      }
13638
13639
    /* Specific to MIPS: Deal with relocation types that require
13640
       knowing the gp of the output bfd.  */
13641
13642
    /* If we've managed to find the gp and have a special
13643
       function for the relocation then go ahead, else default
13644
       to the generic handling.  */
13645
1.23k
    else if (gp_found
13646
0
       && ((*parent)->howto->special_function
13647
0
           == _bfd_mips_elf32_gprel16_reloc))
13648
0
      r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13649
0
                 input_section, relocatable,
13650
0
                 data, gp);
13651
1.23k
    else
13652
1.23k
      r = bfd_perform_relocation (input_bfd,
13653
1.23k
          *parent,
13654
1.23k
          data,
13655
1.23k
          input_section,
13656
1.23k
          relocatable ? abfd : NULL,
13657
1.23k
          &error_message);
13658
13659
1.27k
    if (relocatable)
13660
0
      {
13661
0
        asection *os = input_section->output_section;
13662
13663
        /* A partial link, so keep the relocs.  */
13664
0
        os->orelocation[os->reloc_count] = *parent;
13665
0
        os->reloc_count++;
13666
0
      }
13667
13668
1.27k
    if (r != bfd_reloc_ok)
13669
340
      {
13670
340
        _bfd_link_reloc_status_error (abfd, link_info, input_section,
13671
340
              *parent, error_message, r);
13672
340
        if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
13673
215
    {
13674
215
      install = false;
13675
215
      goto out;
13676
215
    }
13677
340
      }
13678
1.27k
  }
13679
280
    }
13680
13681
1.29k
 out:
13682
1.29k
  mips_elf_free_hi16_list (input_bfd, input_section, install, link_info);
13683
1.29k
  free (reloc_vector);
13684
1.29k
  return data;
13685
295
}
13686

13687
static bool
13688
mips_elf_relax_delete_bytes (bfd *abfd,
13689
           asection *sec, bfd_vma addr, int count)
13690
0
{
13691
0
  Elf_Internal_Shdr *symtab_hdr;
13692
0
  unsigned int sec_shndx;
13693
0
  bfd_byte *contents;
13694
0
  Elf_Internal_Rela *irel, *irelend;
13695
0
  Elf_Internal_Sym *isym;
13696
0
  Elf_Internal_Sym *isymend;
13697
0
  struct elf_link_hash_entry **sym_hashes;
13698
0
  struct elf_link_hash_entry **end_hashes;
13699
0
  struct elf_link_hash_entry **start_hashes;
13700
0
  unsigned int symcount;
13701
13702
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13703
0
  contents = elf_section_data (sec)->this_hdr.contents;
13704
13705
0
  irel = elf_section_data (sec)->relocs;
13706
0
  irelend = irel + sec->reloc_count;
13707
13708
  /* Actually delete the bytes.  */
13709
0
  memmove (contents + addr, contents + addr + count,
13710
0
     (size_t) (sec->size - addr - count));
13711
0
  sec->size -= count;
13712
13713
  /* Adjust all the relocs.  */
13714
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13715
0
    {
13716
      /* Get the new reloc address.  */
13717
0
      if (irel->r_offset > addr)
13718
0
  irel->r_offset -= count;
13719
0
    }
13720
13721
0
  BFD_ASSERT (addr % 2 == 0);
13722
0
  BFD_ASSERT (count % 2 == 0);
13723
13724
  /* Adjust the local symbols defined in this section.  */
13725
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13726
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13727
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13728
0
    if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13729
0
      isym->st_value -= count;
13730
13731
  /* Now adjust the global symbols defined in this section.  */
13732
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13733
0
        - symtab_hdr->sh_info);
13734
0
  sym_hashes = start_hashes = elf_sym_hashes (abfd);
13735
0
  end_hashes = sym_hashes + symcount;
13736
13737
0
  for (; sym_hashes < end_hashes; sym_hashes++)
13738
0
    {
13739
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
13740
13741
0
      if ((sym_hash->root.type == bfd_link_hash_defined
13742
0
     || sym_hash->root.type == bfd_link_hash_defweak)
13743
0
    && sym_hash->root.u.def.section == sec)
13744
0
  {
13745
0
    bfd_vma value = sym_hash->root.u.def.value;
13746
13747
0
    if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13748
0
      value &= MINUS_TWO;
13749
0
    if (value > addr)
13750
0
      sym_hash->root.u.def.value -= count;
13751
0
  }
13752
0
    }
13753
13754
0
  return true;
13755
0
}
13756
13757
13758
/* Opcodes needed for microMIPS relaxation as found in
13759
   opcodes/micromips-opc.c.  */
13760
13761
struct opcode_descriptor {
13762
  unsigned long match;
13763
  unsigned long mask;
13764
};
13765
13766
/* The $ra register aka $31.  */
13767
13768
0
#define RA 31
13769
13770
/* 32-bit instruction format register fields.  */
13771
13772
0
#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13773
0
#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13774
13775
/* Check if a 5-bit register index can be abbreviated to 3 bits.  */
13776
13777
#define OP16_VALID_REG(r) \
13778
0
  ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13779
13780
13781
/* 32-bit and 16-bit branches.  */
13782
13783
static const struct opcode_descriptor b_insns_32[] = {
13784
  { /* "b", "p",    */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13785
  { /* "b", "p",    */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13786
  { 0, 0 }  /* End marker for find_match().  */
13787
};
13788
13789
static const struct opcode_descriptor bc_insn_32 =
13790
  { /* "bc(1|2)(ft)", "N,p",  */ 0x42800000, 0xfec30000 };
13791
13792
static const struct opcode_descriptor bz_insn_32 =
13793
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13794
13795
static const struct opcode_descriptor bzal_insn_32 =
13796
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 };
13797
13798
static const struct opcode_descriptor beq_insn_32 =
13799
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13800
13801
static const struct opcode_descriptor b_insn_16 =
13802
  { /* "b", "mD",   */ 0xcc00,     0xfc00 };
13803
13804
static const struct opcode_descriptor bz_insn_16 =
13805
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 };
13806
13807
13808
/* 32-bit and 16-bit branch EQ and NE zero.  */
13809
13810
/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13811
   eq and second the ne.  This convention is used when replacing a
13812
   32-bit BEQ/BNE with the 16-bit version.  */
13813
13814
0
#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13815
13816
static const struct opcode_descriptor bz_rs_insns_32[] = {
13817
  { /* "beqz",  "s,p",    */ 0x94000000, 0xffe00000 },
13818
  { /* "bnez",  "s,p",    */ 0xb4000000, 0xffe00000 },
13819
  { 0, 0 }  /* End marker for find_match().  */
13820
};
13821
13822
static const struct opcode_descriptor bz_rt_insns_32[] = {
13823
  { /* "beqz",  "t,p",    */ 0x94000000, 0xfc01f000 },
13824
  { /* "bnez",  "t,p",    */ 0xb4000000, 0xfc01f000 },
13825
  { 0, 0 }  /* End marker for find_match().  */
13826
};
13827
13828
static const struct opcode_descriptor bzc_insns_32[] = {
13829
  { /* "beqzc", "s,p",    */ 0x40e00000, 0xffe00000 },
13830
  { /* "bnezc", "s,p",    */ 0x40a00000, 0xffe00000 },
13831
  { 0, 0 }  /* End marker for find_match().  */
13832
};
13833
13834
static const struct opcode_descriptor bz_insns_16[] = {
13835
  { /* "beqz",  "md,mE",  */ 0x8c00,     0xfc00 },
13836
  { /* "bnez",  "md,mE",  */ 0xac00,     0xfc00 },
13837
  { 0, 0 }  /* End marker for find_match().  */
13838
};
13839
13840
/* Switch between a 5-bit register index and its 3-bit shorthand.  */
13841
13842
0
#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13843
#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13844
13845
13846
/* 32-bit instructions with a delay slot.  */
13847
13848
static const struct opcode_descriptor jal_insn_32_bd16 =
13849
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 };
13850
13851
static const struct opcode_descriptor jal_insn_32_bd32 =
13852
  { /* "jal", "a",    */ 0xf4000000, 0xfc000000 };
13853
13854
static const struct opcode_descriptor jal_x_insn_32_bd32 =
13855
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 };
13856
13857
static const struct opcode_descriptor j_insn_32 =
13858
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 };
13859
13860
static const struct opcode_descriptor jalr_insn_32 =
13861
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff };
13862
13863
/* This table can be compacted, because no opcode replacement is made.  */
13864
13865
static const struct opcode_descriptor ds_insns_32_bd16[] = {
13866
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 },
13867
13868
  { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13869
  { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13870
13871
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13872
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13873
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 },
13874
  { 0, 0 }  /* End marker for find_match().  */
13875
};
13876
13877
/* This table can be compacted, because no opcode replacement is made.  */
13878
13879
static const struct opcode_descriptor ds_insns_32_bd32[] = {
13880
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 },
13881
13882
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff },
13883
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 },
13884
  { 0, 0 }  /* End marker for find_match().  */
13885
};
13886
13887
13888
/* 16-bit instructions with a delay slot.  */
13889
13890
static const struct opcode_descriptor jalr_insn_16_bd16 =
13891
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 };
13892
13893
static const struct opcode_descriptor jalr_insn_16_bd32 =
13894
  { /* "jalr",  "my,mj",  */ 0x45c0,     0xffe0 };
13895
13896
static const struct opcode_descriptor jr_insn_16 =
13897
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 };
13898
13899
0
#define JR16_REG(opcode) ((opcode) & 0x1f)
13900
13901
/* This table can be compacted, because no opcode replacement is made.  */
13902
13903
static const struct opcode_descriptor ds_insns_16_bd16[] = {
13904
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 },
13905
13906
  { /* "b", "mD",   */ 0xcc00,     0xfc00 },
13907
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 },
13908
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 },
13909
  { 0, 0 }  /* End marker for find_match().  */
13910
};
13911
13912
13913
/* LUI instruction.  */
13914
13915
static const struct opcode_descriptor lui_insn =
13916
 { /* "lui",  "s,u",    */ 0x41a00000, 0xffe00000 };
13917
13918
13919
/* ADDIU instruction.  */
13920
13921
static const struct opcode_descriptor addiu_insn =
13922
  { /* "addiu", "t,r,j",  */ 0x30000000, 0xfc000000 };
13923
13924
static const struct opcode_descriptor addiupc_insn =
13925
  { /* "addiu", "mb,$pc,mQ",  */ 0x78000000, 0xfc000000 };
13926
13927
#define ADDIUPC_REG_FIELD(r) \
13928
0
  (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13929
13930
13931
/* Relaxable instructions in a JAL delay slot: MOVE.  */
13932
13933
/* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
13934
   (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
13935
#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13936
#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13937
13938
#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13939
#define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
13940
13941
static const struct opcode_descriptor move_insns_32[] = {
13942
  { /* "move",  "d,s",    */ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
13943
  { /* "move",  "d,s",    */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13944
  { 0, 0 }  /* End marker for find_match().  */
13945
};
13946
13947
static const struct opcode_descriptor move_insn_16 =
13948
  { /* "move",  "mp,mj",  */ 0x0c00,     0xfc00 };
13949
13950
13951
/* NOP instructions.  */
13952
13953
static const struct opcode_descriptor nop_insn_32 =
13954
  { /* "nop", "",   */ 0x00000000, 0xffffffff };
13955
13956
static const struct opcode_descriptor nop_insn_16 =
13957
  { /* "nop", "",   */ 0x0c00,     0xffff };
13958
13959
13960
/* Instruction match support.  */
13961
13962
0
#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13963
13964
static int
13965
find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13966
0
{
13967
0
  unsigned long indx;
13968
13969
0
  for (indx = 0; insn[indx].mask != 0; indx++)
13970
0
    if (MATCH (opcode, insn[indx]))
13971
0
      return indx;
13972
13973
0
  return -1;
13974
0
}
13975
13976
13977
/* Branch and delay slot decoding support.  */
13978
13979
/* If PTR points to what *might* be a 16-bit branch or jump, then
13980
   return the minimum length of its delay slot, otherwise return 0.
13981
   Non-zero results are not definitive as we might be checking against
13982
   the second half of another instruction.  */
13983
13984
static int
13985
check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13986
0
{
13987
0
  unsigned long opcode;
13988
0
  int bdsize;
13989
13990
0
  opcode = bfd_get_16 (abfd, ptr);
13991
0
  if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13992
    /* 16-bit branch/jump with a 32-bit delay slot.  */
13993
0
    bdsize = 4;
13994
0
  else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13995
0
     || find_match (opcode, ds_insns_16_bd16) >= 0)
13996
    /* 16-bit branch/jump with a 16-bit delay slot.  */
13997
0
    bdsize = 2;
13998
0
  else
13999
    /* No delay slot.  */
14000
0
    bdsize = 0;
14001
14002
0
  return bdsize;
14003
0
}
14004
14005
/* If PTR points to what *might* be a 32-bit branch or jump, then
14006
   return the minimum length of its delay slot, otherwise return 0.
14007
   Non-zero results are not definitive as we might be checking against
14008
   the second half of another instruction.  */
14009
14010
static int
14011
check_br32_dslot (bfd *abfd, bfd_byte *ptr)
14012
0
{
14013
0
  unsigned long opcode;
14014
0
  int bdsize;
14015
14016
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
14017
0
  if (find_match (opcode, ds_insns_32_bd32) >= 0)
14018
    /* 32-bit branch/jump with a 32-bit delay slot.  */
14019
0
    bdsize = 4;
14020
0
  else if (find_match (opcode, ds_insns_32_bd16) >= 0)
14021
    /* 32-bit branch/jump with a 16-bit delay slot.  */
14022
0
    bdsize = 2;
14023
0
  else
14024
    /* No delay slot.  */
14025
0
    bdsize = 0;
14026
14027
0
  return bdsize;
14028
0
}
14029
14030
/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
14031
   that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
14032
14033
static bool
14034
check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
14035
0
{
14036
0
  unsigned long opcode;
14037
14038
0
  opcode = bfd_get_16 (abfd, ptr);
14039
0
  if (MATCH (opcode, b_insn_16)
14040
            /* B16  */
14041
0
      || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
14042
            /* JR16  */
14043
0
      || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
14044
            /* BEQZ16, BNEZ16  */
14045
0
      || (MATCH (opcode, jalr_insn_16_bd32)
14046
            /* JALR16  */
14047
0
    && reg != JR16_REG (opcode) && reg != RA))
14048
0
    return true;
14049
14050
0
  return false;
14051
0
}
14052
14053
/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
14054
   then return TRUE, otherwise FALSE.  */
14055
14056
static bool
14057
check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
14058
0
{
14059
0
  unsigned long opcode;
14060
14061
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
14062
0
  if (MATCH (opcode, j_insn_32)
14063
            /* J  */
14064
0
      || MATCH (opcode, bc_insn_32)
14065
            /* BC1F, BC1T, BC2F, BC2T  */
14066
0
      || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
14067
            /* JAL, JALX  */
14068
0
      || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
14069
            /* BGEZ, BGTZ, BLEZ, BLTZ  */
14070
0
      || (MATCH (opcode, bzal_insn_32)
14071
            /* BGEZAL, BLTZAL  */
14072
0
    && reg != OP32_SREG (opcode) && reg != RA)
14073
0
      || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
14074
            /* JALR, JALR.HB, BEQ, BNE  */
14075
0
    && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
14076
0
    return true;
14077
14078
0
  return false;
14079
0
}
14080
14081
/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
14082
   IRELEND) at OFFSET indicate that there must be a compact branch there,
14083
   then return TRUE, otherwise FALSE.  */
14084
14085
static bool
14086
check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
14087
         const Elf_Internal_Rela *internal_relocs,
14088
         const Elf_Internal_Rela *irelend)
14089
0
{
14090
0
  const Elf_Internal_Rela *irel;
14091
0
  unsigned long opcode;
14092
14093
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
14094
0
  if (find_match (opcode, bzc_insns_32) < 0)
14095
0
    return false;
14096
14097
0
  for (irel = internal_relocs; irel < irelend; irel++)
14098
0
    if (irel->r_offset == offset
14099
0
  && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
14100
0
      return true;
14101
14102
0
  return false;
14103
0
}
14104
14105
/* Bitsize checking.  */
14106
#define IS_BITSIZE(val, N)            \
14107
0
  (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))   \
14108
0
    - (1ULL << ((N) - 1))) == (val))
14109
14110

14111
bool
14112
_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
14113
           struct bfd_link_info *link_info,
14114
           bool *again)
14115
0
{
14116
0
  bool insn32 = mips_elf_hash_table (link_info)->insn32;
14117
0
  Elf_Internal_Shdr *symtab_hdr;
14118
0
  Elf_Internal_Rela *internal_relocs;
14119
0
  Elf_Internal_Rela *irel, *irelend;
14120
0
  bfd_byte *contents = NULL;
14121
0
  Elf_Internal_Sym *isymbuf = NULL;
14122
14123
  /* Assume nothing changes.  */
14124
0
  *again = false;
14125
14126
  /* We don't have to do anything for a relocatable link, if
14127
     this section does not have relocs, or if this is not a
14128
     code section.  */
14129
14130
0
  if (bfd_link_relocatable (link_info)
14131
0
      || sec->reloc_count == 0
14132
0
      || (sec->flags & SEC_RELOC) == 0
14133
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
14134
0
      || (sec->flags & SEC_CODE) == 0)
14135
0
    return true;
14136
14137
0
  symtab_hdr = &elf_symtab_hdr (abfd);
14138
14139
  /* Get a copy of the native relocations.  */
14140
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
14141
0
                 link_info->keep_memory);
14142
0
  if (internal_relocs == NULL)
14143
0
    goto error_return;
14144
14145
  /* Walk through them looking for relaxing opportunities.  */
14146
0
  irelend = internal_relocs + sec->reloc_count;
14147
0
  for (irel = internal_relocs; irel < irelend; irel++)
14148
0
    {
14149
0
      unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14150
0
      unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14151
0
      bool target_is_micromips_code_p;
14152
0
      unsigned long opcode;
14153
0
      bfd_vma symval;
14154
0
      bfd_vma pcrval;
14155
0
      bfd_byte *ptr;
14156
0
      int fndopc;
14157
14158
      /* The number of bytes to delete for relaxation and from where
14159
   to delete these bytes starting at irel->r_offset.  */
14160
0
      int delcnt = 0;
14161
0
      int deloff = 0;
14162
14163
      /* If this isn't something that can be relaxed, then ignore
14164
   this reloc.  */
14165
0
      if (r_type != R_MICROMIPS_HI16
14166
0
    && r_type != R_MICROMIPS_PC16_S1
14167
0
    && r_type != R_MICROMIPS_26_S1)
14168
0
  continue;
14169
14170
      /* Get the section contents if we haven't done so already.  */
14171
0
      if (contents == NULL)
14172
0
  {
14173
    /* Get cached copy if it exists.  */
14174
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
14175
0
      contents = elf_section_data (sec)->this_hdr.contents;
14176
    /* Go get them off disk.  */
14177
0
    else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14178
0
      goto error_return;
14179
0
  }
14180
0
      ptr = contents + irel->r_offset;
14181
14182
      /* Read this BFD's local symbols if we haven't done so already.  */
14183
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14184
0
  {
14185
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14186
0
    if (isymbuf == NULL)
14187
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14188
0
              symtab_hdr->sh_info, 0,
14189
0
              NULL, NULL, NULL);
14190
0
    if (isymbuf == NULL)
14191
0
      goto error_return;
14192
0
  }
14193
14194
      /* Get the value of the symbol referred to by the reloc.  */
14195
0
      if (r_symndx < symtab_hdr->sh_info)
14196
0
  {
14197
    /* A local symbol.  */
14198
0
    Elf_Internal_Sym *isym;
14199
0
    asection *sym_sec;
14200
14201
0
    isym = isymbuf + r_symndx;
14202
0
    if (isym->st_shndx == SHN_UNDEF)
14203
0
      sym_sec = bfd_und_section_ptr;
14204
0
    else if (isym->st_shndx == SHN_ABS)
14205
0
      sym_sec = bfd_abs_section_ptr;
14206
0
    else if (isym->st_shndx == SHN_COMMON)
14207
0
      sym_sec = bfd_com_section_ptr;
14208
0
    else
14209
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14210
0
    symval = (isym->st_value
14211
0
        + sym_sec->output_section->vma
14212
0
        + sym_sec->output_offset);
14213
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14214
0
  }
14215
0
      else
14216
0
  {
14217
0
    unsigned long indx;
14218
0
    struct elf_link_hash_entry *h;
14219
14220
    /* An external symbol.  */
14221
0
    indx = r_symndx - symtab_hdr->sh_info;
14222
0
    h = elf_sym_hashes (abfd)[indx];
14223
0
    BFD_ASSERT (h != NULL);
14224
14225
0
    if (h->root.type != bfd_link_hash_defined
14226
0
        && h->root.type != bfd_link_hash_defweak)
14227
      /* This appears to be a reference to an undefined
14228
         symbol.  Just ignore it -- it will be caught by the
14229
         regular reloc processing.  */
14230
0
      continue;
14231
14232
0
    symval = (h->root.u.def.value
14233
0
        + h->root.u.def.section->output_section->vma
14234
0
        + h->root.u.def.section->output_offset);
14235
0
    target_is_micromips_code_p = (!h->needs_plt
14236
0
          && ELF_ST_IS_MICROMIPS (h->other));
14237
0
  }
14238
14239
14240
      /* For simplicity of coding, we are going to modify the
14241
   section contents, the section relocs, and the BFD symbol
14242
   table.  We must tell the rest of the code not to free up this
14243
   information.  It would be possible to instead create a table
14244
   of changes which have to be made, as is done in coff-mips.c;
14245
   that would be more work, but would require less memory when
14246
   the linker is run.  */
14247
14248
      /* Only 32-bit instructions relaxed.  */
14249
0
      if (irel->r_offset + 4 > sec->size)
14250
0
  continue;
14251
14252
0
      opcode = bfd_get_micromips_32 (abfd, ptr);
14253
14254
      /* This is the pc-relative distance from the instruction the
14255
   relocation is applied to, to the symbol referred.  */
14256
0
      pcrval = (symval
14257
0
    - (sec->output_section->vma + sec->output_offset)
14258
0
    - irel->r_offset);
14259
14260
      /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14261
   of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14262
   R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
14263
14264
     (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14265
14266
   where pcrval has first to be adjusted to apply against the LO16
14267
   location (we make the adjustment later on, when we have figured
14268
   out the offset).  */
14269
0
      if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14270
0
  {
14271
0
    bool bzc = false;
14272
0
    unsigned long nextopc;
14273
0
    unsigned long reg;
14274
0
    bfd_vma offset;
14275
14276
    /* Give up if the previous reloc was a HI16 against this symbol
14277
       too.  */
14278
0
    if (irel > internal_relocs
14279
0
        && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14280
0
        && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14281
0
      continue;
14282
14283
    /* Or if the next reloc is not a LO16 against this symbol.  */
14284
0
    if (irel + 1 >= irelend
14285
0
        || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14286
0
        || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14287
0
      continue;
14288
14289
    /* Or if the second next reloc is a LO16 against this symbol too.  */
14290
0
    if (irel + 2 >= irelend
14291
0
        && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14292
0
        && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14293
0
      continue;
14294
14295
    /* See if the LUI instruction *might* be in a branch delay slot.
14296
       We check whether what looks like a 16-bit branch or jump is
14297
       actually an immediate argument to a compact branch, and let
14298
       it through if so.  */
14299
0
    if (irel->r_offset >= 2
14300
0
        && check_br16_dslot (abfd, ptr - 2)
14301
0
        && !(irel->r_offset >= 4
14302
0
       && (bzc = check_relocated_bzc (abfd,
14303
0
              ptr - 4, irel->r_offset - 4,
14304
0
              internal_relocs, irelend))))
14305
0
      continue;
14306
0
    if (irel->r_offset >= 4
14307
0
        && !bzc
14308
0
        && check_br32_dslot (abfd, ptr - 4))
14309
0
      continue;
14310
14311
0
    reg = OP32_SREG (opcode);
14312
14313
    /* We only relax adjacent instructions or ones separated with
14314
       a branch or jump that has a delay slot.  The branch or jump
14315
       must not fiddle with the register used to hold the address.
14316
       Subtract 4 for the LUI itself.  */
14317
0
    offset = irel[1].r_offset - irel[0].r_offset;
14318
0
    switch (offset - 4)
14319
0
      {
14320
0
      case 0:
14321
0
        break;
14322
0
      case 2:
14323
0
        if (check_br16 (abfd, ptr + 4, reg))
14324
0
    break;
14325
0
        continue;
14326
0
      case 4:
14327
0
        if (check_br32 (abfd, ptr + 4, reg))
14328
0
    break;
14329
0
        continue;
14330
0
      default:
14331
0
        continue;
14332
0
      }
14333
14334
0
    nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14335
14336
    /* Give up unless the same register is used with both
14337
       relocations.  */
14338
0
    if (OP32_SREG (nextopc) != reg)
14339
0
      continue;
14340
14341
    /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14342
       and rounding up to take masking of the two LSBs into account.  */
14343
0
    pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14344
14345
    /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
14346
0
    if (IS_BITSIZE (symval, 16))
14347
0
      {
14348
        /* Fix the relocation's type.  */
14349
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14350
14351
        /* Instructions using R_MICROMIPS_LO16 have the base or
14352
     source register in bits 20:16.  This register becomes $0
14353
     (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
14354
0
        nextopc &= ~0x001f0000;
14355
0
        bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14356
0
        contents + irel[1].r_offset);
14357
0
      }
14358
14359
    /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14360
       We add 4 to take LUI deletion into account while checking
14361
       the PC-relative distance.  */
14362
0
    else if (symval % 4 == 0
14363
0
       && IS_BITSIZE (pcrval + 4, 25)
14364
0
       && MATCH (nextopc, addiu_insn)
14365
0
       && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14366
0
       && OP16_VALID_REG (OP32_TREG (nextopc)))
14367
0
      {
14368
        /* Fix the relocation's type.  */
14369
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14370
14371
        /* Replace ADDIU with the ADDIUPC version.  */
14372
0
        nextopc = (addiupc_insn.match
14373
0
       | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14374
14375
0
        bfd_put_micromips_32 (abfd, nextopc,
14376
0
            contents + irel[1].r_offset);
14377
0
      }
14378
14379
    /* Can't do anything, give up, sigh...  */
14380
0
    else
14381
0
      continue;
14382
14383
    /* Fix the relocation's type.  */
14384
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14385
14386
    /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
14387
0
    delcnt = 4;
14388
0
    deloff = 0;
14389
0
  }
14390
14391
      /* Compact branch relaxation -- due to the multitude of macros
14392
   employed by the compiler/assembler, compact branches are not
14393
   always generated.  Obviously, this can/will be fixed elsewhere,
14394
   but there is no drawback in double checking it here.  */
14395
0
      else if (r_type == R_MICROMIPS_PC16_S1
14396
0
         && irel->r_offset + 5 < sec->size
14397
0
         && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14398
0
       || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14399
0
         && ((!insn32
14400
0
        && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14401
0
          nop_insn_16) ? 2 : 0))
14402
0
       || (irel->r_offset + 7 < sec->size
14403
0
           && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14404
0
                 ptr + 4),
14405
0
             nop_insn_32) ? 4 : 0))))
14406
0
  {
14407
0
    unsigned long reg;
14408
14409
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14410
14411
    /* Replace BEQZ/BNEZ with the compact version.  */
14412
0
    opcode = (bzc_insns_32[fndopc].match
14413
0
        | BZC32_REG_FIELD (reg)
14414
0
        | (opcode & 0xffff));   /* Addend value.  */
14415
14416
0
    bfd_put_micromips_32 (abfd, opcode, ptr);
14417
14418
    /* Delete the delay slot NOP: two or four bytes from
14419
       irel->offset + 4; delcnt has already been set above.  */
14420
0
    deloff = 4;
14421
0
  }
14422
14423
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
14424
   to check the distance from the next instruction, so subtract 2.  */
14425
0
      else if (!insn32
14426
0
         && r_type == R_MICROMIPS_PC16_S1
14427
0
         && IS_BITSIZE (pcrval - 2, 11)
14428
0
         && find_match (opcode, b_insns_32) >= 0)
14429
0
  {
14430
    /* Fix the relocation's type.  */
14431
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14432
14433
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14434
0
    bfd_put_16 (abfd,
14435
0
          (b_insn_16.match
14436
0
           | (opcode & 0x3ff)),   /* Addend value.  */
14437
0
          ptr);
14438
14439
    /* Delete 2 bytes from irel->r_offset + 2.  */
14440
0
    delcnt = 2;
14441
0
    deloff = 2;
14442
0
  }
14443
14444
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
14445
   to check the distance from the next instruction, so subtract 2.  */
14446
0
      else if (!insn32
14447
0
         && r_type == R_MICROMIPS_PC16_S1
14448
0
         && IS_BITSIZE (pcrval - 2, 8)
14449
0
         && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14450
0
        && OP16_VALID_REG (OP32_SREG (opcode)))
14451
0
       || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14452
0
           && OP16_VALID_REG (OP32_TREG (opcode)))))
14453
0
  {
14454
0
    unsigned long reg;
14455
14456
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14457
14458
    /* Fix the relocation's type.  */
14459
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14460
14461
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14462
0
    bfd_put_16 (abfd,
14463
0
          (bz_insns_16[fndopc].match
14464
0
           | BZ16_REG_FIELD (reg)
14465
0
           | (opcode & 0x7f)),    /* Addend value.  */
14466
0
          ptr);
14467
14468
    /* Delete 2 bytes from irel->r_offset + 2.  */
14469
0
    delcnt = 2;
14470
0
    deloff = 2;
14471
0
  }
14472
14473
      /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
14474
0
      else if (!insn32
14475
0
         && r_type == R_MICROMIPS_26_S1
14476
0
         && target_is_micromips_code_p
14477
0
         && irel->r_offset + 7 < sec->size
14478
0
         && MATCH (opcode, jal_insn_32_bd32))
14479
0
  {
14480
0
    unsigned long n32opc;
14481
0
    bool relaxed = false;
14482
14483
0
    n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14484
14485
0
    if (MATCH (n32opc, nop_insn_32))
14486
0
      {
14487
        /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
14488
0
        bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14489
14490
0
        relaxed = true;
14491
0
      }
14492
0
    else if (find_match (n32opc, move_insns_32) >= 0)
14493
0
      {
14494
        /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
14495
0
        bfd_put_16 (abfd,
14496
0
        (move_insn_16.match
14497
0
         | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14498
0
         | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14499
0
        ptr + 4);
14500
14501
0
        relaxed = true;
14502
0
      }
14503
    /* Other 32-bit instructions relaxable to 16-bit
14504
       instructions will be handled here later.  */
14505
14506
0
    if (relaxed)
14507
0
      {
14508
        /* JAL with 32-bit delay slot that is changed to a JALS
14509
     with 16-bit delay slot.  */
14510
0
        bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14511
14512
        /* Delete 2 bytes from irel->r_offset + 6.  */
14513
0
        delcnt = 2;
14514
0
        deloff = 6;
14515
0
      }
14516
0
  }
14517
14518
0
      if (delcnt != 0)
14519
0
  {
14520
    /* Note that we've changed the relocs, section contents, etc.  */
14521
0
    elf_section_data (sec)->relocs = internal_relocs;
14522
0
    elf_section_data (sec)->this_hdr.contents = contents;
14523
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14524
14525
    /* Delete bytes depending on the delcnt and deloff.  */
14526
0
    if (!mips_elf_relax_delete_bytes (abfd, sec,
14527
0
              irel->r_offset + deloff, delcnt))
14528
0
      goto error_return;
14529
14530
    /* That will change things, so we should relax again.
14531
       Note that this is not required, and it may be slow.  */
14532
0
    *again = true;
14533
0
  }
14534
0
    }
14535
14536
0
  if (isymbuf != NULL
14537
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
14538
0
    {
14539
0
      if (! link_info->keep_memory)
14540
0
  free (isymbuf);
14541
0
      else
14542
0
  {
14543
    /* Cache the symbols for elf_link_input_bfd.  */
14544
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14545
0
  }
14546
0
    }
14547
14548
0
  if (contents != NULL
14549
0
      && elf_section_data (sec)->this_hdr.contents != contents)
14550
0
    {
14551
0
      if (! link_info->keep_memory)
14552
0
  free (contents);
14553
0
      else
14554
0
  {
14555
    /* Cache the section contents for elf_link_input_bfd.  */
14556
0
    elf_section_data (sec)->this_hdr.contents = contents;
14557
0
  }
14558
0
    }
14559
14560
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14561
0
    free (internal_relocs);
14562
14563
0
  return true;
14564
14565
0
 error_return:
14566
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
14567
0
    free (isymbuf);
14568
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
14569
0
    free (contents);
14570
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14571
0
    free (internal_relocs);
14572
14573
0
  return false;
14574
0
}
14575

14576
/* Create a MIPS ELF linker hash table.  */
14577
14578
struct bfd_link_hash_table *
14579
_bfd_mips_elf_link_hash_table_create (bfd *abfd)
14580
0
{
14581
0
  struct mips_elf_link_hash_table *ret;
14582
0
  size_t amt = sizeof (struct mips_elf_link_hash_table);
14583
14584
0
  ret = bfd_zmalloc (amt);
14585
0
  if (ret == NULL)
14586
0
    return NULL;
14587
14588
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14589
0
              mips_elf_link_hash_newfunc,
14590
0
              sizeof (struct mips_elf_link_hash_entry)))
14591
0
    {
14592
0
      free (ret);
14593
0
      return NULL;
14594
0
    }
14595
0
  ret->root.init_plt_refcount.plist = NULL;
14596
0
  ret->root.init_plt_offset.plist = NULL;
14597
14598
0
  return &ret->root.root;
14599
0
}
14600
14601
/* Likewise, but indicate that the target is VxWorks.  */
14602
14603
struct bfd_link_hash_table *
14604
_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14605
0
{
14606
0
  struct bfd_link_hash_table *ret;
14607
14608
0
  ret = _bfd_mips_elf_link_hash_table_create (abfd);
14609
0
  if (ret)
14610
0
    {
14611
0
      struct mips_elf_link_hash_table *htab;
14612
14613
0
      htab = (struct mips_elf_link_hash_table *) ret;
14614
0
      htab->use_plts_and_copy_relocs = true;
14615
0
    }
14616
0
  return ret;
14617
0
}
14618
14619
/* A function that the linker calls if we are allowed to use PLTs
14620
   and copy relocs.  */
14621
14622
void
14623
_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14624
0
{
14625
0
  mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14626
0
}
14627
14628
/* A function that the linker calls to select between all or only
14629
   32-bit microMIPS instructions, and between making or ignoring
14630
   branch relocation checks for invalid transitions between ISA modes.
14631
   Also record whether we have been configured for a GNU target.  */
14632
14633
void
14634
_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14635
          bool ignore_branch_isa,
14636
          bool gnu_target)
14637
0
{
14638
0
  mips_elf_hash_table (info)->insn32 = insn32;
14639
0
  mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14640
0
  mips_elf_hash_table (info)->gnu_target = gnu_target;
14641
0
}
14642
14643
/* A function that the linker calls to enable use of compact branches in
14644
   linker generated code for MIPSR6.  */
14645
14646
void
14647
_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14648
0
{
14649
0
  mips_elf_hash_table (info)->compact_branches = on;
14650
0
}
14651
14652

14653
/* Structure for saying that BFD machine EXTENSION extends BASE.  */
14654
14655
struct mips_mach_extension
14656
{
14657
  unsigned long extension, base;
14658
};
14659
14660
/* An array that maps 64-bit architectures to the corresponding 32-bit
14661
   architectures.  */
14662
static const struct mips_mach_extension mips_mach_32_64[] =
14663
{
14664
  { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14665
  { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14666
  { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14667
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14668
  { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
14669
};
14670
14671
/* An array describing how BFD machines relate to one another.  The entries
14672
   are ordered topologically with MIPS I extensions listed last.  */
14673
14674
static const struct mips_mach_extension mips_mach_extensions[] =
14675
{
14676
  /* MIPS64r2 extensions.  */
14677
  { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14678
  { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14679
  { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14680
  { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14681
  { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14682
  { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14683
  { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14684
14685
  /* MIPS64 extensions.  */
14686
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14687
  { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14688
  { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14689
14690
  /* MIPS V extensions.  */
14691
  { bfd_mach_mipsisa64, bfd_mach_mips5 },
14692
14693
  /* R10000 extensions.  */
14694
  { bfd_mach_mips12000, bfd_mach_mips10000 },
14695
  { bfd_mach_mips14000, bfd_mach_mips10000 },
14696
  { bfd_mach_mips16000, bfd_mach_mips10000 },
14697
14698
  /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
14699
     vr5400 ISA, but doesn't include the multimedia stuff.  It seems
14700
     better to allow vr5400 and vr5500 code to be merged anyway, since
14701
     many libraries will just use the core ISA.  Perhaps we could add
14702
     some sort of ASE flag if this ever proves a problem.  */
14703
  { bfd_mach_mips5500, bfd_mach_mips5400 },
14704
  { bfd_mach_mips5400, bfd_mach_mips5000 },
14705
14706
  /* MIPS IV extensions.  */
14707
  { bfd_mach_mips5, bfd_mach_mips8000 },
14708
  { bfd_mach_mips10000, bfd_mach_mips8000 },
14709
  { bfd_mach_mips5000, bfd_mach_mips8000 },
14710
  { bfd_mach_mips7000, bfd_mach_mips8000 },
14711
  { bfd_mach_mips9000, bfd_mach_mips8000 },
14712
14713
  /* VR4100 extensions.  */
14714
  { bfd_mach_mips4120, bfd_mach_mips4100 },
14715
  { bfd_mach_mips4111, bfd_mach_mips4100 },
14716
14717
  /* MIPS III extensions.  */
14718
  { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14719
  { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14720
  { bfd_mach_mips8000, bfd_mach_mips4000 },
14721
  { bfd_mach_mips_tx49, bfd_mach_mips4000 },
14722
  { bfd_mach_mips4650, bfd_mach_mips4000 },
14723
  { bfd_mach_mips4600, bfd_mach_mips4000 },
14724
  { bfd_mach_mips4400, bfd_mach_mips4000 },
14725
  { bfd_mach_mips4300, bfd_mach_mips4000 },
14726
  { bfd_mach_mips4100, bfd_mach_mips4000 },
14727
  { bfd_mach_mips5900, bfd_mach_mips4000 },
14728
14729
  /* MIPS32r3 extensions.  */
14730
  { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14731
14732
  /* MIPS32r2 extensions.  */
14733
  { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14734
14735
  /* MIPS32 extensions.  */
14736
  { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14737
14738
  /* MIPS II extensions.  */
14739
  { bfd_mach_mips4000, bfd_mach_mips6000 },
14740
  { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14741
  { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14742
  { bfd_mach_mips4010, bfd_mach_mips6000 },
14743
14744
  /* MIPS I extensions.  */
14745
  { bfd_mach_mips6000, bfd_mach_mips3000 },
14746
  { bfd_mach_mips3900, bfd_mach_mips3000 }
14747
};
14748
14749
/* Return true if bfd machine EXTENSION is the same as BASE, or if
14750
   EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
14751
14752
static bool
14753
mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14754
0
{
14755
0
  size_t i;
14756
14757
0
  if (extension == base)
14758
0
    return true;
14759
14760
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14761
0
    if (extension == mips_mach_32_64[i].extension)
14762
0
      return base == mips_mach_32_64[i].base;
14763
14764
0
  return false;
14765
0
}
14766
14767
static bool
14768
mips_mach_extends_p (unsigned long base, unsigned long extension)
14769
0
{
14770
0
  size_t i;
14771
14772
0
  if (mips_mach_extends_32_64 (base, extension))
14773
0
    return true;
14774
14775
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14776
0
    if (extension == mips_mach_extensions[i].extension)
14777
0
      {
14778
0
  extension = mips_mach_extensions[i].base;
14779
0
  if (mips_mach_extends_32_64 (base, extension))
14780
0
    return true;
14781
0
      }
14782
14783
0
  return false;
14784
0
}
14785
14786
/* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
14787
14788
static unsigned long
14789
bfd_mips_isa_ext_mach (unsigned int isa_ext)
14790
0
{
14791
0
  switch (isa_ext)
14792
0
    {
14793
0
    case AFL_EXT_3900:       return bfd_mach_mips3900;
14794
0
    case AFL_EXT_4010:       return bfd_mach_mips4010;
14795
0
    case AFL_EXT_ALLEGREX:    return bfd_mach_mips_allegrex;
14796
0
    case AFL_EXT_4100:       return bfd_mach_mips4100;
14797
0
    case AFL_EXT_4111:       return bfd_mach_mips4111;
14798
0
    case AFL_EXT_4120:       return bfd_mach_mips4120;
14799
0
    case AFL_EXT_4650:       return bfd_mach_mips4650;
14800
0
    case AFL_EXT_TX49:       return bfd_mach_mips_tx49;
14801
0
    case AFL_EXT_5400:       return bfd_mach_mips5400;
14802
0
    case AFL_EXT_5500:       return bfd_mach_mips5500;
14803
0
    case AFL_EXT_5900:       return bfd_mach_mips5900;
14804
0
    case AFL_EXT_10000:       return bfd_mach_mips10000;
14805
0
    case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14806
0
    case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14807
0
    case AFL_EXT_SB1:       return bfd_mach_mips_sb1;
14808
0
    case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
14809
0
    case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
14810
0
    case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
14811
0
    case AFL_EXT_OCTEON3:     return bfd_mach_mips_octeon3;
14812
0
    case AFL_EXT_XLR:       return bfd_mach_mips_xlr;
14813
0
    case AFL_EXT_INTERAPTIV_MR2: return bfd_mach_mips_interaptiv_mr2;
14814
0
    default:          return bfd_mach_mips3000;
14815
0
    }
14816
0
}
14817
14818
/* Return the .MIPS.abiflags value representing each ISA Extension.  */
14819
14820
unsigned int
14821
bfd_mips_isa_ext (bfd *abfd)
14822
0
{
14823
0
  switch (bfd_get_mach (abfd))
14824
0
    {
14825
0
    case bfd_mach_mips3900:     return AFL_EXT_3900;
14826
0
    case bfd_mach_mips4010:     return AFL_EXT_4010;
14827
0
    case bfd_mach_mips_allegrex:    return AFL_EXT_ALLEGREX;
14828
0
    case bfd_mach_mips4100:     return AFL_EXT_4100;
14829
0
    case bfd_mach_mips4111:     return AFL_EXT_4111;
14830
0
    case bfd_mach_mips4120:     return AFL_EXT_4120;
14831
0
    case bfd_mach_mips4650:     return AFL_EXT_4650;
14832
0
    case bfd_mach_mips_tx49:     return AFL_EXT_TX49;
14833
0
    case bfd_mach_mips5400:     return AFL_EXT_5400;
14834
0
    case bfd_mach_mips5500:     return AFL_EXT_5500;
14835
0
    case bfd_mach_mips5900:     return AFL_EXT_5900;
14836
0
    case bfd_mach_mips10000:     return AFL_EXT_10000;
14837
0
    case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14838
0
    case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14839
0
    case bfd_mach_mips_sb1:     return AFL_EXT_SB1;
14840
0
    case bfd_mach_mips_octeon:     return AFL_EXT_OCTEON;
14841
0
    case bfd_mach_mips_octeonp:     return AFL_EXT_OCTEONP;
14842
0
    case bfd_mach_mips_octeon3:     return AFL_EXT_OCTEON3;
14843
0
    case bfd_mach_mips_octeon2:     return AFL_EXT_OCTEON2;
14844
0
    case bfd_mach_mips_xlr:     return AFL_EXT_XLR;
14845
0
    case bfd_mach_mips_interaptiv_mr2:
14846
0
      return AFL_EXT_INTERAPTIV_MR2;
14847
0
    default:          return 0;
14848
0
    }
14849
0
}
14850
14851
/* Encode ISA level and revision as a single value.  */
14852
0
#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14853
14854
/* Decode a single value into level and revision.  */
14855
0
#define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
14856
0
#define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
14857
14858
/* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
14859
14860
static void
14861
update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14862
0
{
14863
0
  int new_isa = 0;
14864
0
  switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14865
0
    {
14866
0
    case EF_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
14867
0
    case EF_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
14868
0
    case EF_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
14869
0
    case EF_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
14870
0
    case EF_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
14871
0
    case EF_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
14872
0
    case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14873
0
    case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14874
0
    case EF_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
14875
0
    case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14876
0
    case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14877
0
    default:
14878
0
      _bfd_error_handler
14879
  /* xgettext:c-format */
14880
0
  (_("%pB: unknown architecture %s"),
14881
0
   abfd, bfd_printable_name (abfd));
14882
0
    }
14883
14884
0
  if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14885
0
    {
14886
0
      abiflags->isa_level = ISA_LEVEL (new_isa);
14887
0
      abiflags->isa_rev = ISA_REV (new_isa);
14888
0
    }
14889
14890
  /* Update the isa_ext if ABFD describes a further extension.  */
14891
0
  if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14892
0
         bfd_get_mach (abfd)))
14893
0
    abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14894
0
}
14895
14896
/* Return true if the given ELF header flags describe a 32-bit binary.  */
14897
14898
static bool
14899
mips_32bit_flags_p (flagword flags)
14900
0
{
14901
0
  return ((flags & EF_MIPS_32BITMODE) != 0
14902
0
    || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
14903
0
    || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
14904
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
14905
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
14906
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
14907
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
14908
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
14909
0
}
14910
14911
/* Infer the content of the ABI flags based on the elf header.  */
14912
14913
static void
14914
infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14915
0
{
14916
0
  obj_attribute *in_attr;
14917
14918
0
  memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14919
0
  update_mips_abiflags_isa (abfd, abiflags);
14920
14921
0
  if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14922
0
    abiflags->gpr_size = AFL_REG_32;
14923
0
  else
14924
0
    abiflags->gpr_size = AFL_REG_64;
14925
14926
0
  abiflags->cpr1_size = AFL_REG_NONE;
14927
14928
0
  in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14929
0
  abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14930
14931
0
  if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14932
0
      || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14933
0
      || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14934
0
    && abiflags->gpr_size == AFL_REG_32))
14935
0
    abiflags->cpr1_size = AFL_REG_32;
14936
0
  else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14937
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14938
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14939
0
    abiflags->cpr1_size = AFL_REG_64;
14940
14941
0
  abiflags->cpr2_size = AFL_REG_NONE;
14942
14943
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14944
0
    abiflags->ases |= AFL_ASE_MDMX;
14945
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14946
0
    abiflags->ases |= AFL_ASE_MIPS16;
14947
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14948
0
    abiflags->ases |= AFL_ASE_MICROMIPS;
14949
14950
0
  if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14951
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14952
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14953
0
      && abiflags->isa_level >= 32
14954
0
      && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14955
0
    abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14956
0
}
14957
14958
/* We need to use a special link routine to handle the .reginfo and
14959
   the .mdebug sections.  We need to merge all instances of these
14960
   sections together, not write them all out sequentially.  */
14961
14962
bool
14963
_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14964
0
{
14965
0
  asection *o;
14966
0
  struct bfd_link_order *p;
14967
0
  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14968
0
  asection *rtproc_sec, *abiflags_sec;
14969
0
  Elf32_RegInfo reginfo;
14970
0
  struct ecoff_debug_info debug;
14971
0
  struct mips_htab_traverse_info hti;
14972
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14973
0
  const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14974
0
  HDRR *symhdr = &debug.symbolic_header;
14975
0
  void *mdebug_handle = NULL;
14976
0
  asection *s;
14977
0
  EXTR esym;
14978
0
  unsigned int i;
14979
0
  bfd_size_type amt;
14980
0
  struct mips_elf_link_hash_table *htab;
14981
14982
0
  static const char * const secname[] =
14983
0
  {
14984
0
    ".text", ".init", ".fini", ".data",
14985
0
    ".rodata", ".sdata", ".sbss", ".bss"
14986
0
  };
14987
0
  static const int sc[] =
14988
0
  {
14989
0
    scText, scInit, scFini, scData,
14990
0
    scRData, scSData, scSBss, scBss
14991
0
  };
14992
14993
0
  htab = mips_elf_hash_table (info);
14994
0
  BFD_ASSERT (htab != NULL);
14995
14996
  /* Sort the dynamic symbols so that those with GOT entries come after
14997
     those without.  */
14998
0
  if (!mips_elf_sort_hash_table (abfd, info))
14999
0
    return false;
15000
15001
  /* Create any scheduled LA25 stubs.  */
15002
0
  hti.info = info;
15003
0
  hti.output_bfd = abfd;
15004
0
  hti.error = false;
15005
0
  htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
15006
0
  if (hti.error)
15007
0
    return false;
15008
15009
  /* Get a value for the GP register.  */
15010
0
  if (elf_gp (abfd) == 0)
15011
0
    {
15012
0
      struct bfd_link_hash_entry *h;
15013
15014
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
15015
0
      if (h != NULL && h->type == bfd_link_hash_defined)
15016
0
  elf_gp (abfd) = (h->u.def.value
15017
0
       + h->u.def.section->output_section->vma
15018
0
       + h->u.def.section->output_offset);
15019
0
      else if (htab->root.target_os == is_vxworks
15020
0
         && (h = bfd_link_hash_lookup (info->hash,
15021
0
               "_GLOBAL_OFFSET_TABLE_",
15022
0
               false, false, true))
15023
0
         && h->type == bfd_link_hash_defined)
15024
0
  elf_gp (abfd) = (h->u.def.section->output_section->vma
15025
0
       + h->u.def.section->output_offset
15026
0
       + h->u.def.value);
15027
0
      else if (bfd_link_relocatable (info))
15028
0
  {
15029
0
    bfd_vma lo = MINUS_ONE;
15030
15031
    /* Find the GP-relative section with the lowest offset.  */
15032
0
    for (o = abfd->sections; o != NULL; o = o->next)
15033
0
      if (o->vma < lo
15034
0
    && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
15035
0
        lo = o->vma;
15036
15037
    /* And calculate GP relative to that.  */
15038
0
    elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
15039
0
  }
15040
0
      else
15041
0
  {
15042
    /* If the relocate_section function needs to do a reloc
15043
       involving the GP value, it should make a reloc_dangerous
15044
       callback to warn that GP is not defined.  */
15045
0
  }
15046
0
    }
15047
15048
  /* Go through the sections and collect the .reginfo and .mdebug
15049
     information.  */
15050
0
  abiflags_sec = NULL;
15051
0
  reginfo_sec = NULL;
15052
0
  mdebug_sec = NULL;
15053
0
  gptab_data_sec = NULL;
15054
0
  gptab_bss_sec = NULL;
15055
0
  for (o = abfd->sections; o != NULL; o = o->next)
15056
0
    {
15057
0
      if (strcmp (o->name, ".MIPS.abiflags") == 0)
15058
0
  {
15059
    /* We have found the .MIPS.abiflags section in the output file.
15060
       Look through all the link_orders comprising it and remove them.
15061
       The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
15062
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15063
0
      {
15064
0
        asection *input_section;
15065
15066
0
        if (p->type != bfd_indirect_link_order)
15067
0
    {
15068
0
      if (p->type == bfd_data_link_order)
15069
0
        continue;
15070
0
      abort ();
15071
0
    }
15072
15073
0
        input_section = p->u.indirect.section;
15074
15075
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15076
     elf_link_input_bfd ignores this section.  */
15077
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15078
0
      }
15079
15080
    /* Size has been set in _bfd_mips_elf_late_size_sections.  */
15081
0
    BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
15082
15083
    /* Skip this section later on (I don't think this currently
15084
       matters, but someday it might).  */
15085
0
    o->map_head.link_order = NULL;
15086
15087
0
    abiflags_sec = o;
15088
0
  }
15089
15090
0
      if (strcmp (o->name, ".reginfo") == 0)
15091
0
  {
15092
0
    memset (&reginfo, 0, sizeof reginfo);
15093
15094
    /* We have found the .reginfo section in the output file.
15095
       Look through all the link_orders comprising it and merge
15096
       the information together.  */
15097
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15098
0
      {
15099
0
        asection *input_section;
15100
0
        bfd *input_bfd;
15101
0
        Elf32_External_RegInfo ext;
15102
0
        Elf32_RegInfo sub;
15103
0
        bfd_size_type sz;
15104
15105
0
        if (p->type != bfd_indirect_link_order)
15106
0
    {
15107
0
      if (p->type == bfd_data_link_order)
15108
0
        continue;
15109
0
      abort ();
15110
0
    }
15111
15112
0
        input_section = p->u.indirect.section;
15113
0
        input_bfd = input_section->owner;
15114
15115
0
        sz = (input_section->size < sizeof (ext)
15116
0
        ? input_section->size : sizeof (ext));
15117
0
        memset (&ext, 0, sizeof (ext));
15118
0
        if (! bfd_get_section_contents (input_bfd, input_section,
15119
0
                &ext, 0, sz))
15120
0
    return false;
15121
15122
0
        bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
15123
15124
0
        reginfo.ri_gprmask |= sub.ri_gprmask;
15125
0
        reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
15126
0
        reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
15127
0
        reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
15128
0
        reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
15129
15130
        /* ri_gp_value is set by the function
15131
     `_bfd_mips_elf_section_processing' when the section is
15132
     finally written out.  */
15133
15134
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15135
     elf_link_input_bfd ignores this section.  */
15136
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15137
0
      }
15138
15139
    /* Size has been set in _bfd_mips_elf_late_size_sections.  */
15140
0
    BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
15141
15142
    /* Skip this section later on (I don't think this currently
15143
       matters, but someday it might).  */
15144
0
    o->map_head.link_order = NULL;
15145
15146
0
    reginfo_sec = o;
15147
0
  }
15148
15149
0
      if (strcmp (o->name, ".mdebug") == 0)
15150
0
  {
15151
0
    struct extsym_info einfo;
15152
0
    bfd_vma last;
15153
15154
    /* We have found the .mdebug section in the output file.
15155
       Look through all the link_orders comprising it and merge
15156
       the information together.  */
15157
0
    symhdr->magic = swap->sym_magic;
15158
    /* FIXME: What should the version stamp be?  */
15159
0
    symhdr->vstamp = 0;
15160
0
    symhdr->ilineMax = 0;
15161
0
    symhdr->cbLine = 0;
15162
0
    symhdr->idnMax = 0;
15163
0
    symhdr->ipdMax = 0;
15164
0
    symhdr->isymMax = 0;
15165
0
    symhdr->ioptMax = 0;
15166
0
    symhdr->iauxMax = 0;
15167
0
    symhdr->issMax = 0;
15168
0
    symhdr->issExtMax = 0;
15169
0
    symhdr->ifdMax = 0;
15170
0
    symhdr->crfd = 0;
15171
0
    symhdr->iextMax = 0;
15172
15173
    /* We accumulate the debugging information itself in the
15174
       debug_info structure.  */
15175
0
    debug.alloc_syments = false;
15176
0
    debug.line = NULL;
15177
0
    debug.external_dnr = NULL;
15178
0
    debug.external_pdr = NULL;
15179
0
    debug.external_sym = NULL;
15180
0
    debug.external_opt = NULL;
15181
0
    debug.external_aux = NULL;
15182
0
    debug.ss = NULL;
15183
0
    debug.ssext = debug.ssext_end = NULL;
15184
0
    debug.external_fdr = NULL;
15185
0
    debug.external_rfd = NULL;
15186
0
    debug.external_ext = debug.external_ext_end = NULL;
15187
15188
0
    mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15189
0
    if (mdebug_handle == NULL)
15190
0
      return false;
15191
15192
0
    esym.jmptbl = 0;
15193
0
    esym.cobol_main = 0;
15194
0
    esym.weakext = 0;
15195
0
    esym.reserved = 0;
15196
0
    esym.ifd = ifdNil;
15197
0
    esym.asym.iss = issNil;
15198
0
    esym.asym.st = stLocal;
15199
0
    esym.asym.reserved = 0;
15200
0
    esym.asym.index = indexNil;
15201
0
    last = 0;
15202
0
    for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15203
0
      {
15204
0
        esym.asym.sc = sc[i];
15205
0
        s = bfd_get_section_by_name (abfd, secname[i]);
15206
0
        if (s != NULL)
15207
0
    {
15208
0
      esym.asym.value = s->vma;
15209
0
      last = s->vma + s->size;
15210
0
    }
15211
0
        else
15212
0
    esym.asym.value = last;
15213
0
        if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15214
0
             secname[i], &esym))
15215
0
    return false;
15216
0
      }
15217
15218
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15219
0
      {
15220
0
        asection *input_section;
15221
0
        bfd *input_bfd;
15222
0
        const struct ecoff_debug_swap *input_swap;
15223
0
        struct ecoff_debug_info input_debug;
15224
0
        char *eraw_src;
15225
0
        char *eraw_end;
15226
15227
0
        if (p->type != bfd_indirect_link_order)
15228
0
    {
15229
0
      if (p->type == bfd_data_link_order)
15230
0
        continue;
15231
0
      abort ();
15232
0
    }
15233
15234
0
        input_section = p->u.indirect.section;
15235
0
        input_bfd = input_section->owner;
15236
15237
0
        if (!is_mips_elf (input_bfd))
15238
0
    {
15239
      /* I don't know what a non MIPS ELF bfd would be
15240
         doing with a .mdebug section, but I don't really
15241
         want to deal with it.  */
15242
0
      continue;
15243
0
    }
15244
15245
0
        input_swap = (get_elf_backend_data (input_bfd)
15246
0
          ->elf_backend_ecoff_debug_swap);
15247
15248
0
        BFD_ASSERT (p->size == input_section->size);
15249
15250
        /* The ECOFF linking code expects that we have already
15251
     read in the debugging information and set up an
15252
     ecoff_debug_info structure, so we do that now.  */
15253
0
        if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15254
0
               &input_debug))
15255
0
    return false;
15256
15257
0
        if (! (bfd_ecoff_debug_accumulate
15258
0
         (mdebug_handle, abfd, &debug, swap, input_bfd,
15259
0
          &input_debug, input_swap, info)))
15260
0
    {
15261
0
      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15262
0
      return false;
15263
0
    }
15264
15265
        /* Loop through the external symbols.  For each one with
15266
     interesting information, try to find the symbol in
15267
     the linker global hash table and save the information
15268
     for the output external symbols.  */
15269
0
        eraw_src = input_debug.external_ext;
15270
0
        eraw_end = (eraw_src
15271
0
        + (input_debug.symbolic_header.iextMax
15272
0
           * input_swap->external_ext_size));
15273
0
        for (;
15274
0
       eraw_src < eraw_end;
15275
0
       eraw_src += input_swap->external_ext_size)
15276
0
    {
15277
0
      EXTR ext;
15278
0
      const char *name;
15279
0
      struct mips_elf_link_hash_entry *h;
15280
15281
0
      (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15282
0
      if (ext.asym.sc == scNil
15283
0
          || ext.asym.sc == scUndefined
15284
0
          || ext.asym.sc == scSUndefined)
15285
0
        continue;
15286
15287
0
      name = input_debug.ssext + ext.asym.iss;
15288
0
      h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15289
0
             name, false, false, true);
15290
0
      if (h == NULL || h->esym.ifd != -2)
15291
0
        continue;
15292
15293
0
      if (ext.ifd != -1)
15294
0
        {
15295
0
          BFD_ASSERT (ext.ifd
15296
0
          < input_debug.symbolic_header.ifdMax);
15297
0
          ext.ifd = input_debug.ifdmap[ext.ifd];
15298
0
        }
15299
15300
0
      h->esym = ext;
15301
0
    }
15302
15303
        /* Free up the information we just read.  */
15304
0
        _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15305
15306
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15307
     elf_link_input_bfd ignores this section.  */
15308
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15309
0
      }
15310
15311
0
    if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15312
0
      {
15313
        /* Create .rtproc section.  */
15314
0
        rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15315
0
        if (rtproc_sec == NULL)
15316
0
    {
15317
0
      flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15318
0
            | SEC_LINKER_CREATED | SEC_READONLY);
15319
15320
0
      rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15321
0
                   ".rtproc",
15322
0
                   flags);
15323
0
      if (rtproc_sec == NULL
15324
0
          || !bfd_set_section_alignment (rtproc_sec, 4))
15325
0
        return false;
15326
0
    }
15327
15328
0
        if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15329
0
                 info, rtproc_sec,
15330
0
                 &debug))
15331
0
    return false;
15332
0
      }
15333
15334
    /* Build the external symbol information.  */
15335
0
    einfo.abfd = abfd;
15336
0
    einfo.info = info;
15337
0
    einfo.debug = &debug;
15338
0
    einfo.swap = swap;
15339
0
    einfo.failed = false;
15340
0
    mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15341
0
               mips_elf_output_extsym, &einfo);
15342
0
    if (einfo.failed)
15343
0
      return false;
15344
15345
    /* Set the size of the .mdebug section.  */
15346
0
    o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15347
15348
    /* Skip this section later on (I don't think this currently
15349
       matters, but someday it might).  */
15350
0
    o->map_head.link_order = NULL;
15351
15352
0
    mdebug_sec = o;
15353
0
  }
15354
15355
0
      if (startswith (o->name, ".gptab."))
15356
0
  {
15357
0
    const char *subname;
15358
0
    unsigned int c;
15359
0
    Elf32_gptab *tab;
15360
0
    Elf32_External_gptab *ext_tab;
15361
0
    unsigned int j;
15362
15363
    /* The .gptab.sdata and .gptab.sbss sections hold
15364
       information describing how the small data area would
15365
       change depending upon the -G switch.  These sections
15366
       not used in executables files.  */
15367
0
    if (! bfd_link_relocatable (info))
15368
0
      {
15369
0
        for (p = o->map_head.link_order; p != NULL; p = p->next)
15370
0
    {
15371
0
      asection *input_section;
15372
15373
0
      if (p->type != bfd_indirect_link_order)
15374
0
        {
15375
0
          if (p->type == bfd_data_link_order)
15376
0
      continue;
15377
0
          abort ();
15378
0
        }
15379
15380
0
      input_section = p->u.indirect.section;
15381
15382
      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15383
         elf_link_input_bfd ignores this section.  */
15384
0
      input_section->flags &= ~SEC_HAS_CONTENTS;
15385
0
    }
15386
15387
        /* Skip this section later on (I don't think this
15388
     currently matters, but someday it might).  */
15389
0
        o->map_head.link_order = NULL;
15390
15391
        /* Really remove the section.  */
15392
0
        bfd_section_list_remove (abfd, o);
15393
0
        --abfd->section_count;
15394
15395
0
        continue;
15396
0
      }
15397
15398
    /* There is one gptab for initialized data, and one for
15399
       uninitialized data.  */
15400
0
    if (strcmp (o->name, ".gptab.sdata") == 0)
15401
0
      gptab_data_sec = o;
15402
0
    else if (strcmp (o->name, ".gptab.sbss") == 0)
15403
0
      gptab_bss_sec = o;
15404
0
    else
15405
0
      {
15406
0
        _bfd_error_handler
15407
    /* xgettext:c-format */
15408
0
    (_("%pB: illegal section name `%pA'"), abfd, o);
15409
0
        bfd_set_error (bfd_error_nonrepresentable_section);
15410
0
        return false;
15411
0
      }
15412
15413
    /* The linker script always combines .gptab.data and
15414
       .gptab.sdata into .gptab.sdata, and likewise for
15415
       .gptab.bss and .gptab.sbss.  It is possible that there is
15416
       no .sdata or .sbss section in the output file, in which
15417
       case we must change the name of the output section.  */
15418
0
    subname = o->name + sizeof ".gptab" - 1;
15419
0
    if (bfd_get_section_by_name (abfd, subname) == NULL)
15420
0
      {
15421
0
        if (o == gptab_data_sec)
15422
0
    o->name = ".gptab.data";
15423
0
        else
15424
0
    o->name = ".gptab.bss";
15425
0
        subname = o->name + sizeof ".gptab" - 1;
15426
0
        BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15427
0
      }
15428
15429
    /* Set up the first entry.  */
15430
0
    c = 1;
15431
0
    amt = c * sizeof (Elf32_gptab);
15432
0
    tab = bfd_malloc (amt);
15433
0
    if (tab == NULL)
15434
0
      return false;
15435
0
    tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15436
0
    tab[0].gt_header.gt_unused = 0;
15437
15438
    /* Combine the input sections.  */
15439
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15440
0
      {
15441
0
        asection *input_section;
15442
0
        bfd *input_bfd;
15443
0
        bfd_size_type size;
15444
0
        unsigned long last;
15445
0
        bfd_size_type gpentry;
15446
15447
0
        if (p->type != bfd_indirect_link_order)
15448
0
    {
15449
0
      if (p->type == bfd_data_link_order)
15450
0
        continue;
15451
0
      abort ();
15452
0
    }
15453
15454
0
        input_section = p->u.indirect.section;
15455
0
        input_bfd = input_section->owner;
15456
15457
        /* Combine the gptab entries for this input section one
15458
     by one.  We know that the input gptab entries are
15459
     sorted by ascending -G value.  */
15460
0
        size = input_section->size;
15461
0
        last = 0;
15462
0
        for (gpentry = sizeof (Elf32_External_gptab);
15463
0
       gpentry < size;
15464
0
       gpentry += sizeof (Elf32_External_gptab))
15465
0
    {
15466
0
      Elf32_External_gptab ext_gptab;
15467
0
      Elf32_gptab int_gptab;
15468
0
      unsigned long val;
15469
0
      unsigned long add;
15470
0
      bool exact;
15471
0
      unsigned int look;
15472
15473
0
      if (! (bfd_get_section_contents
15474
0
       (input_bfd, input_section, &ext_gptab, gpentry,
15475
0
        sizeof (Elf32_External_gptab))))
15476
0
        {
15477
0
          free (tab);
15478
0
          return false;
15479
0
        }
15480
15481
0
      bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15482
0
            &int_gptab);
15483
0
      val = int_gptab.gt_entry.gt_g_value;
15484
0
      add = int_gptab.gt_entry.gt_bytes - last;
15485
15486
0
      exact = false;
15487
0
      for (look = 1; look < c; look++)
15488
0
        {
15489
0
          if (tab[look].gt_entry.gt_g_value >= val)
15490
0
      tab[look].gt_entry.gt_bytes += add;
15491
15492
0
          if (tab[look].gt_entry.gt_g_value == val)
15493
0
      exact = true;
15494
0
        }
15495
15496
0
      if (! exact)
15497
0
        {
15498
0
          Elf32_gptab *new_tab;
15499
0
          unsigned int max;
15500
15501
          /* We need a new table entry.  */
15502
0
          amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15503
0
          new_tab = bfd_realloc (tab, amt);
15504
0
          if (new_tab == NULL)
15505
0
      {
15506
0
        free (tab);
15507
0
        return false;
15508
0
      }
15509
0
          tab = new_tab;
15510
0
          tab[c].gt_entry.gt_g_value = val;
15511
0
          tab[c].gt_entry.gt_bytes = add;
15512
15513
          /* Merge in the size for the next smallest -G
15514
       value, since that will be implied by this new
15515
       value.  */
15516
0
          max = 0;
15517
0
          for (look = 1; look < c; look++)
15518
0
      {
15519
0
        if (tab[look].gt_entry.gt_g_value < val
15520
0
            && (max == 0
15521
0
          || (tab[look].gt_entry.gt_g_value
15522
0
              > tab[max].gt_entry.gt_g_value)))
15523
0
          max = look;
15524
0
      }
15525
0
          if (max != 0)
15526
0
      tab[c].gt_entry.gt_bytes +=
15527
0
        tab[max].gt_entry.gt_bytes;
15528
15529
0
          ++c;
15530
0
        }
15531
15532
0
      last = int_gptab.gt_entry.gt_bytes;
15533
0
    }
15534
15535
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15536
     elf_link_input_bfd ignores this section.  */
15537
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15538
0
      }
15539
15540
    /* The table must be sorted by -G value.  */
15541
0
    if (c > 2)
15542
0
      qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15543
15544
    /* Swap out the table.  */
15545
0
    amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15546
0
    ext_tab = bfd_alloc (abfd, amt);
15547
0
    if (ext_tab == NULL)
15548
0
      {
15549
0
        free (tab);
15550
0
        return false;
15551
0
      }
15552
15553
0
    for (j = 0; j < c; j++)
15554
0
      bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15555
0
    free (tab);
15556
15557
0
    o->size = c * sizeof (Elf32_External_gptab);
15558
0
    o->contents = (bfd_byte *) ext_tab;
15559
0
    o->alloced = 1;
15560
15561
    /* Skip this section later on (I don't think this currently
15562
       matters, but someday it might).  */
15563
0
    o->map_head.link_order = NULL;
15564
0
  }
15565
0
    }
15566
15567
  /* Invoke the regular ELF backend linker to do all the work.  */
15568
0
  if (!_bfd_elf_final_link (abfd, info))
15569
0
    return false;
15570
15571
  /* Now write out the computed sections.  */
15572
15573
0
  if (abiflags_sec != NULL)
15574
0
    {
15575
0
      Elf_External_ABIFlags_v0 ext;
15576
0
      Elf_Internal_ABIFlags_v0 *abiflags;
15577
15578
0
      abiflags = &mips_elf_tdata (abfd)->abiflags;
15579
15580
      /* Set up the abiflags if no valid input sections were found.  */
15581
0
      if (!mips_elf_tdata (abfd)->abiflags_valid)
15582
0
  {
15583
0
    infer_mips_abiflags (abfd, abiflags);
15584
0
    mips_elf_tdata (abfd)->abiflags_valid = true;
15585
0
  }
15586
0
      bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15587
0
      if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15588
0
  return false;
15589
0
    }
15590
15591
0
  if (reginfo_sec != NULL)
15592
0
    {
15593
0
      Elf32_External_RegInfo ext;
15594
15595
0
      bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15596
0
      if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15597
0
  return false;
15598
0
    }
15599
15600
0
  if (mdebug_sec != NULL)
15601
0
    {
15602
0
      BFD_ASSERT (abfd->output_has_begun);
15603
0
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15604
0
                 swap, info,
15605
0
                 mdebug_sec->filepos))
15606
0
  return false;
15607
15608
0
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15609
0
    }
15610
15611
0
  if (gptab_data_sec != NULL)
15612
0
    {
15613
0
      if (! bfd_set_section_contents (abfd, gptab_data_sec,
15614
0
              gptab_data_sec->contents,
15615
0
              0, gptab_data_sec->size))
15616
0
  return false;
15617
0
    }
15618
15619
0
  if (gptab_bss_sec != NULL)
15620
0
    {
15621
0
      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15622
0
              gptab_bss_sec->contents,
15623
0
              0, gptab_bss_sec->size))
15624
0
  return false;
15625
0
    }
15626
15627
0
  if (SGI_COMPAT (abfd))
15628
0
    {
15629
0
      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15630
0
      if (rtproc_sec != NULL)
15631
0
  {
15632
0
    if (! bfd_set_section_contents (abfd, rtproc_sec,
15633
0
            rtproc_sec->contents,
15634
0
            0, rtproc_sec->size))
15635
0
      return false;
15636
0
  }
15637
0
    }
15638
15639
0
  return true;
15640
0
}
15641

15642
/* Merge object file header flags from IBFD into OBFD.  Raise an error
15643
   if there are conflicting settings.  */
15644
15645
static bool
15646
mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15647
0
{
15648
0
  bfd *obfd = info->output_bfd;
15649
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15650
0
  flagword old_flags;
15651
0
  flagword new_flags;
15652
0
  bool ok;
15653
15654
0
  new_flags = elf_elfheader (ibfd)->e_flags;
15655
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15656
0
  old_flags = elf_elfheader (obfd)->e_flags;
15657
15658
  /* Check flag compatibility.  */
15659
15660
0
  new_flags &= ~EF_MIPS_NOREORDER;
15661
0
  old_flags &= ~EF_MIPS_NOREORDER;
15662
15663
  /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
15664
     doesn't seem to matter.  */
15665
0
  new_flags &= ~EF_MIPS_XGOT;
15666
0
  old_flags &= ~EF_MIPS_XGOT;
15667
15668
  /* MIPSpro generates ucode info in n64 objects.  Again, we should
15669
     just be able to ignore this.  */
15670
0
  new_flags &= ~EF_MIPS_UCODE;
15671
0
  old_flags &= ~EF_MIPS_UCODE;
15672
15673
  /* DSOs should only be linked with CPIC code.  */
15674
0
  if ((ibfd->flags & DYNAMIC) != 0)
15675
0
    new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15676
15677
0
  if (new_flags == old_flags)
15678
0
    return true;
15679
15680
0
  ok = true;
15681
15682
0
  if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15683
0
      != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15684
0
    {
15685
0
      _bfd_error_handler
15686
0
  (_("%pB: warning: linking abicalls files with non-abicalls files"),
15687
0
   ibfd);
15688
0
      ok = true;
15689
0
    }
15690
15691
0
  if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15692
0
    elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15693
0
  if (! (new_flags & EF_MIPS_PIC))
15694
0
    elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15695
15696
0
  new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15697
0
  old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15698
15699
  /* Compare the ISAs.  */
15700
0
  if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15701
0
    {
15702
0
      _bfd_error_handler
15703
0
  (_("%pB: linking 32-bit code with 64-bit code"),
15704
0
   ibfd);
15705
0
      ok = false;
15706
0
    }
15707
0
  else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15708
0
    {
15709
      /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
15710
0
      if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15711
0
  {
15712
    /* Copy the architecture info from IBFD to OBFD.  Also copy
15713
       the 32-bit flag (if set) so that we continue to recognise
15714
       OBFD as a 32-bit binary.  */
15715
0
    bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15716
0
    elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15717
0
    elf_elfheader (obfd)->e_flags
15718
0
      |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15719
15720
    /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
15721
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15722
15723
    /* Copy across the ABI flags if OBFD doesn't use them
15724
       and if that was what caused us to treat IBFD as 32-bit.  */
15725
0
    if ((old_flags & EF_MIPS_ABI) == 0
15726
0
        && mips_32bit_flags_p (new_flags)
15727
0
        && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15728
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15729
0
  }
15730
0
      else
15731
0
  {
15732
    /* The ISAs aren't compatible.  */
15733
0
    _bfd_error_handler
15734
      /* xgettext:c-format */
15735
0
      (_("%pB: linking %s module with previous %s modules"),
15736
0
       ibfd,
15737
0
       bfd_printable_name (ibfd),
15738
0
       bfd_printable_name (obfd));
15739
0
    ok = false;
15740
0
  }
15741
0
    }
15742
15743
0
  new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15744
0
  old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15745
15746
  /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
15747
     does set EI_CLASS differently from any 32-bit ABI.  */
15748
0
  if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15749
0
      || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15750
0
    != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15751
0
    {
15752
      /* Only error if both are set (to different values).  */
15753
0
      if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15754
0
    || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15755
0
        != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15756
0
  {
15757
0
    _bfd_error_handler
15758
      /* xgettext:c-format */
15759
0
      (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15760
0
       ibfd,
15761
0
       elf_mips_abi_name (ibfd),
15762
0
       elf_mips_abi_name (obfd));
15763
0
    ok = false;
15764
0
  }
15765
0
      new_flags &= ~EF_MIPS_ABI;
15766
0
      old_flags &= ~EF_MIPS_ABI;
15767
0
    }
15768
15769
  /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
15770
     and allow arbitrary mixing of the remaining ASEs (retain the union).  */
15771
0
  if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15772
0
    {
15773
0
      int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15774
0
      int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15775
0
      int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15776
0
      int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15777
0
      int micro_mis = old_m16 && new_micro;
15778
0
      int m16_mis = old_micro && new_m16;
15779
15780
0
      if (m16_mis || micro_mis)
15781
0
  {
15782
0
    _bfd_error_handler
15783
      /* xgettext:c-format */
15784
0
      (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15785
0
       ibfd,
15786
0
       m16_mis ? "MIPS16" : "microMIPS",
15787
0
       m16_mis ? "microMIPS" : "MIPS16");
15788
0
    ok = false;
15789
0
  }
15790
15791
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15792
15793
0
      new_flags &= ~ EF_MIPS_ARCH_ASE;
15794
0
      old_flags &= ~ EF_MIPS_ARCH_ASE;
15795
0
    }
15796
15797
  /* Compare NaN encodings.  */
15798
0
  if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15799
0
    {
15800
      /* xgettext:c-format */
15801
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15802
0
        ibfd,
15803
0
        (new_flags & EF_MIPS_NAN2008
15804
0
         ? "-mnan=2008" : "-mnan=legacy"),
15805
0
        (old_flags & EF_MIPS_NAN2008
15806
0
         ? "-mnan=2008" : "-mnan=legacy"));
15807
0
      ok = false;
15808
0
      new_flags &= ~EF_MIPS_NAN2008;
15809
0
      old_flags &= ~EF_MIPS_NAN2008;
15810
0
    }
15811
15812
  /* Compare FP64 state.  */
15813
0
  if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15814
0
    {
15815
      /* xgettext:c-format */
15816
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15817
0
        ibfd,
15818
0
        (new_flags & EF_MIPS_FP64
15819
0
         ? "-mfp64" : "-mfp32"),
15820
0
        (old_flags & EF_MIPS_FP64
15821
0
         ? "-mfp64" : "-mfp32"));
15822
0
      ok = false;
15823
0
      new_flags &= ~EF_MIPS_FP64;
15824
0
      old_flags &= ~EF_MIPS_FP64;
15825
0
    }
15826
15827
  /* Warn about any other mismatches */
15828
0
  if (new_flags != old_flags)
15829
0
    {
15830
      /* xgettext:c-format */
15831
0
      _bfd_error_handler
15832
0
  (_("%pB: uses different e_flags (%#x) fields than previous modules "
15833
0
     "(%#x)"),
15834
0
   ibfd, new_flags, old_flags);
15835
0
      ok = false;
15836
0
    }
15837
15838
0
  return ok;
15839
0
}
15840
15841
/* Merge object attributes from IBFD into OBFD.  Raise an error if
15842
   there are conflicting attributes.  */
15843
static bool
15844
mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15845
0
{
15846
0
  bfd *obfd = info->output_bfd;
15847
0
  obj_attribute *in_attr;
15848
0
  obj_attribute *out_attr;
15849
0
  bfd *abi_fp_bfd;
15850
0
  bfd *abi_msa_bfd;
15851
15852
0
  abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15853
0
  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15854
0
  if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15855
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15856
15857
0
  abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15858
0
  if (!abi_msa_bfd
15859
0
      && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15860
0
    mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15861
15862
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
15863
0
    {
15864
      /* This is the first object.  Copy the attributes.  */
15865
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
15866
15867
      /* Use the Tag_null value to indicate the attributes have been
15868
   initialized.  */
15869
0
      elf_known_obj_attributes_proc (obfd)[0].i = 1;
15870
15871
0
      return true;
15872
0
    }
15873
15874
  /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15875
     non-conflicting ones.  */
15876
0
  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15877
0
  if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15878
0
    {
15879
0
      int out_fp, in_fp;
15880
15881
0
      out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15882
0
      in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15883
0
      out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15884
0
      if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15885
0
  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15886
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15887
0
         && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15888
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64
15889
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15890
0
  {
15891
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15892
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15893
0
  }
15894
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15895
0
         && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15896
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64
15897
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15898
0
  /* Keep the current setting.  */;
15899
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15900
0
         && in_fp == Val_GNU_MIPS_ABI_FP_64)
15901
0
  {
15902
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15903
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15904
0
  }
15905
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15906
0
         && out_fp == Val_GNU_MIPS_ABI_FP_64)
15907
0
  /* Keep the current setting.  */;
15908
0
      else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15909
0
  {
15910
0
    const char *out_string, *in_string;
15911
15912
0
    out_string = _bfd_mips_fp_abi_string (out_fp);
15913
0
    in_string = _bfd_mips_fp_abi_string (in_fp);
15914
    /* First warn about cases involving unrecognised ABIs.  */
15915
0
    if (!out_string && !in_string)
15916
      /* xgettext:c-format */
15917
0
      _bfd_error_handler
15918
0
        (_("warning: %pB uses unknown floating point ABI %d "
15919
0
     "(set by %pB), %pB uses unknown floating point ABI %d"),
15920
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15921
0
    else if (!out_string)
15922
0
      _bfd_error_handler
15923
        /* xgettext:c-format */
15924
0
        (_("warning: %pB uses unknown floating point ABI %d "
15925
0
     "(set by %pB), %pB uses %s"),
15926
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15927
0
    else if (!in_string)
15928
0
      _bfd_error_handler
15929
        /* xgettext:c-format */
15930
0
        (_("warning: %pB uses %s (set by %pB), "
15931
0
     "%pB uses unknown floating point ABI %d"),
15932
0
         obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15933
0
    else
15934
0
      {
15935
        /* If one of the bfds is soft-float, the other must be
15936
     hard-float.  The exact choice of hard-float ABI isn't
15937
     really relevant to the error message.  */
15938
0
        if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15939
0
    out_string = "-mhard-float";
15940
0
        else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15941
0
    in_string = "-mhard-float";
15942
0
        _bfd_error_handler
15943
    /* xgettext:c-format */
15944
0
    (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15945
0
     obfd, out_string, abi_fp_bfd, ibfd, in_string);
15946
0
      }
15947
0
  }
15948
0
    }
15949
15950
  /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15951
     non-conflicting ones.  */
15952
0
  if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15953
0
    {
15954
0
      out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15955
0
      if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15956
0
  out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15957
0
      else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15958
0
  switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15959
0
    {
15960
0
    case Val_GNU_MIPS_ABI_MSA_128:
15961
0
      _bfd_error_handler
15962
        /* xgettext:c-format */
15963
0
        (_("warning: %pB uses %s (set by %pB), "
15964
0
     "%pB uses unknown MSA ABI %d"),
15965
0
         obfd, "-mmsa", abi_msa_bfd,
15966
0
         ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15967
0
      break;
15968
15969
0
    default:
15970
0
      switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15971
0
        {
15972
0
        case Val_GNU_MIPS_ABI_MSA_128:
15973
0
    _bfd_error_handler
15974
      /* xgettext:c-format */
15975
0
      (_("warning: %pB uses unknown MSA ABI %d "
15976
0
         "(set by %pB), %pB uses %s"),
15977
0
         obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15978
0
       abi_msa_bfd, ibfd, "-mmsa");
15979
0
      break;
15980
15981
0
        default:
15982
0
    _bfd_error_handler
15983
      /* xgettext:c-format */
15984
0
      (_("warning: %pB uses unknown MSA ABI %d "
15985
0
         "(set by %pB), %pB uses unknown MSA ABI %d"),
15986
0
       obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15987
0
       abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15988
0
    break;
15989
0
        }
15990
0
    }
15991
0
    }
15992
15993
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
15994
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
15995
0
}
15996
15997
/* Merge object ABI flags from IBFD into OBFD.  Raise an error if
15998
   there are conflicting settings.  */
15999
16000
static bool
16001
mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
16002
0
{
16003
0
  obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
16004
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
16005
0
  struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
16006
16007
  /* Update the output abiflags fp_abi using the computed fp_abi.  */
16008
0
  out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
16009
16010
0
#define max(a, b) ((a) > (b) ? (a) : (b))
16011
  /* Merge abiflags.  */
16012
0
  out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
16013
0
               in_tdata->abiflags.isa_level);
16014
0
  out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
16015
0
             in_tdata->abiflags.isa_rev);
16016
0
  out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
16017
0
              in_tdata->abiflags.gpr_size);
16018
0
  out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
16019
0
               in_tdata->abiflags.cpr1_size);
16020
0
  out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
16021
0
               in_tdata->abiflags.cpr2_size);
16022
0
#undef max
16023
0
  out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
16024
0
  out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
16025
16026
0
  return true;
16027
0
}
16028
16029
/* Merge backend specific data from an object file to the output
16030
   object file when linking.  */
16031
16032
bool
16033
_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
16034
0
{
16035
0
  bfd *obfd = info->output_bfd;
16036
0
  struct mips_elf_obj_tdata *out_tdata;
16037
0
  struct mips_elf_obj_tdata *in_tdata;
16038
0
  bool null_input_bfd = true;
16039
0
  asection *sec;
16040
0
  bool ok;
16041
16042
  /* Check if we have the same endianness.  */
16043
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
16044
0
    {
16045
0
      _bfd_error_handler
16046
0
  (_("%pB: endianness incompatible with that of the selected emulation"),
16047
0
   ibfd);
16048
0
      return false;
16049
0
    }
16050
16051
0
  if (!is_mips_elf (ibfd))
16052
0
    return true;
16053
16054
0
  in_tdata = mips_elf_tdata (ibfd);
16055
0
  out_tdata = mips_elf_tdata (obfd);
16056
16057
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
16058
0
    {
16059
0
      _bfd_error_handler
16060
0
  (_("%pB: ABI is incompatible with that of the selected emulation"),
16061
0
   ibfd);
16062
0
      return false;
16063
0
    }
16064
16065
  /* Check to see if the input BFD actually contains any sections.  If not,
16066
     then it has no attributes, and its flags may not have been initialized
16067
     either, but it cannot actually cause any incompatibility.  */
16068
  /* FIXME: This excludes any input shared library from consideration.  */
16069
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
16070
0
    {
16071
      /* Ignore synthetic sections and empty .text, .data and .bss sections
16072
   which are automatically generated by gas.  Also ignore fake
16073
   (s)common sections, since merely defining a common symbol does
16074
   not affect compatibility.  */
16075
0
      if ((sec->flags & SEC_IS_COMMON) == 0
16076
0
    && strcmp (sec->name, ".reginfo")
16077
0
    && strcmp (sec->name, ".mdebug")
16078
0
    && (sec->size != 0
16079
0
        || (strcmp (sec->name, ".text")
16080
0
      && strcmp (sec->name, ".data")
16081
0
      && strcmp (sec->name, ".bss"))))
16082
0
  {
16083
0
    null_input_bfd = false;
16084
0
    break;
16085
0
  }
16086
0
    }
16087
0
  if (null_input_bfd)
16088
0
    return true;
16089
16090
  /* Populate abiflags using existing information.  */
16091
0
  if (in_tdata->abiflags_valid)
16092
0
    {
16093
0
      obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
16094
0
      Elf_Internal_ABIFlags_v0 in_abiflags;
16095
0
      Elf_Internal_ABIFlags_v0 abiflags;
16096
16097
      /* Set up the FP ABI attribute from the abiflags if it is not already
16098
   set.  */
16099
0
      if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
16100
0
  in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
16101
16102
0
      infer_mips_abiflags (ibfd, &abiflags);
16103
0
      in_abiflags = in_tdata->abiflags;
16104
16105
      /* It is not possible to infer the correct ISA revision
16106
   for R3 or R5 so drop down to R2 for the checks.  */
16107
0
      if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
16108
0
  in_abiflags.isa_rev = 2;
16109
16110
0
      if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
16111
0
    < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
16112
0
  _bfd_error_handler
16113
0
    (_("%pB: warning: inconsistent ISA between e_flags and "
16114
0
       ".MIPS.abiflags"), ibfd);
16115
0
      if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
16116
0
    && in_abiflags.fp_abi != abiflags.fp_abi)
16117
0
  _bfd_error_handler
16118
0
    (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
16119
0
       ".MIPS.abiflags"), ibfd);
16120
0
      if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
16121
0
  _bfd_error_handler
16122
0
    (_("%pB: warning: inconsistent ASEs between e_flags and "
16123
0
       ".MIPS.abiflags"), ibfd);
16124
      /* The isa_ext is allowed to be an extension of what can be inferred
16125
   from e_flags.  */
16126
0
      if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
16127
0
        bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
16128
0
  _bfd_error_handler
16129
0
    (_("%pB: warning: inconsistent ISA extensions between e_flags and "
16130
0
       ".MIPS.abiflags"), ibfd);
16131
0
      if (in_abiflags.flags2 != 0)
16132
0
  _bfd_error_handler
16133
0
    (_("%pB: warning: unexpected flag in the flags2 field of "
16134
0
       ".MIPS.abiflags (0x%lx)"), ibfd,
16135
0
     in_abiflags.flags2);
16136
0
    }
16137
0
  else
16138
0
    {
16139
0
      infer_mips_abiflags (ibfd, &in_tdata->abiflags);
16140
0
      in_tdata->abiflags_valid = true;
16141
0
    }
16142
16143
0
  if (!out_tdata->abiflags_valid)
16144
0
    {
16145
      /* Copy input abiflags if output abiflags are not already valid.  */
16146
0
      out_tdata->abiflags = in_tdata->abiflags;
16147
0
      out_tdata->abiflags_valid = true;
16148
0
    }
16149
16150
0
  if (! elf_flags_init (obfd))
16151
0
    {
16152
0
      elf_flags_init (obfd) = true;
16153
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16154
0
      elf_elfheader (obfd)->e_ident[EI_CLASS]
16155
0
  = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16156
16157
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16158
0
    && (bfd_get_arch_info (obfd)->the_default
16159
0
        || mips_mach_extends_p (bfd_get_mach (obfd),
16160
0
              bfd_get_mach (ibfd))))
16161
0
  {
16162
0
    if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16163
0
           bfd_get_mach (ibfd)))
16164
0
      return false;
16165
16166
    /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
16167
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16168
0
  }
16169
16170
0
      ok = true;
16171
0
    }
16172
0
  else
16173
0
    ok = mips_elf_merge_obj_e_flags (ibfd, info);
16174
16175
0
  ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16176
16177
0
  ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16178
16179
0
  if (!ok)
16180
0
    {
16181
0
      bfd_set_error (bfd_error_bad_value);
16182
0
      return false;
16183
0
    }
16184
16185
0
  return true;
16186
0
}
16187
16188
/* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
16189
16190
bool
16191
_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16192
0
{
16193
0
  BFD_ASSERT (!elf_flags_init (abfd)
16194
0
        || elf_elfheader (abfd)->e_flags == flags);
16195
16196
0
  elf_elfheader (abfd)->e_flags = flags;
16197
0
  elf_flags_init (abfd) = true;
16198
0
  return true;
16199
0
}
16200
16201
char *
16202
_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16203
110
{
16204
110
  switch (dtag)
16205
110
    {
16206
110
    default: return "";
16207
0
    case DT_MIPS_RLD_VERSION:
16208
0
      return "MIPS_RLD_VERSION";
16209
0
    case DT_MIPS_TIME_STAMP:
16210
0
      return "MIPS_TIME_STAMP";
16211
0
    case DT_MIPS_ICHECKSUM:
16212
0
      return "MIPS_ICHECKSUM";
16213
0
    case DT_MIPS_IVERSION:
16214
0
      return "MIPS_IVERSION";
16215
0
    case DT_MIPS_FLAGS:
16216
0
      return "MIPS_FLAGS";
16217
0
    case DT_MIPS_BASE_ADDRESS:
16218
0
      return "MIPS_BASE_ADDRESS";
16219
0
    case DT_MIPS_MSYM:
16220
0
      return "MIPS_MSYM";
16221
0
    case DT_MIPS_CONFLICT:
16222
0
      return "MIPS_CONFLICT";
16223
0
    case DT_MIPS_LIBLIST:
16224
0
      return "MIPS_LIBLIST";
16225
0
    case DT_MIPS_LOCAL_GOTNO:
16226
0
      return "MIPS_LOCAL_GOTNO";
16227
0
    case DT_MIPS_CONFLICTNO:
16228
0
      return "MIPS_CONFLICTNO";
16229
0
    case DT_MIPS_LIBLISTNO:
16230
0
      return "MIPS_LIBLISTNO";
16231
0
    case DT_MIPS_SYMTABNO:
16232
0
      return "MIPS_SYMTABNO";
16233
0
    case DT_MIPS_UNREFEXTNO:
16234
0
      return "MIPS_UNREFEXTNO";
16235
0
    case DT_MIPS_GOTSYM:
16236
0
      return "MIPS_GOTSYM";
16237
0
    case DT_MIPS_HIPAGENO:
16238
0
      return "MIPS_HIPAGENO";
16239
0
    case DT_MIPS_RLD_MAP:
16240
0
      return "MIPS_RLD_MAP";
16241
0
    case DT_MIPS_RLD_MAP_REL:
16242
0
      return "MIPS_RLD_MAP_REL";
16243
0
    case DT_MIPS_DELTA_CLASS:
16244
0
      return "MIPS_DELTA_CLASS";
16245
0
    case DT_MIPS_DELTA_CLASS_NO:
16246
0
      return "MIPS_DELTA_CLASS_NO";
16247
0
    case DT_MIPS_DELTA_INSTANCE:
16248
0
      return "MIPS_DELTA_INSTANCE";
16249
0
    case DT_MIPS_DELTA_INSTANCE_NO:
16250
0
      return "MIPS_DELTA_INSTANCE_NO";
16251
0
    case DT_MIPS_DELTA_RELOC:
16252
0
      return "MIPS_DELTA_RELOC";
16253
0
    case DT_MIPS_DELTA_RELOC_NO:
16254
0
      return "MIPS_DELTA_RELOC_NO";
16255
0
    case DT_MIPS_DELTA_SYM:
16256
0
      return "MIPS_DELTA_SYM";
16257
0
    case DT_MIPS_DELTA_SYM_NO:
16258
0
      return "MIPS_DELTA_SYM_NO";
16259
0
    case DT_MIPS_DELTA_CLASSSYM:
16260
0
      return "MIPS_DELTA_CLASSSYM";
16261
0
    case DT_MIPS_DELTA_CLASSSYM_NO:
16262
0
      return "MIPS_DELTA_CLASSSYM_NO";
16263
0
    case DT_MIPS_CXX_FLAGS:
16264
0
      return "MIPS_CXX_FLAGS";
16265
0
    case DT_MIPS_PIXIE_INIT:
16266
0
      return "MIPS_PIXIE_INIT";
16267
0
    case DT_MIPS_SYMBOL_LIB:
16268
0
      return "MIPS_SYMBOL_LIB";
16269
0
    case DT_MIPS_LOCALPAGE_GOTIDX:
16270
0
      return "MIPS_LOCALPAGE_GOTIDX";
16271
0
    case DT_MIPS_LOCAL_GOTIDX:
16272
0
      return "MIPS_LOCAL_GOTIDX";
16273
0
    case DT_MIPS_HIDDEN_GOTIDX:
16274
0
      return "MIPS_HIDDEN_GOTIDX";
16275
0
    case DT_MIPS_PROTECTED_GOTIDX:
16276
0
      return "MIPS_PROTECTED_GOT_IDX";
16277
0
    case DT_MIPS_OPTIONS:
16278
0
      return "MIPS_OPTIONS";
16279
0
    case DT_MIPS_INTERFACE:
16280
0
      return "MIPS_INTERFACE";
16281
0
    case DT_MIPS_DYNSTR_ALIGN:
16282
0
      return "DT_MIPS_DYNSTR_ALIGN";
16283
0
    case DT_MIPS_INTERFACE_SIZE:
16284
0
      return "DT_MIPS_INTERFACE_SIZE";
16285
0
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16286
0
      return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16287
0
    case DT_MIPS_PERF_SUFFIX:
16288
0
      return "DT_MIPS_PERF_SUFFIX";
16289
0
    case DT_MIPS_COMPACT_SIZE:
16290
0
      return "DT_MIPS_COMPACT_SIZE";
16291
0
    case DT_MIPS_GP_VALUE:
16292
0
      return "DT_MIPS_GP_VALUE";
16293
0
    case DT_MIPS_AUX_DYNAMIC:
16294
0
      return "DT_MIPS_AUX_DYNAMIC";
16295
0
    case DT_MIPS_PLTGOT:
16296
0
      return "DT_MIPS_PLTGOT";
16297
0
    case DT_MIPS_RWPLT:
16298
0
      return "DT_MIPS_RWPLT";
16299
0
    case DT_MIPS_XHASH:
16300
0
      return "DT_MIPS_XHASH";
16301
110
    }
16302
110
}
16303
16304
/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16305
   not known.  */
16306
16307
const char *
16308
_bfd_mips_fp_abi_string (int fp)
16309
0
{
16310
0
  switch (fp)
16311
0
    {
16312
      /* These strings aren't translated because they're simply
16313
   option lists.  */
16314
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16315
0
      return "-mdouble-float";
16316
16317
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16318
0
      return "-msingle-float";
16319
16320
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16321
0
      return "-msoft-float";
16322
16323
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16324
0
      return _("-mips32r2 -mfp64 (12 callee-saved)");
16325
16326
0
    case Val_GNU_MIPS_ABI_FP_XX:
16327
0
      return "-mfpxx";
16328
16329
0
    case Val_GNU_MIPS_ABI_FP_64:
16330
0
      return "-mgp32 -mfp64";
16331
16332
0
    case Val_GNU_MIPS_ABI_FP_64A:
16333
0
      return "-mgp32 -mfp64 -mno-odd-spreg";
16334
16335
0
    default:
16336
0
      return 0;
16337
0
    }
16338
0
}
16339
16340
static void
16341
print_mips_ases (FILE *file, unsigned int mask)
16342
0
{
16343
0
  if (mask & AFL_ASE_DSP)
16344
0
    fputs ("\n\tDSP ASE", file);
16345
0
  if (mask & AFL_ASE_DSPR2)
16346
0
    fputs ("\n\tDSP R2 ASE", file);
16347
0
  if (mask & AFL_ASE_DSPR3)
16348
0
    fputs ("\n\tDSP R3 ASE", file);
16349
0
  if (mask & AFL_ASE_EVA)
16350
0
    fputs ("\n\tEnhanced VA Scheme", file);
16351
0
  if (mask & AFL_ASE_MCU)
16352
0
    fputs ("\n\tMCU (MicroController) ASE", file);
16353
0
  if (mask & AFL_ASE_MDMX)
16354
0
    fputs ("\n\tMDMX ASE", file);
16355
0
  if (mask & AFL_ASE_MIPS3D)
16356
0
    fputs ("\n\tMIPS-3D ASE", file);
16357
0
  if (mask & AFL_ASE_MT)
16358
0
    fputs ("\n\tMT ASE", file);
16359
0
  if (mask & AFL_ASE_SMARTMIPS)
16360
0
    fputs ("\n\tSmartMIPS ASE", file);
16361
0
  if (mask & AFL_ASE_VIRT)
16362
0
    fputs ("\n\tVZ ASE", file);
16363
0
  if (mask & AFL_ASE_MSA)
16364
0
    fputs ("\n\tMSA ASE", file);
16365
0
  if (mask & AFL_ASE_MIPS16)
16366
0
    fputs ("\n\tMIPS16 ASE", file);
16367
0
  if (mask & AFL_ASE_MICROMIPS)
16368
0
    fputs ("\n\tMICROMIPS ASE", file);
16369
0
  if (mask & AFL_ASE_XPA)
16370
0
    fputs ("\n\tXPA ASE", file);
16371
0
  if (mask & AFL_ASE_MIPS16E2)
16372
0
    fputs ("\n\tMIPS16e2 ASE", file);
16373
0
  if (mask & AFL_ASE_CRC)
16374
0
    fputs ("\n\tCRC ASE", file);
16375
0
  if (mask & AFL_ASE_GINV)
16376
0
    fputs ("\n\tGINV ASE", file);
16377
0
  if (mask & AFL_ASE_LOONGSON_MMI)
16378
0
    fputs ("\n\tLoongson MMI ASE", file);
16379
0
  if (mask & AFL_ASE_LOONGSON_CAM)
16380
0
    fputs ("\n\tLoongson CAM ASE", file);
16381
0
  if (mask & AFL_ASE_LOONGSON_EXT)
16382
0
    fputs ("\n\tLoongson EXT ASE", file);
16383
0
  if (mask & AFL_ASE_LOONGSON_EXT2)
16384
0
    fputs ("\n\tLoongson EXT2 ASE", file);
16385
0
  if (mask == 0)
16386
0
    fprintf (file, "\n\t%s", _("None"));
16387
0
  else if ((mask & ~AFL_ASE_MASK) != 0)
16388
0
    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16389
0
}
16390
16391
static void
16392
print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16393
0
{
16394
0
  switch (isa_ext)
16395
0
    {
16396
0
    case 0:
16397
0
      fputs (_("None"), file);
16398
0
      break;
16399
0
    case AFL_EXT_XLR:
16400
0
      fputs ("RMI XLR", file);
16401
0
      break;
16402
0
    case AFL_EXT_OCTEON3:
16403
0
      fputs ("Cavium Networks Octeon3", file);
16404
0
      break;
16405
0
    case AFL_EXT_OCTEON2:
16406
0
      fputs ("Cavium Networks Octeon2", file);
16407
0
      break;
16408
0
    case AFL_EXT_OCTEONP:
16409
0
      fputs ("Cavium Networks OcteonP", file);
16410
0
      break;
16411
0
    case AFL_EXT_OCTEON:
16412
0
      fputs ("Cavium Networks Octeon", file);
16413
0
      break;
16414
0
    case AFL_EXT_5900:
16415
0
      fputs ("Toshiba R5900", file);
16416
0
      break;
16417
0
    case AFL_EXT_4650:
16418
0
      fputs ("MIPS R4650", file);
16419
0
      break;
16420
0
    case AFL_EXT_4010:
16421
0
      fputs ("LSI R4010", file);
16422
0
      break;
16423
0
    case AFL_EXT_4100:
16424
0
      fputs ("NEC VR4100", file);
16425
0
      break;
16426
0
    case AFL_EXT_3900:
16427
0
      fputs ("Toshiba R3900", file);
16428
0
      break;
16429
0
    case AFL_EXT_10000:
16430
0
      fputs ("MIPS R10000", file);
16431
0
      break;
16432
0
    case AFL_EXT_SB1:
16433
0
      fputs ("Broadcom SB-1", file);
16434
0
      break;
16435
0
    case AFL_EXT_4111:
16436
0
      fputs ("NEC VR4111/VR4181", file);
16437
0
      break;
16438
0
    case AFL_EXT_4120:
16439
0
      fputs ("NEC VR4120", file);
16440
0
      break;
16441
0
    case AFL_EXT_5400:
16442
0
      fputs ("NEC VR5400", file);
16443
0
      break;
16444
0
    case AFL_EXT_5500:
16445
0
      fputs ("NEC VR5500", file);
16446
0
      break;
16447
0
    case AFL_EXT_LOONGSON_2E:
16448
0
      fputs ("ST Microelectronics Loongson 2E", file);
16449
0
      break;
16450
0
    case AFL_EXT_LOONGSON_2F:
16451
0
      fputs ("ST Microelectronics Loongson 2F", file);
16452
0
      break;
16453
0
    case AFL_EXT_INTERAPTIV_MR2:
16454
0
      fputs ("Imagination interAptiv MR2", file);
16455
0
      break;
16456
0
    default:
16457
0
      fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16458
0
      break;
16459
0
    }
16460
0
}
16461
16462
static void
16463
print_mips_fp_abi_value (FILE *file, int val)
16464
0
{
16465
0
  switch (val)
16466
0
    {
16467
0
    case Val_GNU_MIPS_ABI_FP_ANY:
16468
0
      fprintf (file, _("Hard or soft float\n"));
16469
0
      break;
16470
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16471
0
      fprintf (file, _("Hard float (double precision)\n"));
16472
0
      break;
16473
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16474
0
      fprintf (file, _("Hard float (single precision)\n"));
16475
0
      break;
16476
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16477
0
      fprintf (file, _("Soft float\n"));
16478
0
      break;
16479
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16480
0
      fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16481
0
      break;
16482
0
    case Val_GNU_MIPS_ABI_FP_XX:
16483
0
      fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16484
0
      break;
16485
0
    case Val_GNU_MIPS_ABI_FP_64:
16486
0
      fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16487
0
      break;
16488
0
    case Val_GNU_MIPS_ABI_FP_64A:
16489
0
      fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16490
0
      break;
16491
0
    default:
16492
0
      fprintf (file, "??? (%d)\n", val);
16493
0
      break;
16494
0
    }
16495
0
}
16496
16497
static int
16498
get_mips_reg_size (int reg_size)
16499
0
{
16500
0
  return (reg_size == AFL_REG_NONE) ? 0
16501
0
   : (reg_size == AFL_REG_32) ? 32
16502
0
   : (reg_size == AFL_REG_64) ? 64
16503
0
   : (reg_size == AFL_REG_128) ? 128
16504
0
   : -1;
16505
0
}
16506
16507
bool
16508
_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16509
590
{
16510
590
  FILE *file = ptr;
16511
16512
590
  BFD_ASSERT (abfd != NULL && ptr != NULL);
16513
16514
  /* Print normal ELF private data.  */
16515
590
  _bfd_elf_print_private_bfd_data (abfd, ptr);
16516
16517
  /* xgettext:c-format */
16518
590
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16519
16520
590
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
16521
12
    fprintf (file, _(" [abi=O32]"));
16522
578
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
16523
0
    fprintf (file, _(" [abi=O64]"));
16524
578
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
16525
0
    fprintf (file, _(" [abi=EABI32]"));
16526
578
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
16527
0
    fprintf (file, _(" [abi=EABI64]"));
16528
578
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16529
140
    fprintf (file, _(" [abi unknown]"));
16530
438
  else if (ABI_N32_P (abfd))
16531
114
    fprintf (file, _(" [abi=N32]"));
16532
324
  else if (ABI_64_P (abfd))
16533
155
    fprintf (file, _(" [abi=64]"));
16534
169
  else
16535
169
    fprintf (file, _(" [no abi set]"));
16536
16537
590
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
16538
253
    fprintf (file, " [mips1]");
16539
337
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
16540
1
    fprintf (file, " [mips2]");
16541
336
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
16542
88
    fprintf (file, " [mips3]");
16543
248
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
16544
0
    fprintf (file, " [mips4]");
16545
248
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
16546
2
    fprintf (file, " [mips5]");
16547
246
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
16548
14
    fprintf (file, " [mips32]");
16549
232
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
16550
2
    fprintf (file, " [mips64]");
16551
230
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
16552
54
    fprintf (file, " [mips32r2]");
16553
176
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
16554
0
    fprintf (file, " [mips64r2]");
16555
176
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
16556
73
    fprintf (file, " [mips32r6]");
16557
103
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
16558
90
    fprintf (file, " [mips64r6]");
16559
13
  else
16560
13
    fprintf (file, _(" [unknown ISA]"));
16561
16562
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16563
119
    fprintf (file, " [mdmx]");
16564
16565
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16566
195
    fprintf (file, " [mips16]");
16567
16568
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16569
278
    fprintf (file, " [micromips]");
16570
16571
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16572
144
    fprintf (file, " [nan2008]");
16573
16574
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16575
150
    fprintf (file, " [old fp64]");
16576
16577
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16578
274
    fprintf (file, " [32bitmode]");
16579
316
  else
16580
316
    fprintf (file, _(" [not 32bitmode]"));
16581
16582
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16583
245
    fprintf (file, " [noreorder]");
16584
16585
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16586
24
    fprintf (file, " [PIC]");
16587
16588
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16589
244
    fprintf (file, " [CPIC]");
16590
16591
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16592
36
    fprintf (file, " [XGOT]");
16593
16594
590
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16595
253
    fprintf (file, " [UCODE]");
16596
16597
590
  fputc ('\n', file);
16598
16599
590
  if (mips_elf_tdata (abfd)->abiflags_valid)
16600
0
    {
16601
0
      Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16602
0
      fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16603
0
      fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16604
0
      if (abiflags->isa_rev > 1)
16605
0
  fprintf (file, "r%d", abiflags->isa_rev);
16606
0
      fprintf (file, "\nGPR size: %d",
16607
0
         get_mips_reg_size (abiflags->gpr_size));
16608
0
      fprintf (file, "\nCPR1 size: %d",
16609
0
         get_mips_reg_size (abiflags->cpr1_size));
16610
0
      fprintf (file, "\nCPR2 size: %d",
16611
0
         get_mips_reg_size (abiflags->cpr2_size));
16612
0
      fputs ("\nFP ABI: ", file);
16613
0
      print_mips_fp_abi_value (file, abiflags->fp_abi);
16614
0
      fputs ("ISA Extension: ", file);
16615
0
      print_mips_isa_ext (file, abiflags->isa_ext);
16616
0
      fputs ("\nASEs:", file);
16617
0
      print_mips_ases (file, abiflags->ases);
16618
0
      fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16619
0
      fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16620
0
      fputc ('\n', file);
16621
0
    }
16622
16623
590
  return true;
16624
590
}
16625
16626
const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16627
{
16628
  { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16629
  { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16630
  { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16631
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16632
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16633
  { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
16634
  { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
16635
  { NULL,         0,  0, 0,        0 }
16636
};
16637
16638
/* Merge non visibility st_other attributes.  Ensure that the
16639
   STO_OPTIONAL flag is copied into h->other, even if this is not a
16640
   definiton of the symbol.  */
16641
void
16642
_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16643
              unsigned int st_other,
16644
              bool definition,
16645
              bool dynamic ATTRIBUTE_UNUSED)
16646
0
{
16647
0
  if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16648
0
    {
16649
0
      unsigned char other;
16650
16651
0
      other = (definition ? st_other : h->other);
16652
0
      other &= ~ELF_ST_VISIBILITY (-1);
16653
0
      h->other = other | ELF_ST_VISIBILITY (h->other);
16654
0
    }
16655
16656
0
  if (!definition
16657
0
      && ELF_MIPS_IS_OPTIONAL (st_other))
16658
0
    h->other |= STO_OPTIONAL;
16659
0
}
16660
16661
/* Decide whether an undefined symbol is special and can be ignored.
16662
   This is the case for OPTIONAL symbols on IRIX.  */
16663
bool
16664
_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16665
0
{
16666
0
  return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16667
0
}
16668
16669
bool
16670
_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16671
0
{
16672
0
  return (sym->st_shndx == SHN_COMMON
16673
0
    || sym->st_shndx == SHN_MIPS_ACOMMON
16674
0
    || sym->st_shndx == SHN_MIPS_SCOMMON);
16675
0
}
16676
16677
/* Return address for Ith PLT stub in section PLT, for relocation REL
16678
   or (bfd_vma) -1 if it should not be included.  */
16679
16680
bfd_vma
16681
_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16682
         const arelent *rel ATTRIBUTE_UNUSED)
16683
0
{
16684
0
  return (plt->vma
16685
0
    + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16686
0
    + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16687
0
}
16688
16689
/* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
16690
   and microMIPS PLT slots we may have a many-to-one mapping between .plt
16691
   and .got.plt and also the slots may be of a different size each we walk
16692
   the PLT manually fetching instructions and matching them against known
16693
   patterns.  To make things easier standard MIPS slots, if any, always come
16694
   first.  As we don't create proper ELF symbols we use the UDATA.I member
16695
   of ASYMBOL to carry ISA annotation.  The encoding used is the same as
16696
   with the ST_OTHER member of the ELF symbol.  */
16697
16698
long
16699
_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16700
            long symcount ATTRIBUTE_UNUSED,
16701
            asymbol **syms ATTRIBUTE_UNUSED,
16702
            long dynsymcount, asymbol **dynsyms,
16703
            asymbol **ret)
16704
169
{
16705
169
  static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16706
169
  static const char microsuffix[] = "@micromipsplt";
16707
169
  static const char m16suffix[] = "@mips16plt";
16708
169
  static const char mipssuffix[] = "@plt";
16709
16710
169
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16711
169
  elf_backend_data *bed = get_elf_backend_data (abfd);
16712
169
  bool micromips_p = MICROMIPS_P (abfd);
16713
169
  Elf_Internal_Shdr *hdr;
16714
169
  bfd_byte *plt_data;
16715
169
  bfd_vma plt_offset;
16716
169
  unsigned int other;
16717
169
  bfd_vma entry_size;
16718
169
  bfd_vma plt0_size;
16719
169
  asection *relplt;
16720
169
  bfd_vma opcode;
16721
169
  asection *plt;
16722
169
  asymbol *send;
16723
169
  size_t size;
16724
169
  char *names;
16725
169
  long counti;
16726
169
  arelent *p;
16727
169
  asymbol *s;
16728
169
  char *nend;
16729
169
  long count;
16730
169
  long pi;
16731
169
  long i;
16732
169
  long n;
16733
16734
169
  *ret = NULL;
16735
16736
169
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16737
169
    return 0;
16738
16739
0
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16740
0
  if (relplt == NULL)
16741
0
    return 0;
16742
16743
0
  hdr = &elf_section_data (relplt)->this_hdr;
16744
0
  if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16745
0
    return 0;
16746
16747
0
  plt = bfd_get_section_by_name (abfd, ".plt");
16748
0
  if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16749
0
    return 0;
16750
16751
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16752
0
  if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16753
0
    return -1;
16754
0
  p = relplt->relocation;
16755
16756
  /* Calculating the exact amount of space required for symbols would
16757
     require two passes over the PLT, so just pessimise assuming two
16758
     PLT slots per relocation.  */
16759
0
  count = NUM_SHDR_ENTRIES (hdr);
16760
0
  counti = count * bed->s->int_rels_per_ext_rel;
16761
0
  size = 2 * count * sizeof (asymbol);
16762
0
  size += count * (sizeof (mipssuffix) +
16763
0
       (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16764
0
  for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16765
0
    size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16766
16767
  /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
16768
0
  size += sizeof (asymbol) + sizeof (pltname);
16769
16770
0
  if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16771
0
    return -1;
16772
16773
0
  if (plt->size < 16)
16774
0
    return -1;
16775
16776
0
  s = *ret = bfd_malloc (size);
16777
0
  if (s == NULL)
16778
0
    return -1;
16779
0
  send = s + 2 * count + 1;
16780
16781
0
  names = (char *) send;
16782
0
  nend = (char *) s + size;
16783
0
  n = 0;
16784
16785
0
  opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16786
0
  if (opcode == 0x3302fffe)
16787
0
    {
16788
0
      if (!micromips_p)
16789
0
  return -1;
16790
0
      plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16791
0
      other = STO_MICROMIPS;
16792
0
    }
16793
0
  else if (opcode == 0x0398c1d0)
16794
0
    {
16795
0
      if (!micromips_p)
16796
0
  return -1;
16797
0
      plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16798
0
      other = STO_MICROMIPS;
16799
0
    }
16800
0
  else
16801
0
    {
16802
0
      plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16803
0
      other = 0;
16804
0
    }
16805
16806
0
  s->the_bfd = abfd;
16807
0
  s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16808
0
  s->section = plt;
16809
0
  s->value = 0;
16810
0
  s->name = names;
16811
0
  s->udata.i = other;
16812
0
  memcpy (names, pltname, sizeof (pltname));
16813
0
  names += sizeof (pltname);
16814
0
  ++s, ++n;
16815
16816
0
  pi = 0;
16817
0
  for (plt_offset = plt0_size;
16818
0
       plt_offset + 8 <= plt->size && s < send;
16819
0
       plt_offset += entry_size)
16820
0
    {
16821
0
      bfd_vma gotplt_addr;
16822
0
      const char *suffix;
16823
0
      bfd_vma gotplt_hi;
16824
0
      bfd_vma gotplt_lo;
16825
0
      size_t suffixlen;
16826
16827
0
      opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16828
16829
      /* Check if the second word matches the expected MIPS16 instruction.  */
16830
0
      if (opcode == 0x651aeb00)
16831
0
  {
16832
0
    if (micromips_p)
16833
0
      return -1;
16834
    /* Truncated table???  */
16835
0
    if (plt_offset + 16 > plt->size)
16836
0
      break;
16837
0
    gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16838
0
    entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16839
0
    suffixlen = sizeof (m16suffix);
16840
0
    suffix = m16suffix;
16841
0
    other = STO_MIPS16;
16842
0
  }
16843
      /* Likewise the expected microMIPS instruction (no insn32 mode).  */
16844
0
      else if (opcode == 0xff220000)
16845
0
  {
16846
0
    if (!micromips_p)
16847
0
      return -1;
16848
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16849
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16850
0
    gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16851
0
    gotplt_lo <<= 2;
16852
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16853
0
    gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16854
0
    entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16855
0
    suffixlen = sizeof (microsuffix);
16856
0
    suffix = microsuffix;
16857
0
    other = STO_MICROMIPS;
16858
0
  }
16859
      /* Likewise the expected microMIPS instruction (insn32 mode).  */
16860
0
      else if ((opcode & 0xffff0000) == 0xff2f0000)
16861
0
  {
16862
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16863
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16864
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16865
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16866
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16867
0
    entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16868
0
    suffixlen = sizeof (microsuffix);
16869
0
    suffix = microsuffix;
16870
0
    other = STO_MICROMIPS;
16871
0
  }
16872
      /* Otherwise assume standard MIPS code.  */
16873
0
      else
16874
0
  {
16875
0
    gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16876
0
    gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16877
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16878
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16879
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16880
0
    entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16881
0
    suffixlen = sizeof (mipssuffix);
16882
0
    suffix = mipssuffix;
16883
0
    other = 0;
16884
0
  }
16885
      /* Truncated table???  */
16886
0
      if (plt_offset + entry_size > plt->size)
16887
0
  break;
16888
16889
0
      for (i = 0;
16890
0
     i < count && p[pi].address != gotplt_addr;
16891
0
     i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16892
16893
0
      if (i < count)
16894
0
  {
16895
0
    size_t namelen;
16896
0
    size_t len;
16897
16898
0
    *s = **p[pi].sym_ptr_ptr;
16899
    /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
16900
       we are defining a symbol, ensure one of them is set.  */
16901
0
    if ((s->flags & BSF_LOCAL) == 0)
16902
0
      s->flags |= BSF_GLOBAL;
16903
0
    s->flags |= BSF_SYNTHETIC;
16904
0
    s->section = plt;
16905
0
    s->value = plt_offset;
16906
0
    s->name = names;
16907
0
    s->udata.i = other;
16908
16909
0
    len = strlen ((*p[pi].sym_ptr_ptr)->name);
16910
0
    namelen = len + suffixlen;
16911
0
    if (names + namelen > nend)
16912
0
      break;
16913
16914
0
    memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16915
0
    names += len;
16916
0
    memcpy (names, suffix, suffixlen);
16917
0
    names += suffixlen;
16918
16919
0
    ++s, ++n;
16920
0
    pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16921
0
  }
16922
0
    }
16923
16924
0
  free (plt_data);
16925
16926
0
  return n;
16927
0
}
16928
16929
/* Return the ABI flags associated with ABFD if available.  */
16930
16931
Elf_Internal_ABIFlags_v0 *
16932
bfd_mips_elf_get_abiflags (bfd *abfd)
16933
968k
{
16934
968k
  struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16935
16936
968k
  return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16937
968k
}
16938
16939
/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16940
   field.  Taken from `libc-abis.h' generated at GNU libc build time.
16941
   Using a MIPS_ prefix as other libc targets use different values.  */
16942
enum
16943
{
16944
  MIPS_LIBC_ABI_DEFAULT = 0,
16945
  MIPS_LIBC_ABI_MIPS_PLT,
16946
  MIPS_LIBC_ABI_UNIQUE,
16947
  MIPS_LIBC_ABI_MIPS_O32_FP64,
16948
  MIPS_LIBC_ABI_ABSOLUTE,
16949
  MIPS_LIBC_ABI_XHASH,
16950
  MIPS_LIBC_ABI_MAX
16951
};
16952
16953
bool
16954
_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16955
90
{
16956
90
  struct mips_elf_link_hash_table *htab = NULL;
16957
90
  Elf_Internal_Ehdr *i_ehdrp;
16958
16959
90
  if (!_bfd_elf_init_file_header (abfd, link_info))
16960
0
    return false;
16961
16962
90
  i_ehdrp = elf_elfheader (abfd);
16963
90
  if (link_info)
16964
0
    {
16965
0
      htab = mips_elf_hash_table (link_info);
16966
0
      BFD_ASSERT (htab != NULL);
16967
0
    }
16968
16969
90
  if (htab != NULL
16970
0
      && htab->use_plts_and_copy_relocs
16971
0
      && htab->root.target_os != is_vxworks)
16972
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16973
16974
90
  if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16975
90
      || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16976
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16977
16978
  /* Mark that we need support for absolute symbols in the dynamic loader.  */
16979
90
  if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16980
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16981
16982
  /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16983
     if it is the only hash section that will be created.  */
16984
90
  if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16985
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16986
90
  return true;
16987
90
}
16988
16989
int
16990
_bfd_mips_elf_compact_eh_encoding
16991
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16992
0
{
16993
0
  return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16994
0
}
16995
16996
/* Return the opcode for can't unwind.  */
16997
16998
int
16999
_bfd_mips_elf_cant_unwind_opcode
17000
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
17001
0
{
17002
0
  return COMPACT_EH_CANT_UNWIND_OPCODE;
17003
0
}
17004
17005
/* Record a position XLAT_LOC in the xlat translation table, associated with
17006
   the hash entry H.  The entry in the translation table will later be
17007
   populated with the real symbol dynindx.  */
17008
17009
void
17010
_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
17011
           bfd_vma xlat_loc)
17012
0
{
17013
0
  struct mips_elf_link_hash_entry *hmips;
17014
17015
0
  hmips = (struct mips_elf_link_hash_entry *) h;
17016
0
  hmips->mipsxhash_loc = xlat_loc;
17017
0
}