Debian Codegraph @

libdrgn/kallsyms.c

raw
1 // Copyright (c) 2024 Oracle and/or its affiliates
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <ctype.h>
5 #include <stddef.h>
6
7 #include "binary_buffer.h"
8 #include "drgn_internal.h"
9 #include "kallsyms.h"
10 #include "program.h"
11 #include "string_builder.h"
12 #include "symbol.h"
13
14 /**
15 * This struct contains the tables necessary to reconstruct kallsyms names.
16 *
17 * vmlinux (core kernel) kallsyms names are compressed using table compression.
18 * There is some description of it in the kernel's "scripts/kallsyms.c", but
19 * this is a brief overview that should make the code below comprehensible.
20 *
21 * Table compression uses the remaining 128 characters not defined by ASCII and
22 * maps them to common substrings (e.g. the prefix "write_"). Each name is
23 * represented as a sequence of bytes which refers to strings in this table.
24 * The two arrays below comprise this table:
25 *
26 * - token_table: this is one long string with all of the tokens concatenated
27 * together, e.g. "a\0b\0c\0...z\0write_\0read_\0..."
28 * - token_index: this is a 256-entry long array containing the index into
29 * token_table where you'll find that token's string.
30 *
31 * To decode a string, for each byte you simply index into token_index, then use
32 * that to index into token_table, and copy that string into your buffer.
33 *
34 * The actual kallsyms symbol names are concatenated into a buffer called
35 * "names". The first byte in a name is the length (in tokens, not decoded
36 * bytes) of the symbol name. The remaining "length" bytes are decoded via the
37 * table as described above. The first decoded byte is a character representing
38 * what type of symbol this is (e.g. text, data structure, etc).
39 */
40 struct kallsyms_reader {
41 uint32_t num_syms;
42 uint8_t *names;
43 size_t names_len;
44 char *token_table;
45 size_t token_table_len;
46 uint16_t *token_index;
47 bool long_names;
48 };
49
50 /*
51 * Kallsyms doesn't include symbol length. We determine symbol length by the
52 * start of the subsequent symbol. Unfortunately, there can be large gaps in
53 * the symbol table, for instance on x86_64 the Linux kernel has percpu symbols
54 * near the beginning of the address space, and a large gap before normal kernel
55 * symbols. The result of this is that we can create symbols with incredibly
56 * large sizes, and then drgn's symbolization will print addresses using that
57 * symbol and a very large offset, which is absolutely meaningless.
58 *
59 * To avoid this, we set a cap on the length of a symbol. Unfortunately, this is
60 * a heuristic. It's entirely possible to have very large data symbols. This
61 * value is chosen somewhat arbitrarily, but seems to produce decent results.
62 */
63 #define MAX_SYMBOL_LENGTH 0x10000
64
65 /*
66 * Since 73bbb94466fd3 ("kallsyms: support "big" kernel symbols"), the
67 * "kallsyms_names" array may use the most significant bit to indicate that the
68 * initial element for each symbol (normally representing the number of tokens
69 * in the symbol) requires two bytes.
70 *
71 * Unfortunately, that means that values 128-255 are now ambiguous: on older
72 * kernels, they should be interpreted literally, but on newer kernels, they
73 * require treating as a two byte sequence. Since the commit included no changes
74 * to the symbol names or vmcoreinfo, there's no way to detect it except via
75 * heuristics.
76 *
77 * The commit in question is a new feature and not likely to be backported to
78 * stable, so our heuristic is that it was first included in kernel 6.1.
79 * However, we first check the environment variable DRGN_KALLSYMS_LONG: if it
80 * exists, then we use its first character to determine our behavior: 1, y, Y
81 * all indicate that we should use long names. 0, n, N all indicate that we
82 * should not.
83 */
84 static bool guess_long_names(struct drgn_program *prog)
85 {
86 const char *env = getenv("DRGN_KALLSYMS_LONG");
87 if (env) {
88 if (*env == '1' || *env == 'y' || *env == 'Y')
89 return true;
90 else if (*env == '0' || *env == 'n' || *env == 'N')
91 return false;
92 }
93
94 char *p = prog->vmcoreinfo.osrelease;
95 long major = strtol(p, &p, 10);
96 long minor = 0;
97 if (*p == '.')
98 minor = strtol(p + 1, NULL, 10);
99
100 return (major == 6 && minor >= 1) || major > 6;
101 }
102
103 /**
104 * Copy the kallsyms names tables from the program into host memory.
105 * @param prog Program to read from
106 * @param kr kallsyms_reader to populate
107 * @param vi vmcoreinfo for the program
108 */
109 static struct drgn_error *
110 kallsyms_copy_tables(struct drgn_program *prog, struct kallsyms_reader *kr,
111 struct kallsyms_locations *loc)
112 {
113 struct drgn_error *err;
114 const size_t token_index_size = (UINT8_MAX + 1) * sizeof(uint16_t);
115 uint64_t last_token;
116 size_t names_idx;
117 char data;
118 uint8_t len_u8;
119 int len;
120 bool bswap;
121
122 err = drgn_program_bswap(prog, &bswap);
123 if (err)
124 return err;
125
126 // Read num_syms from vmcore (bswap is done for us already)
127 err = drgn_program_read_u32(prog,
128 loc->kallsyms_num_syms,
129 false, &kr->num_syms);
130 if (err)
131 return err;
132
133 // Read the constant-sized token_index table (256 entries)
134 kr->token_index = malloc(token_index_size);
135 if (!kr->token_index)
136 return &drgn_enomem;
137 err = drgn_program_read_memory(prog, kr->token_index,
138 loc->kallsyms_token_index,
139 token_index_size, false);
140 if (err)
141 return err;
142 if (bswap)
143 for (size_t i = 0; i < kr->num_syms; i++)
144 kr->token_index[i] = bswap_16(kr->token_index[i]);
145
146 // Find the end of the last token, so we get the overall length of
147 // token_table. Then copy the token_table into host memory.
148 last_token = loc->kallsyms_token_table + kr->token_index[UINT8_MAX];
149 do {
150 err = drgn_program_read_memory(prog, &data,
151 last_token, 1, false);
152 if (err)
153 return err;
154
155 last_token++;
156 } while (data);
157 kr->token_table_len = last_token - loc->kallsyms_token_table + 1;
158 kr->token_table = malloc(kr->token_table_len);
159 if (!kr->token_table)
160 return &drgn_enomem;
161 err = drgn_program_read_memory(prog, kr->token_table,
162 loc->kallsyms_token_table,
163 kr->token_table_len, false);
164 if (err)
165 return err;
166
167 // Ensure that all members of token_index are in-bounds for indexing
168 // into token_table.
169 for (size_t i = 0; i <= UINT8_MAX; i++)
170 if (kr->token_index[i] >= kr->token_table_len)
171 return drgn_error_format(DRGN_ERROR_OTHER,
172 "kallsyms: token_index out of bounds (token_index[%zu] = %u >= %zu)",
173 i, kr->token_index[i], kr->token_table_len);
174
175 // Now find the end of the names array by skipping through it, then copy
176 // that into host memory.
177 names_idx = 0;
178 kr->long_names = guess_long_names(prog);
179 for (size_t i = 0; i < kr->num_syms; i++) {
180 err = drgn_program_read_u8(prog,
181 loc->kallsyms_names + names_idx,
182 false, &len_u8);
183 if (err)
184 return err;
185 len = len_u8;
186 if ((len & 0x80) && kr->long_names) {
187 if (__builtin_add_overflow(names_idx, 1, &names_idx))
188 return drgn_error_create(DRGN_ERROR_OTHER,
189 "couldn't find end of kallsyms_names");
190 err = drgn_program_read_u8(prog,
191 loc->kallsyms_names + names_idx,
192 false, &len_u8);
193 if (err)
194 return err;
195 // 73bbb94466fd3 ("kallsyms: support "big" kernel
196 // symbols") mentions that ULEB128 is used, but only
197 // implements the ability to encode lengths with 2
198 // bytes, for a maximum value of 16k. It's possible in
199 // the future we may need to support larger sizes, but
200 // it's difficult to predict the future of the kallsyms
201 // format. For now, just check that there's no third
202 // byte to the length.
203 if (len_u8 & 0x80)
204 return drgn_error_format(
205 DRGN_ERROR_OTHER,
206 "Unexpected 3-byte length encoding in kallsyms names"
207 );
208 len = (len & 0x7F) | (len_u8 << 7);
209 }
210 if (__builtin_add_overflow(names_idx, len + 1, &names_idx))
211 return drgn_error_format(
212 DRGN_ERROR_OTHER, "couldn't find end of kallsyms_names");
213 }
214 kr->names_len = names_idx;
215 kr->names = malloc(names_idx);
216 if (!kr->names)
217 return &drgn_enomem;
218 err = drgn_program_read_memory(prog, kr->names,
219 loc->kallsyms_names,
220 names_idx, false);
221 if (err)
222 return err;
223
224 return NULL;
225 }
226
227 static struct drgn_error *kallsyms_binary_buffer_error(struct binary_buffer *bb,
228 const char *pos,
229 const char *message)
230 {
231 return drgn_error_format(DRGN_ERROR_OTHER,
232 "couldn't parse kallsyms: %s", message);
233 }
234
235 /**
236 * Extract the symbol name and type
237 * @param kr Registry containing kallsyms data
238 * @param names_bb A binary buffer tracking our position within the
239 * `kallsyms_names` array
240 * @param sb Buffer to write output symbol to
241 * @param[out] kind_ret Where to write the symbol kind data
242 * @returns NULL on success, or an error
243 */
244 static struct drgn_error *
245 kallsyms_expand_symbol(struct kallsyms_reader *kr,
246 struct binary_buffer *names_bb,
247 struct string_builder *sb, char *kind_ret)
248 {
249 uint64_t len;
250 struct drgn_error *err = binary_buffer_next_uleb128(names_bb, &len);
251 if (err)
252 return err;
253
254 const uint8_t *data = (uint8_t *)names_bb->pos;
255 err = binary_buffer_skip(names_bb, len);
256 if (err)
257 return err;
258
259 bool skipped_first = false;
260
261 while (len) {
262 char *token_ptr = &kr->token_table[kr->token_index[*data]];
263 while (*token_ptr) {
264 if (skipped_first) {
265 if (!string_builder_appendc(sb, *token_ptr))
266 return &drgn_enomem;
267 } else {
268 *kind_ret = *token_ptr;
269 skipped_first = true;
270 }
271 token_ptr++;
272 }
273
274 data++;
275 len--;
276 }
277
278 if (!string_builder_null_terminate(sb))
279 return &drgn_enomem;
280 return NULL;
281 }
282
283 /**
284 * Used to find _stext in the kallsyms before we've moved everything into
285 * the drgn_symbol_index. Finds the index matching the given name, or -1.
286 */
287 static struct drgn_error *
288 search_for_string(struct kallsyms_reader *kr, const char *name, ssize_t *ret)
289 {
290 STRING_BUILDER(sb);
291 size_t len = strlen(name);
292 struct binary_buffer names_bb;
293 binary_buffer_init(&names_bb, kr->names, kr->names_len, false,
294 kallsyms_binary_buffer_error);
295 for (ssize_t i = 0; i < kr->num_syms; i++) {
296 char kind;
297 sb.len = 0;
298 struct drgn_error *err =
299 kallsyms_expand_symbol(kr, &names_bb, &sb, &kind);
300 if (err)
301 return err;
302 if (sb.len == len && strcmp(name, sb.str) == 0) {
303 *ret = i;
304 return NULL;
305 }
306 }
307 return drgn_error_format(DRGN_ERROR_OTHER,
308 "Could not find '%s' symbol in kallsyms", name);
309 }
310
311 static void symbol_from_kallsyms(uint64_t address, char *name, char kind,
312 uint64_t size, struct drgn_symbol *ret)
313 {
314 char kind_lower = tolower(kind);
315 ret->name = name;
316 ret->address = address;
317 ret->size = size;
318 ret->binding = DRGN_SYMBOL_BINDING_GLOBAL;
319
320 // See nm(1) for information on decoding this "kind" character
321 if (kind == 'u')
322 ret->binding = DRGN_SYMBOL_BINDING_UNIQUE;
323 else if (kind_lower == 'v' || kind_lower == 'w')
324 ret->binding = DRGN_SYMBOL_BINDING_WEAK;
325 else if (isupper(kind))
326 ret->binding = DRGN_SYMBOL_BINDING_GLOBAL;
327 else
328 // If lowercase, the symbol is usually local, but it's
329 // not guaranteed. Use unknown for safety here.
330 ret->binding = DRGN_SYMBOL_BINDING_UNKNOWN;
331
332 switch (kind_lower) {
333 case 'b': // bss
334 case 'c': // uninitialized data
335 case 'd': // initialized data
336 case 'g': // initialized data (small objects)
337 case 'r': // read-only data
338 case 'v': // weak object (guaranteed by elf_info() in kernel/module.c)
339 ret->kind = DRGN_SYMBOL_KIND_OBJECT;
340 break;
341 case 't': // text
342 ret->kind = DRGN_SYMBOL_KIND_FUNC;
343 break;
344 default:
345 ret->kind = DRGN_SYMBOL_KIND_UNKNOWN;
346 }
347 ret->name_lifetime = DRGN_LIFETIME_STATIC;
348 ret->lifetime = DRGN_LIFETIME_STATIC; // avoid copying
349 }
350
351 /** Compute an address via the CONFIG_KALLSYMS_ABSOLUTE_PERCPU method*/
352 static uint64_t absolute_percpu(uint64_t base, int32_t val)
353 {
354 if (val >= 0)
355 return (uint64_t) val;
356 else
357 return base - 1 - val;
358 }
359
360 /**
361 * Load the kallsyms address information from @a prog
362 *
363 * Built-in kallsyms can be stored in (at least) 4 different ways, depending on
364 * the kernel version and configuration. Drgn supports all of them.
365 *
366 * Absolute kallsyms: [v2.6 era, v6.11)
367 * Kconfig: NONE (default when kallsyms is enabled)
368 * Addresses are directly stored in: unsigned long kallsyms_addresses[]
369 * This was the only option until v4.6, and it was used by all arches.
370 *
371 * Base relative: [v4.6, v7.0)
372 * Kconfig: [v4.6, 6.11): CONFIG_KALLSYMS_BASE_RELATIVE
373 * [v6.11, v7.0): NONE (default when kallsyms is enabled)
374 * Addresses are stored as offsets in: int kallsyms_offsets[].
375 * The offsets are from "kallsyms_relative_base".
376 * This was added to reduce memory usage on 64-bit architectures. It became
377 * the de-facto default, except for architectures with large gaps in symbol
378 * addresses (like x86_64).
379 *
380 * Absolute percpu: [v4.6, v6.15)
381 * Kconfig: CONFIG_KALLSYMS_ABSOLUTE_PERCPU
382 * Addresses are encoded in: int kallsyms_offsets[].
383 * Negative values are negated offsets from "kallsyms_relative_base".
384 * Positive values are absolute addresses.
385 * This alternative was added alongside base-relative encoding, mainly to
386 * support x86_64, where percpu addresses were low addresses near zero.
387 *
388 * Place relative: [v7.0, present)
389 * Kconfig: NONE (default when kallsyms is enabled)
390 * Addresses are stored as offsets in: int kallsyms_offsets[].
391 * Offsets are relative to the memory address at which they are stored.
392 * This was introduced as a simplification after absolute percpu was
393 * eliminated. For some 32-bit architectures kernel relocation is not
394 * supported, so these architectures use absolute addresses. Drgn does not
395 * support those architectures but this code should work for them
396 * nonetheless.
397 *
398 * In addition, to read these, the kallsyms variable locations must be encoded
399 * in VMCOREINFO. This happened in v6.0, but those commits have been backported
400 * in some distributions.
401 *
402 * Relevant commit history:
403 *
404 * v4.6 - introduces base relative and absolute percpu.
405 * 2213e9a66bb87 ("kallsyms: add support for relative offsets in kallsyms address table")
406 * v6.0 - adds kallsyms symbols to vmcoreinfo
407 * 5fd8fea935a10 ("vmcoreinfo: include kallsyms symbols")
408 * f09bddbd86619 ("vmcoreinfo: add kallsyms_num_syms symbol")
409 * v6.11 - removes absolute kallsyms
410 * 64e166099b69b ("kallsyms: get rid of code for absolute kallsyms")
411 * v6.15 - removes absolute percpu
412 * 01157ddc58dc2 ("kallsyms: Remove KALLSYMS_ABSOLUTE_PERCPU")
413 * v7.0 - replaces base relative with place relative
414 * a081b5789255d ("kallsyms: Get rid of kallsyms relative base")
415 *
416 * @param prog The program to read from
417 * @param kr The symbol registry to fill
418 * @param vi vmcoreinfo containing necessary symbols
419 * @returns NULL on success, or error
420 */
421 static struct drgn_error *
422 kallsyms_load_addresses(struct drgn_program *prog, struct kallsyms_reader *kr,
423 struct kallsyms_locations *loc, uint64_t **ret)
424 {
425 struct drgn_error *err = NULL;
426 bool bswap, bits64;
427 _cleanup_free_ uint32_t *addr32 = NULL;
428
429 err = drgn_program_bswap(prog, &bswap);
430 if (err)
431 return err;
432 err = drgn_program_is_64_bit(prog, &bits64);
433 if (err)
434 return err;
435
436 _cleanup_free_ uint64_t *addresses =
437 malloc_array(kr->num_syms, sizeof(addresses[0]));
438 if (!addresses)
439 return &drgn_enomem;
440
441 if (loc->kallsyms_addresses) {
442 /* Absolute kallsyms */
443 if (bits64) {
444 err = drgn_program_read_memory(prog, addresses,
445 loc->kallsyms_addresses,
446 kr->num_syms * sizeof(addresses[0]),
447 false);
448 if (err)
449 return err;
450 if (bswap)
451 for (int i = 0; i < kr->num_syms; i++)
452 addresses[i] = bswap_64(addresses[i]);
453 } else {
454 addr32 = malloc_array(kr->num_syms, sizeof(addr32[0]));
455 if (!addr32)
456 return &drgn_enomem;
457
458 err = drgn_program_read_memory(prog, addr32,
459 loc->kallsyms_addresses,
460 kr->num_syms * sizeof(addr32[0]),
461 false);
462 if (err)
463 return err;
464 for (int i = 0; i < kr->num_syms; i++) {
465 if (bswap)
466 addresses[i] = bswap_32(addr32[i]);
467 else
468 addresses[i] = addr32[i];
469 }
470 }
471 } else {
472 /*
473 * Relative base, absolute percpu, and place-relative.
474 * First, read the values, then figure out which way to
475 * interpret them.
476 */
477 uint64_t relative_base = 0;
478 if (loc->kallsyms_relative_base && bits64) {
479 // performs the bswap for us, if necessary
480 err = drgn_program_read_u64(prog, loc->kallsyms_relative_base,
481 false, &relative_base);
482 if (err)
483 return err;
484 } else if (loc->kallsyms_relative_base) {
485 uint32_t rel32;
486 // performs the bswap for us, if necessary
487 err = drgn_program_read_u32(prog, loc->kallsyms_relative_base,
488 false, &rel32);
489 if (err)
490 return err;
491 relative_base = rel32;
492 }
493 addr32 = malloc_array(kr->num_syms, sizeof(addr32[0]));
494 if (!addr32)
495 return &drgn_enomem;
496
497 err = drgn_program_read_memory(prog, addr32,
498 loc->kallsyms_offsets,
499 kr->num_syms * sizeof(uint32_t),
500 false);
501 if (err)
502 return err;
503 if (bswap)
504 for (int i = 0; i < kr->num_syms; i++)
505 addr32[i] = bswap_32(addr32[i]);
506
507 /*
508 * Now that we've read the offsets data, we need to determine
509 * how to interpret them. To do this, use the _stext symbol. We
510 * have the correct value from vmcoreinfo. Compute it both ways
511 * and pick the correct interpretation.
512 */
513 ssize_t stext_idx;
514 err = search_for_string(kr, "_stext", &stext_idx);
515 if (err)
516 return err;
517 /* Relative base (OR: non-relocatable place-relative) */
518 uint64_t relbase = relative_base + addr32[stext_idx];
519 /* Absolute percpu (disable for place-relative) */
520 uint64_t abs_pcpu;
521 if (loc->kallsyms_relative_base)
522 abs_pcpu = absolute_percpu(relative_base, (int32_t)addr32[stext_idx]);
523 /* Place relative */
524 uint64_t prel = loc->kallsyms_offsets + 4 * stext_idx + (int32_t)addr32[stext_idx];
525 if (relbase == loc->_stext) {
526 for (int i = 0; i < kr->num_syms; i++)
527 addresses[i] = relative_base + addr32[i];
528 } else if (loc->kallsyms_relative_base && abs_pcpu == loc->_stext) {
529 for (int i = 0; i < kr->num_syms; i++)
530 addresses[i] = absolute_percpu(relative_base, (int32_t)addr32[i]);
531 } else if (prel == loc->_stext) {
532 for (int i = 0; i < kr->num_syms; i++)
533 addresses[i] = loc->kallsyms_offsets + 4 * i + (int32_t)addr32[i];
534 } else {
535 err = drgn_error_create(
536 DRGN_ERROR_OTHER,
537 "Unable to interpret kallsyms offset encoding");
538 if (err)
539 return err;
540 }
541 }
542 *ret = no_cleanup_ptr(addresses);
543 return NULL;
544 }
545
546 static void kallsyms_reader_cleanup(struct kallsyms_reader *kr)
547 {
548 free(kr->names);
549 free(kr->token_index);
550 free(kr->token_table);
551 }
552
553 struct drgn_error *
554 drgn_load_builtin_kallsyms(struct drgn_program *prog,
555 struct kallsyms_locations *loc,
556 struct drgn_symbol_index *ret)
557 {
558 if (!(loc->kallsyms_names && loc->kallsyms_token_table
559 && loc->kallsyms_token_index && loc->kallsyms_num_syms))
560 return drgn_error_create(
561 DRGN_ERROR_MISSING_DEBUG_INFO,
562 "The symbols: kallsyms_names, kallsyms_token_table, "
563 "kallsyms_token_index, and kallsyms_num_syms were not "
564 "found in VMCOREINFO. There is not enough "
565 "information to load the kallsyms table."
566 );
567
568 _cleanup_(kallsyms_reader_cleanup) struct kallsyms_reader kr = {};
569
570 struct drgn_error *err = kallsyms_copy_tables(prog, &kr, loc);
571 if (err)
572 return err;
573
574 _cleanup_free_ uint64_t *addresses = NULL;
575 err = kallsyms_load_addresses(prog, &kr, loc, &addresses);
576 if (err)
577 return err;
578
579 _cleanup_(drgn_symbol_index_builder_deinit)
580 struct drgn_symbol_index_builder builder;
581 drgn_symbol_index_builder_init(&builder);
582 STRING_BUILDER(sb);
583
584 struct binary_buffer names_bb;
585 binary_buffer_init(&names_bb, kr.names, kr.names_len, false,
586 kallsyms_binary_buffer_error);
587 for (int i = 0; i < kr.num_syms; i++) {
588 struct drgn_symbol symbol;
589 char kind;
590 uint64_t size = 0;
591 sb.len = 0;
592 err = kallsyms_expand_symbol(&kr, &names_bb, &sb, &kind);
593 if (err)
594 return err;
595 if (sb.len == 0)
596 return drgn_error_format(DRGN_ERROR_OTHER,
597 "error: zero-length symbol in kallsyms");
598 if (i + 1 < kr.num_syms &&
599 addresses[i + 1] - addresses[i] < MAX_SYMBOL_LENGTH)
600 size = addresses[i + 1] - addresses[i];
601 symbol_from_kallsyms(addresses[i], sb.str, kind, size, &symbol);
602 if (!drgn_symbol_index_builder_add(&builder, &symbol))
603 return &drgn_enomem;
604 }
605
606 return drgn_symbol_index_init_from_builder(ret, &builder);
607 }
608
609 /** Load kallsyms directly from the /proc/kallsyms file */
610 struct drgn_error *drgn_load_proc_kallsyms(const char *filename, bool modules,
611 struct drgn_symbol_index *ret)
612 {
613 _cleanup_fclose_ FILE *fp = fopen(filename, "r");
614 if (!fp)
615 return drgn_error_create_os("fopen", errno, filename);
616
617 struct drgn_error *err = NULL;
618 struct drgn_symbol sym = {};
619 _cleanup_(drgn_symbol_index_builder_deinit)
620 struct drgn_symbol_index_builder builder;
621 drgn_symbol_index_builder_init(&builder);
622 _cleanup_free_ char *line = NULL;
623 _cleanup_free_ char *current_module = NULL;
624 size_t line_size = 0, line_number = 1;
625 ssize_t res;
626 while ((res = getline(&line, &line_size, fp)) != -1) {
627 char *save = NULL;
628 char *name, *type_str, *mod, *addr_rem, *addr_str;
629 char type;
630 uint64_t addr;
631 bool new_module = false;
632
633 addr_str = strtok_r(line, " \t\r\n", &save);
634 type_str = strtok_r(NULL," \t\r\n", &save);
635 name = strtok_r(NULL," \t\r\n", &save);
636 mod = strtok_r(NULL," \t\r\n", &save);
637
638 if (!addr_str || !type_str || !name) {
639 err = drgn_error_format(DRGN_ERROR_OTHER,
640 "Error parsing kallsyms line %zu",
641 line_number);
642 break;
643 }
644 if (mod && !modules) {
645 break;
646 } else if (mod && (!current_module || strcmp(mod, current_module) != 0)) {
647 free(current_module);
648 current_module = strdup(mod);
649 new_module = true;
650 if (!current_module) {
651 err = &drgn_enomem;
652 break;
653 }
654 }
655
656 type = *type_str;
657 addr = strtoull(addr_str, &addr_rem, 16);
658 if (*addr_rem) {
659 // addr_rem should be set to the first un-parsed character, and
660 // since the entire string should be a valid base 16 integer,
661 // we expect it to be \0
662 err = drgn_error_format(DRGN_ERROR_OTHER,
663 "Invalid address \"%s\" in kallsyms line %zu",
664 addr_str, line_number);
665 break;
666 }
667
668 // We now may know the size of the previous symbol, so long as
669 // that symbol was in the same module as the current one.
670 // Otherwise we'll leave it as zero. Note that for module
671 // kallsyms, it has been observed that addresses are not always
672 // increasing, even within the same module, so we need to be
673 // careful to avoid overflow here.
674 if (!new_module && addr > sym.address) {
675 uint64_t size = addr - sym.address;
676 if (size < MAX_SYMBOL_LENGTH)
677 sym.size = size;
678 }
679 if (sym.name && !drgn_symbol_index_builder_add(&builder, &sym)) {
680 err = &drgn_enomem;
681 break;
682 }
683 free((char *)sym.name);
684
685 symbol_from_kallsyms(addr, name, type, 0, &sym);
686
687 // Copy the name so we don't clobber it in the next iteration
688 sym.name = strdup(name);
689 if (!sym.name) {
690 err = &drgn_enomem;
691 break;
692 }
693
694 line_number++;
695 }
696
697 if (!err && ferror(fp))
698 err = drgn_error_create_os("Error reading kallsyms", errno, "/proc/kallsyms");
699
700 // Append the final symbol
701 if (!err && sym.name && !drgn_symbol_index_builder_add(&builder, &sym))
702 err = &drgn_enomem;
703 free((char *)sym.name);
704
705 if (!err)
706 err = drgn_symbol_index_init_from_builder(ret, &builder);
707 return err;
708 }