(root)/
binutils-2.41/
bfd/
elf64-amdgcn.c
       1  /* AMDGCN ELF support for BFD.
       2  
       3     Copyright (C) 2019-2023 Free Software Foundation, Inc.
       4  
       5     This file is part of BFD, the Binary File Descriptor library.
       6  
       7     This program is free software; you can redistribute it and/or modify
       8     it under the terms of the GNU General Public License as published by
       9     the Free Software Foundation; either version 3 of the License, or
      10     (at your option) any later version.
      11  
      12     This program is distributed in the hope that it will be useful,
      13     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15     GNU General Public License for more details.
      16  
      17     You should have received a copy of the GNU General Public License
      18     along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      19  
      20  /* This file handles ELF files that are of the AMDGCN architecture.  The
      21     format is documented here:
      22  
      23       https://llvm.org/docs/AMDGPUUsage.html#elf-code-object */
      24  
      25  #include "sysdep.h"
      26  #include "bfd.h"
      27  #include "libbfd.h"
      28  #include "elf-bfd.h"
      29  #include "elf/amdgpu.h"
      30  
      31  #include <string.h>
      32  
      33  static bool
      34  elf64_amdgcn_object_p (bfd *abfd)
      35  {
      36    Elf_Internal_Ehdr *hdr = elf_elfheader (abfd);
      37    unsigned int mach;
      38    unsigned char osabi;
      39    unsigned char osabi_version;
      40  
      41    BFD_ASSERT (hdr->e_machine == EM_AMDGPU);
      42  
      43    osabi = hdr->e_ident[EI_OSABI];
      44    osabi_version = hdr->e_ident[EI_ABIVERSION];
      45  
      46    /* Objects with OS ABI HSA version 2 encoded the GPU model differently (in a
      47       note), but they are deprecated, so we don't need to support them.  Reject
      48       them specifically.
      49  
      50       At the time of writing, all AMDGCN objects encode the specific GPU
      51       model in the EF_AMDGPU_MACH field of e_flags.  */
      52    if (osabi == ELFOSABI_AMDGPU_HSA
      53        && osabi_version < ELFABIVERSION_AMDGPU_HSA_V3)
      54      return false;
      55  
      56    mach = elf_elfheader (abfd)->e_flags & EF_AMDGPU_MACH;
      57  
      58    /* Avoid matching non-AMDGCN AMDGPU objects (e.g. r600).  */
      59    if (mach < EF_AMDGPU_MACH_AMDGCN_MIN)
      60      return false;
      61  
      62    bfd_default_set_arch_mach (abfd, bfd_arch_amdgcn, mach);
      63    return true;
      64  }
      65  
      66  
      67  #define TARGET_LITTLE_SYM	amdgcn_elf64_le_vec
      68  #define TARGET_LITTLE_NAME	"elf64-amdgcn"
      69  #define ELF_ARCH		bfd_arch_amdgcn
      70  #define ELF_TARGET_ID		AMDGCN_ELF_DATA
      71  #define ELF_MACHINE_CODE	EM_AMDGPU
      72  #define ELF_MAXPAGESIZE		0x10000 /* 64KB */
      73  #define ELF_COMMONPAGESIZE	0x1000  /* 4KB */
      74  
      75  #define bfd_elf64_bfd_reloc_type_lookup bfd_default_reloc_type_lookup
      76  #define bfd_elf64_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
      77  
      78  #define elf_backend_object_p elf64_amdgcn_object_p
      79  
      80  #include "elf64-target.h"