From patchwork Sat Dec 25 20:23:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1573192 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=fwb3B76b; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4JLwPq5t5sz9sCD for ; Sun, 26 Dec 2021 07:24:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29FB53858416 for ; Sat, 25 Dec 2021 20:24:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29FB53858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1640463851; bh=PHgXMic6Y4/67LXmPDzwy2uYq8KYq0bCgEZLb8SXTsw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fwb3B76boQjrABpRswzZoasBY/JxJlZ2FgRbjYXqtJxJk0KaLD5vmGzf+Ydmr+1Vi OhOkaFlJUeNuBvkDyKUNjjBqzUSB8lS7y1v21ROAPFJSsj6l8jzqE105FO+SzZWylP u+cn0gVi1cFLrNEGagHX+xCyEM7TmNBHPU4NY2Zo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id A57F13858C27 for ; Sat, 25 Dec 2021 20:23:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A57F13858C27 Received: by mail-pj1-x102a.google.com with SMTP id rj2-20020a17090b3e8200b001b1944bad25so10932152pjb.5 for ; Sat, 25 Dec 2021 12:23:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=PHgXMic6Y4/67LXmPDzwy2uYq8KYq0bCgEZLb8SXTsw=; b=zF7LbuddZn7/dnO3AHVwys+efR5ZPIrZHOW+fnwrXqL1Osq5Zy3/o35KucZOTcvOzW lrabjVEhD0IH4UGUGZc2dOcUzs+bc2AZryU7lmHfRdiLdmvW27Mnn22Csp1RVdjL61Rw lUhZ/67mdsjWqq9Kk9U0yk/R7l8ciMZg0rmJkywuKPQkcrPj/5M2Zt2y5JkQ3L33Sc2Q 7wOvoHt734v/F4j/kiJ0cBpMDukfmfdASqH30Li1dV2/FiXGj8ccEzVa2byeXbvX1Zp9 QLfaFTaBn1XbAsw34jIPuiNvJh85m8QgvY/bA91miNzuFeC0PTVGd5+W2Rroc+rX9egu au4Q== X-Gm-Message-State: AOAM532kGWnqxp1UdsoiR8CAchrmI0aySvH4u1NVElM+wGm4aBXSVoNJ qFNQxQKboQhQizwQlAKT/KlyLKpqO1U= X-Google-Smtp-Source: ABdhPJzVEfK7tUCKbgEuB80R4cTng8ESReSQxJ4c++YlU7iNu1oQEvOvGHfhjBEbYdQcC1wL/Q/nkA== X-Received: by 2002:a17:902:bc85:b0:143:954e:8548 with SMTP id bb5-20020a170902bc8500b00143954e8548mr11191907plb.82.1640463835311; Sat, 25 Dec 2021 12:23:55 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id b9sm13012763pfm.127.2021.12.25.12.23.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Dec 2021 12:23:54 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id AD85EC007F for ; Sat, 25 Dec 2021 12:23:53 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH] elf: Add Date: Sat, 25 Dec 2021 12:23:53 -0800 Message-Id: <20211225202353.789968-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3029.0 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 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" Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Add to setup debugging entry in PT_DYNAMIC segment to support DT_DEBUG, DT_MIPS_RLD_MAP_REL and DT_MIPS_RLD_MAP. Tested on x86-64, x32 and i686 as well as with build-many-glibcs.py. --- elf/dl-reloc-static-pie.c | 11 ++-------- elf/rtld.c | 13 ++++-------- sysdeps/generic/dl-debug.h | 33 ++++++++++++++++++++++++++++++ sysdeps/mips/dl-debug.h | 41 ++++++++++++++++++++++++++++++++++++++ sysdeps/mips/dl-machine.h | 15 -------------- 5 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 sysdeps/generic/dl-debug.h create mode 100644 sysdeps/mips/dl-debug.h diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index 5b85df8a2e..ad91721fe3 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -24,6 +24,7 @@ #include #include +#include #define RESOLVE_MAP(map, scope, sym, version, flags) map #include "dynamic-link.h" @@ -68,14 +69,6 @@ _dl_relocate_static_pie (void) /* Set up debugging before the debugger is notified for the first time. */ -# ifdef ELF_MACHINE_DEBUG_SETUP - /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */ - ELF_MACHINE_DEBUG_SETUP (main_map, r); -# else - if (main_map->l_info[DT_DEBUG] != NULL) - /* There is a DT_DEBUG entry in the dynamic section. Fill it in - with the run-time address of the r_debug structure */ - main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; -# endif + elf_setup_debug_entry (main_map, r); } #endif diff --git a/elf/rtld.c b/elf/rtld.c index 4b09e84b0d..f75cb37e47 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -62,6 +62,9 @@ #define RESOLVE_MAP(map, scope, sym, version, flags) map #include "dynamic-link.h" +/* Must include after for DT_MIPS definition. */ +#include + /* Only enables rtld profiling for architectures which provides non generic hp-timing support. The generic support requires either syscall (clock_gettime), which will incur in extra overhead on loading time. @@ -1796,15 +1799,7 @@ dl_main (const ElfW(Phdr) *phdr, size_t count_modids = _dl_count_modids (); /* Set up debugging before the debugger is notified for the first time. */ -#ifdef ELF_MACHINE_DEBUG_SETUP - /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */ - ELF_MACHINE_DEBUG_SETUP (main_map, r); -#else - if (main_map->l_info[DT_DEBUG] != NULL) - /* There is a DT_DEBUG entry in the dynamic section. Fill it in - with the run-time address of the r_debug structure */ - main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; -#endif + elf_setup_debug_entry (main_map, r); /* We start adding objects. */ r->r_state = RT_ADD; diff --git a/sysdeps/generic/dl-debug.h b/sysdeps/generic/dl-debug.h new file mode 100644 index 0000000000..b492c0bcc6 --- /dev/null +++ b/sysdeps/generic/dl-debug.h @@ -0,0 +1,33 @@ +/* Debugging support. Generic version. + Copyright (C) 2021 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 + . */ + +#ifndef _DL_DEBUG_H +#define _DL_DEBUG_H + +/* There is a DT_DEBUG entry in the dynamic section. Fill it in with the + run-time address of the r_debug structure */ + +static inline void +__attribute ((always_inline)) +elf_setup_debug_entry (struct link_map *l, struct r_debug *r) +{ + if (l->l_info[DT_DEBUG] != NULL) + l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; +} + +#endif /* _DL_DEBUG_H */ diff --git a/sysdeps/mips/dl-debug.h b/sysdeps/mips/dl-debug.h new file mode 100644 index 0000000000..a4c8d49309 --- /dev/null +++ b/sysdeps/mips/dl-debug.h @@ -0,0 +1,41 @@ +/* Debugging support. MIPS version. + Copyright (C) 2021 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 + . */ + +#ifndef _DL_DEBUG_H +#define _DL_DEBUG_H + +/* If there is a DT_MIPS_RLD_MAP_REL or DT_MIPS_RLD_MAP entry in the + dynamic section, fill in the debug map pointer with the run-time + address of the r_debug structure. */ + +static inline void +__attribute ((always_inline)) +elf_setup_debug_entry (struct link_map *l, struct r_debug *r) +{ + if (l->l_info[DT_MIPS (RLD_MAP_REL)] != NULL) + { + char *ptr = (char *) l->l_info[DT_MIPS (RLD_MAP_REL)]; + ptr += l->l_info[DT_MIPS (RLD_MAP_REL)]->d_un.d_val; + *(ElfW(Addr) *) ptr = (ElfW(Addr)) r; + } + else if (l->l_info[DT_MIPS (RLD_MAP)] != NULL) + *(ElfW(Addr) *) (l->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) + = (ElfW(Addr)) r; +} + +#endif /* _DL_DEBUG_H */ diff --git a/sysdeps/mips/dl-machine.h b/sysdeps/mips/dl-machine.h index d7b8341b74..ea8c881807 100644 --- a/sysdeps/mips/dl-machine.h +++ b/sysdeps/mips/dl-machine.h @@ -65,21 +65,6 @@ in l_info array. */ #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM) -/* If there is a DT_MIPS_RLD_MAP_REL or DT_MIPS_RLD_MAP entry in the dynamic - section, fill in the debug map pointer with the run-time address of the - r_debug structure. */ -#define ELF_MACHINE_DEBUG_SETUP(l,r) \ -do { if ((l)->l_info[DT_MIPS (RLD_MAP_REL)]) \ - { \ - char *ptr = (char *)(l)->l_info[DT_MIPS (RLD_MAP_REL)]; \ - ptr += (l)->l_info[DT_MIPS (RLD_MAP_REL)]->d_un.d_val; \ - *(ElfW(Addr) *)ptr = (ElfW(Addr)) (r); \ - } \ - else if ((l)->l_info[DT_MIPS (RLD_MAP)]) \ - *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \ - (ElfW(Addr)) (r); \ - } while (0) - #if ((defined __mips_nan2008 && !defined HAVE_MIPS_NAN2008) \ || (!defined __mips_nan2008 && defined HAVE_MIPS_NAN2008)) # error "Configuration inconsistency: __mips_nan2008 != HAVE_MIPS_NAN2008, overridden CFLAGS?"