From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 A58C7385624F for ; Fri, 22 Apr 2022 22:39:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A58C7385624F Received: by mail-pf1-x432.google.com with SMTP id j17so9252300pfi.9 for ; Fri, 22 Apr 2022 15:39:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=J6dPita17JBHi8nzFqwWGDnUse8ak3f5uVzNi7cKHiw=; b=FIhxj4j+S6jYq0zNFq3ukXnBJUuMU+pn44/KTMiornMtZ5sV9v77dcQGlnpP5ukpkQ CDQfQUUDjAKNjbse2EjVyyYzy8UU4NgI5L+AUF2RrCILsv7p21HNFqAEzKLm3S9aKz42 sFCEjI1+Hi120vdSDyKJfs+W8DAGHccLfk3Kzg1CBdLPBRwVIIB+/DUx0MLM/trobik8 Ntk5ZdcJdOyKAVQsgevwr/HiY1yFvSTZBoK1IV6cxh19q3xZGX7FihqoNTH5CLE7Al6L bhC39SaMxSm33bJbr9dtLe6ZkRedwHkM03DF+TRtMECHDWO1GmjsT4P/Lq72IWywcyBj T2dg== X-Gm-Message-State: AOAM530bY33X5EJ9spCvQdakaed78cy4UXViNgMvm+zu3VZNH39IeJCh ufs863jLzh2AudEAXoWArbCvFQ== X-Google-Smtp-Source: ABdhPJzuvZn7UCJFgLoHHz56Y/VRcsBFFBvkcuoE+5cMJIp6c8ORN+AQROhD7qBA392tvJlqz/Szkw== X-Received: by 2002:a62:685:0:b0:50a:5870:10b1 with SMTP id 127-20020a620685000000b0050a587010b1mr7007189pfg.61.1650667159412; Fri, 22 Apr 2022 15:39:19 -0700 (PDT) Received: from google.com ([2620:15c:2ce:200:8f89:e96b:b8b8:a84f]) by smtp.gmail.com with ESMTPSA id bd11-20020a656e0b000000b0039da213aa72sm3019378pgb.5.2022.04.22.15.39.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 15:39:18 -0700 (PDT) Date: Fri, 22 Apr 2022 15:39:15 -0700 From: Fangrui Song To: "H.J. Lu" Cc: libc-alpha@sourceware.org, Adhemerval Zanella Subject: Re: [PATCH v11 4/7] elf: Properly handle zero DT_RELA/DT_REL values Message-ID: <20220422223915.6xzoovbwxr7tdcyv@google.com> References: <20220422190139.2615492-1-hjl.tools@gmail.com> <20220422190139.2615492-5-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20220422190139.2615492-5-hjl.tools@gmail.com> X-Spam-Status: No, score=-27.3 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL 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-List-Received-Date: Fri, 22 Apr 2022 22:39:22 -0000 On 2022-04-22, H.J. Lu wrote: >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 | 24 ++++++++++++++++++------ > 2 files changed, 23 insertions(+), 7 deletions(-) > >diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h >index 019088f248..debbb8c9f7 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) >-- >2.35.1 > LG, Thanks Reviewed-by: Fangrui Song