From patchwork Sat Jan 29 19:41:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1586253 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=iMM1d1qp; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JmPpP2stvz9t8j for ; Sun, 30 Jan 2022 06:41:32 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C3983858405 for ; Sat, 29 Jan 2022 19:41:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C3983858405 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1643485289; bh=MUnX1nsTjUgZapN7ZmUiVMgzE1rcVgNmlNd1pp35ck0=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=iMM1d1qpMKzQ+3Qob0xybspOp8QbP9+ogGUcJmq3fF461RAKhMyGmtPg+POExZkZq 4pRxLA/6NcjPl2ZXdGtS9Z6FGa98ezfrsQFLVmwuTgI2IFFfTNTcE0tWTM4/q/g5Bg nLw6KrpXjrneNAw9ZLjRrkfbSK00OS5QBK1+PQsU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 8670F3858D28 for ; Sat, 29 Jan 2022 19:41:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8670F3858D28 Received: by mail-pl1-x629.google.com with SMTP id c9so9078131plg.11 for ; Sat, 29 Jan 2022 11:41:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=MUnX1nsTjUgZapN7ZmUiVMgzE1rcVgNmlNd1pp35ck0=; b=f3hpVR/b9h2sA1kZRpR4XOEaeUVx+RqCfdItyyhf34JAGBbgochZOSxIAazbpPTLb/ MSgAA0mi3gBKgnwGxnVFr/zEzNeBvpnC87Sf8L4zX2TdwVzce8Hg3I0Zmxi3ozUyfcrS BTI1RWjkt4vf+pSfLx5rdwiKoC2ounNBsxX2Hm0+ypxv83yKAc9/HnrytJt59Vst5Oyg rbrHF4xixnJ+aZuaYebMNuEEg6v5+2r+oM//g1wNcwltGul4DeO6YIso9eGn2KS/X2v0 djtEoAsHlxkfYG4Mus0R8tqGLb3Cz1MSEiNk7uFhZBJ7AhVzBa+hRDRWmYB0Q6BHrnZV Zs5w== X-Gm-Message-State: AOAM533QwsHa/cxU2sjtsUcqPqn+96TFhvIv39BiQz0uPmvMa4bjhVBV RLF50lqL6p9CFput3eDyE+osur35Wo4= X-Google-Smtp-Source: ABdhPJxYL3pJlNHHVcHOJaRgAOZH7TYWK7xjqDfMqaFCqhEAU1oSNMOuVpOfL7jRCSlOGqJilIr9Fw== X-Received: by 2002:a17:90b:f10:: with SMTP id br16mr26306322pjb.57.1643485274440; Sat, 29 Jan 2022 11:41:14 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id mn2sm5980844pjb.38.2022.01.29.11.41.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jan 2022 11:41:14 -0800 (PST) Received: by gnu-tgl-3.localdomain (Postfix, from userid 1000) id 8797DC0903; Sat, 29 Jan 2022 11:41:12 -0800 (PST) Date: Sat, 29 Jan 2022 11:41:12 -0800 To: Xi Ruoyao Subject: [PATCH] elf: Add Message-ID: References: <046da3ae8fc57687b5a9480381904d3f53b1010f.camel@mengyan1223.wang> <4a518e0d1c617e248074db7b0cd122d3c86bb058.camel@mengyan1223.wang> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3028.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: syq@debian.org, GNU C Library , Jiaxun Yang , Joseph Myers Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" On Sat, Jan 29, 2022 at 06:04:12AM +0800, Xi Ruoyao wrote: > On Fri, 2022-01-28 at 13:22 -0800, H.J. Lu wrote: > > > You should drop pldd in filename and also handle tst-dlmopen4.c > > in the same header file. > > v3: pldd and tst-dlmopen4 change revised as a new patch series. Can you try this patch instead? H.J. --- Add to get the adddress of the r_debug structure after relocation and its offset before relocation from the PT_DYNAMIC segment to support DT_DEBUG, DT_MIPS_RLD_MAP_REL and DT_MIPS_RLD_MAP. Co-developed-by: Xi Ruoyao --- elf/pldd-xx.c | 34 +++++++++++-------- elf/tst-dlmopen4.c | 15 +++------ sysdeps/generic/dl-r_debug.h | 36 ++++++++++++++++++++ sysdeps/mips/dl-r_debug.h | 64 ++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 sysdeps/generic/dl-r_debug.h create mode 100644 sysdeps/mips/dl-r_debug.h diff --git a/elf/pldd-xx.c b/elf/pldd-xx.c index 1cdfb49c53..30513b423f 100644 --- a/elf/pldd-xx.c +++ b/elf/pldd-xx.c @@ -22,6 +22,8 @@ #define EW_(e, w, t) EW__(e, w, _##t) #define EW__(e, w, t) e##w##t +#include + struct E(link_map) { EW(Addr) l_addr; @@ -126,21 +128,25 @@ E(find_maps) (const char *exe, int memfd, pid_t pid, void *auxv, != p[i].p_filesz) error (EXIT_FAILURE, 0, gettext ("cannot read dynamic section")); - /* Search for the DT_DEBUG entry. */ + /* Search for the struct r_debug. */ for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j) - if (dyn[j].d_tag == DT_DEBUG && dyn[j].d_un.d_ptr != 0) - { - struct E(r_debug) r; - if (pread (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr) - != sizeof (r)) - error (EXIT_FAILURE, 0, gettext ("cannot read r_debug")); - - if (r.r_map != 0) - { - list = r.r_map; - break; - } - } + { + EW(Addr) off = offset + p[i].p_vaddr + sizeof (EW(Dyn)) * j; + off = E(r_debug_offset) (&dyn[j], memfd, off); + if (off != 0) + { + struct E(r_debug) r; + if (pread (memfd, &r, sizeof (r), off) + != sizeof (r)) + error (EXIT_FAILURE, 0, gettext ("cannot read r_debug")); + + if (r.r_map != 0) + { + list = r.r_map; + break; + } + } + } free (dyn); break; diff --git a/elf/tst-dlmopen4.c b/elf/tst-dlmopen4.c index d8bcf7e9d5..8456f89053 100644 --- a/elf/tst-dlmopen4.c +++ b/elf/tst-dlmopen4.c @@ -25,16 +25,9 @@ #include #include -#ifndef ELF_MACHINE_GET_R_DEBUG -# define ELF_MACHINE_GET_R_DEBUG(d) \ - (__extension__ ({ \ - struct r_debug_extended *debug; \ - if ((d)->d_tag == DT_DEBUG) \ - debug = (struct r_debug_extended *) (d)->d_un.d_ptr; \ - else \ - debug = NULL; \ - debug; })) -#endif +#define E(x) x +#define EW(x) ElfW(x) +#include static int do_test (void) @@ -44,7 +37,7 @@ do_test (void) for (d = _DYNAMIC; d->d_tag != DT_NULL; ++d) { - debug = ELF_MACHINE_GET_R_DEBUG (d); + debug = (struct r_debug_extended *) r_debug_address (d); if (debug != NULL) break; } diff --git a/sysdeps/generic/dl-r_debug.h b/sysdeps/generic/dl-r_debug.h new file mode 100644 index 0000000000..e3edeb4f06 --- /dev/null +++ b/sysdeps/generic/dl-r_debug.h @@ -0,0 +1,36 @@ +/* Function to access r_debug structure. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Return the address of the struct r_debug after relocation. */ + +static inline EW(Addr) +E(r_debug_address) (EW(Dyn) *d) +{ + if (d->d_tag == DT_DEBUG) + return (EW(Addr)) d->d_un.d_ptr; + + return 0; +} + +/* Return the offset of the struct r_debug before relocation. */ + +static inline EW(Addr) +E(r_debug_offset) (EW(Dyn) *d, int fd, EW(Addr) offset) +{ + return E(r_debug_address) (d); +} diff --git a/sysdeps/mips/dl-r_debug.h b/sysdeps/mips/dl-r_debug.h new file mode 100644 index 0000000000..edd83d46fd --- /dev/null +++ b/sysdeps/mips/dl-r_debug.h @@ -0,0 +1,64 @@ +/* Function to access r_debug structure. MIPS specific version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifdef EW_ +/* Return the offset of the struct r_debug before relocation. */ + +static inline EW(Addr) +E(r_debug_offset) (EW(Dyn) *d, int fd, EW(Addr) offset) +{ + switch (d->d_tag) + { + case DT_MIPS_RLD_MAP_REL: + offset += d->d_un.d_val; + break; + case DT_MIPS_RLD_MAP: + offset = d->d_un.d_ptr; + break; + default: + return 0; + } + + if (pread (fd, &offset, sizeof (offset), offset) != sizeof (offset)) + return 0; + + return offset; +} +#else +/* Return the address of the struct r_debug after relocation. */ + +static inline EW(Addr) +E(r_debug_address) (EW(Dyn) *d) +{ + EW(Addr) ptr; + + switch (d->d_tag) + { + case DT_MIPS_RLD_MAP_REL: + ptr = ((EW(Addr)) d) + d->d_un.d_val; + break; + case DT_MIPS_RLD_MAP: + ptr = d->d_un.d_ptr; + break; + default: + return 0; + } + + return *(EW(Addr) *) ptr; +} +#endif