📄 modpost.c
字号:
**/static int strrcmp(const char *s, const char *sub){ int slen, sublen; if (!s || !sub) return 1; slen = strlen(s); sublen = strlen(sub); if ((slen == 0) || (sublen == 0)) return 1; if (sublen > slen) return 1; return memcmp(s + slen - sublen, sub, sublen);}/* * Functions used only during module init is marked __init and is stored in * a .init.text section. Likewise data is marked __initdata and stored in * a .init.data section. * If this section is one of these sections return 1 * See include/linux/init.h for the details */static int init_section(const char *name){ if (strcmp(name, ".init") == 0) return 1; if (strncmp(name, ".init.", strlen(".init.")) == 0) return 1; return 0;}/* * Functions used only during module exit is marked __exit and is stored in * a .exit.text section. Likewise data is marked __exitdata and stored in * a .exit.data section. * If this section is one of these sections return 1 * See include/linux/init.h for the details **/static int exit_section(const char *name){ if (strcmp(name, ".exit.text") == 0) return 1; if (strcmp(name, ".exit.data") == 0) return 1; return 0;}/* * Data sections are named like this: * .data | .data.rel | .data.rel.* * Return 1 if the specified section is a data section */static int data_section(const char *name){ if ((strcmp(name, ".data") == 0) || (strcmp(name, ".data.rel") == 0) || (strncmp(name, ".data.rel.", strlen(".data.rel.")) == 0)) return 1; else return 0;}/** * Whitelist to allow certain references to pass with no warning. * * Pattern 0: * Do not warn if funtion/data are marked with __init_refok/__initdata_refok. * The pattern is identified by: * fromsec = .text.init.refok* | .data.init.refok* * * Pattern 1: * If a module parameter is declared __initdata and permissions=0 * then this is legal despite the warning generated. * We cannot see value of permissions here, so just ignore * this pattern. * The pattern is identified by: * tosec = .init.data * fromsec = .data* * atsym =__param* * * Pattern 2: * Many drivers utilise a *driver container with references to * add, remove, probe functions etc. * These functions may often be marked __init and we do not want to * warn here. * the pattern is identified by: * tosec = init or exit section * fromsec = data section * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer * * Pattern 3: * Whitelist all refereces from .text.head to .init.data * Whitelist all refereces from .text.head to .init.text * * Pattern 4: * Some symbols belong to init section but still it is ok to reference * these from non-init sections as these symbols don't have any memory * allocated for them and symbol address and value are same. So even * if init section is freed, its ok to reference those symbols. * For ex. symbols marking the init section boundaries. * This pattern is identified by * refsymname = __init_begin, _sinittext, _einittext * * Pattern 5: * Xtensa uses literal sections for constants that are accessed PC-relative. * Literal sections may safely reference their text sections. * (Note that the name for the literal section omits any trailing '.text') * tosec = <section>[.text] * fromsec = <section>.literal **/static int secref_whitelist(const char *modname, const char *tosec, const char *fromsec, const char *atsym, const char *refsymname){ int len; const char **s; const char *pat2sym[] = { "driver", "_template", /* scsi uses *_template a lot */ "_timer", /* arm uses ops structures named _timer a lot */ "_sht", /* scsi also used *_sht to some extent */ "_ops", "_probe", "_probe_one", "_console", NULL }; const char *pat3refsym[] = { "__init_begin", "_sinittext", "_einittext", NULL }; /* Check for pattern 0 */ if ((strncmp(fromsec, ".text.init.refok", strlen(".text.init.refok")) == 0) || (strncmp(fromsec, ".exit.text.refok", strlen(".exit.text.refok")) == 0) || (strncmp(fromsec, ".data.init.refok", strlen(".data.init.refok")) == 0)) return 1; /* Check for pattern 1 */ if ((strcmp(tosec, ".init.data") == 0) && (strncmp(fromsec, ".data", strlen(".data")) == 0) && (strncmp(atsym, "__param", strlen("__param")) == 0)) return 1; /* Check for pattern 2 */ if ((init_section(tosec) || exit_section(tosec)) && data_section(fromsec)) for (s = pat2sym; *s; s++) if (strrcmp(atsym, *s) == 0) return 1; /* Check for pattern 3 */ if ((strcmp(fromsec, ".text.head") == 0) && ((strcmp(tosec, ".init.data") == 0) || (strcmp(tosec, ".init.text") == 0))) return 1; /* Check for pattern 4 */ for (s = pat3refsym; *s; s++) if (strcmp(refsymname, *s) == 0) return 1; /* Check for pattern 5 */ if (strrcmp(tosec, ".text") == 0) len = strlen(tosec) - strlen(".text"); else len = strlen(tosec); if ((strncmp(tosec, fromsec, len) == 0) && (strlen(fromsec) > len) && (strcmp(fromsec + len, ".literal") == 0)) return 1; return 0;}/** * Find symbol based on relocation record info. * In some cases the symbol supplied is a valid symbol so * return refsym. If st_name != 0 we assume this is a valid symbol. * In other cases the symbol needs to be looked up in the symbol table * based on section and address. * **/static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, Elf_Sym *relsym){ Elf_Sym *sym; if (relsym->st_name != 0) return relsym; for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { if (sym->st_shndx != relsym->st_shndx) continue; if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) continue; if (sym->st_value == addr) return sym; } return NULL;}static inline int is_arm_mapping_symbol(const char *str){ return str[0] == '$' && strchr("atd", str[1]) && (str[2] == '\0' || str[2] == '.');}/* * If there's no name there, ignore it; likewise, ignore it if it's * one of the magic symbols emitted used by current ARM tools. * * Otherwise if find_symbols_between() returns those symbols, they'll * fail the whitelist tests and cause lots of false alarms ... fixable * only by merging __exit and __init sections into __text, bloating * the kernel (which is especially evil on embedded platforms). */static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym){ const char *name = elf->strtab + sym->st_name; if (!name || !strlen(name)) return 0; return !is_arm_mapping_symbol(name);}/* * Find symbols before or equal addr and after addr - in the section sec. * If we find two symbols with equal offset prefer one with a valid name. * The ELF format may have a better way to detect what type of symbol * it is, but this works for now. **/static void find_symbols_between(struct elf_info *elf, Elf_Addr addr, const char *sec, Elf_Sym **before, Elf_Sym **after){ Elf_Sym *sym; Elf_Ehdr *hdr = elf->hdr; Elf_Addr beforediff = ~0; Elf_Addr afterdiff = ~0; const char *secstrings = (void *)hdr + elf->sechdrs[hdr->e_shstrndx].sh_offset; *before = NULL; *after = NULL; for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { const char *symsec; if (sym->st_shndx >= SHN_LORESERVE) continue; symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; if (strcmp(symsec, sec) != 0) continue; if (!is_valid_name(elf, sym)) continue; if (sym->st_value <= addr) { if ((addr - sym->st_value) < beforediff) { beforediff = addr - sym->st_value; *before = sym; } else if ((addr - sym->st_value) == beforediff) { *before = sym; } } else { if ((sym->st_value - addr) < afterdiff) { afterdiff = sym->st_value - addr; *after = sym; } else if ((sym->st_value - addr) == afterdiff) { *after = sym; } } }}/** * Print a warning about a section mismatch. * Try to find symbols near it so user can find it. * Check whitelist before warning - it may be a false positive. **/static void warn_sec_mismatch(const char *modname, const char *fromsec, struct elf_info *elf, Elf_Sym *sym, Elf_Rela r){ const char *refsymname = ""; Elf_Sym *before, *after; Elf_Sym *refsym; Elf_Ehdr *hdr = elf->hdr; Elf_Shdr *sechdrs = elf->sechdrs; const char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name; find_symbols_between(elf, r.r_offset, fromsec, &before, &after); refsym = find_elf_symbol(elf, r.r_addend, sym); if (refsym && strlen(elf->strtab + refsym->st_name)) refsymname = elf->strtab + refsym->st_name; /* check whitelist - we may ignore it */ if (secref_whitelist(modname, secname, fromsec, before ? elf->strtab + before->st_name : "", refsymname)) return; if (before && after) { warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " "(between '%s' and '%s')\n", modname, fromsec, (unsigned long long)r.r_offset, secname, refsymname, elf->strtab + before->st_name, elf->strtab + after->st_name); } else if (before) { warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " "(after '%s')\n", modname, fromsec, (unsigned long long)r.r_offset, secname, refsymname, elf->strtab + before->st_name); } else if (after) { warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " "before '%s' (at offset -0x%llx)\n", modname, fromsec, (unsigned long long)r.r_offset, secname, refsymname, elf->strtab + after->st_name); } else { warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n", modname, fromsec, (unsigned long long)r.r_offset, secname, refsymname); }}static unsigned int *reloc_location(struct elf_info *elf, int rsection, Elf_Rela *r){ Elf_Shdr *sechdrs = elf->sechdrs; int section = sechdrs[rsection].sh_info; return (void *)elf->hdr + sechdrs[section].sh_offset + (r->r_offset - sechdrs[section].sh_addr);}static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r){ unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int *location = reloc_location(elf, rsection, r); switch (r_typ) { case R_386_32: r->r_addend = TO_NATIVE(*location); break; case R_386_PC32: r->r_addend = TO_NATIVE(*location) + 4; /* For CONFIG_RELOCATABLE=y */ if (elf->hdr->e_type == ET_EXEC) r->r_addend += r->r_offset; break; } return 0;}static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r){ unsigned int r_typ = ELF_R_TYPE(r->r_info); switch (r_typ) { case R_ARM_ABS32: /* From ARM ABI: (S + A) | T */ r->r_addend = (int)(long)(elf->symtab_start + ELF_R_SYM(r->r_info)); break; case R_ARM_PC24: /* From ARM ABI: ((S + A) | T) - P */ r->r_addend = (int)(long)(elf->hdr + elf->sechdrs[rsection].sh_offset + (r->r_offset - elf->sechdrs[rsection].sh_addr)); break; default: return 1; } return 0;}static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r){ unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int *location = reloc_location(elf, rsection, r); unsigned int inst; if (r_typ == R_MIPS_HI16) return 1; /* skip this */ inst = TO_NATIVE(*location); switch (r_typ) { case R_MIPS_LO16: r->r_addend = inst & 0xffff; break; case R_MIPS_26: r->r_addend = (inst & 0x03ffffff) << 2; break; case R_MIPS_32: r->r_addend = inst; break; } return 0;}/** * A module includes a number of sections that are discarded * either when loaded or when used as built-in. * For loaded modules all functions marked __init and all data * marked __initdata will be discarded when the module has been intialized. * Likewise for modules used built-in the sections marked __exit * are discarded because __exit marked function are supposed to be called * only when a moduel is unloaded which never happes for built-in modules. * The check_sec_ref() function traverses all relocation records * to find all references to a section that reference a section that will * be discarded and warns about it. **/static void check_sec_ref(struct module *mod, const char *modname, struct elf_info *elf, int section(const char*), int section_ref_ok(const char *)){ int i; Elf_Sym *sym; Elf_Ehdr *hdr = elf->hdr; Elf_Shdr *sechdrs = elf->sechdrs; const char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; /* Walk through all sections */ for (i = 0; i < hdr->e_shnum; i++) { const char *name = secstrings + sechdrs[i].sh_name; const char *secname; Elf_Rela r; unsigned int r_sym; /* We want to process only relocation sections and not .init */ if (sechdrs[i].sh_type == SHT_RELA) { Elf_Rela *rela; Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset; Elf_Rela *stop = (void*)start + sechdrs[i].sh_size; name += strlen(".rela"); if (section_ref_ok(name)) continue; for (rela = start; rela < stop; rela++) { r.r_offset = TO_NATIVE(rela->r_offset);#if KERNEL_ELFCLASS == ELFCLASS64 if (hdr->e_machine == EM_MIPS) { unsigned int r_typ; r_sym = ELF64_MIPS_R_SYM(rela->r_info); r_sym = TO_NATIVE(r_sym); r_typ = ELF64_MIPS_R_TYPE(rela->r_info); r.r_info = ELF64_R_INFO(r_sym, r_typ); } else { r.r_info = TO_NATIVE(rela->r_info); r_sym = ELF_R_SYM(r.r_info); }#else r.r_info = TO_NATIVE(rela->r_info); r_sym = ELF_R_SYM(r.r_info);#endif r.r_addend = TO_NATIVE(rela->r_addend); sym = elf->symtab_start + r_sym; /* Skip special sections */ if (sym->st_shndx >= SHN_LORESERVE) continue; secname = secstrings + sechdrs[sym->st_shndx].sh_name; if (section(secname)) warn_sec_mismatch(modname, name, elf, sym, r); } } else if (sechdrs[i].sh_type == SHT_REL) { Elf_Rel *rel; Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset; Elf_Rel *stop = (void*)start + sechdrs[i].sh_size; name += strlen(".rel"); if (section_ref_ok(name)) continue; for (rel = start; rel < stop; rel++) { r.r_offset = TO_NATIVE(rel->r_offset);#if KERNEL_ELFCLASS == ELFCLASS64 if (hdr->e_machine == EM_MIPS) { unsigned int r_typ; r_sym = ELF64_MIPS_R_SYM(rel->r_info); r_sym = TO_NATIVE(r_sym); r_typ = ELF64_MIPS_R_TYPE(rel->r_info); r.r_info = ELF64_R_INFO(r_sym, r_typ); } else { r.r_info = TO_NATIVE(rel->r_info); r_sym = ELF_R_SYM(r.r_info); }#else r.r_info = TO_NATIVE(rel->r_info); r_sym = ELF_R_SYM(r.r_info);#endif r.r_addend = 0; switch (hdr->e_machine) { case EM_386: if (addend_386_rel(elf, i, &r)) continue; break; case EM_ARM: if(addend_arm_rel(elf, i, &r)) continue; break; case EM_MIPS: if (addend_mips_rel(elf, i, &r)) continue; break; } sym = elf->symtab_start + r_sym; /* Skip special sections */ if (sym->st_shndx >= SHN_LORESERVE) continue; secname = secstrings + sechdrs[sym->st_shndx].sh_name; if (section(secname)) warn_sec_mismatch(modname, name, elf, sym, r); } } }}/* * Identify sections from which references to either a * .init or a .exit section is OK. * * [OPD] Keith Ownes <kaos@sgi.com> commented: * For our future {in}sanity, add a comment that this is the ppc .opd * section, not the ia64 .opd section. * ia64 .opd should not point to discarded sections. * [.rodata] like for .init.text we ignore .rodata references -same reason */static int initexit_section_ref_ok(const char *name){ const char **s; /* Absolute section names */ const char *namelist1[] = { "__bug_table", /* used by powerpc for BUG() */ "__ex_table", ".altinstructions", ".cranges", /* used by sh64 */ ".fixup", ".machvec", /* ia64 + powerpc uses these */ ".machine.desc", ".opd", /* See comment [OPD] */ "__dbe_table", ".parainstructions", ".pdr", ".plt", /* seen on ARCH=um build on x86_64. Harmless */ ".smp_locks", ".stab", ".m68k_fixup", ".xt.prop", /* xtensa informational section */ ".xt.lit", /* xtensa informational section */ NULL }; /* Start of section names */ const char *namelist2[] = { ".debug", ".eh_frame", ".note", /* ignore ELF notes - may contain anything */ ".got", /* powerpc - global offset table */ ".toc", /* powerpc - table of contents */ NULL }; /* part of section name */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -