home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / unexapollo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  11.0 KB  |  296 lines

  1. /* unexapollo.c -- COFF File UNEXEC for GNU Emacs on Apollo SR10.x
  2.    Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.29. */
  21.  
  22. /* Written by Leonard N. Zubkoff.  */
  23.  
  24. #include "config.h"
  25. #include <fcntl.h>
  26.  
  27.  
  28. #include <a.out.h>
  29. #include <sys/file.h>
  30. #include <apollo/base.h>
  31. #include <apollo/ios.h>
  32. #include <apollo/type_uids.h>
  33. #include <apollo/dst.h>
  34.  
  35.  
  36. #define DST_RECORD_HDR_SIZE    2
  37. #define LONG_ALIGN(X)        (((X)+3)&(~3))
  38.  
  39.  
  40. void
  41. unexec (target_file_name, source_file_name)
  42.      char *target_file_name, *source_file_name;
  43. {
  44.   struct filehdr file_header;
  45.   struct aouthdr domain_header;
  46.   struct scnhdr *section, *sections, *sections_limit;
  47.   struct scnhdr *first_data_section, *last_data_section;
  48.   struct scnhdr *rwdi_section, *blocks_section;
  49.   struct reloc reloc_entry;
  50.   unsigned long data_size, old_data_section_size, source_file_offset_past_rwdi;
  51.   unsigned char buffer[4096];
  52.   long delta_before_rwdi, delta_after_rwdi, byte_count;
  53.   long first_changed_vaddr, old_rwdi_vaddr, i;
  54.   ios_$id_t target_file, source_file;
  55.   status_$t status;
  56.   /* Open the Source File. */
  57.   if ((source_file = open (source_file_name, O_RDONLY)) < 0)
  58.     error ("cannot open source file for input");
  59.   /* Read the File Header. */
  60.   if (read (source_file, &file_header, sizeof (file_header)) != sizeof (file_header))
  61.     error ("cannot read file header");
  62.   
  63.   /* Read the Domain Header. */
  64.   if (read (source_file, &domain_header, sizeof (domain_header))
  65.       != sizeof (domain_header))
  66.     error ("cannot read domain header");
  67.   /* Read the Section Headers. */
  68.   sections =
  69.     (struct scnhdr *) malloc (file_header.f_nscns*sizeof (struct scnhdr));
  70.   if (sections == (struct scnhdr *) 0)
  71.     error ("cannot allocate section header storage");
  72.   sections_limit = sections + file_header.f_nscns;
  73.   if (read (source_file, sections, file_header.f_nscns*sizeof (struct scnhdr))
  74.       != file_header.f_nscns*sizeof (struct scnhdr))
  75.     error ("cannot read section headers");
  76.   /* Compute the new Size of the Data Section. */
  77.   data_size = sbrk (0) - domain_header.data_start;
  78.   delta_before_rwdi = delta_after_rwdi = data_size - domain_header.dsize;
  79.   old_rwdi_vaddr = 0;
  80.   /* Find and Deallocate the .rwdi Section Information. */
  81.   for (rwdi_section = sections; rwdi_section != sections_limit; rwdi_section++)
  82.     if (strcmp (rwdi_section->s_name, ".rwdi") == 0)
  83.       {
  84.     /* If there are relocation entries, we cannot "unrelocate" them. */
  85.     if (rwdi_section->s_nreloc > 0)
  86.       error (".rwdi section needs relocation - cannot dump Emacs");
  87.     delta_after_rwdi = delta_before_rwdi - rwdi_section->s_size;
  88.     old_rwdi_vaddr = rwdi_section->s_vaddr;
  89.     rwdi_section->s_paddr = 0;
  90.     rwdi_section->s_vaddr = 0;
  91.     rwdi_section->s_scnptr = 0;
  92.     rwdi_section->s_size = 0;
  93.     source_file_offset_past_rwdi = (rwdi_section+1)->s_scnptr;
  94.     break;
  95.       }
  96.   /* Skip over the Text Section Headers. */
  97.   for (section = sections; (section->s_flags & STYP_TEXT) != 0; section++) ;
  98.   /*
  99.     Find the First and Last Data Sections and Fixup
  100.     Section Header Relocation Pointers.
  101.     */
  102.   first_data_section = last_data_section = (struct scnhdr *) 0;
  103.   for (; section != sections_limit; section++)
  104.     {
  105.       if ((section->s_flags & STYP_DATA) != 0)
  106.     {
  107.       if (first_data_section == (struct scnhdr *) 0)
  108.         first_data_section = section;
  109.       last_data_section = section;
  110.     }
  111.       if (section->s_relptr != 0)
  112.     section->s_relptr += delta_after_rwdi;
  113.     }
  114.   /* Increment the Size of the Last Data Section. */
  115.   old_data_section_size = last_data_section->s_size;
  116.   last_data_section->s_size += delta_before_rwdi;
  117.   
  118.   /* Update the File Header and Domain Header. */
  119.   file_header.f_symptr += delta_after_rwdi;
  120.   domain_header.dsize = data_size;
  121.   domain_header.bsize = 0;
  122.   /* Skip over subsequent Bss Section Headers. */
  123.   for (section = last_data_section+1;
  124.        (section->s_flags & STYP_BSS) != 0; section++) ;
  125.   /* Update the remaining Section Headers. */
  126.   blocks_section = (struct scnhdr *) 0;
  127.   first_changed_vaddr = 0;
  128.   for (; section != sections_limit; section++)
  129.     {
  130.       long delta = (section < rwdi_section ? delta_before_rwdi : delta_after_rwdi);
  131.       if (section->s_paddr != 0)
  132.     section->s_paddr += delta;
  133.       if (section->s_vaddr != 0)
  134.     {
  135.       if (first_changed_vaddr == 0)
  136.         first_changed_vaddr = section->s_vaddr;
  137.       section->s_vaddr += delta;
  138.     }
  139.       if (section->s_scnptr != 0)
  140.     section->s_scnptr += delta;
  141.       if (strcmp (section->s_name, ".blocks") == 0)
  142.     blocks_section = section;
  143.       else if (strcmp (section->s_name, ".sri") == 0 &&
  144.            domain_header.o_sri != 0)
  145.     domain_header.o_sri += delta;
  146.       else if (strcmp (section->s_name, ".inlib") == 0 &&
  147.            domain_header.o_inlib != 0)
  148.     domain_header.o_inlib += delta;
  149.     }
  150.   /* Open the Target File. */
  151.   ios_$create (target_file_name, strlen (target_file_name), coff_$uid,
  152.            ios_$recreate_mode, ios_$write_opt, &target_file, &status);
  153.   if (status.all != status_$ok)
  154.     error ("cannot open target file for output");
  155.   /* Write the File Header. */
  156.   if (write (target_file, &file_header, sizeof (file_header)) != sizeof (file_header))
  157.     error ("cannot write file header");
  158.   /* Write the Domain Header. */
  159.   if (write (target_file, &domain_header, sizeof (domain_header))
  160.       != sizeof (domain_header))
  161.     error ("cannot write domain header");
  162.   /* Write the Section Headers. */
  163.   if (write (target_file, sections, file_header.f_nscns*sizeof (struct scnhdr))
  164.       != file_header.f_nscns*sizeof (struct scnhdr))
  165.     error ("cannot write section headers");
  166.   /* Copy the Allocated Sections. */
  167.   for (section = sections; section != first_data_section; section++)
  168.     if (section->s_scnptr != 0)
  169.       CopyData (target_file, source_file, LONG_ALIGN(section->s_size));
  170.   /* Write the Expanded Data Segment. */
  171.   if (write (target_file, first_data_section->s_vaddr, data_size) != data_size)
  172.     error ("cannot write new data section");
  173.   
  174.   /* Skip over the Last Data Section and Copy until the .rwdi Section. */
  175.   if (lseek (source_file, last_data_section->s_scnptr
  176.          +old_data_section_size, L_SET) == -1)
  177.     error ("cannot seek past data section");
  178.   for (section = last_data_section+1; section != rwdi_section; section++)
  179.     if (section->s_scnptr != 0)
  180.       CopyData (target_file, source_file, LONG_ALIGN(section->s_size));
  181.   /* Skip over the .rwdi Section and Copy Remainder of Source File. */
  182.   if (lseek (source_file, source_file_offset_past_rwdi, L_SET) == -1)
  183.     error ("cannot seek past .rwdi section");
  184.   while ((byte_count = read (source_file, buffer, sizeof (buffer))) > 0)
  185.     if (write (target_file, buffer, byte_count) != byte_count)
  186.       error ("cannot write data");
  187.   /* Unrelocate .data references to Global Symbols. */
  188.   for (section = first_data_section; section <= last_data_section; section++)
  189.     for (i = 0; i < section->s_nreloc; i++)
  190.       {
  191.     if  (lseek (source_file, section->s_relptr
  192.             +i*sizeof (struct reloc)-delta_after_rwdi, L_SET) == -1)
  193.       error ("cannot seek to relocation info");
  194.     if (read (source_file, &reloc_entry, sizeof (reloc_entry))
  195.         != sizeof (reloc_entry))
  196.       error ("cannot read reloc entry");
  197.     if (lseek (source_file, reloc_entry.r_vaddr-section->s_vaddr
  198.            +section->s_scnptr, L_SET) == -1)
  199.       error ("cannot seek to data element");
  200.     if (lseek (target_file, reloc_entry.r_vaddr-section->s_vaddr
  201.            +section->s_scnptr, L_SET) == -1)
  202.       error ("cannot seek to data element");
  203.     if (read (source_file, buffer, 4) != 4)
  204.       error ("cannot read data element");
  205.     if (write (target_file, buffer, 4) != 4)
  206.       error ("cannot write data element");
  207.       }
  208.   
  209.   /* Correct virtual addresses in .blocks section. */
  210.   if (blocks_section != (struct scnhdr *) 0)
  211.     {
  212.       dst_rec_t dst_record;
  213.       dst_rec_comp_unit_t *comp_unit;
  214.       unsigned short number_of_sections;
  215.       unsigned long section_base;
  216.       unsigned long section_offset = 0;
  217.       /* Find section tables and update section base addresses. */
  218.       while (section_offset < blocks_section->s_size)
  219.     {
  220.       if (lseek (target_file,
  221.              blocks_section->s_scnptr+section_offset, L_SET) == -1)
  222.         error ("cannot seek to comp unit record");
  223.       /* Handle pad records before the comp unit record. */
  224.       if (read (target_file, &dst_record, DST_RECORD_HDR_SIZE)
  225.           != DST_RECORD_HDR_SIZE)
  226.         error ("cannot read dst record tag");
  227.       if (dst_record.rec_type == dst_typ_pad)
  228.         section_offset += DST_RECORD_HDR_SIZE;
  229.       else if (dst_record.rec_type == dst_typ_comp_unit)
  230.         {
  231.           comp_unit = &dst_record.rec_data.comp_unit_;
  232.           if  (read (target_file, comp_unit, sizeof (*comp_unit))
  233.            != sizeof (*comp_unit))
  234.         error ("cannot read comp unit record");
  235.           if (lseek (target_file, blocks_section->s_scnptr
  236.              +section_offset
  237. #if dst_version_major == 1 && dst_version_minor < 4
  238.              +comp_unit->section_table
  239. #else
  240.              +comp_unit->section_table.rel_offset
  241. #endif
  242.              +DST_RECORD_HDR_SIZE,
  243.              L_SET) == -1)
  244.         error ("cannot seek to section table");
  245.           if (read (target_file, &number_of_sections, sizeof (number_of_sections))
  246.           != sizeof (number_of_sections))
  247.         error ("cannot read section table size");
  248.           for (i = 0; i < number_of_sections; i++)
  249.         {
  250.           if (read (target_file, §ion_base, sizeof (section_base))
  251.               != sizeof (section_base))
  252.             error ("cannot read section base value");
  253.           if (section_base < first_changed_vaddr)
  254.             continue;
  255.           else if (section_base < old_rwdi_vaddr)
  256.             section_base += delta_before_rwdi;
  257.           else section_base += delta_after_rwdi;
  258.           if (lseek (target_file, -sizeof (section_base), L_INCR) == -1)
  259.             error ("cannot seek to section base value");
  260.           if (write (target_file, §ion_base, sizeof (section_base))
  261.               != sizeof (section_base))
  262.             error ("cannot write section base");
  263.         }
  264.           section_offset += comp_unit->data_size;
  265.         }
  266.       else error ("unexpected dst record type");
  267.     }
  268.     }
  269.   
  270.   if (close (source_file) == -1)
  271.     error("cannot close source file");
  272.   if (close (target_file) == -1)
  273.     error ("cannot close target file");
  274. }
  275.  
  276.  
  277. static
  278. CopyData (target_file, source_file, total_byte_count)
  279.      int target_file, source_file;
  280.      long total_byte_count;
  281. {
  282.   unsigned char buffer[4096];
  283.   long byte_count;
  284.   while (total_byte_count > 0)
  285.     {
  286.       if (total_byte_count > sizeof (buffer))
  287.     byte_count = sizeof (buffer);
  288.       else byte_count = total_byte_count;
  289.       if (read (source_file, buffer, byte_count) != byte_count)
  290.     error ("cannot read data");
  291.       if (write (target_file, buffer, byte_count) != byte_count)
  292.     error ("cannot write data");
  293.       total_byte_count -= byte_count;
  294.     }
  295. }
  296.