• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2022-2023 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// This file contains the declarations for the @ref fe_iface a.k.a
11 /// "Front End Interface".
12 
13 #ifndef __ABG_ELF_READER_H__
14 #define __ABG_ELF_READER_H__
15 
16 #include <memory>
17 #include <string>
18 
19 #include <elfutils/libdwfl.h>
20 
21 #include "abg-fe-iface.h"
22 #include "abg-ir.h"
23 #include "abg-suppression.h"
24 
25 namespace abigail
26 {
27 
28 /// The namespace for the ELF Reader.
29 namespace elf
30 {
31 
32 /// The kind of ELF file we are looking at.
33 enum elf_type : unsigned
34 {
35   /// A normal executable binary
36   ELF_TYPE_EXEC,
37   /// A Position Independant Executable binary
38   ELF_TYPE_PI_EXEC,
39   /// A dynamic shared object, a.k.a shared library binary.
40   ELF_TYPE_DSO,
41   /// A relocatalbe binary.
42   ELF_TYPE_RELOCATABLE,
43   /// An unknown kind of binary.
44   ELF_TYPE_UNKNOWN
45 };
46 
47 /// This is the interface an ELF reader.
48 ///
49 /// It knows how to open an ELF file, read its content and expose an
50 /// interface for its symbol table and other properties.
51 ///
52 /// Note that the ABI corpus returned by the elf::read_corpus()
53 /// member function doesn't contain any type representation.  It only
54 /// contains the representations of the the ELF symbols found in the
55 /// ELF file.
56 ///
57 /// To construct the type representations for the functions and global
58 /// variables present in the ELF file, please use the implementations
59 /// of the @ref elf_based_reader interface.  Those know how to read
60 /// the debug information from the ELF file to build type
61 /// representation in the @ref abigail::ir::corpus instance.
62 class reader : public fe_iface
63 {
64   struct priv;
65   priv *priv_;
66 
67  public:
68 
69   reader(const std::string&	elf_path,
70 	 const vector<char**>&	debug_info_roots,
71 	 environment&		env);
72 
73   ~reader();
74 
75   virtual void
76   initialize(const std::string&	elf_path,
77 	     const vector<char**>&	debug_info_roots);
78 
79   virtual void
80   initialize(const std::string& elf_path);
81 
82   const vector<char**>&
83   debug_info_root_paths() const;
84 
85   const Dwfl_Callbacks&
86   dwfl_offline_callbacks() const;
87 
88   Dwfl_Callbacks&
89   dwfl_offline_callbacks();
90 
91   Elf*
92   elf_handle() const;
93 
94   const Dwarf*
95   dwarf_debug_info() const;
96 
97   bool
98   has_dwarf_debug_info() const;
99 
100   bool
101   has_ctf_debug_info() const;
102 
103   bool
104   has_btf_debug_info() const;
105 
106   const Dwarf*
107   alternate_dwarf_debug_info() const;
108 
109   const string&
110   alternate_dwarf_debug_info_path() const;
111 
112   bool
113   refers_to_alt_debug_info(string& alt_di_path) const;
114 
115   const Elf_Scn*
116   find_symbol_table_section() const;
117 
118   void
119   reset_symbol_table_section();
120 
121   const Elf_Scn*
122   find_ctf_section() const;
123 
124   const Elf_Scn*
125   find_alternate_ctf_section() const;
126 
127   const Elf_Scn*
128   find_btf_section() const;
129 
130   const vector<string>&
131   dt_needed()const;
132 
133   const string&
134   elf_architecture() const;
135 
136   symtab_reader::symtab_sptr&
137   symtab() const;
138 
139   elf_symbol_sptr
140   function_symbol_is_exported(GElf_Addr symbol_address) const;
141 
142   elf_symbol_sptr
143   variable_symbol_is_exported(GElf_Addr symbol_address) const;
144 
145   elf_symbol_sptr
146   function_symbol_is_exported(const string& name) const;
147 
148   elf_symbol_sptr
149   variable_symbol_is_exported(const string& name) const;
150 
151   void
152   load_dt_soname_and_needed();
153 
154   void
155   load_elf_architecture();
156 
157   void
158   load_elf_properties();
159 
160   virtual ir::corpus_sptr
161   read_corpus(status& status);
162 };//end class reader.
163 
164 /// A convenience typedef for a smart pointer to a
165 /// elf::reader.
166 typedef shared_ptr<elf::reader> reader_sptr;
167 
168 bool
169 get_soname_of_elf_file(const string& path, string &soname);
170 
171 bool
172 get_type_of_elf_file(const string& path, elf_type& type);
173 } // end namespace elf.
174 } // end namespace abigail
175 
176 #endif // __ABG_ELF_READER_H__
177