From patchwork Thu Mar 31 16:38:56 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: 1611856 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=C6IkMECm; 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 4KTpwW5v7Nz9sBy for ; Fri, 1 Apr 2022 03:41:31 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B9D7A3838016 for ; Thu, 31 Mar 2022 16:41:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B9D7A3838016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1648744889; bh=4kk/HBoH5BxW88KHGFUlu6xpdqp8RPr0uS0byFxNhjc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=C6IkMECmkzIUdlvUbMcKCMPbpjXGfbMafF0EGl2m3KPsyWfjxyqSANIl3ebSqr5lt ShZkk3p22e9ExbsGe8RfzIKL+mFvtOON23KwEhl5z2SBKrzq0QqKl2v5KcqWBCRIij 1XcuoI+SAjDKRAOvxKAolIAjDAXcusZWgIjCa0uc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 8E28A3838020 for ; Thu, 31 Mar 2022 16:39:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8E28A3838020 Received: by mail-pj1-x1033.google.com with SMTP id o68-20020a17090a0a4a00b001c686a48263so3256215pjo.1 for ; Thu, 31 Mar 2022 09:39:10 -0700 (PDT) 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=4kk/HBoH5BxW88KHGFUlu6xpdqp8RPr0uS0byFxNhjc=; b=PqAGKARCPc8tKjsoVdi0q63trcTuRNCXR3ujoK66w3Ka1EgksPhXh0W2wUGGN6lP5E 0q1bHsirDSXvURxHLqmLD6qiQlWGGKzRXeOVjLyIMhWGRw88sE4bNkTDfGW2hQop/fj6 7CJYGkRP5VxLGk/wSt8HdRSJeSB/4J5AZ5aHR3QkUaDC12A6XOJQqeBTMk6J3mMvrmVo oiqYg1kBfmGlcwWkrnhGqAFoZmrUjWH4CQZY6lN3zXn+SoCs8NbdB1oIUYZtEr8rRNbx oQs+dEqmwqusKsT65qBLxReyIwFpyfQEH7HSdqx4dk48XXXcOlHdQ9PlsQX9kFJ4RlA0 dbeA== X-Gm-Message-State: AOAM530x//dUKmp+lNxG/Qy94w/0wyKR8mVc17utwt40O0qtH1SAGXWG GiP/ftFvcQ2RPgnh9MG/WH4= X-Google-Smtp-Source: ABdhPJxl8aTMnCH0CehvLneRT9W5GQId8dy1gA891GfmmtOrCZrl9G/EVx0zBUxx2o9BJsWTAM61Qg== X-Received: by 2002:a17:90b:1c07:b0:1c7:5324:c68e with SMTP id oc7-20020a17090b1c0700b001c75324c68emr7022767pjb.202.1648744749312; Thu, 31 Mar 2022 09:39:09 -0700 (PDT) Received: from gnu-tgl-3.localdomain (2603-800c-3343-7600-3dea-466c-2e3a-bd65.res6.spectrum.com. [2603:800c:3343:7600:3dea:466c:2e3a:bd65]) by smtp.gmail.com with ESMTPSA id 124-20020a621682000000b004f6a2e59a4dsm27355371pfw.121.2022.03.31.09.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Mar 2022 09:39:06 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 0776DC05B1; Thu, 31 Mar 2022 09:39:05 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v7 4/6] elf: Properly handle zero DT_RELA/DT_REL values Date: Thu, 31 Mar 2022 09:38:56 -0700 Message-Id: <20220331163858.95516-5-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220331163858.95516-1-hjl.tools@gmail.com> References: <20220331163858.95516-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3028.7 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" 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. Reviewed-by: Adhemerval Zanella --- elf/dynamic-link.h | 6 +++++- elf/get-dynamic-info.h | 24 ++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h index d04c457e55..53b698c1af 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] != NULL \ + && (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..0314baf020 100644 --- a/elf/get-dynamic-info.h +++ b/elf/get-dynamic-info.h @@ -75,24 +75,36 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap, # define ADJUST_DYN_INFO(tag) \ do \ - if (info[tag] != NULL) \ - info[tag]->d_un.d_ptr += l_addr; \ + { \ + if (info[tag] != NULL) \ + info[tag]->d_un.d_ptr += l_addr; \ + } \ while (0) ADJUST_DYN_INFO (DT_HASH); 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)