Cleanup the "find gcc" code (#3169)

* use -m64 compilation on 64-bit targets

* use the pwntools which() function

* make default in y/n obvious
pull/3177/head
k4lizen 5 months ago committed by GitHub
parent 0e39240186
commit 500bb9edbc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -102,7 +102,12 @@ def generate_debug_symbols(
]
# TODO: implement remote debugging support.
gcc_flags = pwndbg.lib.gcc.which(pwndbg.aglib.arch)
try:
gcc_flags = pwndbg.lib.gcc.which(pwndbg.aglib.arch)
except ValueError as _:
# The error message is already printed by pwntools.
return None
if gcc_compiler_path != "":
gcc_flags[0] = gcc_compiler_path # type: ignore[call-overload]
@ -132,7 +137,7 @@ def add_custom_structure(custom_structure_name: str, force=False):
if os.path.exists(pwndbg_custom_structure_path) and not force:
option = input(
message.notice(
"A custom structure was found with the given name, would you like to overwrite it? [y/n] "
"A custom structure was found with the given name, would you like to overwrite it? [y/N] "
)
)
if option != "y":
@ -173,7 +178,7 @@ def add_structure_from_header(
if not force:
option = input(
message.notice(
f"Structure '{custom_structure_name}' already exists. Overwrite? [y/n] "
f"Structure '{custom_structure_name}' already exists. Overwrite? [y/N] "
)
)
if option.lower() != "y":

@ -11,74 +11,22 @@ import platform
from typing import Any
from typing import List
import pwnlib
from pwndbg.lib.arch import ArchDefinition
printed_message = False
def which(arch: ArchDefinition) -> List[str]:
gcc = _which_binutils("g++", arch)
if gcc is None:
global printed_message
if not printed_message:
printed_message = True
print("Can't find appropriate GCC, using default version")
if arch.ptrsize == 32:
return ["g++", "-m32"]
elif arch.ptrsize == 64:
return ["g++", "-m32"]
else:
raise ValueError(f"Unknown pointer size: {arch.ptrsize}")
try:
gcc = pwnlib.asm.which_binutils("g++")
except pwnlib.exception.PwnlibException as _:
raise ValueError("Couldn't find g++ for the current architecture.")
return [gcc] + _flags(arch.name)
def _which_binutils(util: str, arch: ArchDefinition, **kwargs: Any) -> str | None:
###############################
# Borrowed from pwntools' code
###############################
arch_name = arch.name
# Fix up binjitsu vs Debian triplet naming, and account
# for 'thumb' being its own binjitsu architecture.
arches: List[str | None] = [arch_name] + {
"thumb": ["arm", "armcm", "aarch64"],
"i386": ["x86_64", "amd64"],
"i686": ["x86_64", "amd64"],
"i386:x86-64": ["x86_64", "amd64"],
"amd64": ["x86_64", "i386"],
}.get(arch_name, [])
# If one of the candidate architectures matches the native
# architecture, use that as a last resort.
machine = platform.machine()
machine = "i386" if machine == "i686" else machine
if arch_name in arches:
arches.append(None)
for arch_name in arches:
# hack for homebrew-installed binutils on mac
for gutil in ["g" + util, util]:
# e.g. objdump
if arch_name is None:
pattern = gutil
# e.g. aarch64-linux-gnu-objdump
else:
pattern = f"{arch_name}*linux*-{gutil}"
for dir in os.environ["PATH"].split(":"):
res = sorted(glob.glob(os.path.join(dir, pattern)))
if res:
return res[0]
return None
def _flags(arch_name: str) -> List[str]:
if arch_name == "i386":
return ["-m32"]

Loading…
Cancel
Save