From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id A6A13385782D for ; Thu, 19 May 2022 08:30:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A6A13385782D Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24J62YJu027463 for ; Thu, 19 May 2022 08:30:43 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g241sbh9q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 19 May 2022 08:30:42 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 24J8FotX019097 for ; Thu, 19 May 2022 08:30:41 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2104.outbound.protection.outlook.com [104.47.70.104]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3g37cr6pnr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 19 May 2022 08:30:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nWG2ZtYpZOCQQC3o2qDjVT0yO7Snc3AjR+Hw+i7EM102t0OkXkAUGPK6tL7sCPrViejaFKWFQmfsQUQqWRhrcQlUMgM6mQ/9U9ejDqKJQosTrx9/401x7fAmKycv2w1Z5k1pFu9WDCMgt1+Cq7VoOSAB01vcJYgcTkIhOPix/qS4bsUolHSC1wqkqj67uMAIq3vpk4J/RO0tFaOkaDWy1c+vGw12NlqIM0z6za6ZuWOr3aRg0v9mQxNRcg1941eaHSjlhIEyF81ZVnvWC0pXck/TGc3uAtVJOF2b1HvhOi92LKQ1L1HnLAjUuy+6R7i0tbFFW3ciJHiM6OTuuZawSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dHyuis6E6X8LcrBAWQdXsbBZLbd/LgrdeA6Tzfug4/A=; b=hvEdvDRhzMwMTa/lypipf4l6HVtBGLiqslPPya4fJDev6JpKsVmOgf3/nCxymA0js1frGws3xgu5FzSCel+2MK60m3qf8g1HDgQmft5//HESef7ClXgHLhHklJeoBPkylY1BWnHI5PEuDZMqnFmPI9Z9EBtcZQb7NjdRmX52vjU5jLvBWOcKqeRO+n+zsppx2BJPaoJtHul1HvvfG8SPIxDtvROyPTAWP1jyXSOZlKkIPAO6Gow6D4hCliJbYvOH/06D918B8rBLOigNIAqlgth2BmL24y+PJhi46HVxUhQtBj2qTEufyovtP27xhYSYT1kFK0qKRCuU4oklMn84Ww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from MWHPR1001MB2158.namprd10.prod.outlook.com (2603:10b6:301:2d::17) by BN0PR10MB5080.namprd10.prod.outlook.com (2603:10b6:408:114::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.14; Thu, 19 May 2022 08:30:37 +0000 Received: from MWHPR1001MB2158.namprd10.prod.outlook.com ([fe80::c1ba:b4c:fe6f:d171]) by MWHPR1001MB2158.namprd10.prod.outlook.com ([fe80::c1ba:b4c:fe6f:d171%7]) with mapi id 15.20.5250.018; Thu, 19 May 2022 08:30:37 +0000 From: Indu Bhagat To: binutils@sourceware.org Subject: [PATCH,RFC,V2 3/8] gas: generate .ctf_frame Date: Thu, 19 May 2022 01:30:19 -0700 Message-Id: <20220519083024.2709920-4-indu.bhagat@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220519083024.2709920-1-indu.bhagat@oracle.com> References: <20220519083024.2709920-1-indu.bhagat@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: CO2PR04CA0070.namprd04.prod.outlook.com (2603:10b6:102:1::38) To MWHPR1001MB2158.namprd10.prod.outlook.com (2603:10b6:301:2d::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 23000c9c-d36f-4c52-e869-08da3971d997 X-MS-TrafficTypeDiagnostic: BN0PR10MB5080:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eTc+yU3mW9zSsA2zcsSWLkbo3RAXjXKJlRf6FOYoBUCGK/XrLjD92MsyivMVQTdtbYEEl9YmvuEe+gUTsshdxdk6edQk6vgJtxVeO7gj4PGYBu9F58VhzPj5luogL6Fe1kf6UAUBHknRsYLBSIVwBfPQmoReSuro5VGiJN5i380DfAIyj23fFxB7+kDfE6FFCH4TQfsvMRa61IrGHPetHxW/+KloRbfDgs3W8Yq05fxEPbtuT/P5O77BkhgaLhmQEglTGMmUirvR6BFTmUl0oJ/14x8TmGKA6+abvoEOug11///iHqaOJlIczrof94EdMzJO3Z+jz1gt6Cx6JrQnOl/YbVnRMKttdHZZOMNddyd0rZTS+LsfE1yL1gFK8Vb6quXJX7lxFRTso6Jn5PHGuqshq/nWMBpqQt+o6cqqO/E0R6oWs3LZ7nMBNap+X/RYD6QVkAwXCcAQtCMQkeREkDmRdjDcJ7hmviUiwcK1E79+p8roTLMeBsAf/U71toEN0tVXeobBwUbmuTO3UvLyDMMO6yJYgUzguR8pq+BBry+zfFBKCX/wrgFF/5j+V588AXmyOzs1NpyN8uq0WF/rPWI0jgp7qKlqtX7ak9SYBgY8Wlf8mjkjgoOVzDO9GGHf9LI8WgnAwsxHnGfP7zfOHa3BbeaEkS1mw6ncrx6VwSaSILNO3DhFeopAJBHsGsLX/4bwj6/zX1sO47zqceYelgL9Z3IXYyAzfUshHh295q8F7N0cuDDwTwQxZ5IpM6Uj X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MWHPR1001MB2158.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(44832011)(36756003)(8676002)(6506007)(38350700002)(66556008)(66946007)(66476007)(30864003)(316002)(6916009)(38100700002)(6486002)(1076003)(508600001)(6512007)(186003)(5660300002)(8936002)(45080400002)(2906002)(26005)(83380400001)(86362001)(52116002)(2616005)(6666004)(142923001)(2004002)(559001)(579004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?pKDA8kburvkDtifEz/v3TlDj7J8oKANpvdHeQp0SaV5vOdZrufcNIhqlz9za?= =?us-ascii?Q?7T0hl1YMGbkiY7E7l4MRk5lfk0L+cXtIBcXwtx+0s6czhDlidzEf3MjASdDd?= =?us-ascii?Q?+IcRe/Laq7Pr9nhDhyyTyRmqvs/cBVVJUajezGeD+6RZ0KCucRGbO9p/KTov?= =?us-ascii?Q?7CgWGVMZ4xqRVHxGw0Xw+Kn/gk/ofO0fPdqG5zYEuWKuGyreVC8CI89fxX3n?= =?us-ascii?Q?kZClqPUorB3BpfPXmPoc0c2JiEB+lYmBf3TfxN4KgKKkoe7ql/EO93OTD/GU?= =?us-ascii?Q?Si1kgL7ap60LWInkzvN8OWCiWsl62pf98BFVsAFj1rvgayGx6/B+InsVLl6f?= =?us-ascii?Q?2Ohre1nfmOShT9xPnMFXk0zdKNMER6uZsEPoH1Ua0lOGxIPxpweVmK+KLZPl?= =?us-ascii?Q?dC4TCyNvnFaxw6JH3qcGIGYhTGHSpWHoHg7WRxp28bgANUdGlVlX/mbEHxZO?= =?us-ascii?Q?a13MtM89DmSIYMk6sKagP0uEUOosdviSJRwF5obpfVQFaH/2q26F/+bquL4D?= =?us-ascii?Q?lycRMozGbM5YShbvcdMgkUH/2GMwx4qyebZq5nk5bpuJY/ViJqafVFH8a1hS?= =?us-ascii?Q?jqjF0iivX9IUDIng+LwrIkn7+q+SShFWR8eVEuay90j3IqSHY/0sa2viPIyO?= =?us-ascii?Q?cM1UmzONROFz0gIjV4MLiN70iJ/2D9OHOza0NV/iZxcqObCoQMKAx5GggQpf?= =?us-ascii?Q?jQc2ZdzTTg24y5WJ3DQe73/t+2SCyR/m9ZCu25DoWmREfuacM0F1S+NNZ+F/?= =?us-ascii?Q?YLMHBsCsC6Qciry8tg4GvtiwaprcX/c5UuHXRr9JwZas5yLRbn6IqvneBJVi?= =?us-ascii?Q?yI9zKpDM1bgwjHul+0nAArbGyyj6UAfXH4p6Gxjw8WxxsGuxilgHQP9UcGlN?= =?us-ascii?Q?GM0xTnyNVSCFZUuvu8KUpgS03muwBwWSkcVYLVWDDd6u05f9sFWpHoui6I52?= =?us-ascii?Q?0XwEgYhsUSYo0xkXgySkv4OTC2Fo9WJuQMZKvQefj4vv2MOHi9yQqQU+lj1/?= =?us-ascii?Q?AlipztORe0T6cHkExDVl0c48X2kTGe2ntIjyRf26ibN1tRctzv/MBUYUSASH?= =?us-ascii?Q?cFHjIA3CewTFO22MGVJRwh9YE0lVJ1AYvrt/jsB6dxSOxUW/ELBa1MqLr+BT?= =?us-ascii?Q?4/Jx3pHZYfvp8vR4n/yHBYADciURSMgiG3qDE9H77FdYBe5OtwHZu33XRcHb?= =?us-ascii?Q?pzhpTkIckHjPcow9Fllmr3VwSe3FMtiK/I+AHvnb61/e7N5t47YEVFZdEzfQ?= =?us-ascii?Q?64EFwCam58fo+5mdnKhdKGyc+4+XTA6kpxyUa6YKkoQuZOVF0/oZB3bQD8lC?= =?us-ascii?Q?3FChHuDHSpzM7DX2MDwM3UlOuXW/QeILEJP/TzjAHMTrcDlG86YDVpmI50v1?= =?us-ascii?Q?IZRgXXwZkTRSF85PVkYONsZgNv601VmUyW7cR8EB/nlmkNx3PkXin2/vvM6q?= =?us-ascii?Q?MrVPR3qIzrw2FaR4Nf0i2zbTwqslppS8Rr7FgEIvYAnakexuKUH9fg2qml6G?= =?us-ascii?Q?9FiUzcx2v+2h0J8RLRA0c4AALiVvEZ9GAYNqAKjJOva+QmEwc9w4/5G2VL+0?= =?us-ascii?Q?Wmwi/nrTERTwDvBqGjbEdGlChJIVvOzuTW9vYdcmtlDdu99QfGjPjve+DzVQ?= =?us-ascii?Q?6U7FE9VrbrR3hwpMY0Pmm4fssr2k33+ReqdPHTBcJqRxSk6X/tWGCAajFDUv?= =?us-ascii?Q?KM7+FHZhOcfyg3NgEh1gm13tZAdTlCqF0fJ68FifBk87y8h3XSZP9blkxi9v?= =?us-ascii?Q?yny2rz7afmIDsy8HUJbFYZfOAYlXmqE=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 23000c9c-d36f-4c52-e869-08da3971d997 X-MS-Exchange-CrossTenant-AuthSource: MWHPR1001MB2158.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2022 08:30:37.4951 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wGvV8Rx2xJTBoMgSNB6uJbHkQX+fBAiu9M/cgdn4ybH2DrNLwBi/ShpoA4DuAj39bhykgGpcl8hJJSkS6FmARA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR10MB5080 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486, 18.0.874 definitions=2022-05-19_01:2022-05-19, 2022-05-19 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 mlxscore=0 phishscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205190049 X-Proofpoint-GUID: gv9C4twDFMMbtCXoFeNjV0LkyQb3bedm X-Proofpoint-ORIG-GUID: gv9C4twDFMMbtCXoFeNjV0LkyQb3bedm X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 May 2022 08:30:49 -0000 [Changes from V1] - generate .ctf_frame if --gctf-frame is specified OR if .cfi_sections .ctf_frame is specified. - bugfix: use startswith in .cfi_sections. Earlier, strcmp was being used incorrectly. - generate .ctf_frame section even if there are no FDEs (no functions with unwind information). A .ctf_frame section with no FDEs is valid. Such a CTF Frame section will have a valid header with other fields set to appropriate values. - Support conditional compilation on the basis of support_ctf_frame_p. Compile in the CTF Frame generation APIs only when the target supports CTF Frame. Currently, gas emits CTF Frame unwind information for x86_64 and aarch64 only. - doc: update documentation for .cfi_sections to include .ctf_frame section in the list. [End of changes from V1] [PS: Currently, the compiler has not been adapted to generate ".cfi_sections" with ".ctf_frame" in it. The newly added command line option of --gctf-frame provides an easy way to try out .ctf_frame support in the toolchain.] gas interprets the CFI directives to generate DWARF-based .eh_frame info. These internal DWARF structures are now consumed by gen-ctf-frame.[ch] sub-system to, in turn, create the CTF Frame unwind information. These internal DWARF structures are read-only for the purpose of CTF Frame unwind info generation. CTF Frame unwind info generation does not impact .eh_frame unwind info generation. Both .eh_frame and .ctf_frame can co-exist in an ELF file, if so desired by the user. Recall that CTF Frame unwind information only contains the minimal necessary information to generate backtraces and does not provide information to recover all callee-saved registers. The reason being that callee-saved registers other than FP are not needed for stack unwinding, and hence are not included in the .ctf_frame section. Consequently, gen-ctf-frame.[ch] only needs to interpret a subset of DWARF opcodes in gas. More details follow. [Set 1, Interpreted] The following opcodes are interpreted: - DW_CFA_advance_loc - DW_CFA_def_cfa - DW_CFA_def_cfa_register - DW_CFA_def_cfa_offset - DW_CFA_offset - DW_CFA_remember_state - DW_CFA_restore_state - DW_CFA_restore [Set 2, Bypassed] The following opcodes are acknowledged but are not necessary for generating CTF Frame unwind info: - DW_CFA_undefined - DW_CFA_same_value Anything else apart from the two above-mentioned sets is skipped altogether. This means that any function containing a CFI directive not in Set 1 or Set 2 above, will not have any CTF Frame unwind information generated for them. Holes in instructions covered by FREs are not representable in the CTF unwind format. As few examples, following opcodes are not needed to be processed for .ctf_frame generation, and are skipped: - .cfi_personality* - .cfi_*lsda - .cfi_escape - ... x86-64 and aarch64 backends need to have a few new definitions and functions for .ctf_frame generation to provide gas with architecture specific information like SP/FP/RA register numbers and a CTF Frame specific ABI marker. Lastly, the patch also implements an optimization for size, where specific fragments containing CTF FRE start address and CTF FDE function info are fixed up. This is similar to other similar optimizations in gas, where fragments are sized and fixed up when the associated symbols can be resolved. This optimization is controlled by a #define CTF_FRE_START_ADDR_OPT and should be easy to turn off if needed. The optimization is on by default for both x86_64 and aarch64. ChangeLog: * gas/Makefile.am: Include gen-ctf-frame.c and ctffreopt.c. * gas/Makefile.in: Regenerated. * gas/as.h (enum _relax_state): Add new state rs_ctf_fre. (ctf_frame_estimate_size_before_relax): New function. (ctf_frame_relax_frag): Likewise. (ctf_frame_convert_frag): Likewise. * gas/config/tc-aarch64.c (enum aarch64_abi_type): New declaration. (aarch64_support_ctf_frame_p): Likewise. (aarch64_ctf_frame_ra_tracking_p): Likewise. (aarch64_ctf_frame_cfa_ra_offset): Likewise. (aarch64_ctf_frame_get_abi_arch): Likewise. (md_begin): Set values of sp/fp/ra registers. * gas/config/tc-aarch64.h (aarch64_support_ctf_frame_p): New declaration. (support_ctf_frame_p): Likewise. (CTF_FRAME_CFA_SP_REG): Likewise. (CTF_FRAME_CFA_FP_REG): Likewise. (CTF_FRAME_CFA_RA_REG): Likewise. (aarch64_ctf_frame_ra_tracking_p): Likewise. (ctf_frame_ra_tracking_p): Likewise. (aarch64_ctf_frame_cfa_ra_offset): Likewise. (ctf_frame_cfa_ra_offset): Likewise. (aarch64_ctf_frame_get_abi_arch): Likewise. (ctf_frame_get_abi_arch): Likewise. * gas/config/tc-i386.c (md_begin): Set values of sp/fp/ra. (x86_support_ctf_frame_p): New definition. (x86_ctf_frame_ra_tracking_p): Likewise. (x86_ctf_frame_cfa_ra_offset): Likewise. (x86_ctf_frame_get_abi_arch): Likewise. * gas/config/tc-i386.h (x86_support_ctf_frame_p): New declaration. (support_ctf_frame_p): Likewise. (CTF_FRAME_CFA_SP_REG): Likewise. (CTF_FRAME_CFA_FP_REG): Likewise. (x86_ctf_frame_ra_tracking_p): Likewise. (ctf_frame_ra_tracking_p): Likewise. (x86_ctf_frame_cfa_ra_offset): Likewise. (ctf_frame_cfa_ra_offset): Likewise. (x86_ctf_frame_get_abi_arch): Likewise. (ctf_frame_get_abi_arch): Likewise. * gas/config/tc-xtensa.c (unrelaxed_frag_max_size): Add case for rs_ctf_fre. * gas/dw2gencfi.c (cfi_finish): Create a .ctf_frame section. * gas/dw2gencfi.h (CFI_EMIT_ctf_frame): New definition. * gas/write.c (cvt_frag_to_fill): Handle rs_ctf_fre. (relax_segment): Handle rs_ctf_fre. * gas/ctffreopt.c: New file. * gas/gen-ctf-frame.c: New file. * gas/gen-ctf-frame.h: New file. --- gas/Makefile.am | 3 + gas/Makefile.in | 22 +- gas/as.h | 10 +- gas/config/tc-aarch64.c | 42 ++ gas/config/tc-aarch64.h | 29 + gas/config/tc-i386.c | 46 ++ gas/config/tc-i386.h | 26 + gas/config/tc-xtensa.c | 1 + gas/ctffreopt.c | 158 +++++ gas/doc/as.texi | 14 +- gas/dw2gencfi.c | 30 + gas/dw2gencfi.h | 1 + gas/gen-ctf-frame.c | 1212 +++++++++++++++++++++++++++++++++++++++ gas/gen-ctf-frame.h | 142 +++++ gas/write.c | 13 + 15 files changed, 1735 insertions(+), 14 deletions(-) create mode 100644 gas/ctffreopt.c create mode 100644 gas/gen-ctf-frame.c create mode 100644 gas/gen-ctf-frame.h diff --git a/gas/Makefile.am b/gas/Makefile.am index 7159be56a08..b84a6adca7e 100644 --- a/gas/Makefile.am +++ b/gas/Makefile.am @@ -70,6 +70,8 @@ GAS_CFILES = \ atof-generic.c \ compress-debug.c \ cond.c \ + ctffreopt.c \ + gen-ctf-frame.c \ depend.c \ dwarf2dbg.c \ dw2gencfi.c \ @@ -105,6 +107,7 @@ HFILES = \ bit_fix.h \ cgen.h \ compress-debug.h \ + gen-ctf-frame.h \ dwarf2dbg.h \ dw2gencfi.h \ ecoff.h \ diff --git a/gas/Makefile.in b/gas/Makefile.in index 67dac53f68c..b00b02ccc15 100644 --- a/gas/Makefile.in +++ b/gas/Makefile.in @@ -160,14 +160,15 @@ CONFIG_CLEAN_FILES = gdb.ini .gdbinit po/Makefile.in CONFIG_CLEAN_VPATH_FILES = PROGRAMS = $(noinst_PROGRAMS) am__objects_1 = app.$(OBJEXT) as.$(OBJEXT) atof-generic.$(OBJEXT) \ - compress-debug.$(OBJEXT) cond.$(OBJEXT) depend.$(OBJEXT) \ - dwarf2dbg.$(OBJEXT) dw2gencfi.$(OBJEXT) ecoff.$(OBJEXT) \ - ehopt.$(OBJEXT) expr.$(OBJEXT) flonum-copy.$(OBJEXT) \ - flonum-konst.$(OBJEXT) flonum-mult.$(OBJEXT) frags.$(OBJEXT) \ - hash.$(OBJEXT) input-file.$(OBJEXT) input-scrub.$(OBJEXT) \ - listing.$(OBJEXT) literal.$(OBJEXT) macro.$(OBJEXT) \ - messages.$(OBJEXT) output-file.$(OBJEXT) read.$(OBJEXT) \ - remap.$(OBJEXT) sb.$(OBJEXT) stabs.$(OBJEXT) subsegs.$(OBJEXT) \ + compress-debug.$(OBJEXT) cond.$(OBJEXT) ctffreopt.$(OBJEXT) \ + gen-ctf-frame.$(OBJEXT) depend.$(OBJEXT) dwarf2dbg.$(OBJEXT) \ + dw2gencfi.$(OBJEXT) ecoff.$(OBJEXT) ehopt.$(OBJEXT) \ + expr.$(OBJEXT) flonum-copy.$(OBJEXT) flonum-konst.$(OBJEXT) \ + flonum-mult.$(OBJEXT) frags.$(OBJEXT) hash.$(OBJEXT) \ + input-file.$(OBJEXT) input-scrub.$(OBJEXT) listing.$(OBJEXT) \ + literal.$(OBJEXT) macro.$(OBJEXT) messages.$(OBJEXT) \ + output-file.$(OBJEXT) read.$(OBJEXT) remap.$(OBJEXT) \ + sb.$(OBJEXT) stabs.$(OBJEXT) subsegs.$(OBJEXT) \ symbols.$(OBJEXT) write.$(OBJEXT) am_as_new_OBJECTS = $(am__objects_1) am__dirstamp = $(am__leading_dot)dirstamp @@ -549,6 +550,8 @@ GAS_CFILES = \ atof-generic.c \ compress-debug.c \ cond.c \ + ctffreopt.c \ + gen-ctf-frame.c \ depend.c \ dwarf2dbg.c \ dw2gencfi.c \ @@ -583,6 +586,7 @@ HFILES = \ bit_fix.h \ cgen.h \ compress-debug.h \ + gen-ctf-frame.h \ dwarf2dbg.h \ dw2gencfi.h \ ecoff.h \ @@ -1289,6 +1293,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cgen.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/compress-debug.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cond.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctffreopt.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/depend.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dw2gencfi.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwarf2dbg.Po@am__quote@ @@ -1299,6 +1304,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flonum-konst.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flonum-mult.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/frags.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gen-ctf-frame.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hash.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/input-file.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/input-scrub.Po@am__quote@ diff --git a/gas/as.h b/gas/as.h index 70b5ab94440..f7e59de1db3 100644 --- a/gas/as.h +++ b/gas/as.h @@ -253,7 +253,10 @@ enum _relax_state rs_cfa, /* Cross-fragment dwarf2 line number optimization. */ - rs_dwarf2dbg + rs_dwarf2dbg, + + /* CTF frame FRE start address size encoding optimization. */ + rs_ctf_fre }; typedef enum _relax_state relax_stateT; @@ -513,6 +516,11 @@ int eh_frame_relax_frag (fragS *); void eh_frame_convert_frag (fragS *); int generic_force_reloc (struct fix *); +/* CTF frame FRE optimization. */ +int ctf_frame_estimate_size_before_relax (fragS *); +int ctf_frame_relax_frag (fragS *); +void ctf_frame_convert_frag (fragS *); + #include "expr.h" /* Before targ-*.h */ /* This one starts the chain of target dependent headers. */ diff --git a/gas/config/tc-aarch64.c b/gas/config/tc-aarch64.c index 407f8881493..56b40aa81f1 100644 --- a/gas/config/tc-aarch64.c +++ b/gas/config/tc-aarch64.c @@ -31,6 +31,7 @@ #ifdef OBJ_ELF #include "elf/aarch64.h" #include "dw2gencfi.h" +#include "gen-ctf-frame.h" #endif #include "dwarf2dbg.h" @@ -70,6 +71,11 @@ enum aarch64_abi_type AARCH64_ABI_ILP32 = 2 }; +unsigned int aarch64_ctf_frame_cfa_sp_reg; +/* The other CFA base register for CTF unwind info. */ +unsigned int aarch64_ctf_frame_cfa_fp_reg; +unsigned int aarch64_ctf_frame_cfa_ra_reg; + #ifndef DEFAULT_ARCH #define DEFAULT_ARCH "aarch64" #endif @@ -8377,6 +8383,36 @@ tc_aarch64_frame_initial_instructions (void) { cfi_add_CFA_def_cfa (REG_SP, 0); } + +bool +aarch64_support_ctf_frame_p (void) +{ + if (aarch64_abi == AARCH64_ABI_LP64) + return 1; + return 0; +} + +bool +aarch64_ctf_frame_ra_tracking_p (void) +{ + return 1; +} + +offsetT +aarch64_ctf_frame_cfa_ra_offset (void) +{ + return (offsetT)0; +} + +unsigned char +aarch64_ctf_frame_get_abi_arch (void) +{ + if (aarch64_support_ctf_frame_p ()) + return ctf_frame_get_abi_arch_callback ("aarch64", target_big_endian); + else + return 0; +} + #endif /* OBJ_ELF */ /* Convert REGNAME to a DWARF-2 register number. */ @@ -9639,6 +9675,12 @@ md_begin (void) mach = ilp32_p ? bfd_mach_aarch64_ilp32 : bfd_mach_aarch64; bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach); +#ifdef OBJ_ELF + // FIXME - is there a better way to do it ? + aarch64_ctf_frame_cfa_sp_reg = 31; + aarch64_ctf_frame_cfa_fp_reg = 29; /* x29. */ + aarch64_ctf_frame_cfa_ra_reg = 30; +#endif } /* Command line processing. */ diff --git a/gas/config/tc-aarch64.h b/gas/config/tc-aarch64.h index f5c17523796..c72d8d77165 100644 --- a/gas/config/tc-aarch64.h +++ b/gas/config/tc-aarch64.h @@ -235,6 +235,35 @@ struct aarch64_segment_info_type /* We want .cfi_* pseudo-ops for generating unwind info. */ #define TARGET_USE_CFIPOP 1 +/* Whether CTF unwind info is supported. */ +extern bool aarch64_support_ctf_frame_p (void); +#define support_ctf_frame_p aarch64_support_ctf_frame_p + +/* The stack-pointer register number for CTF unwind info. */ +extern unsigned int aarch64_ctf_frame_cfa_sp_reg; +#define CTF_FRAME_CFA_SP_REG aarch64_ctf_frame_cfa_sp_reg + +/* The base-pointer register number for CFA unwind info. */ +extern unsigned int aarch64_ctf_frame_cfa_fp_reg; +#define CTF_FRAME_CFA_FP_REG aarch64_ctf_frame_cfa_fp_reg + +/* The return address register number for CFA unwind info. */ +extern unsigned int aarch64_ctf_frame_cfa_ra_reg; +#define CTF_FRAME_CFA_RA_REG aarch64_ctf_frame_cfa_ra_reg + +/* Specify if RA tracking is needed. */ +extern bool aarch64_ctf_frame_ra_tracking_p (void); +#define ctf_frame_ra_tracking_p aarch64_ctf_frame_ra_tracking_p + +/* Specify the fixed offset to recover RA from CFA. + (useful only when RA tracking is not needed). */ +extern offsetT aarch64_ctf_frame_cfa_ra_offset (void); +#define ctf_frame_cfa_ra_offset aarch64_ctf_frame_cfa_ra_offset + +/* The abi/arch indentifier for CTF frame. */ +unsigned char aarch64_ctf_frame_get_abi_arch (void); +#define ctf_frame_get_abi_arch aarch64_ctf_frame_get_abi_arch + /* CFI hooks. */ #define tc_regname_to_dw2regnum tc_aarch64_regname_to_dw2regnum #define tc_cfi_frame_initial_instructions tc_aarch64_frame_initial_instructions diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c index 61b9af1c24b..77c932b3bc3 100644 --- a/gas/config/tc-i386.c +++ b/gas/config/tc-i386.c @@ -30,6 +30,7 @@ #include "subsegs.h" #include "dwarf2dbg.h" #include "dw2gencfi.h" +#include "gen-ctf-frame.h" #include "elf/x86-64.h" #include "opcodes/i386-init.h" #include @@ -594,6 +595,12 @@ static int use_big_obj = 0; #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) /* 1 if generating code for a shared library. */ static int shared = 0; + +unsigned int x86_ctf_frame_cfa_sp_reg; +/* The other CFA base register for CTF unwind info. */ +unsigned int x86_ctf_frame_cfa_fp_reg; +unsigned int x86_ctf_frame_cfa_ra_reg; + #endif /* 1 for intel syntax, @@ -3169,6 +3176,10 @@ md_begin (void) x86_dwarf2_return_column = 16; #endif x86_cie_data_alignment = -8; +#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) + x86_ctf_frame_cfa_sp_reg = 7; + x86_ctf_frame_cfa_fp_reg = 6; +#endif } else { @@ -9199,6 +9210,41 @@ x86_cleanup (void) if (seg && subseg) subseg_set (seg, subseg); } + +bool +x86_support_ctf_frame_p (void) +{ + /* At this time, CTF unwind is supported for AMD64 ABI only. */ + if (x86_elf_abi == X86_64_ABI) + return true; + return false; +} + +bool +x86_ctf_frame_ra_tracking_p (void) +{ + /* In AMD64, return address is always stored on the stack at a fixed offset + from the CFA (provided via x86_ctf_frame_cfa_ra_offset ()). + Do not track explicitly via a CTF frame row entry. */ + return false; +} + +offsetT +x86_ctf_frame_cfa_ra_offset (void) +{ + gas_assert (x86_elf_abi == X86_64_ABI); + return (offsetT)-8; +} + +unsigned char +x86_ctf_frame_get_abi_arch (void) +{ + if (x86_support_ctf_frame_p ()) + return ctf_frame_get_abi_arch_callback ("x86-64", target_big_endian); + else + return 0; +} + #endif static unsigned int diff --git a/gas/config/tc-i386.h b/gas/config/tc-i386.h index d7c6b33c778..928915252dd 100644 --- a/gas/config/tc-i386.h +++ b/gas/config/tc-i386.h @@ -356,6 +356,32 @@ extern bfd_vma x86_64_section_letter (int, const char **); #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) extern void x86_cleanup (void); #define md_cleanup() x86_cleanup () + +/* Whether CTF unwind info is supported. */ +extern bool x86_support_ctf_frame_p (void); +#define support_ctf_frame_p x86_support_ctf_frame_p + +/* The stack-pointer register number for CTF unwind info. */ +extern unsigned int x86_ctf_frame_cfa_sp_reg; +#define CTF_FRAME_CFA_SP_REG x86_ctf_frame_cfa_sp_reg + +/* The frame-pointer register number for CFA unwind info. */ +extern unsigned int x86_ctf_frame_cfa_fp_reg; +#define CTF_FRAME_CFA_FP_REG x86_ctf_frame_cfa_fp_reg + +/* Specify if RA tracking is needed. */ +extern bool x86_ctf_frame_ra_tracking_p (void); +#define ctf_frame_ra_tracking_p x86_ctf_frame_ra_tracking_p + +/* Specify the fixed offset to recover RA from CFA. + (useful only when RA tracking is not needed). */ +extern offsetT x86_ctf_frame_cfa_ra_offset (void); +#define ctf_frame_cfa_ra_offset x86_ctf_frame_cfa_ra_offset + +/* The abi/arch indentifier for CTF frame. */ +extern unsigned char x86_ctf_frame_get_abi_arch (void); +#define ctf_frame_get_abi_arch x86_ctf_frame_get_abi_arch + #endif #ifdef TE_PE diff --git a/gas/config/tc-xtensa.c b/gas/config/tc-xtensa.c index 3e3deb5dd36..3108b6c9ec2 100644 --- a/gas/config/tc-xtensa.c +++ b/gas/config/tc-xtensa.c @@ -8616,6 +8616,7 @@ unrelaxed_frag_max_size (fragS *fragP) case rs_leb128: case rs_cfa: case rs_dwarf2dbg: + case rs_ctf_fre: /* No further adjustments needed. */ break; case rs_machine_dependent: diff --git a/gas/ctffreopt.c b/gas/ctffreopt.c new file mode 100644 index 00000000000..118c1ad40bd --- /dev/null +++ b/gas/ctffreopt.c @@ -0,0 +1,158 @@ +/* ctffreopt.c - optimize FRE and FDE information in CTF frame. + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GAS, the GNU Assembler. + + GAS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GAS; see the file COPYING. If not, write to the Free + Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA + 02110-1301, USA. */ + +#include "as.h" +#include "ctf-frame.h" + +/* The function estimates the size of a rs_ctf_fre variant frag based on + the current values of the symbols. It is called before the + relaxation loop. We set fr_subtype{0:2} to the expected length. */ + +int +ctf_frame_estimate_size_before_relax (fragS *frag) +{ + offsetT width; + expressionS *exp; + symbolS *widthS; + int ret; + + /* We are dealing with two different kind of fragments here which need + to be fixed up: + - first, FRE start address in each FRE, and + - second, Function info in each FDE (function info stores the FRE type) + The two kind of fragments can be differentiated based on the opcode + of the symbol. */ + exp = symbol_get_value_expression (frag->fr_symbol); + gas_assert ((exp->X_op == O_subtract) || (exp->X_op == O_absent)); + /* Fragment for function info in a CTF frame FDE will always write + only one byte. */ + if (exp->X_op == O_subtract) + ret = 1; + /* Fragment for the start address in a CTF frame FRE may write out + 1/2/4 bytes depending on the value of the diff. */ + else + { + /* Get the width expression from the symbol. */ + widthS = exp->X_op_symbol; + width = resolve_symbol_value (widthS); + + if (width < 0x100) + ret = 1; + else if (width < 0x10000) + ret = 2; + else + ret = 4; + } + + frag->fr_subtype = (frag->fr_subtype & ~7) | (ret & 7); + + return ret; +} + +/* This function relaxes a rs_ctf_fre variant frag based on the current + values of the symbols. fr_subtype{0:2} is the current length of + the frag. This returns the change in frag length. */ + +int +ctf_frame_relax_frag (fragS *frag) +{ + int oldsize, newsize; + + oldsize = frag->fr_subtype & 7; + if (oldsize == 7) + oldsize = -1; + newsize = ctf_frame_estimate_size_before_relax (frag); + return newsize - oldsize; +} + +/* This function converts a rs_ctf_fre variant frag into a normal fill + frag. This is called after all relaxation has been done. + fr_subtype{0:2} will be the desired length of the frag. */ + +void +ctf_frame_convert_frag (fragS *frag) +{ + offsetT fsize; + offsetT diff; + offsetT value; + unsigned char func_info = CTF_FRAME_ROW_ENTRY_TYPE_ADDR4; + expressionS *exp; + symbolS *fsizeS, *diffS; + + /* We are dealing with two different kind of fragments here which need + to be fixed up: + - first, FRE start address in each FRE, and + - second, Function info in each FDE (function info stores the FRE type) + The two kind of fragments can be differentiated based on the opcode + of the symbol. */ + exp = symbol_get_value_expression (frag->fr_symbol); + gas_assert ((exp->X_op == O_subtract) || (exp->X_op == O_absent)); + /* Fragment for function info in a CTF frame FDE. */ + if (exp->X_op == O_subtract) + { + fsizeS = frag->fr_symbol; + fsize = resolve_symbol_value (fsizeS); + if (fsize < 0x100) + func_info = CTF_FRAME_ROW_ENTRY_TYPE_ADDR1; + else if (fsize < 0x10000) + func_info = CTF_FRAME_ROW_ENTRY_TYPE_ADDR2; + else + func_info = CTF_FRAME_ROW_ENTRY_TYPE_ADDR4; + value = func_info; + + frag->fr_literal[frag->fr_fix] = value; + } + /* Fragment for the start address in a CTF frame FRE. */ + else + { + /* Get the fsize expression from the symbol. */ + fsizeS = exp->X_op_symbol; + fsize = resolve_symbol_value (fsizeS); + /* Get the diff expression from the symbol. */ + diffS= exp->X_add_symbol; + diff = resolve_symbol_value (diffS); + value = diff; + + switch (frag->fr_subtype & 7) + { + case 1: + gas_assert (fsize < 0x100); + frag->fr_literal[frag->fr_fix] = diff; + break; + case 2: + gas_assert (fsize < 0x10000); + md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2); + break; + case 4: + md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4); + break; + default: + abort (); + } + } + + frag->fr_fix += frag->fr_subtype & 7; + frag->fr_type = rs_fill; + frag->fr_subtype = 0; + frag->fr_offset = 0; + /* FIXME do this now because we have evaluated and fixed up the fragments + manually ? */ + frag->fr_symbol = 0; +} diff --git a/gas/doc/as.texi b/gas/doc/as.texi index c74b9eee3d6..c1c3233ece6 100644 --- a/gas/doc/as.texi +++ b/gas/doc/as.texi @@ -4846,11 +4846,15 @@ Each expression is assembled into the next byte. @subsection @code{.cfi_sections @var{section_list}} @cindex @code{cfi_sections} directive @code{.cfi_sections} may be used to specify whether CFI directives -should emit @code{.eh_frame} section and/or @code{.debug_frame} section. -If @var{section_list} is @code{.eh_frame}, @code{.eh_frame} is emitted, -if @var{section_list} is @code{.debug_frame}, @code{.debug_frame} is emitted. -To emit both use @code{.eh_frame, .debug_frame}. The default if this -directive is not used is @code{.cfi_sections .eh_frame}. +should emit @code{.eh_frame} section, @code{.debug_frame} section and/or +@code{.ctf_frame} section. If @var{section_list} contains @code{.eh_frame}, +@code{.eh_frame} is emitted, if @var{section_list} contains +@code{.debug_frame}, @code{.debug_frame} is emitted, and finally, if +@var{section_list} contains @code{.ctf_frame}, @code{.ctf_frame} is emitted. +To emit multiple sections, specify them together in a list. For example, to +emit both @code{.eh_frame} and @code{.debug_frame}, use +@code{.eh_frame, .debug_frame}. The default if this directive is not used +is @code{.cfi_sections .eh_frame}. On targets that support compact unwinding tables these can be generated by specifying @code{.eh_frame_entry} instead of @code{.eh_frame}. diff --git a/gas/dw2gencfi.c b/gas/dw2gencfi.c index 961a3b92188..ec1d3405a08 100644 --- a/gas/dw2gencfi.c +++ b/gas/dw2gencfi.c @@ -23,6 +23,7 @@ #include "dw2gencfi.h" #include "subsegs.h" #include "dwarf2dbg.h" +#include "gen-ctf-frame.h" #ifdef TARGET_USE_CFIPOP @@ -1235,6 +1236,8 @@ dot_cfi_sections (int ignored ATTRIBUTE_UNUSED) else if (strcmp (name, tc_cfi_section_name) == 0) sections |= CFI_EMIT_target; #endif + else if (startswith (name, ".ctf_frame")) + sections |= CFI_EMIT_ctf_frame; else { *input_line_pointer = c; @@ -2475,6 +2478,33 @@ cfi_finish (void) flag_traditional_format = save_flag_traditional_format; } + cfi_sections_set = true; + // FIXME - remove this commented line once the compiler can specify + // .ctf_frame for .cfi_sections directive + // if ((all_cfi_sections & CFI_EMIT_ctf_frame) != 0) + if (flag_gen_ctf_frame || (all_cfi_sections & CFI_EMIT_ctf_frame) != 0) + { +#ifdef support_ctf_frame_p + if (support_ctf_frame_p ()) + { + segT ctf_frame_seg; + int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1; + + if (!SUPPORT_FRAME_LINKONCE) + ctf_frame_seg = get_cfi_seg (NULL, ".ctf_frame", + (SEC_ALLOC | SEC_LOAD | SEC_DATA + | DWARF2_EH_FRAME_READ_ONLY), + alignment); + output_ctf_frame (ctf_frame_seg); + } + else + as_bad (_(".ctf_frame not supported for target")); + +#else + as_bad (_(".ctf_frame not supported for target")); +#endif + } + cfi_sections_set = true; if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0) { diff --git a/gas/dw2gencfi.h b/gas/dw2gencfi.h index d570cdb8db3..02ef27af7ca 100644 --- a/gas/dw2gencfi.h +++ b/gas/dw2gencfi.h @@ -200,5 +200,6 @@ extern struct fde_entry *all_fde_data; #define CFI_EMIT_debug_frame (1 << 1) #define CFI_EMIT_target (1 << 2) #define CFI_EMIT_eh_frame_compact (1 << 3) +#define CFI_EMIT_ctf_frame (1 << 4) #endif /* DW2GENCFI_H */ diff --git a/gas/gen-ctf-frame.c b/gas/gen-ctf-frame.c new file mode 100644 index 00000000000..c8ce270b362 --- /dev/null +++ b/gas/gen-ctf-frame.c @@ -0,0 +1,1212 @@ +/* gen-ctf-frame.c - Support for generating CTF frame section. + Copyright (C) 2021 Free Software Foundation, Inc. + + This file is part of GAS, the GNU Assembler. + + GAS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GAS; see the file COPYING. If not, write to the Free + Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA + 02110-1301, USA. */ + +#include "as.h" +#include "subsegs.h" +#include "ctf-frame.h" +#include "gen-ctf-frame.h" +#include "dw2gencfi.h" + +#ifdef support_ctf_frame_p + +/* By default, use 32-bit relocations from .ctf_frame into .text. */ +#ifndef CTF_FRAME_RELOC_SIZE +# define CTF_FRAME_RELOC_SIZE 4 +#endif + +/* Whether frame row entries track RA. + + A target may not need return address tracking for stack unwinding. If it + does need the same, CTF_FRAME_CFA_RA_REG must be defined with the return + address register number. */ + +#if defined (ctf_frame_ra_tracking_p) && defined (CTF_FRAME_CFA_RA_REG) +# ifndef CTFF_ROW_ENTRY_RA_TRACKING +# define CTFF_ROW_ENTRY_RA_TRACKING 1 +# endif +#endif + +/* Some fragments generated for CTF frame section are fixed up later. This + optimization (for size) is on by default. */ + +#ifndef CTF_FRE_START_ADDR_OPT +# define CTF_FRE_START_ADDR_OPT 1 +#endif + +/* Emit a single byte into the current segment. */ + +static inline void +out_one (int byte) +{ + FRAG_APPEND_1_CHAR (byte); +} + +/* Emit a two-byte word into the current segment. */ + +static inline void +out_two (int data) +{ + md_number_to_chars (frag_more (2), data, 2); +} + +/* Emit a four byte word into the current segment. */ + +static inline void +out_four (int data) +{ + md_number_to_chars (frag_more (4), data, 4); +} + +/* Get the start address symbol from the DWARF FDE. */ + +static symbolS* +get_dw_fde_start_addrS (const struct fde_entry *dw_fde) +{ + return dw_fde->start_address; +} + +/* Get the start address symbol from the DWARF FDE. */ + +static symbolS* +get_dw_fde_end_addrS (const struct fde_entry *dw_fde) +{ + return dw_fde->end_address; +} + +/* Callback to create the abi/arch identifier for CTF frame section. */ + +unsigned char +ctf_frame_get_abi_arch_callback (const char *target_arch, + int big_endian_p) +{ + unsigned char ctf_frame_abi_arch = 0; + + if (strcmp (target_arch, "aarch64") == 0) + { + ctf_frame_abi_arch = big_endian_p + ? CTF_FRAME_ABI_AARCH64_ENDIAN_BIG + : CTF_FRAME_ABI_AARCH64_ENDIAN_LITTLE; + } + else if (strcmp (target_arch, "x86-64") == 0) + { + gas_assert (!big_endian_p); + ctf_frame_abi_arch = CTF_FRAME_ABI_AMD64_ENDIAN_LITTLE; + } + else + { + /* Other abi/arch are not supported. Should be unreachable. */ + printf (_("CTF Unsupported abi or arch\n")); + abort (); + } + + return ctf_frame_abi_arch; +} + +/* CTF Frame Row Entry (FRE) related functions. */ + +static void +ctf_fre_set_begin_addr (struct ctf_frame_row_entry *fre, symbolS *beginS) +{ + fre->pc_begin = beginS; +} + +static void +ctf_fre_set_end_addr (struct ctf_frame_row_entry *fre, symbolS *endS) +{ + fre->pc_end = endS; +} + +static void +ctf_fre_set_cfa_base_reg (struct ctf_frame_row_entry *fre, + unsigned int cfa_base_reg) +{ + fre->cfa_base_reg = cfa_base_reg; + fre->merge_candidate = false; +} + +static void +ctf_fre_set_cfa_offset (struct ctf_frame_row_entry *fre, offsetT cfa_offset) +{ + fre->cfa_offset = cfa_offset; + fre->merge_candidate = false; +} + +#ifdef CTFF_ROW_ENTRY_RA_TRACKING +static void +ctf_fre_set_ra_track (struct ctf_frame_row_entry *fre, offsetT ra_offset) +{ + fre->ra_loc = CTF_FRE_ELEM_LOC_STACK; + fre->ra_offset = ra_offset; + fre->merge_candidate = false; +} +#endif + +static void +ctf_fre_set_bp_track (struct ctf_frame_row_entry *fre, offsetT bp_offset) +{ + fre->bp_loc = CTF_FRE_ELEM_LOC_STACK; + fre->bp_offset = bp_offset; + fre->merge_candidate = false; +} + +/* All stack offset values within an FRE are uniformly encoded in the same + number of bytes. The size of the stack offset values will, however, vary + across FREs. */ + +#define VALUE_8BIT 0x7f +#define VALUE_16BIT 0x7fff +#define VALUE_32BIT 0x7fffffff +#define VALUE_64BIT 0x7fffffffffffffff + +/* Given a signed offset, return the size in bytes needed to represent it. */ + +static unsigned int +get_offset_size_in_bytes (offsetT value) +{ + unsigned int size = 0; + + if (value <= VALUE_8BIT && value >= (long)-VALUE_8BIT) + size = 1; + else if (value <= VALUE_16BIT && value >= (long)-VALUE_16BIT) + size = 2; + else if (value <= VALUE_32BIT && value >= (long)-VALUE_32BIT) + size = 4; + else if (value <= VALUE_64BIT && value >= (long)-VALUE_64BIT) + size = 8; + + return size; +} + +#define CTF_FRE_OFFSET_FUNC_MAP_INDEX_1B 0 /* CTF_FRAME_FRE_OFFSET_1B. */ +#define CTF_FRE_OFFSET_FUNC_MAP_INDEX_2B 1 /* CTF_FRAME_FRE_OFFSET_2B. */ +#define CTF_FRE_OFFSET_FUNC_MAP_INDEX_4B 2 /* CTF_FRAME_FRE_OFFSET_4B. */ +#define CTF_FRE_OFFSET_FUNC_MAP_INDEX_8B 3 /* Not supported in CTF. */ +#define CTF_FRE_OFFSET_FUNC_MAP_INDEX_MAX CTF_FRE_OFFSET_FUNC_MAP_INDEX_8B + +/* Helper struct for mapping offset size to output functions. */ + +struct ctf_fre_offset_func_map +{ + unsigned int offset_size; + void (*out_func)(int); +}; + +/* Given an OFFSET_SIZE, return the size in bytes needed to represent it. */ + +static unsigned int +ctf_fre_offset_func_map_index (unsigned int offset_size) +{ + unsigned int index = CTF_FRE_OFFSET_FUNC_MAP_INDEX_MAX; + + switch (offset_size) + { + case CTF_FRAME_FRE_OFFSET_1B: + index = CTF_FRE_OFFSET_FUNC_MAP_INDEX_1B; + break; + case CTF_FRAME_FRE_OFFSET_2B: + index = CTF_FRE_OFFSET_FUNC_MAP_INDEX_2B; + break; + case CTF_FRAME_FRE_OFFSET_4B: + index = CTF_FRE_OFFSET_FUNC_MAP_INDEX_4B; + break; + default: + /* Not supported in CTF frame. */ + break; + } + + return index; +} + +/* Mapping from offset size to the output function to emit the value. */ + +static const +struct ctf_fre_offset_func_map +fre_offset_func_map[CTF_FRE_OFFSET_FUNC_MAP_INDEX_MAX+1] = +{ + { CTF_FRAME_FRE_OFFSET_1B, out_one }, + { CTF_FRAME_FRE_OFFSET_2B, out_two }, + { CTF_FRAME_FRE_OFFSET_4B, out_four }, + { -1, NULL } /* Not Supported in CTF frame. */ +}; + +/* CTF frame version specific operations access. */ + +static struct ctf_frame_version_ops ctf_frame_ver_ops; + +/* CTF frame (CTF_FRAME_VERSION_1) set FRE info. */ + +static unsigned char +ctf_frame_v1_set_fre_info (unsigned int base_reg, unsigned int num_offsets, + unsigned int offset_size) +{ + unsigned char fre_info; + fre_info = CTF_FRAME_V1_FRE_INFO (base_reg, num_offsets, offset_size); + return fre_info; +} + +/* CTF frame (CTF_FRAME_VERSION_1) set function info. */ +static unsigned char +ctf_frame_v1_set_func_info (unsigned int fre_type) +{ + unsigned char func_info; + func_info = CTF_FRAME_V1_FUNC_INFO (fre_type); + return func_info; +} + +/* CTF frame version specific operations setup. */ + +static void +ctf_frame_set_version (uint32_t ctf_frame_version __attribute__((unused))) +{ + ctf_frame_ver_ops.format_version = CTF_FRAME_VERSION_1; + + ctf_frame_ver_ops.set_fre_info = ctf_frame_v1_set_fre_info; + + ctf_frame_ver_ops.set_func_info = ctf_frame_v1_set_func_info; +} + +/* CTF frame set FRE info. */ + +static unsigned char +ctf_frame_set_fre_info (unsigned int base_reg, unsigned int num_offsets, + unsigned int offset_size) +{ + return ctf_frame_ver_ops.set_fre_info (base_reg, num_offsets, + offset_size); +} + +/* CTF frame set func info. */ + +ATTRIBUTE_UNUSED static unsigned char +ctf_frame_set_func_info (unsigned int fre_type) +{ + return ctf_frame_ver_ops.set_func_info (fre_type); +} + +/* Get the number of CTF FDEs for the current file. */ + +static unsigned int +get_num_ctf_fdes (void); + +/* Get the number of CTF frame row entries for the current file. */ + +static unsigned int +get_num_ctf_fres (void); + +/* Get CFA base register ID as represented in CTF frame row entry. */ + +static unsigned int +get_fre_base_reg_id (struct ctf_frame_row_entry *ctf_fre) +{ + unsigned int cfi_insn_cfa_base_reg = ctf_fre->cfa_base_reg; + unsigned fre_base_reg = CTF_FRAME_BASE_REG_SP; + + if (cfi_insn_cfa_base_reg == CTF_FRAME_CFA_FP_REG) + fre_base_reg = CTF_FRAME_BASE_REG_FP; + + /* Only one bit is reserved in CTF_FRAME_VERSION_1. */ + gas_assert (fre_base_reg == CTF_FRAME_BASE_REG_SP + || fre_base_reg == CTF_FRAME_BASE_REG_FP); + + return fre_base_reg; +} + +/* Get number of offsets necessary for the CTF frame row entry. */ + +static unsigned int +get_fre_num_offsets (struct ctf_frame_row_entry *ctf_fre) +{ + /* Atleast 1 must always be present (to recover CFA). */ + unsigned int fre_num_offsets = 1; + + if (ctf_fre->bp_loc == CTF_FRE_ELEM_LOC_STACK) + fre_num_offsets++; +#ifdef CTFF_ROW_ENTRY_RA_TRACKING + if (ctf_fre->ra_loc == CTF_FRE_ELEM_LOC_STACK) + fre_num_offsets++; +#endif + return fre_num_offsets; +} + +/* Get the minimum necessary offset size (in bytes) for this CTF frame + row entry. */ + +static unsigned int +ctf_frame_get_fre_offset_size (struct ctf_frame_row_entry *ctf_fre) +{ + unsigned int max_offset_size = 0; + unsigned int cfa_offset_size = 0; + unsigned int bp_offset_size = 0; + unsigned int ra_offset_size = 0; + + unsigned int fre_offset_size = 0; + + /* What size of offsets appear in this frame row entry. */ + cfa_offset_size = get_offset_size_in_bytes (ctf_fre->cfa_offset); + if (ctf_fre->bp_loc == CTF_FRE_ELEM_LOC_STACK) + bp_offset_size = get_offset_size_in_bytes (ctf_fre->bp_offset); +#ifdef CTFF_ROW_ENTRY_RA_TRACKING + if (ctf_frame_ra_tracking_p () + && ctf_fre->ra_loc == CTF_FRE_ELEM_LOC_STACK) + ra_offset_size = get_offset_size_in_bytes (ctf_fre->ra_offset); +#endif + + /* Get the maximum size needed to represent the offsets. */ + max_offset_size = cfa_offset_size; + if (bp_offset_size > max_offset_size) + max_offset_size = bp_offset_size; + if (ra_offset_size > max_offset_size) + max_offset_size = ra_offset_size; + + gas_assert (max_offset_size); + + switch (max_offset_size) + { + case 1: + fre_offset_size = CTF_FRAME_FRE_OFFSET_1B; + break; + case 2: + fre_offset_size = CTF_FRAME_FRE_OFFSET_2B; + break; + case 4: + fre_offset_size = CTF_FRAME_FRE_OFFSET_4B; + break; + default: + /* Offset of size 8 bytes is not supported in CTF frame format + version 1. */ + printf (_("CTF Unsupported offset value\n")); + abort (); + break; + } + + return fre_offset_size; +} + +#if CTF_FRE_START_ADDR_OPT + +/* FIXME add comments. Cumulative expression = diff OP_absent width. */ + +static void +create_width_expression (expressionS *cexp, symbolS *fre_pc_begin, + symbolS *fde_start_address, + symbolS *fde_end_address) +{ + expressionS diff; + expressionS width; + + /* diff expression stores the FDE start address offset from the start PC + of function. */ + diff.X_op = O_subtract; + diff.X_add_symbol = fre_pc_begin; + diff.X_op_symbol = fde_start_address; + diff.X_add_number = 0; + + /* width expressions stores the size of the function. This is used later + to determine the number of bytes to be used to encode the FRE start + address of each FRE of the function. */ + width.X_op = O_subtract; + width.X_add_symbol = fde_end_address; + width.X_op_symbol = fde_start_address; + width.X_add_number = 0; + + cexp->X_op = O_absent; + cexp->X_add_symbol = make_expr_symbol (&diff); + cexp->X_op_symbol = make_expr_symbol (&width); + cexp->X_add_number = 0; +} +#endif + +static void +output_ctf_frame_row_entry (symbolS *fde_start_addr, + symbolS *fde_end_addr, + struct ctf_frame_row_entry *ctf_fre) +{ + unsigned char fre_info; + unsigned int fre_num_offsets; + unsigned int fre_offset_size; + unsigned int fre_base_reg; + expressionS exp; + unsigned int fre_addr_size; + + unsigned int index = 0; + unsigned int fre_write_offsets = 0; + + fre_addr_size = 4; /* 4 bytes by default. FIXME tie it to fre_type? */ + + /* FRE Start Address. */ +#if CTF_FRE_START_ADDR_OPT + create_width_expression (&exp, ctf_fre->pc_begin, fde_start_addr, fde_end_addr); + frag_grow (fre_addr_size); + frag_var (rs_ctf_fre, fre_addr_size, 0, (relax_substateT) 0, + make_expr_symbol (&exp), 0, (char *) frag_now); +#else + gas_assert (fde_end_addr); + exp.X_op = O_subtract; + exp.X_add_symbol = ctf_fre->pc_begin; /* to. */ + exp.X_op_symbol = fde_start_addr; /* from. */ + exp.X_add_number = 0; + emit_expr (&exp, fre_addr_size); +#endif + + /* Create the fre_info using the CFA base register, number of offsets and max + size of offset in this frame row entry. */ + fre_base_reg = get_fre_base_reg_id (ctf_fre); + fre_num_offsets = get_fre_num_offsets (ctf_fre); + fre_offset_size = ctf_frame_get_fre_offset_size (ctf_fre); + fre_info = ctf_frame_set_fre_info (fre_base_reg, fre_num_offsets, + fre_offset_size); + out_one (fre_info); + + index = ctf_fre_offset_func_map_index (fre_offset_size); + gas_assert (index < CTF_FRE_OFFSET_FUNC_MAP_INDEX_MAX); + + /* Write out the offsets in order - cfa, bp, ra. */ + fre_offset_func_map[index].out_func (ctf_fre->cfa_offset); + fre_write_offsets++; + + if (ctf_fre->bp_loc == CTF_FRE_ELEM_LOC_STACK) + { + fre_offset_func_map[index].out_func (ctf_fre->bp_offset); + fre_write_offsets++; + } + +#ifdef CTFF_ROW_ENTRY_RA_TRACKING + if (ctf_fre->ra_loc == CTF_FRE_ELEM_LOC_STACK) + { + fre_offset_func_map[index].out_func (ctf_fre->ra_offset); + fre_write_offsets++; + } +#endif + /* Check if the expected number offsets have been written out + in this FRE. */ + gas_assert (fre_write_offsets == fre_num_offsets); +} + +static void +output_ctf_frame_funcdesc (symbolS *start_of_fre_section, + symbolS *fre_symbol, + struct ctf_func_desc_entry *ctf_fde) +{ + expressionS exp; + unsigned int addr_size; + symbolS *dw_fde_start_addrS, *dw_fde_end_addrS; + + addr_size = CTF_FRAME_RELOC_SIZE; + dw_fde_start_addrS = get_dw_fde_start_addrS (ctf_fde->dw_fde); + dw_fde_end_addrS = get_dw_fde_end_addrS (ctf_fde->dw_fde); + + /* Start address of the function. */ + exp.X_op = O_subtract; + exp.X_add_symbol = dw_fde_start_addrS; /* to location. */ + exp.X_op_symbol = symbol_temp_new_now (); /* from location. */ + exp.X_add_number = 0; + emit_expr (&exp, addr_size); + + /* Size of the function in bytes. */ + exp.X_op = O_subtract; + exp.X_add_symbol = dw_fde_end_addrS; + exp.X_op_symbol = dw_fde_start_addrS; + exp.X_add_number = 0; + emit_expr (&exp, addr_size); + + /* Offset to the first frame row entry. */ + exp.X_op = O_subtract; + exp.X_add_symbol = fre_symbol; /* Minuend. */ + exp.X_op_symbol = start_of_fre_section; /* Subtrahend. */ + exp.X_add_number = 0; + emit_expr (&exp, addr_size); + + /* Number of FREs. */ + out_four (ctf_fde->num_fres); + + /* Function info. */ +#if CTF_FRE_START_ADDR_OPT + expressionS width; + width.X_op = O_subtract; + width.X_add_symbol = dw_fde_end_addrS; + width.X_op_symbol = dw_fde_start_addrS; + width.X_add_number = 0; + frag_grow (1); /* Size of func info is unsigned char. */ + frag_var (rs_ctf_fre, 1, 0, (relax_substateT) 0, + make_expr_symbol (&width), 0, (char *) frag_now); +#else + unsigned char func_info; + func_info = ctf_frame_set_func_info (CTF_FRAME_ROW_ENTRY_TYPE_ADDR4); + out_one (func_info); +#endif +} + +static void +output_ctf_frame_internal (void) +{ + expressionS exp; + unsigned int i = 0; + + symbolS *end_of_frame_hdr; + symbolS *end_of_frame_section; + symbolS *start_of_func_desc_section; + symbolS *start_of_fre_section; + struct ctf_func_desc_entry *ctf_fde; + struct ctf_frame_row_entry *ctf_fre; + unsigned char abi_arch = 0; + int fixed_bp_offset = CTF_FRAME_CFA_FIXED_FP_INVALID; + int fixed_ra_offset = CTF_FRAME_CFA_FIXED_RA_INVALID; + unsigned int addr_size; + + addr_size = CTF_FRAME_RELOC_SIZE; + + /* The function desciptor entries as dumped by the assembler are not + sorted on PCs. */ + unsigned char ctf_frame_flags = 0; + ctf_frame_flags |= !CTF_FRAME_F_FDE_SORTED; + + unsigned int num_fdes = get_num_ctf_fdes (); + unsigned int num_fres = get_num_ctf_fres (); + symbolS **fre_symbols = XNEWVEC (symbolS *, num_fres); + for (i = 0; i < num_fres; i++) + fre_symbols[i] = symbol_temp_make (); + + end_of_frame_hdr = symbol_temp_make (); + start_of_fre_section = symbol_temp_make (); + start_of_func_desc_section = symbol_temp_make (); + end_of_frame_section = symbol_temp_make (); + + /* Output the preamble of CTF frame section. */ + out_two (CTF_FRAME_MAGIC); + out_one (CTF_FRAME_VERSION); + out_one (ctf_frame_flags); + /* abi/arch. */ +#ifdef ctf_frame_get_abi_arch + abi_arch = ctf_frame_get_abi_arch (); +#endif + gas_assert (abi_arch); + out_one (abi_arch); + + /* Offset for the BP register from CFA. Neither of the AMD64 or AAPCS64 + ABIs have a fixed offset for the BP register from the CFA. This may be + useful in future (but not without additional support in the toolchain) + for specialized handling/encoding for cases where, for example, + -fno-omit-frame-pointer is used. */ + out_one (fixed_bp_offset); + + /* Offset for the return address from CFA is fixed for some ABIs + (e.g., AMD64), output a zero otherwise. */ +#ifdef CTFF_ROW_ENTRY_RA_TRACKING + if (!ctf_frame_ra_tracking_p ()) + fixed_ra_offset = ctf_frame_cfa_ra_offset (); +#endif + out_one (fixed_ra_offset); + + out_four (num_fdes); /* Number of FDEs. */ + out_four (num_fres); /* Number of FREs. */ + + /* FRE sub-section len. */ + exp.X_op = O_subtract; + exp.X_add_symbol = end_of_frame_section; + exp.X_op_symbol = start_of_fre_section; + exp.X_add_number = 0; + emit_expr (&exp, addr_size); + + /* Offset of Function Index sub-section. */ + exp.X_op = O_subtract; + exp.X_add_symbol = end_of_frame_hdr; + exp.X_op_symbol = start_of_func_desc_section; + exp.X_add_number = 0; + emit_expr (&exp, addr_size); + + /* Offset of FRE sub-section. */ + exp.X_op = O_subtract; + exp.X_add_symbol = start_of_fre_section; + exp.X_op_symbol = end_of_frame_hdr; + exp.X_add_number = 0; + emit_expr (&exp, addr_size); + + symbol_set_value_now (end_of_frame_hdr); + symbol_set_value_now (start_of_func_desc_section); + + /* Output the CTF frame function descriptor entries. */ + i = 0; + for (ctf_fde = all_ctf_fde_data; ctf_fde; ctf_fde = ctf_fde->next) + { + output_ctf_frame_funcdesc (start_of_fre_section, + fre_symbols[i], ctf_fde); + i += ctf_fde->num_fres; + } + + symbol_set_value_now (start_of_fre_section); + + /* Output the CTF frame FREs. */ + i = 0; + ctf_fde = all_ctf_fde_data; + + for (ctf_fde = all_ctf_fde_data; ctf_fde; ctf_fde = ctf_fde->next) + { + for (ctf_fre = ctf_fde->ctf_fres; ctf_fre; ctf_fre = ctf_fre->next) + { + symbol_set_value_now (fre_symbols[i]); + output_ctf_frame_row_entry (get_dw_fde_start_addrS (ctf_fde->dw_fde), + get_dw_fde_end_addrS (ctf_fde->dw_fde), + ctf_fre); + i++; + } + } + + symbol_set_value_now (end_of_frame_section); + + gas_assert (i == num_fres); + + free (fre_symbols); + fre_symbols = NULL; +} + +/* List of CTF FDE entries. */ + +struct ctf_func_desc_entry *all_ctf_fde_data; + +/* Tail of the list to add to. */ + +static struct ctf_func_desc_entry **last_ctf_fde_data = &all_ctf_fde_data; + +static unsigned int +get_num_ctf_fdes (void) +{ + struct ctf_func_desc_entry *ctf_fde; + unsigned int total_fdes = 0; + + for (ctf_fde = all_ctf_fde_data; ctf_fde ; ctf_fde = ctf_fde->next) + total_fdes++; + + return total_fdes; +} + +/* Get the total number of CTF frame row entries across the FDEs. */ + +static unsigned int +get_num_ctf_fres (void) +{ + struct ctf_func_desc_entry *ctf_fde; + unsigned int total_fres = 0; + + for (ctf_fde = all_ctf_fde_data; ctf_fde ; ctf_fde = ctf_fde->next) + total_fres += ctf_fde->num_fres; + + return total_fres; +} + +/* Allocate a CTF FDE. */ + +static struct ctf_func_desc_entry* +ctf_fde_alloc (void) +{ + struct ctf_func_desc_entry *ctf_fde = XCNEW (struct ctf_func_desc_entry); + return ctf_fde; +} + +/* Link the CTF FDE in. */ + +static int +ctf_fde_link (struct ctf_func_desc_entry *ctf_fde) +{ + *last_ctf_fde_data = ctf_fde; + last_ctf_fde_data = &ctf_fde->next; + + return 0; +} + +/* Free up the CTF FDE. */ + +static void +ctf_fde_free (struct ctf_func_desc_entry *ctf_fde) +{ + XDELETE (ctf_fde); + ctf_fde = NULL; +} + +/* CTF frame translation context functions. */ + +/* Allocate a new CTF frame translation context. */ + +static struct ctf_frame_xlate_ctx* +ctf_frame_xlate_ctx_alloc (void) +{ + struct ctf_frame_xlate_ctx* xlate_ctx = XCNEW (struct ctf_frame_xlate_ctx); + return xlate_ctx; +} + +/* Initialize the given CTF frame translation context. */ + +static void +ctf_frame_xlate_ctx_init (struct ctf_frame_xlate_ctx *xlate_ctx) +{ + xlate_ctx->dw_fde = NULL; + xlate_ctx->first_fre = NULL; + xlate_ctx->last_fre = NULL; + xlate_ctx->cur_fre = NULL; + xlate_ctx->remember_fre = NULL; + xlate_ctx->num_xlate_fres = 0; +} + +/* Cleanup the given CTF frame translation context. */ + +static void +ctf_frame_xlate_ctx_cleanup (struct ctf_frame_xlate_ctx *xlate_ctx) +{ + struct ctf_frame_row_entry *fre, *fre_next; + + if (xlate_ctx->num_xlate_fres) + { + fre = xlate_ctx->first_fre; + while (fre) + { + fre_next = fre->next; + XDELETE (fre); + fre = fre_next; + } + } + + ctf_frame_xlate_ctx_init (xlate_ctx); +} + +/* Transfer the state from the CTF frame translation context to the CTF FDE. */ + +static void +ctf_frame_xlate_ctx_finalize (struct ctf_frame_xlate_ctx *xlate_ctx, + struct ctf_func_desc_entry *ctf_fde) +{ + ctf_fde->dw_fde = xlate_ctx->dw_fde; + ctf_fde->ctf_fres = xlate_ctx->first_fre; + ctf_fde->num_fres = xlate_ctx->num_xlate_fres; +} + +static struct ctf_frame_row_entry* +ctf_frame_row_entry_new (void) +{ + struct ctf_frame_row_entry *fre = XCNEW (struct ctf_frame_row_entry); + fre->merge_candidate = true; + + return fre; +} + +/* Add the given FRE in the list of frame row entries in the given FDE + translation context. */ + +static void +ctf_frame_xlate_ctx_add_fre (struct ctf_frame_xlate_ctx *xlate_ctx, + struct ctf_frame_row_entry *fre) +{ + gas_assert (xlate_ctx && fre); + + /* Add the frame row entry. */ + if (!xlate_ctx->first_fre) + xlate_ctx->first_fre = fre; + else if (xlate_ctx->last_fre) + xlate_ctx->last_fre->next = fre; + + xlate_ctx->last_fre = fre; + + /* Keep track of the total number of CTF frame row entries. */ + xlate_ctx->num_xlate_fres++; +} + +/* A CTF frame row entry is self-sufficient in terms of unwind information for + a given PC. It contains information assimilated from multiple CFI + instructions, and hence, a new CTF FRE is initialized with the data from + the previous known FRE, if any. + + Understandably, not all information (especially the instruction begin + and end boundaries) needs to be relayed. Hence, the caller of this API + must set the pc_begin and pc_end as applicable. */ + +static void +ctf_frame_row_entry_initialize (struct ctf_frame_row_entry *cur_fre, + struct ctf_frame_row_entry *prev_fre) +{ + gas_assert (prev_fre); + cur_fre->cfa_base_reg = prev_fre->cfa_base_reg; + cur_fre->cfa_offset = prev_fre->cfa_offset; + cur_fre->bp_loc = prev_fre->bp_loc; + cur_fre->bp_offset = prev_fre->bp_offset; + cur_fre->ra_loc = prev_fre->ra_loc; + cur_fre->ra_offset = prev_fre->ra_offset; +} + +static int +ctf_xlate_do_advance_loc (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) +{ + struct ctf_frame_row_entry *last_fre = xlate_ctx->last_fre; + /* Get the scratchpad FRE currently being updated as the cfi_insn's + get interpreted. This FRE eventually gets linked in into the + list of FREs for the specific function. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + + if (cur_fre) + { + if (!cur_fre->merge_candidate) + { + ctf_fre_set_end_addr (cur_fre, cfi_insn->u.ll.lab2); + + ctf_frame_xlate_ctx_add_fre (xlate_ctx, cur_fre); + last_fre = xlate_ctx->last_fre; + + xlate_ctx->cur_fre = ctf_frame_row_entry_new (); + cur_fre = xlate_ctx->cur_fre; + + if (last_fre) + ctf_frame_row_entry_initialize (cur_fre, last_fre); + } + else + { + ctf_fre_set_end_addr (last_fre, cfi_insn->u.ll.lab2); + gas_assert (last_fre->merge_candidate == false); + } + } + else + { + xlate_ctx->cur_fre = ctf_frame_row_entry_new (); + cur_fre = xlate_ctx->cur_fre; + } + + gas_assert (cur_fre); + ctf_fre_set_begin_addr (cur_fre, cfi_insn->u.ll.lab2); + + return 0; +} + +static int +ctf_xlate_do_def_cfa (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) + +{ + /* Get the scratchpad FRE. This FRE will eventually get linked in. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + if (!cur_fre) + { + xlate_ctx->cur_fre = ctf_frame_row_entry_new (); + cur_fre = xlate_ctx->cur_fre; + ctf_fre_set_begin_addr (cur_fre, + get_dw_fde_start_addrS (xlate_ctx->dw_fde)); + } + /* Define the current CFA rule to use the provided register and + offset. */ + ctf_fre_set_cfa_base_reg (cur_fre, cfi_insn->u.ri.reg); + ctf_fre_set_cfa_offset (cur_fre, cfi_insn->u.ri.offset); + cur_fre->merge_candidate = false; + + return 0; +} + +static int +ctf_xlate_do_def_cfa_register (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) +{ + struct ctf_frame_row_entry *last_fre = xlate_ctx->last_fre; + /* Get the scratchpad FRE. This FRE will eventually get linked in. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + gas_assert (cur_fre); + /* Define the current CFA rule to use the provided register (but to + keep the old offset). */ + ctf_fre_set_cfa_base_reg (cur_fre, cfi_insn->u.ri.reg); + ctf_fre_set_cfa_offset (cur_fre, last_fre->cfa_offset); + cur_fre->merge_candidate = false; + + return 0; +} + +static int +ctf_xlate_do_def_cfa_offset (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) +{ + /* The scratchpad FRE currently being updated with each cfi_insn + being interpreted. This FRE eventually gets linked in into the + list of FREs for the specific function. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + + gas_assert (cur_fre); + /* Define the current CFA rule to use the provided offset (but to keep + the old register). */ + ctf_fre_set_cfa_offset (cur_fre, cfi_insn->u.i); + cur_fre->merge_candidate = false; + + return 0; +} + +static int +ctf_xlate_do_offset (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) +{ + /* The scratchpad FRE currently being updated with each cfi_insn + being interpreted. This FRE eventually gets linked in into the + list of FREs for the specific function. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + + gas_assert (cur_fre); + /* Change the rule for the register indicated by the register number to + be the specified offset. */ + if (cfi_insn->u.r == CTF_FRAME_CFA_FP_REG) + { + gas_assert (!cur_fre->base_reg); + ctf_fre_set_bp_track (cur_fre, cfi_insn->u.ri.offset); + cur_fre->merge_candidate = false; + } +#ifdef CTFF_ROW_ENTRY_RA_TRACKING + else if (ctf_frame_ra_tracking_p () + && cfi_insn->u.r == CTF_FRAME_CFA_RA_REG) + { + ctf_fre_set_ra_track (cur_fre, cfi_insn->u.ri.offset); + cur_fre->merge_candidate = false; + } +#endif + /* This is used to track changes to non-rsp registers, skip all others + except FP / RA for now. */ + return 0; +} + +static int +ctf_xlate_do_remember_state (struct ctf_frame_xlate_ctx *xlate_ctx) +{ + struct ctf_frame_row_entry *last_fre = xlate_ctx->last_fre; + + if (!xlate_ctx->remember_fre) + xlate_ctx->remember_fre = ctf_frame_row_entry_new (); + ctf_frame_row_entry_initialize (xlate_ctx->remember_fre, last_fre); + + return 0; +} + +static int +ctf_xlate_do_restore_state (struct ctf_frame_xlate_ctx *xlate_ctx) +{ + /* The scratchpad FRE currently being updated with each cfi_insn + being interpreted. This FRE eventually gets linked in into the + list of FREs for the specific function. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + + gas_assert (xlate_ctx->remember_fre); + gas_assert (cur_fre && cur_fre->merge_candidate); + + /* Get the CFA state from the DW_CFA_remember_state insn. */ + ctf_frame_row_entry_initialize (cur_fre, xlate_ctx->remember_fre); + /* The PC boundaries of the current CTF FRE are updated + via other machinery. */ + cur_fre->merge_candidate = false; + return 0; +} + +static int +ctf_xlate_do_restore (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) +{ + struct ctf_frame_row_entry *cie_fre = xlate_ctx->first_fre; + /* The scratchpad FRE currently being updated with each cfi_insn + being interpreted. This FRE eventually gets linked in into the + list of FREs for the specific function. */ + struct ctf_frame_row_entry *cur_fre = xlate_ctx->cur_fre; + + /* Change the rule for the indicated register to the rule assigned to + it by the initial_instructions in the CIE. */ + gas_assert (cie_fre); + /* CTF FREs track only CFA and FP / RA for backtracing purposes; + skip the other .cfi_restore directives. */ + if (cfi_insn->u.r == CTF_FRAME_CFA_FP_REG) + { + gas_assert (cur_fre); + cur_fre->bp_loc = cie_fre->bp_loc; + cur_fre->bp_offset = cie_fre->bp_offset; + cur_fre->merge_candidate = false; + } +#ifdef CTFF_ROW_ENTRY_RA_TRACKING + else if (ctf_frame_ra_tracking_p () + && cfi_insn->u.r == CTF_FRAME_CFA_RA_REG) + { + gas_assert (cur_fre); + cur_fre->ra_loc = cie_fre->ra_loc; + cur_fre->ra_offset = cie_fre->ra_offset; + cur_fre->merge_candidate = false; + } +#endif + return 0; +} + +/* Process CFI_INSN and update the translation context with the FRE + information. + + Returns an error code if CFI_INSN is not successfully processed. */ + +static int +ctf_frame_do_cfi_insn (struct ctf_frame_xlate_ctx *xlate_ctx, + struct cfi_insn_data *cfi_insn) +{ + int err = 0; + + /* Atleast one cfi_insn per FDE is expected. */ + gas_assert (cfi_insn); + int op = cfi_insn->insn; + + switch (op) + { + case DW_CFA_advance_loc: + err = ctf_xlate_do_advance_loc (xlate_ctx, cfi_insn); + break; + case DW_CFA_def_cfa: + err = ctf_xlate_do_def_cfa (xlate_ctx, cfi_insn); + break; + case DW_CFA_def_cfa_register: + err = ctf_xlate_do_def_cfa_register (xlate_ctx, cfi_insn); + break; + case DW_CFA_def_cfa_offset: + err = ctf_xlate_do_def_cfa_offset (xlate_ctx, cfi_insn); + break; + case DW_CFA_offset: + err = ctf_xlate_do_offset (xlate_ctx, cfi_insn); + break; + case DW_CFA_remember_state: + err = ctf_xlate_do_remember_state (xlate_ctx); + break; + case DW_CFA_restore_state: + err = ctf_xlate_do_restore_state (xlate_ctx); + break; + case DW_CFA_restore: + err = ctf_xlate_do_restore (xlate_ctx, cfi_insn); + break; + case DW_CFA_undefined: + case DW_CFA_same_value: + break; + default: + { + /* Other CFI opcodes are not processed at this time. */ + err = 1; + // printf (_("CTF Unsupported or unknown Dwarf CFI number: %#x\n"), op); + } + } + + return err; +} + +static int ctf_frame_do_fde (struct ctf_frame_xlate_ctx *xlate_ctx, + const struct fde_entry *dw_fde) +{ + struct cfi_insn_data *cfi_insn; + + xlate_ctx->dw_fde = dw_fde; + + /* Iterate over the CFIs and create CTF FREs. */ + for (cfi_insn = dw_fde->data; cfi_insn; cfi_insn = cfi_insn->next) + { + /* Translate each CFI, and buffer the state in translation context. */ + if (ctf_frame_do_cfi_insn (xlate_ctx, cfi_insn)) + { + /* Skip generating CTF unwind info for the function if any offending + CFI is encountered by ctf_frame_do_cfi_insn (). */ + /* FIXME - get a detailed look at the individual cases. */ + // printf ("WATCH THIS ONE! ctf_frame_do_cfi_insn skipped. \n"); + return 1; /* Error. */ + } + } + + /* No errors encountered. */ + + /* Link in the scratchpad FRE that the last few CFI insns helped create. */ + if (xlate_ctx->cur_fre) + { + ctf_frame_xlate_ctx_add_fre (xlate_ctx, xlate_ctx->cur_fre); + xlate_ctx->cur_fre = NULL; + } + /* Designate the end of the last CTF FRE. */ + if (xlate_ctx->last_fre) + { + xlate_ctx->last_fre->pc_end + = get_dw_fde_end_addrS (xlate_ctx->dw_fde); + } + + return 0; +} + +/* Create CTF frame unwind info for all functions. + + This function consumes the already generated FDEs (by dw2gencfi) and + generates unwind data in CTF format. */ + +static void create_ctf_frame_all (void) +{ + struct fde_entry *dw_fde = NULL; + struct ctf_func_desc_entry *ctf_fde = NULL; + + struct ctf_frame_xlate_ctx *xlate_ctx = ctf_frame_xlate_ctx_alloc (); + + for (dw_fde = all_fde_data; dw_fde ; dw_fde = dw_fde->next) + { + /* FIXME - enable this assert once the compiler is generating + .cfi_sections with .ctf_frame enabled. */ + // gas_assert ((fde->sections & CFI_EMIT_ctf_frame)); + ctf_fde = ctf_fde_alloc (); + /* Initialize the translation context with information anew. */ + ctf_frame_xlate_ctx_init (xlate_ctx); + + /* Process and link CTF Frame FDEs if no error. Also skip adding a CTF + FDE if it does not contain any CTF FREs. There is little use of a CTF + FDE if there is no unwind information about the function. */ + int err = ctf_frame_do_fde (xlate_ctx, dw_fde); + if (err || xlate_ctx->num_xlate_fres == 0) + { + ctf_frame_xlate_ctx_cleanup (xlate_ctx); + ctf_fde_free (ctf_fde); + } + else + { + /* All done. Transfer the state from the CTF frame translation + context to the CTF FDE. */ + ctf_frame_xlate_ctx_finalize (xlate_ctx, ctf_fde); + ctf_fde_link (ctf_fde); + } + } +} + +void output_ctf_frame (segT ctf_frame_seg) +{ + (void) ctf_frame_seg; + + /* Setup the version specific access functions. */ + ctf_frame_set_version (CTF_FRAME_VERSION_1); + + /* Process all fdes and create CTF unwind information. */ + create_ctf_frame_all (); + + output_ctf_frame_internal (); +} + +#else /* support_ctf_frame_p */ + +/* Callback to create the abi/arch identifier for CTF frame section. */ + +unsigned char +ctf_frame_get_abi_arch_callback (const char *tarch __attribute__((unused)), + int big_endian_p __attribute__((unused))) +{ + unsigned char ctf_frame_abi_arch = 0; + + return ctf_frame_abi_arch; +} + +void output_ctf_frame (segT ctf_frame_seg __attribute__((unused))) +{ +} + +#endif /* support_ctf_frame_p */ diff --git a/gas/gen-ctf-frame.h b/gas/gen-ctf-frame.h new file mode 100644 index 00000000000..42a95866e81 --- /dev/null +++ b/gas/gen-ctf-frame.h @@ -0,0 +1,142 @@ +/* gen-ctf-frame.h - Support for generating CTF frame. + Copyright (C) 2021 Free Software Foundation, Inc. + + This file is part of GAS, the GNU Assembler. + + GAS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GAS; see the file COPYING. If not, write to the Free + Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA + 02110-1301, USA. */ + +#ifndef GENCTFFRAME_H +#define GENCTFFRAME_H + +#define CTF_FRE_ELEM_LOC_REG 0 +#define CTF_FRE_ELEM_LOC_STACK 1 + +/* CTF frame row entry (FRE). + + A frame row entry is a slice of the frame and can be valid for a set of + program instructions. It keeps all information needed to retrieve the CFA + and the Return Address (RA) if tracked. + + A frame row entry effectively stores accumulated information gathered by + interpreting multiple CFI instructions. More precisely, it is a + self-sufficient record in its own right. Only the subset of information + necessary for unwinding is stored: Given a PC, how to retrieve the CFA and + the RA. +*/ + +struct ctf_frame_row_entry +{ + /* A linked list. */ + struct ctf_frame_row_entry *next; + + /* Start and end of the frame row entry. */ + symbolS *pc_begin; + symbolS *pc_end; + + /* A frame row entry is a merge candidate if new information can be updated + on it. */ + bool merge_candidate; + + /* Track CFA base (architectural) register ID. */ + unsigned int cfa_base_reg; + /* Offset from the CFA base register for recovering CFA. */ + offsetT cfa_offset; + + /* Track the other register used as base register for CFA. Specify whether + it is in register or memory. */ + unsigned int base_reg; + unsigned int bp_loc; + /* If the other register is stashed on stack, note the offset. */ + offsetT bp_offset; + + /* Track RA location. Specify whether it is in register or memory. */ + unsigned int ra_loc; + /* If RA is stashed on stack, note the offset. */ + offsetT ra_offset; +}; + +/* CTF Function Description Entry. */ + +struct ctf_func_desc_entry +{ + /* A linked list. */ + struct ctf_func_desc_entry *next; + + /* Reference to the FDE created from CFI in dw2gencfi. Some information + like the start_address and the segment is made available via this + member. */ + const struct fde_entry *dw_fde; + + /* Reference to the first FRE for this function. */ + struct ctf_frame_row_entry *ctf_fres; + + unsigned int num_fres; +}; + +/* CTF frame Function Description Entry Translation Context. */ + +struct ctf_frame_xlate_ctx +{ + /* Reference to the FDE created from CFI in dw2gencfi. Information + like the FDE start_address, end_address and the cfi insns are + made available via this member. */ + const struct fde_entry *dw_fde; + + /* List of FREs in the current FDE translation context, bounded by first_fre + and last_fre. */ + + /* Keep track of the first FRE for the purpose of restoring state if + necessary (for DW_CFA_restore). */ + struct ctf_frame_row_entry *first_fre; + /* The last FRE in the list. */ + struct ctf_frame_row_entry *last_fre; + + /* The current FRE under construction. */ + struct ctf_frame_row_entry *cur_fre; + /* Remember FRE for an eventual restore. */ + struct ctf_frame_row_entry *remember_fre; + + unsigned num_xlate_fres; +}; + +/* Callback to create the abi/arch identifier for CTF frame section. */ + +unsigned char +ctf_frame_get_abi_arch_callback (const char *target_arch, + int big_endian_p); + +/* The list of all FDEs with data in CTF internal representation. */ + +extern struct ctf_func_desc_entry *all_ctf_fde_data; + +/* CTF frame version specific operations structure. */ + +struct ctf_frame_version_ops +{ + unsigned char format_version; /* CTF frame format version. */ + /* set CTF Frame FRE info. */ + unsigned char (*set_fre_info) (unsigned int, unsigned int, unsigned int); + /* set CTF Frame Func info. */ + unsigned char (*set_func_info) (unsigned int); +}; + +/* Generate CTF unwind info and prepare contents for the output. + outout_ctf_frame () is called at the end of file. */ + +extern void output_ctf_frame (segT ctf_frame_seg); + +#endif /* GENCTFFRAME_H */ + diff --git a/gas/write.c b/gas/write.c index f607562794a..a29fc46190f 100644 --- a/gas/write.c +++ b/gas/write.c @@ -487,6 +487,10 @@ cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP) dwarf2dbg_convert_frag (fragP); break; + case rs_ctf_fre: + ctf_frame_convert_frag (fragP); + break; + case rs_machine_dependent: md_convert_frag (stdoutput, sec, fragP); @@ -2788,6 +2792,11 @@ relax_segment (struct frag *segment_frag_root, segT segment, int pass) address += dwarf2dbg_estimate_size_before_relax (fragP); break; + case rs_ctf_fre: + /* Initial estimate can be set to atleast 1 byte. */ + address += ctf_frame_estimate_size_before_relax (fragP); + break; + default: BAD_CASE (fragP->fr_type); break; @@ -3131,6 +3140,10 @@ relax_segment (struct frag *segment_frag_root, segT segment, int pass) growth = dwarf2dbg_relax_frag (fragP); break; + case rs_ctf_fre: + growth = ctf_frame_relax_frag (fragP); + break; + default: BAD_CASE (fragP->fr_type); break; -- 2.31.1