Skip to content

Commit 85bd2fd

Browse files
committed
kbuild: fix section mismatch check for vmlinux
vmlinux does not contain relocation entries which is used by the section mismatch checks. Reported by: Atsushi Nemoto <anemo@mba.ocn.ne.jp> Use the individual objects as inputs to overcome this limitation. In modpost check the .o files and skip non-ELF files. Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
1 parent dc87c39 commit 85bd2fd

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ vmlinux-init := $(head-y) $(init-y)
603603
vmlinux-main := $(core-y) $(libs-y) $(drivers-y) $(net-y)
604604
vmlinux-all := $(vmlinux-init) $(vmlinux-main)
605605
vmlinux-lds := arch/$(ARCH)/kernel/vmlinux.lds
606+
export KBUILD_VMLINUX_OBJS := $(vmlinux-all)
606607

607608
# Rule to link vmlinux - also used during CONFIG_KALLSYMS
608609
# May be overridden by arch/$(ARCH)/Makefile

‎scripts/Makefile.modpost‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ quiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules
6363
$(if $(KBUILD_EXTMOD),-i,-o) $(kernelsymfile) \
6464
$(if $(KBUILD_EXTMOD),-I $(modulesymfile)) \
6565
$(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \
66-
$(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) \
67-
$(wildcard vmlinux) $(filter-out FORCE,$^)
66+
$(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w)
6867

6968
PHONY += __modpost
7069
__modpost: $(modules:.ko=.o) FORCE
71-
$(call cmd,modpost)
70+
$(call cmd,modpost) $(wildcard vmlinux) $(filter-out FORCE,$^)
7271

7372
quiet_cmd_kernel-mod = MODPOST $@
74-
cmd_kernel-mod = $(cmd_modpost)
73+
cmd_kernel-mod = $(cmd_modpost) $(KBUILD_VMLINUX_OBJS)
7574

75+
PHONY += vmlinux
7676
vmlinux: FORCE
7777
$(call cmd,kernel-mod)
7878

‎scripts/mod/modpost.c‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ void release_file(void *file, unsigned long size)
333333
munmap(file, size);
334334
}
335335

336-
static void parse_elf(struct elf_info *info, const char *filename)
336+
static int parse_elf(struct elf_info *info, const char *filename)
337337
{
338338
unsigned int i;
339-
Elf_Ehdr *hdr = info->hdr;
339+
Elf_Ehdr *hdr;
340340
Elf_Shdr *sechdrs;
341341
Elf_Sym *sym;
342342

@@ -346,9 +346,18 @@ static void parse_elf(struct elf_info *info, const char *filename)
346346
exit(1);
347347
}
348348
info->hdr = hdr;
349-
if (info->size < sizeof(*hdr))
350-
goto truncated;
351-
349+
if (info->size < sizeof(*hdr)) {
350+
/* file too small, assume this is an empty .o file */
351+
return 0;
352+
}
353+
/* Is this a valid ELF file? */
354+
if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
355+
(hdr->e_ident[EI_MAG1] != ELFMAG1) ||
356+
(hdr->e_ident[EI_MAG2] != ELFMAG2) ||
357+
(hdr->e_ident[EI_MAG3] != ELFMAG3)) {
358+
/* Not an ELF file - silently ignore it */
359+
return 0;
360+
}
352361
/* Fix endianness in ELF header */
353362
hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
354363
hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
@@ -371,8 +380,10 @@ static void parse_elf(struct elf_info *info, const char *filename)
371380
= (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
372381
const char *secname;
373382

374-
if (sechdrs[i].sh_offset > info->size)
375-
goto truncated;
383+
if (sechdrs[i].sh_offset > info->size) {
384+
fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
385+
return 0;
386+
}
376387
secname = secstrings + sechdrs[i].sh_name;
377388
if (strcmp(secname, ".modinfo") == 0) {
378389
info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
@@ -407,10 +418,7 @@ static void parse_elf(struct elf_info *info, const char *filename)
407418
sym->st_value = TO_NATIVE(sym->st_value);
408419
sym->st_size = TO_NATIVE(sym->st_size);
409420
}
410-
return;
411-
412-
truncated:
413-
fatal("%s is truncated.\n", filename);
421+
return 1;
414422
}
415423

416424
static void parse_elf_finish(struct elf_info *info)
@@ -1089,7 +1097,8 @@ static void read_symbols(char *modname)
10891097
struct elf_info info = { };
10901098
Elf_Sym *sym;
10911099

1092-
parse_elf(&info, modname);
1100+
if (!parse_elf(&info, modname))
1101+
return;
10931102

10941103
mod = new_module(modname);
10951104

0 commit comments

Comments
 (0)