From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58B0EC433E6 for ; Wed, 3 Feb 2021 08:13:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 05C2D64F67 for ; Wed, 3 Feb 2021 08:13:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232651AbhBCIMk (ORCPT ); Wed, 3 Feb 2021 03:12:40 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:33709 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232611AbhBCIMf (ORCPT ); Wed, 3 Feb 2021 03:12:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612339868; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7Huq6tfU+kDDL1exMPNWAbCb858jufvEInD0eW8A0Yo=; b=NkmcRBUkYOiGyPedvI+AtyBd/g0EFGcmzPWtR5OJEkV+lveICKy0U7hQnvhteKiH1JKJNi 6Ct2buAe1662FRJ4BoQ4DUGlWbf/JaCBgB56rL+CXliKrdg0JjFN4WrlCIF7ETLmLhpZ1o LWCa2wyMLkd+SS1nvk4pexpIedmAc3I= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-570-qHjOwBWQOty3Tne5JEK5xQ-1; Wed, 03 Feb 2021 03:11:06 -0500 X-MC-Unique: qHjOwBWQOty3Tne5JEK5xQ-1 Received: by mail-wm1-f70.google.com with SMTP id q24so2701768wmc.1 for ; Wed, 03 Feb 2021 00:11:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=7Huq6tfU+kDDL1exMPNWAbCb858jufvEInD0eW8A0Yo=; b=WXTY0ISiODuB7bJ4zpSnGUFTGN2QFhn++2jr9xdaTLhExCrLEvIDd8XJki+2PT5NwY O4VOXRqnd7qmo+0DPqX7opttHx0GIFM/fU0ybkFQns1HuP/SzXKAILs3RsBq32/ujl9w VIGRX+qIfivAE5oP/1+ugxhCWTWcchOqBxtDvO+QfHuuSGYD5flrC49e/SyOCQ8Pucqe DwLVbfB2nX1hmXv+wpB2A6c6cmAl4XSczpbX2rWbcEXFylVFK3D5zQOXJq7XLdEOT0vY xc6e9RUgulFB9erJvSsZ3VHuGR1XbLzwq4EZiwHEedoZpBD3rJi4eCwPkXIxsM9VDYUy ZMjQ== X-Gm-Message-State: AOAM5317BvD0NkOLfK4MzQWeLU9DFUYPo8P7iotcdERp5gFhddSzk5Ub l+zEFsHtcXH1JG8/PGV68AAdcrIURyEYw8ZeXYYUQ98P19gXcKKDIM2ogNS16iFDsr1t2GaaTH2 H5GFnhtqrqB1Its46jsaHzLUM X-Received: by 2002:a1c:dcd7:: with SMTP id t206mr1572572wmg.108.1612339865391; Wed, 03 Feb 2021 00:11:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyor899BoErX+lNV6jwHugO2N82UM+DD4bmFtKm617J/dNiWTcXQvxuZH381L0jowogxpvzow== X-Received: by 2002:a1c:dcd7:: with SMTP id t206mr1572547wmg.108.1612339865171; Wed, 03 Feb 2021 00:11:05 -0800 (PST) Received: from ?IPv6:2a01:cb14:499:3d00:cd47:f651:9d80:157a? ([2a01:cb14:499:3d00:cd47:f651:9d80:157a]) by smtp.gmail.com with ESMTPSA id y63sm1590188wmd.21.2021.02.03.00.11.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Feb 2021 00:11:04 -0800 (PST) Subject: Re: [RFC PATCH 12/17] gcc-plugins: objtool: Add plugin to detect switch table on arm64 To: Nick Desaulniers Cc: Josh Poimboeuf , Ard Biesheuvel , Mark Brown , Catalin Marinas , Kees Cook , Linux ARM , linux-efi , linux-hardening@vger.kernel.org, LKML , Mark Rutland , Masahiro Yamada , Michal Marek , Peter Zijlstra , raphael.gault@arm.com, Will Deacon , clang-built-linux , Bill Wendling , swine@google.com, yonghyun@google.com References: <20210120173800.1660730-13-jthierry@redhat.com> <20210127221557.1119744-1-ndesaulniers@google.com> <20210127232651.rj3mo7c2oqh4ytsr@treble> <20210201214423.dhsma73k7ccscovm@treble> <671f1aa9-975e-1bda-6768-259adbdc24c8@redhat.com> From: Julien Thierry Message-ID: Date: Wed, 3 Feb 2021 09:11:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/3/21 12:01 AM, Nick Desaulniers wrote: > On Tue, Feb 2, 2021 at 12:57 AM Julien Thierry wrote: >> >> >> >> On 2/2/21 12:17 AM, Nick Desaulniers wrote: >>> On Mon, Feb 1, 2021 at 1:44 PM Josh Poimboeuf wrote: >>>> >>>> On Fri, Jan 29, 2021 at 10:10:01AM -0800, Nick Desaulniers wrote: >>>>> On Wed, Jan 27, 2021 at 3:27 PM Josh Poimboeuf wrote: >>>>>> >>>>>> On Wed, Jan 27, 2021 at 02:15:57PM -0800, Nick Desaulniers wrote: >>>>>>>> From: Raphael Gault >>>>>>>> >>>>>>>> This plugins comes into play before the final 2 RTL passes of GCC and >>>>>>>> detects switch-tables that are to be outputed in the ELF and writes >>>>>>>> information in an ".discard.switch_table_info" section which will be >>>>>>>> used by objtool. >>>>>>>> >>>>>>>> Signed-off-by: Raphael Gault >>>>>>>> [J.T.: Change section name to store switch table information, >>>>>>>> Make plugin Kconfig be selected rather than opt-in by user, >>>>>>>> Add a relocation in the switch_table_info that points to >>>>>>>> the jump operation itself] >>>>>>>> Signed-off-by: Julien Thierry >>>>>>> >>>>>>> Rather than tightly couple this feature to a particular toolchain via >>>>>>> plugin, it might be nice to consider what features could be spec'ed out >>>>>>> for toolchains to implement (perhaps via a -f flag). >>>>>> >>>>>> The problem is being able to detect switch statement jump table vectors. >>>>>> >>>>>> For a given indirect branch (due to a switch statement), what are all >>>>>> the corresponding jump targets? >>>>>> >>>>>> We would need the compiler to annotate that information somehow. >>>>> >>>>> Makes sense, the compiler should have this information. How is this >>>>> problem solved on x86? >>>> >>>> Thus far we've been able to successfully reverse engineer it on x86, >>>> though it hasn't been easy. >>>> >>>> There were some particulars for arm64 which made doing so impossible. >>>> (I don't remember the details.) >> >> The main issue is that the tables for arm64 have more indirection than x86. > > I wonder if PAC or BTI also make this slightly more complex? PAC at > least has implications for unwinders, IIUC. > >> >> On x86, the dispatching jump instruction fetches the target address from >> a contiguous array of addresses based on a given offset. So the list of >> potential targets of the jump is neatly organized in a table (and sure, >> before link time these are just relocation, but still processable). >> >> On arm64 (with GCC at least), what is stored in a table is an array of >> candidate offsets from the jump instruction. And because arm64 is >> limited to 32bit instructions, the encoding often requires multiple >> instructions to compute the target address: >> >> ldr<*> x_offset, [x_offsets_table, x_index, ...] // load offset >> adr x_dest_base, // load target branch for offset 0 >> add x_dest, x_target_base, x_offset, ... // compute final address >> br x_dest // jump >> >> Where this gets trickier is that (with GCC) the offsets stored in the >> table might or might not be signed constants (and this can be seen in >> GCC intermediate representations, but I do not believe this information >> is output in the final object file). And on top of that, GCC might >> decide to use offsets that are seen as unsigned during intermediate >> representation as signed offset by sign extending them in the add >> instruction. >> >> So, to handle this we'd have to track the different operation done with >> the offset, from the load to the final jump, decoding the instructions >> and deducing the potential target instructions from the table of offsets. >> >> But that is error prone as we don't really know how many instructions >> can be between the ones doing the address computation, and I remember >> some messy case of a jump table inside a jump table where tracking the >> instruction touching one or the other offset would need a lot of corner >> case handling. >> >> And this of course is just for GCC, I haven't looked at what it all >> looks like on Clang's end. > > Sure, but this is what production unwinders do, and they don't require > compiler plugins, right? I don't doubt unwinders can be made simpler > with changes to toolchain output; please work with your compiler > vendor on making such changes rather than relying on compiler plugins > to do so. > I think there is a small confusion. The plugin nor the data it generates is not to be used by a kernel unwinder. It's here to allow objtool to assess whether the code being checked can be unwound (?) reliably (not omitting functions). Part of this is checking that a branch/jump in a function does not end up in some code that is not related to the function without setting up a call frame. This is about static validation rather than functionality. >>> I think the details are pertinent to finding a portable solution. The >>> commit message of this commit in particular doesn't document such >>> details, such as why such an approach is necessary or how the data is >>> laid out for objtool to consume it. >>> >> >> Sorry, I will need to make that clearer. The next patch explains it a >> bit [1] >> >> Basically, for simplicity, the plugin creates a new section containing > > Right, this takes a focus on simplicity, at the cost of alienating a toolchain. > > Ard's point about 3193c0836f20 relating to -fgcse is that when > presented with tricky cases to unwind, the simplest approach is taken. > There it was disabling a compiler specific compiler optimization, here > it's either a compiler specific compiler plugin (or disabling another > compiler optimization). The pattern seems to be "Objtool isn't smart > enough" ... "compiler optimization disabled" or "compiler plugin > dependency." > >> tables (one per jump table) of references to the jump targets, similar >> to what x86 has, except that in this case this table isn't actually used >> by runtime code and is discarded at link time. I only chose this to >> minimize what needed to be changed in objtool and because the format >> seemed simple enough. >> >> But I'm open on some alternative, whether it's a -fjump-table-info > > Yes, I think we could spec out something like that. But I would > appreciate revisiting open questions around stack validation (frame > pointers), preventing the generation of jump tables to begin with > (-fno-jump-tables) in place of making objtool more robust, or > generally the need to depend on compiler plugins. > I'll give it a try at least for the arm64 side. Thanks, -- Julien Thierry From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60BDFC433E0 for ; Wed, 3 Feb 2021 08:12:26 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C2C6564F78 for ; Wed, 3 Feb 2021 08:12:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C2C6564F78 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=5OwRlQnLXf9cGlGHE7hZ08CbQWZvnjHem+W+DF4/vEI=; b=db0sz2WpX3LJoEn7ygRhBrxtQ JUUiGys0PrJ5RiUDlx99ITUktxc9J/CmCAuCW6Z2iV5sGwTllfXUCbO5ROwKq4agIgZxvfMyqXBPf B0iJOoSe4rPcsAHnOhyOMMPL0hHp6aHI1xXo1O3KG5q8RY43O06WIVnuQIMWMHyFRS0BPvRmZ/ouH AHhRUU7NFIYRnNCfgulv6/HoSkEnNqiAOQaAMAyIObSLjz5g3Q96stBIbXKPSHMfOwvOUSzxY1Jd5 nG4E6rY7uQd3gfVsPFN34hzoRG3YVeqDBfelRAgSz6iB1OQmOHL77wauy0b5yALWNYLdaFM2yhTrq a/EI0TVkA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l7DFn-00042g-WB; Wed, 03 Feb 2021 08:11:12 +0000 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l7DFk-00041l-Vv for linux-arm-kernel@lists.infradead.org; Wed, 03 Feb 2021 08:11:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612339868; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7Huq6tfU+kDDL1exMPNWAbCb858jufvEInD0eW8A0Yo=; b=NkmcRBUkYOiGyPedvI+AtyBd/g0EFGcmzPWtR5OJEkV+lveICKy0U7hQnvhteKiH1JKJNi 6Ct2buAe1662FRJ4BoQ4DUGlWbf/JaCBgB56rL+CXliKrdg0JjFN4WrlCIF7ETLmLhpZ1o LWCa2wyMLkd+SS1nvk4pexpIedmAc3I= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-202-WT0SGwYYNj6A_5tQHfOXOQ-1; Wed, 03 Feb 2021 03:11:06 -0500 X-MC-Unique: WT0SGwYYNj6A_5tQHfOXOQ-1 Received: by mail-wm1-f71.google.com with SMTP id j204so2688866wmj.4 for ; Wed, 03 Feb 2021 00:11:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=7Huq6tfU+kDDL1exMPNWAbCb858jufvEInD0eW8A0Yo=; b=Fg/umzJ/u+MeNy6pVyyj2BRgAelvka01GmdEg3/wiL712eukrgBFbrd9eAlCoxA+wO wxrsLxkRiUOnWiDwet8XlZwLhST/3/QERV/0rfJCfEcVJC5xllvCtdgp3iha6STzdJsV 6E7UKrruGrkmAgjEhnH8J1ns20R/Eiry0lXfS3IB8JbIOHjxalIpjypavMYVa6fzRJgm 5Af4sEbIAEHWv/LvXCrfp7FfMfZv79A3+R8HgJp5RI/uogdstB2Yw2rpHjhtqcN+QCnG CWA8OuwZI0THP6NjeguscNMejowOsw46XDNvPOzs4juHppFzlA/Zh4b0hX3EJWX7g+Jh O8gw== X-Gm-Message-State: AOAM533MfwjVSo7qTgfD86d63R1txytRgCWkjcThZLQgZk8HIWT93kfc OBJdKw6GhImX1iO0ywUI4FXd7eA303DdwMdNuCCO0xsVzIGh6dmOQwlyTzJQ4cUlffeielo1tRz lHyJrJx4zWTMNpbtZvHRI49ktux1J1EQMang= X-Received: by 2002:a1c:dcd7:: with SMTP id t206mr1572577wmg.108.1612339865393; Wed, 03 Feb 2021 00:11:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyor899BoErX+lNV6jwHugO2N82UM+DD4bmFtKm617J/dNiWTcXQvxuZH381L0jowogxpvzow== X-Received: by 2002:a1c:dcd7:: with SMTP id t206mr1572547wmg.108.1612339865171; Wed, 03 Feb 2021 00:11:05 -0800 (PST) Received: from ?IPv6:2a01:cb14:499:3d00:cd47:f651:9d80:157a? ([2a01:cb14:499:3d00:cd47:f651:9d80:157a]) by smtp.gmail.com with ESMTPSA id y63sm1590188wmd.21.2021.02.03.00.11.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Feb 2021 00:11:04 -0800 (PST) Subject: Re: [RFC PATCH 12/17] gcc-plugins: objtool: Add plugin to detect switch table on arm64 To: Nick Desaulniers References: <20210120173800.1660730-13-jthierry@redhat.com> <20210127221557.1119744-1-ndesaulniers@google.com> <20210127232651.rj3mo7c2oqh4ytsr@treble> <20210201214423.dhsma73k7ccscovm@treble> <671f1aa9-975e-1bda-6768-259adbdc24c8@redhat.com> From: Julien Thierry Message-ID: Date: Wed, 3 Feb 2021 09:11:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jthierry@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210203_031109_104803_769B5939 X-CRM114-Status: GOOD ( 43.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , swine@google.com, linux-efi , Kees Cook , clang-built-linux , Peter Zijlstra , Catalin Marinas , Masahiro Yamada , yonghyun@google.com, LKML , Michal Marek , raphael.gault@arm.com, Mark Brown , linux-hardening@vger.kernel.org, Josh Poimboeuf , Will Deacon , Ard Biesheuvel , Linux ARM , Bill Wendling Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 2/3/21 12:01 AM, Nick Desaulniers wrote: > On Tue, Feb 2, 2021 at 12:57 AM Julien Thierry wrote: >> >> >> >> On 2/2/21 12:17 AM, Nick Desaulniers wrote: >>> On Mon, Feb 1, 2021 at 1:44 PM Josh Poimboeuf wrote: >>>> >>>> On Fri, Jan 29, 2021 at 10:10:01AM -0800, Nick Desaulniers wrote: >>>>> On Wed, Jan 27, 2021 at 3:27 PM Josh Poimboeuf wrote: >>>>>> >>>>>> On Wed, Jan 27, 2021 at 02:15:57PM -0800, Nick Desaulniers wrote: >>>>>>>> From: Raphael Gault >>>>>>>> >>>>>>>> This plugins comes into play before the final 2 RTL passes of GCC and >>>>>>>> detects switch-tables that are to be outputed in the ELF and writes >>>>>>>> information in an ".discard.switch_table_info" section which will be >>>>>>>> used by objtool. >>>>>>>> >>>>>>>> Signed-off-by: Raphael Gault >>>>>>>> [J.T.: Change section name to store switch table information, >>>>>>>> Make plugin Kconfig be selected rather than opt-in by user, >>>>>>>> Add a relocation in the switch_table_info that points to >>>>>>>> the jump operation itself] >>>>>>>> Signed-off-by: Julien Thierry >>>>>>> >>>>>>> Rather than tightly couple this feature to a particular toolchain via >>>>>>> plugin, it might be nice to consider what features could be spec'ed out >>>>>>> for toolchains to implement (perhaps via a -f flag). >>>>>> >>>>>> The problem is being able to detect switch statement jump table vectors. >>>>>> >>>>>> For a given indirect branch (due to a switch statement), what are all >>>>>> the corresponding jump targets? >>>>>> >>>>>> We would need the compiler to annotate that information somehow. >>>>> >>>>> Makes sense, the compiler should have this information. How is this >>>>> problem solved on x86? >>>> >>>> Thus far we've been able to successfully reverse engineer it on x86, >>>> though it hasn't been easy. >>>> >>>> There were some particulars for arm64 which made doing so impossible. >>>> (I don't remember the details.) >> >> The main issue is that the tables for arm64 have more indirection than x86. > > I wonder if PAC or BTI also make this slightly more complex? PAC at > least has implications for unwinders, IIUC. > >> >> On x86, the dispatching jump instruction fetches the target address from >> a contiguous array of addresses based on a given offset. So the list of >> potential targets of the jump is neatly organized in a table (and sure, >> before link time these are just relocation, but still processable). >> >> On arm64 (with GCC at least), what is stored in a table is an array of >> candidate offsets from the jump instruction. And because arm64 is >> limited to 32bit instructions, the encoding often requires multiple >> instructions to compute the target address: >> >> ldr<*> x_offset, [x_offsets_table, x_index, ...] // load offset >> adr x_dest_base, // load target branch for offset 0 >> add x_dest, x_target_base, x_offset, ... // compute final address >> br x_dest // jump >> >> Where this gets trickier is that (with GCC) the offsets stored in the >> table might or might not be signed constants (and this can be seen in >> GCC intermediate representations, but I do not believe this information >> is output in the final object file). And on top of that, GCC might >> decide to use offsets that are seen as unsigned during intermediate >> representation as signed offset by sign extending them in the add >> instruction. >> >> So, to handle this we'd have to track the different operation done with >> the offset, from the load to the final jump, decoding the instructions >> and deducing the potential target instructions from the table of offsets. >> >> But that is error prone as we don't really know how many instructions >> can be between the ones doing the address computation, and I remember >> some messy case of a jump table inside a jump table where tracking the >> instruction touching one or the other offset would need a lot of corner >> case handling. >> >> And this of course is just for GCC, I haven't looked at what it all >> looks like on Clang's end. > > Sure, but this is what production unwinders do, and they don't require > compiler plugins, right? I don't doubt unwinders can be made simpler > with changes to toolchain output; please work with your compiler > vendor on making such changes rather than relying on compiler plugins > to do so. > I think there is a small confusion. The plugin nor the data it generates is not to be used by a kernel unwinder. It's here to allow objtool to assess whether the code being checked can be unwound (?) reliably (not omitting functions). Part of this is checking that a branch/jump in a function does not end up in some code that is not related to the function without setting up a call frame. This is about static validation rather than functionality. >>> I think the details are pertinent to finding a portable solution. The >>> commit message of this commit in particular doesn't document such >>> details, such as why such an approach is necessary or how the data is >>> laid out for objtool to consume it. >>> >> >> Sorry, I will need to make that clearer. The next patch explains it a >> bit [1] >> >> Basically, for simplicity, the plugin creates a new section containing > > Right, this takes a focus on simplicity, at the cost of alienating a toolchain. > > Ard's point about 3193c0836f20 relating to -fgcse is that when > presented with tricky cases to unwind, the simplest approach is taken. > There it was disabling a compiler specific compiler optimization, here > it's either a compiler specific compiler plugin (or disabling another > compiler optimization). The pattern seems to be "Objtool isn't smart > enough" ... "compiler optimization disabled" or "compiler plugin > dependency." > >> tables (one per jump table) of references to the jump targets, similar >> to what x86 has, except that in this case this table isn't actually used >> by runtime code and is discarded at link time. I only chose this to >> minimize what needed to be changed in objtool and because the format >> seemed simple enough. >> >> But I'm open on some alternative, whether it's a -fjump-table-info > > Yes, I think we could spec out something like that. But I would > appreciate revisiting open questions around stack validation (frame > pointers), preventing the generation of jump tables to begin with > (-fno-jump-tables) in place of making objtool more robust, or > generally the need to depend on compiler plugins. > I'll give it a try at least for the arm64 side. Thanks, -- Julien Thierry _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel