home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / c / faslsgi4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  10.7 KB  |  471 lines

  1. /*
  2.  Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
  3.  
  4. This file is part of GNU Common Lisp, herein referred to as GCL
  5.  
  6. GCL is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GCL is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
  14. License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public License 
  17. along with GCL; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. */
  21.  
  22.  
  23. /* make sure we do allocate aligned for double */
  24. /* actually I understand that ld -A wants alignment on
  25.    the page.  ie multiple of 0x1000
  26. */   
  27.  
  28. #define ALIGN 12
  29. char *
  30. alloc_contblock_aligned(size)
  31. int size;
  32. {
  33.    char *tmp_alloc = ALLOC_ALIGNED(alloc_contblock,size,(1<<12));
  34.    bzero(tmp_alloc, size);
  35.    return(tmp_alloc);
  36.  }
  37.  
  38. #define alloc_contblock alloc_contblock_aligned
  39.  
  40.  
  41.  
  42. #ifdef BSD
  43. #include <a.out.h>
  44. #endif
  45.  
  46. #ifdef ATT
  47. #ifdef mips
  48. #include <unistd.h>
  49. #include <aouthdr.h>
  50. #endif
  51. #include <filehdr.h>
  52. #include <scnhdr.h>
  53. #include <syms.h>
  54. #endif
  55.  
  56. #ifdef E15
  57. #include <a.out.h>
  58. #define exec        bhdr
  59. #define a_text        tsize
  60. #define a_data        dsize
  61. #define a_bss        bsize
  62. #define a_syms        ssize
  63. #define a_trsize    rtsize
  64. #define a_drsize    rdsize
  65. #endif
  66.  
  67.  
  68. #define    MAXPATHLEN    1024
  69.  
  70.  
  71. int
  72. fasload(faslfile)
  73. object faslfile;
  74. {
  75.  
  76. #ifdef BSD
  77.     struct exec header, newheader;
  78. #define    textsize    header.a_text
  79. #define    datasize    header.a_data
  80. #define    bsssize        header.a_bss
  81. #define    textstart    sizeof(header)
  82. #define    newbsssize    newheader.a_bss
  83. #endif
  84.  
  85. #ifdef ATT
  86.     struct filehdr fileheader;
  87.     struct scnhdr sectionheader;
  88. #ifdef mips
  89.     struct aouthdr aouthdr, newaouthdr;
  90.     HDRR symhdr;
  91. # define textsize    aouthdr.tsize
  92. # define datasize    aouthdr.dsize
  93. # define bsssize    aouthdr.bsize
  94. # define textstart    sectionheader.s_scnptr
  95. # define newdatasize    newaouthdr.dsize
  96. # define newbsssize    newaouthdr.bsize
  97. #else
  98.     int textsize, datasize, bsssize;
  99.     int textstart;
  100. #endif /* mips */
  101. #endif
  102.  
  103. #ifdef E15
  104.     struct exec header;
  105. #define    textsize    header.a_text
  106. #define    datasize    header.a_data
  107. #define    bsssize        header.a_bss
  108. #define    textstart    sizeof(header)
  109. #endif
  110.  
  111.     object memory, data, tempfile;
  112.     FILE *fp;
  113.     char filename[MAXPATHLEN];
  114.     char tempfilename[32];
  115.     char command[MAXPATHLEN * 2];
  116.     int i;
  117.     object *old_vs_base = vs_base;
  118.     object *old_vs_top = vs_top;
  119. #ifdef IBMRT
  120.  
  121. #endif
  122.  
  123.     coerce_to_filename(faslfile, filename);
  124.  
  125.     faslfile = open_stream(faslfile, smm_input, Cnil, Kerror);
  126.     vs_push(faslfile);
  127.     fp = faslfile->sm.sm_fp;
  128.  
  129. #ifdef BSD
  130.     fread(&header, sizeof(header), 1, fp);
  131. #endif
  132. #ifdef ATT
  133.     fread(&fileheader, sizeof(fileheader), 1, fp);
  134. #ifdef mips
  135.     fread(&aouthdr, AOUTHSZ, 1, fp);
  136. #else
  137. #ifdef S3000
  138.         if(fileheader.f_opthdr != 0) fseek(fp,fileheader.f_opthdr,1);
  139. #endif
  140.     fread(§ionheader, sizeof(sectionheader), 1, fp);
  141.     textsize = sectionheader.s_size;
  142.     textstart = sectionheader.s_scnptr;
  143.     fread(§ionheader, sizeof(sectionheader), 1, fp);
  144.     datasize = sectionheader.s_size;
  145.     fread(§ionheader, sizeof(sectionheader), 1, fp);
  146.     if (strcmp(sectionheader.s_name, ".bss") == 0)
  147.         bsssize = sectionheader.s_size;
  148.     else
  149.         bsssize = 0;
  150. #endif /* mips */
  151. #endif
  152. #ifdef E15
  153.     fread(&header, sizeof(header), 1, fp);
  154. #endif
  155.  
  156.     memory = alloc_object(t_cfdata);
  157.     memory->cfd.cfd_self = NULL;
  158.     memory->cfd.cfd_start = NULL;
  159.     memory->cfd.cfd_size = textsize + datasize + bsssize;
  160. #ifdef mips
  161. #define MIPS_ROUND 0xC    
  162.     memory->cfd.cfd_size += MIPS_ROUND; /* room for 'ld' to round text upward */
  163. #endif
  164.     vs_push(memory);
  165.     memory->cfd.cfd_start = alloc_contblock(memory->cfd.cfd_size);
  166.  
  167. #ifdef BSD
  168.     fseek(fp,
  169.           header.a_text+header.a_data+
  170.           header.a_syms+header.a_trsize+header.a_drsize,
  171.           1);
  172.     fread(&i, sizeof(i), 1, fp);
  173.     fseek(fp, i - sizeof(i), 1);
  174. #endif
  175.  
  176. #ifdef ATT
  177. #ifdef mips
  178.     fseek(fp, fileheader.f_symptr, SEEK_SET);
  179.     fread(&symhdr, cbHDRR, 1, fp);
  180.     fseek(fp, symhdr.cbExtOffset + symhdr.iextMax * cbEXTR, SEEK_SET);
  181. #else
  182.     fseek(fp,
  183.           fileheader.f_symptr + SYMESZ*fileheader.f_nsyms,
  184.           0);
  185.     fread(&i, sizeof(i), 1, fp);
  186.     fseek(fp, i - sizeof(i), 1);
  187.     while ((i = getc(fp)) == 0)
  188.         ;
  189.     ungetc(i, fp);
  190. #endif /* mips */
  191. #endif
  192.  
  193. #ifdef E15
  194.     fseek(fp,
  195.           header.a_text+header.a_data+
  196.           header.a_syms+header.a_trsize+header.a_drsize,
  197.           1);
  198. #endif
  199.  
  200.     data = read_fasl_vector(faslfile);
  201.     vs_push(data);
  202.     close_stream(faslfile, TRUE);
  203.  
  204.     sprintf(tempfilename, "/tmp/fasltemp%d", getpid());
  205.  
  206. AGAIN:
  207.  
  208. #ifdef BSD
  209.     sprintf(command,
  210.         "ld -d -N -x -A %s -T %x %s -o %s",
  211.         kcl_self,
  212.         memory->cfd.cfd_start,
  213.         filename,
  214.         tempfilename);
  215. #endif
  216. #ifdef ATT
  217. #ifdef mips
  218.     sprintf(command,
  219.         "ld -s -A %s -N -T %x %s -o %s",
  220.         kcl_self,
  221.         (long)memory->cfd.cfd_start+SCNROUND-1&~(SCNROUND-1),
  222.         filename,
  223.         tempfilename);
  224. #else
  225.     coerce_to_filename(symbol_value(siVsystem_directory),
  226.                system_directory);
  227.     sprintf(command,
  228.         "%sild %s %d %s %s",
  229.         system_directory,
  230.         kcl_self,
  231.         memory->cfd.cfd_start,
  232.         filename,
  233.         tempfilename);
  234. #endif /* mips */
  235. #endif
  236. #ifdef E15
  237.     coerce_to_filename(symbol_value(siVsystem_directory),
  238.                system_directory);
  239.     sprintf(command,
  240.         "%sild %s %d %s %s",
  241.         system_directory,
  242.         kcl_self,
  243.         memory->cfd.cfd_start,
  244.         filename,
  245.         tempfilename);
  246. #endif
  247.  
  248.     if (system(command) != 0)
  249.         FEerror("The linkage editor failed.", 0);
  250.  
  251.     tempfile = make_simple_string(tempfilename);
  252.     vs_push(tempfile);
  253.     tempfile = open_stream(tempfile, smm_input, Cnil, Kerror);
  254.     vs_push(tempfile);
  255.     fp = tempfile->sm.sm_fp;
  256.  
  257. #ifdef BSD
  258.     fread(&newheader, sizeof(header), 1, fp);
  259.     if (newbsssize != bsssize) {
  260.         insert_contblock(memory->cfd.cfd_start, memory->cfd.cfd_size);
  261.         bsssize = newbsssize;
  262.         memory->cfd.cfd_start = NULL;
  263.         memory->cfd.cfd_size = textsize + datasize + bsssize;
  264.         memory->cfd.cfd_start = alloc_contblock(memory->cfd.cfd_size);
  265.         close_stream(tempfile, TRUE);
  266.         unlink(tempfilename);
  267.         goto AGAIN;
  268.     }
  269. #endif
  270. #ifdef mips
  271.     fseek(fp, FILHSZ, SEEK_CUR);
  272.     fread(&newaouthdr, AOUTHSZ, 1, fp);
  273.     if (newdatasize + newbsssize > datasize + bsssize) {
  274.         insert_contblock(memory->cfd.cfd_start, memory->cfd.cfd_size);
  275.         datasize = newdatasize;
  276.         bsssize = newbsssize;
  277.         memory->cfd.cfd_start = NULL;
  278.         memory->cfd.cfd_size = textsize + datasize + bsssize + MIPS_ROUND;
  279.         memory->cfd.cfd_start = alloc_contblock(memory->cfd.cfd_size);
  280.         close_stream(tempfile, TRUE);
  281.         unlink(tempfilename);
  282.         goto AGAIN;
  283.     }
  284.     fread(§ionheader, sizeof sectionheader, 1, fp);
  285. #endif
  286.     if (fseek(fp, textstart, 0) < 0)
  287.         error("file seek error");
  288. #ifdef mips
  289.     printf("start address -T %x ",memory->cfd.cfd_start);
  290.     bzero(memory->cfd.cfd_start, MIPS_ROUND);
  291.     fread(sectionheader.s_vaddr, textsize + datasize, 1, fp);
  292. #else
  293.     fread(memory->cfd.cfd_start, textsize + datasize, 1, fp);
  294. #endif
  295.     close_stream(tempfile, TRUE);
  296.  
  297.     unlink(tempfilename);
  298.  
  299.     call_init(0,memory,data);
  300.  
  301.     vs_base = old_vs_base;
  302.     vs_top = old_vs_top;
  303.  
  304.     return(memory->cfd.cfd_size);
  305. }
  306.  
  307. #if defined BSD || defined mips
  308.  
  309. int
  310. faslink(faslfile, ldargstring)
  311. object faslfile, ldargstring;
  312. {
  313. #ifdef mips
  314.     struct filehdr faslheader;
  315.     struct aouthdr aouthdr;
  316.     struct scnhdr sectionheader;
  317.     HDRR symhdr;
  318. #define ldcmdfmt    "ld -s -A %s -N -T %x %s %s -o %s"
  319. #else
  320.     struct exec header, faslheader;
  321. #define    textsize    header.a_text
  322. #define    datasize    header.a_data
  323. #define    bsssize        header.a_bss
  324. #define    textstart    sizeof(header)
  325. #define ldcmdfmt    "ld -d -N -x -A %s -T %x %s %s -o %s"
  326. #endif
  327.  
  328.     object memory, data, tempfile;
  329.     FILE *fp;
  330.     char filename[MAXPATHLEN];
  331.     char ldargstr[MAXPATHLEN];
  332.     char tempfilename[32];
  333.     char command[MAXPATHLEN * 2];
  334.     char buf[BUFSIZ];
  335.     int i;
  336.     object *old_vs_base = vs_base;
  337.     object *old_vs_top = vs_top;
  338. #ifdef IBMRT
  339.  
  340. #endif
  341.  
  342.     coerce_to_filename(ldargstring, ldargstr);
  343.     coerce_to_filename(faslfile, filename);
  344.  
  345.     sprintf(tempfilename, "/tmp/fasltemp%d", getpid());
  346.  
  347.     sprintf(command,
  348.         ldcmdfmt,
  349.         kcl_self,
  350.         (int)core_end,
  351.         filename,
  352.         ldargstr,
  353.         tempfilename);
  354.  
  355.     if (system(command) != 0)
  356.         FEerror("The linkage editor failed.", 0);
  357.  
  358.     fp = fopen(tempfilename, "r");
  359.     setbuf(fp, buf);
  360. #ifdef mips
  361.     fseek(fp, FILHSZ, SEEK_CUR);
  362.     fread(&aouthdr, AOUTHSZ, 1, fp);
  363. #else
  364.     fread(&header, sizeof(header), 1, fp);
  365. #endif
  366.     memory = alloc_object(t_cfdata);
  367.     memory->cfd.cfd_self = NULL;
  368.     memory->cfd.cfd_start = NULL;
  369.     memory->cfd.cfd_size = textsize + datasize + bsssize;
  370. #ifdef mips
  371.     memory->cfd.cfd_size += MIPS_ROUND;
  372. #endif
  373.     vs_push(memory);
  374.     memory->cfd.cfd_start = alloc_contblock(memory->cfd.cfd_size);
  375.     fclose(fp);
  376.  
  377.     faslfile = open_stream(faslfile, smm_input, Cnil, Kerror);
  378.     vs_push(faslfile);
  379.     fp = faslfile->sm.sm_fp;
  380.     fread(&faslheader, sizeof(faslheader), 1, fp);
  381. #ifdef mips
  382.     fseek(fp, AOUTHSZ, SEEK_CUR);
  383.     fread(§ionheader, SCNHSZ, 1, fp);
  384.     fseek(fp, faslheader.f_symptr, SEEK_SET);
  385.     fread(&symhdr, cbHDRR, 1, fp);
  386.     fseek(fp, symhdr.cbExtOffset + symhdr.iextMax * cbEXTR, SEEK_SET);
  387. #else
  388.     fseek(fp,
  389.           faslheader.a_text+faslheader.a_data+
  390.           faslheader.a_syms+faslheader.a_trsize+faslheader.a_drsize,
  391.           1);
  392.     fread(&i, sizeof(i), 1, fp);
  393.     fseek(fp, i - sizeof(i), 1);
  394. #endif
  395.     data = read_fasl_vector(faslfile);
  396.     vs_push(data);
  397.     close_stream(faslfile, TRUE);
  398.  
  399.     sprintf(command,
  400.         ldcmdfmt,
  401.         kcl_self,
  402. #ifdef mips
  403.         (long)memory->cfd.cfd_start+SCNROUND-1&~(SCNROUND-1),
  404. #else
  405.         memory->cfd.cfd_start,
  406. #endif
  407.         filename,
  408.         ldargstr,
  409.         tempfilename);
  410.  
  411.     if (system(command) != 0)
  412.         FEerror("The linkage editor failed.", 0);
  413.  
  414.     tempfile = make_simple_string(tempfilename);
  415.     vs_push(tempfile);
  416.     tempfile = open_stream(tempfile, smm_input, Cnil, Kerror);
  417.     vs_push(tempfile);
  418.     fp = tempfile->sm.sm_fp;
  419.  
  420. #ifdef mips
  421.     fseek(fp, FILHSZ, SEEK_CUR);
  422.     fread(&aouthdr, AOUTHSZ, 1, fp);
  423.     fread(§ionheader, sizeof sectionheader, 1, fp);
  424. #endif
  425.  
  426.     if (fseek(fp, textstart, 0) < 0)
  427.         error("file seek error");
  428. #ifdef mips
  429.     printf("start address -T %x ",memory->cfd.cfd_start);
  430.     bzero(memory->cfd.cfd_start, MIPS_ROUND);
  431.     fread(sectionheader.s_vaddr, textsize + datasize, 1, fp);
  432. #else
  433.     fread(memory->cfd.cfd_start, textsize + datasize, 1, fp);
  434. #endif
  435.     close_stream(tempfile, TRUE);
  436.  
  437.     unlink(tempfilename);
  438.  
  439.     call_init(0,memory,data);
  440.  
  441.     vs_base = old_vs_base;
  442.     vs_top = old_vs_top;
  443.  
  444.     return(memory->cfd.cfd_size);
  445. }
  446.  
  447. siLfaslink()
  448. {
  449.     bds_ptr old_bds_top;
  450.     int i;
  451.     object package;
  452.  
  453.     check_arg(2);
  454.     check_type_or_pathname_string_symbol_stream(&vs_base[0]);
  455.     check_type_string(&vs_base[1]);
  456.     vs_base[0] = coerce_to_pathname(vs_base[0]);
  457.     vs_base[0]->pn.pn_type = FASL_string;
  458.     vs_base[0] = namestring(vs_base[0]);
  459.     package = symbol_value(Vpackage);
  460.     old_bds_top = bds_top;
  461.     bds_bind(Vpackage, package);
  462.     i = faslink(vs_base[0], vs_base[1]);
  463.     bds_unwind(old_bds_top);
  464.     vs_top = vs_base;
  465.     vs_push(make_fixnum(i));
  466. }
  467.  
  468. #endif
  469.  
  470. #define FASLINK
  471.