/* AMDGPU ELF support for BFD.

   Copyright (C) 2019-2026 Free Software Foundation, Inc.

   This file is part of BFD, the Binary File Descriptor library.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef _ELF_AMDGPU_H
#define _ELF_AMDGPU_H

#include "elf/reloc-macros.h"

/* e_ident[EI_ABIVERSION] values, when e_ident[EI_OSABI] is
   ELFOSABI_AMDGPU_HSA.

   https://llvm.org/docs/AMDGPUUsage.html#amdgpu-elf-header-enumeration-values-table  */

#define ELFABIVERSION_AMDGPU_HSA_V2 0
#define ELFABIVERSION_AMDGPU_HSA_V3 1
#define ELFABIVERSION_AMDGPU_HSA_V4 2
#define ELFABIVERSION_AMDGPU_HSA_V5 3
#define ELFABIVERSION_AMDGPU_HSA_V6 4

/* Processor selection mask for EF_AMDGPU_MACH_* values.  */

#define EF_AMDGPU_MACH 0x0ff
#define EF_AMDGPU_MACH_AMDGCN_MIN 0x020

/* EF_AMDGPU_MACH_* values

   https://llvm.org/docs/AMDGPUUsage.html#amdgpu-ef-amdgpu-mach-table  */

#define EF_AMDGPU_MACH_AMDGCN_GFX600  0x020
#define EF_AMDGPU_MACH_AMDGCN_GFX601  0x021
#define EF_AMDGPU_MACH_AMDGCN_GFX700  0x022
#define EF_AMDGPU_MACH_AMDGCN_GFX701  0x023
#define EF_AMDGPU_MACH_AMDGCN_GFX702  0x024
#define EF_AMDGPU_MACH_AMDGCN_GFX703  0x025
#define EF_AMDGPU_MACH_AMDGCN_GFX704  0x026
#define EF_AMDGPU_MACH_AMDGCN_GFX801  0x028
#define EF_AMDGPU_MACH_AMDGCN_GFX802  0x029
#define EF_AMDGPU_MACH_AMDGCN_GFX803  0x02a
#define EF_AMDGPU_MACH_AMDGCN_GFX810  0x02b
#define EF_AMDGPU_MACH_AMDGCN_GFX900  0x02c
#define EF_AMDGPU_MACH_AMDGCN_GFX902  0x02d
#define EF_AMDGPU_MACH_AMDGCN_GFX904  0x02e
#define EF_AMDGPU_MACH_AMDGCN_GFX906  0x02f
#define EF_AMDGPU_MACH_AMDGCN_GFX908  0x030
#define EF_AMDGPU_MACH_AMDGCN_GFX909  0x031
#define EF_AMDGPU_MACH_AMDGCN_GFX90C  0x032
#define EF_AMDGPU_MACH_AMDGCN_GFX1010 0x033
#define EF_AMDGPU_MACH_AMDGCN_GFX1011 0x034
#define EF_AMDGPU_MACH_AMDGCN_GFX1012 0x035
#define EF_AMDGPU_MACH_AMDGCN_GFX1030 0x036
#define EF_AMDGPU_MACH_AMDGCN_GFX1031 0x037
#define EF_AMDGPU_MACH_AMDGCN_GFX1032 0x038
#define EF_AMDGPU_MACH_AMDGCN_GFX1033 0x039
#define EF_AMDGPU_MACH_AMDGCN_GFX602  0x03a
#define EF_AMDGPU_MACH_AMDGCN_GFX705  0x03b
#define EF_AMDGPU_MACH_AMDGCN_GFX805  0x03c
#define EF_AMDGPU_MACH_AMDGCN_GFX1035 0x03d
#define EF_AMDGPU_MACH_AMDGCN_GFX1034 0x03e
#define EF_AMDGPU_MACH_AMDGCN_GFX90A  0x03f
#define EF_AMDGPU_MACH_AMDGCN_GFX940  0x040
#define EF_AMDGPU_MACH_AMDGCN_GFX1100 0x041
#define EF_AMDGPU_MACH_AMDGCN_GFX1013 0x042
#define EF_AMDGPU_MACH_AMDGCN_GFX1150 0x043
#define EF_AMDGPU_MACH_AMDGCN_GFX1036 0x045
#define EF_AMDGPU_MACH_AMDGCN_GFX1101 0x046
#define EF_AMDGPU_MACH_AMDGCN_GFX1102 0x047
#define EF_AMDGPU_MACH_AMDGCN_GFX1200 0x048
#define EF_AMDGPU_MACH_AMDGCN_GFX1250 0x049
#define EF_AMDGPU_MACH_AMDGCN_GFX1151 0x04a
#define EF_AMDGPU_MACH_AMDGCN_GFX942  0x04c
#define EF_AMDGPU_MACH_AMDGCN_GFX1201 0x04e
#define EF_AMDGPU_MACH_AMDGCN_GFX950  0x04f
#define EF_AMDGPU_MACH_AMDGCN_GFX9_GENERIC 0x051
#define EF_AMDGPU_MACH_AMDGCN_GFX10_1_GENERIC 0x052
#define EF_AMDGPU_MACH_AMDGCN_GFX10_3_GENERIC 0x053
#define EF_AMDGPU_MACH_AMDGCN_GFX11_GENERIC 0x054
#define EF_AMDGPU_MACH_AMDGCN_GFX1152 0x055
#define EF_AMDGPU_MACH_AMDGCN_GFX1153 0x058
#define EF_AMDGPU_MACH_AMDGCN_GFX12_GENERIC 0x059
#define EF_AMDGPU_MACH_AMDGCN_GFX12_5_GENERIC 0x05b
#define EF_AMDGPU_MACH_AMDGCN_GFX9_4_GENERIC 0x05f

/* Code object v3 machine flags.

   https://llvm.org/docs/AMDGPUUsage.html#amdgpu-elf-header-e-flags-table-v3  */

#define EF_AMDGPU_FEATURE_XNACK_V3   0x100
#define EF_AMDGPU_FEATURE_SRAMECC_V3 0x200

/* Code object v4 (and later) machine flags.

   https://llvm.org/docs/AMDGPUUsage.html#amdgpu-elf-header-e-flags-table-v4-v5  */

#define EF_AMDGPU_FEATURE_XNACK_V4             0x300
#define EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4 0x000
#define EF_AMDGPU_FEATURE_XNACK_ANY_V4         0x100
#define EF_AMDGPU_FEATURE_XNACK_OFF_V4         0x200
#define EF_AMDGPU_FEATURE_XNACK_ON_V4          0x300

#define EF_AMDGPU_FEATURE_SRAMECC_V4             0xc00
#define EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4 0x000
#define EF_AMDGPU_FEATURE_SRAMECC_ANY_V4         0x400
#define EF_AMDGPU_FEATURE_SRAMECC_OFF_V4         0x800
#define EF_AMDGPU_FEATURE_SRAMECC_ON_V4          0xc00

/* Code object v6 machine flags.

   https://llvm.org/docs/AMDGPUUsage.html#amdgpu-elf-header-e-flags-table-v6-onwards  */

#define EF_AMDGPU_GENERIC_VERSION_V              0xff000000
#define EF_AMDGPU_GENERIC_VERSION_V_SHIFT        24

/* Notes.

   https://llvm.org/docs/AMDGPUUsage.html#code-object-v3-and-above-note-records  */

#define NT_AMDGPU_METADATA                32

/* Relocations.

   https://llvm.org/docs/AMDGPUUsage.html#amdgpu-elf-relocation-records-table  */

START_RELOC_NUMBERS (elf_amdgpu_reloc_type)
 RELOC_NUMBER (R_AMDGPU_NONE,           0)
 RELOC_NUMBER (R_AMDGPU_ABS32_LO,       1)
 RELOC_NUMBER (R_AMDGPU_ABS32_HI,       2)
 RELOC_NUMBER (R_AMDGPU_ABS64,          3)
 RELOC_NUMBER (R_AMDGPU_REL32,          4)
 RELOC_NUMBER (R_AMDGPU_REL64,          5)
 RELOC_NUMBER (R_AMDGPU_ABS32,          6)
 RELOC_NUMBER (R_AMDGPU_GOTPCREL,       7)
 RELOC_NUMBER (R_AMDGPU_GOTPCREL32_LO,  8)
 RELOC_NUMBER (R_AMDGPU_GOTPCREL32_HI,  9)
 RELOC_NUMBER (R_AMDGPU_REL32_LO,      10)
 RELOC_NUMBER (R_AMDGPU_REL32_HI,      11)
 RELOC_NUMBER (R_AMDGPU_RELATIVE64,    13)
 RELOC_NUMBER (R_AMDGPU_REL16,         16)
END_RELOC_NUMBERS (R_AMDGPU_max)

#endif /* _ELF_AMDGPU_H */
