[Lldb-commits] [lldb] r239148 - Improve OSType initialization in elf object file's arch_spec

Ed Maste emaste at freebsd.org
Fri Jun 5 06:03:08 PDT 2015


Author: emaste
Date: Fri Jun  5 08:03:08 2015
New Revision: 239148

URL: http://llvm.org/viewvc/llvm-project?rev=239148&view=rev
Log:
Improve OSType initialization in elf object file's arch_spec

Setting the OSType in the ArchSpec triple is needed to correctly setup
up the register context plugin. ArchSpec::SetArchitecture, for Mach-O
only, sets the OSType. For ELF it was left to the ObjectFileELF to fill
in the missing OSType.

This change moves the ObjectFileELF logic into ArchSpec.

A new optional 'os' parameter has been added to SetArchitecture.
For ELF, this value is the from the ELF header.e_ident[EI_OSABI].
The default value is 0 or ELFOSABI_NONE.

The real work of determining the OSType was done by the ObjectFileELF
helper function GetOsFromOSABI. This logic has been moved
SetArchitecture.

GetOsFromOSABI has been commented as being deprectated.  It is left in
to support asserts.

For ELF the vendor value returned from SetArchitecture should be
UnknownVendor.  An unneeded resetting in ObjectFileELF has been removed
and replaced with an assert.

This fixes a problem reading a core file on FreeBSD/ARM because the spec
triple was arm-unknown-unknown.

Patch by Tom Rix.

Differential Revision:     http://reviews.llvm.org/D9292

Modified:
    lldb/trunk/include/lldb/Core/ArchSpec.h
    lldb/trunk/source/Core/ArchSpec.cpp
    lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Modified: lldb/trunk/include/lldb/Core/ArchSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ArchSpec.h?rev=239148&r1=239147&r2=239148&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ArchSpec.h (original)
+++ lldb/trunk/include/lldb/Core/ArchSpec.h Fri Jun  5 08:03:08 2015
@@ -339,18 +339,46 @@ public:
     MergeFrom(const ArchSpec &other);
     
     //------------------------------------------------------------------
-    /// Change the architecture object type and CPU type.
+    /// Change the architecture object type, CPU type and OS type.
     ///
     /// @param[in] arch_type The object type of this ArchSpec.
     ///
     /// @param[in] cpu The required CPU type.
     ///
-    /// @return True if the object and CPU type were successfully set.
+    /// @param[in] os The optional OS type
+    /// The default value of 0 was choosen to from the ELF spec value
+    /// ELFOSABI_NONE.  ELF is the only one using this parameter.  If another
+    /// format uses this parameter and 0 does not work, use a value over
+    /// 255 because in the ELF header this is value is only a byte.
+    ///
+    /// @return True if the object, and CPU were successfully set.
+    ///
+    /// As a side effect, the vendor value is usually set to unknown.
+    /// The exections are
+    ///   aarch64-apple-ios
+    ///   arm-apple-ios
+    ///   thumb-apple-ios
+    ///   x86-apple-
+    ///   x86_64-apple-
+    ///
+    /// As a side effect, the os value is usually set to unknown
+    /// The exceptions are
+    ///   *-*-aix
+    ///   aarch64-apple-ios
+    ///   arm-apple-ios
+    ///   thumb-apple-ios
+    ///   powerpc-apple-darwin
+    ///   *-*-freebsd
+    ///   *-*-linux
+    ///   *-*-netbsd
+    ///   *-*-openbsd
+    ///   *-*-solaris
     //------------------------------------------------------------------
     bool
     SetArchitecture (ArchitectureType arch_type, 
                      uint32_t cpu,
-                     uint32_t sub);
+                     uint32_t sub,
+                     uint32_t os = 0);
 
     //------------------------------------------------------------------
     /// Returns the byte order for the architecture specification.

Modified: lldb/trunk/source/Core/ArchSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ArchSpec.cpp?rev=239148&r1=239147&r2=239148&view=diff
==============================================================================
--- lldb/trunk/source/Core/ArchSpec.cpp (original)
+++ lldb/trunk/source/Core/ArchSpec.cpp Fri Jun  5 08:03:08 2015
@@ -840,7 +840,7 @@ ArchSpec::MergeFrom(const ArchSpec &othe
 }
 
 bool
-ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
+ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os)
 {
     m_core = kCore_invalid;
     bool update_triple = true;
@@ -885,6 +885,23 @@ ArchSpec::SetArchitecture (ArchitectureT
                             break;
                     }
                 }
+                else if (arch_type == eArchTypeELF)
+                {
+                    llvm::Triple::OSType ostype;
+                    switch (os)
+                    {
+                        case llvm::ELF::ELFOSABI_AIX:      ostype = llvm::Triple::OSType::AIX; break;
+                        case llvm::ELF::ELFOSABI_FREEBSD:  ostype = llvm::Triple::OSType::FreeBSD; break;
+                        case llvm::ELF::ELFOSABI_GNU:      ostype = llvm::Triple::OSType::Linux; break;
+                        case llvm::ELF::ELFOSABI_NETBSD:   ostype = llvm::Triple::OSType::NetBSD; break;
+                        case llvm::ELF::ELFOSABI_OPENBSD:  ostype = llvm::Triple::OSType::OpenBSD; break;
+                        case llvm::ELF::ELFOSABI_SOLARIS:  ostype = llvm::Triple::OSType::Solaris; break;
+                        default:
+                            ostype = llvm::Triple::OSType::UnknownOS;
+                    }
+                    m_triple.setOS (ostype);
+                    m_triple.setVendor (llvm::Triple::UnknownVendor);
+                }
                 // Fall back onto setting the machine type if the arch by name failed...
                 if (m_triple.getArch () == llvm::Triple::UnknownArch)
                     m_triple.setArch (core_def->machine);

Modified: lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp?rev=239148&r1=239147&r2=239148&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp Fri Jun  5 08:03:08 2015
@@ -597,6 +597,12 @@ OSABIAsCString (unsigned char osabi_byte
 #undef _MAKE_OSABI_CASE
 }
 
+//
+// WARNING : This function is being deprecated
+// It's functionality has moved to ArchSpec::SetArchitecture
+// This function is only being kept to validate the move.
+//
+// TODO : Remove this function
 static bool
 GetOsFromOSABI (unsigned char osabi_byte, llvm::Triple::OSType &ostype)
 {
@@ -640,23 +646,28 @@ ObjectFileELF::GetModuleSpecifications (
                 const uint32_t sub_type = subTypeFromElfHeader(header);
                 spec.GetArchitecture().SetArchitecture(eArchTypeELF,
                                                        header.e_machine,
-                                                       sub_type);
+                                                       sub_type,
+                                                       header.e_ident[EI_OSABI]);
 
                 if (spec.GetArchitecture().IsValid())
                 {
                     llvm::Triple::OSType ostype;
-                    // First try to determine the OS type from the OSABI field in the elf header.
+                    llvm::Triple::VendorType vendor;
+                    llvm::Triple::OSType spec_ostype = spec.GetArchitecture ().GetTriple ().getOS ();
 
                     if (log)
                         log->Printf ("ObjectFileELF::%s file '%s' module OSABI: %s", __FUNCTION__, file.GetPath ().c_str (), OSABIAsCString (header.e_ident[EI_OSABI]));
-                    if (GetOsFromOSABI (header.e_ident[EI_OSABI], ostype) && ostype != llvm::Triple::OSType::UnknownOS)
-                    {
-                        spec.GetArchitecture ().GetTriple ().setOS (ostype);
-
-                        // Also clear the vendor so we don't end up with situations like
-                        // x86_64-apple-FreeBSD.
-                        spec.GetArchitecture ().GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
 
+                    // SetArchitecture should have set the vendor to unknown
+                    vendor = spec.GetArchitecture ().GetTriple ().getVendor ();
+                    assert(vendor == llvm::Triple::UnknownVendor);
+
+                    //
+                    // Validate it is ok to remove GetOsFromOSABI
+                    GetOsFromOSABI (header.e_ident[EI_OSABI], ostype);
+                    assert(spec_ostype == ostype);
+                    if (spec_ostype != llvm::Triple::OSType::UnknownOS)
+                    {
                         if (log)
                             log->Printf ("ObjectFileELF::%s file '%s' set ELF module OS type from ELF header OSABI.", __FUNCTION__, file.GetPath ().c_str ());
                     }
@@ -1387,8 +1398,15 @@ ObjectFileELF::GetSectionHeaderInfo(Sect
     // We'll refine this with note data as we parse the notes.
     if (arch_spec.GetTriple ().getOS () == llvm::Triple::OSType::UnknownOS)
     {
+        llvm::Triple::OSType ostype;
+        llvm::Triple::OSType spec_ostype;
         const uint32_t sub_type = subTypeFromElfHeader(header);
-        arch_spec.SetArchitecture (eArchTypeELF, header.e_machine, sub_type);
+        arch_spec.SetArchitecture (eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
+        //
+        // Validate if it is ok to remove GetOsFromOSABI
+        GetOsFromOSABI (header.e_ident[EI_OSABI], ostype);
+        spec_ostype = arch_spec.GetTriple ().getOS ();
+        assert(spec_ostype == ostype);
     }
 
     // If there are no section headers we are done.





More information about the lldb-commits mailing list