Block remapping or unmapping code mappings
[nativeclient.git] / tools / set_abi_version.py
blob17c147b02bb5cf9d7e985d46c51329f49e369386
1 #!/usr/bin/python
2 # Copyright 2008, Google Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 import getopt
33 import sys
36 """Changes the OS ABI number of Native Client executables.
38 This module contains code for updating the OS ABI version number of
39 Native Client executables. This is needed, for example, for changing
40 the bad executables checked into the service runtime test data
41 directory -- those executables were the result of hand editing of ELF
42 headers, since such bad executables cannot be created with our
43 toolchain.
45 """
47 ELF_ABIVERSION_OFFSET = 8
50 def SetOsAbiVersion(elf_file_data, version):
51 """Change the OS ABI number of a string, interpreted as an NaCl executable.
53 Args:
54 elf_file_data: the ELF NaCl executable (as a string)
55 version: the desired version number (as an integer)
57 Returns:
58 Updated ELF NaCl executable.
59 """
60 return (elf_file_data[:ELF_ABIVERSION_OFFSET]
61 + chr(version) + elf_file_data[ELF_ABIVERSION_OFFSET + 1:])
62 #enddef
65 def ModifyFileOsAbiVersion(filename, version):
66 """Changes the OS ABI number of the named file.
68 No sanity checking is done on the file's contents to verify that it
69 indeed is an ELF NaCl executable.
71 Args:
72 filename: the file to be modified.
73 version: the new OS ABI version number.
75 Returns: nothing.
77 Throws: IOError if file does not exist or cannot be written.
79 """
80 file_data = open(filename, 'rb').read()
81 open(filename, 'wb').write(SetOsAbiVersion(file_data, version))
82 # enddef
85 def ReadFileOsAbiVersion(filename):
86 """Read the named ELF NaCl executable and return its OS ABI version number.
88 Args:
89 filename: the file from which the OS ABI number is extracted.
91 Returns:
92 The OS ABI version number (as an integer).
93 """
94 return ord(open(filename, 'rb').read()[ELF_ABIVERSION_OFFSET])
95 #enddef
98 def main(argv):
99 abi_version = -1
100 try:
101 (opts, args) = getopt.getopt(argv[1:], 'v:')
102 except getopt.error, e:
103 print >>sys.stderr, str(e)
104 print >>sys.stderr, 'Usage: set_abi_version [-v version_num] filename...'
105 return 1
106 # endtry
107 for opt, val in opts:
108 if opt == 'v':
109 abi_version = int(val)
110 # endif
111 # endfor
113 for filename in args:
114 if abi_version == -1:
115 new_abi = 1 + ReadFileOsAbiVersion(filename)
116 else:
117 new_abi = abi_version
118 # endif
120 ModifyFileOsAbiVersion(filename, new_abi)
121 # endfor
123 return 0
124 # enddef
126 if __name__ == '__main__':
127 sys.exit(main(sys.argv))
128 # endif