URI:
       tcrack.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tcrack.c (2355B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 #include <mach.h>
            5 #include "elf.h"
            6 
            7 
            8 Mach *mach;
            9 
           10 extern Mach mach386;
           11 extern Mach machpower;
           12 
           13 static Mach *machs[] =
           14 {
           15         &mach386,
           16         &machpower,
           17 };
           18 
           19 Mach*
           20 machbyname(char *name)
           21 {
           22         int i;
           23 
           24         for(i=0; i<nelem(machs); i++)
           25                 if(strcmp(machs[i]->name, name) == 0){
           26                         mach = machs[i];
           27                         return machs[i];
           28                 }
           29         werrstr("machine '%s' not found", name);
           30         return nil;
           31 }
           32 
           33 static struct
           34 {
           35         ulong magic;
           36         int (*fn)(int, Fhdr*);
           37 } cracktab[] = {
           38         0x7F454C46,        crackelf,
           39         0xFEEDFACE,        crackmacho,
           40 };
           41 
           42 Fhdr*
           43 crackhdr(char *name, int mode)
           44 {
           45         uchar buf[4];
           46         ulong magic;
           47         int i, fd;
           48         Fhdr *hdr;
           49 
           50         if((fd = open(name, mode)) < 0)
           51                 return nil;
           52 
           53         if(seek(fd, 0, 0) < 0 || readn(fd, buf, 4) != 4){
           54                 close(fd);
           55                 return nil;
           56         }
           57 
           58         hdr = mallocz(sizeof(Fhdr), 1);
           59         if(hdr == nil){
           60                 close(fd);
           61                 return nil;
           62         }
           63         hdr->filename = strdup(name);
           64         magic = beload4(buf);
           65         werrstr("magic doesn't match");
           66         for(i=0; i<nelem(cracktab); i++)
           67                 if(cracktab[i].magic == magic && seek(fd, 0, 0) == 0 && cracktab[i].fn(fd, hdr) >= 0){
           68                         _addhdr(hdr);
           69                         return hdr;
           70                 }
           71         werrstr("unknown file type: %r");
           72         free(hdr->filename);
           73         free(hdr);
           74         close(fd);
           75         return nil;
           76 }
           77 
           78 void
           79 uncrackhdr(Fhdr *hdr)
           80 {
           81         int i;
           82 
           83         symclose(hdr);
           84         if(hdr->elf)
           85                 elfclose(hdr->elf);
           86         if(hdr->fd >= 0)
           87                 close(hdr->fd);
           88         free(hdr->cmdline);
           89         free(hdr->prog);
           90         for(i=0; i<hdr->nthread; i++)
           91                 free(hdr->thread[i].ureg);
           92         free(hdr->thread);
           93         free(hdr->filename);
           94         free(hdr);
           95 }
           96 
           97 int
           98 mapfile(Fhdr *fp, u64int base, Map *map, Regs **regs)
           99 {
          100         if(fp == nil){
          101                 werrstr("no file");
          102                 return -1;
          103         }
          104         if(map == nil){
          105                 werrstr("no map");
          106                 return -1;
          107         }
          108         if(fp->map == 0){
          109                 werrstr("cannot load map for this file type");
          110                 return -1;
          111         }
          112         if(regs)
          113                 *regs = nil;
          114         return fp->map(fp, base, map, regs);
          115 }
          116 
          117 void
          118 unmapfile(Fhdr *fp, Map *map)
          119 {
          120         int i;
          121 
          122         if(map == nil || fp == nil)
          123                 return;
          124 
          125         for(i=0; i<map->nseg; i++){
          126                 while(i<map->nseg && map->seg[i].fd == fp->fd){
          127                         map->nseg--;
          128                         memmove(&map->seg[i], &map->seg[i+1],
          129                                 (map->nseg-i)*sizeof(map->seg[0]));
          130                 }
          131         }
          132 }
          133 
          134 Regs*
          135 coreregs(Fhdr *fp, uint id)
          136 {
          137         UregRegs *r;
          138         int i;
          139 
          140         for(i=0; i<fp->nthread; i++){
          141                 if(fp->thread[i].id == id){
          142                         if((r = mallocz(sizeof *r, 1)) == nil)
          143                                 return nil;
          144                         r->r.rw = _uregrw;
          145                         r->ureg = fp->thread[i].ureg;
          146                         return &r->r;
          147                 }
          148         }
          149         werrstr("thread not found");
          150         return nil;
          151 }