Debian Codegraph @

libcextract/SymversParser.cpp

raw
1 //===- SymversParser.cpp - Parse linux Modules.symvers files ----*- C++ -*-===//
2 //
3 // This project is licensed under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Parse linux Modules.symvers files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Marcos de Paulo Souza */
15
16 #include "SymversParser.hh"
17
18 #include <iostream>
19 #include <fstream>
20 #include <string>
21 #include <sstream>
22 #include <libgen.h>
23
24 #include "NonLLVMMisc.hh"
25
26 Symvers::Symvers(const std::string &path)
27 : Parser(path)
28 {
29 Parse();
30 }
31
32 void Symvers::Parse()
33 {
34 std::string line;
35 std::ifstream f;
36 f.open(parser_path);
37
38 if (!f.is_open())
39 throw std::runtime_error("File not found: " + parser_path);
40
41 /* The Module.symvers file contains five fields separated by tabs:
42 * https://www.kernel.org/doc/html/latest/kbuild/modules.html#symbols-from-the-kernel-vmlinux-modules
43 * CRC Symbol name Module Path Export type Namespace
44 *
45 * All the symbols on Module.symvers are exported by the kernel and modules.
46 * Any of these can be empty, namespace for example. At this point we only
47 * care for the Symbol name and the module associates with it.
48 */
49 while (f.good()) {
50 std::getline(f, line);
51
52 /* Should be the last line */
53 if (line.empty())
54 continue;
55
56 std::string temp;
57 std::string sym_name;
58 std::string sym_mod;
59
60 std::stringstream ss(line);
61
62 // Discard the CRC
63 std::getline(ss, temp, '\t');
64 // Get the symbol and module
65 std::getline(ss, sym_name, '\t');
66 std::getline(ss, sym_mod, '\t');
67
68 // Only get the name of the module, instead of the path to it
69 Symbol sym(sym_name, get_basename(sym_mod.data()));
70 Insert_Symbols_Into_Hash(sym);
71 }
72 }
73
74 void Symvers::Dump(void)
75 {
76 std::cout << "Symbol\tModule" << std::endl;
77 for (auto &p : map)
78 std::cout << p.first << "\t" << p.second << std::endl;
79 }