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 file contains the declarations of the entry points to
11 /// de-serialize an instance of @ref abigail::corpus from a file in
12 /// elf format, containing dwarf information.
13 
14 #ifndef __ABG_DWARF_READER_H__
15 #define __ABG_DWARF_READER_H__
16 
17 #include <ostream>
18 #include <elfutils/libdwfl.h>
19 #include "abg-corpus.h"
20 #include "abg-suppression.h"
21 
22 namespace abigail
23 {
24 
25 /// The namespace for the DWARF reader.
26 namespace dwarf_reader
27 {
28 
29 using namespace abigail::ir;
30 
31 /// The status of the @ref read_corpus_from_elf() call.
32 enum status
33 {
34   /// The status is in an unknown state
35   STATUS_UNKNOWN = 0,
36 
37   /// This status is for when the call went OK.
38   STATUS_OK = 1,
39 
40   /// This status is for when the debug info could not be read.
41   STATUS_DEBUG_INFO_NOT_FOUND = 1 << 1,
42 
43   /// This status is for when the alternate debug info could not be
44   /// found.
45   STATUS_ALT_DEBUG_INFO_NOT_FOUND = 1 << 2,
46 
47   /// This status is for when the symbols of the ELF binaries could
48   /// not be read.
49   STATUS_NO_SYMBOLS_FOUND = 1 << 3,
50 };
51 
52 string
53 status_to_diagnostic_string(status s);
54 
55 status
56 operator|(status, status);
57 
58 status
59 operator&(status, status);
60 
61 status&
62 operator|=(status&, status);
63 
64 status&
65 operator&=(status&, status);
66 
67 /// The kind of ELF file we are looking at.
68 enum elf_type
69 {
70   /// A normal executable binary
71   ELF_TYPE_EXEC,
72   /// A Position Independant Executable binary
73   ELF_TYPE_PI_EXEC,
74   /// A dynamic shared object, a.k.a shared library binrary.
75   ELF_TYPE_DSO,
76   /// A relocatalbe binary.
77   ELF_TYPE_RELOCATABLE,
78   /// An unknown kind of binary.
79   ELF_TYPE_UNKNOWN
80 };
81 
82 class read_context;
83 
84 /// A convenience typedef for a smart pointer to a
85 /// dwarf_reader::read_context.
86 typedef shared_ptr<read_context> read_context_sptr;
87 
88 read_context_sptr
89 create_read_context(const std::string&	elf_path,
90 		    const vector<char**>& debug_info_root_paths,
91 		    ir::environment*	environment,
92 		    bool		read_all_types = false,
93 		    bool		linux_kernel_mode = false);
94 
95 const string&
96 read_context_get_path(const read_context&);
97 
98 void
99 reset_read_context(read_context_sptr &ctxt,
100 		   const std::string&	elf_path,
101 		   const vector<char**>& debug_info_root_paths,
102 		   ir::environment*	environment,
103 		   bool		read_all_types = false,
104 		   bool		linux_kernel_mode = false);
105 
106 void
107 add_read_context_suppressions(read_context& ctxt,
108 			      const suppr::suppressions_type& supprs);
109 
110 void
111 set_read_context_corpus_group(read_context& ctxt, corpus_group_sptr& group);
112 
113 corpus_sptr
114 read_corpus_from_elf(read_context& ctxt, status& stat);
115 
116 corpus_sptr
117 read_corpus_from_elf(const std::string& elf_path,
118 		     const vector<char**>& debug_info_root_paths,
119 		     ir::environment*	environment,
120 		     bool		load_all_types,
121 		     status&);
122 
123 corpus_sptr
124 read_and_add_corpus_to_group_from_elf(read_context&, corpus_group&, status&);
125 
126 bool
127 lookup_symbol_from_elf(const environment*		env,
128 		       const string&			elf_path,
129 		       const string&			symbol_name,
130 		       bool				demangle,
131 		       vector<elf_symbol_sptr>&	symbols);
132 
133 bool
134 lookup_public_function_symbol_from_elf(const environment*		env,
135 				       const string&			path,
136 				       const string&			symname,
137 				       vector<elf_symbol_sptr>&	func_syms);
138 
139 bool
140 refers_to_alt_debug_info(const read_context&	ctxt,
141 			 string&		alt_di_path);
142 
143 status
144 has_alt_debug_info(read_context&	ctxt,
145 		   bool&		has_alt_di,
146 		   string&		alt_debug_info_path);
147 
148 status
149 has_alt_debug_info(const string&	elf_path,
150 		   char**		debug_info_root_path,
151 		   bool&		has_alt_di,
152 		   string&		alt_debug_info_path);
153 
154 bool
155 get_soname_of_elf_file(const string& path, string& soname);
156 
157 bool
158 get_type_of_elf_file(const string& path, elf_type& type);
159 
160 
161 void
162 set_debug_info_root_path(read_context& ctxt,
163 			 char** path);
164 
165 char**
166 get_debug_info_root_path(read_context& ctxt,
167 			 char**& path);
168 
169 bool
170 get_show_stats(read_context& ctxt);
171 
172 void
173 set_show_stats(read_context& ctxt,
174 	       bool f);
175 
176 void
177 set_drop_undefined_syms(read_context& ctxt,
178 			bool f);
179 
180 void
181 set_merge_translation_units(read_context& ctxt,
182 			    bool f);
183 
184 void
185 set_do_log(read_context& ctxt, bool f);
186 
187 void
188 set_environment(read_context& ctxt,
189 		ir::environment*);
190 
191 const environment_sptr&
192 get_environment(const read_context& ctxt);
193 
194 environment_sptr&
195 get_environment(read_context& ctxt);
196 }// end namespace dwarf_reader
197 
198 }// end namespace abigail
199 
200 #endif //__ABG_DWARF_READER_H__
201