New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 80963


Ignore:
Timestamp:
07/21/11 22:10:06 (4 years ago)
Author:
fotanus@…
Message:

Now macho.c functions responds to non-binary files

this is needed in order to skip non binary files.
also, did some code cleanup

Location:
branches/gsoc11-post-destroot/base/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-post-destroot/base/src/pextlib1.0/macho.c

    r80883 r80963  
    2020 
    2121#include "macho.h" 
    22 Tcl_Interp *interp2; 
    2322typedef struct macho_input { 
    2423        const void *data; 
     
    5857 
    5958/* Parse a Mach-O header */ 
    60 Tcl_Obj * list_macho_dlibs_l (macho_input_t *input, Tcl_Obj * dlibs) { 
     59Tcl_Obj * list_macho_dlibs_l(macho_input_t *input, Tcl_Interp * interp, Tcl_Obj * dlibs) { 
    6160        /* Read the file type. */ 
    6261        const uint32_t *magic = macho_read(input, input->data, sizeof(uint32_t)); 
     
    7978 
    8079        if (magic == NULL) 
    81                 return TCL_ERROR; 
     80                return (Tcl_Obj *)TCL_ERROR; 
    8281 
    8382        switch (*magic) { 
     
    118117 
    119118                default: 
    120                         return TCL_ERROR; 
     119                        return (Tcl_Obj *)TCL_ERROR; 
    121120        } 
    122121 
     
    142141 
    143142                        /* Parse the architecture's Mach-O header */ 
    144                         if (!list_macho_dlibs_l(&arch_input,dlibs)) 
     143                        if (!list_macho_dlibs_l(&arch_input, interp, dlibs)) 
    145144                                return false; 
    146145                } 
     
    155154        cmd = macho_offset(input, header, header_size, sizeof(struct load_command)); 
    156155        if (cmd == NULL) 
    157                 return TCL_ERROR; 
     156                return (Tcl_Obj *)TCL_ERROR; 
    158157        ncmds = swap32(header->ncmds); 
    159158 
     
    162161                /* Load the full command */ 
    163162                uint32_t cmdsize = swap32(cmd->cmdsize); 
     163                uint32_t cmd_type = swap32(cmd->cmd); 
     164                size_t pathlen; 
     165                const void * pathptr; 
     166                char * path; 
     167                size_t namelen; 
     168                const void *nameptr; 
     169                char *name; 
     170 
    164171                cmd = macho_read(input, cmd, cmdsize); 
    165172                if (cmd == NULL) 
    166                         return TCL_ERROR; 
     173                        return (Tcl_Obj *)TCL_ERROR; 
    167174 
    168175                /* Handle known types */ 
    169                 uint32_t cmd_type = swap32(cmd->cmd); 
    170176                switch (cmd_type) { 
    171177                        case LC_RPATH: { 
    172178                                /* Fetch the path */ 
    173179                                if (cmdsize < sizeof(struct rpath_command)) { 
    174                                         return TCL_ERROR; 
     180                                        return (Tcl_Obj *)TCL_ERROR; 
    175181                                } 
    176182 
    177                                 size_t pathlen = cmdsize - sizeof(struct rpath_command); 
    178                                 const void *pathptr = macho_offset(input, cmd, sizeof(struct rpath_command), pathlen); 
     183                                pathlen = cmdsize - sizeof(struct rpath_command); 
     184                                pathptr = macho_offset(input, cmd, sizeof(struct rpath_command), pathlen); 
    179185                                if (pathptr == NULL) 
    180                                         return TCL_ERROR; 
    181  
    182                                 char *path = malloc(pathlen); 
     186                                        return (Tcl_Obj *)TCL_ERROR; 
     187 
     188                                path = malloc(pathlen); 
    183189                                strlcpy(path, pathptr, pathlen); 
    184190                                free(path); 
     
    190196                        case LC_REEXPORT_DYLIB: 
    191197                        case LC_LOAD_DYLIB: { 
    192                                 const struct dylib_command *dylib_cmd = (const struct dylib_command *) cmd; 
    193  
    194198                                /* Extract the install name */ 
    195199                                if (cmdsize < sizeof(struct dylib_command)) { 
    196                                         return TCL_ERROR; 
     200                                        return (Tcl_Obj *)TCL_ERROR; 
    197201                                } 
    198202 
    199                                 size_t namelen = cmdsize - sizeof(struct dylib_command); 
    200                                 const void *nameptr = macho_offset(input, cmd, sizeof(struct dylib_command), namelen); 
     203                                namelen = cmdsize - sizeof(struct dylib_command); 
     204                                nameptr = macho_offset(input, cmd, sizeof(struct dylib_command), namelen); 
    201205                                if (nameptr == NULL) 
    202                                         return TCL_ERROR; 
    203  
    204                                 char *name = malloc(namelen); 
     206                                        return (Tcl_Obj *)TCL_ERROR; 
     207 
     208                                name = malloc(namelen); 
    205209                                strlcpy(name, nameptr, namelen); 
    206210 
    207211                                /* This is a dyld library identifier */ 
    208                                 Tcl_ListObjAppendElement(interp2, dlibs, Tcl_NewStringObj(name, -1)); 
     212                                Tcl_ListObjAppendElement(interp, dlibs, Tcl_NewStringObj(name, -1)); 
    209213 
    210214                                free(name); 
     
    219223                cmd = macho_offset(input, cmd, cmdsize, sizeof(struct load_command)); 
    220224                if (cmd == NULL) 
    221                         return TCL_ERROR; 
     225                        return (Tcl_Obj *)TCL_ERROR; 
    222226        } 
    223227 
     
    225229} 
    226230 
    227 Tcl_Obj * list_macho_dlibs(macho_input_t *input) { 
    228         return list_macho_dlibs_l(input, Tcl_NewListObj(0,NULL)); 
     231Tcl_Obj * list_macho_dlibs(macho_input_t *input, Tcl_Interp *interp) { 
     232        return list_macho_dlibs_l(input, interp, Tcl_NewListObj(0,NULL)); 
    229233} 
    230234 
    231235/* List Mach-O archs */ 
    232 Tcl_Obj * list_macho_archs_l(macho_input_t *input, Tcl_Obj * archs_list) { 
     236Tcl_Obj * list_macho_archs_l(macho_input_t *input, Tcl_Interp *interp, Tcl_Obj * archs_list) { 
     237        const struct mach_header *header; 
     238        const struct mach_header_64 *header64; 
     239        size_t header_size; 
     240        const NXArchInfo *archInfo; 
     241        const struct fat_header *fat_header; 
     242 
     243        /* Parse the Mach-O header */ 
     244        bool universal = false; 
     245        uint32_t (*swap32)(uint32_t) = macho_nswap32; 
     246 
    233247        /* Read the file type. */ 
    234248        const uint32_t *magic = macho_read(input, input->data, sizeof(uint32_t)); 
     
    236250                return false; 
    237251 
    238         /* Parse the Mach-O header */ 
    239         bool universal = false; 
    240         uint32_t (*swap32)(uint32_t) = macho_nswap32; 
    241  
    242         const struct mach_header *header; 
    243         const struct mach_header_64 *header64; 
    244         size_t header_size; 
    245         const struct fat_header *fat_header; 
     252 
    246253 
    247254        switch (*magic) { 
     
    254261                        header = macho_read(input, input->data, header_size); 
    255262                        if (header == NULL) { 
    256                                 return TCL_ERROR; 
     263                                return (Tcl_Obj *)TCL_ERROR; 
    257264                        } 
    258265                        break; 
     
    267274                        header64 = macho_read(input, input->data, sizeof(*header64)); 
    268275                        if (header64 == NULL) 
    269                                 return TCL_ERROR; 
     276                                return (Tcl_Obj *)TCL_ERROR; 
    270277 
    271278                        /* The 64-bit header is a direct superset of the 32-bit header */ 
     
    281288 
    282289                default: 
    283                         return TCL_ERROR; 
     290                        return (Tcl_Obj *)TCL_ERROR; 
    284291        } 
    285292 
     
    288295                uint32_t nfat = OSSwapBigToHostInt32(fat_header->nfat_arch); 
    289296                const struct fat_arch *archs = macho_offset(input, fat_header, sizeof(struct fat_header), sizeof(struct fat_arch)); 
     297                uint32_t i; 
     298                const struct fat_arch *arch; 
     299                macho_input_t arch_input; 
     300 
    290301                if (archs == NULL) 
    291                         return TCL_ERROR; 
    292  
    293                 uint32_t i; 
     302                        return (Tcl_Obj *)TCL_ERROR; 
     303 
    294304                for (i = 0; i < nfat; i++) { 
    295                         const struct fat_arch *arch = macho_read(input, archs + i, sizeof(struct fat_arch)); 
     305                        arch = macho_read(input, archs + i, sizeof(struct fat_arch)); 
    296306                        if (arch == NULL) 
    297                                 return TCL_ERROR; 
     307                                return (Tcl_Obj *)TCL_ERROR; 
    298308 
    299309                        /* Fetch a pointer to the architecture's Mach-O header. */ 
    300                         macho_input_t arch_input; 
    301310                        arch_input.length = OSSwapBigToHostInt32(arch->size); 
    302311                        arch_input.data = macho_offset(input, input->data, OSSwapBigToHostInt32(arch->offset), arch_input.length); 
    303312                        if (arch_input.data == NULL) 
    304                                 return TCL_ERROR; 
     313                                return (Tcl_Obj *)TCL_ERROR; 
    305314 
    306315                        /* Parse the architecture's Mach-O header */ 
    307                         if (!list_macho_archs_l(&arch_input, archs_list)) 
    308                                 return TCL_ERROR; 
     316                        if (!list_macho_archs_l(&arch_input, interp, archs_list)) 
     317                                return (Tcl_Obj *)TCL_ERROR; 
    309318                } 
    310319 
     
    313322 
    314323        /* Fetch the arch name */ 
    315         const NXArchInfo *archInfo = NXGetArchInfoFromCpuType(swap32(header->cputype), swap32(header->cpusubtype)); 
     324        archInfo = NXGetArchInfoFromCpuType(swap32(header->cputype), swap32(header->cpusubtype)); 
    316325        if (archInfo != NULL) { 
    317                 Tcl_ListObjAppendElement(interp2, archs_list, Tcl_NewStringObj(archInfo->name,-1)); 
     326                Tcl_ListObjAppendElement(interp, archs_list, Tcl_NewStringObj(archInfo->name,-1)); 
    318327        } 
    319328        return archs_list; 
    320329} 
    321330 
    322 Tcl_Obj * list_macho_archs(macho_input_t *input) { 
    323         return list_macho_archs_l(input, Tcl_NewListObj(0,NULL)); 
    324 } 
    325  
    326 int list_dlibs(ClientData clientData , Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ 
     331Tcl_Obj * list_macho_archs(macho_input_t *input, Tcl_Interp *interp) { 
     332        return list_macho_archs_l(input, interp, Tcl_NewListObj(0,NULL)); 
     333} 
     334 
     335int list_dlibs(ClientData clientData __attribute__((unused)) , Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ 
    327336        const char *path; 
    328         interp2 = interp; 
     337        int fd; 
     338        struct stat stbuf; 
     339        void * data; 
     340        Tcl_Obj * libs; 
     341        macho_input_t input_file; 
     342 
     343 
    329344 
    330345        if (objc != 2) { 
     
    335350        path = Tcl_GetString(objv[1]); 
    336351 
    337         int fd = open(path, O_RDONLY); 
     352        fd = open(path, O_RDONLY); 
    338353        if (fd < 0) { 
    339354                return TCL_ERROR; 
    340355        } 
    341356 
     357        if (fstat(fd, &stbuf) != 0) { 
     358                return TCL_ERROR; 
     359        } 
     360 
     361        /* mmap */ 
     362        data = mmap(NULL, stbuf.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); 
     363        if (data != MAP_FAILED){ 
     364                /* Parse */ 
     365                input_file.data = data; 
     366                input_file.length = stbuf.st_size; 
     367 
     368                libs = list_macho_dlibs(&input_file, interp); 
     369 
     370                munmap(data, stbuf.st_size); 
     371        } 
     372        else{ 
     373                libs = (Tcl_Obj *)TCL_ERROR; 
     374        } 
     375        close(fd); 
     376 
     377 
     378        if(libs == (Tcl_Obj *)TCL_ERROR){ 
     379                Tcl_SetObjResult(interp, Tcl_NewListObj(0,NULL)); 
     380        } 
     381        else{ 
     382                Tcl_SetObjResult(interp, libs); 
     383        } 
     384        return TCL_OK; 
     385} 
     386 
     387 
     388int list_archs(ClientData clientData  __attribute__((unused)), Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ 
     389        const char *path; 
     390        int fd; 
    342391        struct stat stbuf; 
    343         if (fstat(fd, &stbuf) != 0) { 
    344                 return TCL_ERROR; 
    345         } 
    346  
    347         /* mmap */ 
    348         void *data = mmap(NULL, stbuf.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); 
    349         if (data == MAP_FAILED) 
    350                 return TCL_ERROR; 
    351  
    352         /* Parse */ 
     392        void * data; 
     393        Tcl_Obj * archs; 
    353394        macho_input_t input_file; 
    354         input_file.data = data; 
    355         input_file.length = stbuf.st_size; 
    356  
    357         Tcl_Obj * libs = list_macho_dlibs(&input_file); 
    358  
    359         munmap(data, stbuf.st_size); 
    360         close(fd); 
    361         Tcl_SetObjResult(interp, libs); 
    362         return TCL_OK; 
    363 } 
    364  
    365  
    366 int list_archs(ClientData clientData , Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ 
    367         interp2 = interp; 
    368         const char *path; 
    369395 
    370396        if (objc != 2) { 
     
    375401        path = Tcl_GetString(objv[1]); 
    376402 
    377         int fd = open(path, O_RDONLY); 
     403        fd = open(path, O_RDONLY); 
    378404        if (fd < 0) { 
    379405                return TCL_ERROR; 
    380406        } 
    381407 
    382         struct stat stbuf; 
    383408        if (fstat(fd, &stbuf) != 0) { 
    384409                return TCL_ERROR; 
     
    386411 
    387412        /* mmap */ 
    388         void *data = mmap(NULL, stbuf.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); 
    389         if (data == MAP_FAILED) 
    390                 return TCL_ERROR; 
    391  
    392         /* Parse */ 
    393         macho_input_t input_file; 
    394         input_file.data = data; 
    395         input_file.length = stbuf.st_size; 
    396  
    397         Tcl_Obj * archs = list_macho_archs(&input_file); 
    398  
    399         munmap(data, stbuf.st_size); 
     413        data = mmap(NULL, stbuf.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); 
     414        if (data != MAP_FAILED){ 
     415                /* Parse */ 
     416                input_file.data = data; 
     417                input_file.length = stbuf.st_size; 
     418 
     419                archs = list_macho_archs(&input_file, interp); 
     420 
     421                munmap(data, stbuf.st_size); 
     422        } 
     423        else{ 
     424                archs = (Tcl_Obj *)TCL_ERROR; 
     425        } 
    400426        close(fd); 
    401         Tcl_SetObjResult(interp, archs); 
     427 
     428        if(archs == (Tcl_Obj *)TCL_ERROR) 
     429                Tcl_SetObjResult(interp, Tcl_NewListObj(0,NULL)); 
     430        else 
     431                Tcl_SetObjResult(interp, archs); 
    402432        return TCL_OK; 
    403433} 
  • branches/gsoc11-post-destroot/base/src/port1.0/portcheckdestroot.tcl

    r80618 r80963  
    3939proc portcheckdestroot::get_port_files {portname} { 
    4040} 
    41  
    42 # Check if a file is binary file 
    43 # TODO: Somewhat probabilistic. Must be a better way. 
    44 proc portcheckdestroot::binary? filename { 
    45     set f [open $filename] 
    46     set data [read $f 1024] 
    47     close $f 
    48     expr {[string first \x00 $data]>=0} 
    49 } 
    50  
    5141 
    5242# escape chars in order to be usable as regexp. This function is for internal use. 
     
    249239        #Get package files 
    250240        foreach file [files_list $destroot] { 
    251             if { [binary? "$file"] } { 
    252                 foreach file_lib [list_dlibs $file] { 
     241            foreach file_lib [list_dlibs $file] { 
     242            if { ! [regexp $file_lib $file] } { 
    253243                if { [lsearch $dep_files $file_lib] != -1 } { 
    254244                    ui_debug "$file_lib binary dependency is met" 
     
    277267    global destroot 
    278268    foreach file [files_list $destroot] { 
    279         if { [binary? "$file"] } { 
    280             set file_archs [list_archs $file] 
    281             foreach arch $archs { 
    282                 if { [lsearch $file_archs $arch] == -1 } { 
    283                     return -code error "$file supports the arch $arch, and should not" 
    284                 } 
     269        set file_archs [list_archs $file] 
     270        foreach arch $file_archs { 
     271            if { [lsearch $arch $archs] == -1 } { 
     272                return -code error "$file supports the arch $arch, and should not" 
    285273            } 
    286274        } 
Note: See TracChangeset for help on using the changeset viewer.