/[cmucl]/src/lisp/darwin-lispadjuster.c
ViewVC logotype

Diff of /src/lisp/darwin-lispadjuster.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by cshapiro, Mon Sep 5 06:09:13 2005 UTC revision 1.3 by rtoy, Thu Sep 15 18:26:51 2005 UTC
# Line 12  Line 12 
12  #ifdef DEBUG  #ifdef DEBUG
13  #define dprintf printf  #define dprintf printf
14  #else  #else
15  #define dprintf(...)  #define dprintf(...)
16  #endif  #endif
17    
18  int main(int argc, char** argv)  int
19    main(int argc, char **argv)
20  {  {
21    char* filename;      char *filename;
22    int fd;      int fd;
23    int i,result;      int i, result;
24    off_t curpos;      off_t curpos;
25    struct mach_header theheader;      struct mach_header theheader;
26    struct segment_command thesegment;      struct segment_command thesegment;
27    char segname[17];      char segname[17];
28    
29    if (argc != 2)      if (argc != 2)
30      return 1;          return 1;
31    filename = argv[1];      filename = argv[1];
32    
33    fd = open(filename, O_RDWR, 0755);      fd = open(filename, O_RDWR, 0755);
34    read(fd,&theheader,sizeof(theheader));      read(fd, &theheader, sizeof(theheader));
35    
36    if ((theheader.magic != MH_MAGIC) || (theheader.filetype != MH_EXECUTE))      if ((theheader.magic != MH_MAGIC) || (theheader.filetype != MH_EXECUTE))
37      return 2;          return 2;
38    
39    dprintf("Reading %d commands:\n",theheader.ncmds);      dprintf("Reading %d commands:\n", theheader.ncmds);
40    
41    for (i=0;i<theheader.ncmds;i++) {      for (i = 0; i < theheader.ncmds; i++) {
42      curpos = lseek(fd,0,SEEK_CUR);          curpos = lseek(fd, 0, SEEK_CUR);
43      dprintf("Reading command %d (%d bytes):\n",i,sizeof(struct load_command));          dprintf("Reading command %d (%d bytes):\n", i,
44      read(fd,&thesegment,sizeof(struct load_command));  
45      dprintf("Command is %d, length %d\n",thesegment.cmd,thesegment.cmdsize);                  sizeof(struct load_command));
46      if (thesegment.cmd != LC_SEGMENT) {          read(fd, &thesegment, sizeof(struct load_command));
47        dprintf("Skipping remainder of command...\n");  
48        lseek(fd,thesegment.cmdsize - sizeof(struct load_command),SEEK_CUR);          dprintf("Command is %d, length %d\n", thesegment.cmd,
49        continue;                  thesegment.cmdsize);
50            if (thesegment.cmd != LC_SEGMENT) {
51                dprintf("Skipping remainder of command...\n");
52                lseek(fd, thesegment.cmdsize - sizeof(struct load_command),
53    
54                      SEEK_CUR);
55                continue;
56            }
57    
58            dprintf("Reading remainder of Segment command (%d bytes)",
59                    sizeof(thesegment) - sizeof(struct load_command));
60            read(fd, &thesegment.segname,
61    
62                 sizeof(thesegment) - sizeof(struct load_command));
63            memset(segname, 0, 17);
64            memcpy(segname, thesegment.segname, 16);
65            dprintf("Segname: %s\n", segname);
66            if (0 == strncmp(thesegment.segname, "CMUCLRO", 7)) {
67                dprintf("Frobbing the segment, setting vmsize from %8X to %8X\n",
68                        thesegment.vmsize, READ_ONLY_SPACE_SIZE);
69                thesegment.vmsize = READ_ONLY_SPACE_SIZE;
70                dprintf("Skipping back to %lld.\n", curpos);
71                lseek(fd, curpos, SEEK_SET);
72                dprintf("Writing %d bytes.\n", sizeof(thesegment));
73                result = write(fd, &thesegment, sizeof(thesegment));
74                if (result == -1)
75                    perror("Error:");
76                close(fd);
77                return 0;
78            } else {
79                dprintf("Cmdsize = %d, Already read = %d, Now seeking %d\n",
80                        thesegment.cmdsize, sizeof(thesegment),
81                        thesegment.cmdsize - sizeof(thesegment));
82                lseek(fd, thesegment.cmdsize - sizeof(thesegment), SEEK_CUR);
83            }
84      }      }
85        close(fd);
86      dprintf("Reading remainder of Segment command (%d bytes)", sizeof(thesegment)-sizeof(struct load_command));      return 3;
     read(fd,&thesegment.segname,sizeof(thesegment)-sizeof(struct load_command));  
     memset(segname,0,17);  
     memcpy(segname,thesegment.segname,16);  
     dprintf("Segname: %s\n",segname);  
     if (0==strncmp(thesegment.segname,"CMUCLRO",7)) {  
       dprintf("Frobbing the segment, setting vmsize from %8X to %8X\n",thesegment.vmsize,READ_ONLY_SPACE_SIZE);  
       thesegment.vmsize = READ_ONLY_SPACE_SIZE;  
       dprintf("Skipping back to %lld.\n",curpos);  
       lseek(fd,curpos,SEEK_SET);  
       dprintf("Writing %d bytes.\n",sizeof(thesegment));  
       result=write(fd,&thesegment,sizeof(thesegment));  
       if (result==-1)  
         perror("Error:");  
       close(fd);  
       return 0;  
     } else {  
       dprintf("Cmdsize = %d, Already read = %d, Now seeking %d\n",thesegment.cmdsize, sizeof(thesegment), thesegment.cmdsize - sizeof(thesegment));  
       lseek(fd,thesegment.cmdsize - sizeof(thesegment),SEEK_CUR);  
     }  
   }  
   close(fd);  
   return 3;  
87  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.5