home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / aout-adobe.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  16KB  |  520 lines

  1. /* BFD back-end for a.out.adobe binaries.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.  Based on bout.c.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "libbfd.h"
  24.  
  25. #include "aout/adobe.h"
  26.  
  27. #include "aout/stab_gnu.h"
  28. #include "libaout.h"        /* BFD a.out internal data structures */
  29.  
  30. extern bfd_target a_out_adobe_vec;        /* Forward decl */
  31.  
  32. PROTO (static bfd_target *, aout_adobe_callback, (bfd *));
  33.  
  34. PROTO (boolean, aout_32_slurp_symbol_table, (bfd *abfd));
  35. PROTO (boolean , aout_32_write_syms, ());
  36. PROTO (static void, aout_adobe_write_section, (bfd *abfd, sec_ptr sect));
  37.  
  38. /* Swaps the information in an executable header taken from a raw byte
  39.    stream memory image, into the internal exec_header structure.  */
  40.  
  41. PROTO(void, aout_adobe_swap_exec_header_in,
  42.       (bfd *abfd,
  43.       struct external_exec *raw_bytes,
  44.       struct internal_exec *execp));
  45.      
  46. void
  47. DEFUN(aout_adobe_swap_exec_header_in,(abfd, raw_bytes, execp),
  48.       bfd *abfd AND
  49.       struct external_exec *raw_bytes AND
  50.       struct internal_exec *execp)
  51. {
  52.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  53.  
  54.   /* Now fill in fields in the execp, from the bytes in the raw data.  */
  55.   execp->a_info   = bfd_h_get_32 (abfd, bytes->e_info);
  56.   execp->a_text   = GET_WORD (abfd, bytes->e_text);
  57.   execp->a_data   = GET_WORD (abfd, bytes->e_data);
  58.   execp->a_bss    = GET_WORD (abfd, bytes->e_bss);
  59.   execp->a_syms   = GET_WORD (abfd, bytes->e_syms);
  60.   execp->a_entry  = GET_WORD (abfd, bytes->e_entry);
  61.   execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
  62.   execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
  63. }
  64.  
  65. /* Swaps the information in an internal exec header structure into the
  66.    supplied buffer ready for writing to disk.  */
  67.  
  68. PROTO(void, aout_adobe_swap_exec_header_out,
  69.       (bfd *abfd,
  70.        struct internal_exec *execp,
  71.        struct external_exec *raw_bytes));
  72. void
  73. DEFUN(aout_adobe_swap_exec_header_out,(abfd, execp, raw_bytes),
  74.      bfd *abfd AND
  75.      struct internal_exec *execp AND 
  76.      struct external_exec *raw_bytes)
  77. {
  78.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  79.  
  80.   /* Now fill in fields in the raw data, from the fields in the exec struct. */
  81.   bfd_h_put_32 (abfd, execp->a_info  , bytes->e_info);
  82.   PUT_WORD (abfd, execp->a_text  , bytes->e_text);
  83.   PUT_WORD (abfd, execp->a_data  , bytes->e_data);
  84.   PUT_WORD (abfd, execp->a_bss   , bytes->e_bss);
  85.   PUT_WORD (abfd, execp->a_syms  , bytes->e_syms);
  86.   PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
  87.   PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
  88.   PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
  89. }
  90.  
  91.  
  92. static bfd_target *
  93. aout_adobe_object_p (abfd)
  94.      bfd *abfd;
  95. {
  96.   struct internal_exec anexec;
  97.   struct external_exec exec_bytes;
  98.   char *targ;
  99.  
  100.   if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
  101.       != EXEC_BYTES_SIZE) {
  102.     bfd_error = wrong_format;
  103.     return 0;
  104.   }
  105.  
  106.   anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info);
  107.  
  108.   /* Normally we just compare for the magic number.
  109.      However, a bunch of Adobe tools aren't fixed up yet; they generate
  110.      files using ZMAGIC(!).
  111.      If the environment variable GNUTARGET is set to "a.out.adobe", we will
  112.      take just about any a.out file as an Adobe a.out file.  FIXME!  */
  113.  
  114.   if (N_BADMAG (anexec)) {
  115.     extern char *getenv ();
  116.  
  117.     targ = getenv ("GNUTARGET");
  118.     if (targ && !strcmp (targ, a_out_adobe_vec.name))
  119.       ;        /* Just continue anyway, if specifically set to this format */
  120.     else
  121.       {
  122.     bfd_error = wrong_format;
  123.     return 0;
  124.       }
  125.   }
  126.  
  127.   aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
  128.   return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
  129. }
  130.  
  131.  
  132. /* Finish up the opening of a b.out file for reading.  Fill in all the
  133.    fields that are not handled by common code.  */
  134.  
  135. static bfd_target *
  136. aout_adobe_callback (abfd)
  137.      bfd *abfd;
  138. {
  139.   struct internal_exec *execp = exec_hdr (abfd);
  140.   asection *sect;
  141.   struct external_segdesc ext[1];
  142.   char *section_name;
  143.   char try_again[30];    /* name and number */
  144.   char *newname;
  145.   int trynum;
  146.   flagword flags;
  147.  
  148.   /* Architecture and machine type -- unknown in this format.  */
  149.   bfd_set_arch_mach(abfd, bfd_arch_unknown, 0);
  150.  
  151.   /* The positions of the string table and symbol table.  */
  152.   obj_str_filepos (abfd) = N_STROFF (*execp);
  153.   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  154.  
  155.   /* Suck up the section information from the file, one section at a time.  */
  156.  
  157.   for (;;) {
  158.     if (bfd_read ((PTR) ext, 1, sizeof (*ext), abfd) != sizeof (*ext)) {
  159.       bfd_error = wrong_format;
  160.       return 0;
  161.     }
  162.     switch (ext->e_type[0]) {
  163.     case N_TEXT:
  164.       section_name = ".text";
  165.       flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  166.       break;
  167.  
  168.     case N_DATA:
  169.       section_name = ".data";
  170.       flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  171.       break;
  172.  
  173.     case N_BSS:
  174.       section_name = ".bss";
  175.       flags = SEC_DATA | SEC_HAS_CONTENTS;
  176.       break;
  177.  
  178.     case 0:
  179.       goto no_more_sections;
  180.  
  181.     default:
  182.       fprintf (stderr, "Unknown section type in a.out.adobe file: %x\n", 
  183.            ext->e_type[0]);
  184.       goto no_more_sections;
  185.     }
  186.  
  187.     /* First one is called ".text" or whatever; subsequent ones are
  188.        ".text1", ".text2", ... */
  189.  
  190.     bfd_error = no_error;
  191.     sect = bfd_make_section (abfd, section_name);
  192.     trynum = 0;
  193.     while (!sect) {
  194.       if (bfd_error != no_error)
  195.     return 0;    /* Some other error -- slide into the sunset */
  196.       sprintf (try_again, "%s%d", section_name, ++trynum);
  197.       sect = bfd_make_section (abfd, try_again);
  198.     }
  199.  
  200.     /* Fix the name, if it is a sprintf'd name.  */
  201.     if (sect->name == try_again) {
  202.       newname = (char *) bfd_zalloc(abfd, strlen (sect->name));
  203.       if (newname == NULL) {
  204.     bfd_error = no_memory;
  205.     return 0;
  206.       }
  207.       strcpy (newname, sect->name);
  208.       sect->name = newname;
  209.     }
  210.  
  211.     /* Now set the section's attributes.  */
  212.     bfd_set_section_flags (abfd, sect, flags);
  213.     sect->_raw_size = ((ext->e_size[0] << 8)    /* Assumed big-endian */
  214.               | ext->e_size[1] << 8)
  215.               | ext->e_size[2];
  216.     sect->_cooked_size = sect->_raw_size;
  217.     sect->vma = bfd_h_get_32 (abfd, ext->e_virtbase);
  218.     sect->filepos = bfd_h_get_32 (abfd, ext->e_filebase);
  219.     /* FIXME XXX alignment? */
  220.  
  221.     /* Set relocation information for first section of each type.  */
  222.     if (trynum == 0) switch (ext->e_type[0]) {
  223.     case N_TEXT:
  224.       sect->rel_filepos = N_TRELOFF (*execp);
  225.       sect->reloc_count = execp->a_trsize;
  226.       break;
  227.  
  228.     case N_DATA:
  229.       sect->rel_filepos = N_DRELOFF (*execp);
  230.       sect->reloc_count = execp->a_drsize;
  231.       break;
  232.     }
  233.   }
  234. no_more_sections:  
  235.  
  236.   adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external);
  237.   adata(abfd).symbol_entry_size = sizeof (struct external_nlist);
  238.   adata(abfd).page_size = 1; /* Not applicable. */
  239.   adata(abfd).segment_size = 1; /* Not applicable. */
  240.   adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  241.  
  242.   return abfd->xvec;
  243. }
  244.  
  245. struct bout_data_struct {
  246.     struct aoutdata a;
  247.     struct internal_exec e;
  248. };
  249.  
  250. static boolean
  251. aout_adobe_mkobject (abfd)
  252.      bfd *abfd;
  253. {
  254.   struct bout_data_struct *rawptr;
  255.  
  256.   rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct));
  257.   if (rawptr == NULL) {
  258.       bfd_error = no_memory;
  259.       return false;
  260.     }
  261.  
  262.   abfd->tdata.bout_data = rawptr;
  263.   exec_hdr (abfd) = &rawptr->e;
  264.  
  265.   adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external);
  266.   adata(abfd).symbol_entry_size = sizeof (struct external_nlist);
  267.   adata(abfd).page_size = 1; /* Not applicable. */
  268.   adata(abfd).segment_size = 1; /* Not applicable. */
  269.   adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  270.  
  271.   return true;
  272. }
  273.  
  274.  
  275. static boolean
  276. aout_adobe_write_object_contents (abfd)
  277.      bfd *abfd;
  278. {
  279.   struct external_exec swapped_hdr;
  280.   static struct external_segdesc sentinel[1];    /* Initialized to zero */
  281.   asection *sect;
  282.  
  283.   exec_hdr (abfd)->a_info = ZMAGIC;
  284.  
  285.   /* Calculate text size as total of text sections, etc. */
  286.  
  287.   exec_hdr (abfd)->a_text = 0;
  288.   exec_hdr (abfd)->a_data = 0;
  289.   exec_hdr (abfd)->a_bss  = 0;
  290.   exec_hdr (abfd)->a_trsize = 0;
  291.   exec_hdr (abfd)->a_drsize = 0;
  292.  
  293.   for (sect = abfd->sections; sect; sect = sect->next) {
  294.     if (sect->flags & SEC_CODE)    {
  295.       exec_hdr (abfd)->a_text += sect->_raw_size;
  296.       exec_hdr (abfd)->a_trsize += sect->reloc_count *
  297.                                    sizeof (struct reloc_std_external);
  298.     } else if (sect->flags & SEC_DATA)    {
  299.       exec_hdr (abfd)->a_data += sect->_raw_size;
  300.       exec_hdr (abfd)->a_drsize += sect->reloc_count *
  301.                                    sizeof (struct reloc_std_external);
  302.     } else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD)) {
  303.       exec_hdr (abfd)->a_bss  += sect->_raw_size;
  304.     }
  305.   }
  306.  
  307.   exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
  308.                     * sizeof (struct external_nlist);
  309.   exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
  310.  
  311.   aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
  312.  
  313.   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  314.   bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd);
  315.  
  316.   /* Now write out the section information.  Text first, data next, rest
  317.      afterward.  */
  318.  
  319.   for (sect = abfd->sections; sect; sect = sect->next) {
  320.     if (sect->flags & SEC_CODE)    {
  321.       aout_adobe_write_section (abfd, sect);
  322.     }
  323.   }
  324.   for (sect = abfd->sections; sect; sect = sect->next) {
  325.     if (sect->flags & SEC_DATA)    {
  326.       aout_adobe_write_section (abfd, sect);
  327.     }
  328.   }
  329.   for (sect = abfd->sections; sect; sect = sect->next) {
  330.     if (!(sect->flags & (SEC_CODE|SEC_DATA))) {
  331.       aout_adobe_write_section (abfd, sect);
  332.     }
  333.   }
  334.  
  335.   /* Write final `sentinel` section header (with type of 0).  */
  336.   bfd_write ((PTR) sentinel, 1, sizeof (*sentinel), abfd);
  337.  
  338.   /* Now write out reloc info, followed by syms and strings */
  339.   if (bfd_get_symcount (abfd) != 0) 
  340.     {
  341.       bfd_seek (abfd, (file_ptr)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
  342.  
  343.       if (! aout_32_write_syms (abfd))
  344.     return false;
  345.  
  346.       bfd_seek (abfd, (file_ptr)(N_TRELOFF(*exec_hdr(abfd))), SEEK_SET);
  347.  
  348.       for (sect = abfd->sections; sect; sect = sect->next) {
  349.         if (sect->flags & SEC_CODE)    {
  350.           if (!aout_32_squirt_out_relocs (abfd, sect))
  351.             return false;
  352.         }
  353.       }
  354.  
  355.       bfd_seek (abfd, (file_ptr)(N_DRELOFF(*exec_hdr(abfd))), SEEK_SET);
  356.  
  357.       for (sect = abfd->sections; sect; sect = sect->next) {
  358.         if (sect->flags & SEC_DATA)    {
  359.           if (!aout_32_squirt_out_relocs (abfd, sect))
  360.             return false;
  361.         }
  362.       }
  363.     }
  364.   return true;
  365. }
  366.  
  367. static void
  368. aout_adobe_write_section (abfd, sect)
  369.      bfd *abfd;
  370.      sec_ptr sect;
  371. {
  372.   /* FIXME XXX */
  373. }
  374.  
  375. static boolean
  376. aout_adobe_set_section_contents (abfd, section, location, offset, count)
  377.      bfd *abfd;
  378.      sec_ptr section;
  379.      unsigned char *location;
  380.      file_ptr offset;
  381.       int count;
  382. {
  383.   file_ptr section_start;
  384.   sec_ptr sect;
  385.  
  386.   if (abfd->output_has_begun == false) { /* set by bfd.c handler */
  387.  
  388.     /* Assign file offsets to sections.  Text sections are first, and
  389.        are contiguous.  Then data sections.  Everything else at the end.  */
  390.  
  391.     section_start = N_TXTOFF (ignore<-->me);
  392.  
  393.     for (sect = abfd->sections; sect; sect = sect->next) {
  394.       if (sect->flags & SEC_CODE)    {
  395.         sect->filepos = section_start;
  396.     /* FIXME:  Round to alignment */
  397.     section_start += sect->_raw_size;
  398.       }
  399.     }
  400.  
  401.     for (sect = abfd->sections; sect; sect = sect->next) {
  402.       if (sect->flags & SEC_DATA)    {
  403.         sect->filepos = section_start;
  404.     /* FIXME:  Round to alignment */
  405.     section_start += sect->_raw_size;
  406.       }
  407.     }
  408.  
  409.     for (sect = abfd->sections; sect; sect = sect->next) {
  410.       if (sect->flags & SEC_HAS_CONTENTS &&
  411.       !(sect->flags & (SEC_CODE|SEC_DATA)))    {
  412.         sect->filepos = section_start;
  413.     /* FIXME:  Round to alignment */
  414.     section_start += sect->_raw_size;
  415.       }
  416.     }
  417.   }
  418.  
  419.   /* regardless, once we know what we're doing, we might as well get going */
  420.   bfd_seek (abfd, section->filepos + offset, SEEK_SET);
  421.  
  422.   if (count != 0) {
  423.     return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
  424.   }
  425.   return true;
  426. }
  427.  
  428. static boolean
  429. aout_adobe_set_arch_mach (abfd, arch, machine)
  430.      bfd *abfd;
  431.      enum bfd_architecture arch;
  432.      unsigned long machine;
  433. {
  434.   bfd_default_set_arch_mach(abfd, arch, machine);
  435.  
  436.   if (arch == bfd_arch_unknown)    /* Unknown machine arch is OK */
  437.     return true;
  438.  
  439.   return false;
  440. }
  441.  
  442. static int 
  443. DEFUN(aout_adobe_sizeof_headers,(ignore_abfd, ignore),
  444.       bfd *ignore_abfd AND
  445.       boolean ignore)
  446. {
  447.   return sizeof(struct internal_exec);
  448. }
  449.  
  450.  
  451.  
  452.  
  453. /* Build the transfer vector for Adobe A.Out files.  */
  454.  
  455. /* We don't have core files.  */
  456. #define    aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command
  457. #define    aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal
  458. #define    aout_32_core_file_matches_executable_p    \
  459.                 _bfd_dummy_core_file_matches_executable_p
  460.  
  461. /* We use BSD-Unix generic archive files.  */
  462. #define    aout_32_openr_next_archived_file    bfd_generic_openr_next_archived_file
  463. #define    aout_32_generic_stat_arch_elt    bfd_generic_stat_arch_elt
  464. #define    aout_32_slurp_armap        bfd_slurp_bsd_armap
  465. #define    aout_32_slurp_extended_name_table    bfd_true
  466. #define    aout_32_write_armap        bsd_write_armap
  467. #define    aout_32_truncate_arname        bfd_bsd_truncate_arname
  468.  
  469. /* We override these routines from the usual a.out file routines.  */
  470. #define    aout_32_set_section_contents    aout_adobe_set_section_contents
  471. #define    aout_32_set_arch_mach        aout_adobe_set_arch_mach
  472. #define    aout_32_sizeof_headers        aout_adobe_sizeof_headers
  473.  
  474. #define aout_32_bfd_debug_info_start        bfd_void
  475. #define aout_32_bfd_debug_info_end        bfd_void
  476. #define aout_32_bfd_debug_info_accumulate    (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
  477.  
  478. #define aout_32_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
  479. #define aout_32_bfd_relax_section                   bfd_generic_relax_section
  480. #define aout_32_bfd_reloc_type_lookup \
  481.   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
  482. #define aout_32_bfd_make_debug_symbol \
  483.   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
  484. #define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  485. #define aout_32_bfd_link_add_symbols _bfd_generic_link_add_symbols
  486. #define aout_32_bfd_final_link _bfd_generic_final_link
  487.  
  488. bfd_target a_out_adobe_vec =
  489. {
  490.   "a.out.adobe",        /* name */
  491.   bfd_target_aout_flavour,
  492.   true,                /* data byte order is unknown (big assumed) */
  493.   true,                /* hdr byte order is big */
  494.   (HAS_RELOC | EXEC_P |        /* object flags */
  495.    HAS_LINENO | HAS_DEBUG |
  496.    HAS_SYMS | HAS_LOCALS | WP_TEXT ),
  497.   /* section flags */
  498.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
  499.   '_',                /*  symbol leading char */
  500.   ' ',                /* ar_pad_char */
  501.   16,                /* ar_max_namelen */
  502.   2,                /* minumum alignment power */
  503.  
  504.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  505.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  506.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  507.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  508.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  509.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  510.  {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format */
  511.    bfd_generic_archive_p, _bfd_dummy_target},
  512.  {bfd_false, aout_adobe_mkobject, /* bfd_set_format */
  513.    _bfd_generic_mkarchive, bfd_false},
  514.  {bfd_false, aout_adobe_write_object_contents, /* bfd_write_contents */
  515.    _bfd_write_archive_contents, bfd_false},
  516.  
  517.   JUMP_TABLE(aout_32),
  518.   (PTR) 0
  519. };
  520.