1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAPLEBE_INCLUDE_CG_IFILE_TYPE_H 17 #define MAPLEBE_INCLUDE_CG_IFILE_TYPE_H 18 #include <cstdint> 19 20 namespace maplebe { 21 // The following definitions are excerpted from the elf.h 22 #define EI_NIDENT (16) 23 #define EI_MAG0 0 /* File identification byte 0 index */ 24 #define EI_MAG1 1 /* File identification byte 1 index */ 25 #define EI_MAG2 2 /* File identification byte 2 index */ 26 #define EI_MAG3 3 /* File identification byte 3 index */ 27 #define EI_CLASS 4 /* File class byte index */ 28 #define EI_DATA 5 /* Data encoding byte index */ 29 #define EI_VERSION 6 /* File version byte index */ 30 #define EI_OSABI 7 /* OS ABI identification */ 31 #define EI_ABIVERSION 8 /* ABI version */ 32 #define EI_PAD 9 /* Byte index of padding bytes */ 33 #define ELFCLASS64 2 /* 64-bit objects */ 34 #define ELFDATA2LSB 1 /* 2's complement, little endian */ 35 #define ELFMAG0 0x7f /* Magic number byte 0 */ 36 #define ELFOSABI_LINUX 3 /* Compatibility alias. */ 37 #define ELFOSABI_NONE 0 /* UNIX System V ABI */ 38 #define ELFMAG1 'E' /* Magic number byte 1 */ 39 #define ELFMAG2 'L' /* Magic number byte 2 */ 40 #define ELFMAG3 'F' /* Magic number byte 3 */ 41 #define EM_AARCH64 183 /* ARM AARCH64 */ 42 #define EM_X86_64 62 /* AMD x86-64 architecture */ 43 #define ET_REL 1 /* Relocatable file */ 44 #define EV_CURRENT 1 /* Current version */ 45 #define R_AARCH64_ADR_PREL_PG_HI21 275 /* Page-rel. ADRP imm. from 32:12. */ 46 #define R_AARCH64_ADD_ABS_LO12_NC 277 /* Dir. ADD imm. from bits 11:0. */ 47 #define R_AARCH64_CALL26 283 /* Likewise for CALL. */ 48 #define R_X86_64_32 10 /* Direct 32 bit zero extended */ 49 #define R_X86_64_64 1 /* Direct 64 bit */ 50 #define R_X86_64_NONE 0 /* No reloc */ 51 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */ 52 #define R_X86_64_PC64 24 /* PC relative 64 bit */ 53 #define R_X86_64_PLT32 4 /* 32 bit PLT address */ 54 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ 55 #define SHF_EXECINSTR (1 << 2) /* Executable */ 56 #define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */ 57 #define SHF_WRITE (1 << 0) /* Writable */ 58 #define SHF_MASKPROC 0xf0000000 /* Processor-specific */ 59 #define SHN_COMMON 0xfff2 /* Associated symbol is common */ 60 #define SHT_NULL 0 /* Section header table entry unused */ 61 #define SHT_PROGBITS 1 /* Program data */ 62 #define SHT_SYMTAB 2 /* Symbol table */ 63 #define SHT_STRTAB 3 /* String table */ 64 #define SHT_RELA 4 /* Relocation entries with addends */ 65 #define SHT_NOBITS 8 /* Program space with no data (bss) */ 66 #define STB_LOCAL 0 /* Local symbol */ 67 #define STB_GLOBAL 1 /* Global symbol */ 68 #define STB_WEAK 2 /* Weak symbol */ 69 #define STT_NOTYPE 0 /* Symbol type is unspecified */ 70 #define STT_OBJECT 1 /* Symbol is a data object */ 71 #define STT_FUNC 2 /* Symbol is a code object */ 72 #define STT_SECTION 3 /* Symbol associated with a section */ 73 74 using Address = uint64_t; 75 using Offset = uint64_t; 76 using Word = uint32_t; 77 using Xword = uint64_t; 78 using SectionIndex = uint16_t; 79 using Sxword = int64_t; 80 81 typedef struct { 82 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ 83 uint16_t e_type; 84 uint16_t e_machine; 85 uint32_t e_version; 86 uint64_t e_entry; /* Entry point virtual address */ 87 uint64_t e_phoff; /* Program header table file offset */ 88 uint64_t e_shoff; /* Section header table file offset */ 89 uint32_t e_flags; 90 uint16_t e_ehsize; 91 uint16_t e_phentsize; 92 uint16_t e_phnum; 93 uint16_t e_shentsize; 94 uint16_t e_shnum; 95 uint16_t e_shstrndx; 96 } FileHeader; 97 98 typedef struct { 99 uint32_t sh_name; /* Section name, index in string tbl */ 100 uint32_t sh_type; /* Type of section */ 101 uint64_t sh_flags; /* Miscellaneous section attributes */ 102 uint64_t sh_addr; /* Section virtual addr at execution */ 103 uint64_t sh_offset; /* Section file offset */ 104 uint64_t sh_size; /* Size of section in bytes */ 105 uint32_t sh_link; /* Index of another section */ 106 uint32_t sh_info; /* Additional section information */ 107 uint64_t sh_addralign; /* Section alignment */ 108 uint64_t sh_entsize; /* Entry size if section holds table */ 109 } SectionHeader; 110 111 typedef struct { 112 uint32_t st_name; /* Symbol name, index in string tbl */ 113 unsigned char st_info; /* Type and binding attributes */ 114 unsigned char st_other; /* No defined meaning, 0 */ 115 uint16_t st_shndx; /* Associated section index */ 116 uint64_t st_value; /* Value of the symbol */ 117 uint64_t st_size; /* Associated symbol size */ 118 } Symbol; 119 120 typedef struct { 121 uint32_t p_type; 122 uint32_t p_flags; 123 uint64_t p_offset; 124 uint64_t p_vaddr; 125 uint64_t p_paddr; 126 uint64_t p_filesz; 127 uint64_t p_memsz; 128 uint64_t p_align; 129 } SegmentHeader; 130 131 typedef struct { 132 int64_t d_tag; /* entry tag value */ 133 union { 134 uint64_t d_val; 135 uint64_t d_ptr; 136 } d_un; 137 } DynSectionEntry; 138 139 typedef struct { 140 uint64_t r_offset; /* Location at which to apply the action */ 141 uint64_t r_info; /* index and type of relocation */ 142 int64_t r_addend; /* Constant addend used to compute value */ 143 } Rela; 144 } // namespace maplebe 145 #endif /* MAPLEBE_INCLUDE_CG_IFILE_TYPE_H */