1 /* BFD back-end for PPCbug boot records.
2 Copyright 1996 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 /* Signature bytes for last 2 bytes of the 512 byte record */
70 #define SIGNATURE0 0x55
71 #define SIGNATURE1 0xaa
73 /* Information needed for ppcboot header */
74 typedef struct ppcboot_data
{
75 ppcboot_hdr_t header
; /* raw header */
76 asection
*sec
; /* single section */
79 /* Any bfd we create by reading a ppcboot file has three symbols:
80 a start symbol, an end symbol, and an absolute length symbol. */
81 #define PPCBOOT_SYMS 3
83 static boolean ppcboot_mkobject
PARAMS ((bfd
*));
84 static const bfd_target
*ppcboot_object_p
PARAMS ((bfd
*));
85 static boolean ppcboot_get_section_contents
86 PARAMS ((bfd
*, asection
*, PTR
, file_ptr
, bfd_size_type
));
87 static long ppcboot_get_symtab_upper_bound
PARAMS ((bfd
*));
88 static char *mangle_name
PARAMS ((bfd
*, char *));
89 static long ppcboot_get_symtab
PARAMS ((bfd
*, asymbol
**));
90 static asymbol
*ppcboot_make_empty_symbol
PARAMS ((bfd
*));
91 static void ppcboot_get_symbol_info
PARAMS ((bfd
*, asymbol
*, symbol_info
*));
92 static boolean ppcboot_set_section_contents
93 PARAMS ((bfd
*, asection
*, PTR
, file_ptr
, bfd_size_type
));
94 static int ppcboot_sizeof_headers
PARAMS ((bfd
*, boolean
));
96 #define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (PTR) (ptr))
97 #define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
99 /* Create a ppcboot object. Invoked via bfd_set_format. */
102 ppcboot_mkobject (abfd
)
105 if (!ppcboot_get_tdata (abfd
))
106 ppcboot_set_tdata (abfd
, bfd_zalloc (abfd
, sizeof (ppcboot_data_t
)));
112 /* Set the architecture to PowerPC */
114 ppcboot_set_arch_mach (abfd
, arch
, machine
)
116 enum bfd_architecture arch
;
117 unsigned long machine
;
119 if (arch
== bfd_arch_unknown
)
120 arch
= bfd_arch_powerpc
;
122 else if (arch
!= bfd_arch_powerpc
)
125 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
129 /* Any file may be considered to be a ppcboot file, provided the target
130 was not defaulted. That is, it must be explicitly specified as
133 static const bfd_target
*
134 ppcboot_object_p (abfd
)
141 ppcboot_data_t
*tdata
;
143 if (sizeof (ppcboot_hdr_t
) != 1024)
144 fatal ("ppcboot_hdr_t is not 1024 bytes in size");
146 if (abfd
->target_defaulted
)
148 bfd_set_error (bfd_error_wrong_format
);
152 abfd
->symcount
= PPCBOOT_SYMS
;
154 /* Find the file size. */
155 if (bfd_stat (abfd
, &statbuf
) < 0)
157 bfd_set_error (bfd_error_system_call
);
161 if (statbuf
.st_size
< sizeof (ppcboot_hdr_t
))
163 bfd_set_error (bfd_error_wrong_format
);
167 if (bfd_read ((PTR
) &hdr
, sizeof (hdr
), 1, abfd
) != sizeof (hdr
))
169 if (bfd_get_error () != bfd_error_system_call
)
170 bfd_set_error (bfd_error_wrong_format
);
175 /* Now do some basic checks. */
176 for (i
= 0; i
< sizeof (hdr
.pc_compatibility
); i
++)
177 if (hdr
.pc_compatibility
[i
])
179 bfd_set_error (bfd_error_wrong_format
);
183 if (hdr
.signature
[0] != SIGNATURE0
|| hdr
.signature
[1] != SIGNATURE1
)
185 bfd_set_error (bfd_error_wrong_format
);
189 /* One data section. */
190 sec
= bfd_make_section (abfd
, ".data");
193 sec
->flags
= SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_CODE
| SEC_HAS_CONTENTS
;
195 sec
->_raw_size
= statbuf
.st_size
- sizeof (ppcboot_hdr_t
);
196 sec
->filepos
= sizeof (ppcboot_hdr_t
);
198 ppcboot_mkobject (abfd
);
199 tdata
= ppcboot_get_tdata (abfd
);
201 memcpy ((PTR
) &tdata
->header
, (PTR
) &hdr
, sizeof (ppcboot_hdr_t
));
203 ppcboot_set_arch_mach (abfd
, bfd_arch_powerpc
, 0);
207 #define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
208 #define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
209 #define ppcboot_new_section_hook _bfd_generic_new_section_hook
212 /* Get contents of the only section. */
215 ppcboot_get_section_contents (abfd
, section
, location
, offset
, count
)
222 if (bfd_seek (abfd
, offset
+ sizeof(ppcboot_hdr_t
), SEEK_SET
) != 0
223 || bfd_read (location
, 1, count
, abfd
) != count
)
229 /* Return the amount of memory needed to read the symbol table. */
232 ppcboot_get_symtab_upper_bound (abfd
)
235 return (PPCBOOT_SYMS
+ 1) * sizeof (asymbol
*);
239 /* Create a symbol name based on the bfd's filename. */
242 mangle_name (abfd
, suffix
)
250 size
= (strlen (bfd_get_filename (abfd
))
252 + sizeof "_ppcboot__");
254 buf
= (char *) bfd_alloc (abfd
, size
);
258 sprintf (buf
, "_ppcboot_%s_%s", bfd_get_filename (abfd
), suffix
);
260 /* Change any non-alphanumeric characters to underscores. */
261 for (p
= buf
; *p
; p
++)
269 /* Return the symbol table. */
272 ppcboot_get_symtab (abfd
, alocation
)
276 asection
*sec
= ppcboot_get_tdata (abfd
)->sec
;
280 syms
= (asymbol
*) bfd_alloc (abfd
, PPCBOOT_SYMS
* sizeof (asymbol
));
285 syms
[0].the_bfd
= abfd
;
286 syms
[0].name
= mangle_name (abfd
, "start");
288 syms
[0].flags
= BSF_GLOBAL
;
289 syms
[0].section
= sec
;
290 syms
[0].udata
.p
= NULL
;
293 syms
[1].the_bfd
= abfd
;
294 syms
[1].name
= mangle_name (abfd
, "end");
295 syms
[1].value
= sec
->_raw_size
;
296 syms
[1].flags
= BSF_GLOBAL
;
297 syms
[1].section
= sec
;
298 syms
[1].udata
.p
= NULL
;
301 syms
[2].the_bfd
= abfd
;
302 syms
[2].name
= mangle_name (abfd
, "size");
303 syms
[2].value
= sec
->_raw_size
;
304 syms
[2].flags
= BSF_GLOBAL
;
305 syms
[2].section
= bfd_abs_section_ptr
;
306 syms
[2].udata
.p
= NULL
;
308 for (i
= 0; i
< PPCBOOT_SYMS
; i
++)
309 *alocation
++ = syms
++;
316 /* Make an empty symbol. */
319 ppcboot_make_empty_symbol (abfd
)
322 return (asymbol
*) bfd_alloc (abfd
, sizeof (asymbol
));
326 #define ppcboot_print_symbol _bfd_nosymbols_print_symbol
328 /* Get information about a symbol. */
331 ppcboot_get_symbol_info (ignore_abfd
, symbol
, ret
)
336 bfd_symbol_info (symbol
, ret
);
339 #define ppcboot_bfd_is_local_label bfd_generic_is_local_label
340 #define ppcboot_get_lineno _bfd_nosymbols_get_lineno
341 #define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
342 #define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
343 #define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
344 #define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
346 #define ppcboot_get_reloc_upper_bound \
347 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
348 #define ppcboot_canonicalize_reloc \
349 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
350 #define ppcboot_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
352 /* Set the architecture of a ppcboot file. */
353 #define ppcboot_set_arch_mach ppcboot_set_arch_mach
356 /* Write section contents of a ppcboot file. */
359 ppcboot_set_section_contents (abfd
, sec
, data
, offset
, size
)
366 if (! abfd
->output_has_begun
)
371 /* The lowest section VMA sets the virtual address of the start
372 of the file. We use the set the file position of all the
374 low
= abfd
->sections
->vma
;
375 for (s
= abfd
->sections
->next
; s
!= NULL
; s
= s
->next
)
379 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
380 s
->filepos
= s
->vma
- low
;
382 abfd
->output_has_begun
= true;
385 return _bfd_generic_set_section_contents (abfd
, sec
, data
, offset
, size
);
390 ppcboot_sizeof_headers (abfd
, exec
)
394 return sizeof (ppcboot_hdr_t
);
398 /* Print out the program headers. */
401 ppcboot_bfd_print_private_bfd_data (abfd
, farg
)
405 FILE *f
= (FILE *)farg
;
406 ppcboot_data_t
*tdata
= ppcboot_get_tdata (abfd
);
407 long entry_offset
= bfd_getl_signed_32 ((PTR
) &tdata
->header
.entry_offset
);
408 long length
= bfd_getl_signed_32 ((PTR
) &tdata
->header
.length
);
411 fprintf (f
, "\nppcboot header:\n");
412 fprintf (f
, "Entry offset = 0x%.8lx (%ld)\n", entry_offset
, entry_offset
);
413 fprintf (f
, "Length = 0x%.8lx (%ld)\n", length
, length
);
415 if (tdata
->header
.flags
)
416 fprintf (f
, "Flag field = 0x%.2x\n", tdata
->header
.flags
);
418 if (tdata
->header
.os_id
)
419 fprintf (f
, "OS_ID = 0x%.2x\n", tdata
->header
.os_id
);
421 if (tdata
->header
.partition_name
)
422 fprintf (f
, "Partition name = %s\n", tdata
->header
.partition_name
);
424 for (i
= 0; i
< 4; i
++)
426 long sector_begin
= bfd_getl_signed_32 ((PTR
) &tdata
->header
.partition
[i
].sector_begin
);
427 long sector_length
= bfd_getl_signed_32 ((PTR
) &tdata
->header
.partition
[i
].sector_length
);
429 /* Skip all 0 entries */
430 if (!tdata
->header
.partition
[i
].partition_begin
.ind
431 && !tdata
->header
.partition
[i
].partition_begin
.head
432 && !tdata
->header
.partition
[i
].partition_begin
.sector
433 && !tdata
->header
.partition
[i
].partition_begin
.cylinder
434 && !tdata
->header
.partition
[i
].partition_end
.ind
435 && !tdata
->header
.partition
[i
].partition_end
.head
436 && !tdata
->header
.partition
[i
].partition_end
.sector
437 && !tdata
->header
.partition
[i
].partition_end
.cylinder
438 && !sector_begin
&& !sector_length
)
441 fprintf (f
, "\nPartition[%d] start = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n", i
,
442 tdata
->header
.partition
[i
].partition_begin
.ind
,
443 tdata
->header
.partition
[i
].partition_begin
.head
,
444 tdata
->header
.partition
[i
].partition_begin
.sector
,
445 tdata
->header
.partition
[i
].partition_begin
.cylinder
);
447 fprintf (f
, "Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n", i
,
448 tdata
->header
.partition
[i
].partition_end
.ind
,
449 tdata
->header
.partition
[i
].partition_end
.head
,
450 tdata
->header
.partition
[i
].partition_end
.sector
,
451 tdata
->header
.partition
[i
].partition_end
.cylinder
);
453 fprintf (f
, "Partition[%d] sector = 0x%.8lx (%ld)\n", i
, sector_begin
, sector_begin
);
454 fprintf (f
, "Partition[%d] length = 0x%.8lx (%ld)\n", i
, sector_length
, sector_length
);
462 #define ppcboot_bfd_get_relocated_section_contents \
463 bfd_generic_get_relocated_section_contents
464 #define ppcboot_bfd_relax_section bfd_generic_relax_section
465 #define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
466 #define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
467 #define ppcboot_bfd_final_link _bfd_generic_final_link
468 #define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
469 #define ppcboot_get_section_contents_in_window \
470 _bfd_generic_get_section_contents_in_window
472 #define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
473 #define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
474 #define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
475 #define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
476 #define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
477 #define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
479 const bfd_target ppcboot_vec
=
481 "ppcboot", /* name */
482 bfd_target_unknown_flavour
, /* flavour */
483 BFD_ENDIAN_BIG
, /* byteorder is big endian for code */
484 BFD_ENDIAN_LITTLE
, /* header_byteorder */
485 EXEC_P
, /* object_flags */
486 (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
| SEC_DATA
487 | SEC_ROM
| SEC_HAS_CONTENTS
), /* section_flags */
488 0, /* symbol_leading_char */
489 ' ', /* ar_pad_char */
490 16, /* ar_max_namelen */
491 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
492 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
493 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
494 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
495 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
496 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
497 { /* bfd_check_format */
499 ppcboot_object_p
, /* bfd_check_format */
503 { /* bfd_set_format */
509 { /* bfd_write_contents */
516 BFD_JUMP_TABLE_GENERIC (ppcboot
),
517 BFD_JUMP_TABLE_COPY (ppcboot
),
518 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
519 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
520 BFD_JUMP_TABLE_SYMBOLS (ppcboot
),
521 BFD_JUMP_TABLE_RELOCS (ppcboot
),
522 BFD_JUMP_TABLE_WRITE (ppcboot
),
523 BFD_JUMP_TABLE_LINK (ppcboot
),
524 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),