Finds a binutils in the PATH somewhere. Expects that the utility is prefixed with the architecture name. Examples: >>> import platform >>> which_binutils = pwnlib.asm.which_binutils >>> which_binutils('as', arch=platform.machine()) '.../bin/...as'
(util, check_version=False)
| 156 | |
| 157 | @LocalContext |
| 158 | def which_binutils(util, check_version=False): |
| 159 | """ |
| 160 | Finds a binutils in the PATH somewhere. |
| 161 | Expects that the utility is prefixed with the architecture name. |
| 162 | |
| 163 | Examples: |
| 164 | |
| 165 | >>> import platform |
| 166 | >>> which_binutils = pwnlib.asm.which_binutils |
| 167 | >>> which_binutils('as', arch=platform.machine()) |
| 168 | '.../bin/...as' |
| 169 | >>> which_binutils('as', arch='arm') #doctest: +ELLIPSIS |
| 170 | '.../bin/arm-...-as' |
| 171 | >>> which_binutils('as', arch='powerpc') #doctest: +ELLIPSIS |
| 172 | '.../bin/powerpc...-as' |
| 173 | >>> which_binutils('as', arch='mips', endianness='little') #doctest: +ELLIPSIS |
| 174 | '.../bin/mipsel...-as' |
| 175 | >>> which_binutils('as', arch='msp430') #doctest: +SKIP |
| 176 | ... |
| 177 | Traceback (most recent call last): |
| 178 | ... |
| 179 | Exception: Could not find 'as' installed for ContextType(arch = 'msp430') |
| 180 | """ |
| 181 | arch = context.arch |
| 182 | |
| 183 | # Handle endianness-specific naming for mips/mips64 |
| 184 | arch = { |
| 185 | ('mips', 'little'): 'mipsel', |
| 186 | ('mips64', 'little'): 'mips64el', |
| 187 | }.get((arch, context.endianness), arch) |
| 188 | |
| 189 | # Fix up pwntools vs Debian triplet naming, and account |
| 190 | # for 'thumb' being its own pwntools architecture. |
| 191 | arches = [arch] + { |
| 192 | 'thumb': ['arm', 'aarch64'], |
| 193 | 'i386': ['x86_64', 'amd64'], |
| 194 | 'i686': ['x86_64', 'amd64'], |
| 195 | 'amd64': ['x86_64', 'i386'], |
| 196 | 'mips': ['mipsel'], |
| 197 | 'mipsel': ['mips'], |
| 198 | 'mips64': ['mips', 'mipsel'], |
| 199 | 'mips64el': ['mipsel', 'mips'], |
| 200 | 'powerpc64': ['powerpc'], |
| 201 | 'sparc64': ['sparc'], |
| 202 | 'riscv32': ['riscv64', 'riscv'], |
| 203 | 'riscv64': ['riscv32', 'riscv'], |
| 204 | }.get(arch, []) |
| 205 | |
| 206 | # If one of the candidate architectures matches the native |
| 207 | # architecture, use that as a last resort. |
| 208 | machine = platform.machine() |
| 209 | machine = 'i386' if machine == 'i686' else machine |
| 210 | try: |
| 211 | with context.local(arch = machine): |
| 212 | if context.arch in arches: |
| 213 | arches.append(None) |
| 214 | except AttributeError: |
| 215 | log.warn_once("Your local binutils won't be used because architecture %r is not supported." % machine) |
no test coverage detected