From patchwork Thu Mar 12 10:36:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 1253483 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48dQHY5L98z9sNg for ; Thu, 12 Mar 2020 21:36:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E0573942005; Thu, 12 Mar 2020 10:36:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id D87C8393FC3B for ; Thu, 12 Mar 2020 10:36:50 +0000 (GMT) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3BE69B2E3; Thu, 12 Mar 2020 10:36:49 +0000 (UTC) Subject: Strange read for simple_object_elf_find_sections for .a file From: =?utf-8?q?Martin_Li=C5=A1ka?= To: Richard Biener References: <1ab500be-a957-7dde-8bad-c94fbf8483ab@suse.cz> <4be8dd93-35cf-3155-2843-87a56fb774d9@suse.cz> <20200309201922.GI9796@kam.mff.cuni.cz> <5eeace6a-40cf-354f-238e-59a4740aa165@suse.cz> <20200310110929.GA48643@kam.mff.cuni.cz> <36d32a03-a4b5-1318-38b6-ece55fe7ed70@suse.cz> <78b445d1-ab4f-ad21-e3a1-aa791a15361c@suse.cz> <15eb771d-6d3a-b9e3-3349-cabfb123cdc6@suse.cz> Message-ID: <3653c711-d7e8-a3e5-1263-326ce1c177a2@suse.cz> Date: Thu, 12 Mar 2020 11:36:47 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <15eb771d-6d3a-b9e3-3349-cabfb123cdc6@suse.cz> Content-Language: en-US X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: GCC Patches , Jan Hubicka Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi. I noticed a strange error during parsing of .gnu.lto_.lto section in LTO plugin: $ cat bss.c int global_zero; int global_one = 1; int main() { return 0; } $ gcc -flto bss.c -c $ gcc bss.o read version 9.0 while: $ ar r x.a bss.o ar: creating x.a $ gcc x.a read version 13617.13368 $ objdump -s -j .gnu.lto_.lto.655b2ec59df60b19 x.a In archive x.a: bss.o: file format elf64-x86-64 Contents of section .gnu.lto_.lto.655b2ec59df60b19: 0000 09000000 01000100 ........ which is the same as objdump of bss.o Any idea why that happens? Thanks, Martin From f12831ebf6ed68b569f6d0c6a9243dfa15ba3b40 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Thu, 12 Mar 2020 11:30:17 +0100 Subject: [PATCH] Test x. --- lto-plugin/lto-plugin.c | 57 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/lto-plugin/lto-plugin.c b/lto-plugin/lto-plugin.c index c307fc871bf..fbe2c6885f0 100644 --- a/lto-plugin/lto-plugin.c +++ b/lto-plugin/lto-plugin.c @@ -90,6 +90,8 @@ along with this program; see the file COPYING3. If not see #define LTO_SECTION_PREFIX ".gnu.lto_.symtab" #define LTO_SECTION_PREFIX_LEN (sizeof (LTO_SECTION_PREFIX) - 1) +#define LTO_LTO_PREFIX ".gnu.lto_.lto" +#define LTO_LTO_PREFIX_LEN (sizeof (LTO_LTO_PREFIX) - 1) #define OFFLOAD_SECTION ".gnu.offload_lto_.opts" #define OFFLOAD_SECTION_LEN (sizeof (OFFLOAD_SECTION) - 1) @@ -1010,6 +1012,52 @@ process_offload_section (void *data, const char *name, off_t offset, off_t len) return 1; } +/* Structure that represents LTO ELF section with information + about the format. */ + +struct lto_section + { + int16_t major_version; + int16_t minor_version; + unsigned char slim_object: 1; + unsigned char compression: 4; + int32_t reserved0: 27; +}; + +struct lto_section lto_header; + + +/* Find and parse .lto section of an object file. */ + +static int +process_lto_section (void *data, const char *name, off_t offset, off_t len) +{ + struct plugin_objfile *obj = (struct plugin_objfile *)data; + if (strncmp (name, LTO_LTO_PREFIX, LTO_LTO_PREFIX_LEN) == 0) + { + if (offset != lseek (obj->file->fd, offset, SEEK_SET)) + goto err; + + ssize_t got = read (obj->file->fd, <o_header, + sizeof (struct lto_section)); + if (got != sizeof (struct lto_section)) + goto err; + + fprintf (stderr, "read version %d.%d\n", lto_header.major_version, + lto_header.minor_version); + + return 0; + } + + return 1; + +err: + if (message) + message (LDPL_FATAL, "%s: corrupt object file", obj->file->name); + obj->found = 0; + return 0; +} + /* Callback used by gold to check if the plugin will claim FILE. Writes the result in CLAIMED. */ @@ -1055,8 +1103,13 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) if (!obj.objfile && !err) goto err; - if (obj.objfile) - errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err); + if (obj.objfile) + { + errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err); + + if (!errmsg) + errmsg = simple_object_find_sections (obj.objfile, process_lto_section, &obj, &err); + } if (!obj.objfile || errmsg) { -- 2.25.1