title: undefined reference to__aeabi_ldivmod when building kernel for arm32
body: <p>When building one kernel image for arm32 platform, in the final linking, the error is:   </p>
  
  <blockquote>
    <p>arm-eabi-ld -EL -p --no-undefined -X --build-id -o .tmp_vmlinux1 -T obj/KERNEL/arch/arm/kernel/vmlinux.lds arch/arm/kernel/head.o init/built-in.o --start-group usr/built-in.o arch/arm/vfp/built-in.o arch/arm/kernel/built-in.o arch/arm/mm/built-in.o arch/arm/common/built-in.o arch/arm/net/built-in.o arch/arm/crypto/built-in.o arch/arm/mach-sc/built-in.o kernel/built-in.o mm/built-in.o fs/built-in.o ipc/built-in.o security/built-in.o crypto/built-in.o block/built-in.o arch/arm/lib/lib.a lib/lib.a arch/arm/lib/built-in.o lib/built-in.o drivers/built-in.o sound/built-in.o firmware/built-in.o arch/arm/oprofile/built-in.o net/built-in.o --end-group
    drivers/built-in.o:</p>
    
    <p><strong>undefined reference to `__aeabi_ldivmod'</strong></p>
    
    <p>make[2]: *** [vmlinux] Error 1</p>
  </blockquote>
  
  <hr>
  
  <p>I know the reason is my using 64bit divison for arm32 which 64bit is not supported, and using do_div() can get rid of the __aeabi_ldivmod error. I know __aeabi_ldivmod is defined in the libgcc.a, so I added the following code in my Kernel Makefile:  </p>
  
  <pre><code>--- a/Makefile  
  +++ b/Makefile  
  @@ -677,6 +677,7 @@   
  LDFLAGS_BUILD_ID = $(patsubst -Wl$(comma)%,%,\  
                            $(call cc-ldoption, -Wl$(comma)--build-id,))  
  KBUILD_LDFLAGS_MODULE += $(LDFLAGS_BUILD_ID)  
  LDFLAGS_vmlinux += $(LDFLAGS_BUILD_ID)  
  +LDFLAGS_vmlinux += -L$(MY_LIBPATH) -lgcc  
  </code></pre>
  
  <p>But it still can't work, so can anybody help on my questions:<br>
  <em>1) Why the libgcc.a is not linked default in the kernel building?<br>
  2) How to link the libgcc.a to fix the link error?</em></p>
  
  <hr>
  
  <h2>[Update]</h2>
  
  <p>OK, I found the following note for -lgcc in OSDev wiki: </p>
  
  <blockquote>
    <p><strong>-lgcc</strong><br>
    You disable the important libgcc library when you pass -nodefaultlibs (implied by -nostdlib). The compiler needs this library for many operations that it cannot do itself or that is more efficient to put into a shared function. <strong>You must pass this library near the end of the link line, after all the other object files and libraries, or the linker won't use it and you get strange linker errors</strong>.</p>
  </blockquote>
  
  <p>So I hard coded <code>-L${MYLIB_PATH} -lgcc into</code> into scripts/link-vmlinux.sh:  </p>
  
  <pre><code>--- a/scripts/link-vmlinux.sh
  +++ b/scripts/link-vmlinux.sh
  @@ -55,7 +55,7 @@ vmlinux_link()
          if [ "${SRCARCH}" != "um" ]; then
                  ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}                  \
                          -T ${lds} ${KBUILD_VMLINUX_INIT}                     \
  -                       --start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1}
  +                       --start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1} -L${MYLIB_PATH} -lgcc
          else
                  ${CC} ${CFLAGS_vmlinux} -o ${2}                              \
                          -Wl,-T,${lds} ${KBUILD_VMLINUX_INIT}                 \
  </code></pre>
  
  <p>Then the build went without __aeabi_ldivmod error.<br>
  While I want to find the better modifying than just harding coding and to know why kernel doesn't link libgcc default.</p>