From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id USWcGwZW2mrvWAwAWB0awg (envelope-from ) for ; Thu, 22 Oct 2026 14:29:26 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=a/JX/Vkb; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 6BA531E007; Thu, 22 Oct 2026 14:29:26 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id BB2971E007 for ; Thu, 22 Oct 2026 14:29:25 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E25A14BA79A2 for ; Thu, 22 Oct 2026 18:29:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E25A14BA79A2 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=a/JX/Vkb Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id E2A364BA2E07 for ; Thu, 22 Oct 2026 18:28:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2A364BA2E07 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E2A364BA2E07 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::329 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792693702; cv=none; b=pioczUvmHdY2Ov4PLOIDLc+YvnAkx+RSAs317djJL3RMR51YuptIid8hELLqiGM6Kz3pZe1oO0YI7bu+FdCvlILoCvP25vxpgNXA54Ko4m284WtaQ/la6rV4kh9IGgnNzXn2XrdcFGAvgEW/6Y6fpSoqLkJYKiDCXZPeNRD519c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792693702; c=relaxed/simple; bh=cPPJssPTuB7rCnDR/WNXdPNRB0wSJrEmv8RirxGFm8Y=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=slKg9Gf6l1G/qVoq+o+aBgee+G/Zl6LAvAUW0pjFkESGFHBOHwhMVnxnOe/XxgAxjPi1SfFwUt0ryG9aYQkfw49H3YHgsZwxhgw0yL/kh7uLdpBEEO9GFWFJMXU67z0YZ/ADe5SARIXfD61XSsjvHjbD6TM31aMA4p5eAZp0n1A= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=a/JX/Vkb DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2A364BA2E07 Received: by mail-ot1-x329.google.com with SMTP id 46e09a7af769-84621d8c7aaso24810a34.2 for ; Thu, 22 Oct 2026 11:28:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1792693701; x=1793298501; darn=sourceware.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=5ZTdO7igJ/Zo7C6CPAF9dlIKQ5nKXBRTdtcsqq86vl4=; b=a/JX/Vkbx3GRTdc3Pmf5jfPHYye5lXdmlvZyPGz2Jacjk/+Jl9f4GRP74hWSq5CL0S Ytc8N5nH9tI3/uwH/R5FxtStKDGicySyfT1ZNx9R5sTD7DPaV31DR5gxXevGX2UmPrjh n3dzLuU3LsYevuONAMPgsRP4ZUimYongEIWDNPyrrNYttydIs3Q7OcWU1NdC4gj78WuP 0wi2gqPJctpnsna6k3V3WpSTRuAbm6zseoNwf89rAjaTCBOkghB5xKi8FUM/MfwWx9Jh Qy6FCSR4KhMYHY0cqm9/jzoN2CrscJ5VS/qRWiJVoRjA1ykK+76V+O9TZAyGgVc+hoGo 6vXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792693701; x=1793298501; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=5ZTdO7igJ/Zo7C6CPAF9dlIKQ5nKXBRTdtcsqq86vl4=; b=TWwDDFBG3p/q0pRiH+/4dBQdGlz17JVbn61YwpFiqPEiCLDZl1raI67BSygLD7cRKy jLxBXAT6MQsIZsxYX1apbuD/oEseW1qv0DAIU5atFXv3AbUprtwf7Amww7NzrEnHQbV4 OUigevJxdOHOp8AdqpK4LBf6SfbfoNDnuoLfX1ljkOznPxFbLM5hDpgQsEydlaE2/+Ff y7HwbtDdkMBsg7eqH71/x/W2rRRs7HVsVFNmHrucjkfoR+sYHIVOEFbNzI2kYXcNhSMl ewRqmLAu5w7NCKP//4wrIDaytJ6aCwkf61dK1OsLjYKDIuriVNh1iqvjeqZWQ9UKtawg Ds4g== X-Gm-Message-State: AFq9FYITS7AKqN29bH+T6nwtXLJJWt2ztrEGwpE83HwLYE3PuRcoHPyD 1rsenlqOvtUjbeCpksx7HKZFtnwcw1RwfZabd/SwTPUoKpcIgytvBgEnzOTnxVRqxNnHy+uGb97 HsNm82Q== X-Gm-Gg: AYBFou3c7VpjH+CEe871h1qieR7ucUWAJxozWghhsXERedK9D7SPYM7oVdI6vEpExtO 72qNLhyub7ZXXDzR8i96cY0eySlwqX/yikp+zI3skmYWIMyOvvEQGe3SS3w7I7qz0GC+hZ5RarC 9tUWqovSX95hSfw2JR9NG9H9Mk7EIZP3Zys5TwcBQsKrrT+SGAgbILTbb1MBf6JiIFQ/m+/P3dr +GX3oz6CkR6NrizgudU7NQOgKIcs3XUov6g88FKojyKsy/LaGwFdepDPIoEo+ocuhpnqyjCrdSp 27jDq6vtTWrO0ptsf96dbPZGnzxLHJWTpwyABLdYU+EHIfZgfEIf6ntRrosEN2Y5WzC+ACtQd3R o5wPDfjvI3xDPgD7auTSRaJKadBu+ZpyjwXwV/BKJob1pisGB65TXh4c2doFlDu2jd0G52VxRU/ s4ZYoCGXg2I/VOaaxewz0Yx0OkMsRijgwnCxX2IY/q+aJJ4FTdGPhuILoqnQE13Kl7WNvKZxRCn J7jXrT4dr9+xxutRmHyJgvYQsZr3o/Qt+BbJU5/EvP53LoCzIHdIn/Dzptfk6uFXph+bQztSgl7 AJAasnSyXY5Y/w== X-Received: by 2002:a05:6830:6d52:b0:82f:5471:4dd7 with SMTP id 46e09a7af769-84d09f03601mr4854239a34.30.1792693701084; Thu, 22 Oct 2026 11:28:21 -0700 (PDT) Received: from [192.168.0.26] (97-122-110-129.hlrn.qwest.net. [97.122.110.129]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-84f784f3c50sm419557a34.21.2026.10.22.11.28.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2026 11:28:20 -0700 (PDT) From: Tom Tromey Date: Thu, 22 Oct 2026 12:28:12 -0600 Subject: [PATCH RFC 2/6] Add py-iovec.c MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261022-android-apk-file-v1-2-8444bb5f832a@adacore.com> References: <20261022-android-apk-file-v1-0-8444bb5f832a@adacore.com> In-Reply-To: <20261022-android-apk-file-v1-0-8444bb5f832a@adacore.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.15.2 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This patch adds py-iovec.c, which provides a way to construct a new BFD that can access Python objects to get the underlying data. Due to how subsequent patches are written, Python file-like objects aren't used directly, but instead a wrapper will be provided. --- gdb/Makefile.in | 1 + gdb/python/py-iovec.c | 130 +++++++++++++++++++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 + 3 files changed, 133 insertions(+) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 4260c90d425..e6c99924451 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -413,6 +413,7 @@ SUBDIR_PYTHON_SRCS = \ python/py-infevents.c \ python/py-infthread.c \ python/py-instruction.c \ + python/py-iovec.c \ python/py-lazy-string.c \ python/py-linetable.c \ python/py-membuf.c \ diff --git a/gdb/python/py-iovec.c b/gdb/python/py-iovec.c new file mode 100644 index 00000000000..4bcea6d8a3b --- /dev/null +++ b/gdb/python/py-iovec.c @@ -0,0 +1,130 @@ +/* Python interface to BFD iovec + + Copyright (C) 2026 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "gdb_bfd.h" +#include "gdbcore.h" +#include "python-internal.h" + +struct py_iovec : public gdb_bfd_iovec_base +{ + py_iovec (gdbpy_ref<> &&file_obj, ULONGEST file_size) + : m_file (std::move (file_obj)), + m_size (file_size) + { + } + + ~py_iovec (); + + file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes, + file_ptr offset) override; + + int stat (struct bfd *abfd, struct stat *sb) override + { + /* We only support the size here. */ + memset (sb, 0, sizeof (*sb)); + sb->st_size = m_size; + return 0; + } + + gdbpy_ref<> m_file; + ULONGEST m_size; +}; + +py_iovec::~py_iovec () +{ + /* Make sure the GIL is held before destruction. */ + gdbpy_enter enter_py; + + gdbpy_ref<> result = gdbpy_call_method (m_file.get (), "close"); + if (result == nullptr) + gdbpy_print_stack (); + + m_file.reset (nullptr); +} + +file_ptr +py_iovec::read (bfd *abfd, void *buffer, file_ptr nbytes, file_ptr offset) +{ + /* If this read will read all of the file, limit it to just the rest. */ + if (offset + nbytes > m_size) + nbytes = m_size - offset; + + /* If there are no more bytes left, we've reached EOF. */ + if (nbytes == 0) + return 0; + + gdbpy_enter enter_py; + + char *data; + Py_ssize_t len; + gdbpy_ref<> bytes = gdbpy_call_method (m_file.get (), "read", + (gdb_py_ulongest) offset, + (gdb_py_ulongest) nbytes); + if (bytes == nullptr + || PyBytes_AsStringAndSize (bytes.get (), &data, &len) == -1) + { + gdbpy_print_stack (); + bfd_set_error (bfd_error_bad_value); + return -1; + } + + memcpy (buffer, data, len); + return len; +} + +gdb_bfd_ref_ptr +open_bfd_from_python (gdbpy_ref<> &&file_obj) +{ + int has_attr = PyObject_HasAttrString (file_obj.get (), "read"); + if (has_attr != 1) + { + /* This may return without the error set, but this scenario is + handled by the caller. */ + return nullptr; + } + + gdbpy_ref<> size_obj = gdbpy_call_method (file_obj.get (), "size"); + if (size_obj == nullptr) + return nullptr; + gdb_py_ulongest size = gdb_py_long_as_ulongest (size_obj.get ()); + if (PyErr_Occurred ()) + return nullptr; + + gdbpy_ref<> name_obj = gdbpy_call_method (file_obj.get (), "filename"); + if (name_obj == nullptr) + return nullptr; + gdb::unique_xmalloc_ptr filename + = python_string_to_host_string (name_obj.get ()); + if (filename == nullptr) + return nullptr; + + auto iovec = std::make_unique (std::move (file_obj), size); + + auto open = [&] (bfd *nbfd) -> py_iovec * + { + return iovec.release (); + }; + + gdb_bfd_ref_ptr result = gdb_bfd_openr_iovec (filename.get (), + gnutarget, open); + if (result == nullptr) + PyErr_Format (PyExc_OSError, "could not open %s: %s", + filename.get (), bfd_errmsg (bfd_get_error ())); + return result; +} diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 2e064107cbc..bf7eb795061 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -1416,6 +1416,8 @@ class gdbpy_memoizing_registry_storage extern gdbpy_ref<> eval_python_command (const char *command, int start_symbol, const char *filename = nullptr); +extern gdb_bfd_ref_ptr open_bfd_from_python (gdbpy_ref<> &&file_obj); + /* The following four functions are refcount-safe wrappers around Py_RETURN_{NONE,TRUE,FALSE,NOTIMPLEMENTED}. */ -- 2.56.0