From patchwork Thu Mar 3 04:50:23 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: 51519 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 81AB23858018 for ; Thu, 3 Mar 2022 04:52:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81AB23858018 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1646283137; bh=eQigEyF2jwsfJ/pmlx7K+oz8FwLqbF8N6k1i78Hk4ko=; 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=kvLGRt/gHhgJTVttm0eoX8ymzvg82VBrSjQagWrwIIzietEezg4ivS7UpQnFRizN1 HMY7Atgplb6kkR7A1jCdyB2jeiJfxjFP+ta03Ai+YGNVvJuhzKrPwRNeCybJjsuGad anqOTyg9aa+tXnlYox29Nvf5JYnA/y2ZFGQwfLAs= 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 F33973858416 for ; Thu, 3 Mar 2022 04:50:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F33973858416 Received: by mail-pj1-x1033.google.com with SMTP id c16-20020a17090aa61000b001befad2bfaaso3331187pjq.1 for ; Wed, 02 Mar 2022 20:50:34 -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=eQigEyF2jwsfJ/pmlx7K+oz8FwLqbF8N6k1i78Hk4ko=; b=14tMxGl+V58IM/Lb1HgrwkU1juzJ0udkLWzz+24OnO6Cb6ecUP4WniUrzj6SBz3nTx izFARj0PKB9z7qN9YU4aQzrbZuaBLkDHaezw6mC7L6V4I8lpmYzQe3zxzH9cPqBxfgqE JupCLqDndzxNz+fJZHAb6VtztO2fnW8xCvWpPv/aeWM/Ui25wJVoUdF/wgpS4uHiQc1o qqxE7op0g3Xv2sGds5PgMQY/7PY+rOaa0jcXEMtmZO3/kVZXxCdGz9vOR4gqVqH5hWxg Mtd6a5CoS+m+Fbu06jjNsFvgupP8EirXhH+DNt+4Ka1kUudiSYAUimJe2X8DY3nGphjI 7B2A== X-Gm-Message-State: AOAM532JzjBT0DeoiLJsk/Csilfl4GXGh7hO3QoXB4Ks+rsGVcd2OWuV fz5Dfp2sudTTefFlQEQq/Jk= X-Google-Smtp-Source: ABdhPJyzZ0mYNsdVV+ENyMJdMN0sIRK6y406L061eqUmqjVm+LNEW1NS0D3dm2sNEgwrXAlvZxA63A== X-Received: by 2002:a17:902:9a02:b0:14f:2d93:92f4 with SMTP id v2-20020a1709029a0200b0014f2d9392f4mr35801094plp.160.1646283034046; Wed, 02 Mar 2022 20:50:34 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.88]) by smtp.gmail.com with ESMTPSA id om17-20020a17090b3a9100b001bf0fffee9bsm308003pjb.52.2022.03.02.20.50.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 20:50:33 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 8BBB1C03D4; Wed, 2 Mar 2022 20:50:31 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v5 2/5] elf: Properly handle zero DT_RELA/DT_REL values Date: Wed, 2 Mar 2022 20:50:23 -0800 Message-Id: <20220303045026.4172347-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220303045026.4172347-1-hjl.tools@gmail.com> References: <20220303045026.4172347-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3029.0 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. --- 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)