unknown type name ‘GElf_Nhdr’; did you mean ‘GElf_Shdr’? while building libbpf

NobinPegasus

Member
Joined
Feb 4, 2022
Messages
50
Reaction score
0
Credits
447
Whenever I'm trying to build libbpf. I'm facing the following error. When I try to build tools related libbpf I also face the same errors.
Code:
pegasus@pegasus:~/Documents/eBPF/libbpf/src$ make
  MKDIR    staticobjs
  CC       staticobjs/bpf.o
  CC       staticobjs/btf.o
  CC       staticobjs/libbpf.o
  CC       staticobjs/libbpf_errno.o
  CC       staticobjs/netlink.o
  CC       staticobjs/nlattr.o
  CC       staticobjs/str_error.o
  CC       staticobjs/libbpf_probes.o
  CC       staticobjs/bpf_prog_linfo.o
  CC       staticobjs/btf_dump.o
  CC       staticobjs/hashmap.o
  CC       staticobjs/ringbuf.o
  CC       staticobjs/strset.o
  CC       staticobjs/linker.o
  CC       staticobjs/gen_loader.o
  CC       staticobjs/relo_core.o
  CC       staticobjs/usdt.o
usdt.c:572:56: error: unknown type name ‘GElf_Nhdr’; did you mean ‘GElf_Shdr’?
  572 | static int parse_usdt_note(Elf *elf, const char *path, GElf_Nhdr *nhdr,
      |                                                        ^~~~~~~~~
      |                                                        GElf_Shdr
usdt.c: In function ‘collect_usdt_targets’:
usdt.c:589:9: error: unknown type name ‘GElf_Nhdr’; did you mean ‘GElf_Shdr’?
  589 |         GElf_Nhdr nhdr;
      |         ^~~~~~~~~
      |         GElf_Shdr
usdt.c:621:23: error: implicit declaration of function ‘gelf_getnote’; did you mean ‘gelf_getsym’? [-Werror=implicit-function-declaration]
  621 |         while ((off = gelf_getnote(data, off, &nhdr, &name_off, &desc_off)) > 0) {
      |                       ^~~~~~~~~~~~
      |                       gelf_getsym
usdt.c:627:23: error: implicit declaration of function ‘parse_usdt_note’; did you mean ‘parse_usdt_spec’? [-Werror=implicit-function-declaration]
  627 |                 err = parse_usdt_note(elf, path, &nhdr, data->d_buf, name_off, desc_off, &note);
      |                       ^~~~~~~~~~~~~~~
      |                       parse_usdt_spec
usdt.c: At top level:
usdt.c:1133:56: error: unknown type name ‘GElf_Nhdr’; did you mean ‘GElf_Shdr’?
 1133 | static int parse_usdt_note(Elf *elf, const char *path, GElf_Nhdr *nhdr,
      |                                                        ^~~~~~~~~
      |                                                        GElf_Shdr
cc1: note: unrecognized command-line option ‘-Wno-unknown-warning-option’ may have been intended to silence earlier diagnostics
cc1: all warnings being treated as errors
make: *** [Makefile:134: staticobjs/usdt.o] Error 1
Code:
pegasus@pegasus:~/Documents/eBPF/xdp-tutorial$ uname -a
Linux pegasus 6.4.0-060400-generic #202306271339 SMP PREEMPT_DYNAMIC Tue Jun 27 14:26:34 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

I'm unable to figure out the actual reason of this.
 


I'm unable to figure out the actual reason of this.
To fix these kinds of build errors one needs to open up source file, including header file and all header files that are included and then fix the problem with the help of syntax highlighting and manual compiling (ex. without make file).

Certainly not easy if you're not familiar with C since this are compile time errors.

You might want to not treat warnings as errors btw.
 
To fix your problem open up usdt.c file and add the following typedef at the top of usdt.c file on line 9 after #include <gelf.h>

Code:
typedef Elf64_Nhdr GElf_Nhdr;
 
I'm not concerned about warnings. But the error message. I'm unable to build all the tools that comes in https://github.com/libbpf/libbpf
libbpf repo. On a fresh ubuntu installation it works well. But on my local machine I've some kind of dependency conflicts.
But unable to figure it out. It's part of a big project. So idk how much time manual compiling will take.
 
Also these are the library function they shouldn't be edited. It should work without be manipulating the source
 
Also there are so many usdt.c. I'm not sure which one is getting used.
Code:
pegasus@pegasus:~/Documents/eBPF/xdp-tutorial/libbpf/src$ locate usdt.c
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bcc-static.dir/usdt/usdt.cc.o
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bcc-static.dir/usdt/usdt.cc.o.d
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-shared.dir/libbpf/src/usdt.c.o
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-shared.dir/libbpf/src/usdt.c.o.d
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-shared.dir/usdt/usdt.cc.o
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-shared.dir/usdt/usdt.cc.o.d
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-static.dir/libbpf/src/usdt.c.o
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-static.dir/libbpf/src/usdt.c.o.d
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-static.dir/usdt/usdt.cc.o
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/CMakeFiles/bpf-static.dir/usdt/usdt.cc.o.d
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/usdt/CMakeFiles/usdt-static.dir/usdt.cc.o
/home/pegasus/.local/share/Trash/files/bcc/build/src/cc/usdt/CMakeFiles/usdt-static.dir/usdt.cc.o.d
/home/pegasus/.local/share/Trash/files/bcc/libbpf-tools/bpftool/libbpf/src/usdt.c
/home/pegasus/.local/share/Trash/files/bcc/src/cc/libbpf/src/usdt.c
/home/pegasus/.local/share/Trash/files/bcc/src/cc/usdt/usdt.cc
/home/pegasus/.local/share/Trash/files/bpftool/libbpf/src/usdt.c
/home/pegasus/.local/share/Trash/files/learning-ebpf/learning-ebpf/libbpf/src/usdt.c
/home/pegasus/.local/share/Trash/files/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/bpftool/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/learning-ebpf/bcc/libbpf-tools/bpftool/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/learning-ebpf/bcc/src/cc/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/learning-ebpf/bcc/src/cc/usdt/usdt.cc
/home/pegasus/Documents/eBPF/learning-ebpf/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/vc5/cmd/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/xvs/balancer/libbpf/src/usdt.c
/home/pegasus/Documents/eBPF/xvs/libbpf/src/usdt.c
/home/pegasus/Documents/linux_work/linux_latest/tools/lib/bpf/usdt.c
/home/pegasus/Documents/linux_work/linux_latest/tools/testing/selftests/bpf/prog_tests/usdt.c
/home/pegasus/Documents/linux_work/linux_latest/tools/testing/selftests/bpf/progs/test_urandom_usdt.c
/home/pegasus/Documents/linux_work/linux_latest/tools/testing/selftests/bpf/progs/test_usdt.c
/home/pegasus/Documents/linux_work/linux_mainline/tools/lib/bpf/usdt.c
/home/pegasus/Documents/linux_work/linux_mainline/tools/testing/selftests/bpf/prog_tests/usdt.c
/home/pegasus/Documents/linux_work/linux_mainline/tools/testing/selftests/bpf/progs/test_urandom_usdt.c
/home/pegasus/Documents/linux_work/linux_mainline/tools/testing/selftests/bpf/progs/test_usdt.c
/home/pegasus/Documents/new_bug/linux/tools/lib/bpf/usdt.c
/home/pegasus/Documents/new_bug/linux/tools/testing/selftests/bpf/prog_tests/usdt.c
/home/pegasus/Documents/new_bug/linux/tools/testing/selftests/bpf/progs/test_urandom_usdt.c
/home/pegasus/Documents/new_bug/linux/tools/testing/selftests/bpf/progs/test_usdt.c
/home/pegasus/Documents/new_bug/linux/tools/testing/selftests/bpf/progs/uprobe_multi_usdt.c
/home/pegasus/linux/tools/lib/bpf/usdt.c
/home/pegasus/linux/tools/testing/selftests/bpf/prog_tests/usdt.c
/home/pegasus/linux/tools/testing/selftests/bpf/progs/test_urandom_usdt.c
/home/pegasus/linux/tools/testing/selftests/bpf/progs/test_usdt.c
/home/pegasus/linux_mainline/tools/lib/bpf/usdt.c
/home/pegasus/linux_mainline/tools/testing/selftests/bpf/prog_tests/usdt.c
/home/pegasus/linux_mainline/tools/testing/selftests/bpf/progs/test_urandom_usdt.c
/home/pegasus/linux_mainline/tools/testing/selftests/bpf/progs/test_usdt.c
/linux_demo/linux_stable/tools/lib/bpf/usdt.c
/linux_demo/linux_stable/tools/testing/selftests/bpf/prog_tests/usdt.c
/linux_demo/linux_stable/tools/testing/selftests/bpf/progs/test_urandom_usdt.c
/linux_demo/linux_stable/tools/testing/selftests/bpf/progs/test_usdt.c
/usr/lib/python3/dist-packages/bcc/__pycache__/usdt.cpython-311.pyc
 
You could also refer to here. Where I had a small chat about this issue.
Code:
https://github.com/libbpf/libbpf/issues/784
 
Either your libelf is out of date or otherwise libbpf which you're compiling needs to be edited for missing typedef.

You can run sudo apt search libelf to see if latest version is installed on your system.
 
You're not using the latest version, download and compile latest version from here:

In that source tree if you open gelf.h file you'll see the typedef is declared as:
Code:
typedef Elf64_Nhdr GElf_Nhdr;

The file includes elf.h header which defines Elf64_Nhdr as:
Code:
typedef struct
{
  Elf64_Word n_namesz;            /* Length of the note's name.  */
  Elf64_Word n_descsz;            /* Length of the note's descriptor.  */
  Elf64_Word n_type;            /* Type of the note.  */
} Elf64_Nhdr;

Therefore you system version (or manual one) of the libelf is out of date.
 
I have uninstalled libelf-dev and now
I found this
Code:
pegasus@pegasus:~/Documents/eBPF/libbpf/src$ sudo apt search libelf
Sorting... Done
Full Text Search... Done
libdwarf++0/lunar 0.3-3 amd64
  C++11 DWARF parser

libelf++0/lunar 0.3-3 amd64
  C++11 ELF parser

libelf-dev/lunar 0.188-2.1 amd64
  libelf1 development libraries and header files

libelf-freebsd-1/focal 10.3~svn296373-10 amd64
  library to read and write ELF files

libelf-freebsd-dev/focal 10.3~svn296373-10 amd64
  Development files for libelf (FreeBSD version)

libelf1/lunar,now 0.188-2.1 amd64 [installed,automatic]
  library to read and write ELF files

libelfin-dev/lunar 0.3-3 amd64
  C++11 ELF/DWARF parser (development files)
 
libelf1 package which is reported as automatically installed is not development package and according to current output you have no development version of the package installed at the moment.

So if you're seeing the same error it means the header is pulled in from some other source on your system, and your job is to figure out which source that is to be able to remove it and then replace with correct version.

Normally you do this with by running gcc -M /path/to/usdt.c which should list you directories to include files including gelf.h which is the problem.
You run gcc -M on usdt.c file that's in the libppf repository, which is what you're compiling right?

have you manually downloaded and compiled libelf on that computer? if yes then that one is likely used.
 
I solved the issue. It was an issue with libelf. I downloaded a latest libelf version. Then Configured and installed it. libelf also had some dependency missing like specific versions of sqlite, libcurl, libarchive.
 

Members online


Top