[LLDB] OpenBSD support

Summary:
Add basic OpenBSD support. This is enough to be able to analyze core dumps for OpenBSD/amd64, OpenBSD/arm, OpenBSD/arm64 and OpenBSD/i386.

Note that part of the changes to source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp fix a bug that probably affects other platforms as well.  The GetProgramHeaderByIndex() interface use 1-based indices, but in some case when looping over the headers the, the loop starts at 0 and misses the last header.  This caused problems on OpenBSD since OpenBSD core dumps have the PT_NOTE segment as the last program header.


Reviewers: joerg, labath, krytarowski

Reviewed By: krytarowski

Subscribers: aemerson, emaste, rengolin, srhines, krytarowski, mgorny, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D31131

llvm-svn: 298810
diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index 10213f5..a218782 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -52,6 +52,7 @@
 const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
 const char *const LLDB_NT_OWNER_GNU = "GNU";
 const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
+const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";
 const char *const LLDB_NT_OWNER_CSR = "csr";
 const char *const LLDB_NT_OWNER_ANDROID = "Android";
 const char *const LLDB_NT_OWNER_CORE = "CORE";
@@ -832,7 +833,7 @@
     if (section_list) {
       if (!value_is_offset) {
         bool found_offset = false;
-        for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i) {
+        for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) {
           const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i);
           if (header == nullptr)
             continue;
@@ -1369,6 +1370,12 @@
             "ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32,
             __FUNCTION__, version_info);
     }
+    // Process OpenBSD ELF notes.
+    else if (note.n_name == LLDB_NT_OWNER_OPENBSD) {
+      // Set the elf OS version to OpenBSD.  Also clear the vendor.
+      arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD);
+      arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
+    }
     // Process CSR kalimba notes
     else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) &&
              (note.n_name == LLDB_NT_OWNER_CSR)) {
@@ -3349,7 +3356,7 @@
     // headers
     // that might shed more light on the architecture
     if (ParseProgramHeaders()) {
-      for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i) {
+      for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) {
         const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i);
         if (header && header->p_type == PT_NOTE && header->p_offset != 0 &&
             header->p_filesz > 0) {