i386 kernel modules unusable due to .plt sections

From: Tijl Coosemans <tijl_at_FreeBSD.org>
Date: Fri, 27 Aug 2021 13:41:30 UTC
Hi,

I use devel/llvm* to build base and just switched to llvm12.  It seems
that on i386 clang12 uses R_386_PLT32 relocations for some calls to at
least memset, memcpy and __stack_chk_fail (clang11 uses R_386_PC32).
These are converted to R_386_JMP_SLOT relocations by the linker which
aren't supported by the kernel, e.g. loading linux.ko gives "kldload:
unexpected relocation type" from sys/i386/i386/elf_machdep.c.  The PLT
entries also depend on a base pointer in %ebx but kernel modules aren't
compiled with -fPIC, so this can't work and I suspect this is a
regression in clang12.

The following code shows the difference between clang11 and clang12:

--------
#include <string.h>

void *
test_memset(void *p, int c, size_t len) {
        return (memset(p, c, len));
}

void *
test_memcpy(void *dst, const void *src, size_t len) {
        return (memcpy(dst, src, len));
}

void *
test_memmove(void *dst, const void *src, size_t len) {
        return (memmove(dst, src, len));
}
--------

Output of "readelf -r test.o" when compiled with "clang12 -c test.c -m32":
r_offset r_info   r_type              st_value st_name
0000002c 00000504 R_386_PLT32         00000000 memset
00000067 00000304 R_386_PLT32         00000000 memcpy
000000a7 00000402 R_386_PC32          00000000 memmove

With clang11:
r_offset r_info   r_type              st_value st_name
00000036 00000502 R_386_PC32          00000000 memset
00000083 00000302 R_386_PC32          00000000 memcpy
000000d2 00000402 R_386_PC32          00000000 memmove