1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7
8 /// @file
9 ///
10 /// This program tests the ELF symbols lookup APIs from
11 /// abigail::dwarf_reader. It uses the lookupsym tool from the
12 /// libabigail distribution.
13
14 #include <iostream>
15 #include <cstdlib>
16 #include "abg-tools-utils.h"
17 #include "test-utils.h"
18
19 using std::cerr;
20 using std::string;
21 using abigail::tests::emit_test_status_and_update_counters;
22 using abigail::tests::emit_test_summary;
23
24 struct InOutSpec
25 {
26 const char* in_elf_path;
27 const char* symbol;
28 const char* abisym_options;
29 const char* in_report_path;
30 const char* out_report_path;
31 }; // end struct InOutSpec
32
33 InOutSpec in_out_specs[] =
34 {
35 {
36 "data/test-lookup-syms/test0.o",
37 "main",
38 "",
39 "data/test-lookup-syms/test0-report.txt",
40 "output/test-lookup-syms/test0-report.txt"
41 },
42 {
43 "data/test-lookup-syms/test0.o",
44 "foo",
45 "",
46 "data/test-lookup-syms/test01-report.txt",
47 "output/test-lookup-syms/test01-report.txt"
48 },
49 {
50 "data/test-lookup-syms/test0.o",
51 "\"bar(char)\"",
52 "--demangle",
53 "data/test-lookup-syms/test02-report.txt",
54 "output/test-lookup-syms/test02-report.txt"
55 },
56 {
57 "data/test-lookup-syms/test1.so",
58 "foo",
59 "",
60 "data/test-lookup-syms/test1-1-report.txt",
61 "output/test-lookup-syms/test1-1-report.txt"
62 },
63 {
64 "data/test-lookup-syms/test1-32bits.so",
65 "foo",
66 "",
67 "data/test-lookup-syms/test1-1-report.txt",
68 "output/test-lookup-syms/test1-1-report.txt"
69 },
70 {
71 "data/test-lookup-syms/test1.so",
72 "_foo1",
73 "--no-absolute-path",
74 "data/test-lookup-syms/test1-2-report.txt",
75 "output/test-lookup-syms/test-2-report.txt"
76 },
77 {
78 "data/test-lookup-syms/test1.so",
79 "_foo2",
80 "--no-absolute-path",
81 "data/test-lookup-syms/test1-3-report.txt",
82 "output/test-lookup-syms/test-3-report.txt"
83 },
84 // This should always be the last entry.
85 {NULL, NULL, NULL, NULL, NULL}
86 };
87
88 int
main()89 main()
90 {
91 using abigail::tests::get_src_dir;
92 using abigail::tests::get_build_dir;
93 using abigail::tools_utils::ensure_parent_dir_created;
94
95 unsigned int total_count = 0, passed_count = 0, failed_count = 0;
96
97 string in_elf_path, symbol, abisym, abisym_options,
98 ref_report_path, out_report_path;
99
100 for (InOutSpec* s = in_out_specs; s->in_elf_path; ++s)
101 {
102 bool is_ok = true;
103 in_elf_path = string(get_src_dir()) + "/tests/" + s->in_elf_path;
104 symbol = s->symbol;
105 abisym_options = s->abisym_options;
106 ref_report_path = string(get_src_dir()) + "/tests/" + s->in_report_path;
107 out_report_path =
108 string(get_build_dir()) + "/tests/" + s->out_report_path;
109
110 if (!ensure_parent_dir_created(out_report_path))
111 {
112 cerr << "could not create parent directory for "
113 << out_report_path;
114 is_ok = false;
115 continue;
116 }
117
118 abisym = string(get_build_dir()) + "/tools/abisym";
119 abisym += " " + abisym_options;
120
121 string cmd = abisym + " " + in_elf_path + " " + symbol;
122 cmd += " > " + out_report_path;
123
124 bool abisym_ok = true;
125 if (system(cmd.c_str()))
126 abisym_ok = false;
127
128 if (abisym_ok)
129 {
130 string diff_cmd = "diff -u " + ref_report_path + " "+ out_report_path;
131 if (system(diff_cmd.c_str()))
132 is_ok = false;
133 }
134 else
135 is_ok = false;
136
137 emit_test_status_and_update_counters(is_ok, cmd, passed_count,
138 failed_count, total_count);
139 }
140
141 emit_test_summary(total_count, passed_count, failed_count);
142
143 return failed_count;
144 }
145