arm: global variables in kernel module not loaded correctly in gdb

From: Arend van Spriel
Date: Tue Apr 16 2024 - 05:13:33 EST


This issue should probably be reported to GDB bugzilla, but wanted to put it out here as well. Maybe someone else stumbled upon this or simply knows what is going on. Google searches did not provide much clues nor searching GDB bugzilla.

So what is my issue? When I load a .ko file into GDB using add-symbol-file the (static) global variables do not have the correct memory address. When I use the .o file iso the .ko file things work as expected.

I created a dummy kernel module and compiled it against 6.9-rc4. I did have my suspicion about PLTS which is enabled in my kernel config. When CONFIG_ARM_MODULE_PLTS is disabled the symbol address is correct. From my limited understanding this involves an additional link table. AFAICT the BFD library which is used by GDB supports this, but can not rule out there is a bug in there. I rebuild GDB and added some debug print and also changed a debug variable which makes symbol loading a bit more verbose.

Attached are the two kernel config files and the dummy module source file. Any help would be appreciated.

Regards,
Arend

with CONFIG_ARM_MODULE_PLTS
---------------------------
Reading symbols from vmlinux...
(gdb) add-symbol-file dummy.ko -s .text 0xbf607000 -s .bss 0xbf6091c0
add symbol table from file "dummy.ko" at
.text_addr = 0xbf607000
.bss_addr = 0xbf6091c0
(y or n) y
Reading symbols from dummy.ko...
using ABFD: name=.text idx=2 addr=0 lower=0
addrs[0]: name=.text idx=2 addr=bf607000
using ABFD: name=.bss idx=19 addr=0 lower=bf607000
addrs[1]: name=.bss idx=19 addr=bf6091c0
Reading minimal symbols of objfile /home/av935118/sandbox/mod_dummy/dummy.ko ...
Recording minsym: mst_file_data 0x45 12 __UNIQUE_ID_depends126
Recording minsym: mst_file_data 0x4e 12 __UNIQUE_ID_name125
Recording minsym: mst_file_data 0x59 12 __UNIQUE_ID_vermagic124
Recording minsym: mst_file_data 0x0 1 _note_15
Recording minsym: mst_file_data 0x18 1 _note_14
Recording minsym: mst_file_data 0x0 5 .LC0
Recording minsym: mst_file_text 0x0 3 dummy_exit
Recording minsym: mst_file_data 0xc 5 .LC1
Recording minsym: mst_file_data 0x20 5 .LC2
Recording minsym: mst_file_text 0x0 4 dummy_init
Recording minsym: mst_file_bss 0x0 19 .LANCHOR0
Recording minsym: mst_file_bss *0x0* 19 dummy_value
Recording minsym: mst_file_data 0x0 15 __UNIQUE_ID___addressable_cleanup_module125
Recording minsym: mst_file_data 0x0 16 __UNIQUE_ID___addressable_init_module124
Recording minsym: mst_file_data 0x0 12 __UNIQUE_ID_license127
Recording minsym: mst_file_data 0xe 12 __UNIQUE_ID_author126
Recording minsym: mst_data 0x0 17 __this_module
Recording minsym: mst_text 0x0 3 cleanup_module
Recording minsym: mst_text 0x0 4 init_module
Recording minsym: mst_text 0x0 2 dummy_set_value
Recording minsym: mst_text 0x10 2 dummy_get_value

(gdb) add-symbol-file dummy.o -s .text 0xbf607000 -s .bss 0xbf6091c0
add symbol table from file "dummy.o" at
.text_addr = 0xbf607000
.bss_addr = 0xbf6091c0
(y or n) y
Reading symbols from dummy.o...
using ABFD: name=.text idx=0 addr=0 lower=0
addrs[0]: name=.text idx=0 addr=bf607000
using ABFD: name=.bss idx=2 addr=0 lower=bf607000
addrs[1]: name=.bss idx=2 addr=bf6091c0
Reading minimal symbols of objfile /home/av935118/sandbox/mod_dummy/dummy.o ...
Recording minsym: mst_file_data 0x0 3 .LC0
Recording minsym: mst_file_text 0x38 4 dummy_exit
Recording minsym: mst_file_data 0xc 3 .LC1
Recording minsym: mst_file_data 0x20 3 .LC2
Recording minsym: mst_file_text 0x4c 7 dummy_init
Recording minsym: mst_file_bss 0xbf6091c0 2 .LANCHOR0
Recording minsym: mst_file_bss *0xbf6091c0* 2 dummy_value
Recording minsym: mst_file_data 0xa8 12 __UNIQUE_ID___addressable_cleanup_module125
Recording minsym: mst_file_data 0xac 13 __UNIQUE_ID___addressable_init_module124
Recording minsym: mst_file_data 0xb0 14 __UNIQUE_ID_license127
Recording minsym: mst_file_data 0xbe 14 __UNIQUE_ID_author126
Recording minsym: mst_text 0x38 4 cleanup_module
Recording minsym: mst_text 0x4c 7 init_module
Recording minsym: mst_text 0xbf607000 0 dummy_set_value
Recording minsym: mst_text 0xbf607010 0 dummy_get_value

without CONFIG_ARM_MODULE_PLTS
------------------------------
Reading symbols from vmlinux...
(gdb) add-symbol-file ~/sandbox/mod_dummy/dummy.ko -s .text 0xbf607000 -s .bss 0xbf6091c0
add symbol table from file "/home/av935118/sandbox/mod_dummy/dummy.ko" at
.text_addr = 0xbf607000
.bss_addr = 0xbf6091c0
(y or n) y
Reading symbols from /home/av935118/sandbox/mod_dummy/dummy.ko...
using ABFD: name=.text idx=2 addr=0 lower=0
addrs[0]: name=.text idx=2 addr=bf607000
using ABFD: name=.bss idx=17 addr=0 lower=bf607000
addrs[1]: name=.bss idx=17 addr=bf6091c0
Reading minimal symbols of objfile /home/av935118/sandbox/mod_dummy/dummy.ko ...
Recording minsym: mst_file_data 0x141 12 __UNIQUE_ID_depends126
Recording minsym: mst_file_data 0x14a 12 __UNIQUE_ID_name125
Recording minsym: mst_file_data 0x155 12 __UNIQUE_ID_vermagic124
Recording minsym: mst_file_data 0x24 1 _note_15
Recording minsym: mst_file_data 0x3c 1 _note_14
Recording minsym: mst_file_data 0xa4 5 .LC0
Recording minsym: mst_file_text 0x54 3 dummy_exit
Recording minsym: mst_file_data 0xb0 5 .LC1
Recording minsym: mst_file_data 0xc4 5 .LC2
Recording minsym: mst_file_text 0x60 4 dummy_init
Recording minsym: mst_file_bss 0xbf6091c0 17 .LANCHOR0
Recording minsym: mst_file_bss *0xbf6091c0* 17 dummy_value
Recording minsym: mst_file_data 0x184 14 __UNIQUE_ID___addressable_cleanup_module125
Recording minsym: mst_file_data 0x188 15 __UNIQUE_ID___addressable_init_module124
Recording minsym: mst_file_data 0xfc 12 __UNIQUE_ID_license127
Recording minsym: mst_file_data 0x10a 12 __UNIQUE_ID_author126
Recording minsym: mst_data 0x1c0 16 __this_module
Recording minsym: mst_text 0x54 3 cleanup_module
Recording minsym: mst_text 0x60 4 init_module
Recording minsym: mst_text 0xbf607000 2 dummy_set_value
Recording minsym: mst_text 0xbf607010 2 dummy_get_value

#include <linux/module.h>

static int dummy_value;

void dummy_set_value(int v)
{
dummy_value = v;
}

int dummy_get_value(void)
{
return dummy_value;
}

static int __init dummy_init(void)
{
pr_err("initializing..\n");
dummy_set_value(0xdeadbeef);
pr_err(" module_addr = %px\n", THIS_MODULE->mem[MOD_TEXT].base);
return 0;
}

static void __exit dummy_exit(void)
{
pr_err("leaving\n");
}

module_init(dummy_init);
module_exit(dummy_exit);

MODULE_AUTHOR("Arend van Spriel <arend.vanspriel@xxxxxxxxxxxx>");
MODULE_LICENSE("GPLv2");

Attachment: configs.tar.gz
Description: application/gzip

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature