*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / mipsbsd.c
CommitLineData
252b5132 1/* BFD backend for MIPS BSD (a.out) binaries.
93516ec3 2 Copyright 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4 Written by Ralph Campbell.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
3e110533 20Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 21
252b5132
RH
22/* #define ENTRY_CAN_BE_ZERO */
23#define N_HEADER_IN_TEXT(x) 1
24#define N_SHARED_LIB(x) 0
25#define N_TXTADDR(x) \
26 (N_MAGIC(x) != ZMAGIC ? (x).a_entry : /* object file or NMAGIC */\
27 TEXT_START_ADDR + EXEC_BYTES_SIZE /* no padding */\
28 )
29#define N_DATADDR(x) (N_TXTADDR(x)+N_TXTSIZE(x))
30#define TEXT_START_ADDR 4096
31#define TARGET_PAGE_SIZE 4096
32#define SEGMENT_SIZE TARGET_PAGE_SIZE
33#define DEFAULT_ARCH bfd_arch_mips
34#define MACHTYPE_OK(mtype) ((mtype) == M_UNKNOWN \
35 || (mtype) == M_MIPS1 || (mtype) == M_MIPS2)
36#define MY_symbol_leading_char '\0'
37
e43d48cc
AM
38/* Do not "beautify" the CONCAT* macro args. Traditional C will not
39 remove whitespace added here, and thus will fail to concatenate
40 the tokens. */
41#define MY(OP) CONCAT2 (mipsbsd_,OP)
252b5132
RH
42
43#include "bfd.h"
44#include "sysdep.h"
45#include "libbfd.h"
46#include "libaout.h"
47
48#define SET_ARCH_MACH(ABFD, EXEC) \
1518639e
KH
49 MY(set_arch_mach) (ABFD, N_MACHTYPE (EXEC)); \
50 MY(choose_reloc_size) (ABFD);
dc810e39 51static void MY(set_arch_mach) PARAMS ((bfd *abfd, unsigned long machtype));
252b5132
RH
52static void MY(choose_reloc_size) PARAMS ((bfd *abfd));
53
54#define MY_write_object_contents MY(write_object_contents)
b34976b6 55static bfd_boolean MY(write_object_contents) PARAMS ((bfd *abfd));
252b5132 56
e43d48cc 57/* We can't use MY(x) here because it leads to a recursive call to CONCAT2
252b5132
RH
58 when expanded inside JUMP_TABLE. */
59#define MY_bfd_reloc_type_lookup mipsbsd_reloc_howto_type_lookup
60#define MY_canonicalize_reloc mipsbsd_canonicalize_reloc
61
62#define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
63#define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols
64#define MY_final_link_callback unused
65#define MY_bfd_final_link _bfd_generic_final_link
66
67#define MY_backend_data &MY(backend_data)
68#define MY_BFD_TARGET
69
70#include "aout-target.h"
71
dc810e39 72static bfd_reloc_status_type mips_fix_jmp_addr
fc0a2244 73 PARAMS ((bfd *, arelent *, struct bfd_symbol *, PTR, asection *,
dc810e39
AM
74 bfd *, char **));
75static reloc_howto_type *MY(reloc_howto_type_lookup)
76 PARAMS ((bfd *, bfd_reloc_code_real_type));
e4b17274
NC
77
78long MY(canonicalize_reloc) PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
79
252b5132
RH
80static void
81MY(set_arch_mach) (abfd, machtype)
82 bfd *abfd;
dc810e39 83 unsigned long machtype;
252b5132
RH
84{
85 enum bfd_architecture arch;
dc810e39 86 unsigned int machine;
252b5132 87
1518639e 88 /* Determine the architecture and machine type of the object file. */
e4b17274
NC
89 switch (machtype)
90 {
91 case M_MIPS1:
92 arch = bfd_arch_mips;
250d94fd 93 machine = bfd_mach_mips3000;
e4b17274
NC
94 break;
95
96 case M_MIPS2:
97 arch = bfd_arch_mips;
250d94fd 98 machine = bfd_mach_mips4000;
e4b17274
NC
99 break;
100
101 default:
102 arch = bfd_arch_obscure;
103 machine = 0;
104 break;
105 }
106
107 bfd_set_arch_mach (abfd, arch, machine);
252b5132
RH
108}
109
110/* Determine the size of a relocation entry, based on the architecture */
111static void
e4b17274 112MY (choose_reloc_size) (abfd)
252b5132
RH
113 bfd *abfd;
114{
e4b17274
NC
115 switch (bfd_get_arch (abfd))
116 {
117 case bfd_arch_sparc:
e4b17274
NC
118 case bfd_arch_mips:
119 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
120 break;
121 default:
122 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
123 break;
124 }
252b5132
RH
125}
126
127/* Write an object file in BSD a.out format.
128 Section contents have already been written. We write the
129 file header, symbols, and relocation. */
130
b34976b6 131static bfd_boolean
e4b17274 132MY (write_object_contents) (abfd)
252b5132
RH
133 bfd *abfd;
134{
135 struct external_exec exec_bytes;
136 struct internal_exec *execp = exec_hdr (abfd);
137
138 /* Magic number, maestro, please! */
e4b17274
NC
139 switch (bfd_get_arch (abfd))
140 {
141 case bfd_arch_m68k:
142 switch (bfd_get_mach (abfd))
143 {
144 case bfd_mach_m68010:
145 N_SET_MACHTYPE (*execp, M_68010);
146 break;
147 default:
148 case bfd_mach_m68020:
149 N_SET_MACHTYPE (*execp, M_68020);
150 break;
151 }
252b5132 152 break;
e4b17274
NC
153 case bfd_arch_sparc:
154 N_SET_MACHTYPE (*execp, M_SPARC);
252b5132 155 break;
e4b17274
NC
156 case bfd_arch_i386:
157 N_SET_MACHTYPE (*execp, M_386);
252b5132 158 break;
e4b17274
NC
159 case bfd_arch_mips:
160 switch (bfd_get_mach (abfd))
161 {
250d94fd
AM
162 case bfd_mach_mips4000:
163 case bfd_mach_mips6000:
e4b17274
NC
164 N_SET_MACHTYPE (*execp, M_MIPS2);
165 break;
166 default:
167 N_SET_MACHTYPE (*execp, M_MIPS1);
168 break;
169 }
252b5132 170 break;
e4b17274
NC
171 default:
172 N_SET_MACHTYPE (*execp, M_UNKNOWN);
252b5132 173 }
252b5132 174
e4b17274 175 MY (choose_reloc_size) (abfd);
252b5132 176
e4b17274 177 WRITE_HEADERS (abfd, execp);
252b5132 178
b34976b6 179 return TRUE;
252b5132
RH
180}
181
e4b17274 182/* MIPS relocation types. */
252b5132
RH
183#define MIPS_RELOC_32 0
184#define MIPS_RELOC_JMP 1
185#define MIPS_RELOC_WDISP16 2
186#define MIPS_RELOC_HI16 3
187#define MIPS_RELOC_HI16_S 4
188#define MIPS_RELOC_LO16 5
189
e4b17274
NC
190/* This is only called when performing a BFD_RELOC_MIPS_JMP relocation.
191 The jump destination address is formed from the upper 4 bits of the
192 "current" program counter concatenated with the jump instruction's
193 26 bit field and two trailing zeros.
194 If the destination address is not in the same segment as the "current"
195 program counter, then we need to signal an error. */
196
252b5132 197static bfd_reloc_status_type
dc810e39
AM
198mips_fix_jmp_addr (abfd, reloc_entry, symbol, data, input_section, output_bfd,
199 error_message)
5f771d47 200 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 201 arelent *reloc_entry;
fc0a2244 202 struct bfd_symbol *symbol;
5f771d47 203 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
204 asection *input_section;
205 bfd *output_bfd;
dc810e39 206 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
207{
208 bfd_vma relocation, pc;
1518639e
KH
209
210 /* If this is a partial relocation, just continue. */
252b5132
RH
211 if (output_bfd != (bfd *)NULL)
212 return bfd_reloc_continue;
213
214 /* If this is an undefined symbol, return error */
215 if (bfd_is_und_section (symbol->section)
216 && (symbol->flags & BSF_WEAK) == 0)
217 return bfd_reloc_undefined;
218
7dee875e 219 /* Work out which section the relocation is targeted at and the
e4b17274 220 initial relocation command value. */
252b5132
RH
221 if (bfd_is_com_section (symbol->section))
222 relocation = 0;
223 else
224 relocation = symbol->value;
225
226 relocation += symbol->section->output_section->vma;
227 relocation += symbol->section->output_offset;
228 relocation += reloc_entry->addend;
229
230 pc = input_section->output_section->vma + input_section->output_offset +
231 reloc_entry->address + 4;
232
233 if ((relocation & 0xF0000000) != (pc & 0xF0000000))
234 return bfd_reloc_overflow;
235
236 return bfd_reloc_continue;
237}
238
e4b17274
NC
239/* This is only called when performing a BFD_RELOC_HI16_S relocation.
240 We need to see if bit 15 is set in the result. If it is, we add
241 0x10000 and continue normally. This will compensate for the sign extension
242 when the low bits are added at run time. */
243
252b5132
RH
244static bfd_reloc_status_type
245mips_fix_hi16_s PARAMS ((bfd *, arelent *, asymbol *, PTR,
246 asection *, bfd *, char **));
247
248static bfd_reloc_status_type
249mips_fix_hi16_s (abfd, reloc_entry, symbol, data, input_section,
250 output_bfd, error_message)
5f771d47 251 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
252 arelent *reloc_entry;
253 asymbol *symbol;
5f771d47
ILT
254 PTR data ATTRIBUTE_UNUSED;
255 asection *input_section ATTRIBUTE_UNUSED;
252b5132 256 bfd *output_bfd;
5f771d47 257 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
258{
259 bfd_vma relocation;
1518639e
KH
260
261 /* If this is a partial relocation, just continue. */
252b5132
RH
262 if (output_bfd != (bfd *)NULL)
263 return bfd_reloc_continue;
264
e4b17274 265 /* If this is an undefined symbol, return error. */
252b5132
RH
266 if (bfd_is_und_section (symbol->section)
267 && (symbol->flags & BSF_WEAK) == 0)
268 return bfd_reloc_undefined;
269
7dee875e 270 /* Work out which section the relocation is targeted at and the
e4b17274 271 initial relocation command value. */
252b5132
RH
272 if (bfd_is_com_section (symbol->section))
273 relocation = 0;
274 else
275 relocation = symbol->value;
276
277 relocation += symbol->section->output_section->vma;
278 relocation += symbol->section->output_offset;
279 relocation += reloc_entry->addend;
280
281 if (relocation & 0x8000)
282 reloc_entry->addend += 0x10000;
283
284 return bfd_reloc_continue;
285}
286
287static reloc_howto_type mips_howto_table_ext[] = {
b34976b6
AM
288 {MIPS_RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0,
289 "32", FALSE, 0, 0xffffffff, FALSE},
290 {MIPS_RELOC_JMP, 2, 2, 26, FALSE, 0, complain_overflow_dont,
252b5132 291 mips_fix_jmp_addr,
b34976b6
AM
292 "MIPS_JMP", FALSE, 0, 0x03ffffff, FALSE},
293 {MIPS_RELOC_WDISP16, 2, 2, 16, TRUE, 0, complain_overflow_signed, 0,
294 "WDISP16", FALSE, 0, 0x0000ffff, FALSE},
295 {MIPS_RELOC_HI16, 16, 2, 16, FALSE, 0, complain_overflow_bitfield, 0,
296 "HI16", FALSE, 0, 0x0000ffff, FALSE},
297 {MIPS_RELOC_HI16_S, 16, 2, 16, FALSE, 0, complain_overflow_bitfield,
252b5132 298 mips_fix_hi16_s,
b34976b6
AM
299 "HI16_S", FALSE, 0, 0x0000ffff, FALSE},
300 {MIPS_RELOC_LO16, 0, 2, 16, FALSE, 0, complain_overflow_dont, 0,
301 "LO16", FALSE, 0, 0x0000ffff, FALSE},
252b5132
RH
302};
303
304static reloc_howto_type *
305MY(reloc_howto_type_lookup) (abfd, code)
306 bfd *abfd;
307 bfd_reloc_code_real_type code;
308{
309
310 if (bfd_get_arch (abfd) != bfd_arch_mips)
311 return 0;
312
313 switch (code)
314 {
315 case BFD_RELOC_CTOR:
316 case BFD_RELOC_32:
317 return (&mips_howto_table_ext[MIPS_RELOC_32]);
318 case BFD_RELOC_MIPS_JMP:
319 return (&mips_howto_table_ext[MIPS_RELOC_JMP]);
320 case BFD_RELOC_16_PCREL_S2:
321 return (&mips_howto_table_ext[MIPS_RELOC_WDISP16]);
322 case BFD_RELOC_HI16:
323 return (&mips_howto_table_ext[MIPS_RELOC_HI16]);
324 case BFD_RELOC_HI16_S:
325 return (&mips_howto_table_ext[MIPS_RELOC_HI16_S]);
326 case BFD_RELOC_LO16:
327 return (&mips_howto_table_ext[MIPS_RELOC_LO16]);
328 default:
329 return 0;
330 }
331}
332
e4b17274
NC
333/* This is just like the standard aoutx.h version but we need to do our
334 own mapping of external reloc type values to howto entries. */
252b5132 335long
1518639e 336MY(canonicalize_reloc) (abfd, section, relptr, symbols)
252b5132
RH
337 bfd *abfd;
338 sec_ptr section;
339 arelent **relptr;
340 asymbol **symbols;
341{
342 arelent *tblptr = section->relocation;
343 unsigned int count, c;
344 extern reloc_howto_type NAME(aout,ext_howto_table)[];
345
1518639e 346 /* If we have already read in the relocation table, return the values. */
e4b17274
NC
347 if (section->flags & SEC_CONSTRUCTOR)
348 {
349 arelent_chain *chain = section->constructor_chain;
350
351 for (count = 0; count < section->reloc_count; count++)
352 {
353 *relptr++ = &chain->relent;
354 chain = chain->next;
355 }
356 *relptr = 0;
357 return section->reloc_count;
358 }
252b5132 359
e4b17274
NC
360 if (tblptr && section->reloc_count)
361 {
362 for (count = 0; count++ < section->reloc_count;)
363 *relptr++ = tblptr++;
364 *relptr = 0;
365 return section->reloc_count;
252b5132 366 }
252b5132 367
1518639e 368 if (!NAME(aout,slurp_reloc_table) (abfd, section, symbols))
252b5132
RH
369 return -1;
370 tblptr = section->relocation;
371
e4b17274 372 /* fix up howto entries. */
1518639e 373 for (count = 0; count++ < section->reloc_count;)
252b5132
RH
374 {
375 c = tblptr->howto - NAME(aout,ext_howto_table);
376 tblptr->howto = &mips_howto_table_ext[c];
377
378 *relptr++ = tblptr++;
379 }
380 *relptr = 0;
381 return section->reloc_count;
382}
383
dc810e39 384static const struct aout_backend_data MY(backend_data) = {
252b5132
RH
385 0, /* zmagic contiguous */
386 1, /* text incl header */
387 0, /* entry is text address */
388 0, /* exec_hdr_flags */
389 TARGET_PAGE_SIZE, /* text vma */
390 MY_set_sizes,
391 0, /* text size includes exec header */
392 0, /* add_dynamic_symbols */
393 0, /* add_one_symbol */
394 0, /* link_dynamic_object */
395 0, /* write_dynamic_symbol */
396 0, /* check_dynamic_reloc */
397 0 /* finish_dynamic_link */
398};
399
c3c89269
NC
400extern const bfd_target aout_mips_big_vec;
401
252b5132 402const bfd_target aout_mips_little_vec =
e4b17274
NC
403 {
404 "a.out-mips-little", /* name */
405 bfd_target_aout_flavour,
406 BFD_ENDIAN_LITTLE, /* target byte order (little) */
407 BFD_ENDIAN_LITTLE, /* target headers byte order (little) */
408 (HAS_RELOC | EXEC_P | /* object flags */
409 HAS_LINENO | HAS_DEBUG |
410 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
411 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
412 MY_symbol_leading_char,
413 ' ', /* ar_pad_char */
414 15, /* ar_max_namelen */
415 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
416 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
417 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
418 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
419 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
420 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
252b5132 421 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
e4b17274 422 bfd_generic_archive_p, MY_core_file_p},
252b5132 423 {bfd_false, MY_mkobject, /* bfd_set_format */
e4b17274 424 _bfd_generic_mkarchive, bfd_false},
252b5132 425 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
e4b17274 426 _bfd_write_archive_contents, bfd_false},
252b5132 427
e4b17274
NC
428 BFD_JUMP_TABLE_GENERIC (MY),
429 BFD_JUMP_TABLE_COPY (MY),
430 BFD_JUMP_TABLE_CORE (MY),
431 BFD_JUMP_TABLE_ARCHIVE (MY),
432 BFD_JUMP_TABLE_SYMBOLS (MY),
433 BFD_JUMP_TABLE_RELOCS (MY),
434 BFD_JUMP_TABLE_WRITE (MY),
435 BFD_JUMP_TABLE_LINK (MY),
436 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
252b5132 437
e4b17274 438 & aout_mips_big_vec,
1518639e 439
e4b17274
NC
440 (PTR) MY_backend_data
441 };
252b5132
RH
442
443const bfd_target aout_mips_big_vec =
e4b17274
NC
444 {
445 "a.out-mips-big", /* name */
446 bfd_target_aout_flavour,
447 BFD_ENDIAN_BIG, /* target byte order (big) */
448 BFD_ENDIAN_BIG, /* target headers byte order (big) */
449 (HAS_RELOC | EXEC_P | /* object flags */
450 HAS_LINENO | HAS_DEBUG |
451 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
452 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
453 MY_symbol_leading_char,
454 ' ', /* ar_pad_char */
455 15, /* ar_max_namelen */
456 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
457 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
458 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
459 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
460 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
461 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
252b5132 462 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
e4b17274 463 bfd_generic_archive_p, MY_core_file_p},
252b5132 464 {bfd_false, MY_mkobject, /* bfd_set_format */
e4b17274 465 _bfd_generic_mkarchive, bfd_false},
252b5132 466 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
e4b17274
NC
467 _bfd_write_archive_contents, bfd_false},
468
469 BFD_JUMP_TABLE_GENERIC (MY),
470 BFD_JUMP_TABLE_COPY (MY),
471 BFD_JUMP_TABLE_CORE (MY),
472 BFD_JUMP_TABLE_ARCHIVE (MY),
473 BFD_JUMP_TABLE_SYMBOLS (MY),
474 BFD_JUMP_TABLE_RELOCS (MY),
475 BFD_JUMP_TABLE_WRITE (MY),
476 BFD_JUMP_TABLE_LINK (MY),
477 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
478
479 & aout_mips_little_vec,
480
481 (PTR) MY_backend_data
482 };
This page took 0.397518 seconds and 4 git commands to generate.