1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _LOS_LD_ELH_PRI_H 33 #define _LOS_LD_ELH_PRI_H 34 35 #include "los_base.h" 36 37 #ifdef __cplusplus 38 #if __cplusplus 39 extern "C" { 40 #endif /* __cplusplus */ 41 #endif /* __cplusplus */ 42 43 /* Elf header */ 44 #define LD_EI_NIDENT 16 45 46 typedef struct { 47 UINT8 elfIdent[LD_EI_NIDENT]; /* Magic number and other info */ 48 UINT16 elfType; /* Object file type */ 49 UINT16 elfMachine; /* Architecture */ 50 UINT32 elfVersion; /* Object file version */ 51 UINT32 elfEntry; /* Entry point virtual address */ 52 UINT32 elfPhoff; /* Program header table file offset */ 53 UINT32 elfShoff; /* Section header table file offset */ 54 UINT32 elfFlags; /* Processor-specific flags */ 55 UINT16 elfHeadSize; /* ELF header size in bytes */ 56 UINT16 elfPhEntSize; /* Program header table entry size */ 57 UINT16 elfPhNum; /* Program header table entry count */ 58 UINT16 elfShEntSize; /* Section header table entry size */ 59 UINT16 elfShNum; /* Section header table entry count */ 60 UINT16 elfShStrIndex; /* Section header string table index */ 61 } LDElf32Ehdr; 62 63 typedef struct { 64 UINT8 elfIdent[LD_EI_NIDENT]; /* Magic number and other info */ 65 UINT16 elfType; /* Object file type */ 66 UINT16 elfMachine; /* Architecture */ 67 UINT32 elfVersion; /* Object file version */ 68 UINT64 elfEntry; /* Entry point virtual address */ 69 UINT64 elfPhoff; /* Program header table file offset */ 70 UINT64 elfShoff; /* Section header table file offset */ 71 UINT32 elfFlags; /* Processor-specific flags */ 72 UINT16 elfHeadSize; /* ELF header size in bytes */ 73 UINT16 elfPhEntSize; /* Program header table entry size */ 74 UINT16 elfPhNum; /* Program header table entry count */ 75 UINT16 elfShEntSize; /* Section header table entry size */ 76 UINT16 elfShNum; /* Section header table entry count */ 77 UINT16 elfShStrIndex; /* Section header string table index */ 78 } LDElf64Ehdr; 79 80 #ifdef LOSCFG_AARCH64 81 #define LD_ELF_EHDR LDElf64Ehdr 82 #else 83 #define LD_ELF_EHDR LDElf32Ehdr 84 #endif 85 86 /* e_ident[] values */ 87 #define LD_EI_MAG0 0 88 #define LD_EI_MAG1 1 89 #define LD_EI_MAG2 2 90 #define LD_EI_MAG3 3 91 #define LD_EI_CLASS 4 92 #define LD_EI_DATA 5 93 #define LD_EI_VERSION 6 94 #define LD_EI_PAD 7 95 96 #define LD_ELFMAG0 0x7f 97 #define LD_ELFMAG1 'E' 98 #define LD_ELFMAG2 'L' 99 #define LD_ELFMAG3 'F' 100 #define LD_ELFMAG "\177ELF" 101 #define LD_SELFMAG 4 102 103 /* EI_CLASS */ 104 #define LD_ELF_CLASS_NONE 0 105 #define LD_ELF_CLASS32 1 106 #define LD_ELF_CLASS64 2 107 108 #ifdef LOSCFG_AARCH64 109 #define LD_ELF_CLASS LD_ELF_CLASS64 110 #else 111 #define LD_ELF_CLASS LD_ELF_CLASS32 112 #endif 113 114 /* EI_DATA */ 115 #define LD_ELF_DATA_NONE 0 116 #define LD_ELF_DATA2LSB 1 117 #define LD_ELF_DATA2MSB 2 118 119 /* e_type */ 120 #define LD_ET_NONE 0 121 #define LD_ET_REL 1 122 #define LD_ET_EXEC 2 123 #define LD_ET_DYN 3 124 #define LD_ET_CORE 4 125 #define LD_ET_LOPROC 0xff00 126 #define LD_ET_HIPROC 0xffff 127 128 /* e_machine */ 129 #define LD_EM_NONE 0 /* No machine */ 130 #define LD_EM_M32 1 /* AT&T WE 32100 */ 131 #define LD_EM_SPARC 2 /* SPARC */ 132 #define LD_EM_386 3 /* Intel 80386 */ 133 #define LD_EM_68K 4 /* Motorola 68000 */ 134 #define LD_EM_88K 5 /* Motorola 88000 */ 135 #define LD_EM_486 6 /* Intel 80486 */ 136 #define LD_EM_860 7 /* Intel 80860 */ 137 #define LD_EM_MIPS 8 /* MIPS RS3000 Big-Endian */ 138 #define LD_EM_MIPS_RS4_BE 10 /* MIPS RS4000 Big-Endian */ 139 #define LD_EM_PPC_OLD 17 /* PowerPC - old */ 140 #define LD_EM_PPC 20 /* PowerPC */ 141 #define LD_EM_RCE_OLD 25 /* RCE - old */ 142 #define LD_EM_RCE 39 /* RCE */ 143 #define LD_EM_MCORE 39 /* MCORE */ 144 #define LD_EM_SH 42 /* SH */ 145 #define LD_EM_M32R 36929 /* M32R */ 146 #define LD_EM_NEC 36992 /* NEC 850 series */ 147 #define LD_EM_NEC_830 36 /* NEC 830 series */ 148 #define LD_EM_SC 58 /* SC */ 149 #define LD_EM_ARM 40 /* ARM */ 150 #define LD_EM_XTENSA 0x5E /* XTENSA */ 151 #define LD_EM_AARCH64 183 /* ARM AARCH64 */ 152 153 #ifdef LOSCFG_AARCH64 154 #define LD_EM_TYPE LD_EM_AARCH64 155 #else 156 #define LD_EM_TYPE LD_EM_ARM 157 #endif 158 159 /* e_flags */ 160 #define LD_EF_PPC_EMB 0x80000000 161 162 #define LD_EF_MIPS_NOREORDER 0x00000001 163 #define LD_EF_MIPS_PIC 0x00000002 164 #define LD_EF_MIPS_CPIC 0x00000004 165 #define LD_EF_MIPS_ARCH 0xf0000000 166 #define LD_EF_MIPS_ARCH_MIPS_2 0x10000000 167 #define LD_EF_MIPS_ARCH_MIPS_3 0x20000000 168 169 #define LD_PT_NULL 0 170 #define LD_PT_LOAD 1 171 #define LD_PT_DYNAMIC 2 172 #define LD_PT_INTERP 3 173 #define LD_PT_NOTE 4 174 #define LD_PT_SHLIB 5 175 #define LD_PT_PHDR 6 176 #define LD_PT_GNU_STACK 0x6474e551 177 178 /* e_version and EI_VERSION */ 179 #define LD_EV_NONE 0 180 #define LD_EV_CURRENT 1 181 182 /* Program Header */ 183 typedef struct { 184 UINT32 type; /* Segment type */ 185 UINT32 offset; /* Segment file offset */ 186 UINT32 vAddr; /* Segment virtual address */ 187 UINT32 phyAddr; /* Segment physical address */ 188 UINT32 fileSize; /* Segment size in file */ 189 UINT32 memSize; /* Segment size in memory */ 190 UINT32 flags; /* Segment flags */ 191 UINT32 align; /* Segment alignment */ 192 } LDElf32Phdr; 193 194 typedef struct { 195 UINT32 type; /* Segment type */ 196 UINT32 flags; /* Segment flags */ 197 UINT64 offset; /* Segment file offset */ 198 UINT64 vAddr; /* Segment virtual address */ 199 UINT64 phyAddr; /* Segment physical address */ 200 UINT64 fileSize; /* Segment size in file */ 201 UINT64 memSize; /* Segment size in memory */ 202 UINT64 align; /* Segment alignment */ 203 } LDElf64Phdr; 204 205 #ifdef LOSCFG_AARCH64 206 #define LD_ELF_PHDR LDElf64Phdr 207 #else 208 #define LD_ELF_PHDR LDElf32Phdr 209 #endif 210 211 /* Special section indexes */ 212 #define LD_SHN_UNDEF 0 213 #define LD_SHN_LORESERVE 0xff00 214 #define LD_SHN_LOPROC 0xff00 215 #define LD_SHN_HIPROC 0xff1f 216 #define LD_SHN_ABS 0xfff1 217 #define LD_SHN_COMMON 0xfff2 218 #define LD_SHN_HIRESERVE 0xffff 219 #define LD_SHN_GHCOMMON 0xff00 220 221 /* Section header */ 222 typedef struct { 223 UINT32 shName; /* Section name (string tbl index) */ 224 UINT32 shType; /* Section type */ 225 UINT32 shFlags; /* Section flags */ 226 UINT32 shAddr; /* Section virtual addr at execution */ 227 UINT32 shOffset; /* Section file offset */ 228 UINT32 shSize; /* Section size in bytes */ 229 UINT32 shLink; /* Link to another section */ 230 UINT32 shInfo; /* Additional section information */ 231 UINT32 shAddrAlign; /* Section alignment */ 232 UINT32 shEntSize; /* Entry size if section holds table */ 233 } LDElf32Shdr; 234 235 typedef struct { 236 UINT32 shName; /* Section name (string tbl index) */ 237 UINT32 shType; /* Section type */ 238 UINT64 shFlags; /* Section flags */ 239 UINT64 shAddr; /* Section virtual addr at execution */ 240 UINT64 shOffset; /* Section file offset */ 241 UINT64 shSize; /* Section size in bytes */ 242 UINT32 shLink; /* Link to another section */ 243 UINT32 shInfo; /* Additional section information */ 244 UINT64 shAddrAlign; /* Section alignment */ 245 UINT64 shEntSize; /* Entry size if section holds table */ 246 } LDElf64Shdr; 247 248 #ifdef LOSCFG_AARCH64 249 #define LD_ELF_SHDR LDElf64Shdr 250 #else 251 #define LD_ELF_SHDR LDElf32Shdr 252 #endif 253 254 /* sh_type */ 255 #define LD_SHT_NULL 0U 256 #define LD_SHT_PROGBITS 1U 257 #define LD_SHT_SYMTAB 2U 258 #define LD_SHT_STRTAB 3U 259 #define LD_SHT_RELA 4U 260 #define LD_SHT_HASH 5U 261 #define LD_SHT_DYNAMIC 6U 262 #define LD_SHT_NOTE 7U 263 #define LD_SHT_NOBITS 8U 264 #define LD_SHT_REL 9U 265 #define LD_SHT_SHLIB 10U 266 #define LD_SHT_DYNSYM 11U 267 #define LD_SHT_COMDAT 12U 268 #define LD_SHT_LOPROC 0x70000000U 269 #define LD_SHT_HIPROC 0x7fffffffU 270 #define LD_SHT_LOUSER 0x80000000U 271 #define LD_SHT_HIUSER 0xffffffffU 272 273 /* sh_flags */ 274 #define LD_SHF_WRITE 0x1U 275 #define LD_SHF_ALLOC 0x2U 276 #define LD_SHF_EXECINSTR 0x4U 277 #define LD_SHF_MASKPROC 0xf0000000U 278 279 /* Symbol table */ 280 typedef struct { 281 UINT32 stName; /* Symbol table name (string tbl index) */ 282 UINT32 stValue; /* Symbol table value */ 283 UINT32 stSize; /* Symbol table size */ 284 UINT8 stInfo; /* Symbol table type and binding */ 285 UINT8 stOther; /* Symbol table visibility */ 286 UINT16 stShndx; /* Section table index */ 287 } LDElf32Sym; 288 289 typedef struct { 290 UINT32 stName; /* Symbol table name (string tbl index) */ 291 UINT8 stInfo; /* Symbol table type and binding */ 292 UINT8 stOther; /* Symbol table visibility */ 293 UINT16 stShndx; /* Section table index */ 294 UINT64 stValue; /* Symbol table value */ 295 UINT64 stSize; /* Symbol table size */ 296 } LDElf64Sym; 297 298 #ifdef LOSCFG_AARCH64 299 #define LD_ELF_SYM LDElf64Sym 300 #else 301 #define LD_ELF_SYM LDElf32Sym 302 #endif 303 304 #define LD_STN_UNDEF 0U 305 306 #define LD_STB_LOCAL 0U 307 #define LD_STB_GLOBAL 1U 308 #define LD_STB_WEAK 2U 309 #define LD_STB_LOPROC 13U 310 #define LD_STB_HIPROC 15U 311 312 #define LD_STT_NOTYPE 0U 313 #define LD_STT_OBJECT 1U 314 #define LD_STT_FUNC 2U 315 #define LD_STT_SECTION 3U 316 #define LD_STT_FILE 4U 317 #define LD_STT_LOPROC 13U 318 #define LD_STT_HIPROC 15U 319 #define LD_STT_THUMB 0x80U 320 321 #define LD_ELF_ST_BIND(info) ((info) >> 4) /* Obtain the binding information of the symbol table */ 322 #define LD_ELF_ST_TYPE(info) ((info) & 0xFU) /* Obtain the type of the symbol table */ 323 #define LD_ELF_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xFU)) 324 325 /* Dynamic */ 326 typedef struct { 327 UINT32 dynTag; /* Dynamic entry type */ 328 union { 329 UINT32 val; /* Integer value */ 330 UINT32 ptr; /* Address value */ 331 } dyn; 332 } LDElf32Dyn; 333 334 typedef struct { 335 UINT64 dynTag; /* Dynamic entry type */ 336 union { 337 UINT64 val; /* Integer value */ 338 UINT64 ptr; /* Address value */ 339 } dyn; 340 } LDElf64Dyn; 341 342 #ifdef LOSCFG_AARCH64 343 #define LD_ELF_DYN LDElf64Dyn 344 #else 345 #define LD_ELF_DYN LDElf32Dyn 346 #endif 347 348 /* This is the info that is needed to parse the dynamic section of the file */ 349 #define LD_DT_NULL 0 350 #define LD_DT_NEEDED 1 351 #define LD_DT_PLTRELSZ 2 352 #define LD_DT_PLTGOT 3 353 #define LD_DT_HASH 4 354 #define LD_DT_STRTAB 5 355 #define LD_DT_SYMTAB 6 356 #define LD_DT_RELA 7 357 #define LD_DT_RELASZ 8 358 #define LD_DT_RELAENT 9 359 #define LD_DT_STRSZ 10 360 #define LD_DT_SYMENT 11 361 #define LD_DT_INIT 12 362 #define LD_DT_FINI 13 363 #define LD_DT_SONAME 14 364 #define LD_DT_RPATH 15 365 #define LD_DT_SYMBOLIC 16 366 #define LD_DT_REL 17 367 #define LD_DT_RELSZ 18 368 #define LD_DT_RELENT 19 369 #define LD_DT_PLTREL 20 370 #define LD_DT_DEBUG 21 371 #define LD_DT_TEXTREL 22 372 #define LD_DT_JMPREL 23 373 #define LD_DT_ENCODING 32 374 375 /* Relocation */ 376 typedef struct { 377 UINT32 offset; /* Address */ 378 UINT32 info; /* Relocation type and symbol index */ 379 } LDElf32Rel; 380 381 typedef struct { 382 UINT32 offset; /* Address */ 383 UINT32 info; /* Relocation type and symbol index */ 384 INT32 addend; /* Addend */ 385 } LDElf32Rela; 386 387 typedef struct { 388 UINT64 offset; /* Address */ 389 UINT64 info; /* Relocation type and symbol index */ 390 } LDElf64Rel; 391 392 typedef struct { 393 UINT64 offset; /* Address */ 394 UINT64 info; /* Relocation type and symbol index */ 395 INT64 addend; /* Addend */ 396 } LDElf64Rela; 397 398 #ifdef LOSCFG_AARCH64 399 #define LD_ELF_REL LDElf64Rel 400 #define LD_ELF_RELA LDElf64Rela 401 #else 402 #define LD_ELF_REL LDElf32Rel 403 #define LD_ELF_RELA LDElf32Rela 404 #endif 405 406 #ifdef LOSCFG_AARCH64 407 #define LD_ELF_R_SYM(info) ((info) >> 32) 408 #define LD_ELF_R_TYPE(info) ((info) & 0xFFFFFFFFUL) 409 #define LD_ELF_R_INFO(sym, type) ((((UINT64)(sym)) << 32) + (type)) 410 #else 411 #define LD_ELF_R_SYM(info) ((info) >> 8) 412 #define LD_ELF_R_TYPE(info) ((info) & 0xFFU) 413 #define LD_ELF_R_INFO(sym, type) (((sym) << 8) + (UINT8)(type)) 414 #endif 415 416 #ifdef __cplusplus 417 #if __cplusplus 418 } 419 #endif /* __cplusplus */ 420 #endif /* __cplusplus */ 421 422 #endif /* _LOS_LD_ELH_PRI_H */ 423