xref: /llvm-project/offload/plugins-nextgen/common/include/Utils/ELF.h (revision fa9e90f5d23312587b3a17920941334e0d1a58a1)
1 //===-- Utils/ELF.h - Common ELF functionality ------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common ELF functionality for target plugins.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H
14 #define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H
15 
16 #include "llvm/Object/ELF.h"
17 #include "llvm/Object/ELFObjectFile.h"
18 
19 namespace utils {
20 namespace elf {
21 
22 /// Returns true or false if the \p Buffer is an ELF file.
23 bool isELF(llvm::StringRef Buffer);
24 
25 /// Returns the ELF e_machine value of the current compilation target.
26 uint16_t getTargetMachine();
27 
28 /// Checks if the given \p Object is a valid ELF matching the e_machine value.
29 llvm::Expected<bool> checkMachine(llvm::StringRef Object, uint16_t EMachine);
30 
31 /// Returns a pointer to the given \p Symbol inside of an ELF object.
32 llvm::Expected<const void *>
33 getSymbolAddress(const llvm::object::ELFSymbolRef &Symbol);
34 
35 /// Returns the symbol associated with the \p Name in the \p ELFObj. It will
36 /// first search for the hash sections to identify symbols from the hash table.
37 /// If that fails it will fall back to a linear search in the case of an
38 /// executable file without a hash table.
39 llvm::Expected<std::optional<llvm::object::ELFSymbolRef>>
40 getSymbol(const llvm::object::ObjectFile &ELFObj, llvm::StringRef Name);
41 
42 } // namespace elf
43 } // namespace utils
44 
45 #endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H
46