While trying to build an Android kernel from source, I ran into the following error:

  LD      drivers/net/built-in.o
  LD      drivers/usb/gadget/g_android.o
  LD      drivers/usb/gadget/built-in.o
  LD      drivers/usb/built-in.o
  LD      drivers/built-in.o
  LD      vmlinux.o
  MODPOST vmlinux.o
ERROR: modpost: Found 13 section mismatch(es).
To see full details build your kernel with:
'make CONFIG_DEBUG_SECTION_MISMATCH=y'
To build the kernel despite the mismatches, build with:
'make CONFIG_NO_ERROR_ON_MISMATCH=y'
(NOTE: This is not recommended)
/home/users/ideaman924/android_kernel_samsung_msm8974/scripts/Makefile.modpost:98: recipe for target 'vmlinux.o' failed
make[1]: *** [vmlinux.o] Error 1
Makefile:947: recipe for target 'vmlinux.o' failed
make: *** [vmlinux.o] Error 2
ideaman924@build:~/android_kernel_samsung_msm8974$

Strangely, using different defconfigs or other kernel sources all led me to this weird modpost section mismatch error. I researched the issue and found some Stack Overflow posts, none of which were very helpful. Here are some of the posts I read:

But this was a well-established kernel source straight from LineageOS. I thought code being broken straight from LineageOS’s repositories was unlikely, so I looked around some more for another solution.

Turns out, it wasn’t a code problem, but rather a toolchain problem. @mochi_wwww from the Linux kernel chat on Telegram figured out that I was using the incorrect toolchain - I had been using a completely outdated toolchain from several years ago. So embarrassing.

Now, to my defense, the reference Android kernel building guide links to that toolchain. I just didn’t know that it was outdated. Using the proper toolchain magically solved the problem.

So now you know - if you ever run into these weird errors above, try using the correct toolchain for your kernel. It is usually shown in AndroidKernel.mk if you are using kernel sources from LineageOS. Don’t be an idiot like me and spend hours debugging a code problem that isn’t really a code problem!