From patchwork Sat Feb 5 17:24:41 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: 1588811 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=xDIr/Nq+; 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 4JrfRn5fmHz9s07 for ; Sun, 6 Feb 2022 04:25:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 08541385842C for ; Sat, 5 Feb 2022 17:25:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 08541385842C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1644081903; bh=1fjRhNEnzjQsUZyUMjOWpwJc4iw9bZRKwEZ1NpN4CL0=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=xDIr/Nq+ic2m3shlkoBAgDRNPBZa/XNQdXoqQSEnj3U4I12vLV+wYbMwRloX3TI8S fMqnlbWndUAHgkFhfBuLfuf9V70Yqnlm8J7cP38FVMCVF9FKe0tXvRBj4Prcz8Hti7 9GMh8R02+EZAhMk4oAb8rNpJ24zF/h+ok9ayGxoQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id F19D93858418 for ; Sat, 5 Feb 2022 17:24:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F19D93858418 Received: by mail-pf1-x432.google.com with SMTP id i17so7887453pfq.13 for ; Sat, 05 Feb 2022 09:24:48 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1fjRhNEnzjQsUZyUMjOWpwJc4iw9bZRKwEZ1NpN4CL0=; b=t14VEDI3OqXG8/UUsPJtl9NCsWxlNDyoGFF15ExIWa6e3JkNoIoWCrIFG7FG62HztJ C7z6R0ZK0fBVVC5+XQEQ1L3j4cL6sGFa8idVkR6HSqnqfKcDVUk/+NJqVnVnrhUIdGXP TPPewAP7gwqqsaVyyusH+f8NDgsJV9N5lNe+Zk1CRPsMjdHphbxR07o2LtGKNwxPfygc Q+O5muiU3V5+KrgUgpJMPQ6Rkpk/4iqf3BL8CATp8aIUlJK1JsaUb4YWkB/f7OZiXMrF v87ZKBpBNvk5CE3NFExdjURU65PMVEj+ka4OZqKM2azoOPrUnyLPTA1Q1moE5/XqXQea vK/w== X-Gm-Message-State: AOAM533qu4I0AknQlFJE5L5MNQi33gu9pXYpQ7iYjjk//L4Stt42rp90 d00u51zejwM0GQmKznw+SagnK30hnm8= X-Google-Smtp-Source: ABdhPJxBAWVQKvz1T5ihfyr6nZu8pPBNg6pubCrJKiZYiPqX2YjYR2SojQsdPBw0CHsp6gy+DJbpdQ== X-Received: by 2002:a65:60c3:: with SMTP id r3mr3576445pgv.347.1644081887919; Sat, 05 Feb 2022 09:24:47 -0800 (PST) Received: from gnu-tgl-2.localdomain ([172.58.38.240]) by smtp.gmail.com with ESMTPSA id p64sm4223245pga.13.2022.02.05.09.24.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Feb 2022 09:24:47 -0800 (PST) Received: from gnu-tgl-2.. (localhost [IPv6:::1]) by gnu-tgl-2.localdomain (Postfix) with ESMTP id 7855A3004A8; Sat, 5 Feb 2022 09:24:46 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/7] elf: Properly handle zero DT_RELA/DT_REL values Date: Sat, 5 Feb 2022 09:24:41 -0800 Message-Id: <20220205172446.652011-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220205172446.652011-1-hjl.tools@gmail.com> References: <20220205172446.652011-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3029.9 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, 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: Joseph Myers Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" With DT_RELR, there may be no relocations in DT_RELA/DT_REL and their entry values are zero. Don't relocate DT_RELA/DT_REL and update the combined relocation start address if their entry values are zero. --- elf/dynamic-link.h | 6 +++++- elf/get-dynamic-info.h | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h index 5318079bac..320a9029f2 100644 --- a/elf/dynamic-link.h +++ b/elf/dynamic-link.h @@ -84,7 +84,9 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; } \ ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; \ \ - if ((map)->l_info[DT_##RELOC]) \ + /* With DT_RELR, DT_RELA/DT_REL can have zero value. */ \ + if ((map)->l_info[DT_##RELOC] \ + && (map)->l_info[DT_##RELOC]->d_un.d_ptr != 0) \ { \ ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \ ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \ @@ -98,6 +100,8 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \ ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \ \ + if (ranges[0].start == 0) \ + ranges[0].start = start; \ if (ranges[0].start + ranges[0].size == (start + size)) \ ranges[0].size -= size; \ if (!(do_lazy) \ diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h index 6c2a3a12b1..f4b957684b 100644 --- a/elf/get-dynamic-info.h +++ b/elf/get-dynamic-info.h @@ -83,16 +83,26 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap, ADJUST_DYN_INFO (DT_PLTGOT); ADJUST_DYN_INFO (DT_STRTAB); ADJUST_DYN_INFO (DT_SYMTAB); + ADJUST_DYN_INFO (DT_RELR); + ADJUST_DYN_INFO (DT_JMPREL); + ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM)); + ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH)); +# undef ADJUST_DYN_INFO + + /* DT_RELA/DT_REL are mandatory. But they may have zero value if + there is DT_RELR. Don't relocate them if they are zero. */ +# define ADJUST_DYN_INFO(tag) \ + do \ + if (info[tag] != NULL && info[tag]->d_un.d_ptr != 0) \ + info[tag]->d_un.d_ptr += l_addr; \ + while (0) + # if ! ELF_MACHINE_NO_RELA ADJUST_DYN_INFO (DT_RELA); # endif # if ! ELF_MACHINE_NO_REL ADJUST_DYN_INFO (DT_REL); # endif - ADJUST_DYN_INFO (DT_RELR); - ADJUST_DYN_INFO (DT_JMPREL); - ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM)); - ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH)); # undef ADJUST_DYN_INFO } if (info[DT_PLTREL] != NULL)