Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kallsyms should prefer non weak symbols

When resolving symbol names from addresses with aliased symbol names,
kallsyms_lookup always returns the first symbol, even if it is a weak
symbol.

This patch changes this by sorting the symbols with the weak symbols last
before feeding them to the kernel. This way the kernel runtime isn't
changed at all, only the kallsyms build system is changed.

Another side effect is that the symbols get sorted by address, too. So,
even if future binutils version have some bug in "nm" that makes it fail to
correctly sort symbols by address, the kernel won't be affected by this.

Mathieu says:

I created a module in LTTng that uses kallsyms to get the symbol
corresponding to a specific system call address. Unfortunately, all the
unimplemented syscalls were all referring to the (same) weak symbol
identifying an unrelated system call rather that sys_ni (or whatever
non-weak symbol would be expected). Kallsyms was dumbly returning the first
symbol that matched.

This patch makes sure kallsyms returns the non-weak symbol when there is
one, which seems to be the expected result.

[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Looks-great-to: Rusty Russell <rusty@rustcorp.com.au>
Cc: Sam Ravnborg <sam@ravnborg.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Paulo Marques and committed by
Linus Torvalds
f2df3f65 a6752f3f

+34 -3
+34 -3
scripts/kallsyms.c
··· 31 31 32 32 #define KSYM_NAME_LEN 128 33 33 34 - 35 34 struct sym_entry { 36 35 unsigned long long addr; 37 36 unsigned int len; 37 + unsigned int start_pos; 38 38 unsigned char *sym; 39 39 }; 40 - 41 40 42 41 static struct sym_entry *table; 43 42 static unsigned int table_size, table_cnt; ··· 197 198 exit (1); 198 199 } 199 200 } 200 - if (read_symbol(in, &table[table_cnt]) == 0) 201 + if (read_symbol(in, &table[table_cnt]) == 0) { 202 + table[table_cnt].start_pos = table_cnt; 201 203 table_cnt++; 204 + } 202 205 } 203 206 } 204 207 ··· 503 502 optimize_result(); 504 503 } 505 504 505 + static int compare_symbols(const void *a, const void *b) 506 + { 507 + const struct sym_entry *sa; 508 + const struct sym_entry *sb; 509 + int wa, wb; 510 + 511 + sa = a; 512 + sb = b; 513 + 514 + /* sort by address first */ 515 + if (sa->addr > sb->addr) 516 + return 1; 517 + if (sa->addr < sb->addr) 518 + return -1; 519 + 520 + /* sort by "weakness" type */ 521 + wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W'); 522 + wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W'); 523 + if (wa != wb) 524 + return wa - wb; 525 + 526 + /* sort by initial order, so that other symbols are left undisturbed */ 527 + return sa->start_pos - sb->start_pos; 528 + } 529 + 530 + static void sort_symbols(void) 531 + { 532 + qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols); 533 + } 506 534 507 535 int main(int argc, char **argv) 508 536 { ··· 553 523 usage(); 554 524 555 525 read_map(stdin); 526 + sort_symbols(); 556 527 optimize_token_table(); 557 528 write_src(); 558 529