OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright 2010 The Native Client Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can |
| 5 # be found in the LICENSE file. |
| 6 |
| 7 import getopt |
| 8 import struct |
| 9 import sys |
| 10 |
| 11 import elf |
| 12 import nacl_elf_constants |
| 13 |
| 14 def StringSubst(in_str, pos, replacement): |
| 15 return in_str[:pos] + replacement + in_str[pos+1:] |
| 16 |
| 17 def UpdateFile(fname, align, constants): |
| 18 data = open(fname).read() |
| 19 |
| 20 elf_obj = elf.Elf(data) |
| 21 |
| 22 format_map = elf.ehdr_format_map[elf_obj.wordsize] |
| 23 |
| 24 ehdr_data = list(struct.unpack_from(format_map, data)) |
| 25 |
| 26 ehdr_data[0] = StringSubst(ehdr_data[0], |
| 27 elf.ehdr_ident['osabi'], |
| 28 chr(constants['ELFOSABI_NACL'])) |
| 29 ehdr_data[0] = StringSubst(ehdr_data[0], |
| 30 elf.ehdr_ident['osabi'], |
| 31 chr(constants['ELFOSABI_NACL'])) |
| 32 ehdr_data[0] = StringSubst(ehdr_data[0], |
| 33 elf.ehdr_ident['abiversion'], |
| 34 chr(constants['EF_NACL_ABIVERSION'])) |
| 35 flag_pos = elf.MemberPositionMap(elf.ehdr_member_and_type)['flags'] |
| 36 ehdr_data[flag_pos] = ehdr_data[flag_pos] & ~constants['EF_NACL_ALIGN_MASK'] |
| 37 ehdr_data[flag_pos] = ehdr_data[flag_pos] | constants[ |
| 38 'EF_NACL_ALIGN_' + align] |
| 39 new_hdr = struct.pack(format_map, *ehdr_data) |
| 40 open(fname, 'w').write( |
| 41 new_hdr + data[len(new_hdr):]) |
| 42 |
| 43 def main(argv): |
| 44 try: |
| 45 (opts, args) = getopt.getopt(argv[1:], 'a:h:') |
| 46 except getopt.error, e: |
| 47 print >>sys.stderr, str(e) |
| 48 print >>sys.stderr, ('Usage: elf_patcher [-h /path/to/nacl_elf.h]' + |
| 49 ' filename...') |
| 50 return 1 |
| 51 # endtry |
| 52 |
| 53 constants = None |
| 54 valid_aligns = ['16', '32', 'LIB'] |
| 55 |
| 56 align = '32' |
| 57 |
| 58 for opt, val in opts: |
| 59 if opt == '-a': |
| 60 align = val |
| 61 elif opt == '-h': |
| 62 constants = nacl_elf_constants.GetNaClElfConstants(val) |
| 63 # endif |
| 64 # endfor |
| 65 |
| 66 if constants is None: |
| 67 print >>sys.stderr, 'Current NaCl OS, ABI, align mask values unknown' |
| 68 return 1 |
| 69 |
| 70 if align not in valid_aligns: |
| 71 print >>sys.stderr, ('bundle size / alignment must be one of %s' % |
| 72 ', '.join(valid_aligns)) |
| 73 return 2 |
| 74 |
| 75 for filename in args: |
| 76 UpdateFile(filename, align, constants) |
| 77 # endfor |
| 78 |
| 79 return 0 |
| 80 # enddef |
| 81 |
| 82 if __name__ == '__main__': |
| 83 sys.exit(main(sys.argv)) |
| 84 # endif |
OLD | NEW |