Resolving Undefined Symbol Errors in Linux Kernel Module Linking: A Step-by-Step Guide
When developing Linux kernel modules, developers often encounter undefined symbol errors during the linking phase. This article explains how to resolve such issues through a systematic approach, using generic examples to illustrate key concepts. We'll explore two common solutions: symbol export configuration and header inclusion correction.
Scenario Overview
Imagine two kernel modules:
During main_module compilation, we encounter:
ERROR: "example_symbol" [main_module.ko] undefined!
Visual Flow of Module Dependencies
Problem Analysis
The kernel build system fails to resolve example_symbol because:
Solution 1: Exporting Symbols Correctly
Step 1: Verify Symbol Export in Helper Module
helper_module.c should export the symbol:
// Important: Must be in global scope
void example_symbol(void) {
// Function implementation
}
EXPORT_SYMBOL(example_symbol);
Step 2: Configure Module.symvers Sharing
The kernel build system generates Module.symvers containing symbol information. main_module needs access to this file from helper_module.
Original (Broken) Makefile:
obj-m += main_module.o
Fixed Makefile:
obj-m += main_module.o
# Path to helper_module's build directory
KBUILD_EXTRA_SYMBOLS += /path/to/helper_module/Module.symvers
Solution 2: Correct Header Inclusion
Step 1: Header File Structure
Ensure proper header organization:
project_root/
├── helper_module/
│ ├── helper_module.h # Contains example_symbol declaration
│ └── Makefile
└── main_module/
├── main_module.c
└── Makefile
Step 2: Fix Inclusion Syntax
Original (Problematic):
#include "helper_module.h" // Looks in local directory only
Fixed Code:
#include <helper_module.h> // Searches system include paths
Step 3: Update Makefile Paths
ccflags-y += -I$(src)/../helper_module
Complete Workflow Example
cd helper_module/
make -C /lib/modules/$(uname -r)/build M=$PWD modules
cd ../main_module/
make -C /lib/modules/$(uname -r)/build M=$PWD modules
Key Concepts Explained
1. Symbol Export System
2. Header Inclusion Types
Troubleshooting Checklist
nm helper_module.ko | grep example_symbol
grep example_symbol Module.symvers
Conclusion
Resolving undefined symbol errors in kernel modules requires attention to:
By following these patterns, developers can effectively manage cross-module dependencies in complex kernel projects.