daily update
[deliverable/binutils-gdb.git] / bfd / ppcboot.c
CommitLineData
252b5132 1/* BFD back-end for PPCbug boot records.
117ed4f8 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4 Written by Michael Meissner, Cygnus Support, <meissner@cygnus.com>
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
RH
21
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.
26
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. */
34
3882b010 35#include "safe-ctype.h"
252b5132
RH
36#include "bfd.h"
37#include "sysdep.h"
38#include "libbfd.h"
39
40/* PPCbug location structure */
41typedef 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 */
49typedef 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. */
57typedef 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 */
8c763017
PB
67}
68#ifdef __GNUC__
69 __attribute__ ((packed))
70#endif
71ppcboot_hdr_t;
252b5132
RH
72
73/* Signature bytes for last 2 bytes of the 512 byte record */
74#define SIGNATURE0 0x55
75#define SIGNATURE1 0xaa
76
77/* PowerPC boot type */
78#define PPC_IND 0x41
79
80/* Information needed for ppcboot header */
81typedef struct ppcboot_data {
82 ppcboot_hdr_t header; /* raw header */
83 asection *sec; /* single section */
84} ppcboot_data_t;
85
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
89
b34976b6 90static bfd_boolean ppcboot_mkobject PARAMS ((bfd *));
252b5132 91static const bfd_target *ppcboot_object_p PARAMS ((bfd *));
b34976b6 92static bfd_boolean ppcboot_set_arch_mach
252b5132 93 PARAMS ((bfd *, enum bfd_architecture, unsigned long));
b34976b6 94static bfd_boolean ppcboot_get_section_contents
252b5132
RH
95 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
96static long ppcboot_get_symtab_upper_bound PARAMS ((bfd *));
97static char *mangle_name PARAMS ((bfd *, char *));
6cee3f79 98static long ppcboot_canonicalize_symtab PARAMS ((bfd *, asymbol **));
252b5132 99static void ppcboot_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
b34976b6 100static bfd_boolean ppcboot_set_section_contents
0f867abe 101 PARAMS ((bfd *, asection *, const PTR, file_ptr, bfd_size_type));
b34976b6
AM
102static int ppcboot_sizeof_headers PARAMS ((bfd *, bfd_boolean));
103static bfd_boolean ppcboot_bfd_print_private_bfd_data PARAMS ((bfd *, PTR));
252b5132
RH
104
105#define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (PTR) (ptr))
106#define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
107\f
108/* Create a ppcboot object. Invoked via bfd_set_format. */
109
b34976b6 110static bfd_boolean
252b5132
RH
111ppcboot_mkobject (abfd)
112 bfd *abfd;
113{
114 if (!ppcboot_get_tdata (abfd))
dc810e39
AM
115 {
116 bfd_size_type amt = sizeof (ppcboot_data_t);
117 ppcboot_set_tdata (abfd, bfd_zalloc (abfd, amt));
118 }
252b5132 119
b34976b6 120 return TRUE;
252b5132
RH
121}
122
123\f
124/* Set the architecture to PowerPC */
b34976b6 125static bfd_boolean
252b5132
RH
126ppcboot_set_arch_mach (abfd, arch, machine)
127 bfd *abfd;
128 enum bfd_architecture arch;
129 unsigned long machine;
130{
131 if (arch == bfd_arch_unknown)
132 arch = bfd_arch_powerpc;
133
134 else if (arch != bfd_arch_powerpc)
b34976b6 135 return FALSE;
252b5132
RH
136
137 return bfd_default_set_arch_mach (abfd, arch, machine);
138}
139
140\f
141/* Any file may be considered to be a ppcboot file, provided the target
142 was not defaulted. That is, it must be explicitly specified as
143 being ppcboot. */
144
145static const bfd_target *
146ppcboot_object_p (abfd)
147 bfd *abfd;
148{
149 struct stat statbuf;
150 asection *sec;
151 ppcboot_hdr_t hdr;
152 size_t i;
153 ppcboot_data_t *tdata;
117ed4f8 154 flagword flags;
252b5132
RH
155
156 BFD_ASSERT (sizeof (ppcboot_hdr_t) == 1024);
157
158 if (abfd->target_defaulted)
159 {
160 bfd_set_error (bfd_error_wrong_format);
161 return NULL;
162 }
163
164 /* Find the file size. */
165 if (bfd_stat (abfd, &statbuf) < 0)
166 {
167 bfd_set_error (bfd_error_system_call);
168 return NULL;
169 }
170
171 if ((size_t) statbuf.st_size < sizeof (ppcboot_hdr_t))
172 {
173 bfd_set_error (bfd_error_wrong_format);
174 return NULL;
175 }
176
dc810e39
AM
177 if (bfd_bread ((PTR) &hdr, (bfd_size_type) sizeof (hdr), abfd)
178 != sizeof (hdr))
252b5132
RH
179 {
180 if (bfd_get_error () != bfd_error_system_call)
181 bfd_set_error (bfd_error_wrong_format);
182
183 return NULL;
184 }
185
186 /* Now do some basic checks. */
187 for (i = 0; i < sizeof (hdr.pc_compatibility); i++)
188 if (hdr.pc_compatibility[i])
189 {
190 bfd_set_error (bfd_error_wrong_format);
191 return NULL;
192 }
193
194 if (hdr.signature[0] != SIGNATURE0 || hdr.signature[1] != SIGNATURE1)
195 {
196 bfd_set_error (bfd_error_wrong_format);
197 return NULL;
198 }
199
200 if (hdr.partition[0].partition_end.ind != PPC_IND)
201 {
202 bfd_set_error (bfd_error_wrong_format);
203 return NULL;
204 }
205
206 abfd->symcount = PPCBOOT_SYMS;
207
208 /* One data section. */
117ed4f8
AM
209 flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_CODE | SEC_HAS_CONTENTS;
210 sec = bfd_make_section_with_flags (abfd, ".data", flags);
252b5132
RH
211 if (sec == NULL)
212 return NULL;
252b5132 213 sec->vma = 0;
eea6121a 214 sec->size = statbuf.st_size - sizeof (ppcboot_hdr_t);
252b5132
RH
215 sec->filepos = sizeof (ppcboot_hdr_t);
216
217 ppcboot_mkobject (abfd);
218 tdata = ppcboot_get_tdata (abfd);
219 tdata->sec = sec;
220 memcpy ((PTR) &tdata->header, (PTR) &hdr, sizeof (ppcboot_hdr_t));
221
dc810e39 222 ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0L);
252b5132
RH
223 return abfd->xvec;
224}
225
226#define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
227#define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
228#define ppcboot_new_section_hook _bfd_generic_new_section_hook
229
230\f
231/* Get contents of the only section. */
232
b34976b6 233static bfd_boolean
252b5132
RH
234ppcboot_get_section_contents (abfd, section, location, offset, count)
235 bfd *abfd;
5f771d47 236 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
237 PTR location;
238 file_ptr offset;
239 bfd_size_type count;
240{
dc810e39
AM
241 if (bfd_seek (abfd, offset + (file_ptr) sizeof (ppcboot_hdr_t), SEEK_SET) != 0
242 || bfd_bread (location, count, abfd) != count)
b34976b6
AM
243 return FALSE;
244 return TRUE;
252b5132
RH
245}
246
247\f
248/* Return the amount of memory needed to read the symbol table. */
249
250static long
251ppcboot_get_symtab_upper_bound (abfd)
5f771d47 252 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
253{
254 return (PPCBOOT_SYMS + 1) * sizeof (asymbol *);
255}
256
257\f
258/* Create a symbol name based on the bfd's filename. */
259
260static char *
261mangle_name (abfd, suffix)
262 bfd *abfd;
263 char *suffix;
264{
dc810e39 265 bfd_size_type size;
252b5132
RH
266 char *buf;
267 char *p;
268
269 size = (strlen (bfd_get_filename (abfd))
270 + strlen (suffix)
271 + sizeof "_ppcboot__");
272
273 buf = (char *) bfd_alloc (abfd, size);
274 if (buf == NULL)
275 return "";
276
277 sprintf (buf, "_ppcboot_%s_%s", bfd_get_filename (abfd), suffix);
278
279 /* Change any non-alphanumeric characters to underscores. */
280 for (p = buf; *p; p++)
3882b010 281 if (! ISALNUM (*p))
252b5132
RH
282 *p = '_';
283
284 return buf;
285}
286
287\f
288/* Return the symbol table. */
289
290static long
6cee3f79 291ppcboot_canonicalize_symtab (abfd, alocation)
252b5132
RH
292 bfd *abfd;
293 asymbol **alocation;
294{
295 asection *sec = ppcboot_get_tdata (abfd)->sec;
296 asymbol *syms;
297 unsigned int i;
dc810e39 298 bfd_size_type amt = PPCBOOT_SYMS * sizeof (asymbol);
252b5132 299
dc810e39 300 syms = (asymbol *) bfd_alloc (abfd, amt);
252b5132 301 if (syms == NULL)
b34976b6 302 return FALSE;
252b5132
RH
303
304 /* Start symbol. */
305 syms[0].the_bfd = abfd;
306 syms[0].name = mangle_name (abfd, "start");
307 syms[0].value = 0;
308 syms[0].flags = BSF_GLOBAL;
309 syms[0].section = sec;
310 syms[0].udata.p = NULL;
311
312 /* End symbol. */
313 syms[1].the_bfd = abfd;
314 syms[1].name = mangle_name (abfd, "end");
eea6121a 315 syms[1].value = sec->size;
252b5132
RH
316 syms[1].flags = BSF_GLOBAL;
317 syms[1].section = sec;
318 syms[1].udata.p = NULL;
319
320 /* Size symbol. */
321 syms[2].the_bfd = abfd;
322 syms[2].name = mangle_name (abfd, "size");
eea6121a 323 syms[2].value = sec->size;
252b5132
RH
324 syms[2].flags = BSF_GLOBAL;
325 syms[2].section = bfd_abs_section_ptr;
326 syms[2].udata.p = NULL;
327
328 for (i = 0; i < PPCBOOT_SYMS; i++)
329 *alocation++ = syms++;
330 *alocation = NULL;
331
332 return PPCBOOT_SYMS;
333}
334
3f3c5c34 335#define ppcboot_make_empty_symbol _bfd_generic_make_empty_symbol
252b5132
RH
336#define ppcboot_print_symbol _bfd_nosymbols_print_symbol
337
338/* Get information about a symbol. */
339
340static void
341ppcboot_get_symbol_info (ignore_abfd, symbol, ret)
5f771d47 342 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
343 asymbol *symbol;
344 symbol_info *ret;
345{
346 bfd_symbol_info (symbol, ret);
347}
348
3c9458e9
NC
349#define ppcboot_bfd_is_target_special_symbol \
350 ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
252b5132
RH
351#define ppcboot_bfd_is_local_label_name bfd_generic_is_local_label_name
352#define ppcboot_get_lineno _bfd_nosymbols_get_lineno
353#define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
4ab527b0 354#define ppcboot_find_inliner_info _bfd_nosymbols_find_inliner_info
252b5132
RH
355#define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
356#define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
357#define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
358
359#define ppcboot_get_reloc_upper_bound \
360 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
361#define ppcboot_canonicalize_reloc \
362 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
363#define ppcboot_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
364\f
365/* Write section contents of a ppcboot file. */
366
b34976b6 367static bfd_boolean
252b5132
RH
368ppcboot_set_section_contents (abfd, sec, data, offset, size)
369 bfd *abfd;
370 asection *sec;
0f867abe 371 const PTR data;
252b5132
RH
372 file_ptr offset;
373 bfd_size_type size;
374{
375 if (! abfd->output_has_begun)
376 {
377 bfd_vma low;
378 asection *s;
379
380 /* The lowest section VMA sets the virtual address of the start
381 of the file. We use the set the file position of all the
382 sections. */
383 low = abfd->sections->vma;
384 for (s = abfd->sections->next; s != NULL; s = s->next)
385 if (s->vma < low)
386 low = s->vma;
387
388 for (s = abfd->sections; s != NULL; s = s->next)
389 s->filepos = s->vma - low;
390
b34976b6 391 abfd->output_has_begun = TRUE;
252b5132
RH
392 }
393
394 return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
395}
396
397\f
398static int
399ppcboot_sizeof_headers (abfd, exec)
5f771d47 400 bfd *abfd ATTRIBUTE_UNUSED;
b34976b6 401 bfd_boolean exec ATTRIBUTE_UNUSED;
252b5132
RH
402{
403 return sizeof (ppcboot_hdr_t);
404}
405
406\f
407/* Print out the program headers. */
408
b34976b6 409static bfd_boolean
252b5132
RH
410ppcboot_bfd_print_private_bfd_data (abfd, farg)
411 bfd *abfd;
412 PTR farg;
413{
414 FILE *f = (FILE *)farg;
415 ppcboot_data_t *tdata = ppcboot_get_tdata (abfd);
416 long entry_offset = bfd_getl_signed_32 ((PTR) tdata->header.entry_offset);
417 long length = bfd_getl_signed_32 ((PTR) tdata->header.length);
418 int i;
419
420 fprintf (f, _("\nppcboot header:\n"));
421 fprintf (f, _("Entry offset = 0x%.8lx (%ld)\n"), entry_offset, entry_offset);
422 fprintf (f, _("Length = 0x%.8lx (%ld)\n"), length, length);
423
424 if (tdata->header.flags)
425 fprintf (f, _("Flag field = 0x%.2x\n"), tdata->header.flags);
426
427 if (tdata->header.os_id)
428 fprintf (f, "OS_ID = 0x%.2x\n", tdata->header.os_id);
429
430 if (tdata->header.partition_name)
431 fprintf (f, _("Partition name = \"%s\"\n"), tdata->header.partition_name);
432
433 for (i = 0; i < 4; i++)
434 {
435 long sector_begin = bfd_getl_signed_32 ((PTR) tdata->header.partition[i].sector_begin);
436 long sector_length = bfd_getl_signed_32 ((PTR) tdata->header.partition[i].sector_length);
437
438 /* Skip all 0 entries */
439 if (!tdata->header.partition[i].partition_begin.ind
440 && !tdata->header.partition[i].partition_begin.head
441 && !tdata->header.partition[i].partition_begin.sector
442 && !tdata->header.partition[i].partition_begin.cylinder
443 && !tdata->header.partition[i].partition_end.ind
444 && !tdata->header.partition[i].partition_end.head
445 && !tdata->header.partition[i].partition_end.sector
446 && !tdata->header.partition[i].partition_end.cylinder
447 && !sector_begin && !sector_length)
448 continue;
449
450 fprintf (f, _("\nPartition[%d] start = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
451 tdata->header.partition[i].partition_begin.ind,
452 tdata->header.partition[i].partition_begin.head,
453 tdata->header.partition[i].partition_begin.sector,
454 tdata->header.partition[i].partition_begin.cylinder);
455
456 fprintf (f, _("Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
457 tdata->header.partition[i].partition_end.ind,
458 tdata->header.partition[i].partition_end.head,
459 tdata->header.partition[i].partition_end.sector,
460 tdata->header.partition[i].partition_end.cylinder);
461
462 fprintf (f, _("Partition[%d] sector = 0x%.8lx (%ld)\n"), i, sector_begin, sector_begin);
463 fprintf (f, _("Partition[%d] length = 0x%.8lx (%ld)\n"), i, sector_length, sector_length);
464 }
465
466 fprintf (f, "\n");
b34976b6 467 return TRUE;
252b5132
RH
468}
469
470\f
471#define ppcboot_bfd_get_relocated_section_contents \
472 bfd_generic_get_relocated_section_contents
473#define ppcboot_bfd_relax_section bfd_generic_relax_section
474#define ppcboot_bfd_gc_sections bfd_generic_gc_sections
8550eb6e 475#define ppcboot_bfd_merge_sections bfd_generic_merge_sections
72adc230 476#define ppcboot_bfd_is_group_section bfd_generic_is_group_section
e61463e1 477#define ppcboot_bfd_discard_group bfd_generic_discard_group
082b7297
L
478#define ppcboot_section_already_linked \
479 _bfd_generic_section_already_linked
252b5132 480#define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
e2d34d7d 481#define ppcboot_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
252b5132 482#define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
2d653fc7 483#define ppcboot_bfd_link_just_syms _bfd_generic_link_just_syms
252b5132
RH
484#define ppcboot_bfd_final_link _bfd_generic_final_link
485#define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
486#define ppcboot_get_section_contents_in_window \
487 _bfd_generic_get_section_contents_in_window
488
489#define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
490#define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
491#define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
492#define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
80fccad2 493#define ppcboot_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
252b5132
RH
494#define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
495#define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
496
497const bfd_target ppcboot_vec =
498{
499 "ppcboot", /* name */
500 bfd_target_unknown_flavour, /* flavour */
501 BFD_ENDIAN_BIG, /* byteorder is big endian for code */
502 BFD_ENDIAN_LITTLE, /* header_byteorder */
503 EXEC_P, /* object_flags */
504 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
505 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
506 0, /* symbol_leading_char */
507 ' ', /* ar_pad_char */
508 16, /* ar_max_namelen */
509 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
510 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
511 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
512 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
513 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
514 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
515 { /* bfd_check_format */
516 _bfd_dummy_target,
517 ppcboot_object_p, /* bfd_check_format */
518 _bfd_dummy_target,
519 _bfd_dummy_target,
520 },
521 { /* bfd_set_format */
522 bfd_false,
523 ppcboot_mkobject,
524 bfd_false,
525 bfd_false,
526 },
527 { /* bfd_write_contents */
528 bfd_false,
529 bfd_true,
530 bfd_false,
531 bfd_false,
532 },
533
534 BFD_JUMP_TABLE_GENERIC (ppcboot),
535 BFD_JUMP_TABLE_COPY (ppcboot),
536 BFD_JUMP_TABLE_CORE (_bfd_nocore),
537 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
538 BFD_JUMP_TABLE_SYMBOLS (ppcboot),
539 BFD_JUMP_TABLE_RELOCS (ppcboot),
540 BFD_JUMP_TABLE_WRITE (ppcboot),
541 BFD_JUMP_TABLE_LINK (ppcboot),
542 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
543
c3c89269 544 NULL,
892339ee 545
252b5132
RH
546 NULL
547};
This page took 0.383043 seconds and 4 git commands to generate.