Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 09 Dec 2025 15:49:32 +0000
From:      Ed Maste <emaste@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Cc:        ShengYi Hung <aokblast@FreeBSD.org>
Subject:   git: 87fdc35e5869 - stable/15 - lldb: Fix Architecture parsing by reading the ELF header. (#162811)
Message-ID:  <6938450c.333cd.691606d4@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help

The branch stable/15 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=87fdc35e586930d9782efee4c0217d5e19b9274a

commit 87fdc35e586930d9782efee4c0217d5e19b9274a
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2025-11-21 18:28:25 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-12-09 15:49:10 +0000

    lldb: Fix Architecture parsing by reading the ELF header. (#162811)
    
    Currently, LLDB in FreeBSD host sets the Process Architecture used by
    lldbserver as Default one. Which cause problem when trying to debug a
    32bit binary on amd64 platform since the lldb itself will found mismatch
    architecture with lldbserver's return.
    
    Notice that this patch is only a partial fix for the debugging problem.
    We are still unable to debug x86 on x86_64 so that we don't provide
    testcase in this patch.
    
    PR:             289945
    Obtained from:  llvm-project 394e7ded8b6bcff1382468b407ca620a2837f41b
    
    (cherry picked from commit fa1c56b3affaab7be6ece43070b36da2e75787cb)
---
 .../llvm-project/lldb/source/Host/freebsd/Host.cpp | 41 +++++++++++++++-------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/contrib/llvm-project/lldb/source/Host/freebsd/Host.cpp b/contrib/llvm-project/lldb/source/Host/freebsd/Host.cpp
index 110e803b3354..0778eb320dcf 100644
--- a/contrib/llvm-project/lldb/source/Host/freebsd/Host.cpp
+++ b/contrib/llvm-project/lldb/source/Host/freebsd/Host.cpp
@@ -14,12 +14,13 @@
 #include <sys/sysctl.h>
 #include <sys/user.h>
 
-#include <machine/elf.h>
-
 #include <cstdio>
 #include <dlfcn.h>
 #include <execinfo.h>
 
+#include "llvm/Object/ELF.h"
+
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Host.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Utility/DataBufferHeap.h"
@@ -101,17 +102,33 @@ GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
     proc_args.AppendArgument(llvm::StringRef(cstr));
   }
 
-  return true;
-}
-
-static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) {
-  if (process_info.ProcessIDIsValid()) {
-    process_info.GetArchitecture() =
-        HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
+  auto buffer_sp = FileSystem::Instance().CreateDataBuffer(pathname, 0x20, 0);
+  if (!buffer_sp) {
+    process_info.Clear();
     return true;
   }
-  process_info.GetArchitecture().Clear();
-  return false;
+  uint8_t exe_class =
+      llvm::object::getElfArchType(
+          {reinterpret_cast<const char *>(buffer_sp->GetBytes()),
+           size_t(buffer_sp->GetByteSize())})
+          .first;
+
+  switch (exe_class) {
+  case llvm::ELF::ELFCLASS32:
+    process_info.SetArchitecture(
+        HostInfo::GetArchitecture(HostInfo::eArchKind32));
+    break;
+  case llvm::ELF::ELFCLASS64:
+    process_info.SetArchitecture(
+        HostInfo::GetArchitecture(HostInfo::eArchKind64));
+    break;
+  case llvm::ELF::ELFCLASSNONE:
+    process_info.SetArchitecture(
+        HostInfo::GetArchitecture(HostInfo::eArchKindDefault));
+    break;
+  }
+
+  return true;
 }
 
 static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
@@ -218,7 +235,6 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
     // Make sure our info matches before we go fetch the name and cpu type
     if (match_info_noname.Matches(process_info) &&
         GetFreeBSDProcessArgs(&match_info, process_info)) {
-      GetFreeBSDProcessCPUType(process_info);
       if (match_info.Matches(process_info))
         process_infos.push_back(process_info);
     }
@@ -232,7 +248,6 @@ bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
 
   if (GetFreeBSDProcessArgs(NULL, process_info)) {
     // should use libprocstat instead of going right into sysctl?
-    GetFreeBSDProcessCPUType(process_info);
     GetFreeBSDProcessUserAndGroup(process_info);
     return true;
   }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6938450c.333cd.691606d4>