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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  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
  0 siblings, 1 reply; 8+ messages in thread
From: Jeff Law @ 2022-08-19 16:06 UTC (permalink / raw)
  To: binutils



On 8/19/2022 8:20 AM, Nick Clifton via Binutils wrote:
> 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 ?
I think the same issue arises with golang -- its .o files are not ELF, 
but can be read by the golang variants of objdump, nm, etc. So you might 
want to look at doing something similar for golang as well.

Jeff



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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  2022-08-19 16:06 ` Jeff Law
@ 2022-08-22 10:18   ` Nick Clifton
  2022-08-23 15:26     ` Richard Earnshaw
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Clifton @ 2022-08-22 10:18 UTC (permalink / raw)
  To: Jeff Law, binutils

Hi Jeff,

> I think the same issue arises with golang -- its .o files are not ELF, but can be read by the golang variants of objdump, nm, etc. So you might want to look at doing 
> something similar for golang as well.

A great idea.  I have recoded the patch to create a new function
which checks the magic number, and if it is not ELF then it tests
for LLVM bitcode files and golang object files.  This time I have
put the magic numbers into a static array, so it should be easy to
add more sequences should we wish to do so.

Cheers
   Nick



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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  2022-08-22 10:18   ` Nick Clifton
@ 2022-08-23 15:26     ` Richard Earnshaw
  2022-08-23 15:30       ` Nick Clifton
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Earnshaw @ 2022-08-23 15:26 UTC (permalink / raw)
  To: Nick Clifton, Jeff Law, binutils



On 22/08/2022 11:18, Nick Clifton via Binutils wrote:
> Hi Jeff,
> 
>> I think the same issue arises with golang -- its .o files are not ELF, 
>> but can be read by the golang variants of objdump, nm, etc. So you 
>> might want to look at doing something similar for golang as well.
> 
> A great idea.  I have recoded the patch to create a new function
> which checks the magic number, and if it is not ELF then it tests
> for LLVM bitcode files and golang object files.  This time I have
> put the magic numbers into a static array, so it should be easy to
> add more sequences should we wish to do so.
> 
> Cheers
>    Nick
> 
> 

This fails to build on ubuntu (which defaults to -Werror=format-security):

/home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c: In function 
‘check_magic_number’:
/home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c:5738:6: error: 
format not a string literal and no format arguments 
[-Werror=format-security]
  5738 |      error (known_magic[i].ar_message);
       |      ^~~~~
/home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c:5740:6: error: 
format not a string literal and no format arguments 
[-Werror=format-security]
  5740 |      error (known_magic[i].obj_message);
       |      ^~~~~

You need to pass the output of those strings as a parameter to "%s".

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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  2022-08-23 15:26     ` Richard Earnshaw
@ 2022-08-23 15:30       ` Nick Clifton
  2022-08-23 20:35         ` Luis Machado
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Clifton @ 2022-08-23 15:30 UTC (permalink / raw)
  To: Richard Earnshaw, Jeff Law, binutils

Hi Richard,

> This fails to build on ubuntu (which defaults to -Werror=format-security):
> 
> /home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c:5738:6: error: format not a string literal and no format arguments [-Werror=format-security]

Yeah - I noticed this 10 minutes after I committed the original patch.
I have since applied a small update which fixes the problem, so if
you update the sources, the error should go away.

Cheers
   Nick


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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  2022-08-23 15:30       ` Nick Clifton
@ 2022-08-23 20:35         ` Luis Machado
  2022-08-24  9:24           ` Nick Clifton
  0 siblings, 1 reply; 8+ messages in thread
From: Luis Machado @ 2022-08-23 20:35 UTC (permalink / raw)
  To: Nick Clifton, Richard Earnshaw, Jeff Law, binutils

Hi Nick,

On 8/23/22 16:30, Nick Clifton via Binutils wrote:
> Hi Richard,
> 
>> This fails to build on ubuntu (which defaults to -Werror=format-security):
>>
>> /home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c:5738:6: error: format not a string literal and no format arguments [-Werror=format-security]
> 
> Yeah - I noticed this 10 minutes after I committed the original patch.
> I have since applied a small update which fixes the problem, so if
> you update the sources, the error should go away.
> 
> Cheers
>    Nick
> 

I'm still seeing this for 32-bit Arm builds of binutils-gdb. Was the fix applied?

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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  2022-08-23 20:35         ` Luis Machado
@ 2022-08-24  9:24           ` Nick Clifton
  2022-08-24 10:18             ` Luis Machado
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Clifton @ 2022-08-24  9:24 UTC (permalink / raw)
  To: Luis Machado, Richard Earnshaw, Jeff Law, binutils

Hi Luis,

>>> /home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c:5738:6: error: format not a string literal and no format arguments [-Werror=format-security]

> I'm still seeing this for 32-bit Arm builds of binutils-gdb. Was the fix applied?

I believe so. Commit: b3ea2010cd06

Did I miss something ?

Cheers
   Nick


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

* Re: RFC: readelf: Explain why LLVM bitcode files cannot be read
  2022-08-24  9:24           ` Nick Clifton
@ 2022-08-24 10:18             ` Luis Machado
  0 siblings, 0 replies; 8+ messages in thread
From: Luis Machado @ 2022-08-24 10:18 UTC (permalink / raw)
  To: Nick Clifton, Richard Earnshaw, Jeff Law, binutils

Hi Nick,

On 8/24/22 10:24, Nick Clifton wrote:
> Hi Luis,
> 
>>>> /home/rearnsha/gnusrc/gcc-cross/master/binutils/readelf.c:5738:6: error: format not a string literal and no format arguments [-Werror=format-security]
> 
>> I'm still seeing this for 32-bit Arm builds of binutils-gdb. Was the fix applied?
> 
> I believe so. Commit: b3ea2010cd06
> 
> Did I miss something ?
> 
> Cheers
>    Nick
> 

I tried again this morning and it had failed, but another update about half an hour ago made it work. Something must have gotten stuck somewhere.

Anyway, it's a non-issue now.

Thanks,
Luis

^ 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).