|
| 1 | +import re |
| 2 | + |
| 3 | +import macholib |
| 4 | + |
| 5 | +from .errors import MachoError |
| 6 | +from .dependency import _list_dependencies_macho, _change_command_data_inplace, \ |
| 7 | + _find_lc_dylib_command |
| 8 | +from .detect import detect_macho_type |
| 9 | +from .misc import _install_name_macho, _change_id_dylib_command |
| 10 | +from .rpath import _add_rpath_to_header, _list_rpaths_macho |
| 11 | +from .utils import rstrip_null_bytes, safe_update |
| 12 | + |
| 13 | +class _MachoRewriter(object): |
| 14 | + """ |
| 15 | + Macho rewriters can be used to query and change mach-o properties relevant |
| 16 | + for relocatability. |
| 17 | +
|
| 18 | + Concretely, you can query/modify the following: |
| 19 | +
|
| 20 | + - rpaths sections |
| 21 | + - dependencies |
| 22 | +
|
| 23 | + See also |
| 24 | + -------- |
| 25 | + rewriter_factory which instanciates the right rewriter by auto-guessing the |
| 26 | + mach-o type. |
| 27 | + """ |
| 28 | + def __init__(self, filename): |
| 29 | + self.filename = filename |
| 30 | + |
| 31 | + self._m = macholib.MachO.MachO(filename) |
| 32 | + if len(self._m.headers) == 0: |
| 33 | + raise MachoError("No header found ?") |
| 34 | + elif len(self._m.headers) > 1: |
| 35 | + raise MachoError("Universal binaries not yet supported") |
| 36 | + |
| 37 | + self._rpaths = _list_rpaths_macho(self._m)[0] |
| 38 | + self._dependencies = _list_dependencies_macho(self._m)[0] |
| 39 | + |
| 40 | + def __enter__(self): |
| 41 | + return self |
| 42 | + |
| 43 | + def __exit__(self, *a, **kw): |
| 44 | + self.commit() |
| 45 | + |
| 46 | + def commit(self): |
| 47 | + def writer(f): |
| 48 | + f.seek(0) |
| 49 | + self._m.headers[0].write(f) |
| 50 | + safe_update(self.filename, writer, "wb") |
| 51 | + |
| 52 | + @property |
| 53 | + def rpaths(self): |
| 54 | + """ |
| 55 | + This is the list of defined rpaths. |
| 56 | +
|
| 57 | + Note |
| 58 | + ---- |
| 59 | + This includes the list of uncommitted changes. |
| 60 | + """ |
| 61 | + return self._rpaths |
| 62 | + |
| 63 | + #---------- |
| 64 | + # rpath API |
| 65 | + #---------- |
| 66 | + def extend_rpaths(self, new_rpaths): |
| 67 | + """ |
| 68 | + Extend the existing set of rpaths with the given list. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + new_rpaths: seq |
| 73 | + List of rpaths (i.e. list of strings). |
| 74 | +
|
| 75 | + Note |
| 76 | + ---- |
| 77 | + The binary is not actually updated intil the sync method has been |
| 78 | + called. |
| 79 | + """ |
| 80 | + header = self._m.headers[0] |
| 81 | + for rpath in new_rpaths: |
| 82 | + self._rpaths.append(rpath) |
| 83 | + _add_rpath_to_header(header, rpath) |
| 84 | + |
| 85 | + def append_rpath(self, new_rpath): |
| 86 | + """ |
| 87 | + Append the given rpath to the existing set of rpaths. |
| 88 | +
|
| 89 | + Parameters |
| 90 | + ---------- |
| 91 | + new_rpath: str |
| 92 | + The new rpath. |
| 93 | +
|
| 94 | + Note |
| 95 | + ---- |
| 96 | + The binary is not actually updated intil the sync method has been |
| 97 | + called. |
| 98 | + """ |
| 99 | + header = self._m.headers[0] |
| 100 | + self._rpaths.append(new_rpath) |
| 101 | + _add_rpath_to_header(header, new_rpath) |
| 102 | + |
| 103 | + def append_rpath_if_not_exists(self, new_rpath): |
| 104 | + """ |
| 105 | + Append the given rpath to the existing set of rpaths, but only if it |
| 106 | + does not already defined in the binary. |
| 107 | +
|
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + new_rpath: str |
| 111 | + The new rpath. |
| 112 | +
|
| 113 | + Note |
| 114 | + ---- |
| 115 | + The binary is not actually updated until the sync method has been |
| 116 | + called. |
| 117 | + """ |
| 118 | + header = self._m.headers[0] |
| 119 | + if not new_rpath in self._rpaths: |
| 120 | + _add_rpath_to_header(header, new_rpath) |
| 121 | + |
| 122 | + #----------------- |
| 123 | + # dependencies API |
| 124 | + #----------------- |
| 125 | + @property |
| 126 | + def dependencies(self): |
| 127 | + """ |
| 128 | + The list of dependencies. |
| 129 | +
|
| 130 | + Note |
| 131 | + ---- |
| 132 | + This includes the list of uncommitted changes. |
| 133 | + """ |
| 134 | + return self._dependencies |
| 135 | + |
| 136 | + def change_dependency(self, old_dependency_pattern, new_dependency, |
| 137 | + ignore_error=True): |
| 138 | + """ |
| 139 | + Change the dependency matching the given pattern to the new dependency |
| 140 | + name. |
| 141 | +
|
| 142 | + Parameters |
| 143 | + ---------- |
| 144 | + old_dependency_pattern: str |
| 145 | + Regex pattern to match against |
| 146 | + new_dependency: str |
| 147 | + New dependency name to replace with. |
| 148 | + ignore_error: bool |
| 149 | + If true, do not raise an exception of no dependency has been |
| 150 | + changed. |
| 151 | + """ |
| 152 | + r_old_dependency = re.compile(old_dependency_pattern) |
| 153 | + old_dependencies = self._dependencies[:] |
| 154 | + |
| 155 | + header = self._m.headers[0] |
| 156 | + |
| 157 | + i_dependency = 0 |
| 158 | + for command_index, (load_command, dylib_command, data) in \ |
| 159 | + _find_lc_dylib_command(header, macholib.mach_o.LC_LOAD_DYLIB): |
| 160 | + |
| 161 | + name = rstrip_null_bytes(data) |
| 162 | + m = r_old_dependency.search(name) |
| 163 | + if m: |
| 164 | + _change_command_data_inplace(header, command_index, |
| 165 | + (load_command, dylib_command, data), new_dependency) |
| 166 | + self._dependencies[i_dependency] = new_dependency |
| 167 | + |
| 168 | + i_dependency += 1 |
| 169 | + |
| 170 | + if not ignore_error and old_dependencies != self._dependencies: |
| 171 | + raise MachoError("Pattern {0} not found in the list of dependencies". |
| 172 | + format(old_dependency_pattern)) |
| 173 | + |
| 174 | +class ExecutableRewriter(_MachoRewriter): |
| 175 | + pass |
| 176 | + |
| 177 | +class BundleRewriter(_MachoRewriter): |
| 178 | + pass |
| 179 | + |
| 180 | +class DylibRewriter(_MachoRewriter): |
| 181 | + def __init__(self, filename): |
| 182 | + super(DylibRewriter, self).__init__(filename) |
| 183 | + |
| 184 | + if not self._m.headers[0].filetype == "dylib": |
| 185 | + raise MachoError("file {0} is not a dylib".format(filename)) |
| 186 | + |
| 187 | + self._install_name = _install_name_macho(self._m)[0] |
| 188 | + |
| 189 | + @property |
| 190 | + def install_name(self): |
| 191 | + return self._install_name |
| 192 | + |
| 193 | + @install_name.setter |
| 194 | + def install_name(self, new_install_name): |
| 195 | + _change_id_dylib_command(self._m.headers[0], new_install_name) |
| 196 | + |
| 197 | + self._install_name = new_install_name |
| 198 | + |
| 199 | +def rewriter_factory(filename): |
| 200 | + macho_type = detect_macho_type(filename) |
| 201 | + if macho_type == "dylib": |
| 202 | + return DylibRewriter(filename) |
| 203 | + elif macho_type == "execute": |
| 204 | + return ExecutableRewriter(filename) |
| 205 | + elif macho_type == "bundle": |
| 206 | + return BundleRewriter(filename) |
| 207 | + else: |
| 208 | + raise MachoError("file {0} is not a mach-o file !".format(filename)) |
0 commit comments