def SetPortableUrl(json_dict, shortname, filepath): json_dict['portable'] = {} json_dict['portable']['pnacl-translate'] = {} json_dict['portable']['pnacl-translate']['url'] = shortname fd = DriverOpen(filepath, 'r') sha = hashlib.sha256() sha.update(fd.read()) fd.close() json_dict['portable']['pnacl-translate']['sha256'] = sha.hexdigest()
def IsBitcode(filename): fp = DriverOpen(filename, 'rb') header = fp.read(4) DriverClose(fp) # Raw bitcode if header == LLVM_BITCODE_MAGIC: return True # Wrapped bitcode return IsBitcodeWrapperHeader(header)
def ReadConfig(): # Mock out ReadConfig if running unittests. Settings are applied directly # by DriverTestEnv rather than reading this configuration file. if env.has('PNACL_RUNNING_UNITTESTS'): return driver_bin = env.getone('DRIVER_BIN') driver_conf = pathtools.join(driver_bin, 'driver.conf') fp = DriverOpen(driver_conf, 'r') linecount = 0 for line in fp: linecount += 1 line = line.strip() if line == '' or line.startswith('#'): continue sep = line.find('=') if sep < 0: Log.Fatal("%s: Parse error, missing '=' on line %d", pathtools.touser(driver_conf), linecount) keyname = line[:sep].strip() value = line[sep + 1:].strip() env.setraw(keyname, value) DriverClose(fp) if env.getone('LIBMODE') not in ('newlib', 'glibc'): Log.Fatal('Invalid LIBMODE in %s', pathtools.touser(driver_conf))
def main(argv): env.update(EXTRA_ENV) driver_tools.ParseArgs(argv, DISPatterns) inputs = env.get('INPUTS') output = env.getone('OUTPUT') if len(inputs) == 0: Log.Fatal("No input files given") if len(inputs) > 1 and output != '': Log.Fatal("Cannot have -o with multiple inputs") for infile in inputs: env.push() env.set('input', infile) env.set('output', output) # When we output to stdout, set redirect_stdout and set log_stdout # to False to bypass the driver's line-by-line handling of stdout # which is extremely slow when you have a lot of output if (filetype.IsLLVMBitcode(infile) or filetype.IsPNaClBitcode(infile)): bitcodetype = 'PNaCl' if filetype.IsPNaClBitcode( infile) else 'LLVM' format = bitcodetype.lower() if env.has('FILE_TYPE'): sys.stdout.write('%s: %s bitcode\n' % (infile, bitcodetype)) continue env.append('FLAGS', '-bitcode-format=' + format) if output == '': # LLVM by default outputs to a file if -o is missing # Let's instead output to stdout env.set('output', '-') env.append('FLAGS', '-f') driver_tools.Run('${LLVM_DIS} ${FLAGS} ${input} -o ${output}') elif filetype.IsELF(infile): if env.has('FILE_TYPE'): sys.stdout.write('%s: ELF\n' % infile) continue flags = env.get('FLAGS') if len(flags) == 0: env.append('FLAGS', '-d') if output == '': # objdump to stdout driver_tools.Run('"${OBJDUMP}" ${FLAGS} ${input}') else: # objdump always outputs to stdout, and doesn't recognize -o # Let's add this feature to be consistent. fp = DriverOpen(output, 'w') driver_tools.Run('${OBJDUMP} ${FLAGS} ${input}', redirect_stdout=fp) DriverClose(fp) else: Log.Fatal('Unknown file type') env.pop() # only reached in case of no errors return 0
def ReadDriverRevision(): rev_file = env.getone('DRIVER_REV_FILE') nacl_ver = DriverOpen(rev_file, 'rb').readlines()[0] m = re.search(r'\[GIT\].*/native_client(?:\.git)?:\s*([0-9a-f]{40})', nacl_ver) if m: return m.group(1) # fail-fast: if the REV file exists but regex search failed, # we need to fix the regex to get nacl-version. if not m: Log.Fatal('Failed to parse REV file to get nacl-version.')
def DoExpandCommandFile(argv, i): arg = argv[i] fd = DriverOpen(pathtools.normalize(arg[1:]), 'r') more_args = [] # Use shlex here to process the response file contents. # This ensures that single and double quoted args are # handled correctly. Since this file is very likely # to contain paths with windows path seperators we can't # use the normal shlex.parse() since we need to disable # disable '\' (the default escape char). for line in fd: lex = shlex.shlex(line, posix=True) lex.escape = '' lex.whitespace_split = True more_args += list(lex) fd.close() argv = argv[:i] + more_args + argv[i+1:] return argv
def WrapBitcode(output): """ Hash the bitcode and insert a wrapper header with the sha value. If the bitcode is already wrapped, the old hash is overwritten. """ fd = DriverOpen(output, 'rb') maybe_header = fd.read(16) if IsBitcodeWrapperHeader(maybe_header): offset, bytes_left = GetBasicHeaderData(maybe_header) fd.seek(offset) else: offset = 0 fd.seek(0, os.SEEK_END) bytes_left = fd.tell() fd.seek(0) # get the hash sha = hashlib.sha256() while bytes_left: block = fd.read(min(bytes_left, 4096)) sha.update(block) bytes_left -= len(block) DriverClose(fd) # run bc-wrap Run(' '.join(['${LLVM_BCWRAP}', '-hash', sha.hexdigest(), '"%s"' % output]))
def ReadDriverRevision(): rev_file = env.getone('DRIVER_REV_FILE') # Might be an SVN version or a GIT hash (depending on the NaCl src client) nacl_ver = DriverOpen(rev_file, 'rb').readlines()[0] m = re.search(r'\[SVN\].*/native_client:\s*(\d+)', nacl_ver) if m: return m.group(1) m = re.search(r'\[GIT\].*/native_client.git:\s*(\w+)', nacl_ver) if m: return m.group(1) # fail-fast: if the REV file exists but regex search failed, # we need to fix the regex to get nacl-version. if not m: Log.Fatal('Failed to parse REV file to get nacl-version.')
def main(argv): env.update(EXTRA_ENV) ParseArgs(argv, NMF_PATTERNS) exe_file = env.getone('INPUTS') if not exe_file: Log.Fatal('Expecting input exe_file') DriverExit(1) baseline_file = env.getone('BASELINE_NMF') if not baseline_file: baseline_nmf = {} else: baseline_nmf = ReadBaselineNMF(baseline_file) output = env.getone('OUTPUT') if output == '': output_file = sys.stdout else: output_file = DriverOpen(output, 'w') if not FileType(exe_file) == 'pexe': EchoBaselineNMF(baseline_nmf, output_file) return 0 needed = GetBitcodeMetadata(exe_file).get('NeedsLibrary', []) if len(needed) == 0: GenerateStaticNMF(exe_file, baseline_nmf, output_file) else: # runnable_ld.so is going to ask for libgcc_s.so.1 even though it does # not show up in the pexe's NEEDED metadata, so it won't show up # in the NMF and on the App's webserver. # # For now, hack in libgcc_s.so.1. # http://code.google.com/p/nativeclient/issues/detail?id=2582 needed.append('libgcc_s.so.1') GenerateDynamicNMF(exe_file, baseline_nmf, needed, output_file) DriverClose(output_file) return 0
def ReadBaselineNMF(baseline_file): f = DriverOpen(baseline_file, 'r') json_dict = json.load(f) f.close() return json_dict
def GetELFHeader(filename): fp = DriverOpen(filename, 'rb') header = fp.read(16 + 2 + 2) DriverClose(fp) return DecodeELFHeader(header, filename)
def IsWrappedBitcode(filename): fp = DriverOpen(filename, 'rb') header = fp.read(4) iswbc = IsBitcodeWrapperHeader(header) DriverClose(fp) return iswbc
def GetELFHeader(filename): fp = DriverOpen(filename, 'rb') # Read max(sizeof(Elf64_Ehdr), sizeof(Elf32_Ehdr)), which is 64 bytes. header = fp.read(64) DriverClose(fp) return DecodeELFHeader(header, filename)
def GetBitcodeMagic(filename): fp = DriverOpen(filename, 'rb') header = fp.read(4) DriverClose(fp) return header