1330d8983SJohannes Doerfert //===-- Utils/ELF.h - Common ELF functionality ------------------*- C++ -*-===// 2330d8983SJohannes Doerfert // 3330d8983SJohannes Doerfert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4330d8983SJohannes Doerfert // See https://llvm.org/LICENSE.txt for license information. 5330d8983SJohannes Doerfert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6330d8983SJohannes Doerfert // 7330d8983SJohannes Doerfert //===----------------------------------------------------------------------===// 8330d8983SJohannes Doerfert // 9330d8983SJohannes Doerfert // Common ELF functionality for target plugins. 10330d8983SJohannes Doerfert // 11330d8983SJohannes Doerfert //===----------------------------------------------------------------------===// 12330d8983SJohannes Doerfert 13330d8983SJohannes Doerfert #ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H 14330d8983SJohannes Doerfert #define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H 15330d8983SJohannes Doerfert 16330d8983SJohannes Doerfert #include "llvm/Object/ELF.h" 17330d8983SJohannes Doerfert #include "llvm/Object/ELFObjectFile.h" 18330d8983SJohannes Doerfert 19330d8983SJohannes Doerfert namespace utils { 20330d8983SJohannes Doerfert namespace elf { 21330d8983SJohannes Doerfert 22330d8983SJohannes Doerfert /// Returns true or false if the \p Buffer is an ELF file. 23330d8983SJohannes Doerfert bool isELF(llvm::StringRef Buffer); 24330d8983SJohannes Doerfert 25*e3938f4dSJoseph Huber /// Returns the ELF e_machine value of the current compilation target. 26*e3938f4dSJoseph Huber uint16_t getTargetMachine(); 27*e3938f4dSJoseph Huber 28330d8983SJohannes Doerfert /// Checks if the given \p Object is a valid ELF matching the e_machine value. 29330d8983SJohannes Doerfert llvm::Expected<bool> checkMachine(llvm::StringRef Object, uint16_t EMachine); 30330d8983SJohannes Doerfert 31330d8983SJohannes Doerfert /// Returns a pointer to the given \p Symbol inside of an ELF object. 32330d8983SJohannes Doerfert llvm::Expected<const void *> 33330d8983SJohannes Doerfert getSymbolAddress(const llvm::object::ELFSymbolRef &Symbol); 34330d8983SJohannes Doerfert 35330d8983SJohannes Doerfert /// Returns the symbol associated with the \p Name in the \p ELFObj. It will 36330d8983SJohannes Doerfert /// first search for the hash sections to identify symbols from the hash table. 37330d8983SJohannes Doerfert /// If that fails it will fall back to a linear search in the case of an 38330d8983SJohannes Doerfert /// executable file without a hash table. 39330d8983SJohannes Doerfert llvm::Expected<std::optional<llvm::object::ELFSymbolRef>> 40330d8983SJohannes Doerfert getSymbol(const llvm::object::ObjectFile &ELFObj, llvm::StringRef Name); 41330d8983SJohannes Doerfert 42330d8983SJohannes Doerfert } // namespace elf 43330d8983SJohannes Doerfert } // namespace utils 44330d8983SJohannes Doerfert 45330d8983SJohannes Doerfert #endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H 46