From patchwork Sun Aug 15 00:23:38 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: 44666 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3960E3892016 for ; Sun, 15 Aug 2021 00:25:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3960E3892016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628987145; bh=Hq91ZGg7YKoVg4oGoIsIl5PU+ptVmnKe5Yuaue+7LN8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=p8Aa9EeG/0IrolCDj/zZSugmEv19BQPx6nBZAeC8lZGhZ91qYhiWm40Fs3AaLGTZA 2UzJxq4WAXS7Tnuhpm2c6PhZoyf6Kci7jufL/C6iTIpso7v1i2Z6vmnf1fRkng6+/m 9CzVb0wTzH4Rx3kv9gFiXhqa/98ztnbWILEiyndo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 9C4803858C39; Sun, 15 Aug 2021 00:23:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9C4803858C39 Received: by mail-pl1-x634.google.com with SMTP id d17so16543455plr.12; Sat, 14 Aug 2021 17:23:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Hq91ZGg7YKoVg4oGoIsIl5PU+ptVmnKe5Yuaue+7LN8=; b=WNY97HTNUBlO6EQOWfFQWSI/pKE4o/IFUo5oKziCG87ldNLPGgzIP7c3wnbY3oGkaZ D1U41FhDtq9JT2WPvGq50My4coqcVoHW9eK0h4mNjBI/zyActBhUqWrltW4MD3qrfYHV nT43bm3IIyKGDomPs/Ak4I3PmLR8gzGLABp0VWgFVhXNuYb6YeI6MB5rSS1RAM6GYkK+ HX6vKbsH0S11VSyx9c/uNKVDC0CwrxVrBW90NJeUlc9CM2J4YcOqGh2PQF+lGowFRci/ XlAOkDsVt2firFoP9mJEmm8ON4kuhDeS5a447/lLOFAgE9QB0uyCLQmMjW/zZNF6iV1n /CKQ== X-Gm-Message-State: AOAM530wFf+Y6DC795hIkXRM7GFV46Z7FyOo4SuzNqmSs2krHaXBrlqm lxtlRDnVYHv53JdlpwmWskBKB7zg+sU= X-Google-Smtp-Source: ABdhPJxk3NmVKR+boVuG5VffiiNYrONyqAu+ir2uCppfIHWoOT+AKMzbo13JAibT3ctVL/H9mUPTsQ== X-Received: by 2002:a17:90b:2304:: with SMTP id mt4mr9465576pjb.180.1628987020465; Sat, 14 Aug 2021 17:23:40 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.58.38.240]) by smtp.gmail.com with ESMTPSA id fh19sm5702341pjb.27.2021.08.14.17.23.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Aug 2021 17:23:40 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id EBEB5C007B; Sat, 14 Aug 2021 17:23:38 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH] elf: Add -z [dt-debugsz|nodt-debugsz] to emit DT_DEBUGSZ Date: Sat, 14 Aug 2021 17:23:38 -0700 Message-Id: <20210815002338.2779683-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3033.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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" Cc: libc-alpha@sourceware.org, gdb-patches@sourceware.org Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Add -z [dt-debugsz|nodt-debugsz] to emit a DT_DEBUGSZ dynamic tag so that the dynamic linker can set its value to the size of the structure of debugger interface, whose address is stored in the DT_DEBUG dynamic tag, used by the dynamic linker. bfd/ PR gdb/11839 * elf.c (_bfd_elf_print_private_bfd_data): Support DT_DEBUGSZ. * elflink.c (bfd_elf_size_dynamic_sections): Add DT_DEBUGSZ for -z dt-debugsz in executables. binutils/ PR gdb/11839 * readelf.c (get_dynamic_type): Support DT_DEBUGSZ. include/ PR gdb/11839 * bfdlink.h (bfd_link_info): Add emit_dt_debugsz. * elf/common.h (DT_DEBUGSZ): New. ld/ PR gdb/11839 * NEWS: Mention -z dt-debugsz and -z nodt-debugsz. * configure.ac (--enable-dt-debugsz): New. Default to yes. Define DEFAULT_EMIT_DT_DEBUGSZ. * ld.texi: Document -z dt-debugsz and -z nodt-debugsz. * ldmain.c (main): Initialize link_info.emit_dt_debugsz to DEFAULT_EMIT_DT_DEBUGSZ. * lexsup.c (elf_shlib_list_options): Add -z dt-debugsz and -z nodt-debugsz. * config.in: Regenerate * configure: Likewise. * emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Handle -z dt-debugsz and -z nodt-debugsz. * testsuite/config/default.exp (LDFLAGS): Add "-z nodt-debugsz". * testsuite/ld-elf/dt-debugsz-1.d: New test. * testsuite/ld-elf/dt-debugsz-2.d: Likewise. --- bfd/elf.c | 1 + bfd/elflink.c | 5 +++++ binutils/readelf.c | 1 + include/bfdlink.h | 3 +++ include/elf/common.h | 1 + ld/NEWS | 3 +++ ld/config.in | 4 ++++ ld/configure | 27 +++++++++++++++++++++++++-- ld/configure.ac | 16 ++++++++++++++++ ld/emultempl/elf.em | 4 ++++ ld/ld.texi | 4 ++++ ld/ldmain.c | 1 + ld/lexsup.c | 4 ++++ ld/testsuite/config/default.exp | 3 +++ ld/testsuite/ld-elf/dt-debugsz-1.d | 9 +++++++++ ld/testsuite/ld-elf/dt-debugsz-2.d | 10 ++++++++++ 16 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 ld/testsuite/ld-elf/dt-debugsz-1.d create mode 100644 ld/testsuite/ld-elf/dt-debugsz-2.d diff --git a/bfd/elf.c b/bfd/elf.c index 380d64d699d..8e2ea1f2b36 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -1775,6 +1775,7 @@ _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) case DT_RELENT: name = "RELENT"; break; case DT_PLTREL: name = "PLTREL"; break; case DT_DEBUG: name = "DEBUG"; break; + case DT_DEBUGSZ: name = "DEBUGSZ"; break; case DT_TEXTREL: name = "TEXTREL"; break; case DT_JMPREL: name = "JMPREL"; break; case DT_BIND_NOW: name = "BIND_NOW"; break; diff --git a/bfd/elflink.c b/bfd/elflink.c index c9d5da2dab4..94be0a0e69a 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -7241,6 +7241,11 @@ bfd_elf_size_dynamic_sections (bfd *output_bfd, if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) { + if (info->emit_dt_debugsz + && bfd_link_executable (info) + && !_bfd_elf_add_dynamic_entry (info, DT_DEBUGSZ, 0)) + return false; + if (elf_tdata (output_bfd)->cverdefs) { unsigned int crefs = elf_tdata (output_bfd)->cverdefs; diff --git a/binutils/readelf.c b/binutils/readelf.c index bedd3893b59..92e696c5f0e 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -2198,6 +2198,7 @@ get_dynamic_type (Filedata * filedata, unsigned long type) case DT_RELENT: return "RELENT"; case DT_PLTREL: return "PLTREL"; case DT_DEBUG: return "DEBUG"; + case DT_DEBUGSZ: return "DEBUGSZ"; case DT_TEXTREL: return "TEXTREL"; case DT_JMPREL: return "JMPREL"; case DT_BIND_NOW: return "BIND_NOW"; diff --git a/include/bfdlink.h b/include/bfdlink.h index 566529ee644..3f8606b11ad 100644 --- a/include/bfdlink.h +++ b/include/bfdlink.h @@ -429,6 +429,9 @@ struct bfd_link_info /* TRUE if .gnu.hash section should be created. */ unsigned int emit_gnu_hash: 1; + /* TRUE if DT_DEBUGSZ dynamic tag should be emitted. */ + unsigned int emit_dt_debugsz: 1; + /* If TRUE reduce memory overheads, at the expense of speed. This will cause map file generation to use an O(N^2) algorithm and disable caching ELF symbol buffer. */ diff --git a/include/elf/common.h b/include/elf/common.h index 95ade894e98..f4f8467fe26 100644 --- a/include/elf/common.h +++ b/include/elf/common.h @@ -1125,6 +1125,7 @@ #define DT_SYMINFO 0x6ffffeff #define DT_ADDRRNGHI 0x6ffffeff +#define DT_DEBUGSZ 0x6ffffff8 #define DT_RELACOUNT 0x6ffffff9 #define DT_RELCOUNT 0x6ffffffa #define DT_FLAGS_1 0x6ffffffb diff --git a/ld/NEWS b/ld/NEWS index 6e0b56658d3..a8f4c026482 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -1,5 +1,8 @@ -*- text -*- +* Add -z dt-debugsz and -z nodt-debugsz to ELF linker to control whether + DT_DEBUGSZ dynamic tag should be emitted. + * Add -z indirect-extern-access/-z noindirect-extern-access to x86 ELF linker to control canonical function pointers and copy relocation. diff --git a/ld/config.in b/ld/config.in index 26d55a00d47..d5b2a1f948d 100644 --- a/ld/config.in +++ b/ld/config.in @@ -7,6 +7,10 @@ #endif #define __CONFIG_H__ 1 +/* Define to 1 if you want to emit DT_DEBUGSZ dynamic tag in the ELF linker by + default. */ +#undef DEFAULT_EMIT_DT_DEBUGSZ + /* Define to 1 if you want to emit gnu hash in the ELF linker by default. */ #undef DEFAULT_EMIT_GNU_HASH diff --git a/ld/configure b/ld/configure index 33b7de33d1d..d4440f97857 100755 --- a/ld/configure +++ b/ld/configure @@ -838,6 +838,7 @@ enable_textrel_check enable_separate_code enable_error_handling_script enable_default_hash_style +enable_dt_debugsz enable_initfini_array enable_libctf enable_werror @@ -1513,6 +1514,7 @@ Optional Features: --error-handling-script option --enable-default-hash-style={sysv,gnu,both} use this default hash style + --disable-dt-debugsz do not emit DT_DEBUGSZ dynamic tag --disable-initfini-array do not use .init_array/.fini_array sections --enable-libctf Handle .ctf type-info sections [default=yes] --enable-werror treat compile warnings as errors @@ -11412,7 +11414,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 11415 "configure" +#line 11417 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -11518,7 +11520,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 11521 "configure" +#line 11523 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -15363,6 +15365,27 @@ case "${enable_default_hash_style}" in *) ac_default_emit_gnu_hash=0 ;; esac +# Check whether --enable-dt-debugsz was given. +if test "${enable_dt_debugsz+set}" = set; then : + enableval=$enable_dt_debugsz; case "${enableval}" in + yes|no) ;; + *) as_fn_error $? "invalid --disable-dt-debugsz argument" "$LINENO" 5 ;; + esac +else + emit_dt_debugsz=yes +fi + +if test $emit_dt_debugsz = yes; then + ac_default_emit_dt_debugsz=1 +else + ac_default_emit_dt_debugsz=0 +fi + +cat >>confdefs.h <<_ACEOF +#define DEFAULT_EMIT_DT_DEBUGSZ $ac_default_emit_dt_debugsz +_ACEOF + + # Check whether --enable-initfini-array was given. if test "${enable_initfini_array+set}" = set; then : enableval=$enable_initfini_array; case "${enableval}" in diff --git a/ld/configure.ac b/ld/configure.ac index 8ea97c43cd4..09ee28474d5 100644 --- a/ld/configure.ac +++ b/ld/configure.ac @@ -243,6 +243,22 @@ case "${enable_default_hash_style}" in *) ac_default_emit_gnu_hash=0 ;; esac +AC_ARG_ENABLE(dt-debugsz, +AS_HELP_STRING([--disable-dt-debugsz], + [do not emit DT_DEBUGSZ dynamic tag]), +[case "${enableval}" in + yes|no) ;; + *) AC_MSG_ERROR([invalid --disable-dt-debugsz argument]) ;; + esac], [emit_dt_debugsz=yes]) +if test $emit_dt_debugsz = yes; then + ac_default_emit_dt_debugsz=1 +else + ac_default_emit_dt_debugsz=0 +fi +AC_DEFINE_UNQUOTED([DEFAULT_EMIT_DT_DEBUGSZ], + [$ac_default_emit_dt_debugsz], + [Define to 1 if you want to emit DT_DEBUGSZ dynamic tag in the ELF linker by default.]) + AC_ARG_ENABLE(initfini-array, [ --disable-initfini-array do not use .init_array/.fini_array sections], [case "${enableval}" in diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index bfaf8130a3e..2e5ef68dc13 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -824,6 +824,10 @@ fragment <