* emultempl/elf32.em: For SEC_EXCLUDE sections, ensure that
[binutils.git] / bfd / ppcboot.c
blobeccbe7980cba546d676cec9d089ba3eb48b2c09d
1 /* BFD back-end for PPCbug boot records.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Written by Michael Meissner, Cygnus Support, <meissner@cygnus.com>
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This is a BFD backend which may be used to write PowerPCBug boot objects.
23 It may only be used for output, not input. The intention is that this may
24 be used as an output format for objcopy in order to generate raw binary
25 data.
27 This is very simple. The only complication is that the real data
28 will start at some address X, and in some cases we will not want to
29 include X zeroes just to get to that point. Since the start
30 address is not meaningful for this object file format, we use it
31 instead to indicate the number of zeroes to skip at the start of
32 the file. objcopy cooperates by specially setting the start
33 address to zero by default. */
35 #include <ctype.h>
37 #include "bfd.h"
38 #include "sysdep.h"
39 #include "libbfd.h"
41 /* PPCbug location structure */
42 typedef struct ppcboot_location {
43 bfd_byte ind;
44 bfd_byte head;
45 bfd_byte sector;
46 bfd_byte cylinder;
47 } ppcboot_location_t;
49 /* PPCbug partition table layout */
50 typedef struct ppcboot_partition {
51 ppcboot_location_t partition_begin; /* partition begin */
52 ppcboot_location_t partition_end; /* partition end */
53 bfd_byte sector_begin[4]; /* 32-bit start RBA (zero-based), little endian */
54 bfd_byte sector_length[4]; /* 32-bit RBA count (one-based), little endian */
55 } ppcboot_partition_t;
57 /* PPCbug boot layout. */
58 typedef struct ppcboot_hdr {
59 bfd_byte pc_compatibility[446]; /* x86 instruction field */
60 ppcboot_partition_t partition[4]; /* partition information */
61 bfd_byte signature[2]; /* 0x55 and 0xaa */
62 bfd_byte entry_offset[4]; /* entry point offset, little endian */
63 bfd_byte length[4]; /* load image length, little endian */
64 bfd_byte flags; /* flag field */
65 bfd_byte os_id; /* OS_ID */
66 char partition_name[32]; /* partition name */
67 bfd_byte reserved1[470]; /* reserved */
69 #ifdef __GNUC__
70 __attribute__ ((packed))
71 #endif
72 ppcboot_hdr_t;
74 /* Signature bytes for last 2 bytes of the 512 byte record */
75 #define SIGNATURE0 0x55
76 #define SIGNATURE1 0xaa
78 /* PowerPC boot type */
79 #define PPC_IND 0x41
81 /* Information needed for ppcboot header */
82 typedef struct ppcboot_data {
83 ppcboot_hdr_t header; /* raw header */
84 asection *sec; /* single section */
85 } ppcboot_data_t;
87 /* Any bfd we create by reading a ppcboot file has three symbols:
88 a start symbol, an end symbol, and an absolute length symbol. */
89 #define PPCBOOT_SYMS 3
91 static boolean ppcboot_mkobject PARAMS ((bfd *));
92 static const bfd_target *ppcboot_object_p PARAMS ((bfd *));
93 static boolean ppcboot_set_arch_mach
94 PARAMS ((bfd *, enum bfd_architecture, unsigned long));
95 static boolean ppcboot_get_section_contents
96 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
97 static long ppcboot_get_symtab_upper_bound PARAMS ((bfd *));
98 static char *mangle_name PARAMS ((bfd *, char *));
99 static long ppcboot_get_symtab PARAMS ((bfd *, asymbol **));
100 static asymbol *ppcboot_make_empty_symbol PARAMS ((bfd *));
101 static void ppcboot_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
102 static boolean ppcboot_set_section_contents
103 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
104 static int ppcboot_sizeof_headers PARAMS ((bfd *, boolean));
105 static boolean ppcboot_bfd_print_private_bfd_data PARAMS ((bfd *, PTR));
107 #define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (PTR) (ptr))
108 #define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
110 /* Create a ppcboot object. Invoked via bfd_set_format. */
112 static boolean
113 ppcboot_mkobject (abfd)
114 bfd *abfd;
116 if (!ppcboot_get_tdata (abfd))
117 ppcboot_set_tdata (abfd, bfd_zalloc (abfd, sizeof (ppcboot_data_t)));
119 return true;
123 /* Set the architecture to PowerPC */
124 static boolean
125 ppcboot_set_arch_mach (abfd, arch, machine)
126 bfd *abfd;
127 enum bfd_architecture arch;
128 unsigned long machine;
130 if (arch == bfd_arch_unknown)
131 arch = bfd_arch_powerpc;
133 else if (arch != bfd_arch_powerpc)
134 return false;
136 return bfd_default_set_arch_mach (abfd, arch, machine);
140 /* Any file may be considered to be a ppcboot file, provided the target
141 was not defaulted. That is, it must be explicitly specified as
142 being ppcboot. */
144 static const bfd_target *
145 ppcboot_object_p (abfd)
146 bfd *abfd;
148 struct stat statbuf;
149 asection *sec;
150 ppcboot_hdr_t hdr;
151 size_t i;
152 ppcboot_data_t *tdata;
154 BFD_ASSERT (sizeof (ppcboot_hdr_t) == 1024);
156 if (abfd->target_defaulted)
158 bfd_set_error (bfd_error_wrong_format);
159 return NULL;
162 /* Find the file size. */
163 if (bfd_stat (abfd, &statbuf) < 0)
165 bfd_set_error (bfd_error_system_call);
166 return NULL;
169 if ((size_t) statbuf.st_size < sizeof (ppcboot_hdr_t))
171 bfd_set_error (bfd_error_wrong_format);
172 return NULL;
175 if (bfd_read ((PTR) &hdr, sizeof (hdr), 1, abfd) != sizeof (hdr))
177 if (bfd_get_error () != bfd_error_system_call)
178 bfd_set_error (bfd_error_wrong_format);
180 return NULL;
183 /* Now do some basic checks. */
184 for (i = 0; i < sizeof (hdr.pc_compatibility); i++)
185 if (hdr.pc_compatibility[i])
187 bfd_set_error (bfd_error_wrong_format);
188 return NULL;
191 if (hdr.signature[0] != SIGNATURE0 || hdr.signature[1] != SIGNATURE1)
193 bfd_set_error (bfd_error_wrong_format);
194 return NULL;
197 if (hdr.partition[0].partition_end.ind != PPC_IND)
199 bfd_set_error (bfd_error_wrong_format);
200 return NULL;
203 abfd->symcount = PPCBOOT_SYMS;
205 /* One data section. */
206 sec = bfd_make_section (abfd, ".data");
207 if (sec == NULL)
208 return NULL;
209 sec->flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_CODE | SEC_HAS_CONTENTS;
210 sec->vma = 0;
211 sec->_raw_size = statbuf.st_size - sizeof (ppcboot_hdr_t);
212 sec->filepos = sizeof (ppcboot_hdr_t);
214 ppcboot_mkobject (abfd);
215 tdata = ppcboot_get_tdata (abfd);
216 tdata->sec = sec;
217 memcpy ((PTR) &tdata->header, (PTR) &hdr, sizeof (ppcboot_hdr_t));
219 ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0);
220 return abfd->xvec;
223 #define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
224 #define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
225 #define ppcboot_new_section_hook _bfd_generic_new_section_hook
228 /* Get contents of the only section. */
230 static boolean
231 ppcboot_get_section_contents (abfd, section, location, offset, count)
232 bfd *abfd;
233 asection *section ATTRIBUTE_UNUSED;
234 PTR location;
235 file_ptr offset;
236 bfd_size_type count;
238 if (bfd_seek (abfd, offset + sizeof (ppcboot_hdr_t), SEEK_SET) != 0
239 || bfd_read (location, 1, count, abfd) != count)
240 return false;
241 return true;
245 /* Return the amount of memory needed to read the symbol table. */
247 static long
248 ppcboot_get_symtab_upper_bound (abfd)
249 bfd *abfd ATTRIBUTE_UNUSED;
251 return (PPCBOOT_SYMS + 1) * sizeof (asymbol *);
255 /* Create a symbol name based on the bfd's filename. */
257 static char *
258 mangle_name (abfd, suffix)
259 bfd *abfd;
260 char *suffix;
262 int size;
263 char *buf;
264 char *p;
266 size = (strlen (bfd_get_filename (abfd))
267 + strlen (suffix)
268 + sizeof "_ppcboot__");
270 buf = (char *) bfd_alloc (abfd, size);
271 if (buf == NULL)
272 return "";
274 sprintf (buf, "_ppcboot_%s_%s", bfd_get_filename (abfd), suffix);
276 /* Change any non-alphanumeric characters to underscores. */
277 for (p = buf; *p; p++)
278 if (! isalnum ((unsigned char) *p))
279 *p = '_';
281 return buf;
285 /* Return the symbol table. */
287 static long
288 ppcboot_get_symtab (abfd, alocation)
289 bfd *abfd;
290 asymbol **alocation;
292 asection *sec = ppcboot_get_tdata (abfd)->sec;
293 asymbol *syms;
294 unsigned int i;
296 syms = (asymbol *) bfd_alloc (abfd, PPCBOOT_SYMS * sizeof (asymbol));
297 if (syms == NULL)
298 return false;
300 /* Start symbol. */
301 syms[0].the_bfd = abfd;
302 syms[0].name = mangle_name (abfd, "start");
303 syms[0].value = 0;
304 syms[0].flags = BSF_GLOBAL;
305 syms[0].section = sec;
306 syms[0].udata.p = NULL;
308 /* End symbol. */
309 syms[1].the_bfd = abfd;
310 syms[1].name = mangle_name (abfd, "end");
311 syms[1].value = sec->_raw_size;
312 syms[1].flags = BSF_GLOBAL;
313 syms[1].section = sec;
314 syms[1].udata.p = NULL;
316 /* Size symbol. */
317 syms[2].the_bfd = abfd;
318 syms[2].name = mangle_name (abfd, "size");
319 syms[2].value = sec->_raw_size;
320 syms[2].flags = BSF_GLOBAL;
321 syms[2].section = bfd_abs_section_ptr;
322 syms[2].udata.p = NULL;
324 for (i = 0; i < PPCBOOT_SYMS; i++)
325 *alocation++ = syms++;
326 *alocation = NULL;
328 return PPCBOOT_SYMS;
332 /* Make an empty symbol. */
334 static asymbol *
335 ppcboot_make_empty_symbol (abfd)
336 bfd *abfd;
338 return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
342 #define ppcboot_print_symbol _bfd_nosymbols_print_symbol
344 /* Get information about a symbol. */
346 static void
347 ppcboot_get_symbol_info (ignore_abfd, symbol, ret)
348 bfd *ignore_abfd ATTRIBUTE_UNUSED;
349 asymbol *symbol;
350 symbol_info *ret;
352 bfd_symbol_info (symbol, ret);
355 #define ppcboot_bfd_is_local_label_name bfd_generic_is_local_label_name
356 #define ppcboot_get_lineno _bfd_nosymbols_get_lineno
357 #define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
358 #define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
359 #define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
360 #define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
362 #define ppcboot_get_reloc_upper_bound \
363 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
364 #define ppcboot_canonicalize_reloc \
365 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
366 #define ppcboot_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
368 /* Write section contents of a ppcboot file. */
370 static boolean
371 ppcboot_set_section_contents (abfd, sec, data, offset, size)
372 bfd *abfd;
373 asection *sec;
374 PTR data;
375 file_ptr offset;
376 bfd_size_type size;
378 if (! abfd->output_has_begun)
380 bfd_vma low;
381 asection *s;
383 /* The lowest section VMA sets the virtual address of the start
384 of the file. We use the set the file position of all the
385 sections. */
386 low = abfd->sections->vma;
387 for (s = abfd->sections->next; s != NULL; s = s->next)
388 if (s->vma < low)
389 low = s->vma;
391 for (s = abfd->sections; s != NULL; s = s->next)
392 s->filepos = s->vma - low;
394 abfd->output_has_begun = true;
397 return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
401 static int
402 ppcboot_sizeof_headers (abfd, exec)
403 bfd *abfd ATTRIBUTE_UNUSED;
404 boolean exec ATTRIBUTE_UNUSED;
406 return sizeof (ppcboot_hdr_t);
410 /* Print out the program headers. */
412 static boolean
413 ppcboot_bfd_print_private_bfd_data (abfd, farg)
414 bfd *abfd;
415 PTR farg;
417 FILE *f = (FILE *)farg;
418 ppcboot_data_t *tdata = ppcboot_get_tdata (abfd);
419 long entry_offset = bfd_getl_signed_32 ((PTR) tdata->header.entry_offset);
420 long length = bfd_getl_signed_32 ((PTR) tdata->header.length);
421 int i;
423 fprintf (f, _("\nppcboot header:\n"));
424 fprintf (f, _("Entry offset = 0x%.8lx (%ld)\n"), entry_offset, entry_offset);
425 fprintf (f, _("Length = 0x%.8lx (%ld)\n"), length, length);
427 if (tdata->header.flags)
428 fprintf (f, _("Flag field = 0x%.2x\n"), tdata->header.flags);
430 if (tdata->header.os_id)
431 fprintf (f, "OS_ID = 0x%.2x\n", tdata->header.os_id);
433 if (tdata->header.partition_name)
434 fprintf (f, _("Partition name = \"%s\"\n"), tdata->header.partition_name);
436 for (i = 0; i < 4; i++)
438 long sector_begin = bfd_getl_signed_32 ((PTR) tdata->header.partition[i].sector_begin);
439 long sector_length = bfd_getl_signed_32 ((PTR) tdata->header.partition[i].sector_length);
441 /* Skip all 0 entries */
442 if (!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
446 && !tdata->header.partition[i].partition_end.ind
447 && !tdata->header.partition[i].partition_end.head
448 && !tdata->header.partition[i].partition_end.sector
449 && !tdata->header.partition[i].partition_end.cylinder
450 && !sector_begin && !sector_length)
451 continue;
453 fprintf (f, _("\nPartition[%d] start = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
454 tdata->header.partition[i].partition_begin.ind,
455 tdata->header.partition[i].partition_begin.head,
456 tdata->header.partition[i].partition_begin.sector,
457 tdata->header.partition[i].partition_begin.cylinder);
459 fprintf (f, _("Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
460 tdata->header.partition[i].partition_end.ind,
461 tdata->header.partition[i].partition_end.head,
462 tdata->header.partition[i].partition_end.sector,
463 tdata->header.partition[i].partition_end.cylinder);
465 fprintf (f, _("Partition[%d] sector = 0x%.8lx (%ld)\n"), i, sector_begin, sector_begin);
466 fprintf (f, _("Partition[%d] length = 0x%.8lx (%ld)\n"), i, sector_length, sector_length);
469 fprintf (f, "\n");
470 return true;
474 #define ppcboot_bfd_get_relocated_section_contents \
475 bfd_generic_get_relocated_section_contents
476 #define ppcboot_bfd_relax_section bfd_generic_relax_section
477 #define ppcboot_bfd_gc_sections bfd_generic_gc_sections
478 #define ppcboot_bfd_merge_sections bfd_generic_merge_sections
479 #define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
480 #define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
481 #define ppcboot_bfd_final_link _bfd_generic_final_link
482 #define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
483 #define ppcboot_get_section_contents_in_window \
484 _bfd_generic_get_section_contents_in_window
486 #define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
487 #define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
488 #define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
489 #define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
490 #define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
491 #define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
493 const bfd_target ppcboot_vec =
495 "ppcboot", /* name */
496 bfd_target_unknown_flavour, /* flavour */
497 BFD_ENDIAN_BIG, /* byteorder is big endian for code */
498 BFD_ENDIAN_LITTLE, /* header_byteorder */
499 EXEC_P, /* object_flags */
500 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
501 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
502 0, /* symbol_leading_char */
503 ' ', /* ar_pad_char */
504 16, /* ar_max_namelen */
505 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
506 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
507 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
508 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
509 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
510 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
511 { /* bfd_check_format */
512 _bfd_dummy_target,
513 ppcboot_object_p, /* bfd_check_format */
514 _bfd_dummy_target,
515 _bfd_dummy_target,
517 { /* bfd_set_format */
518 bfd_false,
519 ppcboot_mkobject,
520 bfd_false,
521 bfd_false,
523 { /* bfd_write_contents */
524 bfd_false,
525 bfd_true,
526 bfd_false,
527 bfd_false,
530 BFD_JUMP_TABLE_GENERIC (ppcboot),
531 BFD_JUMP_TABLE_COPY (ppcboot),
532 BFD_JUMP_TABLE_CORE (_bfd_nocore),
533 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
534 BFD_JUMP_TABLE_SYMBOLS (ppcboot),
535 BFD_JUMP_TABLE_RELOCS (ppcboot),
536 BFD_JUMP_TABLE_WRITE (ppcboot),
537 BFD_JUMP_TABLE_LINK (ppcboot),
538 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
540 NULL,
542 NULL