home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / ppcboot.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  16KB  |  536 lines

  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. /* PowerPC boot type */
  74. #define PPC_IND 0x41
  75.  
  76. /* Information needed for ppcboot header */
  77. typedef struct ppcboot_data {
  78.   ppcboot_hdr_t    header;                /* raw header */
  79.   asection *sec;                /* single section */
  80. } ppcboot_data_t;
  81.  
  82. /* Any bfd we create by reading a ppcboot file has three symbols:
  83.    a start symbol, an end symbol, and an absolute length symbol.  */
  84. #define PPCBOOT_SYMS 3
  85.  
  86. static boolean ppcboot_mkobject PARAMS ((bfd *));
  87. static const bfd_target *ppcboot_object_p PARAMS ((bfd *));
  88. static boolean ppcboot_get_section_contents
  89.   PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
  90. static long ppcboot_get_symtab_upper_bound PARAMS ((bfd *));
  91. static char *mangle_name PARAMS ((bfd *, char *));
  92. static long ppcboot_get_symtab PARAMS ((bfd *, asymbol **));
  93. static asymbol *ppcboot_make_empty_symbol PARAMS ((bfd *));
  94. static void ppcboot_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
  95. static boolean ppcboot_set_section_contents
  96.   PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
  97. static int ppcboot_sizeof_headers PARAMS ((bfd *, boolean));
  98.  
  99. #define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (PTR) (ptr))
  100. #define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
  101.  
  102. /* Create a ppcboot object.  Invoked via bfd_set_format.  */
  103.  
  104. static boolean
  105. ppcboot_mkobject (abfd)
  106.      bfd *abfd;
  107. {
  108.   if (!ppcboot_get_tdata (abfd))
  109.     ppcboot_set_tdata (abfd, bfd_zalloc (abfd, sizeof (ppcboot_data_t)));
  110.  
  111.   return true;
  112. }
  113.  
  114.  
  115. /* Set the architecture to PowerPC */
  116. boolean
  117. ppcboot_set_arch_mach (abfd, arch, machine)
  118.      bfd *abfd;
  119.      enum bfd_architecture arch;
  120.      unsigned long machine;
  121. {
  122.   if (arch == bfd_arch_unknown)
  123.     arch = bfd_arch_powerpc;
  124.  
  125.   else if (arch != bfd_arch_powerpc)
  126.     return false;
  127.  
  128.   return bfd_default_set_arch_mach (abfd, arch, machine);
  129. }
  130.  
  131.  
  132. /* Any file may be considered to be a ppcboot file, provided the target
  133.    was not defaulted.  That is, it must be explicitly specified as
  134.    being ppcboot.  */
  135.  
  136. static const bfd_target *
  137. ppcboot_object_p (abfd)
  138.      bfd *abfd;
  139. {
  140.   struct stat statbuf;
  141.   asection *sec;
  142.   ppcboot_hdr_t hdr;
  143.   int i;
  144.   ppcboot_data_t *tdata;
  145.  
  146.   BFD_ASSERT (sizeof (ppcboot_hdr_t) == 1024);
  147.  
  148.   if (abfd->target_defaulted)
  149.     {
  150.       bfd_set_error (bfd_error_wrong_format);
  151.       return NULL;
  152.     }
  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.   if (hdr.partition[0].partition_end.ind != PPC_IND)
  190.     {
  191.       bfd_set_error (bfd_error_wrong_format);
  192.       return NULL;
  193.     }
  194.  
  195.   abfd->symcount = PPCBOOT_SYMS;
  196.  
  197.   /* One data section.  */
  198.   sec = bfd_make_section (abfd, ".data");
  199.   if (sec == NULL)
  200.     return NULL;
  201.   sec->flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_CODE | SEC_HAS_CONTENTS;
  202.   sec->vma = 0;
  203.   sec->_raw_size = statbuf.st_size - sizeof (ppcboot_hdr_t);
  204.   sec->filepos = sizeof (ppcboot_hdr_t);
  205.  
  206.   ppcboot_mkobject (abfd);
  207.   tdata = ppcboot_get_tdata (abfd);
  208.   tdata->sec = sec;
  209.   memcpy ((PTR) &tdata->header, (PTR) &hdr, sizeof (ppcboot_hdr_t));
  210.  
  211.   ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0);
  212.   return abfd->xvec;
  213. }
  214.  
  215. #define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
  216. #define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  217. #define ppcboot_new_section_hook _bfd_generic_new_section_hook
  218.  
  219.  
  220. /* Get contents of the only section.  */
  221.  
  222. static boolean
  223. ppcboot_get_section_contents (abfd, section, location, offset, count)
  224.      bfd *abfd;
  225.      asection *section;
  226.      PTR location;
  227.      file_ptr offset;
  228.      bfd_size_type count;
  229. {
  230.   if (bfd_seek (abfd, offset + sizeof(ppcboot_hdr_t), SEEK_SET) != 0
  231.       || bfd_read (location, 1, count, abfd) != count)
  232.     return false;
  233.   return true;
  234. }
  235.  
  236.  
  237. /* Return the amount of memory needed to read the symbol table.  */
  238.  
  239. static long
  240. ppcboot_get_symtab_upper_bound (abfd)
  241.      bfd *abfd;
  242. {
  243.   return (PPCBOOT_SYMS + 1) * sizeof (asymbol *);
  244. }
  245.  
  246.  
  247. /* Create a symbol name based on the bfd's filename.  */
  248.  
  249. static char *
  250. mangle_name (abfd, suffix)
  251.      bfd *abfd;
  252.      char *suffix;
  253. {
  254.   int size;
  255.   char *buf;
  256.   char *p;
  257.  
  258.   size = (strlen (bfd_get_filename (abfd))
  259.       + strlen (suffix)
  260.       + sizeof "_ppcboot__");
  261.  
  262.   buf = (char *) bfd_alloc (abfd, size);
  263.   if (buf == NULL)
  264.     return "";
  265.  
  266.   sprintf (buf, "_ppcboot_%s_%s", bfd_get_filename (abfd), suffix);
  267.  
  268.   /* Change any non-alphanumeric characters to underscores.  */
  269.   for (p = buf; *p; p++)
  270.     if (! isalnum (*p))
  271.       *p = '_';
  272.  
  273.   return buf;
  274. }
  275.  
  276.  
  277. /* Return the symbol table.  */
  278.  
  279. static long
  280. ppcboot_get_symtab (abfd, alocation)
  281.      bfd *abfd;
  282.      asymbol **alocation;
  283. {
  284.   asection *sec = ppcboot_get_tdata (abfd)->sec;
  285.   asymbol *syms;
  286.   unsigned int i;
  287.  
  288.   syms = (asymbol *) bfd_alloc (abfd, PPCBOOT_SYMS * sizeof (asymbol));
  289.   if (syms == NULL)
  290.     return false;
  291.  
  292.   /* Start symbol.  */
  293.   syms[0].the_bfd = abfd;
  294.   syms[0].name = mangle_name (abfd, "start");
  295.   syms[0].value = 0;
  296.   syms[0].flags = BSF_GLOBAL;
  297.   syms[0].section = sec;
  298.   syms[0].udata.p = NULL;
  299.  
  300.   /* End symbol.  */
  301.   syms[1].the_bfd = abfd;
  302.   syms[1].name = mangle_name (abfd, "end");
  303.   syms[1].value = sec->_raw_size;
  304.   syms[1].flags = BSF_GLOBAL;
  305.   syms[1].section = sec;
  306.   syms[1].udata.p = NULL;
  307.  
  308.   /* Size symbol.  */
  309.   syms[2].the_bfd = abfd;
  310.   syms[2].name = mangle_name (abfd, "size");
  311.   syms[2].value = sec->_raw_size;
  312.   syms[2].flags = BSF_GLOBAL;
  313.   syms[2].section = bfd_abs_section_ptr;
  314.   syms[2].udata.p = NULL;
  315.  
  316.   for (i = 0; i < PPCBOOT_SYMS; i++)
  317.     *alocation++ = syms++;
  318.   *alocation = NULL;
  319.  
  320.   return PPCBOOT_SYMS;
  321. }
  322.  
  323.  
  324. /* Make an empty symbol.  */
  325.  
  326. static asymbol *
  327. ppcboot_make_empty_symbol (abfd)
  328.      bfd *abfd;
  329. {
  330.   return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
  331. }
  332.  
  333.  
  334. #define ppcboot_print_symbol _bfd_nosymbols_print_symbol
  335.  
  336. /* Get information about a symbol.  */
  337.  
  338. static void
  339. ppcboot_get_symbol_info (ignore_abfd, symbol, ret)
  340.      bfd *ignore_abfd;
  341.      asymbol *symbol;
  342.      symbol_info *ret;
  343. {
  344.   bfd_symbol_info (symbol, ret);
  345. }
  346.  
  347. #define ppcboot_bfd_is_local_label bfd_generic_is_local_label
  348. #define ppcboot_get_lineno _bfd_nosymbols_get_lineno
  349. #define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
  350. #define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  351. #define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
  352. #define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  353.  
  354. #define ppcboot_get_reloc_upper_bound \
  355.   ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
  356. #define ppcboot_canonicalize_reloc \
  357.   ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
  358. #define ppcboot_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
  359.  
  360. /* Set the architecture of a ppcboot file.  */
  361. #define ppcboot_set_arch_mach ppcboot_set_arch_mach
  362.  
  363.  
  364. /* Write section contents of a ppcboot file.  */
  365.  
  366. static boolean
  367. ppcboot_set_section_contents (abfd, sec, data, offset, size)
  368.      bfd *abfd;
  369.      asection *sec;
  370.      PTR data;
  371.      file_ptr offset;
  372.      bfd_size_type size;
  373. {
  374.   if (! abfd->output_has_begun)
  375.     {
  376.       bfd_vma low;
  377.       asection *s;
  378.  
  379.       /* The lowest section VMA sets the virtual address of the start
  380.          of the file.  We use the set the file position of all the
  381.          sections.  */
  382.       low = abfd->sections->vma;
  383.       for (s = abfd->sections->next; s != NULL; s = s->next)
  384.     if (s->vma < low)
  385.       low = s->vma;
  386.  
  387.       for (s = abfd->sections; s != NULL; s = s->next)
  388.     s->filepos = s->vma - low;
  389.  
  390.       abfd->output_has_begun = true;
  391.     }
  392.  
  393.   return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
  394. }
  395.  
  396.  
  397. static int
  398. ppcboot_sizeof_headers (abfd, exec)
  399.      bfd *abfd;
  400.      boolean exec;
  401. {
  402.   return sizeof (ppcboot_hdr_t);
  403. }
  404.  
  405.  
  406. /* Print out the program headers.  */
  407.  
  408. boolean
  409. ppcboot_bfd_print_private_bfd_data (abfd, farg)
  410.      bfd *abfd;
  411.      PTR farg;
  412. {
  413.   FILE *f = (FILE *)farg;
  414.   ppcboot_data_t *tdata = ppcboot_get_tdata (abfd);
  415.   long entry_offset = bfd_getl_signed_32 ((PTR) &tdata->header.entry_offset);
  416.   long length = bfd_getl_signed_32 ((PTR) &tdata->header.length);
  417.   int i;
  418.  
  419.   fprintf (f, "\nppcboot header:\n");
  420.   fprintf (f, "Entry offset        = 0x%.8lx (%ld)\n", entry_offset, entry_offset);
  421.   fprintf (f, "Length              = 0x%.8lx (%ld)\n", length, length);
  422.  
  423.   if (tdata->header.flags)
  424.     fprintf (f, "Flag field          = 0x%.2x\n", tdata->header.flags);
  425.  
  426.   if (tdata->header.os_id)
  427.     fprintf (f, "OS_ID               = 0x%.2x\n", tdata->header.os_id);
  428.  
  429.   if (tdata->header.partition_name)
  430.     fprintf (f, "Partition name      = \"%s\"\n", tdata->header.partition_name);
  431.  
  432.   for (i = 0; i < 4; i++)
  433.     {
  434.       long sector_begin  = bfd_getl_signed_32 ((PTR) &tdata->header.partition[i].sector_begin);
  435.       long sector_length = bfd_getl_signed_32 ((PTR) &tdata->header.partition[i].sector_length);
  436.  
  437.       /* Skip all 0 entries */
  438.       if (!tdata->header.partition[i].partition_begin.ind
  439.       && !tdata->header.partition[i].partition_begin.head
  440.       && !tdata->header.partition[i].partition_begin.sector
  441.       && !tdata->header.partition[i].partition_begin.cylinder
  442.       && !tdata->header.partition[i].partition_end.ind
  443.       && !tdata->header.partition[i].partition_end.head
  444.       && !tdata->header.partition[i].partition_end.sector
  445.       && !tdata->header.partition[i].partition_end.cylinder
  446.       && !sector_begin && !sector_length)
  447.     continue;
  448.  
  449.       fprintf (f, "\nPartition[%d] start  = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n", i,
  450.            tdata->header.partition[i].partition_begin.ind,
  451.            tdata->header.partition[i].partition_begin.head,
  452.            tdata->header.partition[i].partition_begin.sector,
  453.            tdata->header.partition[i].partition_begin.cylinder);
  454.  
  455.       fprintf (f, "Partition[%d] end    = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n", i,
  456.            tdata->header.partition[i].partition_end.ind,
  457.            tdata->header.partition[i].partition_end.head,
  458.            tdata->header.partition[i].partition_end.sector,
  459.            tdata->header.partition[i].partition_end.cylinder);
  460.  
  461.       fprintf (f, "Partition[%d] sector = 0x%.8lx (%ld)\n", i, sector_begin, sector_begin);
  462.       fprintf (f, "Partition[%d] length = 0x%.8lx (%ld)\n", i, sector_length, sector_length);
  463.     }
  464.  
  465.   fprintf (f, "\n");
  466.   return true;
  467. }
  468.  
  469.  
  470. #define ppcboot_bfd_get_relocated_section_contents \
  471.   bfd_generic_get_relocated_section_contents
  472. #define ppcboot_bfd_relax_section bfd_generic_relax_section
  473. #define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  474. #define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
  475. #define ppcboot_bfd_final_link _bfd_generic_final_link
  476. #define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
  477. #define ppcboot_get_section_contents_in_window \
  478.   _bfd_generic_get_section_contents_in_window
  479.  
  480. #define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
  481. #define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  482. #define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
  483. #define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
  484. #define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
  485. #define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
  486.  
  487. const bfd_target ppcboot_vec =
  488. {
  489.   "ppcboot",            /* name */
  490.   bfd_target_unknown_flavour,    /* flavour */
  491.   BFD_ENDIAN_BIG,        /* byteorder is big endian for code */
  492.   BFD_ENDIAN_LITTLE,        /* header_byteorder */
  493.   EXEC_P,            /* object_flags */
  494.   (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
  495.    | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
  496.   0,                /* symbol_leading_char */
  497.   ' ',                /* ar_pad_char */
  498.   16,                /* ar_max_namelen */
  499.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  500.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  501.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* data */
  502.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  503.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  504.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* hdrs */
  505.   {                /* bfd_check_format */
  506.     _bfd_dummy_target,
  507.     ppcboot_object_p,        /* bfd_check_format */
  508.     _bfd_dummy_target,
  509.     _bfd_dummy_target,
  510.   },
  511.   {                /* bfd_set_format */
  512.     bfd_false,
  513.     ppcboot_mkobject,
  514.     bfd_false,
  515.     bfd_false,
  516.   },
  517.   {                /* bfd_write_contents */
  518.     bfd_false,
  519.     bfd_true,
  520.     bfd_false,
  521.     bfd_false,
  522.   },
  523.  
  524.   BFD_JUMP_TABLE_GENERIC (ppcboot),
  525.   BFD_JUMP_TABLE_COPY (ppcboot),
  526.   BFD_JUMP_TABLE_CORE (_bfd_nocore),
  527.   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  528.   BFD_JUMP_TABLE_SYMBOLS (ppcboot),
  529.   BFD_JUMP_TABLE_RELOCS (ppcboot),
  530.   BFD_JUMP_TABLE_WRITE (ppcboot),
  531.   BFD_JUMP_TABLE_LINK (ppcboot),
  532.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  533.  
  534.   NULL
  535. };
  536.