elfutils-libelf isn’t libelf!

These days, I am suffering with building ktap on Suse. Since Suse doesn’t provide elfutils-libelf-devel nor libelf-devel, so I decide to build libelf from source code.

First, I install the newest version (0.8.13) from libelf site. Building ktap generate the following errors:

cc  -Wall -O2  -o userspace/kp_main.o -c userspace/kp_main.c
In file included from /usr/local/include/libelf/libelf.h:25:0,
                 from /usr/local/include/libelf/gelf.h:28,
                 from /usr/local/include/gelf.h:1,
                 from userspace/kp_symbol.h:28,
                 from userspace/kp_main.c:40:
/usr/lib64/gcc/x86_64-suse-linux/4.8/include/stddef.h:147:26: error: conflicting types for ‘ptrdiff_t’
 typedef __PTRDIFF_TYPE__ ptrdiff_t;
                          ^
In file included from userspace/kp_main.c:37:0:
userspace/../include/ktap_types.h:20:13: note: previous declaration of ‘ptrdiff_t’ was here
 typedef int ptrdiff_t;
             ^
Makefile:119: recipe for target 'userspace/kp_main.o' failed
make: *** [userspace/kp_main.o] Error 1

The root cause is in this version, libelf.h contains stddef.h which defines ptrdiff_t, while ktap_types.h also defines it. So I decide to use an older version of libelf.

Second, I install the 0.8.5 version of libelf. This time, the compilation generates the following errors:

userspace/kp_symbol.c: In function ‘find_load_address’:
userspace/kp_symbol.c:69:2: warning: implicit declaration of function ‘elf_getphdrnum’ [-Wimplicit-function-declaration]
  if (elf_getphdrnum(elf, &phdrnum))
  ^
userspace/kp_symbol.c: At top level:
userspace/kp_symbol.c:189:44: error: unknown type name ‘GElf_Nhdr’
 static const char *sdt_note_name(Elf *elf, GElf_Nhdr *nhdr, const char *data)
                                            ^
userspace/kp_symbol.c: In function ‘dso_sdt_notes’:
userspace/kp_symbol.c:215:2: error: unknown type name ‘GElf_Nhdr’
  GElf_Nhdr nhdr;
  ^
userspace/kp_symbol.c:222:2: warning: implicit declaration of function ‘elf_getshdrstrndx’ [-Wimplicit-function-declaration]
  if (elf_getshdrstrndx(elf, &shstrndx) != 0)
  ^
userspace/kp_symbol.c:238:3: warning: implicit declaration of function ‘gelf_getnote’ [-Wimplicit-function-declaration]
   (next = gelf_getnote(data, offset, &nhdr, &name_off, &desc_off)) > 0;
   ^
userspace/kp_symbol.c:243:11: error: request for member ‘n_namesz’ in something not a structure or union
   if (nhdr.n_namesz != sizeof(SDT_NOTE_NAME) ||
           ^
userspace/kp_symbol.c:248:3: warning: implicit declaration of function ‘sdt_note_name’ [-Wimplicit-function-declaration]
   name = sdt_note_name(elf, &nhdr, sdt_note_data(data, desc_off));
   ^
userspace/kp_symbol.c:248:8: warning: assignment makes pointer from integer without a cast [enabled by default]
   name = sdt_note_name(elf, &nhdr, sdt_note_data(data, desc_off));
        ^
userspace/kp_symbol.c:253:10: error: request for member ‘n_descsz’ in something not a structure or union
      nhdr.n_descsz, nhdr.n_type);
          ^
userspace/kp_symbol.c:253:25: error: request for member ‘n_type’ in something not a structure or union
      nhdr.n_descsz, nhdr.n_type);
                         ^
Makefile:134: recipe for target 'userspace/kp_symbol.o' failed
make: *** [userspace/kp_symbol.o] Error 1

After googling, I find this issue: elfutils-libelf isn’t libelf! So I install elfutils-libelf from source code (please refer this post), then the build of ktap is OK!