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