Skip to content

Commit 554bdfe

Browse files
Jan Beulichrustyrussell
authored andcommitted
module: reduce string table for loaded modules (v2)
Also remove all parts of the string table (referenced by the symbol table) that are not needed for kallsyms use (i.e. which were only referenced by symbols discarded by the previous patch, or not referenced at all for whatever reason). Signed-off-by: Jan Beulich <jbeulich@novell.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 4a49622 commit 554bdfe

2 files changed

Lines changed: 57 additions & 13 deletions

File tree

‎include/linux/module.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ struct module
315315
*/
316316
Elf_Sym *symtab, *core_symtab;
317317
unsigned int num_symtab, core_num_syms;
318-
char *strtab;
318+
char *strtab, *core_strtab;
319319

320320
/* Section attributes */
321321
struct module_sect_attrs *sect_attrs;

‎kernel/module.c‎

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,12 +1886,17 @@ static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
18861886
static unsigned long layout_symtab(struct module *mod,
18871887
Elf_Shdr *sechdrs,
18881888
unsigned int symindex,
1889+
unsigned int strindex,
18891890
const Elf_Ehdr *hdr,
1890-
const char *secstrings)
1891+
const char *secstrings,
1892+
unsigned long *pstroffs,
1893+
unsigned long *strmap)
18911894
{
18921895
unsigned long symoffs;
18931896
Elf_Shdr *symsect = sechdrs + symindex;
1897+
Elf_Shdr *strsect = sechdrs + strindex;
18941898
const Elf_Sym *src;
1899+
const char *strtab;
18951900
unsigned int i, nsrc, ndst;
18961901

18971902
/* Put symbol section at end of init part of module. */
@@ -1902,14 +1907,31 @@ static unsigned long layout_symtab(struct module *mod,
19021907

19031908
src = (void *)hdr + symsect->sh_offset;
19041909
nsrc = symsect->sh_size / sizeof(*src);
1910+
strtab = (void *)hdr + strsect->sh_offset;
19051911
for (ndst = i = 1; i < nsrc; ++i, ++src)
1906-
if (is_core_symbol(src, sechdrs, hdr->e_shnum))
1912+
if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1913+
unsigned int j = src->st_name;
1914+
1915+
while(!__test_and_set_bit(j, strmap) && strtab[j])
1916+
++j;
19071917
++ndst;
1918+
}
19081919

19091920
/* Append room for core symbols at end of core part. */
19101921
symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
19111922
mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
19121923

1924+
/* Put string table section at end of init part of module. */
1925+
strsect->sh_flags |= SHF_ALLOC;
1926+
strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1927+
strindex) | INIT_OFFSET_MASK;
1928+
DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1929+
1930+
/* Append room for core symbols' strings at end of core part. */
1931+
*pstroffs = mod->core_size;
1932+
__set_bit(0, strmap);
1933+
mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1934+
19131935
return symoffs;
19141936
}
19151937

@@ -1919,11 +1941,14 @@ static void add_kallsyms(struct module *mod,
19191941
unsigned int symindex,
19201942
unsigned int strindex,
19211943
unsigned long symoffs,
1922-
const char *secstrings)
1944+
unsigned long stroffs,
1945+
const char *secstrings,
1946+
unsigned long *strmap)
19231947
{
19241948
unsigned int i, ndst;
19251949
const Elf_Sym *src;
19261950
Elf_Sym *dst;
1951+
char *s;
19271952

19281953
mod->symtab = (void *)sechdrs[symindex].sh_addr;
19291954
mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
@@ -1941,16 +1966,25 @@ static void add_kallsyms(struct module *mod,
19411966
if (!is_core_symbol(src, sechdrs, shnum))
19421967
continue;
19431968
dst[ndst] = *src;
1969+
dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
19441970
++ndst;
19451971
}
19461972
mod->core_num_syms = ndst;
1973+
1974+
mod->core_strtab = s = mod->module_core + stroffs;
1975+
for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1976+
if (test_bit(i, strmap))
1977+
*++s = mod->strtab[i];
19471978
}
19481979
#else
19491980
static inline unsigned long layout_symtab(struct module *mod,
19501981
Elf_Shdr *sechdrs,
19511982
unsigned int symindex,
1983+
unsigned int strindex,
19521984
const Elf_Hdr *hdr,
1953-
const char *secstrings)
1985+
const char *secstrings,
1986+
unsigned long *pstroffs,
1987+
unsigned long *strmap)
19541988
{
19551989
}
19561990
static inline void add_kallsyms(struct module *mod,
@@ -1959,7 +1993,9 @@ static inline void add_kallsyms(struct module *mod,
19591993
unsigned int symindex,
19601994
unsigned int strindex,
19611995
unsigned long symoffs,
1962-
const char *secstrings)
1996+
unsigned long stroffs,
1997+
const char *secstrings,
1998+
const unsigned long *strmap)
19631999
{
19642000
}
19652001
#endif /* CONFIG_KALLSYMS */
@@ -2035,7 +2071,7 @@ static noinline struct module *load_module(void __user *umod,
20352071
long err = 0;
20362072
void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
20372073
#ifdef CONFIG_KALLSYMS
2038-
unsigned long symoffs;
2074+
unsigned long symoffs, stroffs, *strmap;
20392075
#endif
20402076
mm_segment_t old_fs;
20412077

@@ -2118,10 +2154,6 @@ static noinline struct module *load_module(void __user *umod,
21182154
/* Don't keep modinfo and version sections. */
21192155
sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
21202156
sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2121-
#ifdef CONFIG_KALLSYMS
2122-
/* Keep string table for decoding later. */
2123-
sechdrs[strindex].sh_flags |= SHF_ALLOC;
2124-
#endif
21252157

21262158
/* Check module struct version now, before we try to use module. */
21272159
if (!check_modstruct_version(sechdrs, versindex, mod)) {
@@ -2157,6 +2189,13 @@ static noinline struct module *load_module(void __user *umod,
21572189
goto free_hdr;
21582190
}
21592191

2192+
strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2193+
* sizeof(long), GFP_KERNEL);
2194+
if (!strmap) {
2195+
err = -ENOMEM;
2196+
goto free_mod;
2197+
}
2198+
21602199
if (find_module(mod->name)) {
21612200
err = -EEXIST;
21622201
goto free_mod;
@@ -2186,7 +2225,8 @@ static noinline struct module *load_module(void __user *umod,
21862225
this is done generically; there doesn't appear to be any
21872226
special cases for the architectures. */
21882227
layout_sections(mod, hdr, sechdrs, secstrings);
2189-
symoffs = layout_symtab(mod, sechdrs, symindex, hdr, secstrings);
2228+
symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2229+
secstrings, &stroffs, strmap);
21902230

21912231
/* Do the allocs. */
21922232
ptr = module_alloc_update_bounds(mod->core_size);
@@ -2392,7 +2432,9 @@ static noinline struct module *load_module(void __user *umod,
23922432
sechdrs[pcpuindex].sh_size);
23932433

23942434
add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2395-
symoffs, secstrings);
2435+
symoffs, stroffs, secstrings, strmap);
2436+
kfree(strmap);
2437+
strmap = NULL;
23962438

23972439
if (!mod->taints) {
23982440
struct _ddebug *debug;
@@ -2481,6 +2523,7 @@ static noinline struct module *load_module(void __user *umod,
24812523
percpu_modfree(percpu);
24822524
free_mod:
24832525
kfree(args);
2526+
kfree(strmap);
24842527
free_hdr:
24852528
vfree(hdr);
24862529
return ERR_PTR(err);
@@ -2573,6 +2616,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
25732616
#ifdef CONFIG_KALLSYMS
25742617
mod->num_symtab = mod->core_num_syms;
25752618
mod->symtab = mod->core_symtab;
2619+
mod->strtab = mod->core_strtab;
25762620
#endif
25772621
module_free(mod, mod->module_init);
25782622
mod->module_init = NULL;

0 commit comments

Comments
 (0)