libkldelf: add a private library for kernel/kld-related ELF parsing

The libkldelf library was originally a part of kldxref(8). It exposed
ELF parsing helpers specialized in parsing KLDs and the kernel
executable. The library can be used to read metadata such as linker_set,
mod_depend, mod_version and PNP match info, and raw data from the ELF.

To promote the reuse of the facilities the ELF parsing code is separated
from kldxref(8) into a new private library.

kldxref(8) is modified to link against the libkldelf library.

Sponsored by:	Juniper Networks, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D46719
This commit is contained in:
Ka Ho Ng
2024-10-08 04:24:07 +00:00
parent 6c711019f2
commit 0a2cfd653e
18 changed files with 67 additions and 25 deletions
+9 -2
View File
@@ -2509,8 +2509,12 @@ ${_bt}-lib/libdwarf: ${_bt_m4_depend}
_bt_libelf_depend=${_bt}-lib/libelf _bt_libelf_depend=${_bt}-lib/libelf
.endif .endif
_libkldelf= lib/libkldelf
${_bt}-lib/libkldelf: ${_bt_libelf_depend}
_bt_libkldelf_depend=${_bt}-lib/libkldelf
_kldxref= usr.sbin/kldxref _kldxref= usr.sbin/kldxref
${_bt}-usr.sbin/kldxref: ${_bt_libelf_depend} ${_bt}-usr.sbin/kldxref: ${_bt_libelf_depend} ${_bt_libkldelf_depend}
# flua is required to regenerate syscall files. It first appeared during the # flua is required to regenerate syscall files. It first appeared during the
# 13.0-CURRENT cycle, thus needs to be built on -older releases and stable # 13.0-CURRENT cycle, thus needs to be built on -older releases and stable
@@ -2773,6 +2777,7 @@ bootstrap-tools: ${_bt}-links .PHONY
${_cat} \ ${_cat} \
${_kbdcontrol} \ ${_kbdcontrol} \
${_elftoolchain_libs} \ ${_elftoolchain_libs} \
${_libkldelf} \
${_kldxref} \ ${_kldxref} \
lib/libopenbsd \ lib/libopenbsd \
usr.bin/mandoc \ usr.bin/mandoc \
@@ -3216,7 +3221,8 @@ _prebuild_libs= ${_kerberos5_lib_libasn1} \
lib/libfigpar \ lib/libfigpar \
${_lib_libgssapi} \ ${_lib_libgssapi} \
lib/libjail \ lib/libjail \
lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \ lib/libkiconv lib/libkldelf lib/libkvm \
lib/liblzma lib/libmd lib/libnv \
lib/libzstd \ lib/libzstd \
${_lib_casper} \ ${_lib_casper} \
lib/ncurses/tinfo \ lib/ncurses/tinfo \
@@ -3251,6 +3257,7 @@ _prebuild_libs+= lib/libregex
.endif .endif
lib/libgeom__L: lib/libexpat__L lib/libsbuf__L lib/libgeom__L: lib/libexpat__L lib/libsbuf__L
lib/libkldelf__L: lib/libelf__L
lib/libkvm__L: lib/libelf__L lib/libkvm__L: lib/libelf__L
.if ${MK_RADIUS_SUPPORT} != "no" .if ${MK_RADIUS_SUPPORT} != "no"
+1
View File
@@ -66,6 +66,7 @@ SUBDIR= ${SUBDIR_BOOTSTRAP} \
libiscsiutil \ libiscsiutil \
libjail \ libjail \
libkiconv \ libkiconv \
libkldelf \
libkvm \ libkvm \
liblua \ liblua \
liblzma \ liblzma \
+21
View File
@@ -0,0 +1,21 @@
.include <bsd.own.mk>
PACKAGE= runtime
LIB= kldelf
PRIVATELIB= yes
SRCS= ef.c \
ef_obj.c \
elf.c \
ef_aarch64.c \
ef_arm.c \
ef_amd64.c \
ef_i386.c \
ef_mips.c \
ef_powerpc.c \
ef_riscv.c
WARNS?= 2
LIBADD= elf
.include <bsd.lib.mk>
+16
View File
@@ -0,0 +1,16 @@
# Autogenerated - do NOT edit!
DIRDEPS = \
include \
include/xlocale \
lib/${CSU_DIR} \
lib/libc \
lib/libcompiler_rt \
lib/libelf \
.include <dirdeps.mk>
.if ${DEP_RELDIR} == ${_DEP_RELDIR}
# local dependencies - needed for -jN in clean tree
.endif
+1 -1
View File
@@ -41,7 +41,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "ef.h" #include "kldelf.h"
#define MAXSEGS 16 #define MAXSEGS 16
struct ef_file { struct ef_file {
@@ -31,7 +31,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -33,7 +33,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -34,7 +34,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -33,7 +33,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -36,7 +36,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -43,7 +43,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "ef.h" #include "kldelf.h"
typedef struct { typedef struct {
GElf_Addr addr; GElf_Addr addr;
@@ -33,7 +33,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -36,7 +36,7 @@
#include <errno.h> #include <errno.h>
#include <gelf.h> #include <gelf.h>
#include "ef.h" #include "kldelf.h"
/* /*
* Apply relocations to the values obtained from the file. `relbase' is the * Apply relocations to the values obtained from the file. `relbase' is the
@@ -44,7 +44,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "ef.h" #include "kldelf.h"
SET_DECLARE(elf_reloc, struct elf_reloc_data); SET_DECLARE(elf_reloc, struct elf_reloc_data);
@@ -32,8 +32,8 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#ifndef _EF_H_ #ifndef _KLDELF_H_
#define _EF_H_ #define _KLDELF_H_
#include <sys/linker_set.h> #include <sys/linker_set.h>
#include <stdbool.h> #include <stdbool.h>
@@ -312,4 +312,4 @@ int elf_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
__END_DECLS __END_DECLS
#endif /* _EF_H_*/ #endif /* _KLDELF_H_*/
+2
View File
@@ -27,6 +27,7 @@ _PRIVATELIBS= \
gtest_main \ gtest_main \
heimipcc \ heimipcc \
heimipcs \ heimipcs \
kldelf \
ldns \ ldns \
sqlite3 \ sqlite3 \
ssh \ ssh \
@@ -312,6 +313,7 @@ _DP_bsnmp= crypto
.endif .endif
_DP_geom= bsdxml sbuf _DP_geom= bsdxml sbuf
_DP_cam= sbuf _DP_cam= sbuf
_DP_kldelf= elf
_DP_kvm= elf _DP_kvm= elf
_DP_casper= nv _DP_casper= nv
_DP_cap_dns= nv _DP_cap_dns= nv
+4 -9
View File
@@ -1,17 +1,12 @@
PACKAGE= runtime PACKAGE= runtime
PROG= kldxref PROG= kldxref
MAN= kldxref.8 MAN= kldxref.8
SRCS= kldxref.c ef.c ef_obj.c elf.c SRCS= kldxref.c
SRCS+= ef_aarch64.c \
ef_arm.c \ CFLAGS+=-I${SRCTOP}/lib/libkldelf
ef_amd64.c \
ef_i386.c \
ef_mips.c \
ef_powerpc.c \
ef_riscv.c
WARNS?= 2 WARNS?= 2
LIBADD= elf LIBADD= elf kldelf
.include <bsd.prog.mk> .include <bsd.prog.mk>
+1 -1
View File
@@ -51,7 +51,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "ef.h" #include <kldelf.h>
#define MAXRECSIZE (64 << 10) /* 64k */ #define MAXRECSIZE (64 << 10) /* 64k */
#define check(val) if ((error = (val)) != 0) break #define check(val) if ((error = (val)) != 0) break