From patchwork Thu Apr 14 19:04:04 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: 1617412 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=bzMK5r1b; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 4KfTTR0TkZz9sCq for ; Fri, 15 Apr 2022 05:06:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 333D9385780C for ; Thu, 14 Apr 2022 19:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 333D9385780C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1649963193; bh=xKO1p5wpII66g6kHJcRj1E3GE5aEnTBcAXwSFA2fYIg=; 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=bzMK5r1bbVedh3wX+AHrzlhmth0LhL8A/lNb2H2RZMyiqP0loxnUEmq/ppwQnxTP9 SatnKLRKjZiyQ01SD346VwONQX+E4RKF/i29ea6Edw2YipQyCl6Z4UxAasWu7MJ2gg vRXQC5Tbm66CsGyPRV7aTWkFNtHzXav/T8L2DayU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 82A183858430 for ; Thu, 14 Apr 2022 19:04:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 82A183858430 Received: by mail-pf1-x430.google.com with SMTP id n43so1646759pfv.10 for ; Thu, 14 Apr 2022 12:04:14 -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=xKO1p5wpII66g6kHJcRj1E3GE5aEnTBcAXwSFA2fYIg=; b=5jEUfqovVPdW0H6hBcL1G8wmYI7K3tZcPzI8XP6JECUXWlxDiN/4jfOo25yI+f1WUv CqtCJtdttRjsYKMOn5pI92hsr36LvoKwYn9YKNRTG3/8sgxl9AQBFCLj5r2o7wh7yYMQ +4R5OFus7mh2ERJ8PPgeIA2DxUEI0px1xZJQFb0KQBGKtsYQvdTVe2KFfF5e7RcIGLz6 JvQ4/hfuyRyUo231cSaQW+sg3tT9drvYyRHWgvtvAk1pMfQ2cg5LgCeCvfrqrYy4kvog ogspOBYMU5R8VxwQ+2dI7vOZp8poypJHxLqTZCNoouddbgelGAMSeDATOpDoylJaEKSJ 5Yww== X-Gm-Message-State: AOAM531pjBmN7ntwqIl6koKfvQ4t5LpMaM2woHaSWcyDBSYHbVTf+dkf zD9aT73cfIhazSKCNhyXZ4U= X-Google-Smtp-Source: ABdhPJwhct8xp6XGFMoU7FT2Cw1DAW9HvqEZvmuUfDFnIMqC3rM+p5/1RCr9/MURzPfa8PKKXaonMA== X-Received: by 2002:a05:6a00:1988:b0:4fa:c15d:190d with SMTP id d8-20020a056a00198800b004fac15d190dmr5268725pfl.44.1649963053608; Thu, 14 Apr 2022 12:04:13 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.36.153]) by smtp.gmail.com with ESMTPSA id t184-20020a625fc1000000b004fa3bd9bef0sm593324pfb.110.2022.04.14.12.04.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 12:04:12 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 7AF6DC02F6; Thu, 14 Apr 2022 12:04:11 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v9 4/7] elf: Properly handle zero DT_RELA/DT_REL values Date: Thu, 14 Apr 2022 12:04:04 -0700 Message-Id: <20220414190407.3210003-5-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220414190407.3210003-1-hjl.tools@gmail.com> References: <20220414190407.3210003-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" 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. --- 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 7325e0d9fc..7dd8f1b1d6 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)