From patchwork Thu Mar 10 20:03:26 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: 51871 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 50D8C3858408 for ; Thu, 10 Mar 2022 20:06:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50D8C3858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1646942763; bh=U6Aqxvuls9YPKRb+I0VRloQ8xM/8WraBulyeO3YgTzQ=; 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=eXHdm3JrzTGPVpt7dKah0I+se3u4qyRuxUcXA816Y1+z8yCOyUJ3IbOHNkvZWEwsl P7gDsNKrfIcqjJ4JV1K9J/QC3Ziyj2rJP7NuO4tQL4cIeoGRBmFUdI6U1HR1zCC9uC PfAm0AmeGHABg1Yj9JV9/DquESCCec39hVYFEzng= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id 949B83858404 for ; Thu, 10 Mar 2022 20:03:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 949B83858404 Received: by mail-pg1-x52e.google.com with SMTP id o8so5613247pgf.9 for ; Thu, 10 Mar 2022 12:03:36 -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=U6Aqxvuls9YPKRb+I0VRloQ8xM/8WraBulyeO3YgTzQ=; b=PVNvoxVMCdnrTZZ8xrdeoVvmcvyBdJbbWqEReeSFrcG7b6Ra6UfwoAMpIe2ukgfryg k1LGX7g0jkHcOcPoPbw1aF/O7Wjz4hpzlpsoiZYA0v8xrfRK2jeUGajbo1CeCgYmWHgE 13mhV+y1foigSt77SYXfRPlDi/HGW0sJdwvKypBIIZbT+dBhUkQ8iWLN3sBFYjF0r7lT G5fZa6qpqtQbL72YKWMMNCe8EONpl1mdwNDlldgRhjB4I7KDZ87z86yAq3ymo/lYbtii azYU0zXgorXL1tlsfbpbiJIuNndhgGf34UMfmTlXYrRAUqIktumS2Fw63YUToR/xciss 8d3A== X-Gm-Message-State: AOAM532yZFd6B/n5zhOThZHZmvi2jziedJPipRAWZ9Z4vutcXEq/1XST q7pEgiY2uJXBM5kRN2sTYqo+5ed7+zo= X-Google-Smtp-Source: ABdhPJxdtv47ZHqWDtaKwq4U845LaldS3XJ0+Olris8uKPG6iA4LT8c8jTwNI9/Ig3TUh4ve/+VY7Q== X-Received: by 2002:a05:6a00:2186:b0:4f7:5544:1cc9 with SMTP id h6-20020a056a00218600b004f755441cc9mr6655420pfi.62.1646942615589; Thu, 10 Mar 2022 12:03:35 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.38.221]) by smtp.gmail.com with ESMTPSA id h2-20020a656382000000b00370648d902csm6122447pgv.4.2022.03.10.12.03.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Mar 2022 12:03:34 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id A2397C03D3; Thu, 10 Mar 2022 12:03:33 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v6 2/5] elf: Properly handle zero DT_RELA/DT_REL values Date: Thu, 10 Mar 2022 12:03:26 -0800 Message-Id: <20220310200329.1935466-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220310200329.1935466-1-hjl.tools@gmail.com> References: <20220310200329.1935466-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3028.6 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+patchwork=sourceware.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 | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h index d04c457e55..252f407a12 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)