Skip to content

Commit 774aab6

Browse files
committed
gendwarfksyms: Add symbol versioning
Add a basic CRC32 implementation adapted from genksyms, and produce matching output from symtypes strings in type_map. Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
1 parent 6a70751 commit 774aab6

File tree

8 files changed

+348
-17
lines changed

8 files changed

+348
-17
lines changed

scripts/gendwarfksyms/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ hostprogs-always-y += gendwarfksyms
22

33
gendwarfksyms-objs += gendwarfksyms.o
44
gendwarfksyms-objs += cache.o
5+
gendwarfksyms-objs += crc32.o
56
gendwarfksyms-objs += die.o
67
gendwarfksyms-objs += dwarf.o
78
gendwarfksyms-objs += symbols.o

scripts/gendwarfksyms/crc32.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Based on scripts/genksyms/genksyms.c, which has the following
4+
* notice:
5+
*
6+
* Generate kernel symbol version hashes.
7+
* Copyright 1996, 1997 Linux International.
8+
*
9+
* New implementation contributed by Richard Henderson <rth@tamu.edu>
10+
* Based on original work by Bjorn Ekwall <bj0rn@blox.se>
11+
*
12+
* This file was part of the Linux modutils 2.4.22: moved back into the
13+
* kernel sources by Rusty Russell/Kai Germaschewski.
14+
*/
15+
16+
const unsigned int crctab32[] = {
17+
0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
18+
0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
19+
0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
20+
0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
21+
0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
22+
0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
23+
0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
24+
0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
25+
0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
26+
0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
27+
0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
28+
0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
29+
0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
30+
0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
31+
0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
32+
0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
33+
0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
34+
0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
35+
0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
36+
0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
37+
0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
38+
0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
39+
0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
40+
0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
41+
0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
42+
0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
43+
0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
44+
0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
45+
0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
46+
0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
47+
0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
48+
0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
49+
0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
50+
0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
51+
0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
52+
0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
53+
0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
54+
0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
55+
0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
56+
0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
57+
0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
58+
0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
59+
0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
60+
0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
61+
0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
62+
0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
63+
0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
64+
0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
65+
0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
66+
0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
67+
0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
68+
0x2d02ef8dU
69+
};

scripts/gendwarfksyms/crc32.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Based on scripts/genksyms/genksyms.c, which has the following
4+
* notice:
5+
*
6+
* Generate kernel symbol version hashes.
7+
* Copyright 1996, 1997 Linux International.
8+
*
9+
* New implementation contributed by Richard Henderson <rth@tamu.edu>
10+
* Based on original work by Bjorn Ekwall <bj0rn@blox.se>
11+
*
12+
* This file was part of the Linux modutils 2.4.22: moved back into the
13+
* kernel sources by Rusty Russell/Kai Germaschewski.
14+
*/
15+
16+
#ifndef __CRC32_H
17+
#define __CRC32_H
18+
19+
extern const unsigned int crctab32[];
20+
21+
static inline unsigned long partial_crc32_one(unsigned char c,
22+
unsigned long crc)
23+
{
24+
return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8);
25+
}
26+
27+
static inline unsigned long partial_crc32(const char *s, unsigned long crc)
28+
{
29+
while (*s)
30+
crc = partial_crc32_one(*s++, crc);
31+
return crc;
32+
}
33+
34+
#endif /* __CRC32_H */

scripts/gendwarfksyms/dwarf.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,51 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die)
693693
/*
694694
* Exported symbol processing
695695
*/
696+
static int get_symbol_cache(struct state *state, Dwarf_Die *die,
697+
struct die **cache)
698+
{
699+
checkp(symbol_set_die(state->sym, die));
700+
check(die_map_get(die, SYMBOL, cache));
701+
702+
if ((*cache)->state != INCOMPLETE)
703+
return 1; /* We already processed a symbol for this DIE */
704+
705+
(*cache)->tag = dwarf_tag(die);
706+
return 0;
707+
}
708+
696709
static int process_subprogram(struct state *state, Dwarf_Die *die)
697710
{
698-
check(__process_subroutine_type(state, NULL, die, "subprogram"));
699-
state->sym->state = MAPPED;
711+
struct die *cache;
712+
713+
if (checkp(get_symbol_cache(state, die, &cache)) > 0)
714+
return 0;
715+
716+
debug("%s", state->sym->name);
717+
check(__process_subroutine_type(state, cache, die, "subprogram"));
718+
cache->state = SYMBOL;
719+
720+
if (dump_dies)
721+
fputs("\n", stderr);
722+
700723
return 0;
701724
}
702725

703726
static int process_variable(struct state *state, Dwarf_Die *die)
704727
{
705-
check(process(state, NULL, "variable "));
706-
check(process_type_attr(state, NULL, die));
707-
state->sym->state = MAPPED;
728+
struct die *cache;
729+
730+
if (checkp(get_symbol_cache(state, die, &cache)) > 0)
731+
return 0;
732+
733+
debug("%s", state->sym->name);
734+
check(process(state, cache, "variable "));
735+
check(process_type_attr(state, cache, die));
736+
cache->state = SYMBOL;
737+
738+
if (dump_dies)
739+
fputs("\n", stderr);
740+
708741
return 0;
709742
}
710743

@@ -750,7 +783,6 @@ static int process_exported_symbols(struct state *state, struct die *cache,
750783
if (!is_export_symbol(state, die))
751784
return 0;
752785

753-
debug("%s", state->sym->name);
754786
state_init(state);
755787

756788
if (is_symbol_ptr(get_name(&state->die)))
@@ -760,9 +792,6 @@ static int process_exported_symbols(struct state *state, struct die *cache,
760792
else
761793
check(process_variable(state, &state->die));
762794

763-
if (dump_dies)
764-
fputs("\n", stderr);
765-
766795
cache_clear_expanded(&state->expansion_cache);
767796
return 0;
768797
default:

scripts/gendwarfksyms/gendwarfksyms.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ bool dump_dies;
2222
bool dump_die_map;
2323
/* Print out type_map contents */
2424
bool dump_types;
25+
/* Print out expanded type strings used for version calculations */
26+
bool dump_versions;
2527
/* Produce a symtypes file */
2628
bool symtypes;
2729
static const char *symtypes_file;
@@ -35,6 +37,7 @@ static const struct {
3537
{ "--dump-dies", &dump_dies, NULL },
3638
{ "--dump-die-map", &dump_die_map, NULL },
3739
{ "--dump-types", &dump_types, NULL },
40+
{ "--dump-versions", &dump_versions, NULL },
3841
{ "--symtypes", &symtypes, &symtypes_file },
3942
};
4043

@@ -109,9 +112,10 @@ static int process_modules(Dwfl_Module *mod, void **userdata, const char *name,
109112
} while (cu);
110113

111114
/*
112-
* Use die_map to expand type strings and write them to `symfile`.
115+
* Use die_map to expand type strings, write them to `symfile`, and
116+
* calculate symbol versions.
113117
*/
114-
check(generate_symtypes(symfile));
118+
check(generate_symtypes_and_versions(symfile));
115119
die_map_free();
116120

117121
return DWARF_CB_OK;
@@ -187,5 +191,7 @@ int main(int argc, const char **argv)
187191
if (symfile)
188192
fclose(symfile);
189193

194+
symbol_print_versions();
195+
190196
return 0;
191197
}

scripts/gendwarfksyms/gendwarfksyms.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern bool debug;
2323
extern bool dump_dies;
2424
extern bool dump_die_map;
2525
extern bool dump_types;
26+
extern bool dump_versions;
2627
extern bool symtypes;
2728

2829
#define MAX_INPUT_FILES 128
@@ -95,7 +96,7 @@ extern bool symtypes;
9596
#define SYMBOL_DECLONLY_PREFIX "__gendwarfksyms_declonly_"
9697
#define SYMBOL_DECLONLY_PREFIX_LEN (sizeof(SYMBOL_DECLONLY_PREFIX) - 1)
9798

98-
enum symbol_state { UNPROCESSED, MAPPED };
99+
enum symbol_state { UNPROCESSED, MAPPED, PROCESSED };
99100

100101
struct symbol_addr {
101102
uint32_t section;
@@ -119,18 +120,25 @@ struct symbol {
119120
struct hlist_node name_hash;
120121
enum symbol_state state;
121122
uintptr_t die_addr;
123+
unsigned long crc;
122124
};
123125

126+
typedef int (*symbol_callback_t)(struct symbol *, void *arg);
127+
124128
extern bool is_symbol_ptr(const char *name);
125129
extern int symbol_read_exports(FILE *file);
126130
extern int symbol_read_symtab(int fd);
127131
extern struct symbol *symbol_get_unprocessed(const char *name);
132+
extern int symbol_set_die(struct symbol *sym, Dwarf_Die *die);
133+
extern int symbol_set_crc(struct symbol *sym, unsigned long crc);
134+
extern int symbol_for_each(symbol_callback_t func, void *arg);
135+
extern void symbol_print_versions(void);
128136

129137
/*
130138
* die.c
131139
*/
132140

133-
enum die_state { INCOMPLETE, UNEXPANDED, COMPLETE, LAST = COMPLETE };
141+
enum die_state { INCOMPLETE, UNEXPANDED, COMPLETE, SYMBOL, LAST = SYMBOL };
134142
enum die_fragment_type { EMPTY, STRING, LINEBREAK, DIE };
135143

136144
struct die_fragment {
@@ -154,6 +162,7 @@ static inline const char *die_state_name(enum die_state state)
154162
CASE_CONST_TO_STR(INCOMPLETE)
155163
CASE_CONST_TO_STR(UNEXPANDED)
156164
CASE_CONST_TO_STR(COMPLETE)
165+
CASE_CONST_TO_STR(SYMBOL)
157166
}
158167
}
159168

@@ -239,6 +248,6 @@ extern int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie);
239248
* types.c
240249
*/
241250

242-
extern int generate_symtypes(FILE *file);
251+
extern int generate_symtypes_and_versions(FILE *file);
243252

244253
#endif /* __GENDWARFKSYMS_H */

scripts/gendwarfksyms/symbols.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,38 @@ static int for_each(const char *name, bool name_only, symbol_callback_t func,
8484
return 0;
8585
}
8686

87+
static int set_crc(struct symbol *sym, void *data)
88+
{
89+
unsigned long *crc = data;
90+
91+
if (sym->state == PROCESSED && sym->crc != *crc)
92+
warn("overriding version for symbol %s (crc %lx vs. %lx)",
93+
sym->name, sym->crc, *crc);
94+
95+
sym->state = PROCESSED;
96+
sym->crc = *crc;
97+
return 0;
98+
}
99+
100+
int symbol_set_crc(struct symbol *sym, unsigned long crc)
101+
{
102+
if (checkp(for_each(sym->name, false, set_crc, &crc)) > 0)
103+
return 0;
104+
return -1;
105+
}
106+
107+
static int set_die(struct symbol *sym, void *data)
108+
{
109+
sym->die_addr = (uintptr_t)((Dwarf_Die *)data)->addr;
110+
sym->state = MAPPED;
111+
return 0;
112+
}
113+
114+
int symbol_set_die(struct symbol *sym, Dwarf_Die *die)
115+
{
116+
return checkp(for_each(sym->name, false, set_die, die));
117+
}
118+
87119
static bool is_exported(const char *name)
88120
{
89121
return checkp(for_each(name, true, NULL, NULL)) > 0;
@@ -151,6 +183,19 @@ struct symbol *symbol_get_unprocessed(const char *name)
151183
return sym;
152184
}
153185

186+
int symbol_for_each(symbol_callback_t func, void *arg)
187+
{
188+
struct hlist_node *tmp;
189+
struct symbol *sym;
190+
int i;
191+
192+
hash_for_each_safe(symbol_names, i, tmp, sym, name_hash) {
193+
check(func(sym, arg));
194+
}
195+
196+
return 0;
197+
}
198+
154199
typedef int (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym,
155200
Elf32_Word xndx, void *arg);
156201

@@ -264,3 +309,23 @@ int symbol_read_symtab(int fd)
264309
{
265310
return elf_for_each_symbol(fd, process_symbol, NULL);
266311
}
312+
313+
void symbol_print_versions(void)
314+
{
315+
struct hlist_node *tmp;
316+
struct symbol *sym;
317+
int i;
318+
319+
hash_for_each_safe(symbol_names, i, tmp, sym, name_hash) {
320+
if (sym->state != PROCESSED)
321+
warn("no information for symbol %s", sym->name);
322+
323+
printf("#SYMVER %s 0x%08lx\n", sym->name, sym->crc);
324+
325+
free((void *)sym->name);
326+
free(sym);
327+
}
328+
329+
hash_init(symbol_addrs);
330+
hash_init(symbol_names);
331+
}

0 commit comments

Comments
 (0)