From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id 5C6173858C74 for ; Tue, 1 Mar 2022 19:16:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5C6173858C74 Received: by mail-pl1-x631.google.com with SMTP id bd1so14220840plb.13 for ; Tue, 01 Mar 2022 11:16:00 -0800 (PST) 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=2ViB2QHJH5z2bO9lITxTrVb9quoH2ba9gWogeDmlVY8=; b=xYgV3IWAIn+aIld4BU6vmPNhu0XeHnPr8TwJgO68K2dvi53kQO6f1m8CcSkG6UAFAM Cc9ztameIK9eOLb+8R2zZWXGQDSkMM2VCV8IcTEgtyqtjjc8m+HrT/n2vQos6Bcddk5d XGMV6QrR6/8Lzs9z3NrQQd7lkeiwxFlvVaxD6L6JfVNvq6q2N/xwQ2gyeGXYkoCyrpyc Lzps5iUJnsi4KA2/oJkLcJQtoPJKRo4Wur0Q/5NlJVetuqZ24pWu0hhyKNPajV7ZUg8m MrN77LQ774eSnN0LCqWu4NA1C+ChxVVv5jJz5w9yZjNr32pt3ycCXRkt44IbCqz7+8nm anLw== X-Gm-Message-State: AOAM533w0Cs3WRaL7Ra1MfbcrOVau4F+VedoO8lMKgIOrmQgWRhx/KMT nUI0JxRUGDPXtSzbf3G4h/Lfqg== X-Google-Smtp-Source: ABdhPJyerQYe+xp++qAQITwkT+OclU7Od2TJ4Dgw55qFS436Uzyzv84m+JmOVUBWz+POEF8SNHhQZQ== X-Received: by 2002:a17:902:d705:b0:14e:e5a2:1b34 with SMTP id w5-20020a170902d70500b0014ee5a21b34mr26468371ply.88.1646162159372; Tue, 01 Mar 2022 11:15:59 -0800 (PST) Received: from google.com ([2620:15c:2ce:200:88f6:4b70:bdc0:5a68]) by smtp.gmail.com with ESMTPSA id a19-20020a17090ad81300b001bc447c2c91sm2692600pjv.31.2022.03.01.11.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 11:15:58 -0800 (PST) Date: Tue, 1 Mar 2022 11:15:55 -0800 From: Fangrui Song To: "H.J. Lu" Cc: libc-alpha@sourceware.org, Joseph Myers Subject: Re: [PATCH v4 2/5] elf: Properly handle zero DT_RELA/DT_REL values Message-ID: <20220301191555.fbr6agyoe6glx5r2@google.com> References: <20220301161706.185216-1-hjl.tools@gmail.com> <20220301161706.185216-3-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20220301161706.185216-3-hjl.tools@gmail.com> X-Spam-Status: No, score=-17.4 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, T_SCC_BODY_TEXT_LINE, UNWANTED_LANGUAGE_BODY, 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: Tue, 01 Mar 2022 19:16:02 -0000 On 2022-03-01, 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 | 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) >-- >2.35.1 > Reviewed-by: Fangrui Song