Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds conversion functions for EI_OSABI in elf #89280

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

feg208
Copy link
Contributor

@feg208 feg208 commented Apr 18, 2024

These are needed to populate elf headers in core files. This is part of implementing process save-core in lldb

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 18, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Fred Grim (feg208)

Changes

These are needed to populate elf headers in core files. This is part of implementing process save-core in lldb


Full diff: https://github.com/llvm/llvm-project/pull/89280.diff

2 Files Affected:

  • (modified) llvm/include/llvm/BinaryFormat/ELF.h (+6)
  • (modified) llvm/lib/BinaryFormat/ELF.cpp (+80)
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index ed267c100b76eb..56b5d4e399c636 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1939,6 +1939,12 @@ uint16_t convertArchNameToEMachine(StringRef Arch);
 /// Convert an ELF's e_machine value into an architecture name.
 StringRef convertEMachineToArchName(uint16_t EMachine);
 
+/// Convert a OS into ELF's EI_OSABI value.
+uint8_t convertOSToOSAbi(StringRef OS);
+
+/// Convert an ELF's e_machine value into an architecture name.
+StringRef convertOSAbiToOS(uint8_t OSAbi);
+
 } // end namespace ELF
 } // end namespace llvm
 
diff --git a/llvm/lib/BinaryFormat/ELF.cpp b/llvm/lib/BinaryFormat/ELF.cpp
index f4cedffa8b45bd..8c10ed1a980bbc 100644
--- a/llvm/lib/BinaryFormat/ELF.cpp
+++ b/llvm/lib/BinaryFormat/ELF.cpp
@@ -567,3 +567,83 @@ StringRef ELF::convertEMachineToArchName(uint16_t EMachine) {
     return "None";
   }
 }
+
+uint8_t ELF::convertOSToOSAbi(StringRef OS) {
+  std::string LowerOS = OS.lower();
+  return StringSwitch<uint16_t>(LowerOS)
+      .StartsWith("hpux", ELFOSABI_HPUX)
+      .StartsWith("netbsd", ELFOSABI_NETBSD)
+      .StartsWith("linux", ELFOSABI_LINUX)
+      .StartsWith("hurd", ELFOSABI_HURD)
+      .StartsWith("solaris", ELFOSABI_SOLARIS)
+      .StartsWith("aix", ELFOSABI_AIX)
+      .StartsWith("irix", ELFOSABI_IRIX)
+      .StartsWith("freebsd", ELFOSABI_FREEBSD)
+      .StartsWith("tru64", ELFOSABI_TRU64)
+      .StartsWith("modesto", ELFOSABI_MODESTO)
+      .StartsWith("openbsd", ELFOSABI_OPENBSD)
+      .StartsWith("openvms", ELFOSABI_OPENVMS)
+      .StartsWith("nsk", ELFOSABI_NSK)
+      .StartsWith("aros", ELFOSABI_AROS)
+      .StartsWith("fenixos", ELFOSABI_FENIXOS)
+      .StartsWith("cloudabi", ELFOSABI_CLOUDABI)
+      .StartsWith("cuda", ELFOSABI_CUDA)
+      .StartsWith("amdhsa", ELFOSABI_AMDGPU_HSA)
+      .StartsWith("amdpal", ELFOSABI_AMDGPU_PAL)
+      .StartsWith("mesa3d", ELFOSABI_AMDGPU_MESA3D)
+      .StartsWith("arm", ELFOSABI_ARM)
+      .StartsWith("standalone", ELFOSABI_STANDALONE)
+      .StartsWith("none", ELFOSABI_NONE)
+      .Default(ELFOSABI_NONE);
+}
+
+StringRef ELF::convertOSAbiToOS(uint8_t OSAbi) {
+  switch (OSAbi) {
+  case ELFOSABI_HPUX:
+    return "hpux";
+  case ELFOSABI_NETBSD:
+    return "netbsd";
+  case ELFOSABI_LINUX:
+    return "linux";
+  case ELFOSABI_HURD:
+    return "hurd";
+  case ELFOSABI_SOLARIS:
+    return "solaris";
+  case ELFOSABI_AIX:
+    return "aix";
+  case ELFOSABI_IRIX:
+    return "irix";
+  case ELFOSABI_FREEBSD:
+    return "freebsd";
+  case ELFOSABI_TRU64:
+    return "tru64";
+  case ELFOSABI_MODESTO:
+    return "modesto";
+  case ELFOSABI_OPENBSD:
+    return "openbsd";
+  case ELFOSABI_OPENVMS:
+    return "openvms";
+  case ELFOSABI_NSK:
+    return "nsk";
+  case ELFOSABI_AROS:
+    return "aros";
+  case ELFOSABI_FENIXOS:
+    return "fenixos";
+  case ELFOSABI_CLOUDABI:
+    return "cloudabi";
+  case ELFOSABI_CUDA:
+    return "cuda";
+  case ELFOSABI_AMDGPU_HSA:
+    return "amdhsa";
+  case ELFOSABI_AMDGPU_PAL:
+    return "amdpal";
+  case ELFOSABI_AMDGPU_MESA3D:
+    return "mesa3d";
+  case ELFOSABI_ARM:
+    return "arm";
+  case ELFOSABI_STANDALONE:
+    return "standalone";
+  default:
+    return "none";
+  }
+}

Copy link
Contributor

@zeroomega zeroomega left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@feg208 feg208 merged commit 23d88a8 into llvm:main Apr 18, 2024
5 of 6 checks passed
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like some basic unit testing for this should really exist. I'll acknowledge that there's probably not a huge amount of point testing every single OS/OSABI pair, but you could test a few arbitrary cases, plus the "none" and default cases.

@MaskRay
Copy link
Member

MaskRay commented Apr 26, 2024

I feel like some basic unit testing for this should really exist. I'll acknowledge that there's probably not a huge amount of point testing every single OS/OSABI pair, but you could test a few arbitrary cases, plus the "none" and default cases.

Agreed. Created #90270 for unittests and adjusting ELFOSABI_LINUX (historical alias, deprecated https://sourceware.org/bugzilla/show_bug.cgi?id=12913)

MaskRay added a commit that referenced this pull request Apr 29, 2024
Adjust #89280:

* ELFOSABI_LINUX is a historical alias that should not be used in new
  code. readelf -h displays "UNIX - GNU" instead of "Linux".
* "OS" is inappropriate. Some values are architecture-specific, e.g.
  ELFOSABI_ARM.
* Drop lowercase, which seems a job of the caller.

Add some unittests.

Pull Request: #90270
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants