Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Refactor elfutil #220

Open
wants to merge 1 commit into
base: aomp11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions openmp/libomptarget/include/elf_common.h

This file was deleted.

2 changes: 1 addition & 1 deletion openmp/libomptarget/plugins/amdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ add_library(omptarget.rtl.amdgpu SHARED
impl/atmi.cpp
impl/atmi_interop_hsa.cpp
impl/data.cpp
impl/elfutil.cpp
impl/get_elf_mach_gfx_name.cpp
impl/machine.cpp
impl/system.cpp
impl/utils.cpp
Expand Down
15 changes: 0 additions & 15 deletions openmp/libomptarget/plugins/amdgpu/impl/elf_amd.h

This file was deleted.

75 changes: 0 additions & 75 deletions openmp/libomptarget/plugins/amdgpu/impl/elfutil.cpp

This file was deleted.

43 changes: 43 additions & 0 deletions openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "get_elf_mach_gfx_name.h"

// This header conflicts with the system elf.h (macros vs enums of the same
// identifier) and contains more up to date values for the enum checked here.
// rtl.cpp uses the system elf.h.
#include "llvm/BinaryFormat/ELF.h"

const char *get_elf_mach_gfx_name(uint32_t EFlags) {
using namespace llvm::ELF;
uint32_t Gfx = (EFlags & EF_AMDGPU_MACH);
switch (Gfx) {
case EF_AMDGPU_MACH_AMDGCN_GFX801:
return "gfx801";
case EF_AMDGPU_MACH_AMDGCN_GFX802:
return "gfx802";
case EF_AMDGPU_MACH_AMDGCN_GFX803:
return "gfx803";
case EF_AMDGPU_MACH_AMDGCN_GFX810:
return "gfx810";
case EF_AMDGPU_MACH_AMDGCN_GFX900:
return "gfx900";
case EF_AMDGPU_MACH_AMDGCN_GFX902:
return "gfx902";
case EF_AMDGPU_MACH_AMDGCN_GFX904:
return "gfx904";
case EF_AMDGPU_MACH_AMDGCN_GFX906:
return "gfx906";
case EF_AMDGPU_MACH_AMDGCN_GFX908:
return "gfx908";
case EF_AMDGPU_MACH_AMDGCN_GFX909:
return "gfx909";
case EF_AMDGPU_MACH_AMDGCN_GFX1010:
return "gfx1010";
case EF_AMDGPU_MACH_AMDGCN_GFX1011:
return "gfx1011";
case EF_AMDGPU_MACH_AMDGCN_GFX1012:
return "gfx1012";
case EF_AMDGPU_MACH_AMDGCN_GFX1030:
return "gfx1030";
default:
return "--unknown gfx";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef GET_ELF_MACH_GFX_NAME_H_INCLUDED
#define GET_ELF_MACH_GFX_NAME_H_INCLUDED

#include <stdint.h>

const char *get_elf_mach_gfx_name(uint32_t EFlags);

#endif
39 changes: 36 additions & 3 deletions openmp/libomptarget/plugins/amdgpu/src/rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" hsa_status_t hostrpc_init();
extern "C" hsa_status_t hostrpc_terminate();

#include "internal.h"

#include "get_elf_mach_gfx_name.h"
#include "Debug.h"
#include "omptargetplugin.h"

Expand Down Expand Up @@ -79,7 +79,6 @@ uint32_t TgtStackItemSize = 0;
#endif

#include "../../common/elf_common.c"
#include "../impl/elf_amd.h"

/// Keep entries table per device
struct FuncOrGblEntryTy {
Expand Down Expand Up @@ -619,6 +618,40 @@ void finiAsyncInfoPtr(__tgt_async_info *async_info_ptr) {
assert(async_info_ptr->Queue);
async_info_ptr->Queue = 0;
}

bool elf_machine_id_is_amdgcn(__tgt_device_image *image) {
const uint16_t amdgcnMachineID = EM_AMDGPU;
int32_t r = elf_check_machine(image, amdgcnMachineID);
if (!r) {
DP("Supported machine ID not found\n");
}
return r;
}

uint32_t elf_e_flags(__tgt_device_image *image) {
char *img_begin = (char *)image->ImageStart;
size_t img_size = (char *)image->ImageEnd - img_begin;

Elf *e = elf_memory(img_begin, img_size);
if (!e) {
DP("Unable to get ELF handle: %s!\n", elf_errmsg(-1));
return 0;
}

Elf64_Ehdr *eh64 = elf64_getehdr(e);

if (!eh64) {
DP("Unable to get machine ID from ELF file!\n");
elf_end(e);
return 0;
}

uint32_t Flags = eh64->e_flags;

elf_end(e);
DP("ELF Flags: 0x%x\n", Flags);
return Flags;
}
} // namespace

int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *image) {
Expand Down Expand Up @@ -1044,7 +1077,7 @@ __tgt_target_table *__tgt_rtl_load_binary_locked(int32_t device_id,
"Possible gpu arch mismatch: device:%s, image:%s please check"
" compiler flag: -march=<gpu>\n",
DeviceInfo.GPUName[device_id].c_str(),
get_elf_mach_gfx_name(image));
get_elf_mach_gfx_name(elf_e_flags(image)));
return NULL;
}
}
Expand Down
44 changes: 3 additions & 41 deletions openmp/libomptarget/plugins/common/elf_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
//
//===----------------------------------------------------------------------===//

#if !(defined(_OMPTARGET_H_) && defined(DP))
#error Include elf_common.c in the plugin source AFTER omptarget.h has been\
included and macro DP(...) has been defined.
#if !(defined(_OMPTARGET_DEBUG_H))
#error Include elf_common.c in the plugin source AFTER Debug.h has\
been included.
#endif

#include <elf.h>
Expand Down Expand Up @@ -109,41 +109,3 @@ static inline int32_t elf_is_dynamic(__tgt_device_image *image) {
DP("ELF Type: %d\n", Type);
return Type == ET_DYN;
}

static inline uint32_t elf_e_flags(__tgt_device_image *image) {

char *img_begin = (char *)image->ImageStart;
char *img_end = (char *)image->ImageEnd;
size_t img_size = img_end - img_begin;

// Obtain elf handler
Elf *e = elf_memory(img_begin, img_size);
if (!e) {
DP("Unable to get ELF handle: %s!\n", elf_errmsg(-1));
return 0;
}

Elf64_Ehdr *eh64 = elf64_getehdr(e);
Elf32_Ehdr *eh32 = elf32_getehdr(e);

if (!eh64 && !eh32) {
DP("Unable to get machine ID from ELF file!\n");
elf_end(e);
return 0;
}

uint32_t Flags;
if (eh64 && !eh32)
Flags = eh64->e_flags;
else if (eh32 && !eh64)
Flags = eh32->e_flags;
else {
DP("Ambiguous ELF header!\n");
elf_end(e);
return 0;
}

elf_end(e);
DP("ELF Flags: 0x%x\n", Flags);
return Flags;
}