Beispiel #1
0
def process_file_darwin(pArgs):

    ft = FileType.getFileType(pArgs.inputFile)
    logging.debug('Detected file type is {0}'.format(FileType.revMap[ft]))

    pArgs.arCmd   =  ['ar', '-x', '-v'] if pArgs.verboseFlag else ['ar', '-x']
    pArgs.extractor = extract_section_darwin
    pArgs.fileType =  FileType.MACH_OBJECT

    if ft == FileType.MACH_EXECUTABLE or ft == FileType.MACH_SHARED or ft == FileType.MACH_OBJECT:
        logging.info('Generating LLVM Bitcode module')
        return handleExecutable(pArgs)
    elif ft == FileType.ARCHIVE:
        if pArgs.bitcodeModuleFlag:
            logging.info('Generating LLVM Bitcode module from an archive')
        else:
            logging.info('Generating LLVM Bitcode archive from an archive')
        return handleArchive(pArgs)
    else:
        logging.error('File "{0}" of type {1} cannot be used'.format(pArgs.inputFile, FileType.revMap[ft]))
        return 1
Beispiel #2
0
def handleArchive(pArgs):

    originalDir = os.getcwd() # This will be the destination

    pArgs.arCmd.append(pArgs.inputFile)
    
    # Make temporary directory to extract objects to
    tempDir = ''
    bitCodeFiles = [ ]
    retCode=0
    try:
        tempDir = tempfile.mkdtemp(suffix='wllvm')
        os.chdir(tempDir)

        # Extract objects from archive
        try:
            arP = Popen(pArgs.arCmd)
        except OSError as e:
            if e.errno == 2:
                errorMsg = 'Your ar does not seem to be easy to find.\n'
            else:
                errorMsg = 'OS error({0}): {1}'.format(e.errno, e.strerror)
            logging.error(errorMsg)
            raise Exception(errorMsg)

        arPE = arP.wait()

        if arPE != 0:
            errorMsg = 'Failed to execute archiver with command {0}'.format(pArgs.arCmd)
            logging.error(errorMsg)
            raise Exception(errorMsg)

        # Iterate over objects and examine their bitcode inserts
        for (root, dirs, files) in os.walk(tempDir):
           logging.debug('Exploring "{0}"'.format(root))
           for f in files:
               fPath = os.path.join(root, f)
               if FileType.getFileType(fPath) == pArgs.fileType:

                   # Extract bitcode locations from object
                   contents = pArgs.extractor(fPath)

                   for bcFile in contents:
                       if bcFile != '':
                           if not os.path.exists(bcFile):
                               logging.warning('{0} lists bitcode library "{1}" but it could not be found'.format(f, bcFile))
                           else:
                               bitCodeFiles.append(bcFile)
               else:
                   logging.info('Ignoring file "{0}" in archive'.format(f))

        logging.info('Found the following bitcode file names to build bitcode archive:\n{0}'.format(
            pprint.pformat(bitCodeFiles)))

    finally:
        # Delete the temporary folder
        logging.debug('Deleting temporary folder "{0}"'.format(tempDir))
        shutil.rmtree(tempDir)
        
    #write the manifest file if asked for
    if pArgs.manifestFlag:
        manifestFile = '{0}.llvm.manifest'.format(pArgs.inputFile)
        with open(manifestFile, 'w') as output:
            for f in bitCodeFiles:
                output.write('{0}\n'.format(f))

    # Build bitcode archive
    os.chdir(originalDir)

    return buildArchive(pArgs, bitCodeFiles)