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