]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-mips.c
* elf32-i386.c (elf_i386_relocate_section): If -Bsymbolic, when
[thirdparty/binutils-gdb.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "bfdlink.h"
31 #include "genlink.h"
32 #include "elf-bfd.h"
33 #include "elf/mips.h"
34
35 /* Get the ECOFF swapping routines. */
36 #include "coff/sym.h"
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
41 #define ECOFF_32
42 #include "ecoffswap.h"
43
44 static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd,
45 arelent *reloc,
46 asymbol *symbol,
47 PTR data,
48 asection *section,
49 bfd *output_bfd,
50 char **error));
51 static bfd_reloc_status_type mips_elf_got16_reloc PARAMS ((bfd *abfd,
52 arelent *reloc,
53 asymbol *symbol,
54 PTR data,
55 asection *section,
56 bfd *output_bfd,
57 char **error));
58 static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd,
59 arelent *reloc,
60 asymbol *symbol,
61 PTR data,
62 asection *section,
63 bfd *output_bfd,
64 char **error));
65 static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd,
66 arelent *reloc,
67 asymbol *symbol,
68 PTR data,
69 asection *section,
70 bfd *output_bfd,
71 char **error));
72 static bfd_reloc_status_type mips_elf_gprel32_reloc PARAMS ((bfd *abfd,
73 arelent *reloc,
74 asymbol *symbol,
75 PTR data,
76 asection *section,
77 bfd *output_bfd,
78 char **error));
79 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
80 PARAMS ((bfd *, bfd_reloc_code_real_type));
81 static void mips_info_to_howto_rel
82 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
83 static void bfd_mips_elf32_swap_gptab_in
84 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
85 static void bfd_mips_elf32_swap_gptab_out
86 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
87 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
88 static boolean mips_elf_object_p PARAMS ((bfd *));
89 static boolean mips_elf_create_procedure_table
90 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
91 struct ecoff_debug_info *));
92 static int mips_elf_additional_program_headers PARAMS ((bfd *));
93 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
94 static void mips_elf_final_write_processing
95 PARAMS ((bfd *, boolean));
96 static boolean mips_elf_set_private_flags PARAMS ((bfd *, flagword));
97 static boolean mips_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
98 static boolean mips_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
99 static boolean mips_elf_section_from_shdr
100 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
101 static boolean mips_elf_fake_sections
102 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
103 static boolean mips_elf_section_from_bfd_section
104 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
105 static boolean mips_elf_section_processing
106 PARAMS ((bfd *, Elf32_Internal_Shdr *));
107 static void mips_elf_symbol_processing PARAMS ((bfd *, asymbol *));
108 static boolean mips_elf_read_ecoff_info
109 PARAMS ((bfd *, asection *, struct ecoff_debug_info *));
110 static boolean mips_elf_is_local_label
111 PARAMS ((bfd *, asymbol *));
112 static boolean mips_elf_find_nearest_line
113 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
114 const char **, unsigned int *));
115 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
116 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
117 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
118 PARAMS ((bfd *));
119 static int gptab_compare PARAMS ((const void *, const void *));
120 static boolean mips_elf_final_link
121 PARAMS ((bfd *, struct bfd_link_info *));
122 static void mips_elf_relocate_hi16
123 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
124 bfd_vma));
125 static void mips_elf_relocate_got_local
126 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
127 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
128 static void mips_elf_relocate_global_got
129 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
130 static boolean mips_elf_adjust_dynindx
131 PARAMS ((struct elf_link_hash_entry *, PTR));
132 static boolean mips_elf_relocate_section
133 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
134 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
135 static boolean mips_elf_create_dynamic_sections
136 PARAMS ((bfd *, struct bfd_link_info *));
137 static boolean mips_elf_create_compact_rel_section
138 PARAMS ((bfd *, struct bfd_link_info *));
139 static boolean mips_elf_create_got_section
140 PARAMS ((bfd *, struct bfd_link_info *));
141 static boolean mips_elf_check_relocs
142 PARAMS ((bfd *, struct bfd_link_info *, asection *,
143 const Elf_Internal_Rela *));
144 static boolean mips_elf_adjust_dynamic_symbol
145 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
146 static boolean mips_elf_size_dynamic_sections
147 PARAMS ((bfd *, struct bfd_link_info *));
148 static boolean mips_elf_finish_dynamic_symbol
149 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
150 Elf_Internal_Sym *));
151 static boolean mips_elf_finish_dynamic_sections
152 PARAMS ((bfd *, struct bfd_link_info *));
153 static boolean mips_elf_add_symbol_hook
154 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
155 const char **, flagword *, asection **, bfd_vma *));
156 static bfd_reloc_status_type mips_elf_final_gp
157 PARAMS ((bfd *, asymbol *, boolean, char **));
158 static bfd_byte *elf32_mips_get_relocated_section_contents
159 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
160 bfd_byte *, boolean, asymbol **));
161
162 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
163 executables. FIXME: At the moment, we default to always generating
164 Irix 5 executables. */
165
166 #define SGI_COMPAT(abfd) (1)
167
168 /* This structure is used to hold .got information when linking. It
169 is stored in the tdata field of the bfd_elf_section_data structure. */
170
171 struct mips_got_info
172 {
173 /* The symbol index of the first global .got symbol. */
174 unsigned long global_gotsym;
175 /* The number of local .got entries. */
176 unsigned int local_gotno;
177 };
178
179 /* The number of local .got entries we reserve. */
180 #define MIPS_RESERVED_GOTNO (2)
181
182 /* Instructions which appear in a stub. For some reason the stub is
183 slightly different on an SGI system. */
184 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
185 #define STUB_LW(abfd) \
186 (SGI_COMPAT (abfd) \
187 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
188 : 0x8f998000) /* lw t9,0x8000(gp) */
189 #define STUB_MOVE 0x03e07825 /* move t7,ra */
190 #define STUB_JALR 0x0320f809 /* jal t9 */
191 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
192 #define MIPS_FUNCTION_STUB_SIZE (16)
193
194 /* Names of sections which appear in the .dynsym section in an Irix 5
195 executable. */
196
197 static const char * const mips_elf_dynsym_sec_names[] =
198 {
199 ".text",
200 ".init",
201 ".fini",
202 ".data",
203 ".rodata",
204 ".sdata",
205 ".sbss",
206 ".bss",
207 NULL
208 };
209
210 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
211 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
212
213 /* The number of entries in mips_elf_dynsym_sec_names which go in the
214 text segment. */
215
216 #define MIPS_TEXT_DYNSYM_SECNO (3)
217
218 /* The names of the runtime procedure table symbols used on Irix 5. */
219
220 static const char * const mips_elf_dynsym_rtproc_names[] =
221 {
222 "_procedure_table",
223 "_procedure_string_table",
224 "_procedure_table_size",
225 NULL
226 };
227
228 /* These structures are used to generate the .compact_rel section on
229 Irix 5. */
230
231 typedef struct
232 {
233 unsigned long id1; /* Always one? */
234 unsigned long num; /* Number of compact relocation entries. */
235 unsigned long id2; /* Always two? */
236 unsigned long offset; /* The file offset of the first relocation. */
237 unsigned long reserved0; /* Zero? */
238 unsigned long reserved1; /* Zero? */
239 } Elf32_compact_rel;
240
241 typedef struct
242 {
243 bfd_byte id1[4];
244 bfd_byte num[4];
245 bfd_byte id2[4];
246 bfd_byte offset[4];
247 bfd_byte reserved0[4];
248 bfd_byte reserved1[4];
249 } Elf32_External_compact_rel;
250
251 typedef struct
252 {
253 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
254 unsigned int rtype : 4; /* Relocation types. See below. */
255 unsigned int dist2to : 8;
256 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
257 unsigned long konst; /* KONST field. See below. */
258 unsigned long vaddr; /* VADDR to be relocated. */
259 } Elf32_crinfo;
260
261 typedef struct
262 {
263 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
264 unsigned int rtype : 4; /* Relocation types. See below. */
265 unsigned int dist2to : 8;
266 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
267 unsigned long konst; /* KONST field. See below. */
268 } Elf32_crinfo2;
269
270 typedef struct
271 {
272 bfd_byte info[4];
273 bfd_byte konst[4];
274 bfd_byte vaddr[4];
275 } Elf32_External_crinfo;
276
277 typedef struct
278 {
279 bfd_byte info[4];
280 bfd_byte konst[4];
281 } Elf32_External_crinfo2;
282
283 /* These are the constants used to swap the bitfields in a crinfo. */
284
285 #define CRINFO_CTYPE (0x1)
286 #define CRINFO_CTYPE_SH (31)
287 #define CRINFO_RTYPE (0xf)
288 #define CRINFO_RTYPE_SH (27)
289 #define CRINFO_DIST2TO (0xff)
290 #define CRINFO_DIST2TO_SH (19)
291 #define CRINFO_RELVADDR (0x7ffff)
292 #define CRINFO_RELVADDR_SH (0)
293
294 /* A compact relocation info has long (3 words) or short (2 words)
295 formats. A short format doesn't have VADDR field and relvaddr
296 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
297 #define CRF_MIPS_LONG 1
298 #define CRF_MIPS_SHORT 0
299
300 /* There are 4 types of compact relocation at least. The value KONST
301 has different meaning for each type:
302
303 (type) (konst)
304 CT_MIPS_REL32 Address in data
305 CT_MIPS_WORD Address in word (XXX)
306 CT_MIPS_GPHI_LO GP - vaddr
307 CT_MIPS_JMPAD Address to jump
308 */
309
310 #define CRT_MIPS_REL32 0xa
311 #define CRT_MIPS_WORD 0xb
312 #define CRT_MIPS_GPHI_LO 0xc
313 #define CRT_MIPS_JMPAD 0xd
314
315 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
316 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
317 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
318 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
319
320 static void bfd_elf32_swap_compact_rel_out
321 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
322 static void bfd_elf32_swap_crinfo_out
323 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
324
325 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
326
327 enum reloc_type
328 {
329 R_MIPS_NONE = 0,
330 R_MIPS_16, R_MIPS_32,
331 R_MIPS_REL32, R_MIPS_26,
332 R_MIPS_HI16, R_MIPS_LO16,
333 R_MIPS_GPREL16, R_MIPS_LITERAL,
334 R_MIPS_GOT16, R_MIPS_PC16,
335 R_MIPS_CALL16, R_MIPS_GPREL32,
336 /* The remaining relocs are defined on Irix, although they are not
337 in the MIPS ELF ABI. */
338 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
339 R_MIPS_UNUSED3,
340 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
341 R_MIPS_64, R_MIPS_GOT_DISP,
342 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
343 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
344 R_MIPS_SUB, R_MIPS_INSERT_A,
345 R_MIPS_INSERT_B, R_MIPS_DELETE,
346 R_MIPS_HIGHER, R_MIPS_HIGHEST,
347 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
348 R_MIPS_max
349 };
350
351 static reloc_howto_type elf_mips_howto_table[] =
352 {
353 /* No relocation. */
354 HOWTO (R_MIPS_NONE, /* type */
355 0, /* rightshift */
356 0, /* size (0 = byte, 1 = short, 2 = long) */
357 0, /* bitsize */
358 false, /* pc_relative */
359 0, /* bitpos */
360 complain_overflow_dont, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_MIPS_NONE", /* name */
363 false, /* partial_inplace */
364 0, /* src_mask */
365 0, /* dst_mask */
366 false), /* pcrel_offset */
367
368 /* 16 bit relocation. */
369 HOWTO (R_MIPS_16, /* type */
370 0, /* rightshift */
371 1, /* size (0 = byte, 1 = short, 2 = long) */
372 16, /* bitsize */
373 false, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_bitfield, /* complain_on_overflow */
376 bfd_elf_generic_reloc, /* special_function */
377 "R_MIPS_16", /* name */
378 true, /* partial_inplace */
379 0xffff, /* src_mask */
380 0xffff, /* dst_mask */
381 false), /* pcrel_offset */
382
383 /* 32 bit relocation. */
384 HOWTO (R_MIPS_32, /* type */
385 0, /* rightshift */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
387 32, /* bitsize */
388 false, /* pc_relative */
389 0, /* bitpos */
390 complain_overflow_bitfield, /* complain_on_overflow */
391 bfd_elf_generic_reloc, /* special_function */
392 "R_MIPS_32", /* name */
393 true, /* partial_inplace */
394 0xffffffff, /* src_mask */
395 0xffffffff, /* dst_mask */
396 false), /* pcrel_offset */
397
398 /* 32 bit symbol relative relocation. */
399 HOWTO (R_MIPS_REL32, /* type */
400 0, /* rightshift */
401 2, /* size (0 = byte, 1 = short, 2 = long) */
402 32, /* bitsize */
403 false, /* pc_relative */
404 0, /* bitpos */
405 complain_overflow_bitfield, /* complain_on_overflow */
406 bfd_elf_generic_reloc, /* special_function */
407 "R_MIPS_REL32", /* name */
408 true, /* partial_inplace */
409 0xffffffff, /* src_mask */
410 0xffffffff, /* dst_mask */
411 false), /* pcrel_offset */
412
413 /* 26 bit branch address. */
414 HOWTO (R_MIPS_26, /* type */
415 2, /* rightshift */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
417 26, /* bitsize */
418 false, /* pc_relative */
419 0, /* bitpos */
420 complain_overflow_dont, /* complain_on_overflow */
421 /* This needs complex overflow
422 detection, because the upper four
423 bits must match the PC. */
424 bfd_elf_generic_reloc, /* special_function */
425 "R_MIPS_26", /* name */
426 true, /* partial_inplace */
427 0x3ffffff, /* src_mask */
428 0x3ffffff, /* dst_mask */
429 false), /* pcrel_offset */
430
431 /* High 16 bits of symbol value. */
432 HOWTO (R_MIPS_HI16, /* type */
433 0, /* rightshift */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
435 16, /* bitsize */
436 false, /* pc_relative */
437 0, /* bitpos */
438 complain_overflow_dont, /* complain_on_overflow */
439 mips_elf_hi16_reloc, /* special_function */
440 "R_MIPS_HI16", /* name */
441 true, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 false), /* pcrel_offset */
445
446 /* Low 16 bits of symbol value. */
447 HOWTO (R_MIPS_LO16, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 16, /* bitsize */
451 false, /* pc_relative */
452 0, /* bitpos */
453 complain_overflow_dont, /* complain_on_overflow */
454 mips_elf_lo16_reloc, /* special_function */
455 "R_MIPS_LO16", /* name */
456 true, /* partial_inplace */
457 0xffff, /* src_mask */
458 0xffff, /* dst_mask */
459 false), /* pcrel_offset */
460
461 /* GP relative reference. */
462 HOWTO (R_MIPS_GPREL16, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
466 false, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_signed, /* complain_on_overflow */
469 mips_elf_gprel16_reloc, /* special_function */
470 "R_MIPS_GPREL16", /* name */
471 true, /* partial_inplace */
472 0xffff, /* src_mask */
473 0xffff, /* dst_mask */
474 false), /* pcrel_offset */
475
476 /* Reference to literal section. */
477 HOWTO (R_MIPS_LITERAL, /* type */
478 0, /* rightshift */
479 2, /* size (0 = byte, 1 = short, 2 = long) */
480 16, /* bitsize */
481 false, /* pc_relative */
482 0, /* bitpos */
483 complain_overflow_signed, /* complain_on_overflow */
484 mips_elf_gprel16_reloc, /* special_function */
485 "R_MIPS_LITERAL", /* name */
486 true, /* partial_inplace */
487 0xffff, /* src_mask */
488 0xffff, /* dst_mask */
489 false), /* pcrel_offset */
490
491 /* Reference to global offset table. */
492 HOWTO (R_MIPS_GOT16, /* type */
493 0, /* rightshift */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
496 false, /* pc_relative */
497 0, /* bitpos */
498 complain_overflow_signed, /* complain_on_overflow */
499 mips_elf_got16_reloc, /* special_function */
500 "R_MIPS_GOT16", /* name */
501 false, /* partial_inplace */
502 0, /* src_mask */
503 0xffff, /* dst_mask */
504 false), /* pcrel_offset */
505
506 /* 16 bit PC relative reference. */
507 HOWTO (R_MIPS_PC16, /* type */
508 0, /* rightshift */
509 2, /* size (0 = byte, 1 = short, 2 = long) */
510 16, /* bitsize */
511 true, /* pc_relative */
512 0, /* bitpos */
513 complain_overflow_signed, /* complain_on_overflow */
514 bfd_elf_generic_reloc, /* special_function */
515 "R_MIPS_PC16", /* name */
516 true, /* partial_inplace */
517 0xffff, /* src_mask */
518 0xffff, /* dst_mask */
519 false), /* pcrel_offset */
520
521 /* 16 bit call through global offset table. */
522 /* FIXME: This is not handled correctly. */
523 HOWTO (R_MIPS_CALL16, /* type */
524 0, /* rightshift */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
526 16, /* bitsize */
527 false, /* pc_relative */
528 0, /* bitpos */
529 complain_overflow_signed, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_MIPS_CALL16", /* name */
532 false, /* partial_inplace */
533 0, /* src_mask */
534 0xffff, /* dst_mask */
535 false), /* pcrel_offset */
536
537 /* 32 bit GP relative reference. */
538 HOWTO (R_MIPS_GPREL32, /* type */
539 0, /* rightshift */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
541 32, /* bitsize */
542 false, /* pc_relative */
543 0, /* bitpos */
544 complain_overflow_bitfield, /* complain_on_overflow */
545 mips_elf_gprel32_reloc, /* special_function */
546 "R_MIPS_GPREL32", /* name */
547 true, /* partial_inplace */
548 0xffffffff, /* src_mask */
549 0xffffffff, /* dst_mask */
550 false), /* pcrel_offset */
551
552 /* The remaining relocs are defined on Irix 5, although they are
553 not defined by the ABI. */
554 { 13 },
555 { 14 },
556 { 15 },
557
558 /* A 5 bit shift field. */
559 HOWTO (R_MIPS_SHIFT5, /* type */
560 0, /* rightshift */
561 2, /* size (0 = byte, 1 = short, 2 = long) */
562 5, /* bitsize */
563 false, /* pc_relative */
564 6, /* bitpos */
565 complain_overflow_bitfield, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_MIPS_SHIFT5", /* name */
568 true, /* partial_inplace */
569 0x000007c0, /* src_mask */
570 0x000007c0, /* dst_mask */
571 false), /* pcrel_offset */
572
573 /* A 6 bit shift field. */
574 /* FIXME: This is not handled correctly; a special function is
575 needed to put the most significant bit in the right place. */
576 HOWTO (R_MIPS_SHIFT6, /* type */
577 0, /* rightshift */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
579 6, /* bitsize */
580 false, /* pc_relative */
581 6, /* bitpos */
582 complain_overflow_bitfield, /* complain_on_overflow */
583 bfd_elf_generic_reloc, /* special_function */
584 "R_MIPS_SHIFT6", /* name */
585 true, /* partial_inplace */
586 0x000007c4, /* src_mask */
587 0x000007c4, /* dst_mask */
588 false), /* pcrel_offset */
589
590 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
591 { R_MIPS_64 },
592
593 /* Displacement in the global offset table. */
594 /* FIXME: Not handled correctly. */
595 HOWTO (R_MIPS_GOT_DISP, /* type */
596 0, /* rightshift */
597 2, /* size (0 = byte, 1 = short, 2 = long) */
598 16, /* bitsize */
599 false, /* pc_relative */
600 0, /* bitpos */
601 complain_overflow_bitfield, /* complain_on_overflow */
602 bfd_elf_generic_reloc, /* special_function */
603 "R_MIPS_GOT_DISP", /* name */
604 true, /* partial_inplace */
605 0x0000ffff, /* src_mask */
606 0x0000ffff, /* dst_mask */
607 false), /* pcrel_offset */
608
609 /* Displacement to page pointer in the global offset table. */
610 /* FIXME: Not handled correctly. */
611 HOWTO (R_MIPS_GOT_PAGE, /* type */
612 0, /* rightshift */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
614 16, /* bitsize */
615 false, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_bitfield, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_MIPS_GOT_PAGE", /* name */
620 true, /* partial_inplace */
621 0x0000ffff, /* src_mask */
622 0x0000ffff, /* dst_mask */
623 false), /* pcrel_offset */
624
625 /* Offset from page pointer in the global offset table. */
626 /* FIXME: Not handled correctly. */
627 HOWTO (R_MIPS_GOT_OFST, /* type */
628 0, /* rightshift */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
630 16, /* bitsize */
631 false, /* pc_relative */
632 0, /* bitpos */
633 complain_overflow_bitfield, /* complain_on_overflow */
634 bfd_elf_generic_reloc, /* special_function */
635 "R_MIPS_GOT_OFST", /* name */
636 true, /* partial_inplace */
637 0x0000ffff, /* src_mask */
638 0x0000ffff, /* dst_mask */
639 false), /* pcrel_offset */
640
641 /* High 16 bits of displacement in global offset table. */
642 /* FIXME: Not handled correctly. */
643 HOWTO (R_MIPS_GOT_HI16, /* type */
644 0, /* rightshift */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
646 16, /* bitsize */
647 false, /* pc_relative */
648 0, /* bitpos */
649 complain_overflow_dont, /* complain_on_overflow */
650 bfd_elf_generic_reloc, /* special_function */
651 "R_MIPS_GOT_HI16", /* name */
652 true, /* partial_inplace */
653 0x0000ffff, /* src_mask */
654 0x0000ffff, /* dst_mask */
655 false), /* pcrel_offset */
656
657 /* Low 16 bits of displacement in global offset table. */
658 /* FIXME: Not handled correctly. */
659 HOWTO (R_MIPS_GOT_LO16, /* type */
660 0, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 16, /* bitsize */
663 false, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_dont, /* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_MIPS_GOT_LO16", /* name */
668 true, /* partial_inplace */
669 0x0000ffff, /* src_mask */
670 0x0000ffff, /* dst_mask */
671 false), /* pcrel_offset */
672
673 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
674 { R_MIPS_SUB },
675
676 /* Used to cause the linker to insert and delete instructions? */
677 { R_MIPS_INSERT_A },
678 { R_MIPS_INSERT_B },
679 { R_MIPS_DELETE },
680
681 /* Get the higher values of a 64 bit addend. Presumably not used in
682 32 bit ELF. */
683 { R_MIPS_HIGHER },
684 { R_MIPS_HIGHEST },
685
686 /* High 16 bits of displacement in global offset table. */
687 /* FIXME: Not handled correctly. */
688 HOWTO (R_MIPS_CALL_HI16, /* type */
689 0, /* rightshift */
690 2, /* size (0 = byte, 1 = short, 2 = long) */
691 16, /* bitsize */
692 false, /* pc_relative */
693 0, /* bitpos */
694 complain_overflow_dont, /* complain_on_overflow */
695 bfd_elf_generic_reloc, /* special_function */
696 "R_MIPS_CALL_HI16", /* name */
697 true, /* partial_inplace */
698 0x0000ffff, /* src_mask */
699 0x0000ffff, /* dst_mask */
700 false), /* pcrel_offset */
701
702 /* Low 16 bits of displacement in global offset table. */
703 /* FIXME: Not handled correctly. */
704 HOWTO (R_MIPS_CALL_LO16, /* type */
705 0, /* rightshift */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
707 16, /* bitsize */
708 false, /* pc_relative */
709 0, /* bitpos */
710 complain_overflow_dont, /* complain_on_overflow */
711 bfd_elf_generic_reloc, /* special_function */
712 "R_MIPS_CALL_LO16", /* name */
713 true, /* partial_inplace */
714 0x0000ffff, /* src_mask */
715 0x0000ffff, /* dst_mask */
716 false) /* pcrel_offset */
717 };
718
719 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
720 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
721 the HI16. Here we just save the information we need; we do the
722 actual relocation when we see the LO16. MIPS ELF requires that the
723 LO16 immediately follow the HI16, so this ought to work. */
724
725 static bfd_byte *mips_hi16_addr;
726 static bfd_vma mips_hi16_addend;
727
728 static bfd_reloc_status_type
729 mips_elf_hi16_reloc (abfd,
730 reloc_entry,
731 symbol,
732 data,
733 input_section,
734 output_bfd,
735 error_message)
736 bfd *abfd;
737 arelent *reloc_entry;
738 asymbol *symbol;
739 PTR data;
740 asection *input_section;
741 bfd *output_bfd;
742 char **error_message;
743 {
744 bfd_reloc_status_type ret;
745 bfd_vma relocation;
746
747 /* If we're relocating, and this an external symbol, we don't want
748 to change anything. */
749 if (output_bfd != (bfd *) NULL
750 && (symbol->flags & BSF_SECTION_SYM) == 0
751 && reloc_entry->addend == 0)
752 {
753 reloc_entry->address += input_section->output_offset;
754 return bfd_reloc_ok;
755 }
756
757 ret = bfd_reloc_ok;
758
759 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
760 {
761 boolean relocateable;
762
763 if (ret == bfd_reloc_undefined)
764 abort ();
765
766 if (output_bfd != NULL)
767 relocateable = true;
768 else
769 {
770 relocateable = false;
771 output_bfd = symbol->section->output_section->owner;
772 }
773
774 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
775 error_message);
776 if (ret != bfd_reloc_ok)
777 return ret;
778
779 relocation = elf_gp (output_bfd) - reloc_entry->address;
780 }
781 else
782 {
783 if (bfd_is_und_section (symbol->section)
784 && output_bfd == (bfd *) NULL)
785 ret = bfd_reloc_undefined;
786
787 if (bfd_is_com_section (symbol->section))
788 relocation = 0;
789 else
790 relocation = symbol->value;
791 }
792
793 relocation += symbol->section->output_section->vma;
794 relocation += symbol->section->output_offset;
795 relocation += reloc_entry->addend;
796
797 if (reloc_entry->address > input_section->_cooked_size)
798 return bfd_reloc_outofrange;
799
800 /* Save the information, and let LO16 do the actual relocation. */
801 mips_hi16_addr = (bfd_byte *) data + reloc_entry->address;
802 mips_hi16_addend = relocation;
803
804 if (output_bfd != (bfd *) NULL)
805 reloc_entry->address += input_section->output_offset;
806
807 return ret;
808 }
809
810 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
811 inplace relocation; this function exists in order to do the
812 R_MIPS_HI16 relocation described above. */
813
814 static bfd_reloc_status_type
815 mips_elf_lo16_reloc (abfd,
816 reloc_entry,
817 symbol,
818 data,
819 input_section,
820 output_bfd,
821 error_message)
822 bfd *abfd;
823 arelent *reloc_entry;
824 asymbol *symbol;
825 PTR data;
826 asection *input_section;
827 bfd *output_bfd;
828 char **error_message;
829 {
830 arelent gp_disp_relent;
831
832 if (mips_hi16_addr != (bfd_byte *) NULL)
833 {
834 unsigned long insn;
835 unsigned long val;
836 unsigned long vallo;
837
838 /* Do the HI16 relocation. Note that we actually don't need to
839 know anything about the LO16 itself, except where to find the
840 low 16 bits of the addend needed by the LO16. */
841 insn = bfd_get_32 (abfd, mips_hi16_addr);
842 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
843 & 0xffff);
844 val = ((insn & 0xffff) << 16) + vallo;
845 val += mips_hi16_addend;
846
847 /* The low order 16 bits are always treated as a signed value.
848 Therefore, a negative value in the low order bits requires an
849 adjustment in the high order bits. We need to make this
850 adjustment in two ways: once for the bits we took from the
851 data, and once for the bits we are putting back in to the
852 data. */
853 if ((vallo & 0x8000) != 0)
854 val -= 0x10000;
855 if ((val & 0x8000) != 0)
856 val += 0x10000;
857
858 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
859 bfd_put_32 (abfd, insn, mips_hi16_addr);
860
861 mips_hi16_addr = (bfd_byte *) NULL;
862
863 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
864 {
865 gp_disp_relent = *reloc_entry;
866 reloc_entry = &gp_disp_relent;
867 reloc_entry->addend = mips_hi16_addend;
868 }
869 }
870 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
871 {
872 bfd_reloc_status_type ret;
873 bfd_vma relocation;
874
875 /* FIXME: Does this case ever occur? */
876
877 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message);
878 if (ret != bfd_reloc_ok)
879 return ret;
880
881 relocation = elf_gp (output_bfd) - reloc_entry->address;
882 relocation += symbol->section->output_section->vma;
883 relocation += symbol->section->output_offset;
884 relocation += reloc_entry->addend;
885
886 if (reloc_entry->address > input_section->_cooked_size)
887 return bfd_reloc_outofrange;
888
889 gp_disp_relent = *reloc_entry;
890 reloc_entry = &gp_disp_relent;
891 reloc_entry->addend = relocation - 4;
892 }
893
894 /* Now do the LO16 reloc in the usual way. */
895 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
896 input_section, output_bfd, error_message);
897 }
898
899 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
900 table used for PIC code. If the symbol is an external symbol, the
901 instruction is modified to contain the offset of the appropriate
902 entry in the global offset table. If the symbol is a section
903 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
904 addends are combined to form the real addend against the section
905 symbol; the GOT16 is modified to contain the offset of an entry in
906 the global offset table, and the LO16 is modified to offset it
907 appropriately. Thus an offset larger than 16 bits requires a
908 modified value in the global offset table.
909
910 This implementation suffices for the assembler, but the linker does
911 not yet know how to create global offset tables. */
912
913 static bfd_reloc_status_type
914 mips_elf_got16_reloc (abfd,
915 reloc_entry,
916 symbol,
917 data,
918 input_section,
919 output_bfd,
920 error_message)
921 bfd *abfd;
922 arelent *reloc_entry;
923 asymbol *symbol;
924 PTR data;
925 asection *input_section;
926 bfd *output_bfd;
927 char **error_message;
928 {
929 /* If we're relocating, and this an external symbol, we don't want
930 to change anything. */
931 if (output_bfd != (bfd *) NULL
932 && (symbol->flags & BSF_SECTION_SYM) == 0
933 && reloc_entry->addend == 0)
934 {
935 reloc_entry->address += input_section->output_offset;
936 return bfd_reloc_ok;
937 }
938
939 /* If we're relocating, and this is a local symbol, we can handle it
940 just like HI16. */
941 if (output_bfd != (bfd *) NULL
942 && (symbol->flags & BSF_SECTION_SYM) != 0)
943 return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
944 input_section, output_bfd, error_message);
945
946 abort ();
947 }
948
949 /* We have to figure out the gp value, so that we can adjust the
950 symbol value correctly. We look up the symbol _gp in the output
951 BFD. If we can't find it, we're stuck. We cache it in the ELF
952 target data. We don't need to adjust the symbol value for an
953 external symbol if we are producing relocateable output. */
954
955 static bfd_reloc_status_type
956 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message)
957 bfd *output_bfd;
958 asymbol *symbol;
959 boolean relocateable;
960 char **error_message;
961 {
962 if (bfd_is_und_section (symbol->section)
963 && ! relocateable)
964 return bfd_reloc_undefined;
965
966 /* This doesn't work if the BFD is not ELF. */
967 if (output_bfd->xvec->flavour != bfd_target_elf_flavour)
968 abort ();
969
970 if (elf_gp (output_bfd) == 0
971 && (! relocateable
972 || (symbol->flags & BSF_SECTION_SYM) != 0))
973 {
974 if (relocateable)
975 {
976 /* Make up a value. */
977 elf_gp (output_bfd) =
978 symbol->section->output_section->vma + 0x4000;
979 }
980 else
981 {
982 unsigned int count;
983 asymbol **sym;
984 unsigned int i;
985
986 count = bfd_get_symcount (output_bfd);
987 sym = bfd_get_outsymbols (output_bfd);
988
989 if (sym == (asymbol **) NULL)
990 i = count;
991 else
992 {
993 for (i = 0; i < count; i++, sym++)
994 {
995 register CONST char *name;
996
997 name = bfd_asymbol_name (*sym);
998 if (*name == '_' && strcmp (name, "_gp") == 0)
999 {
1000 elf_gp (output_bfd) = bfd_asymbol_value (*sym);
1001 break;
1002 }
1003 }
1004 }
1005
1006 if (i >= count)
1007 {
1008 /* Only get the error once. */
1009 elf_gp (output_bfd) = 4;
1010 *error_message =
1011 (char *) "GP relative relocation when _gp not defined";
1012 return bfd_reloc_dangerous;
1013 }
1014 }
1015 }
1016
1017 return bfd_reloc_ok;
1018 }
1019
1020 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1021 become the offset from the gp register. This function also handles
1022 R_MIPS_LITERAL relocations, although those can be handled more
1023 cleverly because the entries in the .lit8 and .lit4 sections can be
1024 merged. */
1025
1026 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1027 arelent *, asection *,
1028 boolean, PTR, bfd_vma));
1029
1030 static bfd_reloc_status_type
1031 mips_elf_gprel16_reloc (abfd,
1032 reloc_entry,
1033 symbol,
1034 data,
1035 input_section,
1036 output_bfd,
1037 error_message)
1038 bfd *abfd;
1039 arelent *reloc_entry;
1040 asymbol *symbol;
1041 PTR data;
1042 asection *input_section;
1043 bfd *output_bfd;
1044 char **error_message;
1045 {
1046 boolean relocateable;
1047 bfd_reloc_status_type ret;
1048
1049 /* If we're relocating, and this is an external symbol with no
1050 addend, we don't want to change anything. We will only have an
1051 addend if this is a newly created reloc, not read from an ELF
1052 file. */
1053 if (output_bfd != (bfd *) NULL
1054 && (symbol->flags & BSF_SECTION_SYM) == 0
1055 && reloc_entry->addend == 0)
1056 {
1057 reloc_entry->address += input_section->output_offset;
1058 return bfd_reloc_ok;
1059 }
1060
1061 if (output_bfd != (bfd *) NULL)
1062 relocateable = true;
1063 else
1064 {
1065 relocateable = false;
1066 output_bfd = symbol->section->output_section->owner;
1067 }
1068
1069 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message);
1070 if (ret != bfd_reloc_ok)
1071 return ret;
1072
1073 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1074 relocateable, data, elf_gp (output_bfd));
1075 }
1076
1077 static bfd_reloc_status_type
1078 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1079 gp)
1080 bfd *abfd;
1081 asymbol *symbol;
1082 arelent *reloc_entry;
1083 asection *input_section;
1084 boolean relocateable;
1085 PTR data;
1086 bfd_vma gp;
1087 {
1088 bfd_vma relocation;
1089 unsigned long insn;
1090 unsigned long val;
1091
1092 if (bfd_is_com_section (symbol->section))
1093 relocation = 0;
1094 else
1095 relocation = symbol->value;
1096
1097 relocation += symbol->section->output_section->vma;
1098 relocation += symbol->section->output_offset;
1099
1100 if (reloc_entry->address > input_section->_cooked_size)
1101 return bfd_reloc_outofrange;
1102
1103 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1104
1105 /* Set val to the offset into the section or symbol. */
1106 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1107 if (val & 0x8000)
1108 val -= 0x10000;
1109
1110 /* Adjust val for the final section location and GP value. If we
1111 are producing relocateable output, we don't want to do this for
1112 an external symbol. */
1113 if (! relocateable
1114 || (symbol->flags & BSF_SECTION_SYM) != 0)
1115 val += relocation - gp;
1116
1117 insn = (insn &~ 0xffff) | (val & 0xffff);
1118 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1119
1120 if (relocateable)
1121 reloc_entry->address += input_section->output_offset;
1122
1123 /* Make sure it fit in 16 bits. */
1124 if (val >= 0x8000 && val < 0xffff8000)
1125 return bfd_reloc_overflow;
1126
1127 return bfd_reloc_ok;
1128 }
1129
1130 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1131 from the gp register? XXX */
1132
1133 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1134 arelent *, asection *,
1135 boolean, PTR, bfd_vma));
1136
1137 static bfd_reloc_status_type
1138 mips_elf_gprel32_reloc (abfd,
1139 reloc_entry,
1140 symbol,
1141 data,
1142 input_section,
1143 output_bfd,
1144 error_message)
1145 bfd *abfd;
1146 arelent *reloc_entry;
1147 asymbol *symbol;
1148 PTR data;
1149 asection *input_section;
1150 bfd *output_bfd;
1151 char **error_message;
1152 {
1153 boolean relocateable;
1154 bfd_reloc_status_type ret;
1155
1156 /* If we're relocating, and this is an external symbol with no
1157 addend, we don't want to change anything. We will only have an
1158 addend if this is a newly created reloc, not read from an ELF
1159 file. */
1160 if (output_bfd != (bfd *) NULL
1161 && (symbol->flags & BSF_SECTION_SYM) == 0
1162 && reloc_entry->addend == 0)
1163 {
1164 *error_message = (char *)
1165 "32bits gp relative relocation occurs for an external symbol";
1166 return bfd_reloc_outofrange;
1167 }
1168
1169 if (output_bfd != (bfd *) NULL)
1170 relocateable = true;
1171 else
1172 {
1173 relocateable = false;
1174 output_bfd = symbol->section->output_section->owner;
1175
1176 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1177 error_message);
1178 if (ret != bfd_reloc_ok)
1179 return ret;
1180 }
1181
1182 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1183 relocateable, data, elf_gp (output_bfd));
1184 }
1185
1186 static bfd_reloc_status_type
1187 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1188 gp)
1189 bfd *abfd;
1190 asymbol *symbol;
1191 arelent *reloc_entry;
1192 asection *input_section;
1193 boolean relocateable;
1194 PTR data;
1195 bfd_vma gp;
1196 {
1197 bfd_vma relocation;
1198 unsigned long val;
1199
1200 if (bfd_is_com_section (symbol->section))
1201 relocation = 0;
1202 else
1203 relocation = symbol->value;
1204
1205 relocation += symbol->section->output_section->vma;
1206 relocation += symbol->section->output_offset;
1207
1208 if (reloc_entry->address > input_section->_cooked_size)
1209 return bfd_reloc_outofrange;
1210
1211 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1212
1213 /* Set val to the offset into the section or symbol. */
1214 val += reloc_entry->addend;
1215
1216 /* Adjust val for the final section location and GP value. If we
1217 are producing relocateable output, we don't want to do this for
1218 an external symbol. */
1219 if (! relocateable
1220 || (symbol->flags & BSF_SECTION_SYM) != 0)
1221 val += relocation - gp;
1222
1223 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1224
1225 if (relocateable)
1226 reloc_entry->address += input_section->output_offset;
1227
1228 return bfd_reloc_ok;
1229 }
1230
1231 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1232
1233 struct elf_reloc_map {
1234 bfd_reloc_code_real_type bfd_reloc_val;
1235 enum reloc_type elf_reloc_val;
1236 };
1237
1238 static CONST struct elf_reloc_map mips_reloc_map[] =
1239 {
1240 { BFD_RELOC_NONE, R_MIPS_NONE, },
1241 { BFD_RELOC_16, R_MIPS_16 },
1242 { BFD_RELOC_32, R_MIPS_32 },
1243 { BFD_RELOC_CTOR, R_MIPS_32 },
1244 { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
1245 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1246 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1247 { BFD_RELOC_LO16, R_MIPS_LO16 },
1248 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1249 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1250 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1251 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1252 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1253 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }
1254 };
1255
1256 /* Given a BFD reloc type, return a howto structure. */
1257
1258 static reloc_howto_type *
1259 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1260 bfd *abfd;
1261 bfd_reloc_code_real_type code;
1262 {
1263 unsigned int i;
1264
1265 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1266 {
1267 if (mips_reloc_map[i].bfd_reloc_val == code)
1268 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1269 }
1270 return NULL;
1271 }
1272
1273 /* Given a MIPS reloc type, fill in an arelent structure. */
1274
1275 static void
1276 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1277 bfd *abfd;
1278 arelent *cache_ptr;
1279 Elf32_Internal_Rel *dst;
1280 {
1281 unsigned int r_type;
1282
1283 r_type = ELF32_R_TYPE (dst->r_info);
1284 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1285 cache_ptr->howto = &elf_mips_howto_table[r_type];
1286
1287 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1288 value for the object file. We get the addend now, rather than
1289 when we do the relocation, because the symbol manipulations done
1290 by the linker may cause us to lose track of the input BFD. */
1291 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1292 && (r_type == (unsigned int) R_MIPS_GPREL16
1293 || r_type == (unsigned int) R_MIPS_LITERAL))
1294 cache_ptr->addend = elf_gp (abfd);
1295 }
1296 \f
1297 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1298 routines swap this structure in and out. They are used outside of
1299 BFD, so they are globally visible. */
1300
1301 void
1302 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1303 bfd *abfd;
1304 const Elf32_External_RegInfo *ex;
1305 Elf32_RegInfo *in;
1306 {
1307 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1308 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1309 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1310 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1311 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1312 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1313 }
1314
1315 void
1316 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1317 bfd *abfd;
1318 const Elf32_RegInfo *in;
1319 Elf32_External_RegInfo *ex;
1320 {
1321 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1322 (bfd_byte *) ex->ri_gprmask);
1323 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1324 (bfd_byte *) ex->ri_cprmask[0]);
1325 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1326 (bfd_byte *) ex->ri_cprmask[1]);
1327 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1328 (bfd_byte *) ex->ri_cprmask[2]);
1329 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1330 (bfd_byte *) ex->ri_cprmask[3]);
1331 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1332 (bfd_byte *) ex->ri_gp_value);
1333 }
1334
1335 /* Swap an entry in a .gptab section. Note that these routines rely
1336 on the equivalence of the two elements of the union. */
1337
1338 static void
1339 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1340 bfd *abfd;
1341 const Elf32_External_gptab *ex;
1342 Elf32_gptab *in;
1343 {
1344 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1345 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1346 }
1347
1348 static void
1349 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1350 bfd *abfd;
1351 const Elf32_gptab *in;
1352 Elf32_External_gptab *ex;
1353 {
1354 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1355 ex->gt_entry.gt_g_value);
1356 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1357 ex->gt_entry.gt_bytes);
1358 }
1359
1360 static void
1361 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1362 bfd *abfd;
1363 const Elf32_compact_rel *in;
1364 Elf32_External_compact_rel *ex;
1365 {
1366 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1367 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1368 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1369 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1370 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1371 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1372 }
1373
1374 static void
1375 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1376 bfd *abfd;
1377 const Elf32_crinfo *in;
1378 Elf32_External_crinfo *ex;
1379 {
1380 unsigned long l;
1381
1382 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1383 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1384 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1385 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1386 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1387 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1388 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1389 }
1390 \f
1391 /* Determine whether a symbol is global for the purposes of splitting
1392 the symbol table into global symbols and local symbols. At least
1393 on Irix 5, this split must be between section symbols and all other
1394 symbols. On most ELF targets the split is between static symbols
1395 and externally visible symbols. */
1396
1397 /*ARGSUSED*/
1398 static boolean
1399 mips_elf_sym_is_global (abfd, sym)
1400 bfd *abfd;
1401 asymbol *sym;
1402 {
1403 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1404 }
1405 \f
1406 /* Set the right machine number for a MIPS ELF file. */
1407
1408 static boolean
1409 mips_elf_object_p (abfd)
1410 bfd *abfd;
1411 {
1412 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
1413 {
1414 default:
1415 case E_MIPS_ARCH_1:
1416 /* Just use the default, which was set in elfcode.h. */
1417 break;
1418
1419 case E_MIPS_ARCH_2:
1420 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
1421 break;
1422
1423 case E_MIPS_ARCH_3:
1424 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
1425 break;
1426 }
1427
1428 /* Irix 5 is broken. Object file symbol tables are not always
1429 sorted correctly such that local symbols precede global symbols,
1430 and the sh_info field in the symbol table is not always right. */
1431 elf_bad_symtab (abfd) = true;
1432
1433 return true;
1434 }
1435
1436 /* The final processing done just before writing out a MIPS ELF object
1437 file. This gets the MIPS architecture right based on the machine
1438 number. */
1439
1440 /*ARGSUSED*/
1441 static void
1442 mips_elf_final_write_processing (abfd, linker)
1443 bfd *abfd;
1444 boolean linker;
1445 {
1446 unsigned long val;
1447 unsigned int i;
1448 Elf_Internal_Shdr **hdrpp;
1449
1450 switch (bfd_get_mach (abfd))
1451 {
1452 case 3000:
1453 val = E_MIPS_ARCH_1;
1454 break;
1455
1456 case 6000:
1457 val = E_MIPS_ARCH_2;
1458 break;
1459
1460 case 4000:
1461 val = E_MIPS_ARCH_3;
1462 break;
1463
1464 default:
1465 val = 0;
1466 break;
1467 }
1468
1469 elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1470 elf_elfheader (abfd)->e_flags |= val;
1471
1472 /* Set the sh_info field for .gptab sections. */
1473 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1474 i < elf_elfheader (abfd)->e_shnum;
1475 i++, hdrpp++)
1476 {
1477 if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
1478 {
1479 const char *name;
1480 asection *sec;
1481
1482 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1483 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1484 BFD_ASSERT (name != NULL
1485 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1486 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1487 BFD_ASSERT (sec != NULL);
1488 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1489 }
1490 }
1491 }
1492 \f
1493 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1494
1495 static boolean
1496 mips_elf_set_private_flags (abfd, flags)
1497 bfd *abfd;
1498 flagword flags;
1499 {
1500 BFD_ASSERT (!elf_flags_init (abfd)
1501 || elf_elfheader (abfd)->e_flags == flags);
1502
1503 elf_elfheader (abfd)->e_flags = flags;
1504 elf_flags_init (abfd) = true;
1505 return true;
1506 }
1507
1508 /* Copy backend specific data from one object module to another */
1509
1510 static boolean
1511 mips_elf_copy_private_bfd_data (ibfd, obfd)
1512 bfd *ibfd;
1513 bfd *obfd;
1514 {
1515 /* This function is selected based on the input vector. We only
1516 want to copy information over if the output BFD also uses Elf
1517 format. */
1518 if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1519 return true;
1520
1521 BFD_ASSERT (!elf_flags_init (obfd)
1522 || (elf_elfheader (obfd)->e_flags
1523 == elf_elfheader (ibfd)->e_flags));
1524
1525 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1526 elf_flags_init (obfd) = true;
1527 return true;
1528 }
1529
1530 /* Merge backend specific data from an object file to the output
1531 object file when linking. */
1532
1533 static boolean
1534 mips_elf_merge_private_bfd_data (ibfd, obfd)
1535 bfd *ibfd;
1536 bfd *obfd;
1537 {
1538 flagword old_flags;
1539 flagword new_flags;
1540
1541 /* Check if we have the same endianess */
1542 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1543 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1544 {
1545 (*_bfd_error_handler)
1546 ("%s: compiled for a %s endian system and target is %s endian",
1547 bfd_get_filename (ibfd),
1548 bfd_big_endian (ibfd) ? "big" : "little",
1549 bfd_big_endian (obfd) ? "big" : "little");
1550
1551 bfd_set_error (bfd_error_wrong_format);
1552 return false;
1553 }
1554
1555 /* This function is selected based on the input vector. We only
1556 want to copy information over if the output BFD also uses Elf
1557 format. */
1558 if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1559 return true;
1560
1561 new_flags = elf_elfheader (ibfd)->e_flags;
1562 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
1563 old_flags = elf_elfheader (obfd)->e_flags;
1564
1565 if (!elf_flags_init (obfd)) /* First call, no flags set */
1566 {
1567 elf_flags_init (obfd) = true;
1568 elf_elfheader (obfd)->e_flags = new_flags;
1569 }
1570 else if (((new_flags ^ old_flags) & ~EF_MIPS_NOREORDER)
1571 == 0) /* Compatible flags are ok */
1572 ;
1573 else /* Incompatible flags */
1574 {
1575 /* Warn about -fPIC mismatch */
1576 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
1577 {
1578 new_flags &= ~EF_MIPS_PIC;
1579 (*_bfd_error_handler)
1580 ("%s: needs all files compiled with -fPIC",
1581 bfd_get_filename (ibfd));
1582 }
1583
1584 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
1585 {
1586 new_flags &= ~EF_MIPS_CPIC;
1587 (*_bfd_error_handler)
1588 ("%s: needs all files compiled with -mabicalls",
1589 bfd_get_filename (ibfd));
1590 }
1591
1592 /* Warn about any other mismatches */
1593 if (new_flags != old_flags)
1594 (*_bfd_error_handler)
1595 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1596 bfd_get_filename (ibfd), (unsigned long) new_flags,
1597 (unsigned long) old_flags);
1598
1599 bfd_set_error (bfd_error_bad_value);
1600 return false;
1601 }
1602
1603 return true;
1604 }
1605 \f
1606 /* Handle a MIPS specific section when reading an object file. This
1607 is called when elfcode.h finds a section with an unknown type.
1608 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1609 how to. */
1610
1611 static boolean
1612 mips_elf_section_from_shdr (abfd, hdr, name)
1613 bfd *abfd;
1614 Elf32_Internal_Shdr *hdr;
1615 char *name;
1616 {
1617 asection *newsect;
1618
1619 /* There ought to be a place to keep ELF backend specific flags, but
1620 at the moment there isn't one. We just keep track of the
1621 sections by their name, instead. Fortunately, the ABI gives
1622 suggested names for all the MIPS specific sections, so we will
1623 probably get away with this. */
1624 switch (hdr->sh_type)
1625 {
1626 case SHT_MIPS_LIBLIST:
1627 if (strcmp (name, ".liblist") != 0)
1628 return false;
1629 break;
1630 case SHT_MIPS_MSYM:
1631 if (strcmp (name, ".msym") != 0)
1632 return false;
1633 break;
1634 case SHT_MIPS_CONFLICT:
1635 if (strcmp (name, ".conflict") != 0)
1636 return false;
1637 break;
1638 case SHT_MIPS_GPTAB:
1639 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
1640 return false;
1641 break;
1642 case SHT_MIPS_UCODE:
1643 if (strcmp (name, ".ucode") != 0)
1644 return false;
1645 break;
1646 case SHT_MIPS_DEBUG:
1647 if (strcmp (name, ".mdebug") != 0)
1648 return false;
1649 break;
1650 case SHT_MIPS_REGINFO:
1651 if (strcmp (name, ".reginfo") != 0
1652 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
1653 return false;
1654 break;
1655 case SHT_MIPS_OPTIONS:
1656 if (strcmp (name, ".options") != 0)
1657 return false;
1658 break;
1659 case SHT_MIPS_DWARF:
1660 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
1661 return false;
1662 break;
1663 case SHT_MIPS_EVENTS:
1664 if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1665 return false;
1666 break;
1667 default:
1668 return false;
1669 }
1670
1671 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1672 return false;
1673 newsect = hdr->bfd_section;
1674
1675 if (hdr->sh_type == SHT_MIPS_DEBUG)
1676 {
1677 if (! bfd_set_section_flags (abfd, newsect,
1678 (bfd_get_section_flags (abfd, newsect)
1679 | SEC_DEBUGGING)))
1680 return false;
1681 }
1682
1683 /* FIXME: We should record sh_info for a .gptab section. */
1684
1685 /* For a .reginfo section, set the gp value in the tdata information
1686 from the contents of this section. We need the gp value while
1687 processing relocs, so we just get it now. */
1688 if (hdr->sh_type == SHT_MIPS_REGINFO)
1689 {
1690 Elf32_External_RegInfo ext;
1691 Elf32_RegInfo s;
1692
1693 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1694 (file_ptr) 0, sizeof ext))
1695 return false;
1696 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
1697 elf_gp (abfd) = s.ri_gp_value;
1698 }
1699
1700 return true;
1701 }
1702
1703 /* Set the correct type for a MIPS ELF section. We do this by the
1704 section name, which is a hack, but ought to work. */
1705
1706 static boolean
1707 mips_elf_fake_sections (abfd, hdr, sec)
1708 bfd *abfd;
1709 Elf32_Internal_Shdr *hdr;
1710 asection *sec;
1711 {
1712 register const char *name;
1713
1714 name = bfd_get_section_name (abfd, sec);
1715
1716 if (strcmp (name, ".liblist") == 0)
1717 {
1718 hdr->sh_type = SHT_MIPS_LIBLIST;
1719 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
1720 /* FIXME: Set the sh_link field. */
1721 }
1722 else if (strcmp (name, ".msym") == 0)
1723 {
1724 hdr->sh_type = SHT_MIPS_MSYM;
1725 hdr->sh_entsize = 8;
1726 /* FIXME: Set the sh_info field. */
1727 }
1728 else if (strcmp (name, ".conflict") == 0)
1729 hdr->sh_type = SHT_MIPS_CONFLICT;
1730 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
1731 {
1732 hdr->sh_type = SHT_MIPS_GPTAB;
1733 hdr->sh_entsize = sizeof (Elf32_External_gptab);
1734 /* The sh_info field is set in mips_elf_final_write_processing. */
1735 }
1736 else if (strcmp (name, ".ucode") == 0)
1737 hdr->sh_type = SHT_MIPS_UCODE;
1738 else if (strcmp (name, ".mdebug") == 0)
1739 {
1740 hdr->sh_type = SHT_MIPS_DEBUG;
1741 /* In a shared object on Irix 5.3, the .mdebug section has an
1742 entsize of 0. FIXME: Does this matter? */
1743 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
1744 hdr->sh_entsize = 0;
1745 else
1746 hdr->sh_entsize = 1;
1747 }
1748 else if (strcmp (name, ".reginfo") == 0)
1749 {
1750 hdr->sh_type = SHT_MIPS_REGINFO;
1751 /* In a shared object on Irix 5.3, the .reginfo section has an
1752 entsize of 0x18. FIXME: Does this matter? */
1753 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
1754 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
1755 else
1756 hdr->sh_entsize = 1;
1757
1758 /* Force the section size to the correct value, even if the
1759 linker thinks it is larger. The link routine below will only
1760 write out this much data for .reginfo. */
1761 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
1762 }
1763 else if (SGI_COMPAT (abfd)
1764 && (strcmp (name, ".hash") == 0
1765 || strcmp (name, ".dynamic") == 0
1766 || strcmp (name, ".dynstr") == 0))
1767 {
1768 hdr->sh_entsize = 0;
1769 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
1770 }
1771 else if (strcmp (name, ".got") == 0
1772 || strcmp (name, ".sdata") == 0
1773 || strcmp (name, ".sbss") == 0
1774 || strcmp (name, ".lit4") == 0
1775 || strcmp (name, ".lit8") == 0)
1776 hdr->sh_flags |= SHF_MIPS_GPREL;
1777 else if (strcmp (name, ".options") == 0)
1778 {
1779 hdr->sh_type = SHT_MIPS_OPTIONS;
1780 hdr->sh_entsize = 1;
1781 }
1782 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
1783 hdr->sh_type = SHT_MIPS_DWARF;
1784 else if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1785 hdr->sh_type = SHT_MIPS_EVENTS;
1786
1787 return true;
1788 }
1789
1790 /* Given a BFD section, try to locate the corresponding ELF section
1791 index. */
1792
1793 static boolean
1794 mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
1795 bfd *abfd;
1796 Elf32_Internal_Shdr *hdr;
1797 asection *sec;
1798 int *retval;
1799 {
1800 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1801 {
1802 *retval = SHN_MIPS_SCOMMON;
1803 return true;
1804 }
1805 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
1806 {
1807 *retval = SHN_MIPS_ACOMMON;
1808 return true;
1809 }
1810 return false;
1811 }
1812
1813 /* Work over a section just before writing it out. We update the GP
1814 value in the .reginfo section based on the value we are using.
1815 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1816 name; there has to be a better way. */
1817
1818 static boolean
1819 mips_elf_section_processing (abfd, hdr)
1820 bfd *abfd;
1821 Elf32_Internal_Shdr *hdr;
1822 {
1823 if (hdr->sh_type == SHT_MIPS_REGINFO)
1824 {
1825 bfd_byte buf[4];
1826
1827 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
1828 BFD_ASSERT (hdr->contents == NULL);
1829
1830 if (bfd_seek (abfd,
1831 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
1832 SEEK_SET) == -1)
1833 return false;
1834 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
1835 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
1836 return false;
1837 }
1838
1839 if (hdr->bfd_section != NULL)
1840 {
1841 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
1842
1843 if (strcmp (name, ".sdata") == 0)
1844 {
1845 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1846 hdr->sh_type = SHT_PROGBITS;
1847 }
1848 else if (strcmp (name, ".sbss") == 0)
1849 {
1850 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1851 hdr->sh_type = SHT_NOBITS;
1852 }
1853 else if (strcmp (name, ".lit8") == 0
1854 || strcmp (name, ".lit4") == 0)
1855 {
1856 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1857 hdr->sh_type = SHT_PROGBITS;
1858 }
1859 else if (strcmp (name, ".compact_rel") == 0)
1860 {
1861 hdr->sh_flags = 0;
1862 hdr->sh_type = SHT_PROGBITS;
1863 }
1864 else if (strcmp (name, ".rtproc") == 0)
1865 {
1866 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
1867 {
1868 unsigned int adjust;
1869
1870 adjust = hdr->sh_size % hdr->sh_addralign;
1871 if (adjust != 0)
1872 hdr->sh_size += hdr->sh_addralign - adjust;
1873 }
1874 }
1875 }
1876
1877 return true;
1878 }
1879 \f
1880 /* MIPS ELF uses two common sections. One is the usual one, and the
1881 other is for small objects. All the small objects are kept
1882 together, and then referenced via the gp pointer, which yields
1883 faster assembler code. This is what we use for the small common
1884 section. This approach is copied from ecoff.c. */
1885 static asection mips_elf_scom_section;
1886 static asymbol mips_elf_scom_symbol;
1887 static asymbol *mips_elf_scom_symbol_ptr;
1888
1889 /* MIPS ELF also uses an acommon section, which represents an
1890 allocated common symbol which may be overridden by a
1891 definition in a shared library. */
1892 static asection mips_elf_acom_section;
1893 static asymbol mips_elf_acom_symbol;
1894 static asymbol *mips_elf_acom_symbol_ptr;
1895
1896 /* The Irix 5 support uses two virtual sections, which represent
1897 text/data symbols defined in dynamic objects. */
1898 static asection mips_elf_text_section;
1899 static asection *mips_elf_text_section_ptr;
1900 static asymbol mips_elf_text_symbol;
1901 static asymbol *mips_elf_text_symbol_ptr;
1902
1903 static asection mips_elf_data_section;
1904 static asection *mips_elf_data_section_ptr;
1905 static asymbol mips_elf_data_symbol;
1906 static asymbol *mips_elf_data_symbol_ptr;
1907
1908 /* Handle the special MIPS section numbers that a symbol may use. */
1909
1910 static void
1911 mips_elf_symbol_processing (abfd, asym)
1912 bfd *abfd;
1913 asymbol *asym;
1914 {
1915 elf_symbol_type *elfsym;
1916
1917 elfsym = (elf_symbol_type *) asym;
1918 switch (elfsym->internal_elf_sym.st_shndx)
1919 {
1920 case SHN_MIPS_ACOMMON:
1921 /* This section is used in a dynamically linked executable file.
1922 It is an allocated common section. The dynamic linker can
1923 either resolve these symbols to something in a shared
1924 library, or it can just leave them here. For our purposes,
1925 we can consider these symbols to be in a new section. */
1926 if (mips_elf_acom_section.name == NULL)
1927 {
1928 /* Initialize the acommon section. */
1929 mips_elf_acom_section.name = ".acommon";
1930 mips_elf_acom_section.flags = SEC_ALLOC;
1931 mips_elf_acom_section.output_section = &mips_elf_acom_section;
1932 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
1933 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
1934 mips_elf_acom_symbol.name = ".acommon";
1935 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
1936 mips_elf_acom_symbol.section = &mips_elf_acom_section;
1937 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
1938 }
1939 asym->section = &mips_elf_acom_section;
1940 break;
1941
1942 case SHN_COMMON:
1943 /* Common symbols less than the GP size are automatically
1944 treated as SHN_MIPS_SCOMMON symbols. */
1945 if (asym->value > elf_gp_size (abfd))
1946 break;
1947 /* Fall through. */
1948 case SHN_MIPS_SCOMMON:
1949 if (mips_elf_scom_section.name == NULL)
1950 {
1951 /* Initialize the small common section. */
1952 mips_elf_scom_section.name = ".scommon";
1953 mips_elf_scom_section.flags = SEC_IS_COMMON;
1954 mips_elf_scom_section.output_section = &mips_elf_scom_section;
1955 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
1956 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
1957 mips_elf_scom_symbol.name = ".scommon";
1958 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
1959 mips_elf_scom_symbol.section = &mips_elf_scom_section;
1960 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
1961 }
1962 asym->section = &mips_elf_scom_section;
1963 asym->value = elfsym->internal_elf_sym.st_size;
1964 break;
1965
1966 case SHN_MIPS_SUNDEFINED:
1967 asym->section = bfd_und_section_ptr;
1968 break;
1969
1970 #if 0 /* for SGI_COMPAT */
1971 case SHN_MIPS_TEXT:
1972 asym->section = mips_elf_text_section_ptr;
1973 break;
1974
1975 case SHN_MIPS_DATA:
1976 asym->section = mips_elf_data_section_ptr;
1977 break;
1978 #endif
1979 }
1980 }
1981 \f
1982 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
1983 segments. */
1984
1985 static int
1986 mips_elf_additional_program_headers (abfd)
1987 bfd *abfd;
1988 {
1989 asection *s;
1990 int ret;
1991
1992 ret = 0;
1993
1994 if (! SGI_COMPAT (abfd))
1995 return ret;
1996
1997 s = bfd_get_section_by_name (abfd, ".reginfo");
1998 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1999 {
2000 /* We need a PT_MIPS_REGINFO segment. */
2001 ++ret;
2002 }
2003
2004 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2005 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2006 {
2007 /* We need a PT_MIPS_RTPROC segment. */
2008 ++ret;
2009 }
2010
2011 return ret;
2012 }
2013
2014 /* Modify the segment map for an Irix 5 executable. */
2015
2016 static boolean
2017 mips_elf_modify_segment_map (abfd)
2018 bfd *abfd;
2019 {
2020 asection *s;
2021 struct elf_segment_map *m, **pm;
2022
2023 if (! SGI_COMPAT (abfd))
2024 return true;
2025
2026 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2027 segment. */
2028 s = bfd_get_section_by_name (abfd, ".reginfo");
2029 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2030 {
2031 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2032 if (m->p_type == PT_MIPS_REGINFO)
2033 break;
2034 if (m == NULL)
2035 {
2036 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2037 if (m == NULL)
2038 return false;
2039
2040 m->p_type = PT_MIPS_REGINFO;
2041 m->count = 1;
2042 m->sections[0] = s;
2043
2044 /* We want to put it after the PHDR and INTERP segments. */
2045 pm = &elf_tdata (abfd)->segment_map;
2046 while (*pm != NULL
2047 && ((*pm)->p_type == PT_PHDR
2048 || (*pm)->p_type == PT_INTERP))
2049 pm = &(*pm)->next;
2050
2051 m->next = *pm;
2052 *pm = m;
2053 }
2054 }
2055
2056 /* If there are .dynamic and .mdebug sections, we make a room for
2057 the RTPROC header. FIXME: Rewrite without section names. */
2058 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2059 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2060 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2061 {
2062 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2063 if (m->p_type == PT_MIPS_RTPROC)
2064 break;
2065 if (m == NULL)
2066 {
2067 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2068 if (m == NULL)
2069 return false;
2070
2071 m->p_type = PT_MIPS_RTPROC;
2072
2073 s = bfd_get_section_by_name (abfd, ".rtproc");
2074 if (s == NULL)
2075 {
2076 m->count = 0;
2077 m->p_flags = 0;
2078 m->p_flags_valid = 1;
2079 }
2080 else
2081 {
2082 m->count = 1;
2083 m->sections[0] = s;
2084 }
2085
2086 /* We want to put it after the DYNAMIC segment. */
2087 pm = &elf_tdata (abfd)->segment_map;
2088 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2089 pm = &(*pm)->next;
2090 if (*pm != NULL)
2091 pm = &(*pm)->next;
2092
2093 m->next = *pm;
2094 *pm = m;
2095 }
2096 }
2097
2098 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2099 .dynsym, and .hash sections, and everything in between. */
2100 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2101 if ((*pm)->p_type == PT_DYNAMIC)
2102 break;
2103 m = *pm;
2104 if (m != NULL
2105 && m->count == 1
2106 && strcmp (m->sections[0]->name, ".dynamic") == 0)
2107 {
2108 static const char *sec_names[] =
2109 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2110 bfd_vma low, high;
2111 unsigned int i, c;
2112 struct elf_segment_map *n;
2113
2114 low = 0xffffffff;
2115 high = 0;
2116 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
2117 {
2118 s = bfd_get_section_by_name (abfd, sec_names[i]);
2119 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2120 {
2121 bfd_size_type sz;
2122
2123 if (low > s->vma)
2124 low = s->vma;
2125 sz = s->_cooked_size;
2126 if (sz == 0)
2127 sz = s->_raw_size;
2128 if (high < s->vma + sz)
2129 high = s->vma + sz;
2130 }
2131 }
2132
2133 c = 0;
2134 for (s = abfd->sections; s != NULL; s = s->next)
2135 if ((s->flags & SEC_LOAD) != 0
2136 && s->vma >= low
2137 && ((s->vma
2138 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2139 <= high))
2140 ++c;
2141
2142 n = ((struct elf_segment_map *)
2143 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
2144 if (n == NULL)
2145 return false;
2146 *n = *m;
2147 n->count = c;
2148
2149 i = 0;
2150 for (s = abfd->sections; s != NULL; s = s->next)
2151 {
2152 if ((s->flags & SEC_LOAD) != 0
2153 && s->vma >= low
2154 && ((s->vma
2155 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2156 <= high))
2157 {
2158 n->sections[i] = s;
2159 ++i;
2160 }
2161 }
2162
2163 *pm = n;
2164 }
2165
2166 return true;
2167 }
2168 \f
2169 /* The structure of the runtime procedure descriptor created by the
2170 loader for use by the static exception system. */
2171
2172 typedef struct runtime_pdr {
2173 bfd_vma adr; /* memory address of start of procedure */
2174 long regmask; /* save register mask */
2175 long regoffset; /* save register offset */
2176 long fregmask; /* save floating point register mask */
2177 long fregoffset; /* save floating point register offset */
2178 long frameoffset; /* frame size */
2179 short framereg; /* frame pointer register */
2180 short pcreg; /* offset or reg of return pc */
2181 long irpss; /* index into the runtime string table */
2182 long reserved;
2183 struct exception_info *exception_info;/* pointer to exception array */
2184 } RPDR, *pRPDR;
2185 #define cbRPDR sizeof(RPDR)
2186 #define rpdNil ((pRPDR) 0)
2187
2188 /* Swap RPDR (runtime procedure table entry) for output. */
2189
2190 static void ecoff_swap_rpdr_out
2191 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
2192
2193 static void
2194 ecoff_swap_rpdr_out (abfd, in, ex)
2195 bfd *abfd;
2196 const RPDR *in;
2197 struct rpdr_ext *ex;
2198 {
2199 /* ecoff_put_off was defined in ecoffswap.h. */
2200 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
2201 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
2202 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
2203 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
2204 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
2205 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
2206
2207 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
2208 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
2209
2210 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
2211 #if 0 /* FIXME */
2212 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
2213 #endif
2214 }
2215 \f
2216 /* Read ECOFF debugging information from a .mdebug section into a
2217 ecoff_debug_info structure. */
2218
2219 static boolean
2220 mips_elf_read_ecoff_info (abfd, section, debug)
2221 bfd *abfd;
2222 asection *section;
2223 struct ecoff_debug_info *debug;
2224 {
2225 HDRR *symhdr;
2226 const struct ecoff_debug_swap *swap;
2227 char *ext_hdr = NULL;
2228
2229 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2230
2231 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
2232 if (ext_hdr == NULL && swap->external_hdr_size != 0)
2233 goto error_return;
2234
2235 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2236 swap->external_hdr_size)
2237 == false)
2238 goto error_return;
2239
2240 symhdr = &debug->symbolic_header;
2241 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2242
2243 /* The symbolic header contains absolute file offsets and sizes to
2244 read. */
2245 #define READ(ptr, offset, count, size, type) \
2246 if (symhdr->count == 0) \
2247 debug->ptr = NULL; \
2248 else \
2249 { \
2250 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2251 if (debug->ptr == NULL) \
2252 goto error_return; \
2253 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2254 || (bfd_read (debug->ptr, size, symhdr->count, \
2255 abfd) != size * symhdr->count)) \
2256 goto error_return; \
2257 }
2258
2259 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2260 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2261 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2262 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2263 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2264 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2265 union aux_ext *);
2266 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2267 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2268 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2269 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2270 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2271 #undef READ
2272
2273 debug->fdr = NULL;
2274 debug->adjust = NULL;
2275
2276 return true;
2277
2278 error_return:
2279 if (ext_hdr != NULL)
2280 free (ext_hdr);
2281 if (debug->line != NULL)
2282 free (debug->line);
2283 if (debug->external_dnr != NULL)
2284 free (debug->external_dnr);
2285 if (debug->external_pdr != NULL)
2286 free (debug->external_pdr);
2287 if (debug->external_sym != NULL)
2288 free (debug->external_sym);
2289 if (debug->external_opt != NULL)
2290 free (debug->external_opt);
2291 if (debug->external_aux != NULL)
2292 free (debug->external_aux);
2293 if (debug->ss != NULL)
2294 free (debug->ss);
2295 if (debug->ssext != NULL)
2296 free (debug->ssext);
2297 if (debug->external_fdr != NULL)
2298 free (debug->external_fdr);
2299 if (debug->external_rfd != NULL)
2300 free (debug->external_rfd);
2301 if (debug->external_ext != NULL)
2302 free (debug->external_ext);
2303 return false;
2304 }
2305 \f
2306 /* MIPS ELF local labels start with '$', not 'L'. */
2307
2308 /*ARGSUSED*/
2309 static boolean
2310 mips_elf_is_local_label (abfd, symbol)
2311 bfd *abfd;
2312 asymbol *symbol;
2313 {
2314 return symbol->name[0] == '$';
2315 }
2316
2317 /* MIPS ELF uses a special find_nearest_line routine in order the
2318 handle the ECOFF debugging information. */
2319
2320 struct mips_elf_find_line
2321 {
2322 struct ecoff_debug_info d;
2323 struct ecoff_find_line i;
2324 };
2325
2326 static boolean
2327 mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2328 functionname_ptr, line_ptr)
2329 bfd *abfd;
2330 asection *section;
2331 asymbol **symbols;
2332 bfd_vma offset;
2333 const char **filename_ptr;
2334 const char **functionname_ptr;
2335 unsigned int *line_ptr;
2336 {
2337 asection *msec;
2338
2339 msec = bfd_get_section_by_name (abfd, ".mdebug");
2340 if (msec != NULL)
2341 {
2342 flagword origflags;
2343 struct mips_elf_find_line *fi;
2344 const struct ecoff_debug_swap * const swap =
2345 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2346
2347 /* If we are called during a link, mips_elf_final_link may have
2348 cleared the SEC_HAS_CONTENTS field. We force it back on here
2349 if appropriate (which it normally will be). */
2350 origflags = msec->flags;
2351 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2352 msec->flags |= SEC_HAS_CONTENTS;
2353
2354 fi = elf_tdata (abfd)->find_line_info;
2355 if (fi == NULL)
2356 {
2357 bfd_size_type external_fdr_size;
2358 char *fraw_src;
2359 char *fraw_end;
2360 struct fdr *fdr_ptr;
2361
2362 fi = ((struct mips_elf_find_line *)
2363 bfd_alloc (abfd, sizeof (struct mips_elf_find_line)));
2364 if (fi == NULL)
2365 {
2366 msec->flags = origflags;
2367 return false;
2368 }
2369
2370 memset (fi, 0, sizeof (struct mips_elf_find_line));
2371
2372 if (! mips_elf_read_ecoff_info (abfd, msec, &fi->d))
2373 {
2374 msec->flags = origflags;
2375 return false;
2376 }
2377
2378 /* Swap in the FDR information. */
2379 fi->d.fdr = ((struct fdr *)
2380 bfd_alloc (abfd,
2381 (fi->d.symbolic_header.ifdMax *
2382 sizeof (struct fdr))));
2383 if (fi->d.fdr == NULL)
2384 {
2385 msec->flags = origflags;
2386 return false;
2387 }
2388 external_fdr_size = swap->external_fdr_size;
2389 fdr_ptr = fi->d.fdr;
2390 fraw_src = (char *) fi->d.external_fdr;
2391 fraw_end = (fraw_src
2392 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2393 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2394 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2395
2396 elf_tdata (abfd)->find_line_info = fi;
2397
2398 /* Note that we don't bother to ever free this information.
2399 find_nearest_line is either called all the time, as in
2400 objdump -l, so the information should be saved, or it is
2401 rarely called, as in ld error messages, so the memory
2402 wasted is unimportant. Still, it would probably be a
2403 good idea for free_cached_info to throw it away. */
2404 }
2405
2406 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2407 &fi->i, filename_ptr, functionname_ptr,
2408 line_ptr))
2409 {
2410 msec->flags = origflags;
2411 return true;
2412 }
2413
2414 msec->flags = origflags;
2415 }
2416
2417 /* Fall back on the generic ELF find_nearest_line routine. */
2418
2419 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2420 filename_ptr, functionname_ptr,
2421 line_ptr);
2422 }
2423 \f
2424 /* The MIPS ELF linker needs additional information for each symbol in
2425 the global hash table. */
2426
2427 struct mips_elf_link_hash_entry
2428 {
2429 struct elf_link_hash_entry root;
2430
2431 /* External symbol information. */
2432 EXTR esym;
2433 };
2434
2435 /* MIPS ELF linker hash table. */
2436
2437 struct mips_elf_link_hash_table
2438 {
2439 struct elf_link_hash_table root;
2440 /* String section indices for the dynamic section symbols. */
2441 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
2442 /* The number of .rtproc entries. */
2443 bfd_size_type procedure_count;
2444 /* The size of the .compact_rel section (if SGI_COMPAT). */
2445 bfd_size_type compact_rel_size;
2446 };
2447
2448 /* Look up an entry in a MIPS ELF linker hash table. */
2449
2450 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
2451 ((struct mips_elf_link_hash_entry *) \
2452 elf_link_hash_lookup (&(table)->root, (string), (create), \
2453 (copy), (follow)))
2454
2455 /* Traverse a MIPS ELF linker hash table. */
2456
2457 #define mips_elf_link_hash_traverse(table, func, info) \
2458 (elf_link_hash_traverse \
2459 (&(table)->root, \
2460 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2461 (info)))
2462
2463 /* Get the MIPS ELF linker hash table from a link_info structure. */
2464
2465 #define mips_elf_hash_table(p) \
2466 ((struct mips_elf_link_hash_table *) ((p)->hash))
2467
2468 static boolean mips_elf_output_extsym
2469 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
2470
2471 /* Create an entry in a MIPS ELF linker hash table. */
2472
2473 static struct bfd_hash_entry *
2474 mips_elf_link_hash_newfunc (entry, table, string)
2475 struct bfd_hash_entry *entry;
2476 struct bfd_hash_table *table;
2477 const char *string;
2478 {
2479 struct mips_elf_link_hash_entry *ret =
2480 (struct mips_elf_link_hash_entry *) entry;
2481
2482 /* Allocate the structure if it has not already been allocated by a
2483 subclass. */
2484 if (ret == (struct mips_elf_link_hash_entry *) NULL)
2485 ret = ((struct mips_elf_link_hash_entry *)
2486 bfd_hash_allocate (table,
2487 sizeof (struct mips_elf_link_hash_entry)));
2488 if (ret == (struct mips_elf_link_hash_entry *) NULL)
2489 return (struct bfd_hash_entry *) ret;
2490
2491 /* Call the allocation method of the superclass. */
2492 ret = ((struct mips_elf_link_hash_entry *)
2493 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2494 table, string));
2495 if (ret != (struct mips_elf_link_hash_entry *) NULL)
2496 {
2497 /* Set local fields. */
2498 memset (&ret->esym, 0, sizeof (EXTR));
2499 /* We use -2 as a marker to indicate that the information has
2500 not been set. -1 means there is no associated ifd. */
2501 ret->esym.ifd = -2;
2502 }
2503
2504 return (struct bfd_hash_entry *) ret;
2505 }
2506
2507 /* Create a MIPS ELF linker hash table. */
2508
2509 static struct bfd_link_hash_table *
2510 mips_elf_link_hash_table_create (abfd)
2511 bfd *abfd;
2512 {
2513 struct mips_elf_link_hash_table *ret;
2514 unsigned int i;
2515
2516 ret = ((struct mips_elf_link_hash_table *)
2517 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
2518 if (ret == (struct mips_elf_link_hash_table *) NULL)
2519 return NULL;
2520
2521 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2522 mips_elf_link_hash_newfunc))
2523 {
2524 bfd_release (abfd, ret);
2525 return NULL;
2526 }
2527
2528 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
2529 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
2530 ret->procedure_count = 0;
2531 ret->compact_rel_size = 0;
2532
2533 return &ret->root.root;
2534 }
2535
2536 /* Hook called by the linker routine which adds symbols from an object
2537 file. We must handle the special MIPS section numbers here. */
2538
2539 /*ARGSUSED*/
2540 static boolean
2541 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2542 bfd *abfd;
2543 struct bfd_link_info *info;
2544 const Elf_Internal_Sym *sym;
2545 const char **namep;
2546 flagword *flagsp;
2547 asection **secp;
2548 bfd_vma *valp;
2549 {
2550 if (SGI_COMPAT (abfd)
2551 && (abfd->flags & DYNAMIC) != 0
2552 && strcmp (*namep, "_rld_new_interface") == 0)
2553 {
2554 /* Skip Irix 5 rld entry name. */
2555 *namep = NULL;
2556 return true;
2557 }
2558
2559 switch (sym->st_shndx)
2560 {
2561 case SHN_COMMON:
2562 /* Common symbols less than the GP size are automatically
2563 treated as SHN_MIPS_SCOMMON symbols. */
2564 if (sym->st_size > elf_gp_size (abfd))
2565 break;
2566 /* Fall through. */
2567 case SHN_MIPS_SCOMMON:
2568 *secp = bfd_make_section_old_way (abfd, ".scommon");
2569 (*secp)->flags |= SEC_IS_COMMON;
2570 *valp = sym->st_size;
2571 break;
2572
2573 case SHN_MIPS_TEXT:
2574 /* This section is used in a shared object. */
2575 if (mips_elf_text_section_ptr == NULL)
2576 {
2577 /* Initialize the section. */
2578 mips_elf_text_section.name = ".text";
2579 mips_elf_text_section.flags = SEC_NO_FLAGS;
2580 mips_elf_text_section.output_section = NULL;
2581 mips_elf_text_section.owner = abfd;
2582 mips_elf_text_section.symbol = &mips_elf_text_symbol;
2583 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
2584 mips_elf_text_symbol.name = ".text";
2585 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
2586 mips_elf_text_symbol.section = &mips_elf_text_section;
2587 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
2588 mips_elf_text_section_ptr = &mips_elf_text_section;
2589 }
2590 if (info->shared)
2591 *secp = bfd_und_section_ptr;
2592 else
2593 *secp = mips_elf_text_section_ptr;
2594 break;
2595
2596 case SHN_MIPS_ACOMMON:
2597 /* Fall through. XXX Can we treat this as allocated data? */
2598 case SHN_MIPS_DATA:
2599 /* This section is used in a shared object. */
2600 if (mips_elf_data_section_ptr == NULL)
2601 {
2602 /* Initialize the section. */
2603 mips_elf_data_section.name = ".data";
2604 mips_elf_data_section.flags = SEC_NO_FLAGS;
2605 mips_elf_data_section.output_section = NULL;
2606 mips_elf_data_section.owner = abfd;
2607 mips_elf_data_section.symbol = &mips_elf_data_symbol;
2608 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
2609 mips_elf_data_symbol.name = ".data";
2610 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
2611 mips_elf_data_symbol.section = &mips_elf_data_section;
2612 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
2613 mips_elf_data_section_ptr = &mips_elf_data_section;
2614 }
2615 if (info->shared)
2616 *secp = bfd_und_section_ptr;
2617 else
2618 *secp = mips_elf_data_section_ptr;
2619 break;
2620
2621 case SHN_MIPS_SUNDEFINED:
2622 *secp = bfd_und_section_ptr;
2623 break;
2624 }
2625
2626 return true;
2627 }
2628
2629 /* Structure used to pass information to mips_elf_output_extsym. */
2630
2631 struct extsym_info
2632 {
2633 bfd *abfd;
2634 struct bfd_link_info *info;
2635 struct ecoff_debug_info *debug;
2636 const struct ecoff_debug_swap *swap;
2637 boolean failed;
2638 };
2639
2640 /* This routine is used to write out ECOFF debugging external symbol
2641 information. It is called via mips_elf_link_hash_traverse. The
2642 ECOFF external symbol information must match the ELF external
2643 symbol information. Unfortunately, at this point we don't know
2644 whether a symbol is required by reloc information, so the two
2645 tables may wind up being different. We must sort out the external
2646 symbol information before we can set the final size of the .mdebug
2647 section, and we must set the size of the .mdebug section before we
2648 can relocate any sections, and we can't know which symbols are
2649 required by relocation until we relocate the sections.
2650 Fortunately, it is relatively unlikely that any symbol will be
2651 stripped but required by a reloc. In particular, it can not happen
2652 when generating a final executable. */
2653
2654 static boolean
2655 mips_elf_output_extsym (h, data)
2656 struct mips_elf_link_hash_entry *h;
2657 PTR data;
2658 {
2659 struct extsym_info *einfo = (struct extsym_info *) data;
2660 boolean strip;
2661 asection *sec, *output_section;
2662
2663 if (h->root.indx == -2)
2664 strip = false;
2665 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2666 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2667 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2668 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2669 strip = true;
2670 else if (einfo->info->strip == strip_all
2671 || (einfo->info->strip == strip_some
2672 && bfd_hash_lookup (einfo->info->keep_hash,
2673 h->root.root.root.string,
2674 false, false) == NULL))
2675 strip = true;
2676 else
2677 strip = false;
2678
2679 if (strip)
2680 return true;
2681
2682 if (h->esym.ifd == -2)
2683 {
2684 h->esym.jmptbl = 0;
2685 h->esym.cobol_main = 0;
2686 h->esym.weakext = 0;
2687 h->esym.reserved = 0;
2688 h->esym.ifd = ifdNil;
2689 h->esym.asym.value = 0;
2690 h->esym.asym.st = stGlobal;
2691
2692 if (SGI_COMPAT (einfo->abfd)
2693 && (h->root.root.type == bfd_link_hash_undefined
2694 || h->root.root.type == bfd_link_hash_undefweak))
2695 {
2696 const char *name;
2697
2698 /* Use undefined class. Also, set class and type for some
2699 special symbols. */
2700 name = h->root.root.root.string;
2701 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2702 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2703 {
2704 h->esym.asym.sc = scData;
2705 h->esym.asym.st = stLabel;
2706 h->esym.asym.value = 0;
2707 }
2708 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2709 {
2710 h->esym.asym.sc = scAbs;
2711 h->esym.asym.st = stLabel;
2712 h->esym.asym.value =
2713 mips_elf_hash_table (einfo->info)->procedure_count;
2714 }
2715 else if (strcmp (name, "_gp_disp") == 0)
2716 {
2717 h->esym.asym.sc = scAbs;
2718 h->esym.asym.st = stLabel;
2719 h->esym.asym.value = elf_gp (einfo->abfd);
2720 }
2721 else
2722 h->esym.asym.sc = scUndefined;
2723 }
2724 else if (h->root.root.type != bfd_link_hash_defined
2725 && h->root.root.type != bfd_link_hash_defweak)
2726 h->esym.asym.sc = scAbs;
2727 else
2728 {
2729 const char *name;
2730
2731 sec = h->root.root.u.def.section;
2732 output_section = sec->output_section;
2733
2734 /* When making a shared library and symbol h is the one from
2735 the another shared library, OUTPUT_SECTION may be null. */
2736 if (output_section == NULL)
2737 h->esym.asym.sc = scUndefined;
2738 else
2739 {
2740 name = bfd_section_name (output_section->owner, output_section);
2741
2742 if (strcmp (name, ".text") == 0)
2743 h->esym.asym.sc = scText;
2744 else if (strcmp (name, ".data") == 0)
2745 h->esym.asym.sc = scData;
2746 else if (strcmp (name, ".sdata") == 0)
2747 h->esym.asym.sc = scSData;
2748 else if (strcmp (name, ".rodata") == 0
2749 || strcmp (name, ".rdata") == 0)
2750 h->esym.asym.sc = scRData;
2751 else if (strcmp (name, ".bss") == 0)
2752 h->esym.asym.sc = scBss;
2753 else if (strcmp (name, ".sbss") == 0)
2754 h->esym.asym.sc = scSBss;
2755 else if (strcmp (name, ".init") == 0)
2756 h->esym.asym.sc = scInit;
2757 else if (strcmp (name, ".fini") == 0)
2758 h->esym.asym.sc = scFini;
2759 else
2760 h->esym.asym.sc = scAbs;
2761 }
2762 }
2763
2764 h->esym.asym.reserved = 0;
2765 h->esym.asym.index = indexNil;
2766 }
2767
2768 if (h->root.root.type == bfd_link_hash_common)
2769 h->esym.asym.value = h->root.root.u.c.size;
2770 else if (h->root.root.type == bfd_link_hash_defined
2771 || h->root.root.type == bfd_link_hash_defweak)
2772 {
2773 if (h->esym.asym.sc == scCommon)
2774 h->esym.asym.sc = scBss;
2775 else if (h->esym.asym.sc == scSCommon)
2776 h->esym.asym.sc = scSBss;
2777
2778 sec = h->root.root.u.def.section;
2779 output_section = sec->output_section;
2780 if (output_section != NULL)
2781 h->esym.asym.value = (h->root.root.u.def.value
2782 + sec->output_offset
2783 + output_section->vma);
2784 else
2785 h->esym.asym.value = 0;
2786 }
2787 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2788 {
2789 /* Set type and value for a symbol with a function stub. */
2790 h->esym.asym.st = stProc;
2791 sec = h->root.root.u.def.section;
2792 if (sec == NULL)
2793 h->esym.asym.value = 0;
2794 else
2795 {
2796 output_section = sec->output_section;
2797 if (output_section != NULL)
2798 h->esym.asym.value = (h->root.plt_offset
2799 + sec->output_offset
2800 + output_section->vma);
2801 else
2802 h->esym.asym.value = 0;
2803 }
2804 #if 0 /* FIXME? */
2805 h->esym.ifd = 0;
2806 #endif
2807 }
2808
2809 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2810 h->root.root.root.string,
2811 &h->esym))
2812 {
2813 einfo->failed = true;
2814 return false;
2815 }
2816
2817 return true;
2818 }
2819
2820 /* Create a runtime procedure table from the .mdebug section. */
2821
2822 static boolean
2823 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
2824 PTR handle;
2825 bfd *abfd;
2826 struct bfd_link_info *info;
2827 asection *s;
2828 struct ecoff_debug_info *debug;
2829 {
2830 const struct ecoff_debug_swap *swap;
2831 HDRR *hdr = &debug->symbolic_header;
2832 RPDR *rpdr, *rp;
2833 struct rpdr_ext *erp;
2834 PTR rtproc;
2835 struct pdr_ext *epdr;
2836 struct sym_ext *esym;
2837 char *ss, **sv;
2838 char *str;
2839 unsigned long size, count;
2840 unsigned long sindex;
2841 unsigned long i;
2842 PDR pdr;
2843 SYMR sym;
2844 const char *no_name_func = "static procedure (no name)";
2845
2846 epdr = NULL;
2847 rpdr = NULL;
2848 esym = NULL;
2849 ss = NULL;
2850
2851 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2852
2853 sindex = strlen (no_name_func) + 1;
2854 count = hdr->ipdMax;
2855 if (count > 0)
2856 {
2857 size = swap->external_pdr_size;
2858
2859 epdr = (struct pdr_ext *) bfd_malloc (size * count);
2860 if (epdr == NULL)
2861 goto error_return;
2862
2863 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
2864 goto error_return;
2865
2866 size = sizeof (RPDR);
2867 rp = rpdr = (RPDR *) bfd_malloc (size * count);
2868 if (rpdr == NULL)
2869 goto error_return;
2870
2871 sv = (char **) bfd_malloc (sizeof (char *) * count);
2872 if (sv == NULL)
2873 goto error_return;
2874
2875 count = hdr->isymMax;
2876 size = swap->external_sym_size;
2877 esym = (struct sym_ext *) bfd_malloc (size * count);
2878 if (esym == NULL)
2879 goto error_return;
2880
2881 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
2882 goto error_return;
2883
2884 count = hdr->issMax;
2885 ss = (char *) bfd_malloc (count);
2886 if (ss == NULL)
2887 goto error_return;
2888 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
2889 goto error_return;
2890
2891 count = hdr->ipdMax;
2892 for (i = 0; i < count; i++, rp++, epdr++)
2893 {
2894 (*swap->swap_pdr_in) (abfd, (PTR) epdr, &pdr);
2895 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
2896 rp->adr = sym.value;
2897 rp->regmask = pdr.regmask;
2898 rp->regoffset = pdr.regoffset;
2899 rp->fregmask = pdr.fregmask;
2900 rp->fregoffset = pdr.fregoffset;
2901 rp->frameoffset = pdr.frameoffset;
2902 rp->framereg = pdr.framereg;
2903 rp->pcreg = pdr.pcreg;
2904 rp->irpss = sindex;
2905 sv[i] = ss + sym.iss;
2906 sindex += strlen (sv[i]) + 1;
2907 }
2908 }
2909
2910 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
2911 size = BFD_ALIGN (size, 16);
2912 rtproc = (PTR) bfd_alloc (abfd, size);
2913 if (rtproc == NULL)
2914 {
2915 mips_elf_hash_table (info)->procedure_count = 0;
2916 goto error_return;
2917 }
2918
2919 mips_elf_hash_table (info)->procedure_count = count + 2;
2920
2921 erp = (struct rpdr_ext *) rtproc;
2922 memset (rp, 0, sizeof (struct rpdr_ext));
2923 erp++;
2924 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
2925 strcpy (str, no_name_func);
2926 str += strlen (no_name_func) + 1;
2927 for (i = 0; i < count; i++)
2928 {
2929 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
2930 strcpy (str, sv[i]);
2931 str += strlen (sv[i]) + 1;
2932 }
2933 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
2934
2935 /* Set the size and contents of .rtproc section. */
2936 s->_raw_size = size;
2937 s->contents = rtproc;
2938
2939 /* Skip this section later on (I don't think this currently
2940 matters, but someday it might). */
2941 s->link_order_head = (struct bfd_link_order *) NULL;
2942
2943 free (epdr);
2944 free (rpdr);
2945 free (esym);
2946 free (ss);
2947 free (sv);
2948
2949 return true;
2950
2951 error_return:
2952 if (epdr != NULL)
2953 free (epdr);
2954 if (rpdr != NULL)
2955 free (rpdr);
2956 if (esym != NULL)
2957 free (esym);
2958 if (ss != NULL)
2959 free (ss);
2960 if (sv != NULL)
2961 free (sv);
2962 return false;
2963 }
2964
2965 /* A comparison routine used to sort .gptab entries. */
2966
2967 static int
2968 gptab_compare (p1, p2)
2969 const PTR p1;
2970 const PTR p2;
2971 {
2972 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
2973 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
2974
2975 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2976 }
2977
2978 /* We need to use a special link routine to handle the .reginfo and
2979 the .mdebug sections. We need to merge all instances of these
2980 sections together, not write them all out sequentially. */
2981
2982 static boolean
2983 mips_elf_final_link (abfd, info)
2984 bfd *abfd;
2985 struct bfd_link_info *info;
2986 {
2987 asection **secpp;
2988 asection *o;
2989 struct bfd_link_order *p;
2990 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
2991 asection *rtproc_sec;
2992 Elf32_RegInfo reginfo;
2993 struct ecoff_debug_info debug;
2994 const struct ecoff_debug_swap *swap
2995 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2996 HDRR *symhdr = &debug.symbolic_header;
2997 PTR mdebug_handle = NULL;
2998
2999 /* Drop the .options section, since it has special semantics which I
3000 haven't bothered to figure out. */
3001 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
3002 {
3003 if (strcmp ((*secpp)->name, ".options") == 0)
3004 {
3005 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
3006 if (p->type == bfd_indirect_link_order)
3007 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
3008 (*secpp)->link_order_head = NULL;
3009 *secpp = (*secpp)->next;
3010 --abfd->section_count;
3011 break;
3012 }
3013 }
3014
3015 /* Get a value for the GP register. */
3016 if (elf_gp (abfd) == 0)
3017 {
3018 struct bfd_link_hash_entry *h;
3019
3020 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
3021 if (h != (struct bfd_link_hash_entry *) NULL
3022 && h->type == bfd_link_hash_defined)
3023 elf_gp (abfd) = (h->u.def.value
3024 + h->u.def.section->output_section->vma
3025 + h->u.def.section->output_offset);
3026 else if (info->relocateable)
3027 {
3028 bfd_vma lo;
3029
3030 /* Make up a value. */
3031 lo = (bfd_vma) -1;
3032 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3033 {
3034 if (o->vma < lo
3035 && (strcmp (o->name, ".sbss") == 0
3036 || strcmp (o->name, ".sdata") == 0
3037 || strcmp (o->name, ".lit4") == 0
3038 || strcmp (o->name, ".lit8") == 0))
3039 lo = o->vma;
3040 }
3041 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
3042 }
3043 else
3044 {
3045 /* If the relocate_section function needs to do a reloc
3046 involving the GP value, it should make a reloc_dangerous
3047 callback to warn that GP is not defined. */
3048 }
3049 }
3050
3051 /* Go through the sections and collect the .reginfo and .mdebug
3052 information. */
3053 reginfo_sec = NULL;
3054 mdebug_sec = NULL;
3055 gptab_data_sec = NULL;
3056 gptab_bss_sec = NULL;
3057 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3058 {
3059 if (strcmp (o->name, ".reginfo") == 0)
3060 {
3061 memset (&reginfo, 0, sizeof reginfo);
3062
3063 /* We have found the .reginfo section in the output file.
3064 Look through all the link_orders comprising it and merge
3065 the information together. */
3066 for (p = o->link_order_head;
3067 p != (struct bfd_link_order *) NULL;
3068 p = p->next)
3069 {
3070 asection *input_section;
3071 bfd *input_bfd;
3072 Elf32_External_RegInfo ext;
3073 Elf32_RegInfo sub;
3074
3075 if (p->type != bfd_indirect_link_order)
3076 {
3077 if (p->type == bfd_fill_link_order)
3078 continue;
3079 abort ();
3080 }
3081
3082 input_section = p->u.indirect.section;
3083 input_bfd = input_section->owner;
3084
3085 /* The linker emulation code has probably clobbered the
3086 size to be zero bytes. */
3087 if (input_section->_raw_size == 0)
3088 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
3089
3090 if (! bfd_get_section_contents (input_bfd, input_section,
3091 (PTR) &ext,
3092 (file_ptr) 0,
3093 sizeof ext))
3094 return false;
3095
3096 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
3097
3098 reginfo.ri_gprmask |= sub.ri_gprmask;
3099 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3100 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3101 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3102 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3103
3104 /* ri_gp_value is set by the function
3105 mips_elf_section_processing when the section is
3106 finally written out. */
3107
3108 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3109 elf_link_input_bfd ignores this section. */
3110 input_section->flags &=~ SEC_HAS_CONTENTS;
3111 }
3112
3113 /* Force the section size to the value we want. */
3114 o->_raw_size = sizeof (Elf32_External_RegInfo);
3115
3116 /* Skip this section later on (I don't think this currently
3117 matters, but someday it might). */
3118 o->link_order_head = (struct bfd_link_order *) NULL;
3119
3120 reginfo_sec = o;
3121 }
3122
3123 if (strcmp (o->name, ".mdebug") == 0)
3124 {
3125 struct extsym_info einfo;
3126
3127 /* We have found the .mdebug section in the output file.
3128 Look through all the link_orders comprising it and merge
3129 the information together. */
3130 symhdr->magic = swap->sym_magic;
3131 /* FIXME: What should the version stamp be? */
3132 symhdr->vstamp = 0;
3133 symhdr->ilineMax = 0;
3134 symhdr->cbLine = 0;
3135 symhdr->idnMax = 0;
3136 symhdr->ipdMax = 0;
3137 symhdr->isymMax = 0;
3138 symhdr->ioptMax = 0;
3139 symhdr->iauxMax = 0;
3140 symhdr->issMax = 0;
3141 symhdr->issExtMax = 0;
3142 symhdr->ifdMax = 0;
3143 symhdr->crfd = 0;
3144 symhdr->iextMax = 0;
3145
3146 /* We accumulate the debugging information itself in the
3147 debug_info structure. */
3148 debug.line = NULL;
3149 debug.external_dnr = NULL;
3150 debug.external_pdr = NULL;
3151 debug.external_sym = NULL;
3152 debug.external_opt = NULL;
3153 debug.external_aux = NULL;
3154 debug.ss = NULL;
3155 debug.ssext = debug.ssext_end = NULL;
3156 debug.external_fdr = NULL;
3157 debug.external_rfd = NULL;
3158 debug.external_ext = debug.external_ext_end = NULL;
3159
3160 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3161 if (mdebug_handle == (PTR) NULL)
3162 return false;
3163
3164 if (SGI_COMPAT (abfd))
3165 {
3166 asection *s;
3167 EXTR esym;
3168 bfd_vma last;
3169 unsigned int i;
3170 static const char * const name[] =
3171 { ".text", ".init", ".fini", ".data",
3172 ".rodata", ".sdata", ".sbss", ".bss" };
3173 static const int sc[] = { scText, scInit, scFini, scData,
3174 scRData, scSData, scSBss, scBss };
3175
3176 esym.jmptbl = 0;
3177 esym.cobol_main = 0;
3178 esym.weakext = 0;
3179 esym.reserved = 0;
3180 esym.ifd = ifdNil;
3181 esym.asym.iss = issNil;
3182 esym.asym.st = stLocal;
3183 esym.asym.reserved = 0;
3184 esym.asym.index = indexNil;
3185 for (i = 0; i < 8; i++)
3186 {
3187 esym.asym.sc = sc[i];
3188 s = bfd_get_section_by_name (abfd, name[i]);
3189 if (s != NULL)
3190 {
3191 esym.asym.value = s->vma;
3192 last = s->vma + s->_raw_size;
3193 }
3194 else
3195 esym.asym.value = last;
3196
3197 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3198 name[i], &esym))
3199 return false;
3200 }
3201 }
3202
3203 for (p = o->link_order_head;
3204 p != (struct bfd_link_order *) NULL;
3205 p = p->next)
3206 {
3207 asection *input_section;
3208 bfd *input_bfd;
3209 const struct ecoff_debug_swap *input_swap;
3210 struct ecoff_debug_info input_debug;
3211 char *eraw_src;
3212 char *eraw_end;
3213
3214 if (p->type != bfd_indirect_link_order)
3215 {
3216 if (p->type == bfd_fill_link_order)
3217 continue;
3218 abort ();
3219 }
3220
3221 input_section = p->u.indirect.section;
3222 input_bfd = input_section->owner;
3223
3224 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
3225 || (get_elf_backend_data (input_bfd)
3226 ->elf_backend_ecoff_debug_swap) == NULL)
3227 {
3228 /* I don't know what a non MIPS ELF bfd would be
3229 doing with a .mdebug section, but I don't really
3230 want to deal with it. */
3231 continue;
3232 }
3233
3234 input_swap = (get_elf_backend_data (input_bfd)
3235 ->elf_backend_ecoff_debug_swap);
3236
3237 BFD_ASSERT (p->size == input_section->_raw_size);
3238
3239 /* The ECOFF linking code expects that we have already
3240 read in the debugging information and set up an
3241 ecoff_debug_info structure, so we do that now. */
3242 if (! mips_elf_read_ecoff_info (input_bfd, input_section,
3243 &input_debug))
3244 return false;
3245
3246 if (! (bfd_ecoff_debug_accumulate
3247 (mdebug_handle, abfd, &debug, swap, input_bfd,
3248 &input_debug, input_swap, info)))
3249 return false;
3250
3251 /* Loop through the external symbols. For each one with
3252 interesting information, try to find the symbol in
3253 the linker global hash table and save the information
3254 for the output external symbols. */
3255 eraw_src = input_debug.external_ext;
3256 eraw_end = (eraw_src
3257 + (input_debug.symbolic_header.iextMax
3258 * input_swap->external_ext_size));
3259 for (;
3260 eraw_src < eraw_end;
3261 eraw_src += input_swap->external_ext_size)
3262 {
3263 EXTR ext;
3264 const char *name;
3265 struct mips_elf_link_hash_entry *h;
3266
3267 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
3268 if (ext.asym.sc == scNil
3269 || ext.asym.sc == scUndefined
3270 || ext.asym.sc == scSUndefined)
3271 continue;
3272
3273 name = input_debug.ssext + ext.asym.iss;
3274 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
3275 name, false, false, true);
3276 if (h == NULL || h->esym.ifd != -2)
3277 continue;
3278
3279 if (ext.ifd != -1)
3280 {
3281 BFD_ASSERT (ext.ifd
3282 < input_debug.symbolic_header.ifdMax);
3283 ext.ifd = input_debug.ifdmap[ext.ifd];
3284 }
3285
3286 h->esym = ext;
3287 }
3288
3289 /* Free up the information we just read. */
3290 free (input_debug.line);
3291 free (input_debug.external_dnr);
3292 free (input_debug.external_pdr);
3293 free (input_debug.external_sym);
3294 free (input_debug.external_opt);
3295 free (input_debug.external_aux);
3296 free (input_debug.ss);
3297 free (input_debug.ssext);
3298 free (input_debug.external_fdr);
3299 free (input_debug.external_rfd);
3300 free (input_debug.external_ext);
3301
3302 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3303 elf_link_input_bfd ignores this section. */
3304 input_section->flags &=~ SEC_HAS_CONTENTS;
3305 }
3306
3307 if (SGI_COMPAT (abfd) && info->shared)
3308 {
3309 /* Create .rtproc section. */
3310 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3311 if (rtproc_sec == NULL)
3312 {
3313 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
3314 | SEC_READONLY);
3315
3316 rtproc_sec = bfd_make_section (abfd, ".rtproc");
3317 if (rtproc_sec == NULL
3318 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
3319 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
3320 return false;
3321 }
3322
3323 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
3324 info, rtproc_sec, &debug))
3325 return false;
3326 }
3327
3328 /* Build the external symbol information. */
3329 einfo.abfd = abfd;
3330 einfo.info = info;
3331 einfo.debug = &debug;
3332 einfo.swap = swap;
3333 einfo.failed = false;
3334 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
3335 mips_elf_output_extsym,
3336 (PTR) &einfo);
3337 if (einfo.failed)
3338 return false;
3339
3340 /* Set the size of the .mdebug section. */
3341 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
3342
3343 /* Skip this section later on (I don't think this currently
3344 matters, but someday it might). */
3345 o->link_order_head = (struct bfd_link_order *) NULL;
3346
3347 mdebug_sec = o;
3348 }
3349
3350 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
3351 {
3352 const char *subname;
3353 unsigned int c;
3354 Elf32_gptab *tab;
3355 Elf32_External_gptab *ext_tab;
3356 unsigned int i;
3357
3358 /* The .gptab.sdata and .gptab.sbss sections hold
3359 information describing how the small data area would
3360 change depending upon the -G switch. These sections
3361 not used in executables files. */
3362 if (! info->relocateable)
3363 {
3364 asection **secpp;
3365
3366 for (p = o->link_order_head;
3367 p != (struct bfd_link_order *) NULL;
3368 p = p->next)
3369 {
3370 asection *input_section;
3371
3372 if (p->type != bfd_indirect_link_order)
3373 {
3374 if (p->type == bfd_fill_link_order)
3375 continue;
3376 abort ();
3377 }
3378
3379 input_section = p->u.indirect.section;
3380
3381 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3382 elf_link_input_bfd ignores this section. */
3383 input_section->flags &=~ SEC_HAS_CONTENTS;
3384 }
3385
3386 /* Skip this section later on (I don't think this
3387 currently matters, but someday it might). */
3388 o->link_order_head = (struct bfd_link_order *) NULL;
3389
3390 /* Really remove the section. */
3391 for (secpp = &abfd->sections;
3392 *secpp != o;
3393 secpp = &(*secpp)->next)
3394 ;
3395 *secpp = (*secpp)->next;
3396 --abfd->section_count;
3397
3398 continue;
3399 }
3400
3401 /* There is one gptab for initialized data, and one for
3402 uninitialized data. */
3403 if (strcmp (o->name, ".gptab.sdata") == 0)
3404 gptab_data_sec = o;
3405 else if (strcmp (o->name, ".gptab.sbss") == 0)
3406 gptab_bss_sec = o;
3407 else
3408 {
3409 (*_bfd_error_handler)
3410 ("%s: illegal section name `%s'",
3411 bfd_get_filename (abfd), o->name);
3412 bfd_set_error (bfd_error_nonrepresentable_section);
3413 return false;
3414 }
3415
3416 /* The linker script always combines .gptab.data and
3417 .gptab.sdata into .gptab.sdata, and likewise for
3418 .gptab.bss and .gptab.sbss. It is possible that there is
3419 no .sdata or .sbss section in the output file, in which
3420 case we must change the name of the output section. */
3421 subname = o->name + sizeof ".gptab" - 1;
3422 if (bfd_get_section_by_name (abfd, subname) == NULL)
3423 {
3424 if (o == gptab_data_sec)
3425 o->name = ".gptab.data";
3426 else
3427 o->name = ".gptab.bss";
3428 subname = o->name + sizeof ".gptab" - 1;
3429 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
3430 }
3431
3432 /* Set up the first entry. */
3433 c = 1;
3434 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
3435 if (tab == NULL)
3436 return false;
3437 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
3438 tab[0].gt_header.gt_unused = 0;
3439
3440 /* Combine the input sections. */
3441 for (p = o->link_order_head;
3442 p != (struct bfd_link_order *) NULL;
3443 p = p->next)
3444 {
3445 asection *input_section;
3446 bfd *input_bfd;
3447 bfd_size_type size;
3448 unsigned long last;
3449 bfd_size_type gpentry;
3450
3451 if (p->type != bfd_indirect_link_order)
3452 {
3453 if (p->type == bfd_fill_link_order)
3454 continue;
3455 abort ();
3456 }
3457
3458 input_section = p->u.indirect.section;
3459 input_bfd = input_section->owner;
3460
3461 /* Combine the gptab entries for this input section one
3462 by one. We know that the input gptab entries are
3463 sorted by ascending -G value. */
3464 size = bfd_section_size (input_bfd, input_section);
3465 last = 0;
3466 for (gpentry = sizeof (Elf32_External_gptab);
3467 gpentry < size;
3468 gpentry += sizeof (Elf32_External_gptab))
3469 {
3470 Elf32_External_gptab ext_gptab;
3471 Elf32_gptab int_gptab;
3472 unsigned long val;
3473 unsigned long add;
3474 boolean exact;
3475 unsigned int look;
3476
3477 if (! (bfd_get_section_contents
3478 (input_bfd, input_section, (PTR) &ext_gptab,
3479 gpentry, sizeof (Elf32_External_gptab))))
3480 {
3481 free (tab);
3482 return false;
3483 }
3484
3485 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
3486 &int_gptab);
3487 val = int_gptab.gt_entry.gt_g_value;
3488 add = int_gptab.gt_entry.gt_bytes - last;
3489
3490 exact = false;
3491 for (look = 1; look < c; look++)
3492 {
3493 if (tab[look].gt_entry.gt_g_value >= val)
3494 tab[look].gt_entry.gt_bytes += add;
3495
3496 if (tab[look].gt_entry.gt_g_value == val)
3497 exact = true;
3498 }
3499
3500 if (! exact)
3501 {
3502 Elf32_gptab *new_tab;
3503 unsigned int max;
3504
3505 /* We need a new table entry. */
3506 new_tab = ((Elf32_gptab *)
3507 bfd_realloc ((PTR) tab,
3508 (c + 1) * sizeof (Elf32_gptab)));
3509 if (new_tab == NULL)
3510 {
3511 free (tab);
3512 return false;
3513 }
3514 tab = new_tab;
3515 tab[c].gt_entry.gt_g_value = val;
3516 tab[c].gt_entry.gt_bytes = add;
3517
3518 /* Merge in the size for the next smallest -G
3519 value, since that will be implied by this new
3520 value. */
3521 max = 0;
3522 for (look = 1; look < c; look++)
3523 {
3524 if (tab[look].gt_entry.gt_g_value < val
3525 && (max == 0
3526 || (tab[look].gt_entry.gt_g_value
3527 > tab[max].gt_entry.gt_g_value)))
3528 max = look;
3529 }
3530 if (max != 0)
3531 tab[c].gt_entry.gt_bytes +=
3532 tab[max].gt_entry.gt_bytes;
3533
3534 ++c;
3535 }
3536
3537 last = int_gptab.gt_entry.gt_bytes;
3538 }
3539
3540 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3541 elf_link_input_bfd ignores this section. */
3542 input_section->flags &=~ SEC_HAS_CONTENTS;
3543 }
3544
3545 /* The table must be sorted by -G value. */
3546 if (c > 2)
3547 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
3548
3549 /* Swap out the table. */
3550 ext_tab = ((Elf32_External_gptab *)
3551 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
3552 if (ext_tab == NULL)
3553 {
3554 free (tab);
3555 return false;
3556 }
3557
3558 for (i = 0; i < c; i++)
3559 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
3560 free (tab);
3561
3562 o->_raw_size = c * sizeof (Elf32_External_gptab);
3563 o->contents = (bfd_byte *) ext_tab;
3564
3565 /* Skip this section later on (I don't think this currently
3566 matters, but someday it might). */
3567 o->link_order_head = (struct bfd_link_order *) NULL;
3568 }
3569 }
3570
3571 /* Invoke the regular ELF backend linker to do all the work. */
3572 if (! bfd_elf32_bfd_final_link (abfd, info))
3573 return false;
3574
3575 /* Now write out the computed sections. */
3576
3577 if (reginfo_sec != (asection *) NULL)
3578 {
3579 Elf32_External_RegInfo ext;
3580
3581 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
3582 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
3583 (file_ptr) 0, sizeof ext))
3584 return false;
3585 }
3586
3587 if (mdebug_sec != (asection *) NULL)
3588 {
3589 BFD_ASSERT (abfd->output_has_begun);
3590 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
3591 swap, info,
3592 mdebug_sec->filepos))
3593 return false;
3594
3595 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
3596 }
3597
3598 if (gptab_data_sec != (asection *) NULL)
3599 {
3600 if (! bfd_set_section_contents (abfd, gptab_data_sec,
3601 gptab_data_sec->contents,
3602 (file_ptr) 0,
3603 gptab_data_sec->_raw_size))
3604 return false;
3605 }
3606
3607 if (gptab_bss_sec != (asection *) NULL)
3608 {
3609 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
3610 gptab_bss_sec->contents,
3611 (file_ptr) 0,
3612 gptab_bss_sec->_raw_size))
3613 return false;
3614 }
3615
3616 if (SGI_COMPAT (abfd))
3617 {
3618 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3619 if (rtproc_sec != NULL)
3620 {
3621 if (! bfd_set_section_contents (abfd, rtproc_sec,
3622 rtproc_sec->contents,
3623 (file_ptr) 0,
3624 rtproc_sec->_raw_size))
3625 return false;
3626 }
3627 }
3628
3629 return true;
3630 }
3631
3632 /* Handle a MIPS ELF HI16 reloc. */
3633
3634 static void
3635 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
3636 bfd *input_bfd;
3637 Elf_Internal_Rela *relhi;
3638 Elf_Internal_Rela *rello;
3639 bfd_byte *contents;
3640 bfd_vma addend;
3641 {
3642 bfd_vma insn;
3643 bfd_vma addlo;
3644
3645 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
3646
3647 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
3648 addlo &= 0xffff;
3649
3650 addend += ((insn & 0xffff) << 16) + addlo;
3651
3652 if ((addlo & 0x8000) != 0)
3653 addend -= 0x10000;
3654 if ((addend & 0x8000) != 0)
3655 addend += 0x10000;
3656
3657 bfd_put_32 (input_bfd,
3658 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
3659 contents + relhi->r_offset);
3660 }
3661
3662 /* Handle a MIPS ELF local GOT16 reloc. */
3663
3664 static void
3665 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
3666 contents, addend)
3667 bfd *output_bfd;
3668 bfd *input_bfd;
3669 asection *sgot;
3670 Elf_Internal_Rela *relhi;
3671 Elf_Internal_Rela *rello;
3672 bfd_byte *contents;
3673 bfd_vma addend;
3674 {
3675 int local_gotno;
3676 int i;
3677 bfd_vma insn;
3678 bfd_vma addlo;
3679 bfd_vma address;
3680 bfd_vma hipage;
3681 bfd_byte *got_contents;
3682 struct mips_got_info *g;
3683
3684 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
3685
3686 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
3687 addlo &= 0xffff;
3688
3689 addend += ((insn & 0xffff) << 16) + addlo;
3690
3691 if ((addlo & 0x8000) != 0)
3692 addend -= 0x10000;
3693 if ((addend & 0x8000) != 0)
3694 addend += 0x10000;
3695
3696 /* Get a got entry representing requested hipage. */
3697 BFD_ASSERT (elf_section_data (sgot) != NULL);
3698 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
3699 BFD_ASSERT (g != NULL);
3700
3701 local_gotno = g->local_gotno;
3702 got_contents = sgot->contents;
3703 hipage = addend & 0xffff0000;
3704
3705 for (i = MIPS_RESERVED_GOTNO; i < local_gotno; i++)
3706 {
3707 address = bfd_get_32 (input_bfd, got_contents + i * 4);
3708 if (hipage == (address & 0xffff0000))
3709 break;
3710 if (address == (bfd_vma) 0)
3711 {
3712 bfd_put_32 (input_bfd, hipage, got_contents + i * 4);
3713 break;
3714 }
3715 }
3716
3717 BFD_ASSERT (i < local_gotno);
3718 #if 1
3719 if (i == local_gotno)
3720 (*_bfd_error_handler)
3721 ("ELF MIPS linker: more got entries are needed for hipage: %x",
3722 hipage);
3723 #endif
3724
3725 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
3726 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
3727 contents + relhi->r_offset);
3728 }
3729
3730 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
3731
3732 static void
3733 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
3734 bfd *input_bfd;
3735 Elf_Internal_Rela *rel;
3736 bfd_byte *contents;
3737 bfd_vma offset;
3738 {
3739 bfd_vma insn;
3740
3741 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3742 bfd_put_32 (input_bfd,
3743 (insn & 0xffff0000) | (offset & 0xffff),
3744 contents + rel->r_offset);
3745 }
3746
3747 /* Relocate a MIPS ELF section. */
3748
3749 static boolean
3750 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
3751 contents, relocs, local_syms, local_sections)
3752 bfd *output_bfd;
3753 struct bfd_link_info *info;
3754 bfd *input_bfd;
3755 asection *input_section;
3756 bfd_byte *contents;
3757 Elf_Internal_Rela *relocs;
3758 Elf_Internal_Sym *local_syms;
3759 asection **local_sections;
3760 {
3761 Elf_Internal_Shdr *symtab_hdr;
3762 size_t locsymcount;
3763 size_t extsymoff;
3764 asection *sgot, *sreloc, *scpt;
3765 bfd *dynobj;
3766 Elf_Internal_Rela *rel;
3767 Elf_Internal_Rela *relend;
3768 struct mips_got_info *g;
3769
3770 dynobj = elf_hash_table (info)->dynobj;
3771 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3772
3773 sgot = NULL;
3774 sreloc = NULL;
3775 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
3776 scpt = NULL;
3777 else
3778 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
3779 g = NULL;
3780
3781 if (elf_bad_symtab (input_bfd))
3782 {
3783 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
3784 extsymoff = 0;
3785 }
3786 else
3787 {
3788 locsymcount = symtab_hdr->sh_info;
3789 extsymoff = symtab_hdr->sh_info;
3790 }
3791
3792 rel = relocs;
3793 relend = relocs + input_section->reloc_count;
3794 for (; rel < relend; rel++)
3795 {
3796 int r_type;
3797 reloc_howto_type *howto;
3798 unsigned long r_symndx;
3799 bfd_vma addend;
3800 struct elf_link_hash_entry *h;
3801 asection *sec;
3802 Elf_Internal_Sym *sym;
3803 bfd_reloc_status_type r;
3804
3805 r_type = ELF32_R_TYPE (rel->r_info);
3806 if (r_type < 0 || r_type >= (int) R_MIPS_max)
3807 {
3808 bfd_set_error (bfd_error_bad_value);
3809 return false;
3810 }
3811 howto = elf_mips_howto_table + r_type;
3812
3813 if (dynobj != NULL
3814 && (r_type == R_MIPS_CALL16
3815 || r_type == R_MIPS_GOT16
3816 || r_type == R_MIPS_CALL_HI16
3817 || r_type == R_MIPS_CALL_LO16
3818 || r_type == R_MIPS_GOT_HI16
3819 || r_type == R_MIPS_GOT_LO16))
3820 {
3821 /* We need the .got section. */
3822 if (sgot == NULL)
3823 {
3824 sgot = bfd_get_section_by_name (dynobj, ".got");
3825 BFD_ASSERT (sgot != NULL);
3826 BFD_ASSERT (elf_section_data (sgot) != NULL);
3827 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
3828 BFD_ASSERT (g != NULL);
3829 }
3830 }
3831
3832 r_symndx = ELF32_R_SYM (rel->r_info);
3833
3834 /* Mix in the change in GP address for a GP relative reloc. */
3835 if (r_type != R_MIPS_GPREL16
3836 && r_type != R_MIPS_LITERAL
3837 && r_type != R_MIPS_GPREL32)
3838 addend = 0;
3839 else
3840 {
3841 if (elf_gp (output_bfd) == 0)
3842 {
3843 if (! ((*info->callbacks->reloc_dangerous)
3844 (info,
3845 "GP relative relocation when GP not defined",
3846 input_bfd, input_section,
3847 rel->r_offset)))
3848 return false;
3849 /* Only give the error once per link. */
3850 elf_gp (output_bfd) = 4;
3851 }
3852
3853 if (r_symndx < extsymoff
3854 || (elf_bad_symtab (input_bfd)
3855 && local_sections[r_symndx] != NULL))
3856 {
3857 /* This is a relocation against a section. The current
3858 addend in the instruction is the difference between
3859 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
3860 must change this to be the difference between the
3861 final definition (which will end up in RELOCATION)
3862 and the GP value of OUTPUT_BFD (which is in GP). */
3863 addend = elf_gp (input_bfd) - elf_gp (output_bfd);
3864 }
3865 else if (! info->relocateable)
3866 {
3867 /* We are doing a final link. The current addend in the
3868 instruction is simply the desired offset into the
3869 symbol (normally zero). We want the instruction to
3870 hold the difference between the final definition of
3871 the symbol (which will end up in RELOCATION) and the
3872 GP value of OUTPUT_BFD (which is in GP). */
3873 addend = - elf_gp (output_bfd);
3874 }
3875 else
3876 {
3877 /* We are generating relocateable output, and we aren't
3878 going to define this symbol, so we just leave the
3879 instruction alone. */
3880 addend = 0;
3881 }
3882 }
3883
3884 h = NULL;
3885 sym = NULL;
3886 sec = NULL;
3887 if (info->relocateable)
3888 {
3889 /* This is a relocateable link. We don't have to change
3890 anything, unless the reloc is against a section symbol,
3891 in which case we have to adjust according to where the
3892 section symbol winds up in the output section. */
3893 if (r_symndx >= locsymcount
3894 || (elf_bad_symtab (input_bfd)
3895 && local_sections[r_symndx] == NULL))
3896 r = bfd_reloc_ok;
3897 else
3898 {
3899 sym = local_syms + r_symndx;
3900 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3901 r = bfd_reloc_ok;
3902 else
3903 {
3904 sec = local_sections[r_symndx];
3905
3906 /* It would be logical to add sym->st_value here,
3907 but Irix 5 sometimes generates a garbage symbol
3908 value. */
3909 addend += sec->output_offset;
3910
3911 /* If this is HI16 or GOT16 with an associated LO16,
3912 adjust the addend accordingly. Otherwise, just
3913 relocate. */
3914 if ((r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
3915 || (rel + 1) >= relend
3916 || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
3917 r = _bfd_relocate_contents (howto, input_bfd,
3918 addend,
3919 contents + rel->r_offset);
3920 else
3921 {
3922 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
3923 contents, addend);
3924 r = bfd_reloc_ok;
3925 }
3926 }
3927 }
3928 }
3929 else
3930 {
3931 bfd_vma relocation;
3932 boolean local;
3933
3934 /* This is a final link. */
3935 sym = NULL;
3936 if (r_symndx < extsymoff
3937 || (elf_bad_symtab (input_bfd)
3938 && local_sections[r_symndx] != NULL))
3939 {
3940 local = true;
3941 sym = local_syms + r_symndx;
3942 sec = local_sections[r_symndx];
3943 relocation = (sec->output_section->vma
3944 + sec->output_offset);
3945
3946 /* It would be logical to always add sym->st_value here,
3947 but Irix 5 sometimes generates a garbage symbol
3948 value. */
3949 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3950 relocation += sym->st_value;
3951 }
3952 else
3953 {
3954 long indx;
3955
3956 local = false;
3957 indx = r_symndx - extsymoff;
3958 h = elf_sym_hashes (input_bfd)[indx];
3959 if (strcmp (h->root.root.string, "_gp_disp") == 0)
3960 {
3961 if (elf_gp (output_bfd) == 0)
3962 {
3963 if (! ((*info->callbacks->reloc_dangerous)
3964 (info,
3965 "_gp_disp used when GP not defined",
3966 input_bfd, input_section,
3967 rel->r_offset)))
3968 return false;
3969 /* Only give the error once per link. */
3970 elf_gp (output_bfd) = 4;
3971 relocation = 0;
3972 }
3973 else
3974 {
3975 sec = input_section;
3976 if (sec->output_section != NULL)
3977 relocation = (elf_gp (output_bfd)
3978 - (rel->r_offset
3979 + sec->output_section->vma
3980 + sec->output_offset));
3981 else
3982 relocation = elf_gp (output_bfd) - rel->r_offset;
3983 if (r_type == R_MIPS_LO16)
3984 relocation += 4;
3985 }
3986 }
3987 else if (h->root.type == bfd_link_hash_defined
3988 || h->root.type == bfd_link_hash_defweak)
3989 {
3990 sec = h->root.u.def.section;
3991 if (sec->output_section == NULL)
3992 relocation = 0;
3993 else
3994 relocation = (h->root.u.def.value
3995 + sec->output_section->vma
3996 + sec->output_offset);
3997 }
3998 else if (h->root.type == bfd_link_hash_undefweak)
3999 relocation = 0;
4000 else if (info->shared && ! info->symbolic)
4001 relocation = 0;
4002 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
4003 {
4004 /* If this is a dynamic link, we should have created
4005 a _DYNAMIC_LINK symbol in
4006 mips_elf_create_dynamic_sections. Otherwise, we
4007 should define the symbol with a value of 0.
4008 FIXME: It should probably get into the symbol
4009 table somehow as well. */
4010 BFD_ASSERT (! info->shared);
4011 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
4012 ".dynamic") == NULL);
4013 relocation = 0;
4014 }
4015 else
4016 {
4017 if (! ((*info->callbacks->undefined_symbol)
4018 (info, h->root.root.string, input_bfd,
4019 input_section, rel->r_offset)))
4020 return false;
4021 relocation = 0;
4022 }
4023 }
4024
4025 if (r_type == R_MIPS_HI16
4026 && (rel + 1) < relend
4027 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
4028 {
4029 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
4030 contents, relocation + addend);
4031 r = bfd_reloc_ok;
4032 }
4033 else if (r_type == R_MIPS_GOT16 && local)
4034 {
4035 /* GOT16 must be also with associated LO16 in the local
4036 case. In this case, the addend is extracted and the
4037 section in which the referenced object is determined.
4038 Then the final address of the object is computed and
4039 the GOT entry for the hipage (an aligned 64kb chunk)
4040 is added to .got section if needed. The offset field
4041 of the GOT16-relocated instruction is replaced by the
4042 index of this GOT entry for the hipage. */
4043 if ((rel + 1) < relend
4044 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
4045 {
4046 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot,
4047 rel, rel + 1,
4048 contents,
4049 relocation + addend);
4050 r = bfd_reloc_ok;
4051 }
4052 else
4053 r = bfd_reloc_outofrange;
4054 }
4055 else if (r_type == R_MIPS_CALL16
4056 || r_type == R_MIPS_GOT16
4057 || r_type == R_MIPS_CALL_LO16
4058 || r_type == R_MIPS_GOT_LO16)
4059 {
4060 bfd_vma offset;
4061
4062 /* This symbol must be registered as a global symbol
4063 having the corresponding got entry. */
4064 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
4065
4066 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
4067 BFD_ASSERT (g->local_gotno <= offset
4068 && offset < sgot->_raw_size);
4069 bfd_put_32 (output_bfd, relocation + addend,
4070 sgot->contents + offset);
4071 offset = (sgot->output_section->vma + sgot->output_offset
4072 + offset - elf_gp (output_bfd));
4073 mips_elf_relocate_global_got (input_bfd, rel, contents,
4074 offset);
4075 r = bfd_reloc_ok;
4076 }
4077 else if (r_type == R_MIPS_CALL_HI16
4078 || r_type == R_MIPS_GOT_HI16)
4079 {
4080 bfd_vma offset;
4081
4082 /* This must be a global symbol with a got entry. The
4083 next reloc must be the corresponding LO16 reloc. */
4084 BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
4085 BFD_ASSERT ((rel + 1) < relend);
4086 BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
4087 == (r_type == R_MIPS_CALL_HI16
4088 ? R_MIPS_CALL_LO16
4089 : R_MIPS_GOT_LO16));
4090
4091 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
4092 BFD_ASSERT (g->local_gotno <= offset
4093 && offset < sgot->_raw_size);
4094 bfd_put_32 (output_bfd, relocation + addend,
4095 sgot->contents + offset);
4096 offset = (sgot->output_section->vma + sgot->output_offset
4097 + offset - elf_gp (output_bfd));
4098 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
4099 offset);
4100 r = bfd_reloc_ok;
4101 }
4102 else if (r_type == R_MIPS_REL32
4103 || r_type == R_MIPS_32)
4104 {
4105 Elf_Internal_Rel outrel;
4106 Elf32_crinfo cptrel;
4107 bfd_byte *cr;
4108
4109 if (info->shared
4110 && (input_section->flags & SEC_ALLOC) != 0)
4111 {
4112 /* When generating a shared object, these
4113 relocations are copied into the output file to be
4114 resolved at run time. */
4115 if (sreloc == NULL)
4116 {
4117 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
4118 BFD_ASSERT (sreloc != NULL);
4119 }
4120
4121 outrel.r_offset = (rel->r_offset
4122 + input_section->output_section->vma
4123 + input_section->output_offset);
4124
4125 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4126
4127 if (h != NULL
4128 && (! info->symbolic
4129 || (h->elf_link_hash_flags
4130 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4131 {
4132 BFD_ASSERT (h->dynindx != -1);
4133 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
4134 sec = input_section;
4135 }
4136 else
4137 {
4138 long indx;
4139
4140 if (h == NULL)
4141 sec = local_sections[r_symndx];
4142 else
4143 {
4144 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4145 || (h->root.type
4146 == bfd_link_hash_defweak));
4147 sec = h->root.u.def.section;
4148 }
4149 if (sec != NULL && bfd_is_abs_section (sec))
4150 indx = 0;
4151 else if (sec == NULL || sec->owner == NULL)
4152 {
4153 bfd_set_error (bfd_error_bad_value);
4154 return false;
4155 }
4156 else
4157 {
4158 asection *osec;
4159
4160 osec = sec->output_section;
4161 indx = elf_section_data (osec)->dynindx;
4162 if (indx == 0)
4163 abort ();
4164 }
4165
4166 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
4167 addend += relocation;
4168 }
4169
4170 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
4171 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
4172 (((Elf32_External_Rel *)
4173 sreloc->contents)
4174 + sreloc->reloc_count));
4175 ++sreloc->reloc_count;
4176
4177 if (SGI_COMPAT (output_bfd))
4178 {
4179 if (scpt == NULL)
4180 continue;
4181
4182 /* Make an entry of compact relocation info. */
4183 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4184 cptrel.vaddr = (rel->r_offset
4185 + input_section->output_section->vma
4186 + input_section->output_offset);
4187 if (r_type == R_MIPS_REL32)
4188 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4189 else
4190 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4191 mips_elf_set_cr_dist2to (cptrel, 0);
4192 cptrel.konst = addend;
4193
4194 cr = (scpt->contents
4195 + sizeof (Elf32_External_compact_rel));
4196 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4197 ((Elf32_External_crinfo *) cr
4198 + scpt->reloc_count));
4199 ++scpt->reloc_count;
4200 }
4201
4202 /* This reloc will be computed at runtime, so
4203 there's no need to do anything now. */
4204 continue;
4205 }
4206 else
4207 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4208 contents, rel->r_offset,
4209 relocation, addend);
4210 }
4211 else
4212 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4213 contents, rel->r_offset,
4214 relocation, addend);
4215
4216 if (SGI_COMPAT (abfd)
4217 && scpt != NULL
4218 && (input_section->flags & SEC_ALLOC) != 0)
4219 {
4220 Elf32_crinfo cptrel;
4221 bfd_byte *cr;
4222
4223 /* Make an entry of compact relocation info. */
4224 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4225 cptrel.vaddr = (rel->r_offset
4226 + input_section->output_section->vma
4227 + input_section->output_offset);
4228
4229 switch (r_type)
4230 {
4231 case R_MIPS_26:
4232 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
4233 /* XXX How should we set dist2to in this case. */
4234 mips_elf_set_cr_dist2to (cptrel, 8);
4235 cptrel.konst = addend + relocation;
4236 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
4237 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4238 ((Elf32_External_crinfo *) cr
4239 + scpt->reloc_count));
4240 ++scpt->reloc_count;
4241 break;
4242
4243 case R_MIPS_GPREL16:
4244 case R_MIPS_LITERAL:
4245 case R_MIPS_GPREL32:
4246 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
4247 cptrel.konst = elf_gp (output_bfd) - cptrel.vaddr;
4248 mips_elf_set_cr_dist2to (cptrel, 4);
4249 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
4250 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4251 ((Elf32_External_crinfo *) cr
4252 + scpt->reloc_count));
4253 ++scpt->reloc_count;
4254 break;
4255
4256 default:
4257 break;
4258 }
4259 }
4260 }
4261
4262 if (r != bfd_reloc_ok)
4263 {
4264 switch (r)
4265 {
4266 default:
4267 case bfd_reloc_outofrange:
4268 abort ();
4269 case bfd_reloc_overflow:
4270 {
4271 const char *name;
4272
4273 if (h != NULL)
4274 name = h->root.root.string;
4275 else
4276 {
4277 name = bfd_elf_string_from_elf_section (input_bfd,
4278 symtab_hdr->sh_link,
4279 sym->st_name);
4280 if (name == NULL)
4281 return false;
4282 if (*name == '\0')
4283 name = bfd_section_name (input_bfd, sec);
4284 }
4285 if (! ((*info->callbacks->reloc_overflow)
4286 (info, name, howto->name, (bfd_vma) 0,
4287 input_bfd, input_section, rel->r_offset)))
4288 return false;
4289 }
4290 break;
4291 }
4292 }
4293 }
4294
4295 return true;
4296 }
4297 \f
4298 /* Functions for the dynamic linker. */
4299
4300 /* The name of the dynamic interpreter. This is put in the .interp
4301 section. */
4302
4303 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
4304
4305 /* Create dynamic sections when linking against a dynamic object. */
4306
4307 static boolean
4308 mips_elf_create_dynamic_sections (abfd, info)
4309 bfd *abfd;
4310 struct bfd_link_info *info;
4311 {
4312 struct elf_link_hash_entry *h;
4313 flagword flags;
4314 register asection *s;
4315 const char * const *namep;
4316
4317 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4318 | SEC_READONLY);
4319
4320 /* Mips ABI requests the .dynamic section to be read only. */
4321 s = bfd_get_section_by_name (abfd, ".dynamic");
4322 if (s != NULL)
4323 {
4324 if (! bfd_set_section_flags (abfd, s, flags))
4325 return false;
4326 }
4327
4328 /* We need to create .got section. */
4329 if (! mips_elf_create_got_section (abfd, info))
4330 return false;
4331
4332 /* Create .stub section. */
4333 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
4334 {
4335 s = bfd_make_section (abfd, ".stub");
4336 if (s == NULL
4337 || ! bfd_set_section_flags (abfd, s, flags)
4338 || ! bfd_set_section_alignment (abfd, s, 2))
4339 return false;
4340 }
4341
4342 if (SGI_COMPAT (abfd))
4343 {
4344 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
4345 {
4346 h = NULL;
4347 if (! (_bfd_generic_link_add_one_symbol
4348 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
4349 (bfd_vma) 0, (const char *) NULL, false,
4350 get_elf_backend_data (abfd)->collect,
4351 (struct bfd_link_hash_entry **) &h)))
4352 return false;
4353 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4354 h->type = STT_SECTION;
4355
4356 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4357 return false;
4358 }
4359
4360 /* We need to create a .compact_rel section. */
4361 if (! mips_elf_create_compact_rel_section (abfd, info))
4362 return false;
4363
4364 /* Change aligments of some sections. */
4365 s = bfd_get_section_by_name (abfd, ".hash");
4366 if (s != NULL)
4367 bfd_set_section_alignment (abfd, s, 4);
4368 s = bfd_get_section_by_name (abfd, ".dynsym");
4369 if (s != NULL)
4370 bfd_set_section_alignment (abfd, s, 4);
4371 s = bfd_get_section_by_name (abfd, ".dynstr");
4372 if (s != NULL)
4373 bfd_set_section_alignment (abfd, s, 4);
4374 s = bfd_get_section_by_name (abfd, ".reginfo");
4375 if (s != NULL)
4376 bfd_set_section_alignment (abfd, s, 4);
4377 s = bfd_get_section_by_name (abfd, ".dynamic");
4378 if (s != NULL)
4379 bfd_set_section_alignment (abfd, s, 4);
4380 }
4381
4382 if (!info->shared)
4383 {
4384 h = NULL;
4385 if (! (_bfd_generic_link_add_one_symbol
4386 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
4387 (bfd_vma) 0, (const char *) NULL, false,
4388 get_elf_backend_data (abfd)->collect,
4389 (struct bfd_link_hash_entry **) &h)))
4390 return false;
4391 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4392 h->type = STT_SECTION;
4393
4394 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4395 return false;
4396 }
4397
4398 return true;
4399 }
4400
4401 /* Create the .compact_rel section. */
4402
4403 static boolean
4404 mips_elf_create_compact_rel_section (abfd, info)
4405 bfd *abfd;
4406 struct bfd_link_info *info;
4407 {
4408 flagword flags;
4409 register asection *s;
4410
4411 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4412 {
4413 flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY;
4414
4415 s = bfd_make_section (abfd, ".compact_rel");
4416 if (s == NULL
4417 || ! bfd_set_section_flags (abfd, s, flags)
4418 || ! bfd_set_section_alignment (abfd, s, 2))
4419 return false;
4420
4421 s->_raw_size = sizeof (Elf32_External_compact_rel);
4422 }
4423
4424 return true;
4425 }
4426
4427 /* Create the .got section to hold the global offset table. */
4428
4429 static boolean
4430 mips_elf_create_got_section (abfd, info)
4431 bfd *abfd;
4432 struct bfd_link_info *info;
4433 {
4434 flagword flags;
4435 register asection *s;
4436 struct elf_link_hash_entry *h;
4437 struct mips_got_info *g;
4438
4439 /* This function may be called more than once. */
4440 if (bfd_get_section_by_name (abfd, ".got") != NULL)
4441 return true;
4442
4443 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
4444
4445 s = bfd_make_section (abfd, ".got");
4446 if (s == NULL
4447 || ! bfd_set_section_flags (abfd, s, flags)
4448 || ! bfd_set_section_alignment (abfd, s, 4))
4449 return false;
4450
4451 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4452 linker script because we don't want to define the symbol if we
4453 are not creating a global offset table. */
4454 h = NULL;
4455 if (! (_bfd_generic_link_add_one_symbol
4456 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4457 (bfd_vma) 0, (const char *) NULL, false,
4458 get_elf_backend_data (abfd)->collect,
4459 (struct bfd_link_hash_entry **) &h)))
4460 return false;
4461 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4462 h->type = STT_OBJECT;
4463
4464 if (info->shared
4465 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
4466 return false;
4467
4468 /* The first several global offset table entries are reserved. */
4469 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
4470
4471 g = (struct mips_got_info *) bfd_alloc (abfd,
4472 sizeof (struct mips_got_info));
4473 if (g == NULL)
4474 return false;
4475 g->global_gotsym = 0;
4476 g->local_gotno = MIPS_RESERVED_GOTNO;
4477 if (elf_section_data (s) == NULL)
4478 {
4479 s->used_by_bfd =
4480 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
4481 if (elf_section_data (s) == NULL)
4482 return false;
4483 }
4484 elf_section_data (s)->tdata = (PTR) g;
4485
4486 return true;
4487 }
4488
4489 /* Look through the relocs for a section during the first phase, and
4490 allocate space in the global offset table. */
4491
4492 static boolean
4493 mips_elf_check_relocs (abfd, info, sec, relocs)
4494 bfd *abfd;
4495 struct bfd_link_info *info;
4496 asection *sec;
4497 const Elf_Internal_Rela *relocs;
4498 {
4499 bfd *dynobj;
4500 Elf_Internal_Shdr *symtab_hdr;
4501 struct elf_link_hash_entry **sym_hashes;
4502 struct mips_got_info *g;
4503 size_t extsymoff;
4504 const Elf_Internal_Rela *rel;
4505 const Elf_Internal_Rela *rel_end;
4506 asection *sgot;
4507 asection *sreloc;
4508
4509 if (info->relocateable)
4510 return true;
4511
4512 dynobj = elf_hash_table (info)->dynobj;
4513 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4514 sym_hashes = elf_sym_hashes (abfd);
4515 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
4516
4517 sgot = NULL;
4518 sreloc = NULL;
4519
4520 rel_end = relocs + sec->reloc_count;
4521 for (rel = relocs; rel < rel_end; rel++)
4522 {
4523 unsigned long r_symndx;
4524 struct elf_link_hash_entry *h;
4525
4526 r_symndx = ELF32_R_SYM (rel->r_info);
4527
4528 if (r_symndx < extsymoff)
4529 h = NULL;
4530 else
4531 h = sym_hashes[r_symndx - extsymoff];
4532
4533 /* Some relocs require a global offset table. */
4534 if (dynobj == NULL)
4535 {
4536 switch (ELF32_R_TYPE (rel->r_info))
4537 {
4538 case R_MIPS_GOT16:
4539 case R_MIPS_CALL16:
4540 case R_MIPS_CALL_HI16:
4541 case R_MIPS_CALL_LO16:
4542 case R_MIPS_GOT_HI16:
4543 case R_MIPS_GOT_LO16:
4544 elf_hash_table (info)->dynobj = dynobj = abfd;
4545 if (! mips_elf_create_got_section (dynobj, info))
4546 return false;
4547 break;
4548
4549 default:
4550 break;
4551 }
4552 }
4553
4554 switch (ELF32_R_TYPE (rel->r_info))
4555 {
4556 case R_MIPS_CALL16:
4557 case R_MIPS_CALL_HI16:
4558 case R_MIPS_CALL_LO16:
4559 /* This symbol requires a global offset table entry. */
4560 if (sgot == NULL)
4561 {
4562 sgot = bfd_get_section_by_name (dynobj, ".got");
4563 BFD_ASSERT (sgot != NULL);
4564 BFD_ASSERT (elf_section_data (sgot) != NULL);
4565 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4566 BFD_ASSERT (g != NULL);
4567 }
4568
4569 BFD_ASSERT (h != NULL);
4570
4571 /* Make sure this symbol is output as a dynamic symbol. */
4572 if (h->dynindx == -1)
4573 {
4574 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4575 return false;
4576 }
4577
4578 if (h->got_offset != (bfd_vma) -1)
4579 {
4580 /* We have already allocated space in the .got. */
4581 break;
4582 }
4583
4584 /* Note the index of the first global got symbol in .dynsym. */
4585 if (g->global_gotsym == 0
4586 || g->global_gotsym > (unsigned long) h->dynindx)
4587 g->global_gotsym = h->dynindx;
4588
4589 /* Make this symbol to have the corresponding got entry. */
4590 h->got_offset = 0;
4591
4592 /* We need a stub, not a plt entry for the undefined
4593 function. But we record it as if it needs plt. See
4594 elf_adjust_dynamic_symbol in elflink.h. */
4595 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4596 h->type = STT_FUNC;
4597
4598 break;
4599
4600 case R_MIPS_GOT16:
4601 case R_MIPS_GOT_HI16:
4602 case R_MIPS_GOT_LO16:
4603 /* This symbol requires a global offset table entry. */
4604 if (sgot == NULL)
4605 {
4606 sgot = bfd_get_section_by_name (dynobj, ".got");
4607 BFD_ASSERT (sgot != NULL);
4608 BFD_ASSERT (elf_section_data (sgot) != NULL);
4609 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4610 BFD_ASSERT (g != NULL);
4611 }
4612
4613 if (h != NULL)
4614 {
4615 /* Make sure this symbol is output as a dynamic symbol. */
4616 if (h->dynindx == -1)
4617 {
4618 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4619 return false;
4620 }
4621
4622 if (h->got_offset != (bfd_vma) -1)
4623 {
4624 /* We have already allocated space in the .got. */
4625 break;
4626 }
4627 /* Note the index of the first global got symbol in
4628 .dynsym. */
4629 if (g->global_gotsym == 0
4630 || g->global_gotsym > (unsigned long) h->dynindx)
4631 g->global_gotsym = h->dynindx;
4632
4633 /* Make this symbol to be the global got symbol. */
4634 h->got_offset = 0;
4635 }
4636
4637 break;
4638
4639 case R_MIPS_32:
4640 case R_MIPS_REL32:
4641 if (info->shared
4642 && (sec->flags & SEC_ALLOC) != 0)
4643 {
4644 /* When creating a shared object, we must copy these
4645 reloc types into the output file as R_MIPS_REL32
4646 relocs. We create the .rel.dyn reloc section in
4647 dynobj and make room for this reloc. */
4648 if (sreloc == NULL)
4649 {
4650 const char *name = ".rel.dyn";
4651
4652 sreloc = bfd_get_section_by_name (dynobj, name);
4653 if (sreloc == NULL)
4654 {
4655 sreloc = bfd_make_section (dynobj, name);
4656 if (sreloc == NULL
4657 || ! bfd_set_section_flags (dynobj, sreloc,
4658 (SEC_ALLOC
4659 | SEC_LOAD
4660 | SEC_HAS_CONTENTS
4661 | SEC_IN_MEMORY
4662 | SEC_READONLY))
4663 || ! bfd_set_section_alignment (dynobj, sreloc, 4))
4664 return false;
4665
4666 /* Add a null element. */
4667 sreloc->_raw_size += sizeof (Elf32_External_Rel);
4668 ++sreloc->reloc_count;
4669 }
4670 }
4671
4672 sreloc->_raw_size += sizeof (Elf32_External_Rel);
4673 }
4674
4675 if (SGI_COMPAT (abfd))
4676 mips_elf_hash_table (info)->compact_rel_size +=
4677 sizeof (Elf32_External_crinfo);
4678
4679 break;
4680
4681 case R_MIPS_26:
4682 case R_MIPS_GPREL16:
4683 case R_MIPS_LITERAL:
4684 case R_MIPS_GPREL32:
4685 if (SGI_COMPAT (abfd))
4686 mips_elf_hash_table (info)->compact_rel_size +=
4687 sizeof (Elf32_External_crinfo);
4688 break;
4689
4690 default:
4691 break;
4692 }
4693 }
4694
4695 return true;
4696 }
4697
4698 /* Adjust a symbol defined by a dynamic object and referenced by a
4699 regular object. The current definition is in some section of the
4700 dynamic object, but we're not including those sections. We have to
4701 change the definition to something the rest of the link can
4702 understand. */
4703
4704 static boolean
4705 mips_elf_adjust_dynamic_symbol (info, h)
4706 struct bfd_link_info *info;
4707 struct elf_link_hash_entry *h;
4708 {
4709 bfd *dynobj;
4710 asection *s;
4711
4712 dynobj = elf_hash_table (info)->dynobj;
4713
4714 /* Make sure we know what is going on here. */
4715 BFD_ASSERT (dynobj != NULL
4716 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
4717 || h->weakdef != NULL
4718 || ((h->elf_link_hash_flags
4719 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4720 && (h->elf_link_hash_flags
4721 & ELF_LINK_HASH_REF_REGULAR) != 0
4722 && (h->elf_link_hash_flags
4723 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
4724
4725 /* For a function, create a stub, if needed. */
4726 if (h->type == STT_FUNC
4727 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4728 {
4729 if (! elf_hash_table (info)->dynamic_sections_created)
4730 return true;
4731
4732 /* If this symbol is not defined in a regular file, then set
4733 the symbol to the stub location. This is required to make
4734 function pointers compare as equal between the normal
4735 executable and the shared library. */
4736 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4737 {
4738 /* We need .stub section. */
4739 s = bfd_get_section_by_name (dynobj, ".stub");
4740 BFD_ASSERT (s != NULL);
4741
4742 h->root.u.def.section = s;
4743 h->root.u.def.value = s->_raw_size;
4744
4745 /* XXX Write this stub address somewhere. */
4746 h->plt_offset = s->_raw_size;
4747
4748 /* Make room for this stub code. */
4749 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4750
4751 /* The last half word of the stub will be filled with the index
4752 of this symbol in .dynsym section. */
4753 return true;
4754 }
4755 }
4756
4757 /* If this is a weak symbol, and there is a real definition, the
4758 processor independent code will have arranged for us to see the
4759 real definition first, and we can just use the same value. */
4760 if (h->weakdef != NULL)
4761 {
4762 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4763 || h->weakdef->root.type == bfd_link_hash_defweak);
4764 h->root.u.def.section = h->weakdef->root.u.def.section;
4765 h->root.u.def.value = h->weakdef->root.u.def.value;
4766 return true;
4767 }
4768
4769 /* This is a reference to a symbol defined by a dynamic object which
4770 is not a function. */
4771
4772 return true;
4773 }
4774
4775 /* Set the sizes of the dynamic sections. */
4776
4777 static boolean
4778 mips_elf_size_dynamic_sections (output_bfd, info)
4779 bfd *output_bfd;
4780 struct bfd_link_info *info;
4781 {
4782 bfd *dynobj;
4783 asection *s;
4784 boolean reltext;
4785 asection *sgot;
4786 struct mips_got_info *g;
4787
4788 dynobj = elf_hash_table (info)->dynobj;
4789 BFD_ASSERT (dynobj != NULL);
4790
4791 if (elf_hash_table (info)->dynamic_sections_created)
4792 {
4793 /* Set the contents of the .interp section to the interpreter. */
4794 if (! info->shared)
4795 {
4796 s = bfd_get_section_by_name (dynobj, ".interp");
4797 BFD_ASSERT (s != NULL);
4798 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4799 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4800 }
4801 }
4802
4803 /* Recompute the size of .got for local entires (reserved and
4804 hipages) if needed. To estimate it, get the upper bound of total
4805 size of loadable sections. */
4806 sgot = bfd_get_section_by_name (dynobj, ".got");
4807
4808 if (sgot != NULL)
4809 {
4810 bfd_size_type loadable_size = 0;
4811 bfd_size_type local_gotno;
4812 struct _bfd *sub;
4813
4814 BFD_ASSERT (elf_section_data (sgot) != NULL);
4815 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4816 BFD_ASSERT (g != NULL);
4817
4818 for (sub = info->input_bfds; sub; sub = sub->link_next)
4819 for (s = sub->sections; s != NULL; s = s->next)
4820 {
4821 if ((s->flags & SEC_ALLOC) == 0)
4822 continue;
4823 loadable_size += (s->_raw_size + 0xf) & ~0xf;
4824 }
4825
4826 loadable_size += MIPS_FUNCTION_STUB_SIZE;
4827
4828 /* Assume there are two loadable segments consisting of
4829 contiguous sections. Is 5 enough? */
4830 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
4831 g->local_gotno = local_gotno;
4832 sgot->_raw_size += local_gotno * 4;
4833 }
4834
4835 /* The check_relocs and adjust_dynamic_symbol entry points have
4836 determined the sizes of the various dynamic sections. Allocate
4837 memory for them. */
4838 reltext = false;
4839 for (s = dynobj->sections; s != NULL; s = s->next)
4840 {
4841 const char *name;
4842 boolean strip;
4843
4844 /* It's OK to base decisions on the section name, because none
4845 of the dynobj section names depend upon the input files. */
4846 name = bfd_get_section_name (dynobj, s);
4847
4848 if ((s->flags & SEC_IN_MEMORY) == 0)
4849 continue;
4850
4851 strip = false;
4852
4853 if (strncmp (name, ".rel", 4) == 0)
4854 {
4855 if (s->_raw_size == 0)
4856 strip = true;
4857 else
4858 {
4859 asection *target;
4860
4861 /* If this relocation section applies to a read only
4862 section, then we probably need a DT_TEXTREL entry.
4863 If the relocation section is .rel.dyn, we always
4864 assert a DT_TEXTREL entry rather than testing whether
4865 there exists a relocation to a read only section or
4866 not. */
4867 target = bfd_get_section_by_name (output_bfd, name + 4);
4868 if ((target != NULL && (target->flags & SEC_READONLY) != 0)
4869 || strcmp (name, ".rel.dyn") == 0)
4870 reltext = true;
4871
4872 /* We use the reloc_count field as a counter if we need
4873 to copy relocs into the output file. */
4874 if (strcmp (name, ".rel.dyn") != 0)
4875 s->reloc_count = 0;
4876 }
4877 }
4878 else if (strncmp (name, ".got", 4) == 0)
4879 {
4880 int i;
4881
4882 BFD_ASSERT (elf_section_data (s) != NULL);
4883 g = (struct mips_got_info *) elf_section_data (s)->tdata;
4884 BFD_ASSERT (g != NULL);
4885
4886 /* Fix the size of .got section for the correspondence of
4887 global symbols and got entries. This adds some useless
4888 got entries. Is this required by ABI really? */
4889 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
4890 s->_raw_size += i * 4;
4891 }
4892 else if (strncmp (name, ".stub", 5) == 0)
4893 {
4894 /* Irix rld assumes that the function stub isn't at the end
4895 of .text section. So put a dummy. XXX */
4896 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4897 }
4898 else if (SGI_COMPAT (output_bfd)
4899 && strncmp (name, ".compact_rel", 12) == 0)
4900 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
4901 else if (strncmp (name, ".init", 5) != 0)
4902 {
4903 /* It's not one of our sections, so don't allocate space. */
4904 continue;
4905 }
4906
4907 if (strip)
4908 {
4909 asection **spp;
4910
4911 for (spp = &s->output_section->owner->sections;
4912 *spp != s->output_section;
4913 spp = &(*spp)->next)
4914 ;
4915 *spp = s->output_section->next;
4916 --s->output_section->owner->section_count;
4917
4918 continue;
4919 }
4920
4921 /* Allocate memory for the section contents. */
4922 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
4923 if (s->contents == NULL && s->_raw_size != 0)
4924 {
4925 bfd_set_error (bfd_error_no_memory);
4926 return false;
4927 }
4928 memset (s->contents, 0, s->_raw_size);
4929 }
4930
4931 if (elf_hash_table (info)->dynamic_sections_created)
4932 {
4933 /* Add some entries to the .dynamic section. We fill in the
4934 values later, in elf_mips_finish_dynamic_sections, but we
4935 must add the entries now so that we get the correct size for
4936 the .dynamic section. The DT_DEBUG entry is filled in by the
4937 dynamic linker and used by the debugger. */
4938 if (! info->shared)
4939 {
4940 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
4941 return false;
4942 }
4943
4944 if (reltext)
4945 {
4946 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
4947 return false;
4948 }
4949
4950 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
4951 return false;
4952
4953 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
4954 {
4955 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
4956 return false;
4957
4958 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
4959 return false;
4960
4961 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
4962 return false;
4963 }
4964
4965 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
4966 return false;
4967
4968 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
4969 return false;
4970
4971 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
4972 {
4973 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
4974 return false;
4975
4976 s = bfd_get_section_by_name (dynobj, ".liblist");
4977 BFD_ASSERT (s != NULL);
4978
4979 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
4980 return false;
4981 }
4982
4983 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
4984 return false;
4985
4986 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
4987 return false;
4988
4989 #if 0
4990 /* Time stamps in executable files are a bad idea. */
4991 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
4992 return false;
4993 #endif
4994
4995 #if 0 /* FIXME */
4996 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
4997 return false;
4998 #endif
4999
5000 #if 0 /* FIXME */
5001 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
5002 return false;
5003 #endif
5004
5005 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
5006 return false;
5007
5008 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
5009 return false;
5010
5011 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
5012 return false;
5013
5014 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
5015 return false;
5016
5017 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
5018 return false;
5019
5020 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
5021 return false;
5022
5023 #if 0 /* (SGI_COMPAT) */
5024 if (! bfd_get_section_by_name (dynobj, ".init"))
5025 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
5026 return false;
5027
5028 if (! bfd_get_section_by_name (dynobj, ".fini"))
5029 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
5030 return false;
5031 #endif
5032 }
5033
5034 /* If we use dynamic linking, we generate a section symbol for each
5035 output section. These are local symbols, which means that they
5036 must come first in the dynamic symbol table.
5037 That means we must increment the dynamic symbol index of every
5038 other dynamic symbol. */
5039 {
5040 const char * const *namep;
5041 unsigned int c, i;
5042 bfd_size_type strindex;
5043 struct bfd_strtab_hash *dynstr;
5044 struct mips_got_info *g;
5045
5046 if (elf_hash_table (info)->dynamic_sections_created)
5047 {
5048 if (SGI_COMPAT (output_bfd))
5049 {
5050 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
5051 elf_link_hash_traverse (elf_hash_table (info),
5052 mips_elf_adjust_dynindx,
5053 (PTR) &c);
5054 elf_hash_table (info)->dynsymcount += c;
5055
5056 dynstr = elf_hash_table (info)->dynstr;
5057 BFD_ASSERT (dynstr != NULL);
5058
5059 for (i = 1, namep = mips_elf_dynsym_sec_names;
5060 *namep != NULL;
5061 i++, namep++)
5062 {
5063 s = bfd_get_section_by_name (output_bfd, *namep);
5064 if (s != NULL)
5065 elf_section_data (s)->dynindx = i;
5066
5067 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
5068 if (strindex == (bfd_size_type) -1)
5069 return false;
5070
5071 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
5072 }
5073 }
5074 else
5075 {
5076 c = bfd_count_sections (output_bfd);
5077 elf_link_hash_traverse (elf_hash_table (info),
5078 mips_elf_adjust_dynindx,
5079 (PTR) &c);
5080 elf_hash_table (info)->dynsymcount += c;
5081
5082 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
5083 {
5084 elf_section_data (s)->dynindx = i;
5085 /* These symbols will have no names, so we don't need to
5086 fiddle with dynstr_index. */
5087 }
5088 }
5089 }
5090
5091 s = bfd_get_section_by_name (dynobj, ".got");
5092 BFD_ASSERT (s != NULL);
5093 BFD_ASSERT (elf_section_data (s) != NULL);
5094 g = (struct mips_got_info *) elf_section_data (s)->tdata;
5095 BFD_ASSERT (g != NULL);
5096
5097 /* If there are no global got symbols, fake the last symbol so for
5098 safety. */
5099 if (g->global_gotsym)
5100 g->global_gotsym += c;
5101 else
5102 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
5103 }
5104
5105 return true;
5106 }
5107
5108 /* Increment the index of a dynamic symbol by a given amount. Called
5109 via elf_link_hash_traverse. */
5110
5111 static boolean
5112 mips_elf_adjust_dynindx (h, cparg)
5113 struct elf_link_hash_entry *h;
5114 PTR cparg;
5115 {
5116 unsigned int *cp = (unsigned int *) cparg;
5117
5118 if (h->dynindx != -1)
5119 h->dynindx += *cp;
5120 return true;
5121 }
5122
5123 /* Finish up dynamic symbol handling. We set the contents of various
5124 dynamic sections here. */
5125
5126 static boolean
5127 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5128 bfd *output_bfd;
5129 struct bfd_link_info *info;
5130 struct elf_link_hash_entry *h;
5131 Elf_Internal_Sym *sym;
5132 {
5133 bfd *dynobj;
5134 bfd_vma gval;
5135 asection *sgot;
5136 struct mips_got_info *g;
5137 const char *name;
5138
5139 dynobj = elf_hash_table (info)->dynobj;
5140 gval = sym->st_value;
5141
5142 if (h->plt_offset != (bfd_vma) -1)
5143 {
5144 asection *s;
5145 bfd_byte *p;
5146 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5147
5148 /* This symbol has a stub. Set it up. */
5149
5150 BFD_ASSERT (h->dynindx != -1);
5151
5152 s = bfd_get_section_by_name (dynobj, ".stub");
5153 BFD_ASSERT (s != NULL);
5154
5155 /* Fill the stub. */
5156 p = stub;
5157 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
5158 p += 4;
5159 bfd_put_32 (output_bfd, STUB_MOVE, p);
5160 p += 4;
5161
5162 /* FIXME: Can h->dynindex be more than 64K? */
5163 if (h->dynindx & 0xffff0000)
5164 return false;
5165
5166 bfd_put_32 (output_bfd, STUB_JALR, p);
5167 p += 4;
5168 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
5169
5170 BFD_ASSERT (h->plt_offset <= s->_raw_size);
5171 memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
5172
5173 /* Mark the symbol as undefined. plt_offset != -1 occurs
5174 only for the referenced symbol. */
5175 sym->st_shndx = SHN_UNDEF;
5176
5177 /* The run-time linker uses the st_value field of the symbol
5178 to reset the global offset table entry for this external
5179 to its stub address when unlinking a shared object. */
5180 gval = s->output_section->vma + s->output_offset + h->plt_offset;
5181 sym->st_value = gval;
5182 }
5183
5184 BFD_ASSERT (h->dynindx != -1);
5185
5186 sgot = bfd_get_section_by_name (dynobj, ".got");
5187 BFD_ASSERT (sgot != NULL);
5188 BFD_ASSERT (elf_section_data (sgot) != NULL);
5189 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5190 BFD_ASSERT (g != NULL);
5191
5192 if ((unsigned long) h->dynindx >= g->global_gotsym)
5193 {
5194 bfd_size_type offset;
5195
5196 /* This symbol has an entry in the global offset table. Set its
5197 value to the corresponding got entry, if needed. */
5198 if (h->got_offset == (bfd_vma) -1)
5199 {
5200 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5201 BFD_ASSERT (g->local_gotno * 4 <= offset
5202 && offset < sgot->_raw_size);
5203 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
5204 }
5205 }
5206
5207 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5208 name = h->root.root.string;
5209 if (strcmp (name, "_DYNAMIC") == 0
5210 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
5211 sym->st_shndx = SHN_ABS;
5212 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
5213 {
5214 sym->st_shndx = SHN_ABS;
5215 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5216 sym->st_value = 1;
5217 }
5218 else if (SGI_COMPAT (output_bfd))
5219 {
5220 if (strcmp (name, "_gp_disp") == 0)
5221 {
5222 sym->st_shndx = SHN_ABS;
5223 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5224 sym->st_value = elf_gp (output_bfd);
5225 }
5226 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
5227 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
5228 {
5229 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5230 sym->st_other = STO_PROTECTED;
5231 sym->st_value = 0;
5232 sym->st_shndx = SHN_MIPS_DATA;
5233 }
5234 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
5235 {
5236 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5237 sym->st_other = STO_PROTECTED;
5238 sym->st_value = mips_elf_hash_table (info)->procedure_count;
5239 sym->st_shndx = SHN_ABS;
5240 }
5241 else if (sym->st_shndx != SHN_UNDEF)
5242 {
5243 if (h->type == STT_FUNC)
5244 sym->st_shndx = SHN_MIPS_TEXT;
5245 else if (h->type == STT_OBJECT)
5246 sym->st_shndx = SHN_MIPS_DATA;
5247 }
5248 }
5249
5250 return true;
5251 }
5252
5253 /* Finish up the dynamic sections. */
5254
5255 static boolean
5256 mips_elf_finish_dynamic_sections (output_bfd, info)
5257 bfd *output_bfd;
5258 struct bfd_link_info *info;
5259 {
5260 bfd *dynobj;
5261 asection *sdyn;
5262 asection *sgot;
5263 struct mips_got_info *g;
5264
5265 dynobj = elf_hash_table (info)->dynobj;
5266
5267 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5268
5269 sgot = bfd_get_section_by_name (dynobj, ".got");
5270 BFD_ASSERT (sgot != NULL);
5271
5272 BFD_ASSERT (elf_section_data (sgot) != NULL);
5273 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5274 BFD_ASSERT (g != NULL);
5275
5276 if (elf_hash_table (info)->dynamic_sections_created)
5277 {
5278 Elf32_External_Dyn *dyncon, *dynconend;
5279
5280 BFD_ASSERT (sdyn != NULL);
5281
5282 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5283 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5284 for (; dyncon < dynconend; dyncon++)
5285 {
5286 Elf_Internal_Dyn dyn;
5287 const char *name;
5288 size_t elemsize;
5289 asection *s;
5290
5291 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5292
5293 switch (dyn.d_tag)
5294 {
5295 default:
5296 break;
5297
5298 case DT_RELENT:
5299 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5300 BFD_ASSERT (s != NULL);
5301 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
5302 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5303 break;
5304
5305 case DT_STRSZ:
5306 /* Rewrite DT_STRSZ. */
5307 dyn.d_un.d_val =
5308 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
5309 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5310 break;
5311
5312 case DT_PLTGOT:
5313 name = ".got";
5314 goto get_vma;
5315 case DT_MIPS_CONFLICT:
5316 name = ".conflict";
5317 goto get_vma;
5318 case DT_MIPS_LIBLIST:
5319 name = ".liblist";
5320 get_vma:
5321 s = bfd_get_section_by_name (output_bfd, name);
5322 BFD_ASSERT (s != NULL);
5323 dyn.d_un.d_ptr = s->vma;
5324 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5325 break;
5326
5327 case DT_MIPS_RLD_VERSION:
5328 dyn.d_un.d_val = 1; /* XXX */
5329 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5330 break;
5331
5332 case DT_MIPS_FLAGS:
5333 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
5334 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5335 break;
5336
5337 case DT_MIPS_CONFLICTNO:
5338 name = ".conflict";
5339 elemsize = sizeof (Elf32_Conflict);
5340 goto set_elemno;
5341
5342 case DT_MIPS_LIBLISTNO:
5343 name = ".liblist";
5344 elemsize = sizeof (Elf32_Lib);
5345 set_elemno:
5346 s = bfd_get_section_by_name (output_bfd, name);
5347 if (s != NULL)
5348 {
5349 if (s->_cooked_size != 0)
5350 dyn.d_un.d_val = s->_cooked_size / elemsize;
5351 else
5352 dyn.d_un.d_val = s->_raw_size / elemsize;
5353 }
5354 else
5355 dyn.d_un.d_val = 0;
5356
5357 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5358 break;
5359
5360 case DT_MIPS_TIME_STAMP:
5361 time ((time_t *) &dyn.d_un.d_val);
5362 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5363 break;
5364
5365 case DT_MIPS_ICHECKSUM:
5366 /* XXX FIXME: */
5367 break;
5368
5369 case DT_MIPS_IVERSION:
5370 /* XXX FIXME: */
5371 break;
5372
5373 case DT_MIPS_BASE_ADDRESS:
5374 s = output_bfd->sections;
5375 BFD_ASSERT (s != NULL);
5376 dyn.d_un.d_ptr = s->vma & ~(0xffff);
5377 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5378 break;
5379
5380 case DT_MIPS_LOCAL_GOTNO:
5381 dyn.d_un.d_val = g->local_gotno;
5382 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5383 break;
5384
5385 case DT_MIPS_SYMTABNO:
5386 name = ".dynsym";
5387 elemsize = sizeof (Elf32_External_Sym);
5388 s = bfd_get_section_by_name (output_bfd, name);
5389 BFD_ASSERT (s != NULL);
5390
5391 if (s->_cooked_size != 0)
5392 dyn.d_un.d_val = s->_cooked_size / elemsize;
5393 else
5394 dyn.d_un.d_val = s->_raw_size / elemsize;
5395 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5396 break;
5397
5398 case DT_MIPS_UNREFEXTNO:
5399 /* XXX FIXME: */
5400 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
5401 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5402 break;
5403
5404 case DT_MIPS_GOTSYM:
5405 dyn.d_un.d_val = g->global_gotsym;
5406 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5407 break;
5408
5409 case DT_MIPS_HIPAGENO:
5410 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
5411 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5412 break;
5413
5414 }
5415 }
5416 }
5417
5418 /* The first entry of the global offset table will be filled at
5419 runtime. The second entry will be used by some runtime loaders.
5420 This isn't the case of Irix rld. */
5421 if (sgot->_raw_size > 0)
5422 {
5423 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5424 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
5425 }
5426
5427 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5428
5429 {
5430 asection *sdynsym;
5431 asection *s;
5432 unsigned int i;
5433 bfd_vma last;
5434 Elf_Internal_Sym sym;
5435 long dindx;
5436 const char *name;
5437 const char * const * namep = mips_elf_dynsym_sec_names;
5438 Elf32_compact_rel cpt;
5439
5440 /* Set up the section symbols for the output sections. SGI sets
5441 the STT_NOTYPE attribute for these symbols. Should we do so? */
5442
5443 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
5444 if (sdynsym != NULL)
5445 {
5446 if (SGI_COMPAT (output_bfd))
5447 {
5448 sym.st_size = 0;
5449 sym.st_name = 0;
5450 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
5451 sym.st_other = 0;
5452
5453 i = 0;
5454 while ((name = *namep++) != NULL)
5455 {
5456 s = bfd_get_section_by_name (output_bfd, name);
5457 if (s != NULL)
5458 {
5459 sym.st_value = s->vma;
5460 dindx = elf_section_data (s)->dynindx;
5461 last = s->vma + s->_raw_size;
5462 }
5463 else
5464 {
5465 sym.st_value = last;
5466 dindx++;
5467 }
5468
5469 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
5470 ? SHN_MIPS_TEXT
5471 : SHN_MIPS_DATA);
5472 ++i;
5473 sym.st_name =
5474 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
5475
5476 bfd_elf32_swap_symbol_out (output_bfd, &sym,
5477 (((Elf32_External_Sym *)
5478 sdynsym->contents)
5479 + dindx));
5480 }
5481
5482 /* Set the sh_info field of the output .dynsym section to
5483 the index of the first global symbol. */
5484 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
5485 SIZEOF_MIPS_DYNSYM_SECNAMES;
5486 }
5487 else
5488 {
5489 sym.st_size = 0;
5490 sym.st_name = 0;
5491 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5492 sym.st_other = 0;
5493
5494 for (s = output_bfd->sections; s != NULL; s = s->next)
5495 {
5496 int indx;
5497
5498 sym.st_value = s->vma;
5499
5500 indx = elf_section_data (s)->this_idx;
5501 BFD_ASSERT (indx > 0);
5502 sym.st_shndx = indx;
5503
5504 bfd_elf32_swap_symbol_out (output_bfd, &sym,
5505 (((Elf32_External_Sym *)
5506 sdynsym->contents)
5507 + elf_section_data (s)->dynindx));
5508 }
5509
5510 /* Set the sh_info field of the output .dynsym section to
5511 the index of the first global symbol. */
5512 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
5513 bfd_count_sections (output_bfd) + 1;
5514 }
5515 }
5516
5517 if (SGI_COMPAT (output_bfd))
5518 {
5519 /* Write .compact_rel section out. */
5520 s = bfd_get_section_by_name (dynobj, ".compact_rel");
5521 if (s != NULL)
5522 {
5523 cpt.id1 = 1;
5524 cpt.num = s->reloc_count;
5525 cpt.id2 = 2;
5526 cpt.offset = (s->output_section->filepos
5527 + sizeof (Elf32_External_compact_rel));
5528 cpt.reserved0 = 0;
5529 cpt.reserved1 = 0;
5530 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
5531 ((Elf32_External_compact_rel *)
5532 s->contents));
5533
5534 /* Clean up a dummy stub function entry in .text. */
5535 s = bfd_get_section_by_name (dynobj, ".stub");
5536 if (s != NULL)
5537 {
5538 file_ptr dummy_offset;
5539
5540 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
5541 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
5542 memset (s->contents + dummy_offset, 0,
5543 MIPS_FUNCTION_STUB_SIZE);
5544 }
5545 }
5546 }
5547
5548 /* Clean up a first relocation in .rel.dyn. */
5549 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5550 if (s != NULL)
5551 memset (s->contents, 0, sizeof (Elf32_External_Rel));
5552 }
5553
5554 return true;
5555 }
5556 \f
5557 /* This is almost identical to bfd_generic_get_... except that some
5558 MIPS relocations need to be handled specially. Sigh. */
5559
5560 static bfd_byte *
5561 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
5562 relocateable, symbols)
5563 bfd *abfd;
5564 struct bfd_link_info *link_info;
5565 struct bfd_link_order *link_order;
5566 bfd_byte *data;
5567 boolean relocateable;
5568 asymbol **symbols;
5569 {
5570 /* Get enough memory to hold the stuff */
5571 bfd *input_bfd = link_order->u.indirect.section->owner;
5572 asection *input_section = link_order->u.indirect.section;
5573
5574 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
5575 arelent **reloc_vector = NULL;
5576 long reloc_count;
5577
5578 if (reloc_size < 0)
5579 goto error_return;
5580
5581 reloc_vector = (arelent **) bfd_malloc (reloc_size);
5582 if (reloc_vector == NULL && reloc_size != 0)
5583 goto error_return;
5584
5585 /* read in the section */
5586 if (!bfd_get_section_contents (input_bfd,
5587 input_section,
5588 (PTR) data,
5589 0,
5590 input_section->_raw_size))
5591 goto error_return;
5592
5593 /* We're not relaxing the section, so just copy the size info */
5594 input_section->_cooked_size = input_section->_raw_size;
5595 input_section->reloc_done = true;
5596
5597 reloc_count = bfd_canonicalize_reloc (input_bfd,
5598 input_section,
5599 reloc_vector,
5600 symbols);
5601 if (reloc_count < 0)
5602 goto error_return;
5603
5604 if (reloc_count > 0)
5605 {
5606 arelent **parent;
5607 /* for mips */
5608 int gp_found;
5609 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
5610
5611 {
5612 struct bfd_hash_entry *h;
5613 struct bfd_link_hash_entry *lh;
5614 /* Skip all this stuff if we aren't mixing formats. */
5615 if (abfd && input_bfd
5616 && abfd->xvec == input_bfd->xvec)
5617 lh = 0;
5618 else
5619 {
5620 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
5621 lh = (struct bfd_link_hash_entry *) h;
5622 }
5623 lookup:
5624 if (lh)
5625 {
5626 switch (lh->type)
5627 {
5628 case bfd_link_hash_undefined:
5629 case bfd_link_hash_undefweak:
5630 case bfd_link_hash_common:
5631 gp_found = 0;
5632 break;
5633 case bfd_link_hash_defined:
5634 case bfd_link_hash_defweak:
5635 gp_found = 1;
5636 gp = lh->u.def.value;
5637 break;
5638 case bfd_link_hash_indirect:
5639 case bfd_link_hash_warning:
5640 lh = lh->u.i.link;
5641 /* @@FIXME ignoring warning for now */
5642 goto lookup;
5643 case bfd_link_hash_new:
5644 default:
5645 abort ();
5646 }
5647 }
5648 else
5649 gp_found = 0;
5650 }
5651 /* end mips */
5652 for (parent = reloc_vector; *parent != (arelent *) NULL;
5653 parent++)
5654 {
5655 char *error_message = (char *) NULL;
5656 bfd_reloc_status_type r;
5657
5658 /* Specific to MIPS: Deal with relocation types that require
5659 knowing the gp of the output bfd. */
5660 asymbol *sym = *(*parent)->sym_ptr_ptr;
5661 if (bfd_is_abs_section (sym->section) && abfd)
5662 {
5663 /* The special_function wouldn't get called anyways. */
5664 }
5665 else if (!gp_found)
5666 {
5667 /* The gp isn't there; let the special function code
5668 fall over on its own. */
5669 }
5670 else if ((*parent)->howto->special_function
5671 == mips_elf_gprel16_reloc)
5672 {
5673 /* bypass special_function call */
5674 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
5675 relocateable, (PTR) data, gp);
5676 goto skip_bfd_perform_relocation;
5677 }
5678 /* end mips specific stuff */
5679
5680 r = bfd_perform_relocation (input_bfd,
5681 *parent,
5682 (PTR) data,
5683 input_section,
5684 relocateable ? abfd : (bfd *) NULL,
5685 &error_message);
5686 skip_bfd_perform_relocation:
5687
5688 if (relocateable)
5689 {
5690 asection *os = input_section->output_section;
5691
5692 /* A partial link, so keep the relocs */
5693 os->orelocation[os->reloc_count] = *parent;
5694 os->reloc_count++;
5695 }
5696
5697 if (r != bfd_reloc_ok)
5698 {
5699 switch (r)
5700 {
5701 case bfd_reloc_undefined:
5702 if (!((*link_info->callbacks->undefined_symbol)
5703 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5704 input_bfd, input_section, (*parent)->address)))
5705 goto error_return;
5706 break;
5707 case bfd_reloc_dangerous:
5708 BFD_ASSERT (error_message != (char *) NULL);
5709 if (!((*link_info->callbacks->reloc_dangerous)
5710 (link_info, error_message, input_bfd, input_section,
5711 (*parent)->address)))
5712 goto error_return;
5713 break;
5714 case bfd_reloc_overflow:
5715 if (!((*link_info->callbacks->reloc_overflow)
5716 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5717 (*parent)->howto->name, (*parent)->addend,
5718 input_bfd, input_section, (*parent)->address)))
5719 goto error_return;
5720 break;
5721 case bfd_reloc_outofrange:
5722 default:
5723 abort ();
5724 break;
5725 }
5726
5727 }
5728 }
5729 }
5730 if (reloc_vector != NULL)
5731 free (reloc_vector);
5732 return data;
5733
5734 error_return:
5735 if (reloc_vector != NULL)
5736 free (reloc_vector);
5737 return NULL;
5738 }
5739 #define bfd_elf32_bfd_get_relocated_section_contents \
5740 elf32_mips_get_relocated_section_contents
5741 \f
5742 /* ECOFF swapping routines. These are used when dealing with the
5743 .mdebug section, which is in the ECOFF debugging format. */
5744 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
5745 {
5746 /* Symbol table magic number. */
5747 magicSym,
5748 /* Alignment of debugging information. E.g., 4. */
5749 4,
5750 /* Sizes of external symbolic information. */
5751 sizeof (struct hdr_ext),
5752 sizeof (struct dnr_ext),
5753 sizeof (struct pdr_ext),
5754 sizeof (struct sym_ext),
5755 sizeof (struct opt_ext),
5756 sizeof (struct fdr_ext),
5757 sizeof (struct rfd_ext),
5758 sizeof (struct ext_ext),
5759 /* Functions to swap in external symbolic data. */
5760 ecoff_swap_hdr_in,
5761 ecoff_swap_dnr_in,
5762 ecoff_swap_pdr_in,
5763 ecoff_swap_sym_in,
5764 ecoff_swap_opt_in,
5765 ecoff_swap_fdr_in,
5766 ecoff_swap_rfd_in,
5767 ecoff_swap_ext_in,
5768 _bfd_ecoff_swap_tir_in,
5769 _bfd_ecoff_swap_rndx_in,
5770 /* Functions to swap out external symbolic data. */
5771 ecoff_swap_hdr_out,
5772 ecoff_swap_dnr_out,
5773 ecoff_swap_pdr_out,
5774 ecoff_swap_sym_out,
5775 ecoff_swap_opt_out,
5776 ecoff_swap_fdr_out,
5777 ecoff_swap_rfd_out,
5778 ecoff_swap_ext_out,
5779 _bfd_ecoff_swap_tir_out,
5780 _bfd_ecoff_swap_rndx_out,
5781 /* Function to read in symbolic data. */
5782 mips_elf_read_ecoff_info
5783 };
5784 \f
5785 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
5786 #define TARGET_LITTLE_NAME "elf32-littlemips"
5787 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
5788 #define TARGET_BIG_NAME "elf32-bigmips"
5789 #define ELF_ARCH bfd_arch_mips
5790 #define ELF_MACHINE_CODE EM_MIPS
5791 #define ELF_MAXPAGESIZE 0x10000
5792 #define elf_backend_collect true
5793 #define elf_backend_type_change_ok true
5794 #define elf_info_to_howto 0
5795 #define elf_info_to_howto_rel mips_info_to_howto_rel
5796 #define elf_backend_sym_is_global mips_elf_sym_is_global
5797 #define elf_backend_object_p mips_elf_object_p
5798 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
5799 #define elf_backend_fake_sections mips_elf_fake_sections
5800 #define elf_backend_section_from_bfd_section \
5801 mips_elf_section_from_bfd_section
5802 #define elf_backend_section_processing mips_elf_section_processing
5803 #define elf_backend_symbol_processing mips_elf_symbol_processing
5804 #define elf_backend_additional_program_headers \
5805 mips_elf_additional_program_headers
5806 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
5807 #define elf_backend_final_write_processing \
5808 mips_elf_final_write_processing
5809 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
5810
5811 #define bfd_elf32_bfd_is_local_label mips_elf_is_local_label
5812 #define bfd_elf32_find_nearest_line mips_elf_find_nearest_line
5813
5814 #define bfd_elf32_bfd_link_hash_table_create \
5815 mips_elf_link_hash_table_create
5816 #define bfd_elf32_bfd_final_link mips_elf_final_link
5817 #define bfd_elf32_bfd_copy_private_bfd_data \
5818 mips_elf_copy_private_bfd_data
5819 #define bfd_elf32_bfd_merge_private_bfd_data \
5820 mips_elf_merge_private_bfd_data
5821 #define bfd_elf32_bfd_set_private_flags mips_elf_set_private_flags
5822 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
5823 #define elf_backend_create_dynamic_sections \
5824 mips_elf_create_dynamic_sections
5825 #define elf_backend_check_relocs mips_elf_check_relocs
5826 #define elf_backend_adjust_dynamic_symbol \
5827 mips_elf_adjust_dynamic_symbol
5828 #define elf_backend_size_dynamic_sections \
5829 mips_elf_size_dynamic_sections
5830 #define elf_backend_relocate_section mips_elf_relocate_section
5831 #define elf_backend_finish_dynamic_symbol \
5832 mips_elf_finish_dynamic_symbol
5833 #define elf_backend_finish_dynamic_sections \
5834 mips_elf_finish_dynamic_sections
5835 #define elf_backend_want_hdr_in_seg 1
5836
5837 #include "elf32-target.h"