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