title: Error: could not insert module. Unknown symbol in module
body: <p>I'm trying to port <a href="http://rr0d.droids-corp.org/" rel="nofollow noreferrer">RR0D Rasta Ring0 Debugger</a> from 32-bit Linux to 64-bit Linux. I have converted 32-bit gcc inline assembly to 64-bit using vim regex as mentioned in my question: <a href="https://stackoverflow.com/questions/11897116/how-to-convert-linux-32-bit-gcc-inline-assembly-to-64-bit-code">How to convert Linux 32-bit gcc inline assembly to 64-bit code?</a></p>
  
  <p>I'm using gcc with <code>-m64</code> flag. The target environment is Linux x86-64, custom kernel version 3.5.5.</p>
  
  <p>The <code>Makefile</code> is the following:</p>
  
  <pre><code>EXTRA_CFLAGS +=  -O2 -Wall -DLINUX_26 -m64
  
  OBJ          := module_nux.o breakpoint.o buffering.o command.o disasmbak.o idt.o 
  OBJ          += keyboard.o page.o video.o utils.o import_symb.o core_rr0d.o pci.o
  MODULE       := rr0d.o 
  
  obj-m        := $(MODULE)
  rr0d-objs    := $(OBJ)
  
  default:
      make -C /lib/modules/`uname -r`/build/ SUBDIRS=`pwd` modules
  
  clean:
      rm -f  *.o .*.o.cmd .*.ko.cmd *.mod.c  *~ 
      rm -rf .tmp_versions
  
  mrproper:
      make clean
      rm -f *.ko
  </code></pre>
  
  <p><code>make</code> gives a lot of warnings like <code>warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]</code> and <code>warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]</code>, but these are probably irrelevant to the topic.</p>
  
  <p>The last rows of the output of <code>make</code> are probably the important ones:</p>
  
  <pre><code>/home/user/code/rr0d/0.3/core_rr0d.c: In function ‘cleanup_rr0d’:
  /home/user/code/rr0d/0.3/core_rr0d.c:1938:36: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    CC [M]  /home/user/code/rr0d/0.3/pci.o
    LD [M]  /home/user/code/rr0d/0.3/rr0d.o
    Building modules, stage 2.
    MODPOST 1 modules
  WARNING: "RING_HOOO_SEGMENT" [/home/user/code/rr0d/0.3/rr0d.ko] undefined!
    CC      /home/user/code/rr0d/0.3/rr0d.mod.o
    LD [M]  /home/user/code/rr0d/0.3/rr0d.ko
  make[1]: Leaving directory `/home/user/code/kernel/linux-3.5.5'
  </code></pre>
  
  <p>So, <code>RING_HOOO_SEGMENT</code> is undefined.</p>
  
  <p>When I try to <code>insmod</code> the module with <code>insmod ./rr0d.ko</code> as root, I get:</p>
  
  <pre><code>Error: could not insert module ./rr0d.ko: Unknown symbol in module
  </code></pre>
  
  <p>Checking with <code>dmesg | tail -n 1</code> gives the following output:</p>
  
  <pre><code>[15975.412346] rr0d: Unknown symbol RING_HOOO_SEGMENT (err 0)
  </code></pre>
  
  <p>So, the unknown symbol definitively is <code>RING_HOOO_SEGMENT</code>.</p>
  
  <p><code>RING_HOOO_SEGMENT</code> is a constant created with <code>#define</code> in <code>vars.h</code>, that is included in several <code>.c</code> files with <code>#include "vars.h"</code>.</p>
  
  <p>The essential <code>#ifdef</code> block of <code>vars.h</code> with <code>#define RING_HOOO_SEGMENT</code> is this one:</p>
  
  <pre><code>#ifdef LINUX_26
  
  #define fake_naked
  
  #if defined(__GNUC__)
  // the line below is the important one.
  #define RING_HOOO_SEGMENT "$0x7b"
  //#define       RING_HOOO_SEGMENT "$0x60"
  #elif defined(_MSC_VER)
  #define RING_HOOO_SEGMENT 0x7b
  #endif
  
  #else /* LINUX_24 */
  
  #define fake_naked _asm_("\t" \
                       "add $0x08, %esp\n\t" \
                       "popl %ebp\n" \
  );
  
  #if defined(__GNUC__)
  #define RING_HOOO_SEGMENT "$0x18"
  #elif defined(_MSC_VER)
  #define RING_HOOO_SEGMENT 0x18
  #endif
  
  #define RING_HOOO_SEGMENT_VALUE 0x18
  
  #endif /* LINUX_26 */
  </code></pre>
  
  <p>Obviously if <code>#define RING_HOOO_SEGMENT "$0x7b"</code> (in <code>#if defined(__GNUC__)</code> inside <code>#ifdef LINUX_26</code>) is commented out, the code won't compile, so it's clear that <code>RING_HOOO_SEGMENT</code> gets defined.</p>
  
  <p>Grepping for <code>RING_HOOO_SEGMENT</code> gives the following matches:</p>
  
  <pre><code>$ grep 'RING_HOOO_SEGMENT' *.c *.o *.ko
  
  core_rr0d.c:    "movq RING_HOOO_SEGMENT, %rax\n\t"\
  core_rr0d.c:  __asm{    movq RING_HOOO_SEGMENT, %rax}\
  Binary file rr0d.ko matches
  </code></pre>
  
  <p>Both <code>core_rr0d.c</code> rows are inline assembly. <code>core_rr0d.c</code> contains <code>#include "vars.h"</code> so that should be fine.</p>
  
  <p>Also the binary module <code>rr0d.ko</code> matches, so it contains the string <code>RING_HOOO_SEGMENT</code> (in some form), even if <code>insmod ./rr0d.ko</code> fails with <code>Error: could not insert module ./rr0d.ko: Unknown symbol in module</code>.</p>
  
  <p>Any ideas what might the reason for this problem and how to proceed to be able to <code>insmod</code> the module?</p>