1 /* BFD back-end for PPCbug boot records.
2 Copyright 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 Written by Michael Meissner, Cygnus Support, <meissner@cygnus.com>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This is a BFD backend which may be used to write PowerPCBug boot objects.
22 It may only be used for output, not input. The intention is that this may
23 be used as an output format for objcopy in order to generate raw binary
26 This is very simple. The only complication is that the real data
27 will start at some address X, and in some cases we will not want to
28 include X zeroes just to get to that point. Since the start
29 address is not meaningful for this object file format, we use it
30 instead to indicate the number of zeroes to skip at the start of
31 the file. objcopy cooperates by specially setting the start
32 address to zero by default. */
40 /* PPCbug location structure */
41 typedef struct ppcboot_location
{
48 /* PPCbug partition table layout */
49 typedef struct ppcboot_partition
{
50 ppcboot_location_t partition_begin
; /* partition begin */
51 ppcboot_location_t partition_end
; /* partition end */
52 bfd_byte sector_begin
[4]; /* 32-bit start RBA (zero-based), little endian */
53 bfd_byte sector_length
[4]; /* 32-bit RBA count (one-based), little endian */
54 } ppcboot_partition_t
;
56 /* PPCbug boot layout. */
57 typedef struct ppcboot_hdr
{
58 bfd_byte pc_compatibility
[446]; /* x86 instruction field */
59 ppcboot_partition_t partition
[4]; /* partition information */
60 bfd_byte signature
[2]; /* 0x55 and 0xaa */
61 bfd_byte entry_offset
[4]; /* entry point offset, little endian */
62 bfd_byte length
[4]; /* load image length, little endian */
63 bfd_byte flags
; /* flag field */
64 bfd_byte os_id
; /* OS_ID */
65 char partition_name
[32]; /* partition name */
66 bfd_byte reserved1
[470]; /* reserved */
69 __attribute__ ((packed
))
73 /* Signature bytes for last 2 bytes of the 512 byte record */
74 #define SIGNATURE0 0x55
75 #define SIGNATURE1 0xaa
77 /* PowerPC boot type */
80 /* Information needed for ppcboot header */
81 typedef struct ppcboot_data
{
82 ppcboot_hdr_t header
; /* raw header */
83 asection
*sec
; /* single section */
86 /* Any bfd we create by reading a ppcboot file has three symbols:
87 a start symbol, an end symbol, and an absolute length symbol. */
88 #define PPCBOOT_SYMS 3
90 static boolean ppcboot_mkobject
PARAMS ((bfd
*));
91 static const bfd_target
*ppcboot_object_p
PARAMS ((bfd
*));
92 static boolean ppcboot_set_arch_mach
93 PARAMS ((bfd
*, enum bfd_architecture
, unsigned long));
94 static boolean ppcboot_get_section_contents
95 PARAMS ((bfd
*, asection
*, PTR
, file_ptr
, bfd_size_type
));
96 static long ppcboot_get_symtab_upper_bound
PARAMS ((bfd
*));
97 static char *mangle_name
PARAMS ((bfd
*, char *));
98 static long ppcboot_get_symtab
PARAMS ((bfd
*, asymbol
**));
99 static asymbol
*ppcboot_make_empty_symbol
PARAMS ((bfd
*));
100 static void ppcboot_get_symbol_info
PARAMS ((bfd
*, asymbol
*, symbol_info
*));
101 static boolean ppcboot_set_section_contents
102 PARAMS ((bfd
*, asection
*, PTR
, file_ptr
, bfd_size_type
));
103 static int ppcboot_sizeof_headers
PARAMS ((bfd
*, boolean
));
104 static boolean ppcboot_bfd_print_private_bfd_data
PARAMS ((bfd
*, PTR
));
106 #define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (PTR) (ptr))
107 #define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
109 /* Create a ppcboot object. Invoked via bfd_set_format. */
112 ppcboot_mkobject (abfd
)
115 if (!ppcboot_get_tdata (abfd
))
116 ppcboot_set_tdata (abfd
, bfd_zalloc (abfd
, sizeof (ppcboot_data_t
)));
122 /* Set the architecture to PowerPC */
124 ppcboot_set_arch_mach (abfd
, arch
, machine
)
126 enum bfd_architecture arch
;
127 unsigned long machine
;
129 if (arch
== bfd_arch_unknown
)
130 arch
= bfd_arch_powerpc
;
132 else if (arch
!= bfd_arch_powerpc
)
135 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
139 /* Any file may be considered to be a ppcboot file, provided the target
140 was not defaulted. That is, it must be explicitly specified as
143 static const bfd_target
*
144 ppcboot_object_p (abfd
)
151 ppcboot_data_t
*tdata
;
153 BFD_ASSERT (sizeof (ppcboot_hdr_t
) == 1024);
155 if (abfd
->target_defaulted
)
157 bfd_set_error (bfd_error_wrong_format
);
161 /* Find the file size. */
162 if (bfd_stat (abfd
, &statbuf
) < 0)
164 bfd_set_error (bfd_error_system_call
);
168 if ((size_t) statbuf
.st_size
< sizeof (ppcboot_hdr_t
))
170 bfd_set_error (bfd_error_wrong_format
);
174 if (bfd_read ((PTR
) &hdr
, sizeof (hdr
), 1, abfd
) != sizeof (hdr
))
176 if (bfd_get_error () != bfd_error_system_call
)
177 bfd_set_error (bfd_error_wrong_format
);
182 /* Now do some basic checks. */
183 for (i
= 0; i
< sizeof (hdr
.pc_compatibility
); i
++)
184 if (hdr
.pc_compatibility
[i
])
186 bfd_set_error (bfd_error_wrong_format
);
190 if (hdr
.signature
[0] != SIGNATURE0
|| hdr
.signature
[1] != SIGNATURE1
)
192 bfd_set_error (bfd_error_wrong_format
);
196 if (hdr
.partition
[0].partition_end
.ind
!= PPC_IND
)
198 bfd_set_error (bfd_error_wrong_format
);
202 abfd
->symcount
= PPCBOOT_SYMS
;
204 /* One data section. */
205 sec
= bfd_make_section (abfd
, ".data");
208 sec
->flags
= SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_CODE
| SEC_HAS_CONTENTS
;
210 sec
->_raw_size
= statbuf
.st_size
- sizeof (ppcboot_hdr_t
);
211 sec
->filepos
= sizeof (ppcboot_hdr_t
);
213 ppcboot_mkobject (abfd
);
214 tdata
= ppcboot_get_tdata (abfd
);
216 memcpy ((PTR
) &tdata
->header
, (PTR
) &hdr
, sizeof (ppcboot_hdr_t
));
218 ppcboot_set_arch_mach (abfd
, bfd_arch_powerpc
, 0);
222 #define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
223 #define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
224 #define ppcboot_new_section_hook _bfd_generic_new_section_hook
227 /* Get contents of the only section. */
230 ppcboot_get_section_contents (abfd
, section
, location
, offset
, count
)
232 asection
*section ATTRIBUTE_UNUSED
;
237 if (bfd_seek (abfd
, offset
+ sizeof (ppcboot_hdr_t
), SEEK_SET
) != 0
238 || bfd_read (location
, 1, count
, abfd
) != count
)
244 /* Return the amount of memory needed to read the symbol table. */
247 ppcboot_get_symtab_upper_bound (abfd
)
248 bfd
*abfd ATTRIBUTE_UNUSED
;
250 return (PPCBOOT_SYMS
+ 1) * sizeof (asymbol
*);
254 /* Create a symbol name based on the bfd's filename. */
257 mangle_name (abfd
, suffix
)
265 size
= (strlen (bfd_get_filename (abfd
))
267 + sizeof "_ppcboot__");
269 buf
= (char *) bfd_alloc (abfd
, size
);
273 sprintf (buf
, "_ppcboot_%s_%s", bfd_get_filename (abfd
), suffix
);
275 /* Change any non-alphanumeric characters to underscores. */
276 for (p
= buf
; *p
; p
++)
277 if (! isalnum ((unsigned char) *p
))
284 /* Return the symbol table. */
287 ppcboot_get_symtab (abfd
, alocation
)
291 asection
*sec
= ppcboot_get_tdata (abfd
)->sec
;
295 syms
= (asymbol
*) bfd_alloc (abfd
, PPCBOOT_SYMS
* sizeof (asymbol
));
300 syms
[0].the_bfd
= abfd
;
301 syms
[0].name
= mangle_name (abfd
, "start");
303 syms
[0].flags
= BSF_GLOBAL
;
304 syms
[0].section
= sec
;
305 syms
[0].udata
.p
= NULL
;
308 syms
[1].the_bfd
= abfd
;
309 syms
[1].name
= mangle_name (abfd
, "end");
310 syms
[1].value
= sec
->_raw_size
;
311 syms
[1].flags
= BSF_GLOBAL
;
312 syms
[1].section
= sec
;
313 syms
[1].udata
.p
= NULL
;
316 syms
[2].the_bfd
= abfd
;
317 syms
[2].name
= mangle_name (abfd
, "size");
318 syms
[2].value
= sec
->_raw_size
;
319 syms
[2].flags
= BSF_GLOBAL
;
320 syms
[2].section
= bfd_abs_section_ptr
;
321 syms
[2].udata
.p
= NULL
;
323 for (i
= 0; i
< PPCBOOT_SYMS
; i
++)
324 *alocation
++ = syms
++;
331 /* Make an empty symbol. */
334 ppcboot_make_empty_symbol (abfd
)
337 return (asymbol
*) bfd_alloc (abfd
, sizeof (asymbol
));
341 #define ppcboot_print_symbol _bfd_nosymbols_print_symbol
343 /* Get information about a symbol. */
346 ppcboot_get_symbol_info (ignore_abfd
, symbol
, ret
)
347 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
351 bfd_symbol_info (symbol
, ret
);
354 #define ppcboot_bfd_is_local_label_name bfd_generic_is_local_label_name
355 #define ppcboot_get_lineno _bfd_nosymbols_get_lineno
356 #define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
357 #define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
358 #define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
359 #define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
361 #define ppcboot_get_reloc_upper_bound \
362 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
363 #define ppcboot_canonicalize_reloc \
364 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
365 #define ppcboot_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
367 /* Write section contents of a ppcboot file. */
370 ppcboot_set_section_contents (abfd
, sec
, data
, offset
, size
)
377 if (! abfd
->output_has_begun
)
382 /* The lowest section VMA sets the virtual address of the start
383 of the file. We use the set the file position of all the
385 low
= abfd
->sections
->vma
;
386 for (s
= abfd
->sections
->next
; s
!= NULL
; s
= s
->next
)
390 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
391 s
->filepos
= s
->vma
- low
;
393 abfd
->output_has_begun
= true;
396 return _bfd_generic_set_section_contents (abfd
, sec
, data
, offset
, size
);
401 ppcboot_sizeof_headers (abfd
, exec
)
402 bfd
*abfd ATTRIBUTE_UNUSED
;
403 boolean exec ATTRIBUTE_UNUSED
;
405 return sizeof (ppcboot_hdr_t
);
409 /* Print out the program headers. */
412 ppcboot_bfd_print_private_bfd_data (abfd
, farg
)
416 FILE *f
= (FILE *)farg
;
417 ppcboot_data_t
*tdata
= ppcboot_get_tdata (abfd
);
418 long entry_offset
= bfd_getl_signed_32 ((PTR
) tdata
->header
.entry_offset
);
419 long length
= bfd_getl_signed_32 ((PTR
) tdata
->header
.length
);
422 fprintf (f
, _("\nppcboot header:\n"));
423 fprintf (f
, _("Entry offset = 0x%.8lx (%ld)\n"), entry_offset
, entry_offset
);
424 fprintf (f
, _("Length = 0x%.8lx (%ld)\n"), length
, length
);
426 if (tdata
->header
.flags
)
427 fprintf (f
, _("Flag field = 0x%.2x\n"), tdata
->header
.flags
);
429 if (tdata
->header
.os_id
)
430 fprintf (f
, "OS_ID = 0x%.2x\n", tdata
->header
.os_id
);
432 if (tdata
->header
.partition_name
)
433 fprintf (f
, _("Partition name = \"%s\"\n"), tdata
->header
.partition_name
);
435 for (i
= 0; i
< 4; i
++)
437 long sector_begin
= bfd_getl_signed_32 ((PTR
) tdata
->header
.partition
[i
].sector_begin
);
438 long sector_length
= bfd_getl_signed_32 ((PTR
) tdata
->header
.partition
[i
].sector_length
);
440 /* Skip all 0 entries */
441 if (!tdata
->header
.partition
[i
].partition_begin
.ind
442 && !tdata
->header
.partition
[i
].partition_begin
.head
443 && !tdata
->header
.partition
[i
].partition_begin
.sector
444 && !tdata
->header
.partition
[i
].partition_begin
.cylinder
445 && !tdata
->header
.partition
[i
].partition_end
.ind
446 && !tdata
->header
.partition
[i
].partition_end
.head
447 && !tdata
->header
.partition
[i
].partition_end
.sector
448 && !tdata
->header
.partition
[i
].partition_end
.cylinder
449 && !sector_begin
&& !sector_length
)
452 fprintf (f
, _("\nPartition[%d] start = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i
,
453 tdata
->header
.partition
[i
].partition_begin
.ind
,
454 tdata
->header
.partition
[i
].partition_begin
.head
,
455 tdata
->header
.partition
[i
].partition_begin
.sector
,
456 tdata
->header
.partition
[i
].partition_begin
.cylinder
);
458 fprintf (f
, _("Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i
,
459 tdata
->header
.partition
[i
].partition_end
.ind
,
460 tdata
->header
.partition
[i
].partition_end
.head
,
461 tdata
->header
.partition
[i
].partition_end
.sector
,
462 tdata
->header
.partition
[i
].partition_end
.cylinder
);
464 fprintf (f
, _("Partition[%d] sector = 0x%.8lx (%ld)\n"), i
, sector_begin
, sector_begin
);
465 fprintf (f
, _("Partition[%d] length = 0x%.8lx (%ld)\n"), i
, sector_length
, sector_length
);
473 #define ppcboot_bfd_get_relocated_section_contents \
474 bfd_generic_get_relocated_section_contents
475 #define ppcboot_bfd_relax_section bfd_generic_relax_section
476 #define ppcboot_bfd_gc_sections bfd_generic_gc_sections
477 #define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
478 #define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
479 #define ppcboot_bfd_final_link _bfd_generic_final_link
480 #define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
481 #define ppcboot_get_section_contents_in_window \
482 _bfd_generic_get_section_contents_in_window
484 #define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
485 #define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
486 #define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
487 #define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
488 #define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
489 #define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
491 const bfd_target ppcboot_vec
=
493 "ppcboot", /* name */
494 bfd_target_unknown_flavour
, /* flavour */
495 BFD_ENDIAN_BIG
, /* byteorder is big endian for code */
496 BFD_ENDIAN_LITTLE
, /* header_byteorder */
497 EXEC_P
, /* object_flags */
498 (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
| SEC_DATA
499 | SEC_ROM
| SEC_HAS_CONTENTS
), /* section_flags */
500 0, /* symbol_leading_char */
501 ' ', /* ar_pad_char */
502 16, /* ar_max_namelen */
503 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
504 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
505 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
506 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
507 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
508 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
509 { /* bfd_check_format */
511 ppcboot_object_p
, /* bfd_check_format */
515 { /* bfd_set_format */
521 { /* bfd_write_contents */
528 BFD_JUMP_TABLE_GENERIC (ppcboot
),
529 BFD_JUMP_TABLE_COPY (ppcboot
),
530 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
531 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
532 BFD_JUMP_TABLE_SYMBOLS (ppcboot
),
533 BFD_JUMP_TABLE_RELOCS (ppcboot
),
534 BFD_JUMP_TABLE_WRITE (ppcboot
),
535 BFD_JUMP_TABLE_LINK (ppcboot
),
536 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),