1 // Copyright 2024 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #pragma once 16 17 #include <cstddef> 18 #include <cstdint> 19 20 namespace pw::elf { 21 22 // ELF data types 23 using Elf32_Half = uint16_t; 24 using Elf64_Half = uint16_t; 25 26 using Elf32_Word = uint32_t; 27 using Elf64_Word = uint32_t; 28 using Elf32_Sword = int32_t; 29 using Elf64_Sword = int32_t; 30 31 using Elf32_Xword = uint64_t; 32 using Elf64_Xword = uint64_t; 33 using Elf32_Sxword = int64_t; 34 using Elf64_Sxword = int64_t; 35 36 using Elf32_Addr = uint32_t; 37 using Elf64_Addr = uint64_t; 38 39 using Elf32_Off = uint32_t; 40 using Elf64_Off = uint64_t; 41 42 // ELF constants 43 inline constexpr size_t EI_NIDENT = 16; 44 45 inline constexpr size_t EI_MAG0 = 0; 46 inline constexpr unsigned char ELFMAG0 = 0x7f; 47 48 inline constexpr size_t EI_MAG1 = 1; 49 inline constexpr unsigned char ELFMAG1 = 'E'; 50 51 inline constexpr size_t EI_MAG2 = 2; 52 inline constexpr unsigned char ELFMAG2 = 'L'; 53 54 inline constexpr size_t EI_MAG3 = 3; 55 inline constexpr unsigned char ELFMAG3 = 'F'; 56 57 inline constexpr size_t EI_CLASS = 4; 58 inline constexpr unsigned char ELFCLASS32 = 1; 59 inline constexpr unsigned char ELFCLASS64 = 2; 60 61 inline constexpr size_t EI_DATA = 5; 62 inline constexpr unsigned char ELFDATA2LSB = 1; 63 inline constexpr unsigned char ELFDATA2MSB = 2; 64 65 // ELF structures 66 struct Elf32_Ehdr { 67 unsigned char e_ident[EI_NIDENT]; 68 Elf32_Half e_type; 69 Elf32_Half e_machine; 70 Elf32_Word e_version; 71 Elf32_Addr e_entry; 72 Elf32_Off e_phoff; 73 Elf32_Off e_shoff; 74 Elf32_Word e_flags; 75 Elf32_Half e_ehsize; 76 Elf32_Half e_phentsize; 77 Elf32_Half e_phnum; 78 Elf32_Half e_shentsize; 79 Elf32_Half e_shnum; 80 Elf32_Half e_shstrndx; 81 }; 82 static_assert(sizeof(Elf32_Ehdr) == 52); 83 84 struct Elf32_Shdr { 85 Elf32_Word sh_name; 86 Elf32_Word sh_type; 87 Elf32_Word sh_flags; 88 Elf32_Addr sh_addr; 89 Elf32_Off sh_offset; 90 Elf32_Word sh_size; 91 Elf32_Word sh_link; 92 Elf32_Word sh_info; 93 Elf32_Word sh_addralign; 94 Elf32_Word sh_entsize; 95 }; 96 static_assert(sizeof(Elf32_Shdr) == 40); 97 98 struct Elf64_Ehdr { 99 unsigned char e_ident[EI_NIDENT]; 100 Elf64_Half e_type; 101 Elf64_Half e_machine; 102 Elf64_Word e_version; 103 Elf64_Addr e_entry; 104 Elf64_Off e_phoff; 105 Elf64_Off e_shoff; 106 Elf64_Word e_flags; 107 Elf64_Half e_ehsize; 108 Elf64_Half e_phentsize; 109 Elf64_Half e_phnum; 110 Elf64_Half e_shentsize; 111 Elf64_Half e_shnum; 112 Elf64_Half e_shstrndx; 113 }; 114 static_assert(sizeof(Elf64_Ehdr) == 64); 115 116 struct Elf64_Shdr { 117 Elf64_Word sh_name; 118 Elf64_Word sh_type; 119 Elf64_Xword sh_flags; 120 Elf64_Addr sh_addr; 121 Elf64_Off sh_offset; 122 Elf64_Xword sh_size; 123 Elf64_Word sh_link; 124 Elf64_Word sh_info; 125 Elf64_Xword sh_addralign; 126 Elf64_Xword sh_entsize; 127 }; 128 static_assert(sizeof(Elf64_Shdr) == 64); 129 130 } // namespace pw::elf 131