Integrate new symtab reader into corpus and read_context

While reading the corpus in the read_context, also load the new type
symtab object side-by-side and set it accordingly in the resulting
corpus. This is still side by side and passive code that gets active in
the following changes. This is applicable for the dwarf reader as well
as for the reader that consumes XML.

	* include/abg-corpus.h (corpus::set_symtab): New method declaration.
	  (corpus::get_symtab): New method declaration.
	* include/abg-fwd.h (symtab_reader::symtab_sptr): New forward
	  declaration.
	* src/abg-corpus-priv.h (corpus::priv::symtab_): New data member.
	* src/abg-corpus.cc
	  (corpus::set_symtab): Likewise.
	  (corpus::get_symtab): Likewise.
	* src/abg-dwarf-reader.cc (read_context::symtab_): New data member.
	  (read_context::initialize): reset symtab_ as well
	  (read_context::symtab): new method that loads a symtab on
	  first access and returns it.
	  (read_debug_info_into_corpus): also set the new symtab object
	  on the current corpus.
	  (read_corpus_from_elf): Also determine (i.e. load) the new
	  symtab object and contribute to the load status.
	* src/abg-reader.cc (read_corpus_from_input): also set the new
	  type symtab when reading from xml.
	* tests/test-symtab.cc: Add test assertions.

Reviewed-by: Giuliano Procida <gprocida@google.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
Change-Id: I3210c628c1b1f370cccc4e0730051612b3e4b36f
diff --git a/include/abg-corpus.h b/include/abg-corpus.h
index e60b501..4257021 100644
--- a/include/abg-corpus.h
+++ b/include/abg-corpus.h
@@ -153,6 +153,12 @@
   operator==(const corpus&) const;
 
   void
+  set_symtab(symtab_reader::symtab_sptr);
+
+  const symtab_reader::symtab_sptr&
+  get_symtab() const;
+
+  void
   set_fun_symbol_map(string_elf_symbols_map_sptr);
 
   void
diff --git a/include/abg-fwd.h b/include/abg-fwd.h
index bb839fe..76df1b5 100644
--- a/include/abg-fwd.h
+++ b/include/abg-fwd.h
@@ -1375,6 +1375,14 @@
 
 } // end namespace suppr
 
+namespace symtab_reader
+{
+
+class symtab;
+typedef std::shared_ptr<symtab> symtab_sptr;
+
+} // end namespace symtab_reader
+
 void
 dump(const decl_base_sptr, std::ostream&);
 
diff --git a/src/abg-corpus-priv.h b/src/abg-corpus-priv.h
index a4a97ad..290e19c 100644
--- a/src/abg-corpus-priv.h
+++ b/src/abg-corpus-priv.h
@@ -17,6 +17,7 @@
 #include "abg-internal.h"
 #include "abg-regex.h"
 #include "abg-sptr-utils.h"
+#include "abg-symtab-reader.h"
 
 namespace abigail
 {
@@ -684,6 +685,7 @@
   string_elf_symbols_map_sptr			undefined_var_symbol_map;
   elf_symbols					sorted_var_symbols;
   elf_symbols					sorted_undefined_var_symbols;
+  symtab_reader::symtab_sptr			symtab_;
   string_elf_symbols_map_sptr			fun_symbol_map;
   string_elf_symbols_map_sptr			undefined_fun_symbol_map;
   elf_symbols					sorted_fun_symbols;
diff --git a/src/abg-corpus.cc b/src/abg-corpus.cc
index 2c37e25..0d5849d 100644
--- a/src/abg-corpus.cc
+++ b/src/abg-corpus.cc
@@ -23,6 +23,7 @@
 #include "abg-ir.h"
 #include "abg-reader.h"
 #include "abg-sptr-utils.h"
+#include "abg-symtab-reader.h"
 #include "abg-tools-utils.h"
 #include "abg-writer.h"
 
@@ -850,6 +851,14 @@
 	  && j == other.get_translation_units().end());
 }
 
+void
+corpus::set_symtab(symtab_reader::symtab_sptr symtab)
+{priv_->symtab_ = symtab;}
+
+const symtab_reader::symtab_sptr&
+corpus::get_symtab() const
+{ return priv_->symtab_; }
+
 /// Setter of the function symbols map.
 ///
 /// @param map a shared pointer to the new function symbols map.
diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index 2eee213..83f206c 100644
--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -44,6 +44,7 @@
 
 #include "abg-dwarf-reader.h"
 #include "abg-sptr-utils.h"
+#include "abg-symtab-reader.h"
 #include "abg-tools-utils.h"
 
 ABG_END_EXPORT_DECLARATIONS
@@ -2259,6 +2260,9 @@
   bool				drop_undefined_syms_;
   read_context();
 
+private:
+  mutable symtab_reader::symtab_sptr symtab_;
+
 public:
 
   /// Constructor of read_context.
@@ -2408,6 +2412,8 @@
     dt_soname_.clear();
     elf_architecture_.clear();
 
+    symtab_.reset();
+
     clear_per_translation_unit_data();
 
     memset(&offline_callbacks_, 0, sizeof(offline_callbacks_));
@@ -5807,6 +5813,21 @@
     return symbol;
   }
 
+  const symtab_reader::symtab_sptr&
+  symtab() const
+  {
+    if (!symtab_)
+      symtab_ = symtab_reader::symtab::load(
+	elf_handle(), options_.env, [&](const elf_symbol_sptr& symbol) {
+	  return is_elf_symbol_suppressed(symbol);
+	});
+
+    if (!symtab_)
+      std::cerr << "Symbol table of '" << elf_path_
+		<< "' could not be loaded\n";
+    return symtab_;
+  }
+
   /// Getter for a pointer to the map that associates the address of
   /// an entry point of a function with the symbol of that function.
   ///
@@ -15925,6 +15946,7 @@
     group->add_corpus(ctxt.current_corpus());
 
   // Set symbols information to the corpus.
+  ctxt.current_corpus()->set_symtab(ctxt.symtab());
   if (!get_ignore_symbol_table(ctxt))
     {
       if (ctxt.load_in_linux_kernel_mode()
@@ -17207,6 +17229,9 @@
 	status |= STATUS_NO_SYMBOLS_FOUND;
     }
 
+  if (!ctxt.symtab() || !ctxt.symtab()->has_symbols())
+    status |= STATUS_NO_SYMBOLS_FOUND;
+
   if (// If no elf symbol was found ...
       status & STATUS_NO_SYMBOLS_FOUND
       // ... or if debug info was found but not the required alternate
diff --git a/src/abg-reader.cc b/src/abg-reader.cc
index 1fb0cf7..68b19cf 100644
--- a/src/abg-reader.cc
+++ b/src/abg-reader.cc
@@ -33,6 +33,7 @@
 #include "abg-libxml-utils.h"
 #include "abg-reader.h"
 #include "abg-corpus.h"
+#include "abg-symtab-reader.h"
 
 #ifdef WITH_ZIP_ARCHIVE
 #include "abg-libzip-utils.h"
@@ -1907,6 +1908,8 @@
       // Note that it's possible that both fn_sym_db and var_sym_db
       // are nil, due to potential suppression specifications.  That's
       // fine.
+      corp.set_symtab(symtab_reader::symtab::load(fn_sym_db, var_sym_db));
+
       if (fn_sym_db)
 	{
 	  corp.set_fun_symbol_map(fn_sym_db);
diff --git a/tests/data/test-symtab/basic/no_debug_info.c b/tests/data/test-symtab/basic/no_debug_info.c
index 5bb380b..8ac0901 100644
--- a/tests/data/test-symtab/basic/no_debug_info.c
+++ b/tests/data/test-symtab/basic/no_debug_info.c
@@ -1 +1 @@
-// empty!
+void exported_function(){}
diff --git a/tests/data/test-symtab/basic/no_debug_info.so b/tests/data/test-symtab/basic/no_debug_info.so
index 827c1ee..0b23101 100755
--- a/tests/data/test-symtab/basic/no_debug_info.so
+++ b/tests/data/test-symtab/basic/no_debug_info.so
Binary files differ
diff --git a/tests/test-symtab.cc b/tests/test-symtab.cc
index ffb20a4..ff467d8 100644
--- a/tests/test-symtab.cc
+++ b/tests/test-symtab.cc
@@ -53,14 +53,9 @@
   const std::string	     binary = "basic/empty.so";
   corpus_sptr		     corpus_ptr;
   const dwarf_reader::status status = read_corpus(binary, corpus_ptr);
-  REQUIRE(corpus_ptr);
+  REQUIRE(!corpus_ptr);
 
-  REQUIRE((status & dwarf_reader::STATUS_OK));
-
-  // TODO: Those two assertions are currently not met. Empty symtabs are
-  //       currently treated like the error case.
-  // REQUIRE((status & dwarf_reader::STATUS_OK));
-  // REQUIRE((status & dwarf_reader::STATUS_NO_SYMBOLS_FOUND));
+  REQUIRE((status & dwarf_reader::STATUS_NO_SYMBOLS_FOUND));
 }
 
 TEST_CASE("Symtab::NoDebugInfo", "[symtab, basic]")
@@ -70,9 +65,9 @@
   const dwarf_reader::status status = read_corpus(binary, corpus_ptr);
   REQUIRE(corpus_ptr);
 
-  REQUIRE(status
-	  == (dwarf_reader::STATUS_OK
-	      | dwarf_reader::STATUS_DEBUG_INFO_NOT_FOUND));
+  REQUIRE(
+    status
+    == (dwarf_reader::STATUS_OK | dwarf_reader::STATUS_DEBUG_INFO_NOT_FOUND));
 }
 
 // this value indicates in the following helper method, that we do not want to