Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ nanobind_add_stub(
libdebug_ptrace_binding_stub
MODULE libdebug_ptrace_binding
DEPENDS libdebug_ptrace_binding
OUTPUT libdebug_ptrace_binding.pyi
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/libdebug/ptrace/native/libdebug_ptrace_binding.pyi"
PYTHON_PATH "."
)

Expand All @@ -197,7 +197,7 @@ nanobind_add_stub(
libdebug_linux_binding_stub
MODULE libdebug_linux_binding
DEPENDS libdebug_linux_binding
OUTPUT libdebug_linux_binding.pyi
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/libdebug/native/libdebug_linux_binding.pyi"
PYTHON_PATH "."
)

Expand All @@ -215,7 +215,7 @@ nanobind_add_stub(
libdebug_elf_api_stub
MODULE libdebug_elf_api
DEPENDS libdebug_elf_api
OUTPUT libdebug_elf_api.pyi
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/libdebug/native/libdebug_elf_api.pyi"
PYTHON_PATH "."
)

Expand All @@ -226,7 +226,7 @@ if(BUILD_SYMBOLS)

add_library(libdebug_sym_parser_lib STATIC
libdebug/native/symbols/debug_sym_parser.cpp
libdebug/native/symbols/debug_sym_parser_new.cpp
libdebug/native/symbols/debug_sym_parser_dwarf.cpp
)
else()
message(STATUS "Using legacy libdwarf API")
Expand All @@ -250,7 +250,8 @@ if(BUILD_SYMBOLS)
libdebug_debug_sym_parser
LTO
STABLE_ABI
libdebug/native/symbols/debug_sym_parser_shared.cpp
libdebug/native/symbols/debug_sym_parser_shared.cpp
libdebug/native/symbols/debug_sym_structs.cpp
)
else()
# We build a dummy module that does not depend on libdwarf or libelf
Expand All @@ -260,6 +261,7 @@ else()
STABLE_ABI
libdebug/native/symbols/debug_sym_parser_dummy.cpp
libdebug/native/symbols/debug_sym_parser_shared.cpp
libdebug/native/symbols/debug_sym_structs.cpp
)
endif()

Expand All @@ -268,7 +270,7 @@ nanobind_add_stub(
libdebug_debug_sym_parser_stub
MODULE libdebug_debug_sym_parser
DEPENDS libdebug_debug_sym_parser
OUTPUT libdebug_debug_sym_parser.pyi
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/libdebug/native/libdebug_debug_sym_parser.pyi"
PYTHON_PATH "."
)

Expand Down
3 changes: 1 addition & 2 deletions examples/bof_detection/detect_bof.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import magic

from libdebug import debugger, libcontext
from libdebug.utils.debugging_utils import resolve_address_in_maps

libcontext.sym_lvl = 5

Expand Down Expand Up @@ -195,7 +194,7 @@ def print_color(message, color, end="\n"):

# If the current rip corresponds to a known symbol, print the symbol
try:
symbol = resolve_address_in_maps(curr_rip, d.maps)
symbol = d._internal_debugger._symbol_manager.resolve_address(curr_rip, d.maps)

if not symbol.startswith("0x"):
print_color(f"<{symbol}> ", color=LT_COLOR_CYAN, end="")
Expand Down
4 changes: 2 additions & 2 deletions libdebug/architectures/aarch64/aarch64_call_utilities.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2024 Roberto Alessandro Bertolini. All rights reserved.
# Copyright (c) 2024-2026 Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from libdebug.architectures.call_utilities_manager import CallUtilitiesManager
from libdebug.architectures.shared.call_utilities_manager import CallUtilitiesManager


class Aarch64CallUtilities(CallUtilitiesManager):
Expand Down
4 changes: 2 additions & 2 deletions libdebug/architectures/aarch64/aarch64_stack_unwinder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2023-2024 Roberto Alessandro Bertolini, Francesco Panebianco, Gabriele Digregorio. All rights reserved.
# Copyright (c) 2023-2026 Roberto Alessandro Bertolini, Francesco Panebianco, Gabriele Digregorio. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

Expand All @@ -9,7 +9,7 @@
import sys
from typing import TYPE_CHECKING

from libdebug.architectures.stack_unwinding_manager import StackUnwindingManager
from libdebug.architectures.shared.stack_unwinding_manager import StackUnwindingManager
from libdebug.liblog import liblog

if TYPE_CHECKING:
Expand Down
60 changes: 60 additions & 0 deletions libdebug/architectures/aarch64/aarch64_tls_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2026 Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from typing import TYPE_CHECKING

from libdebug.architectures.shared.tls_resolver import TLSResolver
from libdebug.liblog import liblog

if TYPE_CHECKING:
from libdebug.native.libdebug_debug_sym_parser import TLSModuleInfo
from libdebug.state.thread_context import ThreadContext


class Aarch64TLSResolver(TLSResolver):
"""TLS resolver for the AArch64 architecture.

On AArch64 Linux, TLS is accessed via the TPIDR_EL0 register.
Uses variant 1 TLS layout (TLS data after thread pointer, positive offsets).
"""

def get_tls_base(self: Aarch64TLSResolver, thread: ThreadContext) -> int | None:
"""Get the TPIDR_EL0 register value for a thread.

Args:
thread: The thread context.

Returns:
The TPIDR_EL0 value, or None if not available.
"""
try:
regs = thread.regs
if hasattr(regs, "tpidr_el0"):
return regs.tpidr_el0
except (AttributeError, OSError) as e:
liblog.debugger("Failed to get tpidr_el0: %s", e)
return None

def compute_tls_address(
self: Aarch64TLSResolver,
tls_base: int,
tls_offset: int,
_: TLSModuleInfo | None,
) -> int:
"""Compute TLS address using variant 1 layout (positive offsets).

Args:
tls_base: The TLS base address for the thread.
tls_offset: The offset of the symbol within the TLS block.
tls_module: Optional TLS module info (unused for variant 1).

Returns:
The computed address.
"""
# Variant 1: TLS data is after the thread pointer
return tls_base + tls_offset
4 changes: 2 additions & 2 deletions libdebug/architectures/amd64/amd64_call_utilities.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2024 Francesco Panebianco. All rights reserved.
# Copyright (c) 2024-2026 Francesco Panebianco. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from libdebug.architectures.call_utilities_manager import CallUtilitiesManager
from libdebug.architectures.shared.call_utilities_manager import CallUtilitiesManager


class Amd64CallUtilities(CallUtilitiesManager):
Expand Down
4 changes: 2 additions & 2 deletions libdebug/architectures/amd64/amd64_stack_unwinder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2023-2024 Gabriele Digregorio, Roberto Alessandro Bertolini, Francesco Panebianco. All rights reserved.
# Copyright (c) 2023-2026 Gabriele Digregorio, Roberto Alessandro Bertolini, Francesco Panebianco. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

Expand All @@ -9,7 +9,7 @@
import sys
from typing import TYPE_CHECKING

from libdebug.architectures.stack_unwinding_manager import StackUnwindingManager
from libdebug.architectures.shared.stack_unwinding_manager import StackUnwindingManager
from libdebug.liblog import liblog

if TYPE_CHECKING:
Expand Down
41 changes: 41 additions & 0 deletions libdebug/architectures/amd64/amd64_tls_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2026 Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from typing import TYPE_CHECKING

from libdebug.architectures.shared.tls_resolver import TLSResolver
from libdebug.liblog import liblog

if TYPE_CHECKING:
from libdebug.state.thread_context import ThreadContext


class Amd64TLSResolver(TLSResolver):
"""TLS resolver for the x86-64 architecture.

On x86-64 Linux, TLS is accessed via the FS segment register.
The FS base points to the Thread Control Block (TCB).
Static TLS variables are stored at negative offsets from FS base.
"""

def get_tls_base(self: Amd64TLSResolver, thread: ThreadContext) -> int | None:
"""Get the FS base address for a thread.

Args:
thread: The thread context.

Returns:
The FS base address, or None if not available.
"""
try:
regs = thread.regs
if hasattr(regs, "fs_base"):
return regs.fs_base
except (AttributeError, OSError) as e:
liblog.debugger("Failed to get fs_base: %s", e)
return None
4 changes: 2 additions & 2 deletions libdebug/architectures/call_utilities_provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2024 Francesco Panebianco, Roberto Alessandro Bertolini. All rights reserved.
# Copyright (c) 2024-2026 Francesco Panebianco, Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

Expand All @@ -10,10 +10,10 @@
from libdebug.architectures.amd64.amd64_call_utilities import (
Amd64CallUtilities,
)
from libdebug.architectures.call_utilities_manager import CallUtilitiesManager
from libdebug.architectures.i386.i386_call_utilities import (
I386CallUtilities,
)
from libdebug.architectures.shared.call_utilities_manager import CallUtilitiesManager

_aarch64_call_utilities = Aarch64CallUtilities()
_amd64_call_utilities = Amd64CallUtilities()
Expand Down
4 changes: 2 additions & 2 deletions libdebug/architectures/i386/i386_call_utilities.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2024 Roberto Alessandro Bertolini, Francesco Panebianco. All rights reserved.
# Copyright (c) 2024-2026 Roberto Alessandro Bertolini, Francesco Panebianco. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from libdebug.architectures.call_utilities_manager import CallUtilitiesManager
from libdebug.architectures.shared.call_utilities_manager import CallUtilitiesManager


class I386CallUtilities(CallUtilitiesManager):
Expand Down
4 changes: 2 additions & 2 deletions libdebug/architectures/i386/i386_stack_unwinder.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2023-2024 Gabriele Digregorio, Roberto Alessandro Bertolini, Francesco Panebianco. All rights reserved.
# Copyright (c) 2023-2026 Gabriele Digregorio, Roberto Alessandro Bertolini, Francesco Panebianco. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from typing import TYPE_CHECKING

from libdebug.architectures.stack_unwinding_manager import StackUnwindingManager
from libdebug.architectures.shared.stack_unwinding_manager import StackUnwindingManager
from libdebug.liblog import liblog

if TYPE_CHECKING:
Expand Down
41 changes: 41 additions & 0 deletions libdebug/architectures/i386/i386_tls_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2026 Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

from __future__ import annotations

from typing import TYPE_CHECKING

from libdebug.architectures.shared.tls_resolver import TLSResolver
from libdebug.liblog import liblog

if TYPE_CHECKING:
from libdebug.state.thread_context import ThreadContext


class I386TLSResolver(TLSResolver):
"""TLS resolver for the i386 architecture.

On i386 Linux, TLS is accessed via the GS segment register.
The GS base points to the Thread Control Block (TCB).
Uses variant 2 TLS layout (same as x86-64).
"""

def get_tls_base(self: I386TLSResolver, thread: ThreadContext) -> int | None:
"""Get the GS base address for a thread.

Args:
thread: The thread context.

Returns:
The GS base address, or None if not available.
"""
try:
regs = thread.regs
if hasattr(regs, "gs_base"):
return regs.gs_base
except (AttributeError, OSError) as e:
liblog.debugger("Failed to get gs_base: %s", e)
return None
Empty file.
Loading
Loading