This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / bfd / ppcboot.c
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>
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
20
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
24    data.
25
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.  */
33
34 #include <ctype.h>
35
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "libbfd.h"
39
40 /* PPCbug location structure */
41 typedef struct ppcboot_location {
42   bfd_byte      ind;
43   bfd_byte      head;
44   bfd_byte      sector;
45   bfd_byte      cylinder;
46 } ppcboot_location_t;
47
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;
55
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 */
67 } ppcboot_hdr_t;
68
69 /* Signature bytes for last 2 bytes of the 512 byte record */
70 #define SIGNATURE0 0x55
71 #define SIGNATURE1 0xaa
72
73 /* Information needed for ppcboot header */
74 typedef struct ppcboot_data {
75   ppcboot_hdr_t header;                         /* raw header */
76   asection *sec;                                /* single section */
77 } ppcboot_data_t;
78
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
82
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));
95
96 #define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (PTR) (ptr))
97 #define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
98 \f
99 /* Create a ppcboot object.  Invoked via bfd_set_format.  */
100
101 static boolean
102 ppcboot_mkobject (abfd)
103      bfd *abfd;
104 {
105   if (!ppcboot_get_tdata (abfd))
106     ppcboot_set_tdata (abfd, bfd_zalloc (abfd, sizeof (ppcboot_data_t)));
107
108   return true;
109 }
110
111 \f
112 /* Set the architecture to PowerPC */
113 boolean
114 ppcboot_set_arch_mach (abfd, arch, machine)
115      bfd *abfd;
116      enum bfd_architecture arch;
117      unsigned long machine;
118 {
119   if (arch == bfd_arch_unknown)
120     arch = bfd_arch_powerpc;
121
122   else if (arch != bfd_arch_powerpc)
123     return false;
124
125   return bfd_default_set_arch_mach (abfd, arch, machine);
126 }
127
128 \f
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
131    being ppcboot.  */
132
133 static const bfd_target *
134 ppcboot_object_p (abfd)
135      bfd *abfd;
136 {
137   struct stat statbuf;
138   asection *sec;
139   ppcboot_hdr_t hdr;
140   int i;
141   ppcboot_data_t *tdata;
142
143   if (sizeof (ppcboot_hdr_t) != 1024)
144     fatal ("ppcboot_hdr_t is not 1024 bytes in size");
145
146   if (abfd->target_defaulted)
147     {
148       bfd_set_error (bfd_error_wrong_format);
149       return NULL;
150     }
151
152   abfd->symcount = PPCBOOT_SYMS;
153
154   /* Find the file size.  */
155   if (bfd_stat (abfd, &statbuf) < 0)
156     {
157       bfd_set_error (bfd_error_system_call);
158       return NULL;
159     }
160
161   if (statbuf.st_size < sizeof (ppcboot_hdr_t))
162     {
163       bfd_set_error (bfd_error_wrong_format);
164       return NULL;
165     }
166
167   if (bfd_read ((PTR) &hdr, sizeof (hdr), 1, abfd) != sizeof (hdr))
168     {
169       if (bfd_get_error () != bfd_error_system_call)
170         bfd_set_error (bfd_error_wrong_format);
171
172       return NULL;
173     }
174
175   /* Now do some basic checks.  */
176   for (i = 0; i < sizeof (hdr.pc_compatibility); i++)
177     if (hdr.pc_compatibility[i])
178       {
179         bfd_set_error (bfd_error_wrong_format);
180         return NULL;
181       }
182
183   if (hdr.signature[0] != SIGNATURE0 || hdr.signature[1] != SIGNATURE1)
184     {
185       bfd_set_error (bfd_error_wrong_format);
186       return NULL;
187     }
188
189   /* One data section.  */
190   sec = bfd_make_section (abfd, ".data");
191   if (sec == NULL)
192     return NULL;
193   sec->flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_CODE | SEC_HAS_CONTENTS;
194   sec->vma = 0;
195   sec->_raw_size = statbuf.st_size - sizeof (ppcboot_hdr_t);
196   sec->filepos = sizeof (ppcboot_hdr_t);
197
198   ppcboot_mkobject (abfd);
199   tdata = ppcboot_get_tdata (abfd);
200   tdata->sec = sec;
201   memcpy ((PTR) &tdata->header, (PTR) &hdr, sizeof (ppcboot_hdr_t));
202
203   ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0);
204   return abfd->xvec;
205 }
206
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
210
211 \f
212 /* Get contents of the only section.  */
213
214 static boolean
215 ppcboot_get_section_contents (abfd, section, location, offset, count)
216      bfd *abfd;
217      asection *section;
218      PTR location;
219      file_ptr offset;
220      bfd_size_type count;
221 {
222   if (bfd_seek (abfd, offset + sizeof(ppcboot_hdr_t), SEEK_SET) != 0
223       || bfd_read (location, 1, count, abfd) != count)
224     return false;
225   return true;
226 }
227
228 \f
229 /* Return the amount of memory needed to read the symbol table.  */
230
231 static long
232 ppcboot_get_symtab_upper_bound (abfd)
233      bfd *abfd;
234 {
235   return (PPCBOOT_SYMS + 1) * sizeof (asymbol *);
236 }
237
238 \f
239 /* Create a symbol name based on the bfd's filename.  */
240
241 static char *
242 mangle_name (abfd, suffix)
243      bfd *abfd;
244      char *suffix;
245 {
246   int size;
247   char *buf;
248   char *p;
249
250   size = (strlen (bfd_get_filename (abfd))
251           + strlen (suffix)
252           + sizeof "_ppcboot__");
253
254   buf = (char *) bfd_alloc (abfd, size);
255   if (buf == NULL)
256     return "";
257
258   sprintf (buf, "_ppcboot_%s_%s", bfd_get_filename (abfd), suffix);
259
260   /* Change any non-alphanumeric characters to underscores.  */
261   for (p = buf; *p; p++)
262     if (! isalnum (*p))
263       *p = '_';
264
265   return buf;
266 }
267
268 \f
269 /* Return the symbol table.  */
270
271 static long
272 ppcboot_get_symtab (abfd, alocation)
273      bfd *abfd;
274      asymbol **alocation;
275 {
276   asection *sec = ppcboot_get_tdata (abfd)->sec;
277   asymbol *syms;
278   unsigned int i;
279
280   syms = (asymbol *) bfd_alloc (abfd, PPCBOOT_SYMS * sizeof (asymbol));
281   if (syms == NULL)
282     return false;
283
284   /* Start symbol.  */
285   syms[0].the_bfd = abfd;
286   syms[0].name = mangle_name (abfd, "start");
287   syms[0].value = 0;
288   syms[0].flags = BSF_GLOBAL;
289   syms[0].section = sec;
290   syms[0].udata.p = NULL;
291
292   /* End symbol.  */
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;
299
300   /* Size symbol.  */
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;
307
308   for (i = 0; i < PPCBOOT_SYMS; i++)
309     *alocation++ = syms++;
310   *alocation = NULL;
311
312   return PPCBOOT_SYMS;
313 }
314
315 \f
316 /* Make an empty symbol.  */
317
318 static asymbol *
319 ppcboot_make_empty_symbol (abfd)
320      bfd *abfd;
321 {
322   return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
323 }
324
325 \f
326 #define ppcboot_print_symbol _bfd_nosymbols_print_symbol
327
328 /* Get information about a symbol.  */
329
330 static void
331 ppcboot_get_symbol_info (ignore_abfd, symbol, ret)
332      bfd *ignore_abfd;
333      asymbol *symbol;
334      symbol_info *ret;
335 {
336   bfd_symbol_info (symbol, ret);
337 }
338
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
345
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
351
352 /* Set the architecture of a ppcboot file.  */
353 #define ppcboot_set_arch_mach ppcboot_set_arch_mach
354
355 \f
356 /* Write section contents of a ppcboot file.  */
357
358 static boolean
359 ppcboot_set_section_contents (abfd, sec, data, offset, size)
360      bfd *abfd;
361      asection *sec;
362      PTR data;
363      file_ptr offset;
364      bfd_size_type size;
365 {
366   if (! abfd->output_has_begun)
367     {
368       bfd_vma low;
369       asection *s;
370
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
373          sections.  */
374       low = abfd->sections->vma;
375       for (s = abfd->sections->next; s != NULL; s = s->next)
376         if (s->vma < low)
377           low = s->vma;
378
379       for (s = abfd->sections; s != NULL; s = s->next)
380         s->filepos = s->vma - low;
381
382       abfd->output_has_begun = true;
383     }
384
385   return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
386 }
387
388 \f
389 static int
390 ppcboot_sizeof_headers (abfd, exec)
391      bfd *abfd;
392      boolean exec;
393 {
394   return sizeof (ppcboot_hdr_t);
395 }
396
397 \f
398 /* Print out the program headers.  */
399
400 boolean
401 ppcboot_bfd_print_private_bfd_data (abfd, farg)
402      bfd *abfd;
403      PTR farg;
404 {
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);
409   int i;
410
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);
414
415   if (tdata->header.flags)
416     fprintf (f, "Flag field          = 0x%.2x\n", tdata->header.flags);
417
418   if (tdata->header.os_id)
419     fprintf (f, "OS_ID               = 0x%.2x\n", tdata->header.os_id);
420
421   if (tdata->header.partition_name)
422     fprintf (f, "Partition name      = %s\n", tdata->header.partition_name);
423
424   for (i = 0; i < 4; i++)
425     {
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);
428
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)
439         continue;
440
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);
446
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);
452
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);
455     }
456
457   fprintf (f, "\n");
458   return true;
459 }
460
461 \f
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
471
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
478
479 const bfd_target ppcboot_vec =
480 {
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 */
498     _bfd_dummy_target,
499     ppcboot_object_p,           /* bfd_check_format */
500     _bfd_dummy_target,
501     _bfd_dummy_target,
502   },
503   {                             /* bfd_set_format */
504     bfd_false,
505     ppcboot_mkobject,
506     bfd_false,
507     bfd_false,
508   },
509   {                             /* bfd_write_contents */
510     bfd_false,
511     bfd_true,
512     bfd_false,
513     bfd_false,
514   },
515
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),
525
526   NULL
527 };