Skip to content

Commit b4f0bb7

Browse files
committed
Clean up. Ready for some stress testing on Monday.
1 parent c189d2d commit b4f0bb7

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

extract-bc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,24 @@ def getSectionContent(size, offset, filename):
102102
# otool -X -s __LLVM __llvm_bc inputFile | xxd -r
103103
# to work this can be simplified.
104104
def extract_section_darwin(inputFile):
105+
105106
otoolCmd = ['otool', '-X', '-s', darwinSegmentName, darwinSectionName, inputFile]
106-
output = sp.check_output(otoolCmd)
107-
lines = output.splitlines()
107+
otoolProc = Popen(otoolCmd, stdout=sp.PIPE)
108+
109+
otoolOutput = otoolProc.communicate()[0]
110+
if otoolProc.returncode != 0:
111+
logging.error('otool failed on %s' % inputFile)
112+
sys.exit(-1)
113+
114+
lines = otoolOutput.splitlines()
108115
octets = []
109116
for line in lines:
110117
(_, octetline) = line.split('\t')
111118
octets.extend(octetline.split())
112119
octets = ''.join(octets)
113120
contents = octets.decode('hex').splitlines()
114121
if not contents:
115-
logging.error('{0} contained no {1} section is empty'.format(inputFile, darwinSegmentName))
122+
logging.error('{0} contained no {1} segment'.format(inputFile, darwinSegmentName))
116123
return contents
117124

118125
#analagous procedure for linux (relying on objcopy)
@@ -325,7 +332,7 @@ def main(args):
325332
process_file_darwin(inputFile, outputFile, llvmLinker, llvmArchiver)
326333

327334
else:
328-
#iam: do we work on anything else?
335+
#iam: do we work on anything else?
329336
logging.error('Unsupported or unrecognized platform: {0}'.format(sys.platform))
330337
return 1
331338

@@ -335,15 +342,15 @@ def process_file_unix(inputFile, outputFile, llvmLinker, llvmArchiver):
335342
logging.debug('Detected file type is {0}'.format(FileType.revMap[ft]))
336343

337344
extractor = extract_section_linux
345+
arCmd = ['ar','x']
346+
ofileType = FileType.ELF_OBJECT
338347

339348
if ft == FileType.ELF_EXECUTABLE or ft == FileType.ELF_SHARED:
340349
logging.info('Generating LLVM Bitcode module')
341350
return handleExecutable(inputFile, outputFile, extractor, llvmLinker)
342351
elif ft == FileType.ARCHIVE:
343-
arCmd = ['ar','x']
344-
fileType = FileType.ELF_OBJECT
345352
logging.info('Generating LLVM Bitcode archive')
346-
return handleArchive(inputFile, outputFile, arCmd, fileType, extractor, llvmArchiver)
353+
return handleArchive(inputFile, outputFile, arCmd, ofileType, extractor, llvmArchiver)
347354
else:
348355
logging.error('File "{0}" of type {1} cannot be used'.format(inputFile,FileType.revMap[ft]))
349356
return 1
@@ -355,15 +362,15 @@ def process_file_darwin(inputFile, outputFile, llvmLinker, llvmArchiver):
355362
logging.debug('Detected file type is {0}'.format(FileType.revMap[ft]))
356363

357364
extractor = extract_section_darwin
365+
arCmd = ['ar','-x']
366+
ofileType = FileType.MACH_OBJECT
358367

359368
if ft == FileType.MACH_EXECUTABLE or ft == FileType.MACH_SHARED:
360369
logging.info('Generating LLVM Bitcode module')
361370
return handleExecutable(inputFile, outputFile, extractor, llvmLinker)
362371
elif ft == FileType.ARCHIVE:
363-
arCmd = ['ar','-x']
364-
fileType = FileType.MACH_OBJECT
365372
logging.info('Generating LLVM Bitcode archive')
366-
return handleArchive(inputFile, outputFile, arCmd, fileType, extractor, llvmArchiver)
373+
return handleArchive(inputFile, outputFile, arCmd, ofileType, extractor, llvmArchiver)
367374
else:
368375
logging.error('File "{0}" of type {1} cannot be used'.format(inputFile,FileType.revMap[ft]))
369376
return 1

0 commit comments

Comments
 (0)