5

Imagine a project, which needs to build two linux kernel modules, with the following layout of sources tree:

modules/
|--common/
|  `--common_data.c
|--mod1/
|  `--mod1_main.c
`--mod2/
   `--mod2_main.c

The common_data.o object file needs to be included into both modules. So Kbuild file for this project looks something like this:

obj-m  := mod1.o mod2.o
mod1-y := mod1/mod1_main.o common/common_data.o
mod2-y := mod2/mod2_main.o common/common_data.o

It builds fine if there is not exported symbols (i.e. EXPORT_SYMBOL) in common_data.c file. But, if there are some exported symbols (e.g. some_func), then MODPOST will produce warnings like:

WARNING: mod2: 'some_func' exported twice. Previous export was in mod1.ko

This is because both modules share single Module.symvers file. It's possible to create two separate Kbuild files in mod1/ and mod2/ directories to avoid this warning, because then there will be two separate Module.symvers files. But this will lead to another issue - commond_data.o object will be built twice - first time when compiling mod1.ko and second time when compiling mod2.ko.

Are there any ways to suppress this warning when using single Kbuild file or to prevent second rebuild of common_data.o file when using two separate Kbuild files?

Dima Chumak
  • 139
  • 1
  • 4
  • 2
    If both modules export the same symbol name, then it wouldn't be possible to insert both modules into the kernel at one time. Is that what you want? Usually, common functionality, eg your exported symbol, would be factored into its own module. – Austin Phillips Feb 26 '13 at 22:40
  • Yes, I know that it wouldn't be possible to insert them both at the same time, but this is not required. In my case mod1 and mod2 are deliberately designed in this way. Basically, mod2 is the same mod1 module, but with some additional functionality (e.g. unit tests). – Dima Chumak Feb 27 '13 at 09:32
  • @AustinPhillips I'm in the same situation, but I'm a bit hesitant to simply ignore the messages without understanding why they occur. Are the warning message purely warnings (i.e. just there to warn you that only one of these modules may be loaded at a time)? – Vilhelm Gray Sep 25 '13 at 17:55
  • 1
    @VilhelmGray Yes, they're only a warning. When you insert a module, it's like the link step when producing a binary, all the symbols are resolved to addresses. As in a normal link step, you cannot link two different object files which have the same symbol. The kernel symbol namespace is global, so it's just trying to warn you that someone else has used that global name. – Austin Phillips Sep 25 '13 at 23:14

1 Answers1

0

After some tinkering with Kbuild system I ended up with conclusion that a most straightforward way to handle situation like this, is to filter-out warning messages from make output in the outer makefile:

$(MAKE) -C $(KDIR) M=$$PWD 2>&1 \
| grep -v '^WARNING:.*exported twice\. Previous export was in'

or w/o sacrificing STDERR and squashing it into STDOUT, but it requires bash:

bash -c "$(MAKE) -C $(KDIR) M=$$PWD 2> >( grep -v '^WARNING:.*exported twice\. Previous export was in' )"
Dima Chumak
  • 139
  • 1
  • 4