store module metadata in .modinfo elf section
cover store module metadata in .modinfo elf section
This patch series stores module metadata in a .modinfo elf section and adds a utility to read it. For now the utility just prints the meta data. The plan is to store this in some file and have qemu read it.
Background is that the hard-coded lists in util/module.c are somewhat ugly and also wouldn't work very well with a large number of modules, so I'm looking for something else.
Posting this now to collect feedback on the apprach. Should I continue this?
Better ideas?
Other comments & suggestions?
take care,
Gerd
Gerd Hoffmann (9):
Add module metadata macros, add qxl module annotations add qemu-modinfo utility
virtio-gpu module annotations
chardev module annotations
audio module annotations
usb-redir module annotations
ccid module annotations
ui module annotations
s390x module annotations
include/qemu/module.h | 18 ++++ audio/spiceaudio.c | 2 + chardev/baum.c | 1 + chardev/spice.c | 4 + hw/display/qxl.c | 4 + hw/display/vhost-user-gpu-pci.c | 1 + hw/display/vhost-user-gpu.c | 1 + hw/display/vhost-user-vga.c | 1 + hw/display/virtio-gpu-base.c | 1 + hw/display/virtio-gpu-gl.c | 3 + hw/display/virtio-gpu-pci-gl.c | 3 + hw/display/virtio-gpu-pci.c | 2 + hw/display/virtio-gpu.c | 1 + hw/display/virtio-vga-gl.c | 3 + hw/display/virtio-vga.c | 2 + hw/s390x/virtio-ccw-gpu.c | 3 + hw/usb/ccid-card-emulated.c | 1 + hw/usb/ccid-card-passthru.c | 1 + hw/usb/redirect.c | 1 + qemu-modinfo.c | 182 ++++++++++++++++++++++++++++++++ ui/egl-headless.c | 4 + ui/gtk.c | 4 + ui/sdl2.c | 4 + ui/spice-app.c | 3 + ui/spice-core.c | 4 + meson.build | 3 + 26 files changed, 257 insertions(+) create mode 100644 qemu-modinfo.c
Signature · 1 line
2.31.1
PATCH 1/9 Add module metadata macros, add qxl module annotations
Stealing an idea from the linux kernel: Place module metadata in an .modinfo elf section. This patch adds macros and qxl module annotations as example.
--- include/qemu/module.h | 18 ++++++++++++++++++ hw/display/qxl.c | 4 ++++ 2 files changed, 22 insertions(+)
include/qemu/module.h +18 -0
@@ -73,4 +73,22 @@ bool module_load_one(const char *prefix, const char *lib_name, bool mayfail);void module_load_qom_one (const char *type);void module_load_qom_all (void ); +/* +* objdump -t -s -j .modinfo ${module}.so +*/ + +#define ___PASTE (a, b) a##b +#define __PASTE (a, b) ___PASTE(a, b) + +#define modinfo (kind, value) \ + static const char __PASTE(kind, __LINE__)[] \ + __attribute__((__used__)) \ + __attribute__((section(".modinfo"))) \ + __attribute__((aligned(1))) \ + = stringify(kind) "=" value + +#define module_obj (name) modinfo(obj, name) +#define module_dep (name) modinfo(dep, name) +#define module_arch (name) modinfo(arch, name) +#endif
hw/display/qxl.c +4 -0
@@ -2522,6 +2522,7 @@ static const TypeInfo qxl_primary_info = { .parent = TYPE_PCI_QXL, .class_init = qxl_primary_class_init, }; +module_obj ("qxl-vga" );static void qxl_secondary_class_init (ObjectClass *klass,void *data) {
@@ -2538,6 +2539,7 @@ static const TypeInfo qxl_secondary_info = { .parent = TYPE_PCI_QXL, .class_init = qxl_secondary_class_init, }; +module_obj ("qxl" );static void qxl_register_types (void ) {
@@ -2547,3 +2549,5 @@ static void qxl_register_types(void) } type_init(qxl_register_types) + +module_dep("ui-spice-core");
Signature · 1 line
2.31.1
PATCH 2/9 add qemu-modinfo utility
In reply to Gerd HoffmannFor now only dump metadata.
TODO:
* store in some file format (json?).
* make qemu parse it, replace hard-coded lists in util/module.c
* fix windows (seems to not have glob).
* do we need non-elf support?
--- qemu-modinfo.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ meson.build | 3 + 2 files changed, 185 insertions(+) create mode 100644 qemu-modinfo.c
qemu-modinfo.c new file +182 -0
@@ -0,0 +1,182 @@ +/* +* QEMU module parser +* +* read modules, find modinfo section, parse & store metadata. +* +* Copyright Red Hat, Inc. 2021 +* +* Authors: +* Gerd Hoffmann <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#include "qemu/osdep.h" +#include "elf.h" +#include <glob.h> + +#define Elf_Ehdr Elf64_Ehdr +#define Elf_Shdr Elf64_Shdr +#define ELFCLASS ELFCLASS64 + +static const char *moddir =CONFIG_QEMU_MODDIR ; +static const char *dsosuf =CONFIG_HOST_DSOSUF ; + +static void modinfo (const char *module,char *info,size_t size) +{ +size_t pos =0 , len; + +fprintf (stderr,"%s\n" , module); +do { +fprintf (stderr," -> %s\n" , info + pos); + len =strlen (info + pos) +1 ; + pos += len; + }while (pos < size); +} + +static void elf_read_section_hdr (FILE *fp,Elf_Ehdr *ehdr, +int section,Elf_Shdr *shdr) +{ +size_t pos, len; +int ret; + + pos = ehdr->e_shoff + section * ehdr->e_shentsize; + len =MIN (ehdr->e_shentsize,sizeof (*shdr)); + + ret =fseek (fp, pos,SEEK_SET ); +if (ret !=0 ) { +fprintf (stderr,"seek error\n" ); +exit (1 ); + } + +memset (shdr,0 ,sizeof (*shdr)); + ret =fread (shdr, len,1 , fp); +if (ret !=1 ) { +fprintf (stderr,"read error\n" ); +exit (1 ); + } +} + +static void *elf_read_section (FILE *fp,Elf_Ehdr *ehdr, +int section,size_t *size) +{ +Elf_Shdr shdr; +void *data; +int ret; + +elf_read_section_hdr (fp, ehdr, section, &shdr); +if (shdr.sh_offset && shdr.sh_size) { + ret =fseek (fp, shdr.sh_offset,SEEK_SET ); +if (ret !=0 ) { +fprintf (stderr,"seek error\n" ); +exit (1 ); + } + + data =g_malloc (shdr.sh_size); + ret =fread (data, shdr.sh_size,1 , fp); +if (ret !=1 ) { +fprintf (stderr,"read error\n" ); +exit (1 ); + } + *size = shdr.sh_size; + }else { + data =NULL ; + *size =0 ; + } +return data; +} + +static void elf_parse_module (const char *module) +{ +Elf_Ehdr ehdr; +Elf_Shdr shdr; +FILE *fp; +int ret, i; +char *str; +size_t str_size; +char *info; +size_t info_size; + + fp =fopen (module,"r" ); +if (NULL == fp) { +fprintf (stderr,"open %s: %s\n" , module,strerror (errno)); +exit (1 ); + } + + ret =fread (&ehdr,sizeof (ehdr),1 , fp); +if (ret !=1 ) { +fprintf (stderr,"read error (%s)\n" , module); +exit (1 ); + } + +if (ehdr.e_ident[EI_MAG0 ] !=ELFMAG0 || + ehdr.e_ident[EI_MAG1 ] !=ELFMAG1 || + ehdr.e_ident[EI_MAG2 ] !=ELFMAG2 || + ehdr.e_ident[EI_MAG3 ] !=ELFMAG3 ) { +fprintf (stderr,"not an elf file (%s)\n" , module); +exit (1 ); + } +if (ehdr.e_ident[EI_CLASS ] !=ELFCLASS64 ) { +fprintf (stderr,"elf class mismatch (%s)\n" , module); +exit (1 ); + } +if (ehdr.e_shoff ==0 ) { +fprintf (stderr,"no section header (%s)\n" , module); +exit (1 ); + } + +/* read string table */ +if (ehdr.e_shstrndx ==0 ) { +fprintf (stderr,"no section strings (%s)\n" , module); +exit (1 ); + } + str =elf_read_section (fp, &ehdr, ehdr.e_shstrndx, &str_size); +if (NULL == str) { +fprintf (stderr,"no section strings (%s)\n" , module); +exit (1 ); + } + +/* find and read modinfo section */ + info =NULL ; +for (i =0 ; i < ehdr.e_shnum; i++) { +elf_read_section_hdr (fp, &ehdr, i, &shdr); +if (!shdr.sh_name) { +continue ; + } +if (strcmp (str + shdr.sh_name,".modinfo" ) ==0 ) { + info =elf_read_section (fp, &ehdr, i, &info_size); + } + } + +if (info) { +modinfo (module, info, info_size); + } + +fclose (fp); +} + +int main (int argc,char **argv) +{ +char *pattern; +glob_t files; +int ret, i; + +if (argc >1 ) { + moddir = argv[1 ]; + } + + pattern =g_strdup_printf ("%s/*%s" , moddir, dsosuf); + ret =glob (pattern,0 ,NULL , &files); +if (ret !=0 ) { +fprintf (stderr,"glob(%s) failed: %d\n" , pattern, ret); +exit (1 ); + } + +for (i =0 ; i < files.gl_pathc; i++) { +elf_parse_module (files.gl_pathv[i]); + } + +globfree (&files); +g_free (pattern); +return 0 ; +}
meson.build +3 -0
@@ -2358,6 +2358,9 @@ if xkbcommon.found() dependencies: [qemuutil, xkbcommon], install: have_tools) endif +qemu_modinfo = executable('qemu-modinfo', files('qemu-modinfo.c') + genh, + dependencies: [glib], install: have_tools) + if have_tools qemu_img = executable('qemu-img', [files('qemu-img.c'), hxdep], dependencies: [authz, block, crypto, io, qom, qemuutil], install: true)
Signature · 1 line
2.31.1
PATCH 3/9 virtio-gpu module annotations
In reply to Gerd Hoffmann--- hw/display/vhost-user-gpu-pci.c | 1 + hw/display/vhost-user-gpu.c | 1 + hw/display/vhost-user-vga.c | 1 + hw/display/virtio-gpu-base.c | 1 + hw/display/virtio-gpu-gl.c | 3 +++ hw/display/virtio-gpu-pci-gl.c | 3 +++ hw/display/virtio-gpu-pci.c | 2 ++ hw/display/virtio-gpu.c | 1 + hw/display/virtio-vga-gl.c | 3 +++ hw/display/virtio-vga.c | 2 ++ 10 files changed, 18 insertions(+)
hw/display/vhost-user-gpu-pci.c +1 -0
@@ -43,6 +43,7 @@ static const VirtioPCIDeviceTypeInfo vhost_user_gpu_pci_info = { .instance_size = sizeof(VhostUserGPUPCI), .instance_init = vhost_user_gpu_pci_initfn, }; +module_obj (TYPE_VHOST_USER_GPU_PCI );static void vhost_user_gpu_pci_register_types (void ) {
hw/display/vhost-user-gpu.c +1 -0
@@ -596,6 +596,7 @@ static const TypeInfo vhost_user_gpu_info = { .instance_finalize = vhost_user_gpu_instance_finalize, .class_init = vhost_user_gpu_class_init, }; +module_obj (TYPE_VHOST_USER_GPU );static void vhost_user_gpu_register_types (void ) {
hw/display/vhost-user-vga.c +1 -0
@@ -44,6 +44,7 @@ static const VirtioPCIDeviceTypeInfo vhost_user_vga_info = { .instance_size = sizeof(VhostUserVGA), .instance_init = vhost_user_vga_inst_initfn, }; +module_obj (TYPE_VHOST_USER_VGA );static void vhost_user_vga_register_types (void ) {
hw/display/virtio-gpu-base.c +1 -0
@@ -256,6 +256,7 @@ static const TypeInfo virtio_gpu_base_info = { .class_init = virtio_gpu_base_class_init, .abstract = true }; +module_obj (TYPE_VIRTIO_GPU_BASE );static void virtio_register_types (void )
hw/display/virtio-gpu-gl.c +3 -0
@@ -154,6 +154,7 @@ static const TypeInfo virtio_gpu_gl_info = { .instance_size = sizeof(VirtIOGPUGL), .class_init = virtio_gpu_gl_class_init, }; +module_obj (TYPE_VIRTIO_GPU_GL );static void virtio_register_types (void ) {
@@ -161,3 +162,5 @@ static void virtio_register_types(void) } type_init(virtio_register_types) + +module_dep("hw-display-virtio-gpu");
hw/display/virtio-gpu-pci-gl.c +3 -0
@@ -46,6 +46,7 @@ static const VirtioPCIDeviceTypeInfo virtio_gpu_gl_pci_info = { .instance_size = sizeof(VirtIOGPUGLPCI), .instance_init = virtio_gpu_gl_initfn, }; +module_obj (TYPE_VIRTIO_GPU_GL_PCI );static void virtio_gpu_gl_pci_register_types (void ) {
@@ -53,3 +54,5 @@ static void virtio_gpu_gl_pci_register_types(void) } type_init(virtio_gpu_gl_pci_register_types) + +module_dep("hw-display-virtio-gpu-pci");
hw/display/virtio-gpu-pci.c +2 -0
@@ -64,6 +64,7 @@ static const TypeInfo virtio_gpu_pci_base_info = { .class_init = virtio_gpu_pci_base_class_init, .abstract = true }; +module_obj (TYPE_VIRTIO_GPU_PCI_BASE );#define TYPE_VIRTIO_GPU_PCI "virtio-gpu-pci"typedef struct VirtIOGPUPCI VirtIOGPUPCI ;
@@ -90,6 +91,7 @@ static const VirtioPCIDeviceTypeInfo virtio_gpu_pci_info = { .instance_size = sizeof(VirtIOGPUPCI), .instance_init = virtio_gpu_initfn, }; +module_obj (TYPE_VIRTIO_GPU_PCI );static void virtio_gpu_pci_register_types (void ) {
hw/display/virtio-gpu.c +1 -0
@@ -1419,6 +1419,7 @@ static const TypeInfo virtio_gpu_info = { .class_size = sizeof(VirtIOGPUClass), .class_init = virtio_gpu_class_init, }; +module_obj (TYPE_VIRTIO_GPU );static void virtio_register_types (void ) {
hw/display/virtio-vga-gl.c +3 -0
@@ -36,6 +36,7 @@ static VirtioPCIDeviceTypeInfo virtio_vga_gl_info = { .instance_size = sizeof(VirtIOVGAGL), .instance_init = virtio_vga_gl_inst_initfn, }; +module_obj (TYPE_VIRTIO_VGA_GL );static void virtio_vga_register_types (void ) {
@@ -45,3 +46,5 @@ static void virtio_vga_register_types(void) } type_init(virtio_vga_register_types) + +module_dep("hw-display-virtio-vga");
hw/display/virtio-vga.c +2 -0
@@ -239,6 +239,7 @@ static TypeInfo virtio_vga_base_info = { .class_init = virtio_vga_base_class_init, .abstract = true, }; +module_obj (TYPE_VIRTIO_VGA_BASE );#define TYPE_VIRTIO_VGA "virtio-vga"
@@ -268,6 +269,7 @@ static VirtioPCIDeviceTypeInfo virtio_vga_info = { .instance_size = sizeof(VirtIOVGA), .instance_init = virtio_vga_inst_initfn, }; +module_obj (TYPE_VIRTIO_VGA );static void virtio_vga_register_types (void ) {
Signature · 1 line
2.31.1
PATCH 4/9 chardev module annotations
In reply to Gerd Hoffmann--- chardev/baum.c | 1 + chardev/spice.c | 4 ++++ 2 files changed, 5 insertions(+)
chardev/baum.c +1 -0
@@ -680,6 +680,7 @@ static const TypeInfo char_braille_type_info = { .instance_finalize = char_braille_finalize, .class_init = char_braille_class_init, }; +module_obj (TYPE_CHARDEV_BRAILLE );static void register_types (void ) {
chardev/spice.c +4 -0
@@ -366,6 +366,7 @@ static const TypeInfo char_spice_type_info = { .class_init = char_spice_class_init, .abstract = true, }; +module_obj (TYPE_CHARDEV_SPICE );static void char_spicevmc_class_init (ObjectClass *oc,void *data) {
@@ -396,6 +397,7 @@ static const TypeInfo char_spiceport_type_info = { .parent = TYPE_CHARDEV_SPICE, .class_init = char_spiceport_class_init, }; +module_obj (TYPE_CHARDEV_SPICEPORT );static void register_types (void ) {
@@ -405,3 +407,5 @@ static void register_types(void) }type_init (register_types); + +module_dep ("ui-spice-core" );
Signature · 1 line
2.31.1
PATCH 5/9 audio module annotations
In reply to Gerd Hoffmann--- audio/spiceaudio.c | 2 ++ 1 file changed, 2 insertions(+)
audio/spiceaudio.c +2 -0
@@ -317,3 +317,5 @@ static void register_audio_spice(void)audio_driver_register (&spice_audio_driver); }type_init (register_audio_spice); + +module_dep ("ui-spice-core" );
Signature · 1 line
2.31.1
PATCH 6/9 usb-redir module annotations
In reply to Gerd Hoffmann--- hw/usb/redirect.c | 1 + 1 file changed, 1 insertion(+)
hw/usb/redirect.c +1 -0
@@ -2608,6 +2608,7 @@ static const TypeInfo usbredir_dev_info = { .class_init = usbredir_class_initfn, .instance_init = usbredir_instance_init, }; +module_obj (TYPE_USB_REDIR );static void usbredir_register_types (void ) {
Signature · 1 line
2.31.1
PATCH 7/9 ccid module annotations
In reply to Gerd Hoffmann--- hw/usb/ccid-card-emulated.c | 1 + hw/usb/ccid-card-passthru.c | 1 + 2 files changed, 2 insertions(+)
hw/usb/ccid-card-emulated.c +1 -0
@@ -612,6 +612,7 @@ static const TypeInfo emulated_card_info = { .instance_size = sizeof(EmulatedState), .class_init = emulated_class_initfn, }; +module_obj (TYPE_EMULATED_CCID );static void ccid_card_emulated_register_types (void ) {
hw/usb/ccid-card-passthru.c +1 -0
@@ -414,6 +414,7 @@ static const TypeInfo passthru_card_info = { .instance_size = sizeof(PassthruState), .class_init = passthru_class_initfn, }; +module_obj (TYPE_CCID_PASSTHRU );static void ccid_card_passthru_register_types (void ) {
Signature · 1 line
2.31.1
PATCH 8/9 ui module annotations
In reply to Gerd Hoffmann--- ui/egl-headless.c | 4 ++++ ui/gtk.c | 4 ++++ ui/sdl2.c | 4 ++++ ui/spice-app.c | 3 +++ ui/spice-core.c | 4 ++++ 5 files changed, 19 insertions(+)
ui/egl-headless.c +4 -0
@@ -213,3 +213,7 @@ static void register_egl(void) }type_init (register_egl); + +#ifdef CONFIG_OPENGL +module_dep ("chardev-spice" ); +#endif
ui/gtk.c +4 -0
@@ -2333,3 +2333,7 @@ static void register_gtk(void) }type_init (register_gtk); + +#ifdef CONFIG_OPENGL +module_dep ("chardev-spice" ); +#endif
ui/sdl2.c +4 -0
@@ -918,3 +918,7 @@ static void register_sdl1(void) }type_init (register_sdl1); + +#ifdef CONFIG_OPENGL +module_dep ("chardev-spice" ); +#endif
ui/spice-app.c +3 -0
@@ -221,3 +221,6 @@ static void register_spice_app(void) }type_init (register_spice_app); + +module_dep ("ui-spice-core" ); +module_dep ("chardev-spice" );
ui/spice-core.c +4 -0
@@ -1037,3 +1037,7 @@ static void spice_register_config(void)qemu_add_opts (&qemu_spice_opts); }opts_init (spice_register_config); + +#ifdef CONFIG_OPENGL +module_dep ("chardev-spice" ); +#endif
Signature · 1 line
2.31.1
PATCH 9/9 s390x module annotations
In reply to Gerd Hoffmann--- hw/s390x/virtio-ccw-gpu.c | 3 +++ 1 file changed, 3 insertions(+)
hw/s390x/virtio-ccw-gpu.c +3 -0
@@ -59,6 +59,7 @@ static const TypeInfo virtio_ccw_gpu = { .instance_init = virtio_ccw_gpu_instance_init, .class_init = virtio_ccw_gpu_class_init, }; +module_obj (TYPE_VIRTIO_GPU_CCW );static void virtio_ccw_gpu_register (void ) {
@@ -68,3 +69,5 @@ static void virtio_ccw_gpu_register(void) } type_init(virtio_ccw_gpu_register) + +module_arch("s390x");
Signature · 1 line
2.31.1