Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2988049rwr; Fri, 28 Apr 2023 21:25:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7f39KzlWXk+o4LSPhnYAERJf2k7KGcHuy7OciM7mAxuCH5rDX2aFMDgTV4rLktMZvAaMzH X-Received: by 2002:a05:6a00:288e:b0:641:3e9f:289f with SMTP id ch14-20020a056a00288e00b006413e9f289fmr1729929pfb.0.1682742310930; Fri, 28 Apr 2023 21:25:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682742310; cv=none; d=google.com; s=arc-20160816; b=fHl1rEhOGwI+NM4gT0YbqhNeoIRkvXYviZL2ye14+A54Vp4DKoy0FS+KJZj/0BmveB todeN/ggsj3plQwxKwdVG+TUJPpyfEi2S0bW+hldgfVSSrJAayiLd07UdUcS8vfaNPHA pDUFeg0xlj3Fog76Vn3z1+4S7PF8g51AhcBHkEdaLQcjeWsYIEu0Kkv6abIZlvyKPjs+ 5/BBaTzDA+fAnvpm2bRNkN4/CaEVp7Gg9a0hV4BsEIcdyhOJeTi0bU0N5iQEajoKiUcS JP0xgO2GdRcTrcqhZzAd84q146lCvyJ9bYbn7cpf5lTpcGyIuIJ2Z2aIEk/290QIcEvd gmaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=lnEurvOfGspK41cgTX4H/s9q2D0I4/QDypU3jlLuFbo=; b=uzrAwPv2XbSBj/bz0yskWe7akWq1hQtQGJR/7dgcaRBhzlEPgYUOfzadVwgIsIGCrf W8bii2zWUkVcG7OfRpopvH9nyfXx0ePnXW63+LRq+C35qAXbjL75NATdGP1o++PDgijy 7Rb/g/edmSrXdXpD/erkKhtz61/ALiWnBeYtKPMJWoAoUKcry1zOBLKw+Nfu5EJpoGAT QAzfrHv1YWapDhSGbVMbLplvXmNiKHyA15YSqUq+DhoqbC/oCj0zKPdt30C4UXOJ56xr 1nA+pqsDC9gPv2BeSbPDdS0P6/w87+ZPdhrINnR8k42yzHayonvJVR9+yik+yMoBShmX eBVw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t6-20020a625f06000000b006251fb701a6si22884169pfb.285.2023.04.28.21.24.56; Fri, 28 Apr 2023 21:25:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229952AbjD2EEd (ORCPT + 99 others); Sat, 29 Apr 2023 00:04:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjD2EE3 (ORCPT ); Sat, 29 Apr 2023 00:04:29 -0400 Received: from out0-208.mail.aliyun.com (out0-208.mail.aliyun.com [140.205.0.208]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7286F2719; Fri, 28 Apr 2023 21:04:27 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R591e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047202;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=20;SR=0;TI=SMTPD_---.STnJpK0_1682741061; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.STnJpK0_1682741061) by smtp.aliyun-inc.com; Sat, 29 Apr 2023 12:04:22 +0800 Date: Sat, 29 Apr 2023 12:04:21 +0800 From: "Hou Wenlong" To: Peter Zijlstra Cc: "Christophe Leroy" , "linux-kernel@vger.kernel.org" , "Thomas Garnier" , "Lai Jiangshan" , "Kees Cook" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , "x86@kernel.org" , "H. Peter Anvin" , "Masahiro Yamada" , "Nathan Chancellor" , "Nick Desaulniers" , "Nicolas Schier" , "Josh Poimboeuf" , "Sathvika Vasireddy" , "Thomas =?utf-8?B?V2Vp77+9c2NodWg=?=" , "linux-kbuild@vger.kernel.org" Subject: Re: [PATCH RFC 33/43] objtool: Add validation for x86 PIE support Message-ID: <20230429040421.GB126816@k08j02272.eu95sqa> References: <226af8c63c5bfa361763dd041a997ee84fe926cf.1682673543.git.houwenlong.hwl@antgroup.com> <461b3a8d-9ad4-7866-f3b2-369de75fd2e1@csgroup.eu> <20230428114338.GB1449475@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230428114338.GB1449475@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.5.21 (2010-09-15) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 28, 2023 at 07:43:38PM +0800, Peter Zijlstra wrote: > On Fri, Apr 28, 2023 at 10:28:19AM +0000, Christophe Leroy wrote: > > > > > diff --git a/tools/objtool/check.c b/tools/objtool/check.c > > > index 5b600bbf2389..d67b80251eec 100644 > > > --- a/tools/objtool/check.c > > > +++ b/tools/objtool/check.c > > > @@ -131,6 +131,27 @@ static struct instruction *prev_insn_same_sym(struct objtool_file *file, > > > for (insn = next_insn_same_sec(file, insn); insn; \ > > > insn = next_insn_same_sec(file, insn)) > > > > > > +static struct instruction *find_insn_containing(struct objtool_file *file, > > > + struct section *sec, > > > + unsigned long offset) > > > +{ > > > + struct instruction *insn; > > > + > > > + insn = find_insn(file, sec, 0); > > > + if (!insn) > > > + return NULL; > > > + > > > + sec_for_each_insn_from(file, insn) { > > > + if (insn->offset > offset) > > > + return NULL; > > > + if (insn->offset <= offset && (insn->offset + insn->len) > offset) > > > + return insn; > > > + } > > > + > > > + return NULL; > > > +} > > Urgh, this is horrendous crap. Yes you're only using it in case of a > warning, but adding a function like this makes it appear like it's > actually sane to use. > > A far better implementation -- but still not stellar -- would be > something like: > > sym = find_symbol_containing(sec, offset); > if (!sym) > // fail > sym_for_each_insn(file, sym, insn) { > ... > } > > But given insn_hash uses sec_offset_hash() you can do something similar > to find_reloc_by_dest_range() > > start = offset - (INSN_MAX_SIZE - 1); > for_offset_range(o, start, start + INSN_MAX_SIZE) { > hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, o)) { > if (insn->sec != sec) > continue; > > if (insn->offset <= offset && > insn->offset + inns->len > offset) > return insn; > } > } > return NULL; > Thanks for your suggestion, I'll pick it in the next version. > > > + > > > + > > > static inline struct symbol *insn_call_dest(struct instruction *insn) > > > { > > > if (insn->type == INSN_JUMP_DYNAMIC || > > > @@ -4529,6 +4550,61 @@ static int validate_reachable_instructions(struct objtool_file *file) > > > return 0; > > > } > > > > > > +static int is_in_pvh_code(struct instruction *insn) > > > +{ > > > + struct symbol *sym = insn->sym; > > > + > > > + return sym && !strcmp(sym->name, "pvh_start_xen"); > > > +} > > > + > > > +static int validate_pie(struct objtool_file *file) > > > +{ > > > + struct section *sec; > > > + struct reloc *reloc; > > > + struct instruction *insn; > > > + int warnings = 0; > > > + > > > + for_each_sec(file, sec) { > > > + if (!sec->reloc) > > > + continue; > > > + if (!(sec->sh.sh_flags & SHF_ALLOC)) > > > + continue; > > > + > > > + list_for_each_entry(reloc, &sec->reloc->reloc_list, list) { > > > + switch (reloc->type) { > > > + case R_X86_64_NONE: > > > + case R_X86_64_PC32: > > > + case R_X86_64_PLT32: > > > + case R_X86_64_64: > > > + case R_X86_64_PC64: > > > + case R_X86_64_GOTPCREL: > > > + break; > > > + case R_X86_64_32: > > > + case R_X86_64_32S: > > > > That looks very specific to X86, should it go at another place ? > > > > If it can work for any architecture, can you add generic macros, just > > like commit c1449735211d ("objtool: Use macros to define arch specific > > reloc types") then commit c984aef8c832 ("objtool/powerpc: Add --mcount > > specific implementation") ? > > Yes, this should be something like arch_PIE_reloc() or so. Similar to > arch_pc_relative_reloc().