{"id":7,"date":"2008-03-27T00:24:00","date_gmt":"2008-03-26T23:24:00","guid":{"rendered":"http:\/\/paguilar.org\/?p=7"},"modified":"2008-03-27T00:24:00","modified_gmt":"2008-03-26T23:24:00","slug":"compiling-a-kernel-driver-that-has-multiple-source-files","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=7","title":{"rendered":"Compiling a kernel driver that has multiple source files"},"content":{"rendered":"<p>All the drivers that I had dealt with until today where written in just one source file. But today I had several problems trying to compile a driver for a 2.6 kernel that had several source files. Most of the info I found like in the <a href=\"http:\/\/www.tldp.org\/LDP\/lkmpg\/2.6\/html\/x351.html\">LKMPG<\/a> and in <a href=\"http:\/\/lwn.net\/Kernel\/LDD3\/\">LDD3<\/a>, did not work for me. The problem was with my Makefile that I was writing in this way (the tabs after the all and clean targets were removed due to formatting issues):<\/p>\n<pre lang=\"bash\">\nPWD = $(shell pwd)\nobj-m += modmain.o\nmodmain-objs += file1.o\nall:\nmake -C \/lib\/modules\/$(shell uname -r)\/build SUBDIRS=$(PWD) modules\nclean:\nmake -C \/lib\/modules\/$(shell uname -r)\/build SUBDIRS=$(PWD) clean<\/pre>\n<p>The files in my dir were:<\/p>\n<pre lang=\"bash\">\n$ ls\nfile1.c  file1.h  Makefile  modmain.c<\/pre>\n<p>If you follow the LKMPG or other sources you see that this seems correct: I wanted to compile a couple of files, <em>modmain.c<\/em> that contains the module init\/exit funcs, and <em>file.c<\/em> that contains funcs used in <em>modmain.c<\/em>, and generate a driver module <em>modmain.ko<\/em>..<br \/>\nAfter compiling and linking without problems, I insmod the module but I didn&#8217;t see any of the printk() I wrote, in fact, the driver can be inserted and removed, but it does nothing.<br \/>\nAfter a while, I use <em>nm<\/em> to check which symbols were inside the driver and to my surprise the only symbols inside the .ko were<\/p>\n<pre lang=\"bash\">\n00000000 r __func__.1640\n00000020 r __mod_vermagic5\n00000000 r __module_depends\n00000000 D __this_module\n00000000 T init_stuff\nU printk<\/pre>\n<p>No signs of init_module and friends. At this point I realized that the file modmain.c was not being compiled. In fact, my output from the Makefile was the following:<\/p>\n<pre lang=\"bash\">\n$ make\nmake -C \/lib\/modules\/2.6.23.1-42.fc8\/build SUBDIRS=\/home\/piter\/code\/c modules\nmake[1]: Entering directory '\/usr\/src\/kernels\/2.6.23.1-42.fc8-i686'\nCC [M]  \/home\/piter\/code\/c\/file1.o\nLD [M]  \/home\/piter\/code\/c\/modmain.o\nBuilding modules, stage 2.\nMODPOST 1 modules\nCC      \/home\/piter\/code\/c\/modmain.mod.o\nLD [M]  \/home\/piter\/code\/c\/modmain.ko\nmake[1]: Leaving directory '\/usr\/src\/kernels\/2.6.23.1-42.fc8-i686'<\/pre>\n<p><em>modmain.c<\/em> was not being compiled!<\/p>\n<p>After playing around with the Makefile I got the right file (again, the tabs after the all and clean targets were removed due to formatting issues):<\/p>\n<pre lang=\"bash\">\nPWD = $(shell pwd)\nobj-m += mymod.o\nmymod-objs += file1.o modmain.o\nall:\nmake -C \/lib\/modules\/$(shell uname -r)\/build SUBDIRS=$(PWD) modules\nclean:\nmake -C \/lib\/modules\/$(shell uname -r)\/build SUBDIRS=$(PWD) clean<\/pre>\n<p>Now, the output when compiling and linking is the following:<\/p>\n<pre lang=\"bash\">\n$ make\nmake -C \/lib\/modules\/2.6.23.1-42.fc8\/build SUBDIRS=\/home\/piter\/code\/c modules\nmake[1]: Entering directory '\/usr\/src\/kernels\/2.6.23.1-42.fc8-i686'\nCC [M]  \/home\/piter\/code\/c\/file1.o\nCC [M]  \/home\/piter\/code\/c\/modmain.o\nLD [M]  \/home\/piter\/code\/c\/mymod.o\nBuilding modules, stage 2.\nMODPOST 1 modules\nCC      \/home\/piter\/code\/c\/mymod.mod.o\nLD [M]  \/home\/piter\/code\/c\/mymod.ko\nmake[1]: Leaving directory '\/usr\/src\/kernels\/2.6.23.1-42.fc8-i686'<\/pre>\n<p><em>modmain.c<\/em> is being compiled this time!<br \/>\nAnd <em>nm<\/em> says the following:<\/p>\n<pre lang=\"bash\">\n00000000 r __func__.1640\n00000017 r __func__.8988\n0000000b r __func__.8993\n00000024 r __mod_author6\n0000000c r __mod_description7\n00000000 r __mod_license8\n00000080 r __mod_vermagic5\n00000060 r __module_depends\n00000000 D __this_module\n00000000 T cleanup_module\n00000000 T init_module\n00000000 T init_stuff\n00000000 t my_mod_exit\n00000000 t my_mod_init\nU printk<\/pre>\n<p>The symbols used in <em>modmain.c<\/em> appear normally.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nI changed the final driver name to <em>mymod.ko<\/em> and the file <em>modmain.c<\/em> is now part of the mymod-objs variable, in this way I&#8217;m sure that it&#8217;s compiled. Notice that the final driver name is different from any of the source files, including the file that contains the module_init\/exit funcs.<\/p>\n<p><strong>Notice<\/strong><br \/>\nSince I had the above mentioned issues for compiling a driver that has multiple source files following commonly linux kernel drivers docs and I have seen other people in the same situation, I decided to share the procedure that worked for me, but this does not mean in any way that the references I used were mistaken, most probably I missed something.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the drivers that I had dealt with until today where written in just one source file. But today I had several problems trying to compile a driver for a 2.6 kernel that had several source files. Most of the info I found like in the LKMPG and in LDD3, did not work for me.\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=7\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-compiling","category-driver"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"}],"version-history":[{"count":0,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}