Make the library usable as a library.

- Add namespace unwindstack everywhere so that it's easier for other
  code to use the library.
- Move some of the header files into include/unwindstack so that they
  can be exposed.
- Modify the headers so that only a limited number need to be exposed.
- Update the tools to use the new headers.
- Add a GetLoadBias() call on the Elf object. This prevents the need
  to get the interface object out of the Elf object.
- Move the GetRelPc() call out of the Reg class, to the Elf class. It's
  not always the case that a Reg object will be around when you want to
  get a relative pc. The tests for this moved to ElfTest.cpp.

Bug: 23762183

Test: Unit tests pass.
Change-Id: Iac609dac1dd90ed83d1a1e24ff2579c96c023bc3
diff --git a/libunwindstack/tools/unwind.cpp b/libunwindstack/tools/unwind.cpp
index 34cd1ce..642105a 100644
--- a/libunwindstack/tools/unwind.cpp
+++ b/libunwindstack/tools/unwind.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <elf.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <signal.h>
@@ -27,10 +28,11 @@
 
 #include <string>
 
-#include "Elf.h"
-#include "Maps.h"
-#include "Memory.h"
-#include "Regs.h"
+#include <unwindstack/Elf.h>
+#include <unwindstack/MapInfo.h>
+#include <unwindstack/Maps.h>
+#include <unwindstack/Memory.h>
+#include <unwindstack/Regs.h>
 
 static bool Attach(pid_t pid) {
   if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) {
@@ -54,14 +56,14 @@
 }
 
 void DoUnwind(pid_t pid) {
-  RemoteMaps remote_maps(pid);
+  unwindstack::RemoteMaps remote_maps(pid);
   if (!remote_maps.Parse()) {
     printf("Failed to parse map data.\n");
     return;
   }
 
   uint32_t machine_type;
-  Regs* regs = Regs::RemoteGet(pid, &machine_type);
+  unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid, &machine_type);
   if (regs == nullptr) {
     printf("Unable to get remote reg data\n");
     return;
@@ -90,20 +92,20 @@
   }
   printf("\n");
 
-  MemoryRemote remote_memory(pid);
+  unwindstack::MemoryRemote remote_memory(pid);
   for (size_t frame_num = 0; frame_num < 64; frame_num++) {
     if (regs->pc() == 0) {
       break;
     }
-    MapInfo* map_info = remote_maps.Find(regs->pc());
+    unwindstack::MapInfo* map_info = remote_maps.Find(regs->pc());
     if (map_info == nullptr) {
       printf("Failed to find map data for the pc\n");
       break;
     }
 
-    Elf* elf = map_info->GetElf(pid, true);
+    unwindstack::Elf* elf = map_info->GetElf(pid, true);
 
-    uint64_t rel_pc = regs->GetRelPc(elf, map_info);
+    uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
     uint64_t adjusted_rel_pc = rel_pc;
     // Don't need to adjust the first frame pc.
     if (frame_num != 0) {
diff --git a/libunwindstack/tools/unwind_info.cpp b/libunwindstack/tools/unwind_info.cpp
index e889f8c..66a9439 100644
--- a/libunwindstack/tools/unwind_info.cpp
+++ b/libunwindstack/tools/unwind_info.cpp
@@ -25,13 +25,16 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <unwindstack/DwarfSection.h>
+#include <unwindstack/DwarfStructs.h>
+#include <unwindstack/Elf.h>
+#include <unwindstack/ElfInterface.h>
+#include <unwindstack/Log.h>
+
 #include "ArmExidx.h"
-#include "DwarfSection.h"
-#include "DwarfStructs.h"
-#include "Elf.h"
-#include "ElfInterface.h"
 #include "ElfInterfaceArm.h"
-#include "Log.h"
+
+namespace unwindstack {
 
 void DumpArm(ElfInterfaceArm* interface) {
   if (interface == nullptr) {
@@ -100,27 +103,12 @@
   }
 }
 
-int main(int argc, char** argv) {
-  if (argc != 2) {
-    printf("Need to pass the name of an elf file to the program.\n");
-    return 1;
-  }
-
-  struct stat st;
-  if (stat(argv[1], &st) == -1) {
-    printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
-    return 1;
-  }
-  if (!S_ISREG(st.st_mode)) {
-    printf("%s is not a regular file.\n", argv[1]);
-    return 1;
-  }
-
+int GetElfInfo(const char* file) {
   // Send all log messages to stdout.
   log_to_stdout(true);
 
   MemoryFileAtOffset* memory = new MemoryFileAtOffset;
-  if (!memory->Init(argv[1], 0)) {
+  if (!memory->Init(file, 0)) {
     // Initializatation failed.
     printf("Failed to init\n");
     return 1;
@@ -128,7 +116,7 @@
 
   Elf elf(memory);
   if (!elf.Init() || !elf.valid()) {
-    printf("%s is not a valid elf file.\n", argv[1]);
+    printf("%s is not a valid elf file.\n", file);
     return 1;
   }
 
@@ -173,3 +161,24 @@
 
   return 0;
 }
+
+}  // namespace unwindstack
+
+int main(int argc, char** argv) {
+  if (argc != 2) {
+    printf("Need to pass the name of an elf file to the program.\n");
+    return 1;
+  }
+
+  struct stat st;
+  if (stat(argv[1], &st) == -1) {
+    printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
+    return 1;
+  }
+  if (!S_ISREG(st.st_mode)) {
+    printf("%s is not a regular file.\n", argv[1]);
+    return 1;
+  }
+
+  return unwindstack::GetElfInfo(argv[1]);
+}
diff --git a/libunwindstack/tools/unwind_symbols.cpp b/libunwindstack/tools/unwind_symbols.cpp
index c010dfc..b757c1e 100644
--- a/libunwindstack/tools/unwind_symbols.cpp
+++ b/libunwindstack/tools/unwind_symbols.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <elf.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <stdio.h>
@@ -22,9 +23,9 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "Elf.h"
-#include "ElfInterface.h"
-#include "Log.h"
+#include <unwindstack/Elf.h>
+#include <unwindstack/Log.h>
+#include <unwindstack/Memory.h>
 
 int main(int argc, char** argv) {
   if (argc != 2) {
@@ -43,15 +44,15 @@
   }
 
   // Send all log messages to stdout.
-  log_to_stdout(true);
+  unwindstack::log_to_stdout(true);
 
-  MemoryFileAtOffset* memory = new MemoryFileAtOffset;
+  unwindstack::MemoryFileAtOffset* memory = new unwindstack::MemoryFileAtOffset;
   if (!memory->Init(argv[1], 0)) {
     printf("Failed to init\n");
     return 1;
   }
 
-  Elf elf(memory);
+  unwindstack::Elf elf(memory);
   if (!elf.Init() || !elf.valid()) {
     printf("%s is not a valid elf file.\n", argv[1]);
     return 1;