diff mbox series

Strange read for simple_object_elf_find_sections for .a file

Message ID 3653c711-d7e8-a3e5-1263-326ce1c177a2@suse.cz
State New
Headers show
Series Strange read for simple_object_elf_find_sections for .a file | expand

Commit Message

Martin Liška March 12, 2020, 10:36 a.m. UTC
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

Comments

Martin Liška March 12, 2020, 10:39 a.m. UTC | #1
The answer is simple:


diff --git a/lto-plugin/lto-plugin.c b/lto-plugin/lto-plugin.c
index fbe2c6885f0..83f1ac38d37 100644
--- a/lto-plugin/lto-plugin.c
+++ b/lto-plugin/lto-plugin.c
@@ -1033,6 +1033,7 @@ static int
  process_lto_section (void *data, const char *name, off_t offset, off_t len)
  {
    struct plugin_objfile *obj = (struct plugin_objfile *)data;
+  offset += obj->file->offset;
    if (strncmp (name, LTO_LTO_PREFIX, LTO_LTO_PREFIX_LEN) == 0)
      {
        if (offset != lseek (obj->file->fd, offset, SEEK_SET))

Sorry for the noise.
Martin
diff mbox series

Patch

From f12831ebf6ed68b569f6d0c6a9243dfa15ba3b40 Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@suse.cz>
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, &lto_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