Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
dra_file_util.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 // ====================================================================
00037 // ====================================================================
00038 //
00039 //
00040 // Revision history:
00041 //  2-Aug-96: Original Version
00042 //
00043 // Description:
00044 //  File utility routines used in processing .rii files
00045 //
00046 // ====================================================================
00047 // ====================================================================
00048 
00049 #include <fcntl.h>              // open()
00050 #include <unistd.h>             // close()
00051 #include <sys/stat.h>           // fstat() 
00052 #include <sys/mman.h>           // mmap()
00053 #include <sys/types.h>          // off_t, size_t
00054 #include <sys/param.h>          // MAXPATHLEN
00055 
00056 #include "defs.h"               // Standard definitions
00057 #include "x_libgen.h"           // for basename(), dirname()
00058 #include "glob.h"               // Src_File_Name
00059 #include "erbe.h"               // EC_DRA_*
00060 #include "erglob.h"             // EC_No_Mem
00061 #include "file_util.h"          // New_Extension
00062 
00063 #include "dra_internal.h"       // Internal DRA interface
00064 
00065 
00066 // =====================================================================
00067 // 
00068 //                      Exported variables
00069 //
00070 // =====================================================================
00071 
00072 INT DRA_file_desc = -1;
00073 
00074 char *DRA_file_mmap = NULL;
00075 
00076 char DRA_file_name[MAXPATHLEN];
00077 
00078 
00079 // =====================================================================
00080 // 
00081 //                      Local function prototypes
00082 //
00083 // =====================================================================
00084 
00085 static void DRA_Make_File_Name();
00086 
00087 // =====================================================================
00088 // 
00089 //                      File static variables
00090 //
00091 // =====================================================================
00092 
00093 static char *DRA_keep_old_file;
00094 
00095 static off_t DRA_file_size, DRA_bytes_to_keep;
00096 
00097 static const char* DRA_DIRECTORY = "/rii_files/"; // Directory for .rii files
00098 
00099 static const char* DRA_FILE_EXTENSION = ".rii";   // Prelinker file extension
00100 
00101 
00102 // =====================================================================
00103 // 
00104 //                      Exported function definitions
00105 //
00106 // =====================================================================
00107 
00108 
00109 // =====================================================================
00110 //
00111 // Function Name: DRA_Open_And_Map_File
00112 //
00113 // Description: Open the file and perform memory mapping.
00114 //
00115 // =====================================================================
00116 
00117 void 
00118 DRA_Open_And_Map_File()
00119 {
00120   Set_Error_Phase("Reading prelinker file");
00121 
00122   DRA_Make_File_Name();
00123 
00124   struct stat stat_buf;
00125   errno = 0;
00126 
00127   DRA_file_desc = open(DRA_file_name, O_RDONLY);
00128 
00129   if (DRA_file_desc < 0 || fstat(DRA_file_desc, &stat_buf) != 0) {
00130         close(DRA_file_desc);
00131         ErrMsg(EC_DRA_rii_file_io, DRA_file_name, errno);
00132         return;
00133   }
00134 
00135   DRA_file_mmap = (char *) mmap(0, 
00136                                 stat_buf.st_size, 
00137                                 PROT_READ|PROT_WRITE, 
00138                                 MAP_PRIVATE, 
00139                                 DRA_file_desc, 
00140                                 0);
00141 
00142   if (DRA_file_mmap == (char *)(MAP_FAILED)) {
00143         close(DRA_file_desc);
00144         ErrMsg(EC_DRA_rii_file_io, DRA_file_name, errno);
00145         return;
00146   }
00147 
00148   close(DRA_file_desc);
00149 
00150   // If everything is fine, save the size of the .rii file
00151   //
00152   DRA_file_size = stat_buf.st_size;
00153 
00154   // Find the size of the first two sections of the .rii file
00155   // CMDLINE, PWD
00156   // ----
00157   // Cloning requests
00158   // ----
00159   // Common layout info
00160   //
00161   char *first_sep = strstr(DRA_file_mmap, DRA_FILE_SEPARATOR);
00162 
00163   // The first separator should always be there
00164   //
00165   if (first_sep == NULL) {
00166     (void) unlink(DRA_file_name);
00167     ErrMsg(EC_DRA_rii_file_format, DRA_file_name);
00168         return;
00169   }
00170   
00171   char *second_sep = strstr(first_sep+1, DRA_FILE_SEPARATOR);
00172 
00173   if (second_sep == NULL) {
00174     DRA_bytes_to_keep = DRA_file_size;
00175   }
00176   else {
00177     DRA_bytes_to_keep = second_sep - DRA_file_mmap;
00178   }
00179 
00180   DRA_keep_old_file = CXX_NEW_ARRAY(char, DRA_bytes_to_keep, Malloc_Mem_Pool);
00181   if (DRA_keep_old_file == NULL) {
00182     ErrMsg(EC_No_Mem, "DRA_Open_And_Map_File");
00183         return;
00184   }
00185   else {
00186     (void) strncpy(DRA_keep_old_file, DRA_file_mmap, DRA_bytes_to_keep);
00187   }
00188 }
00189 
00190 
00191 
00192 
00193 // =====================================================================
00194 //
00195 // Function Name: DRA_Set_Write_Location
00196 //
00197 // Description: Find the place in .rii file where information
00198 //              about common blocks should be written.
00199 //
00200 // =====================================================================
00201 
00202 void
00203 DRA_Set_Write_Location(void)
00204 {
00205   errno = 0;
00206 
00207   DRA_file_desc = open(DRA_file_name, O_WRONLY|O_TRUNC);
00208 
00209   if (DRA_file_desc < 0) {
00210         close(DRA_file_desc);
00211         ErrMsg(EC_DRA_rii_file_io, DRA_file_name, errno);
00212         return;
00213   }
00214 
00215   write(DRA_file_desc, (void*)DRA_keep_old_file, DRA_bytes_to_keep);
00216   write(DRA_file_desc, (void*)DRA_FILE_SEPARATOR, strlen(DRA_FILE_SEPARATOR));
00217   
00218   CXX_DELETE_ARRAY(DRA_keep_old_file, Malloc_Mem_Pool);
00219 }
00220 
00221 
00222 
00223 
00224 // =====================================================================
00225 //
00226 // Function Name: DRA_Mem_Unmap_File
00227 //
00228 // Description: Unmap the memory associated with the prelinker file
00229 //
00230 // =====================================================================
00231 
00232 void 
00233 DRA_Mem_Unmap_File()
00234 {
00235   // Note: Solaris CC, against Unix standard, wants first arg to be (char*)
00236   (void) munmap(DRA_file_mmap, (size_t)DRA_file_size);
00237 }
00238 
00239 
00240 
00241 
00242 // =====================================================================
00243 //
00244 // Function Name: DRA_Close_File
00245 //
00246 // Description: Close .rii file
00247 //
00248 // =====================================================================
00249 
00250 void 
00251 DRA_Close_File()
00252 {
00253   if (DRA_file_desc >= 0) {
00254     close(DRA_file_desc);
00255   }
00256 }
00257 
00258 
00259 
00260 
00261 // ======================================================================
00262 // 
00263 // The following functions were lifted from the prelinker (edg_prelink.c)
00264 //
00265 // ======================================================================
00266 
00267 // ======================================================================
00268 // 
00269 // Function Name: Make_DRA_File_Name
00270 //
00271 // Description: Build the instantiation info filename. It mimics the 
00272 //              "make_ii_file_name" routine in edg_prelink. The filename 
00273 //              we build is: dirname(obj)/rii_files/basename(obj,.o).rii
00274 //              The name is written into file static buffer rii_file_name
00275 //
00276 // ======================================================================
00277 
00278 static void 
00279 DRA_Make_File_Name()
00280 {
00281   char *obj_file_name = Obj_File_Name ? 
00282     Obj_File_Name : New_Extension (Src_File_Name, ".o");
00283   
00284   char *dir = ux_dirname(obj_file_name);
00285   strcpy (DRA_file_name, dir);
00286   strcat (DRA_file_name, DRA_DIRECTORY);
00287 
00288   char *base = ux_basename(obj_file_name);
00289   INT baselen = strlen(base);
00290   
00291   if (base[baselen-2] == '.' && base[baselen-1] == 'o')
00292     strcpy(&base[baselen-2], DRA_FILE_EXTENSION);
00293   else
00294     strcpy(&base[baselen], DRA_FILE_EXTENSION);
00295 
00296   strcat (DRA_file_name, base);
00297 } 
00298 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines