linux/tools/perf/util/symbol.c
<<
>>
Prefs
   1#include <dirent.h>
   2#include <errno.h>
   3#include <stdlib.h>
   4#include <stdio.h>
   5#include <string.h>
   6#include <linux/kernel.h>
   7#include <sys/types.h>
   8#include <sys/stat.h>
   9#include <sys/param.h>
  10#include <fcntl.h>
  11#include <unistd.h>
  12#include <inttypes.h>
  13#include "annotate.h"
  14#include "build-id.h"
  15#include "util.h"
  16#include "debug.h"
  17#include "machine.h"
  18#include "symbol.h"
  19#include "strlist.h"
  20#include "intlist.h"
  21#include "header.h"
  22#include "path.h"
  23#include "sane_ctype.h"
  24
  25#include <elf.h>
  26#include <limits.h>
  27#include <symbol/kallsyms.h>
  28#include <sys/utsname.h>
  29
  30static int dso__load_kernel_sym(struct dso *dso, struct map *map);
  31static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
  32static bool symbol__is_idle(const char *name);
  33
  34int vmlinux_path__nr_entries;
  35char **vmlinux_path;
  36
  37struct symbol_conf symbol_conf = {
  38        .use_modules            = true,
  39        .try_vmlinux_path       = true,
  40        .annotate_src           = true,
  41        .demangle               = true,
  42        .demangle_kernel        = false,
  43        .cumulate_callchain     = true,
  44        .show_hist_headers      = true,
  45        .symfs                  = "",
  46        .event_group            = true,
  47        .inline_name            = true,
  48};
  49
  50static enum dso_binary_type binary_type_symtab[] = {
  51        DSO_BINARY_TYPE__KALLSYMS,
  52        DSO_BINARY_TYPE__GUEST_KALLSYMS,
  53        DSO_BINARY_TYPE__JAVA_JIT,
  54        DSO_BINARY_TYPE__DEBUGLINK,
  55        DSO_BINARY_TYPE__BUILD_ID_CACHE,
  56        DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  57        DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  58        DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  59        DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  60        DSO_BINARY_TYPE__GUEST_KMODULE,
  61        DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
  62        DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  63        DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
  64        DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  65        DSO_BINARY_TYPE__NOT_FOUND,
  66};
  67
  68#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  69
  70bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  71{
  72        symbol_type = toupper(symbol_type);
  73
  74        switch (map_type) {
  75        case MAP__FUNCTION:
  76                return symbol_type == 'T' || symbol_type == 'W';
  77        case MAP__VARIABLE:
  78                return symbol_type == 'D';
  79        default:
  80                return false;
  81        }
  82}
  83
  84static int prefix_underscores_count(const char *str)
  85{
  86        const char *tail = str;
  87
  88        while (*tail == '_')
  89                tail++;
  90
  91        return tail - str;
  92}
  93
  94int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
  95{
  96        return strcmp(namea, nameb);
  97}
  98
  99int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
 100                                        unsigned int n)
 101{
 102        return strncmp(namea, nameb, n);
 103}
 104
 105int __weak arch__choose_best_symbol(struct symbol *syma,
 106                                    struct symbol *symb __maybe_unused)
 107{
 108        /* Avoid "SyS" kernel syscall aliases */
 109        if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
 110                return SYMBOL_B;
 111        if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
 112                return SYMBOL_B;
 113
 114        return SYMBOL_A;
 115}
 116
 117static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
 118{
 119        s64 a;
 120        s64 b;
 121        size_t na, nb;
 122
 123        /* Prefer a symbol with non zero length */
 124        a = syma->end - syma->start;
 125        b = symb->end - symb->start;
 126        if ((b == 0) && (a > 0))
 127                return SYMBOL_A;
 128        else if ((a == 0) && (b > 0))
 129                return SYMBOL_B;
 130
 131        /* Prefer a non weak symbol over a weak one */
 132        a = syma->binding == STB_WEAK;
 133        b = symb->binding == STB_WEAK;
 134        if (b && !a)
 135                return SYMBOL_A;
 136        if (a && !b)
 137                return SYMBOL_B;
 138
 139        /* Prefer a global symbol over a non global one */
 140        a = syma->binding == STB_GLOBAL;
 141        b = symb->binding == STB_GLOBAL;
 142        if (a && !b)
 143                return SYMBOL_A;
 144        if (b && !a)
 145                return SYMBOL_B;
 146
 147        /* Prefer a symbol with less underscores */
 148        a = prefix_underscores_count(syma->name);
 149        b = prefix_underscores_count(symb->name);
 150        if (b > a)
 151                return SYMBOL_A;
 152        else if (a > b)
 153                return SYMBOL_B;
 154
 155        /* Choose the symbol with the longest name */
 156        na = strlen(syma->name);
 157        nb = strlen(symb->name);
 158        if (na > nb)
 159                return SYMBOL_A;
 160        else if (na < nb)
 161                return SYMBOL_B;
 162
 163        return arch__choose_best_symbol(syma, symb);
 164}
 165
 166void symbols__fixup_duplicate(struct rb_root *symbols)
 167{
 168        struct rb_node *nd;
 169        struct symbol *curr, *next;
 170
 171        if (symbol_conf.allow_aliases)
 172                return;
 173
 174        nd = rb_first(symbols);
 175
 176        while (nd) {
 177                curr = rb_entry(nd, struct symbol, rb_node);
 178again:
 179                nd = rb_next(&curr->rb_node);
 180                next = rb_entry(nd, struct symbol, rb_node);
 181
 182                if (!nd)
 183                        break;
 184
 185                if (curr->start != next->start)
 186                        continue;
 187
 188                if (choose_best_symbol(curr, next) == SYMBOL_A) {
 189                        rb_erase(&next->rb_node, symbols);
 190                        symbol__delete(next);
 191                        goto again;
 192                } else {
 193                        nd = rb_next(&curr->rb_node);
 194                        rb_erase(&curr->rb_node, symbols);
 195                        symbol__delete(curr);
 196                }
 197        }
 198}
 199
 200void symbols__fixup_end(struct rb_root *symbols)
 201{
 202        struct rb_node *nd, *prevnd = rb_first(symbols);
 203        struct symbol *curr, *prev;
 204
 205        if (prevnd == NULL)
 206                return;
 207
 208        curr = rb_entry(prevnd, struct symbol, rb_node);
 209
 210        for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
 211                prev = curr;
 212                curr = rb_entry(nd, struct symbol, rb_node);
 213
 214                if (prev->end == prev->start && prev->end != curr->start)
 215                        prev->end = curr->start;
 216        }
 217
 218        /* Last entry */
 219        if (curr->end == curr->start)
 220                curr->end = roundup(curr->start, 4096) + 4096;
 221}
 222
 223void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
 224{
 225        struct maps *maps = &mg->maps[type];
 226        struct map *next, *curr;
 227
 228        down_write(&maps->lock);
 229
 230        curr = maps__first(maps);
 231        if (curr == NULL)
 232                goto out_unlock;
 233
 234        for (next = map__next(curr); next; next = map__next(curr)) {
 235                if (!curr->end)
 236                        curr->end = next->start;
 237                curr = next;
 238        }
 239
 240        /*
 241         * We still haven't the actual symbols, so guess the
 242         * last map final address.
 243         */
 244        if (!curr->end)
 245                curr->end = ~0ULL;
 246
 247out_unlock:
 248        up_write(&maps->lock);
 249}
 250
 251struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
 252{
 253        size_t namelen = strlen(name) + 1;
 254        struct symbol *sym = calloc(1, (symbol_conf.priv_size +
 255                                        sizeof(*sym) + namelen));
 256        if (sym == NULL)
 257                return NULL;
 258
 259        if (symbol_conf.priv_size) {
 260                if (symbol_conf.init_annotation) {
 261                        struct annotation *notes = (void *)sym;
 262                        pthread_mutex_init(&notes->lock, NULL);
 263                }
 264                sym = ((void *)sym) + symbol_conf.priv_size;
 265        }
 266
 267        sym->start   = start;
 268        sym->end     = len ? start + len : start;
 269        sym->binding = binding;
 270        sym->namelen = namelen - 1;
 271
 272        pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
 273                  __func__, name, start, sym->end);
 274        memcpy(sym->name, name, namelen);
 275
 276        return sym;
 277}
 278
 279void symbol__delete(struct symbol *sym)
 280{
 281        free(((void *)sym) - symbol_conf.priv_size);
 282}
 283
 284void symbols__delete(struct rb_root *symbols)
 285{
 286        struct symbol *pos;
 287        struct rb_node *next = rb_first(symbols);
 288
 289        while (next) {
 290                pos = rb_entry(next, struct symbol, rb_node);
 291                next = rb_next(&pos->rb_node);
 292                rb_erase(&pos->rb_node, symbols);
 293                symbol__delete(pos);
 294        }
 295}
 296
 297void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
 298{
 299        struct rb_node **p = &symbols->rb_node;
 300        struct rb_node *parent = NULL;
 301        const u64 ip = sym->start;
 302        struct symbol *s;
 303
 304        if (kernel) {
 305                const char *name = sym->name;
 306                /*
 307                 * ppc64 uses function descriptors and appends a '.' to the
 308                 * start of every instruction address. Remove it.
 309                 */
 310                if (name[0] == '.')
 311                        name++;
 312                sym->idle = symbol__is_idle(name);
 313        }
 314
 315        while (*p != NULL) {
 316                parent = *p;
 317                s = rb_entry(parent, struct symbol, rb_node);
 318                if (ip < s->start)
 319                        p = &(*p)->rb_left;
 320                else
 321                        p = &(*p)->rb_right;
 322        }
 323        rb_link_node(&sym->rb_node, parent, p);
 324        rb_insert_color(&sym->rb_node, symbols);
 325}
 326
 327void symbols__insert(struct rb_root *symbols, struct symbol *sym)
 328{
 329        __symbols__insert(symbols, sym, false);
 330}
 331
 332static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
 333{
 334        struct rb_node *n;
 335
 336        if (symbols == NULL)
 337                return NULL;
 338
 339        n = symbols->rb_node;
 340
 341        while (n) {
 342                struct symbol *s = rb_entry(n, struct symbol, rb_node);
 343
 344                if (ip < s->start)
 345                        n = n->rb_left;
 346                else if (ip > s->end || (ip == s->end && ip != s->start))
 347                        n = n->rb_right;
 348                else
 349                        return s;
 350        }
 351
 352        return NULL;
 353}
 354
 355static struct symbol *symbols__first(struct rb_root *symbols)
 356{
 357        struct rb_node *n = rb_first(symbols);
 358
 359        if (n)
 360                return rb_entry(n, struct symbol, rb_node);
 361
 362        return NULL;
 363}
 364
 365static struct symbol *symbols__last(struct rb_root *symbols)
 366{
 367        struct rb_node *n = rb_last(symbols);
 368
 369        if (n)
 370                return rb_entry(n, struct symbol, rb_node);
 371
 372        return NULL;
 373}
 374
 375static struct symbol *symbols__next(struct symbol *sym)
 376{
 377        struct rb_node *n = rb_next(&sym->rb_node);
 378
 379        if (n)
 380                return rb_entry(n, struct symbol, rb_node);
 381
 382        return NULL;
 383}
 384
 385static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
 386{
 387        struct rb_node **p = &symbols->rb_node;
 388        struct rb_node *parent = NULL;
 389        struct symbol_name_rb_node *symn, *s;
 390
 391        symn = container_of(sym, struct symbol_name_rb_node, sym);
 392
 393        while (*p != NULL) {
 394                parent = *p;
 395                s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
 396                if (strcmp(sym->name, s->sym.name) < 0)
 397                        p = &(*p)->rb_left;
 398                else
 399                        p = &(*p)->rb_right;
 400        }
 401        rb_link_node(&symn->rb_node, parent, p);
 402        rb_insert_color(&symn->rb_node, symbols);
 403}
 404
 405static void symbols__sort_by_name(struct rb_root *symbols,
 406                                  struct rb_root *source)
 407{
 408        struct rb_node *nd;
 409
 410        for (nd = rb_first(source); nd; nd = rb_next(nd)) {
 411                struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
 412                symbols__insert_by_name(symbols, pos);
 413        }
 414}
 415
 416int symbol__match_symbol_name(const char *name, const char *str,
 417                              enum symbol_tag_include includes)
 418{
 419        const char *versioning;
 420
 421        if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
 422            (versioning = strstr(name, "@@"))) {
 423                int len = strlen(str);
 424
 425                if (len < versioning - name)
 426                        len = versioning - name;
 427
 428                return arch__compare_symbol_names_n(name, str, len);
 429        } else
 430                return arch__compare_symbol_names(name, str);
 431}
 432
 433static struct symbol *symbols__find_by_name(struct rb_root *symbols,
 434                                            const char *name,
 435                                            enum symbol_tag_include includes)
 436{
 437        struct rb_node *n;
 438        struct symbol_name_rb_node *s = NULL;
 439
 440        if (symbols == NULL)
 441                return NULL;
 442
 443        n = symbols->rb_node;
 444
 445        while (n) {
 446                int cmp;
 447
 448                s = rb_entry(n, struct symbol_name_rb_node, rb_node);
 449                cmp = symbol__match_symbol_name(s->sym.name, name, includes);
 450
 451                if (cmp > 0)
 452                        n = n->rb_left;
 453                else if (cmp < 0)
 454                        n = n->rb_right;
 455                else
 456                        break;
 457        }
 458
 459        if (n == NULL)
 460                return NULL;
 461
 462        if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
 463                /* return first symbol that has same name (if any) */
 464                for (n = rb_prev(n); n; n = rb_prev(n)) {
 465                        struct symbol_name_rb_node *tmp;
 466
 467                        tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
 468                        if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
 469                                break;
 470
 471                        s = tmp;
 472                }
 473
 474        return &s->sym;
 475}
 476
 477void dso__reset_find_symbol_cache(struct dso *dso)
 478{
 479        enum map_type type;
 480
 481        for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
 482                dso->last_find_result[type].addr   = 0;
 483                dso->last_find_result[type].symbol = NULL;
 484        }
 485}
 486
 487void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
 488{
 489        __symbols__insert(&dso->symbols[type], sym, dso->kernel);
 490
 491        /* update the symbol cache if necessary */
 492        if (dso->last_find_result[type].addr >= sym->start &&
 493            (dso->last_find_result[type].addr < sym->end ||
 494            sym->start == sym->end)) {
 495                dso->last_find_result[type].symbol = sym;
 496        }
 497}
 498
 499struct symbol *dso__find_symbol(struct dso *dso,
 500                                enum map_type type, u64 addr)
 501{
 502        if (dso->last_find_result[type].addr != addr || dso->last_find_result[type].symbol == NULL) {
 503                dso->last_find_result[type].addr   = addr;
 504                dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
 505        }
 506
 507        return dso->last_find_result[type].symbol;
 508}
 509
 510struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
 511{
 512        return symbols__first(&dso->symbols[type]);
 513}
 514
 515struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
 516{
 517        return symbols__last(&dso->symbols[type]);
 518}
 519
 520struct symbol *dso__next_symbol(struct symbol *sym)
 521{
 522        return symbols__next(sym);
 523}
 524
 525struct symbol *symbol__next_by_name(struct symbol *sym)
 526{
 527        struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
 528        struct rb_node *n = rb_next(&s->rb_node);
 529
 530        return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
 531}
 532
 533 /*
 534  * Teturns first symbol that matched with @name.
 535  */
 536struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
 537                                        const char *name)
 538{
 539        struct symbol *s = symbols__find_by_name(&dso->symbol_names[type], name,
 540                                                 SYMBOL_TAG_INCLUDE__NONE);
 541        if (!s)
 542                s = symbols__find_by_name(&dso->symbol_names[type], name,
 543                                          SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
 544        return s;
 545}
 546
 547void dso__sort_by_name(struct dso *dso, enum map_type type)
 548{
 549        dso__set_sorted_by_name(dso, type);
 550        return symbols__sort_by_name(&dso->symbol_names[type],
 551                                     &dso->symbols[type]);
 552}
 553
 554int modules__parse(const char *filename, void *arg,
 555                   int (*process_module)(void *arg, const char *name,
 556                                         u64 start, u64 size))
 557{
 558        char *line = NULL;
 559        size_t n;
 560        FILE *file;
 561        int err = 0;
 562
 563        file = fopen(filename, "r");
 564        if (file == NULL)
 565                return -1;
 566
 567        while (1) {
 568                char name[PATH_MAX];
 569                u64 start, size;
 570                char *sep, *endptr;
 571                ssize_t line_len;
 572
 573                line_len = getline(&line, &n, file);
 574                if (line_len < 0) {
 575                        if (feof(file))
 576                                break;
 577                        err = -1;
 578                        goto out;
 579                }
 580
 581                if (!line) {
 582                        err = -1;
 583                        goto out;
 584                }
 585
 586                line[--line_len] = '\0'; /* \n */
 587
 588                sep = strrchr(line, 'x');
 589                if (sep == NULL)
 590                        continue;
 591
 592                hex2u64(sep + 1, &start);
 593
 594                sep = strchr(line, ' ');
 595                if (sep == NULL)
 596                        continue;
 597
 598                *sep = '\0';
 599
 600                scnprintf(name, sizeof(name), "[%s]", line);
 601
 602                size = strtoul(sep + 1, &endptr, 0);
 603                if (*endptr != ' ' && *endptr != '\t')
 604                        continue;
 605
 606                err = process_module(arg, name, start, size);
 607                if (err)
 608                        break;
 609        }
 610out:
 611        free(line);
 612        fclose(file);
 613        return err;
 614}
 615
 616struct process_kallsyms_args {
 617        struct map *map;
 618        struct dso *dso;
 619};
 620
 621/*
 622 * These are symbols in the kernel image, so make sure that
 623 * sym is from a kernel DSO.
 624 */
 625static bool symbol__is_idle(const char *name)
 626{
 627        const char * const idle_symbols[] = {
 628                "cpu_idle",
 629                "cpu_startup_entry",
 630                "intel_idle",
 631                "default_idle",
 632                "native_safe_halt",
 633                "enter_idle",
 634                "exit_idle",
 635                "mwait_idle",
 636                "mwait_idle_with_hints",
 637                "poll_idle",
 638                "ppc64_runlatch_off",
 639                "pseries_dedicated_idle_sleep",
 640                NULL
 641        };
 642        int i;
 643
 644        for (i = 0; idle_symbols[i]; i++) {
 645                if (!strcmp(idle_symbols[i], name))
 646                        return true;
 647        }
 648
 649        return false;
 650}
 651
 652static int map__process_kallsym_symbol(void *arg, const char *name,
 653                                       char type, u64 start)
 654{
 655        struct symbol *sym;
 656        struct process_kallsyms_args *a = arg;
 657        struct rb_root *root = &a->dso->symbols[a->map->type];
 658
 659        if (!symbol_type__is_a(type, a->map->type))
 660                return 0;
 661
 662        /*
 663         * module symbols are not sorted so we add all
 664         * symbols, setting length to 0, and rely on
 665         * symbols__fixup_end() to fix it up.
 666         */
 667        sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
 668        if (sym == NULL)
 669                return -ENOMEM;
 670        /*
 671         * We will pass the symbols to the filter later, in
 672         * map__split_kallsyms, when we have split the maps per module
 673         */
 674        __symbols__insert(root, sym, !strchr(name, '['));
 675
 676        return 0;
 677}
 678
 679/*
 680 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
 681 * so that we can in the next step set the symbol ->end address and then
 682 * call kernel_maps__split_kallsyms.
 683 */
 684static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
 685                                  struct map *map)
 686{
 687        struct process_kallsyms_args args = { .map = map, .dso = dso, };
 688        return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
 689}
 690
 691static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
 692{
 693        struct map_groups *kmaps = map__kmaps(map);
 694        struct map *curr_map;
 695        struct symbol *pos;
 696        int count = 0;
 697        struct rb_root old_root = dso->symbols[map->type];
 698        struct rb_root *root = &dso->symbols[map->type];
 699        struct rb_node *next = rb_first(root);
 700
 701        if (!kmaps)
 702                return -1;
 703
 704        *root = RB_ROOT;
 705
 706        while (next) {
 707                char *module;
 708
 709                pos = rb_entry(next, struct symbol, rb_node);
 710                next = rb_next(&pos->rb_node);
 711
 712                rb_erase_init(&pos->rb_node, &old_root);
 713
 714                module = strchr(pos->name, '\t');
 715                if (module)
 716                        *module = '\0';
 717
 718                curr_map = map_groups__find(kmaps, map->type, pos->start);
 719
 720                if (!curr_map) {
 721                        symbol__delete(pos);
 722                        continue;
 723                }
 724
 725                pos->start -= curr_map->start - curr_map->pgoff;
 726                if (pos->end)
 727                        pos->end -= curr_map->start - curr_map->pgoff;
 728                symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
 729                ++count;
 730        }
 731
 732        /* Symbols have been adjusted */
 733        dso->adjust_symbols = 1;
 734
 735        return count;
 736}
 737
 738/*
 739 * Split the symbols into maps, making sure there are no overlaps, i.e. the
 740 * kernel range is broken in several maps, named [kernel].N, as we don't have
 741 * the original ELF section names vmlinux have.
 742 */
 743static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
 744{
 745        struct map_groups *kmaps = map__kmaps(map);
 746        struct machine *machine;
 747        struct map *curr_map = map;
 748        struct symbol *pos;
 749        int count = 0, moved = 0;
 750        struct rb_root *root = &dso->symbols[map->type];
 751        struct rb_node *next = rb_first(root);
 752        int kernel_range = 0;
 753
 754        if (!kmaps)
 755                return -1;
 756
 757        machine = kmaps->machine;
 758
 759        while (next) {
 760                char *module;
 761
 762                pos = rb_entry(next, struct symbol, rb_node);
 763                next = rb_next(&pos->rb_node);
 764
 765                module = strchr(pos->name, '\t');
 766                if (module) {
 767                        if (!symbol_conf.use_modules)
 768                                goto discard_symbol;
 769
 770                        *module++ = '\0';
 771
 772                        if (strcmp(curr_map->dso->short_name, module)) {
 773                                if (curr_map != map &&
 774                                    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
 775                                    machine__is_default_guest(machine)) {
 776                                        /*
 777                                         * We assume all symbols of a module are
 778                                         * continuous in * kallsyms, so curr_map
 779                                         * points to a module and all its
 780                                         * symbols are in its kmap. Mark it as
 781                                         * loaded.
 782                                         */
 783                                        dso__set_loaded(curr_map->dso,
 784                                                        curr_map->type);
 785                                }
 786
 787                                curr_map = map_groups__find_by_name(kmaps,
 788                                                        map->type, module);
 789                                if (curr_map == NULL) {
 790                                        pr_debug("%s/proc/{kallsyms,modules} "
 791                                                 "inconsistency while looking "
 792                                                 "for \"%s\" module!\n",
 793                                                 machine->root_dir, module);
 794                                        curr_map = map;
 795                                        goto discard_symbol;
 796                                }
 797
 798                                if (curr_map->dso->loaded &&
 799                                    !machine__is_default_guest(machine))
 800                                        goto discard_symbol;
 801                        }
 802                        /*
 803                         * So that we look just like we get from .ko files,
 804                         * i.e. not prelinked, relative to map->start.
 805                         */
 806                        pos->start = curr_map->map_ip(curr_map, pos->start);
 807                        pos->end   = curr_map->map_ip(curr_map, pos->end);
 808                } else if (curr_map != map) {
 809                        char dso_name[PATH_MAX];
 810                        struct dso *ndso;
 811
 812                        if (delta) {
 813                                /* Kernel was relocated at boot time */
 814                                pos->start -= delta;
 815                                pos->end -= delta;
 816                        }
 817
 818                        if (count == 0) {
 819                                curr_map = map;
 820                                goto add_symbol;
 821                        }
 822
 823                        if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
 824                                snprintf(dso_name, sizeof(dso_name),
 825                                        "[guest.kernel].%d",
 826                                        kernel_range++);
 827                        else
 828                                snprintf(dso_name, sizeof(dso_name),
 829                                        "[kernel].%d",
 830                                        kernel_range++);
 831
 832                        ndso = dso__new(dso_name);
 833                        if (ndso == NULL)
 834                                return -1;
 835
 836                        ndso->kernel = dso->kernel;
 837
 838                        curr_map = map__new2(pos->start, ndso, map->type);
 839                        if (curr_map == NULL) {
 840                                dso__put(ndso);
 841                                return -1;
 842                        }
 843
 844                        curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
 845                        map_groups__insert(kmaps, curr_map);
 846                        ++kernel_range;
 847                } else if (delta) {
 848                        /* Kernel was relocated at boot time */
 849                        pos->start -= delta;
 850                        pos->end -= delta;
 851                }
 852add_symbol:
 853                if (curr_map != map) {
 854                        rb_erase(&pos->rb_node, root);
 855                        symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
 856                        ++moved;
 857                } else
 858                        ++count;
 859
 860                continue;
 861discard_symbol:
 862                rb_erase(&pos->rb_node, root);
 863                symbol__delete(pos);
 864        }
 865
 866        if (curr_map != map &&
 867            dso->kernel == DSO_TYPE_GUEST_KERNEL &&
 868            machine__is_default_guest(kmaps->machine)) {
 869                dso__set_loaded(curr_map->dso, curr_map->type);
 870        }
 871
 872        return count + moved;
 873}
 874
 875bool symbol__restricted_filename(const char *filename,
 876                                 const char *restricted_filename)
 877{
 878        bool restricted = false;
 879
 880        if (symbol_conf.kptr_restrict) {
 881                char *r = realpath(filename, NULL);
 882
 883                if (r != NULL) {
 884                        restricted = strcmp(r, restricted_filename) == 0;
 885                        free(r);
 886                        return restricted;
 887                }
 888        }
 889
 890        return restricted;
 891}
 892
 893struct module_info {
 894        struct rb_node rb_node;
 895        char *name;
 896        u64 start;
 897};
 898
 899static void add_module(struct module_info *mi, struct rb_root *modules)
 900{
 901        struct rb_node **p = &modules->rb_node;
 902        struct rb_node *parent = NULL;
 903        struct module_info *m;
 904
 905        while (*p != NULL) {
 906                parent = *p;
 907                m = rb_entry(parent, struct module_info, rb_node);
 908                if (strcmp(mi->name, m->name) < 0)
 909                        p = &(*p)->rb_left;
 910                else
 911                        p = &(*p)->rb_right;
 912        }
 913        rb_link_node(&mi->rb_node, parent, p);
 914        rb_insert_color(&mi->rb_node, modules);
 915}
 916
 917static void delete_modules(struct rb_root *modules)
 918{
 919        struct module_info *mi;
 920        struct rb_node *next = rb_first(modules);
 921
 922        while (next) {
 923                mi = rb_entry(next, struct module_info, rb_node);
 924                next = rb_next(&mi->rb_node);
 925                rb_erase(&mi->rb_node, modules);
 926                zfree(&mi->name);
 927                free(mi);
 928        }
 929}
 930
 931static struct module_info *find_module(const char *name,
 932                                       struct rb_root *modules)
 933{
 934        struct rb_node *n = modules->rb_node;
 935
 936        while (n) {
 937                struct module_info *m;
 938                int cmp;
 939
 940                m = rb_entry(n, struct module_info, rb_node);
 941                cmp = strcmp(name, m->name);
 942                if (cmp < 0)
 943                        n = n->rb_left;
 944                else if (cmp > 0)
 945                        n = n->rb_right;
 946                else
 947                        return m;
 948        }
 949
 950        return NULL;
 951}
 952
 953static int __read_proc_modules(void *arg, const char *name, u64 start,
 954                               u64 size __maybe_unused)
 955{
 956        struct rb_root *modules = arg;
 957        struct module_info *mi;
 958
 959        mi = zalloc(sizeof(struct module_info));
 960        if (!mi)
 961                return -ENOMEM;
 962
 963        mi->name = strdup(name);
 964        mi->start = start;
 965
 966        if (!mi->name) {
 967                free(mi);
 968                return -ENOMEM;
 969        }
 970
 971        add_module(mi, modules);
 972
 973        return 0;
 974}
 975
 976static int read_proc_modules(const char *filename, struct rb_root *modules)
 977{
 978        if (symbol__restricted_filename(filename, "/proc/modules"))
 979                return -1;
 980
 981        if (modules__parse(filename, modules, __read_proc_modules)) {
 982                delete_modules(modules);
 983                return -1;
 984        }
 985
 986        return 0;
 987}
 988
 989int compare_proc_modules(const char *from, const char *to)
 990{
 991        struct rb_root from_modules = RB_ROOT;
 992        struct rb_root to_modules = RB_ROOT;
 993        struct rb_node *from_node, *to_node;
 994        struct module_info *from_m, *to_m;
 995        int ret = -1;
 996
 997        if (read_proc_modules(from, &from_modules))
 998                return -1;
 999
1000        if (read_proc_modules(to, &to_modules))
1001                goto out_delete_from;
1002
1003        from_node = rb_first(&from_modules);
1004        to_node = rb_first(&to_modules);
1005        while (from_node) {
1006                if (!to_node)
1007                        break;
1008
1009                from_m = rb_entry(from_node, struct module_info, rb_node);
1010                to_m = rb_entry(to_node, struct module_info, rb_node);
1011
1012                if (from_m->start != to_m->start ||
1013                    strcmp(from_m->name, to_m->name))
1014                        break;
1015
1016                from_node = rb_next(from_node);
1017                to_node = rb_next(to_node);
1018        }
1019
1020        if (!from_node && !to_node)
1021                ret = 0;
1022
1023        delete_modules(&to_modules);
1024out_delete_from:
1025        delete_modules(&from_modules);
1026
1027        return ret;
1028}
1029
1030static int do_validate_kcore_modules(const char *filename, struct map *map,
1031                                  struct map_groups *kmaps)
1032{
1033        struct rb_root modules = RB_ROOT;
1034        struct map *old_map;
1035        int err;
1036
1037        err = read_proc_modules(filename, &modules);
1038        if (err)
1039                return err;
1040
1041        old_map = map_groups__first(kmaps, map->type);
1042        while (old_map) {
1043                struct map *next = map_groups__next(old_map);
1044                struct module_info *mi;
1045
1046                if (old_map == map || old_map->start == map->start) {
1047                        /* The kernel map */
1048                        old_map = next;
1049                        continue;
1050                }
1051
1052                /* Module must be in memory at the same address */
1053                mi = find_module(old_map->dso->short_name, &modules);
1054                if (!mi || mi->start != old_map->start) {
1055                        err = -EINVAL;
1056                        goto out;
1057                }
1058
1059                old_map = next;
1060        }
1061out:
1062        delete_modules(&modules);
1063        return err;
1064}
1065
1066/*
1067 * If kallsyms is referenced by name then we look for filename in the same
1068 * directory.
1069 */
1070static bool filename_from_kallsyms_filename(char *filename,
1071                                            const char *base_name,
1072                                            const char *kallsyms_filename)
1073{
1074        char *name;
1075
1076        strcpy(filename, kallsyms_filename);
1077        name = strrchr(filename, '/');
1078        if (!name)
1079                return false;
1080
1081        name += 1;
1082
1083        if (!strcmp(name, "kallsyms")) {
1084                strcpy(name, base_name);
1085                return true;
1086        }
1087
1088        return false;
1089}
1090
1091static int validate_kcore_modules(const char *kallsyms_filename,
1092                                  struct map *map)
1093{
1094        struct map_groups *kmaps = map__kmaps(map);
1095        char modules_filename[PATH_MAX];
1096
1097        if (!kmaps)
1098                return -EINVAL;
1099
1100        if (!filename_from_kallsyms_filename(modules_filename, "modules",
1101                                             kallsyms_filename))
1102                return -EINVAL;
1103
1104        if (do_validate_kcore_modules(modules_filename, map, kmaps))
1105                return -EINVAL;
1106
1107        return 0;
1108}
1109
1110static int validate_kcore_addresses(const char *kallsyms_filename,
1111                                    struct map *map)
1112{
1113        struct kmap *kmap = map__kmap(map);
1114
1115        if (!kmap)
1116                return -EINVAL;
1117
1118        if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1119                u64 start;
1120
1121                if (kallsyms__get_function_start(kallsyms_filename,
1122                                                 kmap->ref_reloc_sym->name, &start))
1123                        return -ENOENT;
1124                if (start != kmap->ref_reloc_sym->addr)
1125                        return -EINVAL;
1126        }
1127
1128        return validate_kcore_modules(kallsyms_filename, map);
1129}
1130
1131struct kcore_mapfn_data {
1132        struct dso *dso;
1133        enum map_type type;
1134        struct list_head maps;
1135};
1136
1137static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1138{
1139        struct kcore_mapfn_data *md = data;
1140        struct map *map;
1141
1142        map = map__new2(start, md->dso, md->type);
1143        if (map == NULL)
1144                return -ENOMEM;
1145
1146        map->end = map->start + len;
1147        map->pgoff = pgoff;
1148
1149        list_add(&map->node, &md->maps);
1150
1151        return 0;
1152}
1153
1154static int dso__load_kcore(struct dso *dso, struct map *map,
1155                           const char *kallsyms_filename)
1156{
1157        struct map_groups *kmaps = map__kmaps(map);
1158        struct machine *machine;
1159        struct kcore_mapfn_data md;
1160        struct map *old_map, *new_map, *replacement_map = NULL;
1161        bool is_64_bit;
1162        int err, fd;
1163        char kcore_filename[PATH_MAX];
1164        struct symbol *sym;
1165
1166        if (!kmaps)
1167                return -EINVAL;
1168
1169        machine = kmaps->machine;
1170
1171        /* This function requires that the map is the kernel map */
1172        if (map != machine->vmlinux_maps[map->type])
1173                return -EINVAL;
1174
1175        if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1176                                             kallsyms_filename))
1177                return -EINVAL;
1178
1179        /* Modules and kernel must be present at their original addresses */
1180        if (validate_kcore_addresses(kallsyms_filename, map))
1181                return -EINVAL;
1182
1183        md.dso = dso;
1184        md.type = map->type;
1185        INIT_LIST_HEAD(&md.maps);
1186
1187        fd = open(kcore_filename, O_RDONLY);
1188        if (fd < 0) {
1189                pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1190                         kcore_filename);
1191                return -EINVAL;
1192        }
1193
1194        /* Read new maps into temporary lists */
1195        err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1196                              &is_64_bit);
1197        if (err)
1198                goto out_err;
1199        dso->is_64_bit = is_64_bit;
1200
1201        if (list_empty(&md.maps)) {
1202                err = -EINVAL;
1203                goto out_err;
1204        }
1205
1206        /* Remove old maps */
1207        old_map = map_groups__first(kmaps, map->type);
1208        while (old_map) {
1209                struct map *next = map_groups__next(old_map);
1210
1211                if (old_map != map)
1212                        map_groups__remove(kmaps, old_map);
1213                old_map = next;
1214        }
1215
1216        /* Find the kernel map using the first symbol */
1217        sym = dso__first_symbol(dso, map->type);
1218        list_for_each_entry(new_map, &md.maps, node) {
1219                if (sym && sym->start >= new_map->start &&
1220                    sym->start < new_map->end) {
1221                        replacement_map = new_map;
1222                        break;
1223                }
1224        }
1225
1226        if (!replacement_map)
1227                replacement_map = list_entry(md.maps.next, struct map, node);
1228
1229        /* Add new maps */
1230        while (!list_empty(&md.maps)) {
1231                new_map = list_entry(md.maps.next, struct map, node);
1232                list_del_init(&new_map->node);
1233                if (new_map == replacement_map) {
1234                        map->start      = new_map->start;
1235                        map->end        = new_map->end;
1236                        map->pgoff      = new_map->pgoff;
1237                        map->map_ip     = new_map->map_ip;
1238                        map->unmap_ip   = new_map->unmap_ip;
1239                        /* Ensure maps are correctly ordered */
1240                        map__get(map);
1241                        map_groups__remove(kmaps, map);
1242                        map_groups__insert(kmaps, map);
1243                        map__put(map);
1244                } else {
1245                        map_groups__insert(kmaps, new_map);
1246                }
1247
1248                map__put(new_map);
1249        }
1250
1251        /*
1252         * Set the data type and long name so that kcore can be read via
1253         * dso__data_read_addr().
1254         */
1255        if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1256                dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1257        else
1258                dso->binary_type = DSO_BINARY_TYPE__KCORE;
1259        dso__set_long_name(dso, strdup(kcore_filename), true);
1260
1261        close(fd);
1262
1263        if (map->type == MAP__FUNCTION)
1264                pr_debug("Using %s for kernel object code\n", kcore_filename);
1265        else
1266                pr_debug("Using %s for kernel data\n", kcore_filename);
1267
1268        return 0;
1269
1270out_err:
1271        while (!list_empty(&md.maps)) {
1272                map = list_entry(md.maps.next, struct map, node);
1273                list_del_init(&map->node);
1274                map__put(map);
1275        }
1276        close(fd);
1277        return -EINVAL;
1278}
1279
1280/*
1281 * If the kernel is relocated at boot time, kallsyms won't match.  Compute the
1282 * delta based on the relocation reference symbol.
1283 */
1284static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1285{
1286        struct kmap *kmap = map__kmap(map);
1287        u64 addr;
1288
1289        if (!kmap)
1290                return -1;
1291
1292        if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1293                return 0;
1294
1295        if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
1296                return -1;
1297
1298        *delta = addr - kmap->ref_reloc_sym->addr;
1299        return 0;
1300}
1301
1302int __dso__load_kallsyms(struct dso *dso, const char *filename,
1303                         struct map *map, bool no_kcore)
1304{
1305        u64 delta = 0;
1306
1307        if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1308                return -1;
1309
1310        if (dso__load_all_kallsyms(dso, filename, map) < 0)
1311                return -1;
1312
1313        if (kallsyms__delta(map, filename, &delta))
1314                return -1;
1315
1316        symbols__fixup_end(&dso->symbols[map->type]);
1317        symbols__fixup_duplicate(&dso->symbols[map->type]);
1318
1319        if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1320                dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1321        else
1322                dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1323
1324        if (!no_kcore && !dso__load_kcore(dso, map, filename))
1325                return dso__split_kallsyms_for_kcore(dso, map);
1326        else
1327                return dso__split_kallsyms(dso, map, delta);
1328}
1329
1330int dso__load_kallsyms(struct dso *dso, const char *filename,
1331                       struct map *map)
1332{
1333        return __dso__load_kallsyms(dso, filename, map, false);
1334}
1335
1336static int dso__load_perf_map(struct dso *dso, struct map *map)
1337{
1338        char *line = NULL;
1339        size_t n;
1340        FILE *file;
1341        int nr_syms = 0;
1342
1343        file = fopen(dso->long_name, "r");
1344        if (file == NULL)
1345                goto out_failure;
1346
1347        while (!feof(file)) {
1348                u64 start, size;
1349                struct symbol *sym;
1350                int line_len, len;
1351
1352                line_len = getline(&line, &n, file);
1353                if (line_len < 0)
1354                        break;
1355
1356                if (!line)
1357                        goto out_failure;
1358
1359                line[--line_len] = '\0'; /* \n */
1360
1361                len = hex2u64(line, &start);
1362
1363                len++;
1364                if (len + 2 >= line_len)
1365                        continue;
1366
1367                len += hex2u64(line + len, &size);
1368
1369                len++;
1370                if (len + 2 >= line_len)
1371                        continue;
1372
1373                sym = symbol__new(start, size, STB_GLOBAL, line + len);
1374
1375                if (sym == NULL)
1376                        goto out_delete_line;
1377
1378                symbols__insert(&dso->symbols[map->type], sym);
1379                nr_syms++;
1380        }
1381
1382        free(line);
1383        fclose(file);
1384
1385        return nr_syms;
1386
1387out_delete_line:
1388        free(line);
1389out_failure:
1390        return -1;
1391}
1392
1393static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1394                                           enum dso_binary_type type)
1395{
1396        switch (type) {
1397        case DSO_BINARY_TYPE__JAVA_JIT:
1398        case DSO_BINARY_TYPE__DEBUGLINK:
1399        case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1400        case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1401        case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1402        case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1403        case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1404                return !kmod && dso->kernel == DSO_TYPE_USER;
1405
1406        case DSO_BINARY_TYPE__KALLSYMS:
1407        case DSO_BINARY_TYPE__VMLINUX:
1408        case DSO_BINARY_TYPE__KCORE:
1409                return dso->kernel == DSO_TYPE_KERNEL;
1410
1411        case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1412        case DSO_BINARY_TYPE__GUEST_VMLINUX:
1413        case DSO_BINARY_TYPE__GUEST_KCORE:
1414                return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1415
1416        case DSO_BINARY_TYPE__GUEST_KMODULE:
1417        case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1418        case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1419        case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1420                /*
1421                 * kernel modules know their symtab type - it's set when
1422                 * creating a module dso in machine__findnew_module_map().
1423                 */
1424                return kmod && dso->symtab_type == type;
1425
1426        case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1427                return true;
1428
1429        case DSO_BINARY_TYPE__NOT_FOUND:
1430        default:
1431                return false;
1432        }
1433}
1434
1435int dso__load(struct dso *dso, struct map *map)
1436{
1437        char *name;
1438        int ret = -1;
1439        u_int i;
1440        struct machine *machine;
1441        char *root_dir = (char *) "";
1442        int ss_pos = 0;
1443        struct symsrc ss_[2];
1444        struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1445        bool kmod;
1446        unsigned char build_id[BUILD_ID_SIZE];
1447
1448        pthread_mutex_lock(&dso->lock);
1449
1450        /* check again under the dso->lock */
1451        if (dso__loaded(dso, map->type)) {
1452                ret = 1;
1453                goto out;
1454        }
1455
1456        if (dso->kernel) {
1457                if (dso->kernel == DSO_TYPE_KERNEL)
1458                        ret = dso__load_kernel_sym(dso, map);
1459                else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1460                        ret = dso__load_guest_kernel_sym(dso, map);
1461
1462                goto out;
1463        }
1464
1465        if (map->groups && map->groups->machine)
1466                machine = map->groups->machine;
1467        else
1468                machine = NULL;
1469
1470        dso->adjust_symbols = 0;
1471
1472        if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1473                struct stat st;
1474
1475                if (lstat(dso->name, &st) < 0)
1476                        goto out;
1477
1478                if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1479                        pr_warning("File %s not owned by current user or root, "
1480                                   "ignoring it (use -f to override).\n", dso->name);
1481                        goto out;
1482                }
1483
1484                ret = dso__load_perf_map(dso, map);
1485                dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1486                                             DSO_BINARY_TYPE__NOT_FOUND;
1487                goto out;
1488        }
1489
1490        if (machine)
1491                root_dir = machine->root_dir;
1492
1493        name = malloc(PATH_MAX);
1494        if (!name)
1495                goto out;
1496
1497        kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1498                dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1499                dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1500                dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1501
1502
1503        /*
1504         * Read the build id if possible. This is required for
1505         * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1506         */
1507        if (!dso->has_build_id &&
1508            is_regular_file(dso->long_name)) {
1509            __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1510            if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1511                dso__set_build_id(dso, build_id);
1512        }
1513
1514        /*
1515         * Iterate over candidate debug images.
1516         * Keep track of "interesting" ones (those which have a symtab, dynsym,
1517         * and/or opd section) for processing.
1518         */
1519        for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1520                struct symsrc *ss = &ss_[ss_pos];
1521                bool next_slot = false;
1522
1523                enum dso_binary_type symtab_type = binary_type_symtab[i];
1524
1525                if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1526                        continue;
1527
1528                if (dso__read_binary_type_filename(dso, symtab_type,
1529                                                   root_dir, name, PATH_MAX))
1530                        continue;
1531
1532                if (!is_regular_file(name))
1533                        continue;
1534
1535                /* Name is now the name of the next image to try */
1536                if (symsrc__init(ss, dso, name, symtab_type) < 0)
1537                        continue;
1538
1539                if (!syms_ss && symsrc__has_symtab(ss)) {
1540                        syms_ss = ss;
1541                        next_slot = true;
1542                        if (!dso->symsrc_filename)
1543                                dso->symsrc_filename = strdup(name);
1544                }
1545
1546                if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1547                        runtime_ss = ss;
1548                        next_slot = true;
1549                }
1550
1551                if (next_slot) {
1552                        ss_pos++;
1553
1554                        if (syms_ss && runtime_ss)
1555                                break;
1556                } else {
1557                        symsrc__destroy(ss);
1558                }
1559
1560        }
1561
1562        if (!runtime_ss && !syms_ss)
1563                goto out_free;
1564
1565        if (runtime_ss && !syms_ss) {
1566                syms_ss = runtime_ss;
1567        }
1568
1569        /* We'll have to hope for the best */
1570        if (!runtime_ss && syms_ss)
1571                runtime_ss = syms_ss;
1572
1573        if (syms_ss)
1574                ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1575        else
1576                ret = -1;
1577
1578        if (ret > 0) {
1579                int nr_plt;
1580
1581                nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
1582                if (nr_plt > 0)
1583                        ret += nr_plt;
1584        }
1585
1586        for (; ss_pos > 0; ss_pos--)
1587                symsrc__destroy(&ss_[ss_pos - 1]);
1588out_free:
1589        free(name);
1590        if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1591                ret = 0;
1592out:
1593        dso__set_loaded(dso, map->type);
1594        pthread_mutex_unlock(&dso->lock);
1595
1596        return ret;
1597}
1598
1599struct map *map_groups__find_by_name(struct map_groups *mg,
1600                                     enum map_type type, const char *name)
1601{
1602        struct maps *maps = &mg->maps[type];
1603        struct map *map;
1604
1605        down_read(&maps->lock);
1606
1607        for (map = maps__first(maps); map; map = map__next(map)) {
1608                if (map->dso && strcmp(map->dso->short_name, name) == 0)
1609                        goto out_unlock;
1610        }
1611
1612        map = NULL;
1613
1614out_unlock:
1615        up_read(&maps->lock);
1616        return map;
1617}
1618
1619int dso__load_vmlinux(struct dso *dso, struct map *map,
1620                      const char *vmlinux, bool vmlinux_allocated)
1621{
1622        int err = -1;
1623        struct symsrc ss;
1624        char symfs_vmlinux[PATH_MAX];
1625        enum dso_binary_type symtab_type;
1626
1627        if (vmlinux[0] == '/')
1628                snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1629        else
1630                symbol__join_symfs(symfs_vmlinux, vmlinux);
1631
1632        if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1633                symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1634        else
1635                symtab_type = DSO_BINARY_TYPE__VMLINUX;
1636
1637        if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1638                return -1;
1639
1640        err = dso__load_sym(dso, map, &ss, &ss, 0);
1641        symsrc__destroy(&ss);
1642
1643        if (err > 0) {
1644                if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1645                        dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1646                else
1647                        dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1648                dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1649                dso__set_loaded(dso, map->type);
1650                pr_debug("Using %s for symbols\n", symfs_vmlinux);
1651        }
1652
1653        return err;
1654}
1655
1656int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1657{
1658        int i, err = 0;
1659        char *filename = NULL;
1660
1661        pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1662                 vmlinux_path__nr_entries + 1);
1663
1664        for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1665                err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1666                if (err > 0)
1667                        goto out;
1668        }
1669
1670        if (!symbol_conf.ignore_vmlinux_buildid)
1671                filename = dso__build_id_filename(dso, NULL, 0);
1672        if (filename != NULL) {
1673                err = dso__load_vmlinux(dso, map, filename, true);
1674                if (err > 0)
1675                        goto out;
1676                free(filename);
1677        }
1678out:
1679        return err;
1680}
1681
1682static bool visible_dir_filter(const char *name, struct dirent *d)
1683{
1684        if (d->d_type != DT_DIR)
1685                return false;
1686        return lsdir_no_dot_filter(name, d);
1687}
1688
1689static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1690{
1691        char kallsyms_filename[PATH_MAX];
1692        int ret = -1;
1693        struct strlist *dirs;
1694        struct str_node *nd;
1695
1696        dirs = lsdir(dir, visible_dir_filter);
1697        if (!dirs)
1698                return -1;
1699
1700        strlist__for_each_entry(nd, dirs) {
1701                scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1702                          "%s/%s/kallsyms", dir, nd->s);
1703                if (!validate_kcore_addresses(kallsyms_filename, map)) {
1704                        strlcpy(dir, kallsyms_filename, dir_sz);
1705                        ret = 0;
1706                        break;
1707                }
1708        }
1709
1710        strlist__delete(dirs);
1711
1712        return ret;
1713}
1714
1715/*
1716 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1717 * since access(R_OK) only checks with real UID/GID but open() use effective
1718 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1719 */
1720static bool filename__readable(const char *file)
1721{
1722        int fd = open(file, O_RDONLY);
1723        if (fd < 0)
1724                return false;
1725        close(fd);
1726        return true;
1727}
1728
1729static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1730{
1731        u8 host_build_id[BUILD_ID_SIZE];
1732        char sbuild_id[SBUILD_ID_SIZE];
1733        bool is_host = false;
1734        char path[PATH_MAX];
1735
1736        if (!dso->has_build_id) {
1737                /*
1738                 * Last resort, if we don't have a build-id and couldn't find
1739                 * any vmlinux file, try the running kernel kallsyms table.
1740                 */
1741                goto proc_kallsyms;
1742        }
1743
1744        if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1745                                 sizeof(host_build_id)) == 0)
1746                is_host = dso__build_id_equal(dso, host_build_id);
1747
1748        /* Try a fast path for /proc/kallsyms if possible */
1749        if (is_host) {
1750                /*
1751                 * Do not check the build-id cache, unless we know we cannot use
1752                 * /proc/kcore or module maps don't match to /proc/kallsyms.
1753                 * To check readability of /proc/kcore, do not use access(R_OK)
1754                 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1755                 * can't check it.
1756                 */
1757                if (filename__readable("/proc/kcore") &&
1758                    !validate_kcore_addresses("/proc/kallsyms", map))
1759                        goto proc_kallsyms;
1760        }
1761
1762        build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1763
1764        /* Find kallsyms in build-id cache with kcore */
1765        scnprintf(path, sizeof(path), "%s/%s/%s",
1766                  buildid_dir, DSO__NAME_KCORE, sbuild_id);
1767
1768        if (!find_matching_kcore(map, path, sizeof(path)))
1769                return strdup(path);
1770
1771        /* Use current /proc/kallsyms if possible */
1772        if (is_host) {
1773proc_kallsyms:
1774                return strdup("/proc/kallsyms");
1775        }
1776
1777        /* Finally, find a cache of kallsyms */
1778        if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1779                pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1780                       sbuild_id);
1781                return NULL;
1782        }
1783
1784        return strdup(path);
1785}
1786
1787static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1788{
1789        int err;
1790        const char *kallsyms_filename = NULL;
1791        char *kallsyms_allocated_filename = NULL;
1792        /*
1793         * Step 1: if the user specified a kallsyms or vmlinux filename, use
1794         * it and only it, reporting errors to the user if it cannot be used.
1795         *
1796         * For instance, try to analyse an ARM perf.data file _without_ a
1797         * build-id, or if the user specifies the wrong path to the right
1798         * vmlinux file, obviously we can't fallback to another vmlinux (a
1799         * x86_86 one, on the machine where analysis is being performed, say),
1800         * or worse, /proc/kallsyms.
1801         *
1802         * If the specified file _has_ a build-id and there is a build-id
1803         * section in the perf.data file, we will still do the expected
1804         * validation in dso__load_vmlinux and will bail out if they don't
1805         * match.
1806         */
1807        if (symbol_conf.kallsyms_name != NULL) {
1808                kallsyms_filename = symbol_conf.kallsyms_name;
1809                goto do_kallsyms;
1810        }
1811
1812        if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1813                return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1814        }
1815
1816        if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1817                err = dso__load_vmlinux_path(dso, map);
1818                if (err > 0)
1819                        return err;
1820        }
1821
1822        /* do not try local files if a symfs was given */
1823        if (symbol_conf.symfs[0] != 0)
1824                return -1;
1825
1826        kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1827        if (!kallsyms_allocated_filename)
1828                return -1;
1829
1830        kallsyms_filename = kallsyms_allocated_filename;
1831
1832do_kallsyms:
1833        err = dso__load_kallsyms(dso, kallsyms_filename, map);
1834        if (err > 0)
1835                pr_debug("Using %s for symbols\n", kallsyms_filename);
1836        free(kallsyms_allocated_filename);
1837
1838        if (err > 0 && !dso__is_kcore(dso)) {
1839                dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1840                dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1841                map__fixup_start(map);
1842                map__fixup_end(map);
1843        }
1844
1845        return err;
1846}
1847
1848static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1849{
1850        int err;
1851        const char *kallsyms_filename = NULL;
1852        struct machine *machine;
1853        char path[PATH_MAX];
1854
1855        if (!map->groups) {
1856                pr_debug("Guest kernel map hasn't the point to groups\n");
1857                return -1;
1858        }
1859        machine = map->groups->machine;
1860
1861        if (machine__is_default_guest(machine)) {
1862                /*
1863                 * if the user specified a vmlinux filename, use it and only
1864                 * it, reporting errors to the user if it cannot be used.
1865                 * Or use file guest_kallsyms inputted by user on commandline
1866                 */
1867                if (symbol_conf.default_guest_vmlinux_name != NULL) {
1868                        err = dso__load_vmlinux(dso, map,
1869                                                symbol_conf.default_guest_vmlinux_name,
1870                                                false);
1871                        return err;
1872                }
1873
1874                kallsyms_filename = symbol_conf.default_guest_kallsyms;
1875                if (!kallsyms_filename)
1876                        return -1;
1877        } else {
1878                sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1879                kallsyms_filename = path;
1880        }
1881
1882        err = dso__load_kallsyms(dso, kallsyms_filename, map);
1883        if (err > 0)
1884                pr_debug("Using %s for symbols\n", kallsyms_filename);
1885        if (err > 0 && !dso__is_kcore(dso)) {
1886                dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1887                machine__mmap_name(machine, path, sizeof(path));
1888                dso__set_long_name(dso, strdup(path), true);
1889                map__fixup_start(map);
1890                map__fixup_end(map);
1891        }
1892
1893        return err;
1894}
1895
1896static void vmlinux_path__exit(void)
1897{
1898        while (--vmlinux_path__nr_entries >= 0)
1899                zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1900        vmlinux_path__nr_entries = 0;
1901
1902        zfree(&vmlinux_path);
1903}
1904
1905static const char * const vmlinux_paths[] = {
1906        "vmlinux",
1907        "/boot/vmlinux"
1908};
1909
1910static const char * const vmlinux_paths_upd[] = {
1911        "/boot/vmlinux-%s",
1912        "/usr/lib/debug/boot/vmlinux-%s",
1913        "/lib/modules/%s/build/vmlinux",
1914        "/usr/lib/debug/lib/modules/%s/vmlinux",
1915        "/usr/lib/debug/boot/vmlinux-%s.debug"
1916};
1917
1918static int vmlinux_path__add(const char *new_entry)
1919{
1920        vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1921        if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1922                return -1;
1923        ++vmlinux_path__nr_entries;
1924
1925        return 0;
1926}
1927
1928static int vmlinux_path__init(struct perf_env *env)
1929{
1930        struct utsname uts;
1931        char bf[PATH_MAX];
1932        char *kernel_version;
1933        unsigned int i;
1934
1935        vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1936                              ARRAY_SIZE(vmlinux_paths_upd)));
1937        if (vmlinux_path == NULL)
1938                return -1;
1939
1940        for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1941                if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1942                        goto out_fail;
1943
1944        /* only try kernel version if no symfs was given */
1945        if (symbol_conf.symfs[0] != 0)
1946                return 0;
1947
1948        if (env) {
1949                kernel_version = env->os_release;
1950        } else {
1951                if (uname(&uts) < 0)
1952                        goto out_fail;
1953
1954                kernel_version = uts.release;
1955        }
1956
1957        for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1958                snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1959                if (vmlinux_path__add(bf) < 0)
1960                        goto out_fail;
1961        }
1962
1963        return 0;
1964
1965out_fail:
1966        vmlinux_path__exit();
1967        return -1;
1968}
1969
1970int setup_list(struct strlist **list, const char *list_str,
1971                      const char *list_name)
1972{
1973        if (list_str == NULL)
1974                return 0;
1975
1976        *list = strlist__new(list_str, NULL);
1977        if (!*list) {
1978                pr_err("problems parsing %s list\n", list_name);
1979                return -1;
1980        }
1981
1982        symbol_conf.has_filter = true;
1983        return 0;
1984}
1985
1986int setup_intlist(struct intlist **list, const char *list_str,
1987                  const char *list_name)
1988{
1989        if (list_str == NULL)
1990                return 0;
1991
1992        *list = intlist__new(list_str);
1993        if (!*list) {
1994                pr_err("problems parsing %s list\n", list_name);
1995                return -1;
1996        }
1997        return 0;
1998}
1999
2000static bool symbol__read_kptr_restrict(void)
2001{
2002        bool value = false;
2003        FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2004
2005        if (fp != NULL) {
2006                char line[8];
2007
2008                if (fgets(line, sizeof(line), fp) != NULL)
2009                        value = ((geteuid() != 0) || (getuid() != 0)) ?
2010                                        (atoi(line) != 0) :
2011                                        (atoi(line) == 2);
2012
2013                fclose(fp);
2014        }
2015
2016        return value;
2017}
2018
2019int symbol__annotation_init(void)
2020{
2021        if (symbol_conf.initialized) {
2022                pr_err("Annotation needs to be init before symbol__init()\n");
2023                return -1;
2024        }
2025
2026        if (symbol_conf.init_annotation) {
2027                pr_warning("Annotation being initialized multiple times\n");
2028                return 0;
2029        }
2030
2031        symbol_conf.priv_size += sizeof(struct annotation);
2032        symbol_conf.init_annotation = true;
2033        return 0;
2034}
2035
2036int symbol__init(struct perf_env *env)
2037{
2038        const char *symfs;
2039
2040        if (symbol_conf.initialized)
2041                return 0;
2042
2043        symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2044
2045        symbol__elf_init();
2046
2047        if (symbol_conf.sort_by_name)
2048                symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2049                                          sizeof(struct symbol));
2050
2051        if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2052                return -1;
2053
2054        if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2055                pr_err("'.' is the only non valid --field-separator argument\n");
2056                return -1;
2057        }
2058
2059        if (setup_list(&symbol_conf.dso_list,
2060                       symbol_conf.dso_list_str, "dso") < 0)
2061                return -1;
2062
2063        if (setup_list(&symbol_conf.comm_list,
2064                       symbol_conf.comm_list_str, "comm") < 0)
2065                goto out_free_dso_list;
2066
2067        if (setup_intlist(&symbol_conf.pid_list,
2068                       symbol_conf.pid_list_str, "pid") < 0)
2069                goto out_free_comm_list;
2070
2071        if (setup_intlist(&symbol_conf.tid_list,
2072                       symbol_conf.tid_list_str, "tid") < 0)
2073                goto out_free_pid_list;
2074
2075        if (setup_list(&symbol_conf.sym_list,
2076                       symbol_conf.sym_list_str, "symbol") < 0)
2077                goto out_free_tid_list;
2078
2079        if (setup_list(&symbol_conf.bt_stop_list,
2080                       symbol_conf.bt_stop_list_str, "symbol") < 0)
2081                goto out_free_sym_list;
2082
2083        /*
2084         * A path to symbols of "/" is identical to ""
2085         * reset here for simplicity.
2086         */
2087        symfs = realpath(symbol_conf.symfs, NULL);
2088        if (symfs == NULL)
2089                symfs = symbol_conf.symfs;
2090        if (strcmp(symfs, "/") == 0)
2091                symbol_conf.symfs = "";
2092        if (symfs != symbol_conf.symfs)
2093                free((void *)symfs);
2094
2095        symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2096
2097        symbol_conf.initialized = true;
2098        return 0;
2099
2100out_free_sym_list:
2101        strlist__delete(symbol_conf.sym_list);
2102out_free_tid_list:
2103        intlist__delete(symbol_conf.tid_list);
2104out_free_pid_list:
2105        intlist__delete(symbol_conf.pid_list);
2106out_free_comm_list:
2107        strlist__delete(symbol_conf.comm_list);
2108out_free_dso_list:
2109        strlist__delete(symbol_conf.dso_list);
2110        return -1;
2111}
2112
2113void symbol__exit(void)
2114{
2115        if (!symbol_conf.initialized)
2116                return;
2117        strlist__delete(symbol_conf.bt_stop_list);
2118        strlist__delete(symbol_conf.sym_list);
2119        strlist__delete(symbol_conf.dso_list);
2120        strlist__delete(symbol_conf.comm_list);
2121        intlist__delete(symbol_conf.tid_list);
2122        intlist__delete(symbol_conf.pid_list);
2123        vmlinux_path__exit();
2124        symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2125        symbol_conf.bt_stop_list = NULL;
2126        symbol_conf.initialized = false;
2127}
2128
2129int symbol__config_symfs(const struct option *opt __maybe_unused,
2130                         const char *dir, int unset __maybe_unused)
2131{
2132        char *bf = NULL;
2133        int ret;
2134
2135        symbol_conf.symfs = strdup(dir);
2136        if (symbol_conf.symfs == NULL)
2137                return -ENOMEM;
2138
2139        /* skip the locally configured cache if a symfs is given, and
2140         * config buildid dir to symfs/.debug
2141         */
2142        ret = asprintf(&bf, "%s/%s", dir, ".debug");
2143        if (ret < 0)
2144                return -ENOMEM;
2145
2146        set_buildid_dir(bf);
2147
2148        free(bf);
2149        return 0;
2150}
2151