OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2008, Google Inc. | 2 # Copyright 2008, Google Inc. |
3 # All rights reserved. | 3 # All rights reserved. |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 14 matching lines...) Expand all Loading... |
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 | 31 |
32 import getopt | 32 import getopt |
33 import sys | 33 import sys |
34 | 34 |
| 35 import nacl_elf_constants |
| 36 |
35 | 37 |
36 """Changes the OS ABI number of Native Client executables. | 38 """Changes the OS ABI number of Native Client executables. |
37 | 39 |
38 This module contains code for updating the OS ABI version number of | 40 This module contains code for updating the OS ABI version number of |
39 Native Client executables. This is needed, for example, for changing | 41 Native Client executables. This is needed, for example, for changing |
40 the bad executables checked into the service runtime test data | 42 the bad executables checked into the service runtime test data |
41 directory -- those executables were the result of hand editing of ELF | 43 directory -- those executables were the result of hand editing of ELF |
42 headers, since such bad executables cannot be created with our | 44 headers, since such bad executables cannot be created with our |
43 toolchain. | 45 toolchain. |
44 | 46 |
45 """ | 47 """ |
46 | 48 |
| 49 ELF_OSABI_OFFSET = 7 |
47 ELF_ABIVERSION_OFFSET = 8 | 50 ELF_ABIVERSION_OFFSET = 8 |
48 | 51 |
| 52 OS_NUMBER_NACL = 123 |
49 | 53 |
50 def SetOsAbiVersion(elf_file_data, version): | 54 |
| 55 def SetOsAbiVersion(elf_file_data, os_number, abi_version): |
51 """Change the OS ABI number of a string, interpreted as an NaCl executable. | 56 """Change the OS ABI number of a string, interpreted as an NaCl executable. |
52 | 57 |
53 Args: | 58 Args: |
54 elf_file_data: the ELF NaCl executable (as a string) | 59 elf_file_data: the ELF NaCl executable (as a string) |
55 version: the desired version number (as an integer) | 60 version: the desired version number (as an integer) |
56 | 61 |
57 Returns: | 62 Returns: |
58 Updated ELF NaCl executable. | 63 Updated ELF NaCl executable. |
59 """ | 64 """ |
60 return (elf_file_data[:ELF_ABIVERSION_OFFSET] | 65 data = elf_file_data |
61 + chr(version) + elf_file_data[ELF_ABIVERSION_OFFSET + 1:]) | 66 |
| 67 data = (data[:ELF_OSABI_OFFSET] |
| 68 + chr(os_number) + data[ELF_OSABI_OFFSET + 1:]) |
| 69 data = (data[:ELF_ABIVERSION_OFFSET] |
| 70 + chr(abi_version) + data[ELF_ABIVERSION_OFFSET + 1:]) |
| 71 return data |
62 #enddef | 72 #enddef |
63 | 73 |
64 | 74 |
65 def ModifyFileOsAbiVersion(filename, version): | 75 def ModifyFileOsAbiVersion(filename, os_number, abi_version): |
66 """Changes the OS ABI number of the named file. | 76 """Changes the OS ABI number of the named file. |
67 | 77 |
68 No sanity checking is done on the file's contents to verify that it | 78 No sanity checking is done on the file's contents to verify that it |
69 indeed is an ELF NaCl executable. | 79 indeed is an ELF NaCl executable. |
70 | 80 |
71 Args: | 81 Args: |
72 filename: the file to be modified. | 82 filename: the file to be modified. |
73 version: the new OS ABI version number. | 83 version: the new OS ABI version number. |
74 | 84 |
75 Returns: nothing. | 85 Returns: nothing. |
76 | 86 |
77 Throws: IOError if file does not exist or cannot be written. | 87 Throws: IOError if file does not exist or cannot be written. |
78 | 88 |
79 """ | 89 """ |
80 file_data = open(filename, 'rb').read() | 90 file_data = open(filename, 'rb').read() |
81 open(filename, 'wb').write(SetOsAbiVersion(file_data, version)) | 91 open(filename, 'wb').write(SetOsAbiVersion(file_data, |
| 92 os_number, abi_version)) |
82 # enddef | 93 # enddef |
83 | 94 |
84 | 95 |
85 def ReadFileOsAbiVersion(filename): | 96 def ReadFileOsAbiVersion(filename): |
86 """Read the named ELF NaCl executable and return its OS ABI version number. | 97 """Read the named ELF NaCl executable and return its OS ABI version number. |
87 | 98 |
88 Args: | 99 Args: |
89 filename: the file from which the OS ABI number is extracted. | 100 filename: the file from which the OS ABI number is extracted. |
90 | 101 |
91 Returns: | 102 Returns: |
92 The OS ABI version number (as an integer). | 103 The OS ABI version number (as an integer). |
93 """ | 104 """ |
94 return ord(open(filename, 'rb').read()[ELF_ABIVERSION_OFFSET]) | 105 return ord(open(filename, 'rb').read()[ELF_ABIVERSION_OFFSET]) |
95 #enddef | 106 #enddef |
96 | 107 |
97 | 108 |
98 def main(argv): | 109 def main(argv): |
99 abi_version = -1 | 110 abi_version = -1 |
100 try: | 111 try: |
101 (opts, args) = getopt.getopt(argv[1:], 'v:') | 112 (opts, args) = getopt.getopt(argv[1:], 'c:v:') |
102 except getopt.error, e: | 113 except getopt.error, e: |
103 print >>sys.stderr, str(e) | 114 print >>sys.stderr, str(e) |
104 print >>sys.stderr, 'Usage: set_abi_version [-v version_num] filename...' | 115 print >>sys.stderr, 'Usage: set_abi_version [-v version_num] filename...' |
105 return 1 | 116 return 1 |
106 # endtry | 117 # endtry |
| 118 |
| 119 os_number = OS_NUMBER_NACL # default |
| 120 |
107 for opt, val in opts: | 121 for opt, val in opts: |
108 if opt == '-v': | 122 if opt == '-c': |
| 123 const_dict = nacl_elf_constants.GetNaClElfConstants(val) |
| 124 os_number = const_dict['ELFOSABI_NACL'] |
| 125 abi_version = const_dict['EF_NACL_ABIVERSION'] |
| 126 elif opt == '-v': |
109 abi_version = int(val) | 127 abi_version = int(val) |
110 # endif | 128 # endif |
111 # endfor | 129 # endfor |
112 | 130 |
113 for filename in args: | 131 for filename in args: |
114 if abi_version == -1: | 132 if abi_version == -1: |
115 new_abi = 1 + ReadFileOsAbiVersion(filename) | 133 new_abi = 1 + ReadFileOsAbiVersion(filename) |
116 else: | 134 else: |
117 new_abi = abi_version | 135 new_abi = abi_version |
118 # endif | 136 # endif |
119 | 137 |
120 ModifyFileOsAbiVersion(filename, new_abi) | 138 ModifyFileOsAbiVersion(filename, os_number, new_abi) |
121 # endfor | 139 # endfor |
122 | 140 |
123 return 0 | 141 return 0 |
124 # enddef | 142 # enddef |
125 | 143 |
126 if __name__ == '__main__': | 144 if __name__ == '__main__': |
127 sys.exit(main(sys.argv)) | 145 sys.exit(main(sys.argv)) |
128 # endif | 146 # endif |
OLD | NEW |