public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Alan Maguire <alan.maguire@oracle.com>
To: SuHsueyu <anolasc13@gmail.com>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	"Jose E. Marchesi" <jemarch@gnu.org>
Cc: bpf@vger.kernel.org
Subject: Re: Support for gcc
Date: Wed, 21 Dec 2022 17:41:11 +0000	[thread overview]
Message-ID: <f0b6775d-116c-e403-ec19-a363f90534bf@oracle.com> (raw)
In-Reply-To: <CAEc2n-tRdpoFRA+-wQYdmgabx44xMy9cD6aAW+y7bKMUJ1r=gw@mail.gmail.com>

On 21/12/2022 11:03, SuHsueyu wrote:
> I used clang and gcc to generate the obj files, and then readelf these
> two files, I found that there is a problem with the machine item, the
> machine item generated by clang is "Linux BPF", and the one generated
> by gcc is my physical machine architecture "x86-64", which makes the
> check of "ehdr->e_machine != EM_BPF" fail.
> This seems to be a problem with my not specifying a compilation option
> similar to clang target  when compiling with gcc.
>
> On Wed, Dec 21, 2022 at 8:34 AM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
>>
>> On Tue, Dec 20, 2022 at 3:56 AM SuHsueyu <anolasc13@gmail.com> wrote:
>>>
>>> Hello, I use gcc 12.1.0 to compile a source file:
>>> t.c
>>> struct t {
>>>   int a:2;
>>>   int b:3;
>>>   int c:2;
>>> } g;
>>> with gcc -c -gbtf t.c
>>> and try to use libbpf API btf__parse_split, bpf_object__open, and
>>> bpf_object__open to parse and load into the kernel, but it failed with
>>> "libbpf: elf: /path/to/t.o is not a valid eBPF object file".
>>
>> if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF))
>>
>> This check is failing in libbpf. So check which of those two are not
>> set appropriately. cc Jose to point where to report GCC-BPF specific
>> issues.
>>
>>>
>>> Is it wrong for me to do so? Due to some constraint, I cannot use
>>> clang but gcc. How to parse and load gcc compiled object file with
>>> libbpf?

As mentioned https://gcc.gnu.org/wiki/BPFBackEnd has some details;
I used those to install gcc-bpf-unknown-none/binutils-bpf-unknown-none
(package names differ for different distros so use the wiki page to
find the ones you need) and ran

$ bpf-unknown-none-gcc t.c -c -o t.o -gbtf
$ bpftool btf dump file t.o
[1] STRUCT 't' size=4 vlen=3
	'a' type_id=2 bits_offset=0 bitfield_size=2
	'b' type_id=2 bits_offset=2 bitfield_size=3
	'c' type_id=2 bits_offset=5 bitfield_size=2
[2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[3] VAR 'g' type_id=1, linkage=global
[4] DATASEC '.bss' size=0 vlen=1
	type_id=3 offset=0 size=4 (VAR 'g')

$ file t.o
t.o: ELF 64-bit LSB relocatable, eBPF, version 1 (SYSV), not stripped

Hope this helps,

Alan

  reply	other threads:[~2022-12-21 17:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-20 11:45 Support for gcc SuHsueyu
2022-12-20 17:27 ` sdf
2022-12-21  0:34 ` Andrii Nakryiko
2022-12-21 11:03   ` SuHsueyu
2022-12-21 17:41     ` Alan Maguire [this message]
2022-12-21 18:26   ` Jose E. Marchesi
2022-12-21 18:22 ` Jose E. Marchesi
2022-12-21 18:30   ` Jose E. Marchesi
2022-12-25 15:24     ` SuHsueyu
2022-12-26  9:42       ` Jose E. Marchesi
  -- strict thread matches above, loose matches on Subject: below --
2022-12-29 18:22 Marc Poulhiès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f0b6775d-116c-e403-ec19-a363f90534bf@oracle.com \
    --to=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=anolasc13@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=jemarch@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox