public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* RFC: readelf: Explain why LLVM bitcode files cannot be read
@ 2022-08-19 14:20 Nick Clifton
  2022-08-19 16:06 ` Jeff Law
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Clifton @ 2022-08-19 14:20 UTC (permalink / raw)
  To: binutils

Hi Guys,

  When the LLVM compiler produces a bitcode object file it uses its own
  format, rather than ELF.  But since the file looks like a normal
  object file a user might try to run readelf on it:

    % echo "void lto_function(){}" > foo.c
    % clang -flto -O2 -c foo.c 
    % readelf -a foo.o
    readelf: Error: Not an ELF file - it has the wrong magic bytes at the start

  I am proposing the patch below so that readelf will produce a more
  helpful error message:

    % readelf -a foo.o 
    readelf: Error: This is a LLVM bitcode file - try using llvm-bcanalyzer

  One thing I am not sure about in the patch is the LLVM bitcode magic
  number.  I do not know if this is defined in a header file somewhere,
  nor if there might be more than one possible value for the magic.  I
  did not want to add any new dependencies to readelf however, so for
  now the patch just defines the number locally.

  Any comments/thoughts ?

Cheers
  Nick

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 1ec25239938..28e80e228cc 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -5700,8 +5700,30 @@ process_file_header (Filedata * filedata)
       || header->e_ident[EI_MAG2] != ELFMAG2
       || header->e_ident[EI_MAG3] != ELFMAG3)
     {
-      error
-	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
+#define LLVM_BC_MAG0 'B'
+#define LLVM_BC_MAG1 'C'
+#define LLVM_BC_MAG2 0xc0
+#define LLVM_BC_MAG3 0xde
+      /* LLVM bitcode files look like ordinary object files, but they are not
+	 in the ELF file format.  Let users know if this is the case.
+	 Note: if we had plugin support we could use the LLVMgold.so plugin
+	 to translate the file for us.  */
+      if (header->e_ident[EI_MAG0] == LLVM_BC_MAG0
+	  && header->e_ident[EI_MAG1] == LLVM_BC_MAG1
+	  && header->e_ident[EI_MAG2] == LLVM_BC_MAG2
+	  && header->e_ident[EI_MAG3] == LLVM_BC_MAG3)
+	{
+	  /* llvm-bcanalyzer does not handle archives...   */
+	  if (filedata->archive_file_size > 0)
+	    error
+	      (_("This is a LLVM bitcode file - try extracing and then using llvm-bcanalyzer\n"));
+	  else
+	    error
+	      (_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"));	    
+	}
+      else
+	error
+	  (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
       return false;
     }
 


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-08-24 10:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-19 14:20 RFC: readelf: Explain why LLVM bitcode files cannot be read Nick Clifton
2022-08-19 16:06 ` Jeff Law
2022-08-22 10:18   ` Nick Clifton
2022-08-23 15:26     ` Richard Earnshaw
2022-08-23 15:30       ` Nick Clifton
2022-08-23 20:35         ` Luis Machado
2022-08-24  9:24           ` Nick Clifton
2022-08-24 10:18             ` Luis Machado

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).