| 1 | |
| 2 | /*** Start of inlined file: elfio_dump.hpp ***/ |
| 3 | #ifndef ELFIO_DUMP_HPP |
| 4 | #define ELFIO_DUMP_HPP |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <string> |
| 8 | #include <ostream> |
| 9 | #include <sstream> |
| 10 | #include <iomanip> |
| 11 | |
| 12 | /*** Start of inlined file: elfio.hpp ***/ |
| 13 | #ifndef ELFIO_HPP |
| 14 | #define ELFIO_HPP |
| 15 | |
| 16 | #ifdef _MSC_VER |
| 17 | #pragma warning( push ) |
| 18 | #pragma warning( disable : 4996 ) |
| 19 | #pragma warning( disable : 4355 ) |
| 20 | #pragma warning( disable : 4244 ) |
| 21 | #endif |
| 22 | |
| 23 | #include <string> |
| 24 | #include <iostream> |
| 25 | #include <fstream> |
| 26 | #include <functional> |
| 27 | #include <algorithm> |
| 28 | #include <vector> |
| 29 | #include <deque> |
| 30 | #include <iterator> |
| 31 | |
| 32 | |
| 33 | /*** Start of inlined file: elf_types.hpp ***/ |
| 34 | #ifndef ELFTYPES_H |
| 35 | #define ELFTYPES_H |
| 36 | |
| 37 | #ifndef ELFIO_NO_OWN_TYPES |
| 38 | #if !defined( ELFIO_NO_CSTDINT ) && !defined( ELFIO_NO_INTTYPES ) |
| 39 | #include <stdint.h> |
| 40 | #else |
| 41 | typedef unsigned char uint8_t; |
| 42 | typedef signed char int8_t; |
| 43 | typedef unsigned short uint16_t; |
| 44 | typedef signed short int16_t; |
| 45 | #ifdef _MSC_VER |
| 46 | typedef unsigned __int32 uint32_t; |
| 47 | typedef signed __int32 int32_t; |
| 48 | typedef unsigned __int64 uint64_t; |
| 49 | typedef signed __int64 int64_t; |
| 50 | #else |
| 51 | typedef unsigned int uint32_t; |
| 52 | typedef signed int int32_t; |
| 53 | typedef unsigned long long uint64_t; |
| 54 | typedef signed long long int64_t; |
| 55 | #endif // _MSC_VER |
| 56 | #endif // ELFIO_NO_CSTDINT |
| 57 | #endif // ELFIO_NO_OWN_TYPES |
| 58 | |
| 59 | namespace ELFIO { |
| 60 | |
| 61 | // Attention! Platform depended definitions. |
| 62 | typedef uint16_t Elf_Half; |
| 63 | typedef uint32_t Elf_Word; |
| 64 | typedef int32_t Elf_Sword; |
| 65 | typedef uint64_t Elf_Xword; |
| 66 | typedef int64_t Elf_Sxword; |
| 67 | |
| 68 | typedef uint32_t Elf32_Addr; |
| 69 | typedef uint32_t Elf32_Off; |
| 70 | typedef uint64_t Elf64_Addr; |
| 71 | typedef uint64_t Elf64_Off; |
| 72 | |
| 73 | #define Elf32_Half Elf_Half |
| 74 | #define Elf64_Half Elf_Half |
| 75 | #define Elf32_Word Elf_Word |
| 76 | #define Elf64_Word Elf_Word |
| 77 | #define Elf32_Sword Elf_Sword |
| 78 | #define Elf64_Sword Elf_Sword |
| 79 | |
| 80 | /////////////////////// |
| 81 | // ELF Header Constants |
| 82 | |
| 83 | // File type |
| 84 | #define ET_NONE 0 |
| 85 | #define ET_REL 1 |
| 86 | #define ET_EXEC 2 |
| 87 | #define ET_DYN 3 |
| 88 | #define ET_CORE 4 |
| 89 | #define ET_LOOS 0xFE00 |
| 90 | #define ET_HIOS 0xFEFF |
| 91 | #define ET_LOPROC 0xFF00 |
| 92 | #define ET_HIPROC 0xFFFF |
| 93 | |
| 94 | #define EM_NONE 0 // No machine |
| 95 | #define EM_M32 1 // AT&T WE 32100 |
| 96 | #define EM_SPARC 2 // SUN SPARC |
| 97 | #define EM_386 3 // Intel 80386 |
| 98 | #define EM_68K 4 // Motorola m68k family |
| 99 | #define EM_88K 5 // Motorola m88k family |
| 100 | #define EM_486 6 // Intel 80486// Reserved for future use |
| 101 | #define EM_860 7 // Intel 80860 |
| 102 | #define EM_MIPS 8 // MIPS R3000 (officially, big-endian only) |
| 103 | #define EM_S370 9 // IBM System/370 |
| 104 | #define EM_MIPS_RS3_LE \ |
| 105 | 10 // MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated |
| 106 | #define EM_res011 11 // Reserved |
| 107 | #define EM_res012 12 // Reserved |
| 108 | #define EM_res013 13 // Reserved |
| 109 | #define EM_res014 14 // Reserved |
| 110 | #define EM_PARISC 15 // HPPA |
| 111 | #define EM_res016 16 // Reserved |
| 112 | #define EM_VPP550 17 // Fujitsu VPP500 |
| 113 | #define EM_SPARC32PLUS 18 // Sun's "v8plus" |
| 114 | #define EM_960 19 // Intel 80960 |
| 115 | #define EM_PPC 20 // PowerPC |
| 116 | #define EM_PPC64 21 // 64-bit PowerPC |
| 117 | #define EM_S390 22 // IBM S/390 |
| 118 | #define EM_SPU 23 // Sony/Toshiba/IBM SPU |
| 119 | #define EM_res024 24 // Reserved |
| 120 | #define EM_res025 25 // Reserved |
| 121 | #define EM_res026 26 // Reserved |
| 122 | #define EM_res027 27 // Reserved |
| 123 | #define EM_res028 28 // Reserved |
| 124 | #define EM_res029 29 // Reserved |
| 125 | #define EM_res030 30 // Reserved |
| 126 | #define EM_res031 31 // Reserved |
| 127 | #define EM_res032 32 // Reserved |
| 128 | #define EM_res033 33 // Reserved |
| 129 | #define EM_res034 34 // Reserved |
| 130 | #define EM_res035 35 // Reserved |
| 131 | #define EM_V800 36 // NEC V800 series |
| 132 | #define EM_FR20 37 // Fujitsu FR20 |
| 133 | #define EM_RH32 38 // TRW RH32 |
| 134 | #define EM_MCORE 39 // Motorola M*Core // May also be taken by Fujitsu MMA |
| 135 | #define EM_RCE 39 // Old name for MCore |
| 136 | #define EM_ARM 40 // ARM |
| 137 | #define EM_OLD_ALPHA 41 // Digital Alpha |
| 138 | #define EM_SH 42 // Renesas (formerly Hitachi) / SuperH SH |
| 139 | #define EM_SPARCV9 43 // SPARC v9 64-bit |
| 140 | #define EM_TRICORE 44 // Siemens Tricore embedded processor |
| 141 | #define EM_ARC 45 // ARC Cores |
| 142 | #define EM_H8_300 46 // Renesas (formerly Hitachi) H8/300 |
| 143 | #define EM_H8_300H 47 // Renesas (formerly Hitachi) H8/300H |
| 144 | #define EM_H8S 48 // Renesas (formerly Hitachi) H8S |
| 145 | #define EM_H8_500 49 // Renesas (formerly Hitachi) H8/500 |
| 146 | #define EM_IA_64 50 // Intel IA-64 Processor |
| 147 | #define EM_MIPS_X 51 // Stanford MIPS-X |
| 148 | #define EM_COLDFIRE 52 // Motorola Coldfire |
| 149 | #define EM_68HC12 53 // Motorola M68HC12 |
| 150 | #define EM_MMA 54 // Fujitsu Multimedia Accelerator |
| 151 | #define EM_PCP 55 // Siemens PCP |
| 152 | #define EM_NCPU 56 // Sony nCPU embedded RISC processor |
| 153 | #define EM_NDR1 57 // Denso NDR1 microprocesspr |
| 154 | #define EM_STARCORE 58 // Motorola Star*Core processor |
| 155 | #define EM_ME16 59 // Toyota ME16 processor |
| 156 | #define EM_ST100 60 // STMicroelectronics ST100 processor |
| 157 | #define EM_TINYJ 61 // Advanced Logic Corp. TinyJ embedded processor |
| 158 | #define EM_X86_64 62 // Advanced Micro Devices X86-64 processor |
| 159 | #define EM_PDSP 63 // Sony DSP Processor |
| 160 | #define EM_PDP10 64 // Digital Equipment Corp. PDP-10 |
| 161 | #define EM_PDP11 65 // Digital Equipment Corp. PDP-11 |
| 162 | #define EM_FX66 66 // Siemens FX66 microcontroller |
| 163 | #define EM_ST9PLUS 67 // STMicroelectronics ST9+ 8/16 bit microcontroller |
| 164 | #define EM_ST7 68 // STMicroelectronics ST7 8-bit microcontroller |
| 165 | #define EM_68HC16 69 // Motorola MC68HC16 Microcontroller |
| 166 | #define EM_68HC11 70 // Motorola MC68HC11 Microcontroller |
| 167 | #define EM_68HC08 71 // Motorola MC68HC08 Microcontroller |
| 168 | #define EM_68HC05 72 // Motorola MC68HC05 Microcontroller |
| 169 | #define EM_SVX 73 // Silicon Graphics SVx |
| 170 | #define EM_ST19 74 // STMicroelectronics ST19 8-bit cpu |
| 171 | #define EM_VAX 75 // Digital VAX |
| 172 | #define EM_CRIS 76 // Axis Communications 32-bit embedded processor |
| 173 | #define EM_JAVELIN 77 // Infineon Technologies 32-bit embedded cpu |
| 174 | #define EM_FIREPATH 78 // Element 14 64-bit DSP processor |
| 175 | #define EM_ZSP 79 // LSI Logic's 16-bit DSP processor |
| 176 | #define EM_MMIX 80 // Donald Knuth's educational 64-bit processor |
| 177 | #define EM_HUANY 81 // Harvard's machine-independent format |
| 178 | #define EM_PRISM 82 // SiTera Prism |
| 179 | #define EM_AVR 83 // Atmel AVR 8-bit microcontroller |
| 180 | #define EM_FR30 84 // Fujitsu FR30 |
| 181 | #define EM_D10V 85 // Mitsubishi D10V |
| 182 | #define EM_D30V 86 // Mitsubishi D30V |
| 183 | #define EM_V850 87 // NEC v850 |
| 184 | #define EM_M32R 88 // Renesas M32R (formerly Mitsubishi M32R) |
| 185 | #define EM_MN10300 89 // Matsushita MN10300 |
| 186 | #define EM_MN10200 90 // Matsushita MN10200 |
| 187 | #define EM_PJ 91 // picoJava |
| 188 | #define EM_OPENRISC 92 // OpenRISC 32-bit embedded processor |
| 189 | #define EM_ARC_A5 93 // ARC Cores Tangent-A5 |
| 190 | #define EM_XTENSA 94 // Tensilica Xtensa Architecture |
| 191 | #define EM_VIDEOCORE 95 // Alphamosaic VideoCore processor |
| 192 | #define EM_TMM_GPP 96 // Thompson Multimedia General Purpose Processor |
| 193 | #define EM_NS32K 97 // National Semiconductor 32000 series |
| 194 | #define EM_TPC 98 // Tenor Network TPC processor |
| 195 | #define EM_SNP1K 99 // Trebia SNP 1000 processor |
| 196 | #define EM_ST200 100 // STMicroelectronics ST200 microcontroller |
| 197 | #define EM_IP2K 101 // Ubicom IP2022 micro controller |
| 198 | #define EM_MAX 102 // MAX Processor |
| 199 | #define EM_CR 103 // National Semiconductor CompactRISC |
| 200 | #define EM_F2MC16 104 // Fujitsu F2MC16 |
| 201 | #define EM_MSP430 105 // TI msp430 micro controller |
| 202 | #define EM_BLACKFIN 106 // ADI Blackfin |
| 203 | #define EM_SE_C33 107 // S1C33 Family of Seiko Epson processors |
| 204 | #define EM_SEP 108 // Sharp embedded microprocessor |
| 205 | #define EM_ARCA 109 // Arca RISC Microprocessor |
| 206 | #define EM_UNICORE \ |
| 207 | 110 // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University |
| 208 | #define EM_EXCESS 111 // eXcess: 16/32/64-bit configurable embedded CPU |
| 209 | #define EM_DXP 112 // Icera Semiconductor Inc. Deep Execution Processor |
| 210 | #define EM_ALTERA_NIOS2 113 // Altera Nios II soft-core processor |
| 211 | #define EM_CRX 114 // National Semiconductor CRX |
| 212 | #define EM_XGATE 115 // Motorola XGATE embedded processor |
| 213 | #define EM_C166 116 // Infineon C16x/XC16x processor |
| 214 | #define EM_M16C 117 // Renesas M16C series microprocessors |
| 215 | #define EM_DSPIC30F \ |
| 216 | 118 // Microchip Technology dsPIC30F Digital Signal Controller |
| 217 | #define EM_CE 119 // Freescale Communication Engine RISC core |
| 218 | #define EM_M32C 120 // Renesas M32C series microprocessors |
| 219 | #define EM_res121 121 // Reserved |
| 220 | #define EM_res122 122 // Reserved |
| 221 | #define EM_res123 123 // Reserved |
| 222 | #define EM_res124 124 // Reserved |
| 223 | #define EM_res125 125 // Reserved |
| 224 | #define EM_res126 126 // Reserved |
| 225 | #define EM_res127 127 // Reserved |
| 226 | #define EM_res128 128 // Reserved |
| 227 | #define EM_res129 129 // Reserved |
| 228 | #define EM_res130 130 // Reserved |
| 229 | #define EM_TSK3000 131 // Altium TSK3000 core |
| 230 | #define EM_RS08 132 // Freescale RS08 embedded processor |
| 231 | #define EM_res133 133 // Reserved |
| 232 | #define EM_ECOG2 134 // Cyan Technology eCOG2 microprocessor |
| 233 | #define EM_SCORE 135 // Sunplus Score |
| 234 | #define EM_SCORE7 135 // Sunplus S+core7 RISC processor |
| 235 | #define EM_DSP24 136 // New Japan Radio (NJR) 24-bit DSP Processor |
| 236 | #define EM_VIDEOCORE3 137 // Broadcom VideoCore III processor |
| 237 | #define EM_LATTICEMICO32 138 // RISC processor for Lattice FPGA architecture |
| 238 | #define EM_SE_C17 139 // Seiko Epson C17 family |
| 239 | #define EM_TI_C6000 140 // Texas Instruments TMS320C6000 DSP family |
| 240 | #define EM_TI_C2000 141 // Texas Instruments TMS320C2000 DSP family |
| 241 | #define EM_TI_C5500 142 // Texas Instruments TMS320C55x DSP family |
| 242 | #define EM_res143 143 // Reserved |
| 243 | #define EM_res144 144 // Reserved |
| 244 | #define EM_res145 145 // Reserved |
| 245 | #define EM_res146 146 // Reserved |
| 246 | #define EM_res147 147 // Reserved |
| 247 | #define EM_res148 148 // Reserved |
| 248 | #define EM_res149 149 // Reserved |
| 249 | #define EM_res150 150 // Reserved |
| 250 | #define EM_res151 151 // Reserved |
| 251 | #define EM_res152 152 // Reserved |
| 252 | #define EM_res153 153 // Reserved |
| 253 | #define EM_res154 154 // Reserved |
| 254 | #define EM_res155 155 // Reserved |
| 255 | #define EM_res156 156 // Reserved |
| 256 | #define EM_res157 157 // Reserved |
| 257 | #define EM_res158 158 // Reserved |
| 258 | #define EM_res159 159 // Reserved |
| 259 | #define EM_MMDSP_PLUS 160 // STMicroelectronics 64bit VLIW Data Signal Processor |
| 260 | #define EM_CYPRESS_M8C 161 // Cypress M8C microprocessor |
| 261 | #define EM_R32C 162 // Renesas R32C series microprocessors |
| 262 | #define EM_TRIMEDIA 163 // NXP Semiconductors TriMedia architecture family |
| 263 | #define EM_QDSP6 164 // QUALCOMM DSP6 Processor |
| 264 | #define EM_8051 165 // Intel 8051 and variants |
| 265 | #define EM_STXP7X 166 // STMicroelectronics STxP7x family |
| 266 | #define EM_NDS32 \ |
| 267 | 167 // Andes Technology compact code size embedded RISC processor family |
| 268 | #define EM_ECOG1 168 // Cyan Technology eCOG1X family |
| 269 | #define EM_ECOG1X 168 // Cyan Technology eCOG1X family |
| 270 | #define EM_MAXQ30 169 // Dallas Semiconductor MAXQ30 Core Micro-controllers |
| 271 | #define EM_XIMO16 170 // New Japan Radio (NJR) 16-bit DSP Processor |
| 272 | #define EM_MANIK 171 // M2000 Reconfigurable RISC Microprocessor |
| 273 | #define EM_CRAYNV2 172 // Cray Inc. NV2 vector architecture |
| 274 | #define EM_RX 173 // Renesas RX family |
| 275 | #define EM_METAG 174 // Imagination Technologies META processor architecture |
| 276 | #define EM_MCST_ELBRUS 175 // MCST Elbrus general purpose hardware architecture |
| 277 | #define EM_ECOG16 176 // Cyan Technology eCOG16 family |
| 278 | #define EM_CR16 177 // National Semiconductor CompactRISC 16-bit processor |
| 279 | #define EM_ETPU 178 // Freescale Extended Time Processing Unit |
| 280 | #define EM_SLE9X 179 // Infineon Technologies SLE9X core |
| 281 | #define EM_L1OM 180 // Intel L1OM |
| 282 | #define EM_INTEL181 181 // Reserved by Intel |
| 283 | #define EM_INTEL182 182 // Reserved by Intel |
| 284 | #define EM_res183 183 // Reserved by ARM |
| 285 | #define EM_res184 184 // Reserved by ARM |
| 286 | #define EM_AVR32 185 // Atmel Corporation 32-bit microprocessor family |
| 287 | #define EM_STM8 186 // STMicroeletronics STM8 8-bit microcontroller |
| 288 | #define EM_TILE64 187 // Tilera TILE64 multicore architecture family |
| 289 | #define EM_TILEPRO 188 // Tilera TILEPro multicore architecture family |
| 290 | #define EM_MICROBLAZE 189 // Xilinx MicroBlaze 32-bit RISC soft processor core |
| 291 | #define EM_CUDA 190 // NVIDIA CUDA architecture |
| 292 | #define EM_TILEGX 191 // Tilera TILE-Gx multicore architecture family |
| 293 | #define EM_CLOUDSHIELD 192 // CloudShield architecture family |
| 294 | #define EM_COREA_1ST 193 // KIPO-KAIST Core-A 1st generation processor family |
| 295 | #define EM_COREA_2ND 194 // KIPO-KAIST Core-A 2nd generation processor family |
| 296 | #define EM_ARC_COMPACT2 195 // Synopsys ARCompact V2 |
| 297 | #define EM_OPEN8 196 // Open8 8-bit RISC soft processor core |
| 298 | #define EM_RL78 197 // Renesas RL78 family |
| 299 | #define EM_VIDEOCORE5 198 // Broadcom VideoCore V processor |
| 300 | #define EM_78KOR 199 // Renesas 78KOR family |
| 301 | #define EM_56800EX 200 // Freescale 56800EX Digital Signal Controller (DSC) |
| 302 | #define EM_BA1 201 // Beyond BA1 CPU architecture |
| 303 | #define EM_BA2 202 // Beyond BA2 CPU architecture |
| 304 | #define EM_XCORE 203 // XMOS xCORE processor family |
| 305 | #define EM_MCHP_PIC 204 // Microchip 8-bit PIC(r) family |
| 306 | #define EM_INTEL205 205 // Reserved by Intel |
| 307 | #define EM_INTEL206 206 // Reserved by Intel |
| 308 | #define EM_INTEL207 207 // Reserved by Intel |
| 309 | #define EM_INTEL208 208 // Reserved by Intel |
| 310 | #define EM_INTEL209 209 // Reserved by Intel |
| 311 | #define EM_KM32 210 // KM211 KM32 32-bit processor |
| 312 | #define EM_KMX32 211 // KM211 KMX32 32-bit processor |
| 313 | #define EM_KMX16 212 // KM211 KMX16 16-bit processor |
| 314 | #define EM_KMX8 213 // KM211 KMX8 8-bit processor |
| 315 | #define EM_KVARC 214 // KM211 KVARC processor |
| 316 | #define EM_CDP 215 // Paneve CDP architecture family |
| 317 | #define EM_COGE 216 // Cognitive Smart Memory Processor |
| 318 | #define EM_COOL 217 // iCelero CoolEngine |
| 319 | #define EM_NORC 218 // Nanoradio Optimized RISC |
| 320 | #define EM_CSR_KALIMBA 219 // CSR Kalimba architecture family |
| 321 | #define EM_Z80 220 // Zilog Z80 |
| 322 | #define EM_VISIUM 221 // Controls and Data Services VISIUMcore processor |
| 323 | #define EM_FT32 222 // FTDI Chip FT32 high performance 32-bit RISC architecture |
| 324 | #define EM_MOXIE 223 // Moxie processor family |
| 325 | #define EM_AMDGPU 224 // AMD GPU architecture |
| 326 | #define EM_RISCV 243 // RISC-V |
| 327 | #define EM_LANAI 244 // Lanai processor |
| 328 | #define EM_CEVA 245 // CEVA Processor Architecture Family |
| 329 | #define EM_CEVA_X2 246 // CEVA X2 Processor Family |
| 330 | #define EM_BPF 247 // Linux BPF – in-kernel virtual machine |
| 331 | #define EM_GRAPHCORE_IPU 248 // Graphcore Intelligent Processing Unit |
| 332 | #define EM_IMG1 249 // Imagination Technologies |
| 333 | #define EM_NFP 250 // Netronome Flow Processor (P) |
| 334 | #define EM_CSKY 252 // C-SKY processor family |
| 335 | |
| 336 | // File version |
| 337 | #define EV_NONE 0 |
| 338 | #define EV_CURRENT 1 |
| 339 | |
| 340 | // Identification index |
| 341 | #define EI_MAG0 0 |
| 342 | #define EI_MAG1 1 |
| 343 | #define EI_MAG2 2 |
| 344 | #define EI_MAG3 3 |
| 345 | #define EI_CLASS 4 |
| 346 | #define EI_DATA 5 |
| 347 | #define EI_VERSION 6 |
| 348 | #define EI_OSABI 7 |
| 349 | #define EI_ABIVERSION 8 |
| 350 | #define EI_PAD 9 |
| 351 | #define EI_NIDENT 16 |
| 352 | |
| 353 | // Magic number |
| 354 | #define ELFMAG0 0x7F |
| 355 | #define ELFMAG1 'E' |
| 356 | #define ELFMAG2 'L' |
| 357 | #define ELFMAG3 'F' |
| 358 | |
| 359 | // File class |
| 360 | #define ELFCLASSNONE 0 |
| 361 | #define ELFCLASS32 1 |
| 362 | #define ELFCLASS64 2 |
| 363 | |
| 364 | // Encoding |
| 365 | #define ELFDATANONE 0 |
| 366 | #define ELFDATA2LSB 1 |
| 367 | #define ELFDATA2MSB 2 |
| 368 | |
| 369 | // OS extensions |
| 370 | #define ELFOSABI_NONE 0 // No extensions or unspecified |
| 371 | #define ELFOSABI_HPUX 1 // Hewlett-Packard HP-UX |
| 372 | #define ELFOSABI_NETBSD 2 // NetBSD |
| 373 | #define ELFOSABI_LINUX 3 // Linux |
| 374 | #define ELFOSABI_SOLARIS 6 // Sun Solaris |
| 375 | #define ELFOSABI_AIX 7 // AIX |
| 376 | #define ELFOSABI_IRIX 8 // IRIX |
| 377 | #define ELFOSABI_FREEBSD 9 // FreeBSD |
| 378 | #define ELFOSABI_TRU64 10 // Compaq TRU64 UNIX |
| 379 | #define ELFOSABI_MODESTO 11 // Novell Modesto |
| 380 | #define ELFOSABI_OPENBSD 12 // Open BSD |
| 381 | #define ELFOSABI_OPENVMS 13 // Open VMS |
| 382 | #define ELFOSABI_NSK 14 // Hewlett-Packard Non-Stop Kernel |
| 383 | #define ELFOSABI_AROS 15 // Amiga Research OS |
| 384 | #define ELFOSABI_FENIXOS 16 // The FenixOS highly scalable multi-core OS |
| 385 | // 64-255 Architecture-specific value range |
| 386 | #define ELFOSABI_AMDGPU_HSA \ |
| 387 | 64 // AMDGPU OS for HSA compatible compute // kernels. |
| 388 | #define ELFOSABI_AMDGPU_PAL \ |
| 389 | 65 // AMDGPU OS for AMD PAL compatible graphics // shaders and compute kernels. |
| 390 | #define ELFOSABI_AMDGPU_MESA3D \ |
| 391 | 66 // AMDGPU OS for Mesa3D compatible graphics // shaders and compute kernels. |
| 392 | |
| 393 | // AMDGPU specific e_flags |
| 394 | #define EF_AMDGPU_MACH 0x0ff // AMDGPU processor selection mask. |
| 395 | #define EF_AMDGPU_XNACK \ |
| 396 | 0x100 // Indicates if the XNACK target feature is // enabled for all code contained in the ELF. |
| 397 | // AMDGPU processors |
| 398 | #define EF_AMDGPU_MACH_NONE 0x000 // Unspecified processor. |
| 399 | #define EF_AMDGPU_MACH_R600_R600 0x001 |
| 400 | #define EF_AMDGPU_MACH_R600_R630 0x002 |
| 401 | #define EF_AMDGPU_MACH_R600_RS880 0x003 |
| 402 | #define EF_AMDGPU_MACH_R600_RV670 0x004 |
| 403 | #define EF_AMDGPU_MACH_R600_RV710 0x005 |
| 404 | #define EF_AMDGPU_MACH_R600_RV730 0x006 |
| 405 | #define EF_AMDGPU_MACH_R600_RV770 0x007 |
| 406 | #define EF_AMDGPU_MACH_R600_CEDAR 0x008 |
| 407 | #define EF_AMDGPU_MACH_R600_CYPRESS 0x009 |
| 408 | #define EF_AMDGPU_MACH_R600_JUNIPER 0x00a |
| 409 | #define EF_AMDGPU_MACH_R600_REDWOOD 0x00b |
| 410 | #define EF_AMDGPU_MACH_R600_SUMO 0x00c |
| 411 | #define EF_AMDGPU_MACH_R600_BARTS 0x00d |
| 412 | #define EF_AMDGPU_MACH_R600_CAICOS 0x00e |
| 413 | #define EF_AMDGPU_MACH_R600_CAYMAN 0x00f |
| 414 | #define EF_AMDGPU_MACH_R600_TURKS 0x010 |
| 415 | #define EF_AMDGPU_MACH_R600_RESERVED_FIRST 0x011 |
| 416 | #define EF_AMDGPU_MACH_R600_RESERVED_LAST 0x01f |
| 417 | #define EF_AMDGPU_MACH_R600_FIRST EF_AMDGPU_MACH_R600_R600 |
| 418 | #define EF_AMDGPU_MACH_R600_LAST EF_AMDGPU_MACH_R600_TURKS |
| 419 | #define EF_AMDGPU_MACH_AMDGCN_GFX600 0x020 |
| 420 | #define EF_AMDGPU_MACH_AMDGCN_GFX601 0x021 |
| 421 | #define EF_AMDGPU_MACH_AMDGCN_GFX700 0x022 |
| 422 | #define EF_AMDGPU_MACH_AMDGCN_GFX701 0x023 |
| 423 | #define EF_AMDGPU_MACH_AMDGCN_GFX702 0x024 |
| 424 | #define EF_AMDGPU_MACH_AMDGCN_GFX703 0x025 |
| 425 | #define EF_AMDGPU_MACH_AMDGCN_GFX704 0x026 |
| 426 | #define EF_AMDGPU_MACH_AMDGCN_GFX801 0x028 |
| 427 | #define EF_AMDGPU_MACH_AMDGCN_GFX802 0x029 |
| 428 | #define EF_AMDGPU_MACH_AMDGCN_GFX803 0x02a |
| 429 | #define EF_AMDGPU_MACH_AMDGCN_GFX810 0x02b |
| 430 | #define EF_AMDGPU_MACH_AMDGCN_GFX900 0x02c |
| 431 | #define EF_AMDGPU_MACH_AMDGCN_GFX902 0x02d |
| 432 | #define EF_AMDGPU_MACH_AMDGCN_GFX904 0x02e |
| 433 | #define EF_AMDGPU_MACH_AMDGCN_GFX906 0x02f |
| 434 | #define EF_AMDGPU_MACH_AMDGCN_RESERVED0 0x027 |
| 435 | #define EF_AMDGPU_MACH_AMDGCN_RESERVED1 0x030 |
| 436 | #define EF_AMDGPU_MACH_AMDGCN_FIRST EF_AMDGPU_MACH_AMDGCN_GFX600 |
| 437 | #define EF_AMDGPU_MACH_AMDGCN_LAST EF_AMDGPU_MACH_AMDGCN_GFX906 |
| 438 | |
| 439 | ///////////////////// |
| 440 | // Sections constants |
| 441 | |
| 442 | // Section indexes |
| 443 | #define SHN_UNDEF 0 |
| 444 | #define SHN_LORESERVE 0xFF00 |
| 445 | #define SHN_LOPROC 0xFF00 |
| 446 | #define SHN_HIPROC 0xFF1F |
| 447 | #define SHN_LOOS 0xFF20 |
| 448 | #define SHN_HIOS 0xFF3F |
| 449 | #define SHN_ABS 0xFFF1 |
| 450 | #define SHN_COMMON 0xFFF2 |
| 451 | #define SHN_XINDEX 0xFFFF |
| 452 | #define SHN_HIRESERVE 0xFFFF |
| 453 | |
| 454 | // Section types |
| 455 | #define SHT_NULL 0 |
| 456 | #define SHT_PROGBITS 1 |
| 457 | #define SHT_SYMTAB 2 |
| 458 | #define SHT_STRTAB 3 |
| 459 | #define SHT_RELA 4 |
| 460 | #define SHT_HASH 5 |
| 461 | #define SHT_DYNAMIC 6 |
| 462 | #define SHT_NOTE 7 |
| 463 | #define SHT_NOBITS 8 |
| 464 | #define SHT_REL 9 |
| 465 | #define SHT_SHLIB 10 |
| 466 | #define SHT_DYNSYM 11 |
| 467 | #define SHT_INIT_ARRAY 14 |
| 468 | #define SHT_FINI_ARRAY 15 |
| 469 | #define SHT_PREINIT_ARRAY 16 |
| 470 | #define SHT_GROUP 17 |
| 471 | #define SHT_SYMTAB_SHNDX 18 |
| 472 | #define SHT_LOOS 0x60000000 |
| 473 | #define SHT_HIOS 0x6fffffff |
| 474 | #define SHT_LOPROC 0x70000000 |
| 475 | #define SHT_HIPROC 0x7FFFFFFF |
| 476 | #define SHT_LOUSER 0x80000000 |
| 477 | #define SHT_HIUSER 0xFFFFFFFF |
| 478 | |
| 479 | // Section attribute flags |
| 480 | #define SHF_WRITE 0x1 |
| 481 | #define SHF_ALLOC 0x2 |
| 482 | #define SHF_EXECINSTR 0x4 |
| 483 | #define SHF_MERGE 0x10 |
| 484 | #define SHF_STRINGS 0x20 |
| 485 | #define SHF_INFO_LINK 0x40 |
| 486 | #define SHF_LINK_ORDER 0x80 |
| 487 | #define SHF_OS_NONCONFORMING 0x100 |
| 488 | #define SHF_GROUP 0x200 |
| 489 | #define SHF_TLS 0x400 |
| 490 | #define SHF_MASKOS 0x0ff00000 |
| 491 | #define SHF_MASKPROC 0xF0000000 |
| 492 | |
| 493 | // Section group flags |
| 494 | #define GRP_COMDAT 0x1 |
| 495 | #define GRP_MASKOS 0x0ff00000 |
| 496 | #define GRP_MASKPROC 0xf0000000 |
| 497 | |
| 498 | // Symbol binding |
| 499 | #define STB_LOCAL 0 |
| 500 | #define STB_GLOBAL 1 |
| 501 | #define STB_WEAK 2 |
| 502 | #define STB_LOOS 10 |
| 503 | #define STB_HIOS 12 |
| 504 | #define STB_MULTIDEF 13 |
| 505 | #define STB_LOPROC 13 |
| 506 | #define STB_HIPROC 15 |
| 507 | |
| 508 | // Note types |
| 509 | #define NT_AMDGPU_METADATA 1 |
| 510 | #define NT_AMD_AMDGPU_HSA_METADATA 10 |
| 511 | #define NT_AMD_AMDGPU_ISA 11 |
| 512 | #define NT_AMD_AMDGPU_PAL_METADATA 12 |
| 513 | |
| 514 | // Symbol types |
| 515 | #define STT_NOTYPE 0 |
| 516 | #define STT_OBJECT 1 |
| 517 | #define STT_FUNC 2 |
| 518 | #define STT_SECTION 3 |
| 519 | #define STT_FILE 4 |
| 520 | #define STT_COMMON 5 |
| 521 | #define STT_TLS 6 |
| 522 | #define STT_LOOS 10 |
| 523 | #define STT_AMDGPU_HSA_KERNEL 10 |
| 524 | #define STT_HIOS 12 |
| 525 | #define STT_LOPROC 13 |
| 526 | #define STT_HIPROC 15 |
| 527 | |
| 528 | // Symbol visibility |
| 529 | #define STV_DEFAULT 0 |
| 530 | #define STV_INTERNAL 1 |
| 531 | #define STV_HIDDEN 2 |
| 532 | #define STV_PROTECTED 3 |
| 533 | |
| 534 | // Undefined name |
| 535 | #define STN_UNDEF 0 |
| 536 | |
| 537 | // Relocation types |
| 538 | #define R_386_NONE 0 |
| 539 | #define R_X86_64_NONE 0 |
| 540 | #define R_AMDGPU_NONE 0 |
| 541 | #define R_386_32 1 |
| 542 | #define R_X86_64_64 1 |
| 543 | #define R_AMDGPU_ABS32_LO 1 |
| 544 | #define R_386_PC32 2 |
| 545 | #define R_X86_64_PC32 2 |
| 546 | #define R_AMDGPU_ABS32_HI 2 |
| 547 | #define R_386_GOT32 3 |
| 548 | #define R_X86_64_GOT32 3 |
| 549 | #define R_AMDGPU_ABS64 3 |
| 550 | #define R_386_PLT32 4 |
| 551 | #define R_X86_64_PLT32 4 |
| 552 | #define R_AMDGPU_REL32 4 |
| 553 | #define R_386_COPY 5 |
| 554 | #define R_X86_64_COPY 5 |
| 555 | #define R_AMDGPU_REL64 5 |
| 556 | #define R_386_GLOB_DAT 6 |
| 557 | #define R_X86_64_GLOB_DAT 6 |
| 558 | #define R_AMDGPU_ABS32 6 |
| 559 | #define R_386_JMP_SLOT 7 |
| 560 | #define R_X86_64_JUMP_SLOT 7 |
| 561 | #define R_AMDGPU_GOTPCREL 7 |
| 562 | #define R_386_RELATIVE 8 |
| 563 | #define R_X86_64_RELATIVE 8 |
| 564 | #define R_AMDGPU_GOTPCREL32_LO 8 |
| 565 | #define R_386_GOTOFF 9 |
| 566 | #define R_X86_64_GOTPCREL 9 |
| 567 | #define R_AMDGPU_GOTPCREL32_HI 9 |
| 568 | #define R_386_GOTPC 10 |
| 569 | #define R_X86_64_32 10 |
| 570 | #define R_AMDGPU_REL32_LO 10 |
| 571 | #define R_386_32PLT 11 |
| 572 | #define R_X86_64_32S 11 |
| 573 | #define R_AMDGPU_REL32_HI 11 |
| 574 | #define R_X86_64_16 12 |
| 575 | #define R_X86_64_PC16 13 |
| 576 | #define R_AMDGPU_RELATIVE64 13 |
| 577 | #define R_386_TLS_TPOFF 14 |
| 578 | #define R_X86_64_8 14 |
| 579 | #define R_386_TLS_IE 15 |
| 580 | #define R_X86_64_PC8 15 |
| 581 | #define R_386_TLS_GOTIE 16 |
| 582 | #define R_X86_64_DTPMOD64 16 |
| 583 | #define R_386_TLS_LE 17 |
| 584 | #define R_X86_64_DTPOFF64 17 |
| 585 | #define R_386_TLS_GD 18 |
| 586 | #define R_X86_64_TPOFF64 18 |
| 587 | #define R_386_TLS_LDM 19 |
| 588 | #define R_X86_64_TLSGD 19 |
| 589 | #define R_386_16 20 |
| 590 | #define R_X86_64_TLSLD 20 |
| 591 | #define R_386_PC16 21 |
| 592 | #define R_X86_64_DTPOFF32 21 |
| 593 | #define R_386_8 22 |
| 594 | #define R_X86_64_GOTTPOFF 22 |
| 595 | #define R_386_PC8 23 |
| 596 | #define R_X86_64_TPOFF32 23 |
| 597 | #define R_386_TLS_GD_32 24 |
| 598 | #define R_X86_64_PC64 24 |
| 599 | #define R_386_TLS_GD_PUSH 25 |
| 600 | #define R_X86_64_GOTOFF64 25 |
| 601 | #define R_386_TLS_GD_CALL 26 |
| 602 | #define R_X86_64_GOTPC32 26 |
| 603 | #define R_386_TLS_GD_POP 27 |
| 604 | #define R_X86_64_GOT64 27 |
| 605 | #define R_386_TLS_LDM_32 28 |
| 606 | #define R_X86_64_GOTPCREL64 28 |
| 607 | #define R_386_TLS_LDM_PUSH 29 |
| 608 | #define R_X86_64_GOTPC64 29 |
| 609 | #define R_386_TLS_LDM_CALL 30 |
| 610 | #define R_X86_64_GOTPLT64 30 |
| 611 | #define R_386_TLS_LDM_POP 31 |
| 612 | #define R_X86_64_PLTOFF64 31 |
| 613 | #define R_386_TLS_LDO_32 32 |
| 614 | #define R_386_TLS_IE_32 33 |
| 615 | #define R_386_TLS_LE_32 34 |
| 616 | #define R_X86_64_GOTPC32_TLSDESC 34 |
| 617 | #define R_386_TLS_DTPMOD32 35 |
| 618 | #define R_X86_64_TLSDESC_CALL 35 |
| 619 | #define R_386_TLS_DTPOFF32 36 |
| 620 | #define R_X86_64_TLSDESC 36 |
| 621 | #define R_386_TLS_TPOFF32 37 |
| 622 | #define R_X86_64_IRELATIVE 37 |
| 623 | #define R_386_SIZE32 38 |
| 624 | #define R_386_TLS_GOTDESC 39 |
| 625 | #define R_386_TLS_DESC_CALL 40 |
| 626 | #define R_386_TLS_DESC 41 |
| 627 | #define R_386_IRELATIVE 42 |
| 628 | #define R_386_GOT32X 43 |
| 629 | #define R_X86_64_GNU_VTINHERIT 250 |
| 630 | #define R_X86_64_GNU_VTENTRY 251 |
| 631 | |
| 632 | // Segment types |
| 633 | #define PT_NULL 0 |
| 634 | #define PT_LOAD 1 |
| 635 | #define PT_DYNAMIC 2 |
| 636 | #define PT_INTERP 3 |
| 637 | #define PT_NOTE 4 |
| 638 | #define PT_SHLIB 5 |
| 639 | #define PT_PHDR 6 |
| 640 | #define PT_TLS 7 |
| 641 | #define PT_LOOS 0x60000000 |
| 642 | #define PT_HIOS 0x6fffffff |
| 643 | #define PT_LOPROC 0x70000000 |
| 644 | #define PT_HIPROC 0x7FFFFFFF |
| 645 | |
| 646 | // Segment flags |
| 647 | #define PF_X 1 // Execute |
| 648 | #define PF_W 2 // Write |
| 649 | #define PF_R 4 // Read |
| 650 | #define PF_MASKOS 0x0ff00000 // Unspecified |
| 651 | #define PF_MASKPROC 0xf0000000 // Unspecified |
| 652 | |
| 653 | // Dynamic Array Tags |
| 654 | #define DT_NULL 0 |
| 655 | #define DT_NEEDED 1 |
| 656 | #define DT_PLTRELSZ 2 |
| 657 | #define DT_PLTGOT 3 |
| 658 | #define DT_HASH 4 |
| 659 | #define DT_STRTAB 5 |
| 660 | #define DT_SYMTAB 6 |
| 661 | #define DT_RELA 7 |
| 662 | #define DT_RELASZ 8 |
| 663 | #define DT_RELAENT 9 |
| 664 | #define DT_STRSZ 10 |
| 665 | #define DT_SYMENT 11 |
| 666 | #define DT_INIT 12 |
| 667 | #define DT_FINI 13 |
| 668 | #define DT_SONAME 14 |
| 669 | #define DT_RPATH 15 |
| 670 | #define DT_SYMBOLIC 16 |
| 671 | #define DT_REL 17 |
| 672 | #define DT_RELSZ 18 |
| 673 | #define DT_RELENT 19 |
| 674 | #define DT_PLTREL 20 |
| 675 | #define DT_DEBUG 21 |
| 676 | #define DT_TEXTREL 22 |
| 677 | #define DT_JMPREL 23 |
| 678 | #define DT_BIND_NOW 24 |
| 679 | #define DT_INIT_ARRAY 25 |
| 680 | #define DT_FINI_ARRAY 26 |
| 681 | #define DT_INIT_ARRAYSZ 27 |
| 682 | #define DT_FINI_ARRAYSZ 28 |
| 683 | #define DT_RUNPATH 29 |
| 684 | #define DT_FLAGS 30 |
| 685 | #define DT_ENCODING 32 |
| 686 | #define DT_PREINIT_ARRAY 32 |
| 687 | #define DT_PREINIT_ARRAYSZ 33 |
| 688 | #define DT_MAXPOSTAGS 34 |
| 689 | #define DT_LOOS 0x6000000D |
| 690 | #define DT_HIOS 0x6ffff000 |
| 691 | #define DT_LOPROC 0x70000000 |
| 692 | #define DT_HIPROC 0x7FFFFFFF |
| 693 | |
| 694 | // DT_FLAGS values |
| 695 | #define DF_ORIGIN 0x1 |
| 696 | #define DF_SYMBOLIC 0x2 |
| 697 | #define DF_TEXTREL 0x4 |
| 698 | #define DF_BIND_NOW 0x8 |
| 699 | #define DF_STATIC_TLS 0x10 |
| 700 | |
| 701 | // ELF file header |
| 702 | struct Elf32_Ehdr |
| 703 | { |
| 704 | unsigned char e_ident[EI_NIDENT]; |
| 705 | Elf_Half e_type; |
| 706 | Elf_Half e_machine; |
| 707 | Elf_Word e_version; |
| 708 | Elf32_Addr e_entry; |
| 709 | Elf32_Off e_phoff; |
| 710 | Elf32_Off e_shoff; |
| 711 | Elf_Word e_flags; |
| 712 | Elf_Half e_ehsize; |
| 713 | Elf_Half e_phentsize; |
| 714 | Elf_Half e_phnum; |
| 715 | Elf_Half e_shentsize; |
| 716 | Elf_Half e_shnum; |
| 717 | Elf_Half e_shstrndx; |
| 718 | }; |
| 719 | |
| 720 | struct Elf64_Ehdr |
| 721 | { |
| 722 | unsigned char e_ident[EI_NIDENT]; |
| 723 | Elf_Half e_type; |
| 724 | Elf_Half e_machine; |
| 725 | Elf_Word e_version; |
| 726 | Elf64_Addr e_entry; |
| 727 | Elf64_Off e_phoff; |
| 728 | Elf64_Off e_shoff; |
| 729 | Elf_Word e_flags; |
| 730 | Elf_Half e_ehsize; |
| 731 | Elf_Half e_phentsize; |
| 732 | Elf_Half e_phnum; |
| 733 | Elf_Half e_shentsize; |
| 734 | Elf_Half e_shnum; |
| 735 | Elf_Half e_shstrndx; |
| 736 | }; |
| 737 | |
| 738 | // Section header |
| 739 | struct Elf32_Shdr |
| 740 | { |
| 741 | Elf_Word sh_name; |
| 742 | Elf_Word sh_type; |
| 743 | Elf_Word sh_flags; |
| 744 | Elf32_Addr sh_addr; |
| 745 | Elf32_Off sh_offset; |
| 746 | Elf_Word sh_size; |
| 747 | Elf_Word sh_link; |
| 748 | Elf_Word sh_info; |
| 749 | Elf_Word sh_addralign; |
| 750 | Elf_Word sh_entsize; |
| 751 | }; |
| 752 | |
| 753 | struct Elf64_Shdr |
| 754 | { |
| 755 | Elf_Word sh_name; |
| 756 | Elf_Word sh_type; |
| 757 | Elf_Xword sh_flags; |
| 758 | Elf64_Addr sh_addr; |
| 759 | Elf64_Off sh_offset; |
| 760 | Elf_Xword sh_size; |
| 761 | Elf_Word sh_link; |
| 762 | Elf_Word sh_info; |
| 763 | Elf_Xword sh_addralign; |
| 764 | Elf_Xword sh_entsize; |
| 765 | }; |
| 766 | |
| 767 | // Segment header |
| 768 | struct Elf32_Phdr |
| 769 | { |
| 770 | Elf_Word p_type; |
| 771 | Elf32_Off p_offset; |
| 772 | Elf32_Addr p_vaddr; |
| 773 | Elf32_Addr p_paddr; |
| 774 | Elf_Word p_filesz; |
| 775 | Elf_Word p_memsz; |
| 776 | Elf_Word p_flags; |
| 777 | Elf_Word p_align; |
| 778 | }; |
| 779 | |
| 780 | struct Elf64_Phdr |
| 781 | { |
| 782 | Elf_Word p_type; |
| 783 | Elf_Word p_flags; |
| 784 | Elf64_Off p_offset; |
| 785 | Elf64_Addr p_vaddr; |
| 786 | Elf64_Addr p_paddr; |
| 787 | Elf_Xword p_filesz; |
| 788 | Elf_Xword p_memsz; |
| 789 | Elf_Xword p_align; |
| 790 | }; |
| 791 | |
| 792 | // Symbol table entry |
| 793 | struct Elf32_Sym |
| 794 | { |
| 795 | Elf_Word st_name; |
| 796 | Elf32_Addr st_value; |
| 797 | Elf_Word st_size; |
| 798 | unsigned char st_info; |
| 799 | unsigned char st_other; |
| 800 | Elf_Half st_shndx; |
| 801 | }; |
| 802 | |
| 803 | struct Elf64_Sym |
| 804 | { |
| 805 | Elf_Word st_name; |
| 806 | unsigned char st_info; |
| 807 | unsigned char st_other; |
| 808 | Elf_Half st_shndx; |
| 809 | Elf64_Addr st_value; |
| 810 | Elf_Xword st_size; |
| 811 | }; |
| 812 | |
| 813 | #define ELF_ST_BIND( i ) ( ( i ) >> 4 ) |
| 814 | #define ELF_ST_TYPE( i ) ( (i)&0xf ) |
| 815 | #define ELF_ST_INFO( b, t ) ( ( ( b ) << 4 ) + ( (t)&0xf ) ) |
| 816 | |
| 817 | #define ELF_ST_VISIBILITY( o ) ( (o)&0x3 ) |
| 818 | |
| 819 | // Relocation entries |
| 820 | struct Elf32_Rel |
| 821 | { |
| 822 | Elf32_Addr r_offset; |
| 823 | Elf_Word r_info; |
| 824 | }; |
| 825 | |
| 826 | struct Elf32_Rela |
| 827 | { |
| 828 | Elf32_Addr r_offset; |
| 829 | Elf_Word r_info; |
| 830 | Elf_Sword r_addend; |
| 831 | }; |
| 832 | |
| 833 | struct Elf64_Rel |
| 834 | { |
| 835 | Elf64_Addr r_offset; |
| 836 | Elf_Xword r_info; |
| 837 | }; |
| 838 | |
| 839 | struct Elf64_Rela |
| 840 | { |
| 841 | Elf64_Addr r_offset; |
| 842 | Elf_Xword r_info; |
| 843 | Elf_Sxword r_addend; |
| 844 | }; |
| 845 | |
| 846 | #define ELF32_R_SYM( i ) ( ( i ) >> 8 ) |
| 847 | #define ELF32_R_TYPE( i ) ( (unsigned char)( i ) ) |
| 848 | #define ELF32_R_INFO( s, t ) ( ( ( s ) << 8 ) + (unsigned char)( t ) ) |
| 849 | |
| 850 | #define ELF64_R_SYM( i ) ( ( i ) >> 32 ) |
| 851 | #define ELF64_R_TYPE( i ) ( (i)&0xffffffffL ) |
| 852 | #define ELF64_R_INFO( s, t ) \ |
| 853 | ( ( ( ( int64_t )( s ) ) << 32 ) + ( (t)&0xffffffffL ) ) |
| 854 | |
| 855 | // Dynamic structure |
| 856 | struct Elf32_Dyn |
| 857 | { |
| 858 | Elf_Sword d_tag; |
| 859 | union { |
| 860 | Elf_Word d_val; |
| 861 | Elf32_Addr d_ptr; |
| 862 | } d_un; |
| 863 | }; |
| 864 | |
| 865 | struct Elf64_Dyn |
| 866 | { |
| 867 | Elf_Sxword d_tag; |
| 868 | union { |
| 869 | Elf_Xword d_val; |
| 870 | Elf64_Addr d_ptr; |
| 871 | } d_un; |
| 872 | }; |
| 873 | |
| 874 | } // namespace ELFIO |
| 875 | |
| 876 | #endif // ELFTYPES_H |
| 877 | |
| 878 | /*** End of inlined file: elf_types.hpp ***/ |
| 879 | |
| 880 | |
| 881 | /*** Start of inlined file: elfio_version.hpp ***/ |
| 882 | #define ELFIO_VERSION "3.8" |
| 883 | |
| 884 | /*** End of inlined file: elfio_version.hpp ***/ |
| 885 | |
| 886 | |
| 887 | /*** Start of inlined file: elfio_utils.hpp ***/ |
| 888 | #ifndef ELFIO_UTILS_HPP |
| 889 | #define ELFIO_UTILS_HPP |
| 890 | |
| 891 | #define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \ |
| 892 | TYPE get_##NAME() const { return ( *convertor )( FIELD ); } |
| 893 | #define ELFIO_SET_ACCESS( TYPE, NAME, FIELD ) \ |
| 894 | void set_##NAME( TYPE value ) \ |
| 895 | { \ |
| 896 | FIELD = value; \ |
| 897 | FIELD = ( *convertor )( FIELD ); \ |
| 898 | } |
| 899 | #define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \ |
| 900 | TYPE get_##NAME() const { return ( *convertor )( FIELD ); } \ |
| 901 | void set_##NAME( TYPE value ) \ |
| 902 | { \ |
| 903 | FIELD = value; \ |
| 904 | FIELD = ( *convertor )( FIELD ); \ |
| 905 | } |
| 906 | |
| 907 | #define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) virtual TYPE get_##NAME() const = 0 |
| 908 | |
| 909 | #define ELFIO_SET_ACCESS_DECL( TYPE, NAME ) \ |
| 910 | virtual void set_##NAME( TYPE value ) = 0 |
| 911 | |
| 912 | #define ELFIO_GET_SET_ACCESS_DECL( TYPE, NAME ) \ |
| 913 | virtual TYPE get_##NAME() const = 0; \ |
| 914 | virtual void set_##NAME( TYPE value ) = 0 |
| 915 | |
| 916 | namespace ELFIO { |
| 917 | |
| 918 | //------------------------------------------------------------------------------ |
| 919 | class endianess_convertor |
| 920 | { |
| 921 | public: |
| 922 | //------------------------------------------------------------------------------ |
| 923 | endianess_convertor() { need_conversion = false; } |
| 924 | |
| 925 | //------------------------------------------------------------------------------ |
| 926 | void setup( unsigned char elf_file_encoding ) |
| 927 | { |
| 928 | need_conversion = ( elf_file_encoding != get_host_encoding() ); |
| 929 | } |
| 930 | |
| 931 | //------------------------------------------------------------------------------ |
| 932 | uint64_t operator()( uint64_t value ) const |
| 933 | { |
| 934 | if ( !need_conversion ) { |
| 935 | return value; |
| 936 | } |
| 937 | value = ( ( value & 0x00000000000000FFull ) << 56 ) | |
| 938 | ( ( value & 0x000000000000FF00ull ) << 40 ) | |
| 939 | ( ( value & 0x0000000000FF0000ull ) << 24 ) | |
| 940 | ( ( value & 0x00000000FF000000ull ) << 8 ) | |
| 941 | ( ( value & 0x000000FF00000000ull ) >> 8 ) | |
| 942 | ( ( value & 0x0000FF0000000000ull ) >> 24 ) | |
| 943 | ( ( value & 0x00FF000000000000ull ) >> 40 ) | |
| 944 | ( ( value & 0xFF00000000000000ull ) >> 56 ); |
| 945 | |
| 946 | return value; |
| 947 | } |
| 948 | |
| 949 | //------------------------------------------------------------------------------ |
| 950 | int64_t operator()( int64_t value ) const |
| 951 | { |
| 952 | if ( !need_conversion ) { |
| 953 | return value; |
| 954 | } |
| 955 | return ( int64_t )( *this )( (uint64_t)value ); |
| 956 | } |
| 957 | |
| 958 | //------------------------------------------------------------------------------ |
| 959 | uint32_t operator()( uint32_t value ) const |
| 960 | { |
| 961 | if ( !need_conversion ) { |
| 962 | return value; |
| 963 | } |
| 964 | value = |
| 965 | ( ( value & 0x000000FF ) << 24 ) | ( ( value & 0x0000FF00 ) << 8 ) | |
| 966 | ( ( value & 0x00FF0000 ) >> 8 ) | ( ( value & 0xFF000000 ) >> 24 ); |
| 967 | |
| 968 | return value; |
| 969 | } |
| 970 | |
| 971 | //------------------------------------------------------------------------------ |
| 972 | int32_t operator()( int32_t value ) const |
| 973 | { |
| 974 | if ( !need_conversion ) { |
| 975 | return value; |
| 976 | } |
| 977 | return ( int32_t )( *this )( (uint32_t)value ); |
| 978 | } |
| 979 | |
| 980 | //------------------------------------------------------------------------------ |
| 981 | uint16_t operator()( uint16_t value ) const |
| 982 | { |
| 983 | if ( !need_conversion ) { |
| 984 | return value; |
| 985 | } |
| 986 | value = ( ( value & 0x00FF ) << 8 ) | ( ( value & 0xFF00 ) >> 8 ); |
| 987 | |
| 988 | return value; |
| 989 | } |
| 990 | |
| 991 | //------------------------------------------------------------------------------ |
| 992 | int16_t operator()( int16_t value ) const |
| 993 | { |
| 994 | if ( !need_conversion ) { |
| 995 | return value; |
| 996 | } |
| 997 | return ( int16_t )( *this )( (uint16_t)value ); |
| 998 | } |
| 999 | |
| 1000 | //------------------------------------------------------------------------------ |
| 1001 | int8_t operator()( int8_t value ) const { return value; } |
| 1002 | |
| 1003 | //------------------------------------------------------------------------------ |
| 1004 | uint8_t operator()( uint8_t value ) const { return value; } |
| 1005 | |
| 1006 | //------------------------------------------------------------------------------ |
| 1007 | private: |
| 1008 | //------------------------------------------------------------------------------ |
| 1009 | unsigned char get_host_encoding() const |
| 1010 | { |
| 1011 | static const int tmp = 1; |
| 1012 | if ( 1 == *(const char*)&tmp ) { |
| 1013 | return ELFDATA2LSB; |
| 1014 | } |
| 1015 | else { |
| 1016 | return ELFDATA2MSB; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | //------------------------------------------------------------------------------ |
| 1021 | private: |
| 1022 | bool need_conversion; |
| 1023 | }; |
| 1024 | |
| 1025 | //------------------------------------------------------------------------------ |
| 1026 | inline uint32_t elf_hash( const unsigned char* name ) |
| 1027 | { |
| 1028 | uint32_t h = 0, g; |
| 1029 | while ( *name ) { |
| 1030 | h = ( h << 4 ) + *name++; |
| 1031 | g = h & 0xf0000000; |
| 1032 | if ( g != 0 ) |
| 1033 | h ^= g >> 24; |
| 1034 | h &= ~g; |
| 1035 | } |
| 1036 | return h; |
| 1037 | } |
| 1038 | |
| 1039 | } // namespace ELFIO |
| 1040 | |
| 1041 | #endif // ELFIO_UTILS_HPP |
| 1042 | |
| 1043 | /*** End of inlined file: elfio_utils.hpp ***/ |
| 1044 | |
| 1045 | |
| 1046 | /*** Start of inlined file: elfio_header.hpp ***/ |
| 1047 | #ifndef ELF_HEADER_HPP |
| 1048 | #define ELF_HEADER_HPP |
| 1049 | |
| 1050 | #include <iostream> |
| 1051 | |
| 1052 | namespace ELFIO { |
| 1053 | |
| 1054 | class elf_header |
| 1055 | { |
| 1056 | public: |
| 1057 | virtual ~elf_header(){}; |
| 1058 | virtual bool load( std::istream& stream ) = 0; |
| 1059 | virtual bool save( std::ostream& stream ) const = 0; |
| 1060 | |
| 1061 | // ELF header functions |
| 1062 | ELFIO_GET_ACCESS_DECL( unsigned char, class ); |
| 1063 | ELFIO_GET_ACCESS_DECL( unsigned char, elf_version ); |
| 1064 | ELFIO_GET_ACCESS_DECL( unsigned char, encoding ); |
| 1065 | ELFIO_GET_ACCESS_DECL( Elf_Half, header_size ); |
| 1066 | ELFIO_GET_ACCESS_DECL( Elf_Half, section_entry_size ); |
| 1067 | ELFIO_GET_ACCESS_DECL( Elf_Half, segment_entry_size ); |
| 1068 | |
| 1069 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, version ); |
| 1070 | ELFIO_GET_SET_ACCESS_DECL( unsigned char, os_abi ); |
| 1071 | ELFIO_GET_SET_ACCESS_DECL( unsigned char, abi_version ); |
| 1072 | ELFIO_GET_SET_ACCESS_DECL( Elf_Half, type ); |
| 1073 | ELFIO_GET_SET_ACCESS_DECL( Elf_Half, machine ); |
| 1074 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags ); |
| 1075 | ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, entry ); |
| 1076 | ELFIO_GET_SET_ACCESS_DECL( Elf_Half, sections_num ); |
| 1077 | ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, sections_offset ); |
| 1078 | ELFIO_GET_SET_ACCESS_DECL( Elf_Half, segments_num ); |
| 1079 | ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, segments_offset ); |
| 1080 | ELFIO_GET_SET_ACCESS_DECL( Elf_Half, section_name_str_index ); |
| 1081 | }; |
| 1082 | |
| 1083 | template <class T> struct elf_header_impl_types; |
| 1084 | template <> struct elf_header_impl_types<Elf32_Ehdr> |
| 1085 | { |
| 1086 | typedef Elf32_Phdr Phdr_type; |
| 1087 | typedef Elf32_Shdr Shdr_type; |
| 1088 | static const unsigned char file_class = ELFCLASS32; |
| 1089 | }; |
| 1090 | template <> struct elf_header_impl_types<Elf64_Ehdr> |
| 1091 | { |
| 1092 | typedef Elf64_Phdr Phdr_type; |
| 1093 | typedef Elf64_Shdr Shdr_type; |
| 1094 | static const unsigned char file_class = ELFCLASS64; |
| 1095 | }; |
| 1096 | |
| 1097 | template <class T> class elf_header_impl : public elf_header |
| 1098 | { |
| 1099 | public: |
| 1100 | //------------------------------------------------------------------------------ |
| 1101 | elf_header_impl( endianess_convertor* convertor_, unsigned char encoding ) |
| 1102 | { |
| 1103 | convertor = convertor_; |
| 1104 | |
| 1105 | std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), |
| 1106 | '\0' ); |
| 1107 | |
| 1108 | header.e_ident[EI_MAG0] = ELFMAG0; |
| 1109 | header.e_ident[EI_MAG1] = ELFMAG1; |
| 1110 | header.e_ident[EI_MAG2] = ELFMAG2; |
| 1111 | header.e_ident[EI_MAG3] = ELFMAG3; |
| 1112 | header.e_ident[EI_CLASS] = elf_header_impl_types<T>::file_class; |
| 1113 | header.e_ident[EI_DATA] = encoding; |
| 1114 | header.e_ident[EI_VERSION] = EV_CURRENT; |
| 1115 | header.e_version = ( *convertor )( (Elf_Word)EV_CURRENT ); |
| 1116 | header.e_ehsize = ( sizeof( header ) ); |
| 1117 | header.e_ehsize = ( *convertor )( header.e_ehsize ); |
| 1118 | header.e_shstrndx = ( *convertor )( (Elf_Half)1 ); |
| 1119 | header.e_phentsize = |
| 1120 | sizeof( typename elf_header_impl_types<T>::Phdr_type ); |
| 1121 | header.e_shentsize = |
| 1122 | sizeof( typename elf_header_impl_types<T>::Shdr_type ); |
| 1123 | header.e_phentsize = ( *convertor )( header.e_phentsize ); |
| 1124 | header.e_shentsize = ( *convertor )( header.e_shentsize ); |
| 1125 | } |
| 1126 | |
| 1127 | //------------------------------------------------------------------------------ |
| 1128 | bool load( std::istream& stream ) |
| 1129 | { |
| 1130 | stream.seekg( 0 ); |
| 1131 | stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) ); |
| 1132 | |
| 1133 | return ( stream.gcount() == sizeof( header ) ); |
| 1134 | } |
| 1135 | |
| 1136 | //------------------------------------------------------------------------------ |
| 1137 | bool save( std::ostream& stream ) const |
| 1138 | { |
| 1139 | stream.seekp( 0 ); |
| 1140 | stream.write( reinterpret_cast<const char*>( &header ), |
| 1141 | sizeof( header ) ); |
| 1142 | |
| 1143 | return stream.good(); |
| 1144 | } |
| 1145 | |
| 1146 | //------------------------------------------------------------------------------ |
| 1147 | // ELF header functions |
| 1148 | ELFIO_GET_ACCESS( unsigned char, class, header.e_ident[EI_CLASS] ); |
| 1149 | ELFIO_GET_ACCESS( unsigned char, elf_version, header.e_ident[EI_VERSION] ); |
| 1150 | ELFIO_GET_ACCESS( unsigned char, encoding, header.e_ident[EI_DATA] ); |
| 1151 | ELFIO_GET_ACCESS( Elf_Half, header_size, header.e_ehsize ); |
| 1152 | ELFIO_GET_ACCESS( Elf_Half, section_entry_size, header.e_shentsize ); |
| 1153 | ELFIO_GET_ACCESS( Elf_Half, segment_entry_size, header.e_phentsize ); |
| 1154 | |
| 1155 | ELFIO_GET_SET_ACCESS( Elf_Word, version, header.e_version ); |
| 1156 | ELFIO_GET_SET_ACCESS( unsigned char, os_abi, header.e_ident[EI_OSABI] ); |
| 1157 | ELFIO_GET_SET_ACCESS( unsigned char, |
| 1158 | abi_version, |
| 1159 | header.e_ident[EI_ABIVERSION] ); |
| 1160 | ELFIO_GET_SET_ACCESS( Elf_Half, type, header.e_type ); |
| 1161 | ELFIO_GET_SET_ACCESS( Elf_Half, machine, header.e_machine ); |
| 1162 | ELFIO_GET_SET_ACCESS( Elf_Word, flags, header.e_flags ); |
| 1163 | ELFIO_GET_SET_ACCESS( Elf_Half, section_name_str_index, header.e_shstrndx ); |
| 1164 | ELFIO_GET_SET_ACCESS( Elf64_Addr, entry, header.e_entry ); |
| 1165 | ELFIO_GET_SET_ACCESS( Elf_Half, sections_num, header.e_shnum ); |
| 1166 | ELFIO_GET_SET_ACCESS( Elf64_Off, sections_offset, header.e_shoff ); |
| 1167 | ELFIO_GET_SET_ACCESS( Elf_Half, segments_num, header.e_phnum ); |
| 1168 | ELFIO_GET_SET_ACCESS( Elf64_Off, segments_offset, header.e_phoff ); |
| 1169 | |
| 1170 | private: |
| 1171 | T header; |
| 1172 | endianess_convertor* convertor; |
| 1173 | }; |
| 1174 | |
| 1175 | } // namespace ELFIO |
| 1176 | |
| 1177 | #endif // ELF_HEADER_HPP |
| 1178 | |
| 1179 | /*** End of inlined file: elfio_header.hpp ***/ |
| 1180 | |
| 1181 | |
| 1182 | /*** Start of inlined file: elfio_section.hpp ***/ |
| 1183 | #ifndef ELFIO_SECTION_HPP |
| 1184 | #define ELFIO_SECTION_HPP |
| 1185 | |
| 1186 | #include <string> |
| 1187 | #include <iostream> |
| 1188 | #include <new> |
| 1189 | |
| 1190 | namespace ELFIO { |
| 1191 | |
| 1192 | class section |
| 1193 | { |
| 1194 | friend class elfio; |
| 1195 | |
| 1196 | public: |
| 1197 | virtual ~section(){}; |
| 1198 | |
| 1199 | ELFIO_GET_ACCESS_DECL( Elf_Half, index ); |
| 1200 | ELFIO_GET_SET_ACCESS_DECL( std::string, name ); |
| 1201 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type ); |
| 1202 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, flags ); |
| 1203 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, info ); |
| 1204 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, link ); |
| 1205 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, addr_align ); |
| 1206 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, entry_size ); |
| 1207 | ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, address ); |
| 1208 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, size ); |
| 1209 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset ); |
| 1210 | ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); |
| 1211 | |
| 1212 | virtual const char* get_data() const = 0; |
| 1213 | virtual void set_data( const char* pData, Elf_Word size ) = 0; |
| 1214 | virtual void set_data( const std::string& data ) = 0; |
| 1215 | virtual void append_data( const char* pData, Elf_Word size ) = 0; |
| 1216 | virtual void append_data( const std::string& data ) = 0; |
| 1217 | virtual size_t get_stream_size() const = 0; |
| 1218 | virtual void set_stream_size( size_t value ) = 0; |
| 1219 | |
| 1220 | protected: |
| 1221 | ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); |
| 1222 | ELFIO_SET_ACCESS_DECL( Elf_Half, index ); |
| 1223 | |
| 1224 | virtual void load( std::istream& stream, std::streampos header_offset ) = 0; |
| 1225 | virtual void save( std::ostream& stream, |
| 1226 | std::streampos header_offset, |
| 1227 | std::streampos data_offset ) = 0; |
| 1228 | virtual bool is_address_initialized() const = 0; |
| 1229 | }; |
| 1230 | |
| 1231 | template <class T> class section_impl : public section |
| 1232 | { |
| 1233 | public: |
| 1234 | //------------------------------------------------------------------------------ |
| 1235 | section_impl( const endianess_convertor* convertor_ ) |
| 1236 | : convertor( convertor_ ) |
| 1237 | { |
| 1238 | std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), |
| 1239 | '\0' ); |
| 1240 | is_address_set = false; |
| 1241 | data = 0; |
| 1242 | data_size = 0; |
| 1243 | index = 0; |
| 1244 | stream_size = 0; |
| 1245 | } |
| 1246 | |
| 1247 | //------------------------------------------------------------------------------ |
| 1248 | ~section_impl() { delete[] data; } |
| 1249 | |
| 1250 | //------------------------------------------------------------------------------ |
| 1251 | // Section info functions |
| 1252 | ELFIO_GET_SET_ACCESS( Elf_Word, type, header.sh_type ); |
| 1253 | ELFIO_GET_SET_ACCESS( Elf_Xword, flags, header.sh_flags ); |
| 1254 | ELFIO_GET_SET_ACCESS( Elf_Xword, size, header.sh_size ); |
| 1255 | ELFIO_GET_SET_ACCESS( Elf_Word, link, header.sh_link ); |
| 1256 | ELFIO_GET_SET_ACCESS( Elf_Word, info, header.sh_info ); |
| 1257 | ELFIO_GET_SET_ACCESS( Elf_Xword, addr_align, header.sh_addralign ); |
| 1258 | ELFIO_GET_SET_ACCESS( Elf_Xword, entry_size, header.sh_entsize ); |
| 1259 | ELFIO_GET_SET_ACCESS( Elf_Word, name_string_offset, header.sh_name ); |
| 1260 | ELFIO_GET_ACCESS( Elf64_Addr, address, header.sh_addr ); |
| 1261 | |
| 1262 | //------------------------------------------------------------------------------ |
| 1263 | Elf_Half get_index() const { return index; } |
| 1264 | |
| 1265 | //------------------------------------------------------------------------------ |
| 1266 | std::string get_name() const { return name; } |
| 1267 | |
| 1268 | //------------------------------------------------------------------------------ |
| 1269 | void set_name( std::string name_ ) { name = name_; } |
| 1270 | |
| 1271 | //------------------------------------------------------------------------------ |
| 1272 | void set_address( Elf64_Addr value ) |
| 1273 | { |
| 1274 | header.sh_addr = value; |
| 1275 | header.sh_addr = ( *convertor )( header.sh_addr ); |
| 1276 | is_address_set = true; |
| 1277 | } |
| 1278 | |
| 1279 | //------------------------------------------------------------------------------ |
| 1280 | bool is_address_initialized() const { return is_address_set; } |
| 1281 | |
| 1282 | //------------------------------------------------------------------------------ |
| 1283 | const char* get_data() const { return data; } |
| 1284 | |
| 1285 | //------------------------------------------------------------------------------ |
| 1286 | void set_data( const char* raw_data, Elf_Word size ) |
| 1287 | { |
| 1288 | if ( get_type() != SHT_NOBITS ) { |
| 1289 | delete[] data; |
| 1290 | data = new ( std::nothrow ) char[size]; |
| 1291 | if ( 0 != data && 0 != raw_data ) { |
| 1292 | data_size = size; |
| 1293 | std::copy( raw_data, raw_data + size, data ); |
| 1294 | } |
| 1295 | else { |
| 1296 | data_size = 0; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | set_size( data_size ); |
| 1301 | } |
| 1302 | |
| 1303 | //------------------------------------------------------------------------------ |
| 1304 | void set_data( const std::string& str_data ) |
| 1305 | { |
| 1306 | return set_data( str_data.c_str(), (Elf_Word)str_data.size() ); |
| 1307 | } |
| 1308 | |
| 1309 | //------------------------------------------------------------------------------ |
| 1310 | void append_data( const char* raw_data, Elf_Word size ) |
| 1311 | { |
| 1312 | if ( get_type() != SHT_NOBITS ) { |
| 1313 | if ( get_size() + size < data_size ) { |
| 1314 | std::copy( raw_data, raw_data + size, data + get_size() ); |
| 1315 | } |
| 1316 | else { |
| 1317 | data_size = 2 * ( data_size + size ); |
| 1318 | char* new_data = new ( std::nothrow ) char[data_size]; |
| 1319 | |
| 1320 | if ( 0 != new_data ) { |
| 1321 | std::copy( data, data + get_size(), new_data ); |
| 1322 | std::copy( raw_data, raw_data + size, |
| 1323 | new_data + get_size() ); |
| 1324 | delete[] data; |
| 1325 | data = new_data; |
| 1326 | } |
| 1327 | else { |
| 1328 | size = 0; |
| 1329 | } |
| 1330 | } |
| 1331 | set_size( get_size() + size ); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | //------------------------------------------------------------------------------ |
| 1336 | void append_data( const std::string& str_data ) |
| 1337 | { |
| 1338 | return append_data( str_data.c_str(), (Elf_Word)str_data.size() ); |
| 1339 | } |
| 1340 | |
| 1341 | //------------------------------------------------------------------------------ |
| 1342 | protected: |
| 1343 | //------------------------------------------------------------------------------ |
| 1344 | ELFIO_GET_SET_ACCESS( Elf64_Off, offset, header.sh_offset ); |
| 1345 | |
| 1346 | //------------------------------------------------------------------------------ |
| 1347 | void set_index( Elf_Half value ) { index = value; } |
| 1348 | |
| 1349 | //------------------------------------------------------------------------------ |
| 1350 | void load( std::istream& stream, std::streampos header_offset ) |
| 1351 | { |
| 1352 | std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), |
| 1353 | '\0' ); |
| 1354 | |
| 1355 | stream.seekg( 0, stream.end ); |
| 1356 | set_stream_size( stream.tellg() ); |
| 1357 | |
| 1358 | stream.seekg( header_offset ); |
| 1359 | stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) ); |
| 1360 | |
| 1361 | Elf_Xword size = get_size(); |
| 1362 | if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() && |
| 1363 | size < get_stream_size() ) { |
| 1364 | data = new ( std::nothrow ) char[size + 1]; |
| 1365 | |
| 1366 | if ( ( 0 != size ) && ( 0 != data ) ) { |
| 1367 | stream.seekg( ( *convertor )( header.sh_offset ) ); |
| 1368 | stream.read( data, size ); |
| 1369 | data[size] = 0; // Ensure data is ended with 0 to avoid oob read |
| 1370 | data_size = size; |
| 1371 | } |
| 1372 | else { |
| 1373 | data_size = 0; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | //------------------------------------------------------------------------------ |
| 1379 | void save( std::ostream& stream, |
| 1380 | std::streampos header_offset, |
| 1381 | std::streampos data_offset ) |
| 1382 | { |
| 1383 | if ( 0 != get_index() ) { |
| 1384 | header.sh_offset = data_offset; |
| 1385 | header.sh_offset = ( *convertor )( header.sh_offset ); |
| 1386 | } |
| 1387 | |
| 1388 | save_header( stream, header_offset ); |
| 1389 | if ( get_type() != SHT_NOBITS && get_type() != SHT_NULL && |
| 1390 | get_size() != 0 && data != 0 ) { |
| 1391 | save_data( stream, data_offset ); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | //------------------------------------------------------------------------------ |
| 1396 | private: |
| 1397 | //------------------------------------------------------------------------------ |
| 1398 | void save_header( std::ostream& stream, std::streampos header_offset ) const |
| 1399 | { |
| 1400 | stream.seekp( header_offset ); |
| 1401 | stream.write( reinterpret_cast<const char*>( &header ), |
| 1402 | sizeof( header ) ); |
| 1403 | } |
| 1404 | |
| 1405 | //------------------------------------------------------------------------------ |
| 1406 | void save_data( std::ostream& stream, std::streampos data_offset ) const |
| 1407 | { |
| 1408 | stream.seekp( data_offset ); |
| 1409 | stream.write( get_data(), get_size() ); |
| 1410 | } |
| 1411 | |
| 1412 | //------------------------------------------------------------------------------ |
| 1413 | size_t get_stream_size() const { return stream_size; } |
| 1414 | |
| 1415 | //------------------------------------------------------------------------------ |
| 1416 | void set_stream_size( size_t value ) { stream_size = value; } |
| 1417 | |
| 1418 | //------------------------------------------------------------------------------ |
| 1419 | private: |
| 1420 | T header; |
| 1421 | Elf_Half index; |
| 1422 | std::string name; |
| 1423 | char* data; |
| 1424 | Elf_Word data_size; |
| 1425 | const endianess_convertor* convertor; |
| 1426 | bool is_address_set; |
| 1427 | size_t stream_size; |
| 1428 | }; |
| 1429 | |
| 1430 | } // namespace ELFIO |
| 1431 | |
| 1432 | #endif // ELFIO_SECTION_HPP |
| 1433 | |
| 1434 | /*** End of inlined file: elfio_section.hpp ***/ |
| 1435 | |
| 1436 | |
| 1437 | /*** Start of inlined file: elfio_segment.hpp ***/ |
| 1438 | #ifndef ELFIO_SEGMENT_HPP |
| 1439 | #define ELFIO_SEGMENT_HPP |
| 1440 | |
| 1441 | #include <iostream> |
| 1442 | #include <vector> |
| 1443 | #include <new> |
| 1444 | |
| 1445 | namespace ELFIO { |
| 1446 | |
| 1447 | class segment |
| 1448 | { |
| 1449 | friend class elfio; |
| 1450 | |
| 1451 | public: |
| 1452 | virtual ~segment(){}; |
| 1453 | |
| 1454 | ELFIO_GET_ACCESS_DECL( Elf_Half, index ); |
| 1455 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type ); |
| 1456 | ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags ); |
| 1457 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align ); |
| 1458 | ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, virtual_address ); |
| 1459 | ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, physical_address ); |
| 1460 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size ); |
| 1461 | ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size ); |
| 1462 | ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); |
| 1463 | |
| 1464 | virtual const char* get_data() const = 0; |
| 1465 | |
| 1466 | virtual Elf_Half add_section_index( Elf_Half index, |
| 1467 | Elf_Xword addr_align ) = 0; |
| 1468 | virtual Elf_Half get_sections_num() const = 0; |
| 1469 | virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0; |
| 1470 | virtual bool is_offset_initialized() const = 0; |
| 1471 | |
| 1472 | protected: |
| 1473 | ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); |
| 1474 | ELFIO_SET_ACCESS_DECL( Elf_Half, index ); |
| 1475 | |
| 1476 | virtual const std::vector<Elf_Half>& get_sections() const = 0; |
| 1477 | virtual void load( std::istream& stream, std::streampos header_offset ) = 0; |
| 1478 | virtual void save( std::ostream& stream, |
| 1479 | std::streampos header_offset, |
| 1480 | std::streampos data_offset ) = 0; |
| 1481 | }; |
| 1482 | |
| 1483 | //------------------------------------------------------------------------------ |
| 1484 | template <class T> class segment_impl : public segment |
| 1485 | { |
| 1486 | public: |
| 1487 | //------------------------------------------------------------------------------ |
| 1488 | segment_impl( endianess_convertor* convertor_ ) |
| 1489 | : stream_size( 0 ), index( 0 ), data( 0 ), convertor( convertor_ ) |
| 1490 | { |
| 1491 | is_offset_set = false; |
| 1492 | std::fill_n( reinterpret_cast<char*>( &ph ), sizeof( ph ), '\0' ); |
| 1493 | } |
| 1494 | |
| 1495 | //------------------------------------------------------------------------------ |
| 1496 | virtual ~segment_impl() { delete[] data; } |
| 1497 | |
| 1498 | //------------------------------------------------------------------------------ |
| 1499 | // Section info functions |
| 1500 | ELFIO_GET_SET_ACCESS( Elf_Word, type, ph.p_type ); |
| 1501 | ELFIO_GET_SET_ACCESS( Elf_Word, flags, ph.p_flags ); |
| 1502 | ELFIO_GET_SET_ACCESS( Elf_Xword, align, ph.p_align ); |
| 1503 | ELFIO_GET_SET_ACCESS( Elf64_Addr, virtual_address, ph.p_vaddr ); |
| 1504 | ELFIO_GET_SET_ACCESS( Elf64_Addr, physical_address, ph.p_paddr ); |
| 1505 | ELFIO_GET_SET_ACCESS( Elf_Xword, file_size, ph.p_filesz ); |
| 1506 | ELFIO_GET_SET_ACCESS( Elf_Xword, memory_size, ph.p_memsz ); |
| 1507 | ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset ); |
| 1508 | size_t stream_size; |
| 1509 | |
| 1510 | //------------------------------------------------------------------------------ |
| 1511 | size_t get_stream_size() const { return stream_size; } |
| 1512 | |
| 1513 | //------------------------------------------------------------------------------ |
| 1514 | void set_stream_size( size_t value ) { stream_size = value; } |
| 1515 | |
| 1516 | //------------------------------------------------------------------------------ |
| 1517 | Elf_Half get_index() const { return index; } |
| 1518 | |
| 1519 | //------------------------------------------------------------------------------ |
| 1520 | const char* get_data() const { return data; } |
| 1521 | |
| 1522 | //------------------------------------------------------------------------------ |
| 1523 | Elf_Half add_section_index( Elf_Half sec_index, Elf_Xword addr_align ) |
| 1524 | { |
| 1525 | sections.push_back( sec_index ); |
| 1526 | if ( addr_align > get_align() ) { |
| 1527 | set_align( addr_align ); |
| 1528 | } |
| 1529 | |
| 1530 | return (Elf_Half)sections.size(); |
| 1531 | } |
| 1532 | |
| 1533 | //------------------------------------------------------------------------------ |
| 1534 | Elf_Half get_sections_num() const { return (Elf_Half)sections.size(); } |
| 1535 | |
| 1536 | //------------------------------------------------------------------------------ |
| 1537 | Elf_Half get_section_index_at( Elf_Half num ) const |
| 1538 | { |
| 1539 | if ( num < sections.size() ) { |
| 1540 | return sections[num]; |
| 1541 | } |
| 1542 | |
| 1543 | return Elf_Half( -1 ); |
| 1544 | } |
| 1545 | |
| 1546 | //------------------------------------------------------------------------------ |
| 1547 | protected: |
| 1548 | //------------------------------------------------------------------------------ |
| 1549 | |
| 1550 | //------------------------------------------------------------------------------ |
| 1551 | void set_offset( Elf64_Off value ) |
| 1552 | { |
| 1553 | ph.p_offset = value; |
| 1554 | ph.p_offset = ( *convertor )( ph.p_offset ); |
| 1555 | is_offset_set = true; |
| 1556 | } |
| 1557 | |
| 1558 | //------------------------------------------------------------------------------ |
| 1559 | bool is_offset_initialized() const { return is_offset_set; } |
| 1560 | |
| 1561 | //------------------------------------------------------------------------------ |
| 1562 | const std::vector<Elf_Half>& get_sections() const { return sections; } |
| 1563 | |
| 1564 | //------------------------------------------------------------------------------ |
| 1565 | void set_index( Elf_Half value ) { index = value; } |
| 1566 | |
| 1567 | //------------------------------------------------------------------------------ |
| 1568 | void load( std::istream& stream, std::streampos header_offset ) |
| 1569 | { |
| 1570 | |
| 1571 | stream.seekg( 0, stream.end ); |
| 1572 | set_stream_size( stream.tellg() ); |
| 1573 | |
| 1574 | stream.seekg( header_offset ); |
| 1575 | stream.read( reinterpret_cast<char*>( &ph ), sizeof( ph ) ); |
| 1576 | is_offset_set = true; |
| 1577 | |
| 1578 | if ( PT_NULL != get_type() && 0 != get_file_size() ) { |
| 1579 | stream.seekg( ( *convertor )( ph.p_offset ) ); |
| 1580 | Elf_Xword size = get_file_size(); |
| 1581 | |
| 1582 | if ( size > get_stream_size() ) { |
| 1583 | data = 0; |
| 1584 | } |
| 1585 | else { |
| 1586 | data = new (std::nothrow) char[size + 1]; |
| 1587 | |
| 1588 | if ( 0 != data ) { |
| 1589 | stream.read( data, size ); |
| 1590 | data[size] = 0; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | //------------------------------------------------------------------------------ |
| 1597 | void save( std::ostream& stream, |
| 1598 | std::streampos header_offset, |
| 1599 | std::streampos data_offset ) |
| 1600 | { |
| 1601 | ph.p_offset = data_offset; |
| 1602 | ph.p_offset = ( *convertor )( ph.p_offset ); |
| 1603 | stream.seekp( header_offset ); |
| 1604 | stream.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) ); |
| 1605 | } |
| 1606 | |
| 1607 | //------------------------------------------------------------------------------ |
| 1608 | private: |
| 1609 | T ph; |
| 1610 | Elf_Half index; |
| 1611 | char* data; |
| 1612 | std::vector<Elf_Half> sections; |
| 1613 | endianess_convertor* convertor; |
| 1614 | bool is_offset_set; |
| 1615 | }; |
| 1616 | |
| 1617 | } // namespace ELFIO |
| 1618 | |
| 1619 | #endif // ELFIO_SEGMENT_HPP |
| 1620 | |
| 1621 | /*** End of inlined file: elfio_segment.hpp ***/ |
| 1622 | |
| 1623 | |
| 1624 | /*** Start of inlined file: elfio_strings.hpp ***/ |
| 1625 | #ifndef ELFIO_STRINGS_HPP |
| 1626 | #define ELFIO_STRINGS_HPP |
| 1627 | |
| 1628 | #include <cstdlib> |
| 1629 | #include <cstring> |
| 1630 | #include <string> |
| 1631 | |
| 1632 | namespace ELFIO { |
| 1633 | |
| 1634 | //------------------------------------------------------------------------------ |
| 1635 | template <class S> class string_section_accessor_template |
| 1636 | { |
| 1637 | public: |
| 1638 | //------------------------------------------------------------------------------ |
| 1639 | string_section_accessor_template( S* section_ ) : string_section( section_ ) |
| 1640 | { |
| 1641 | } |
| 1642 | |
| 1643 | //------------------------------------------------------------------------------ |
| 1644 | const char* get_string( Elf_Word index ) const |
| 1645 | { |
| 1646 | if ( string_section ) { |
| 1647 | if ( index < string_section->get_size() ) { |
| 1648 | const char* data = string_section->get_data(); |
| 1649 | if ( 0 != data ) { |
| 1650 | return data + index; |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | //------------------------------------------------------------------------------ |
| 1659 | Elf_Word add_string( const char* str ) |
| 1660 | { |
| 1661 | Elf_Word current_position = 0; |
| 1662 | |
| 1663 | if ( string_section ) { |
| 1664 | // Strings are addeded to the end of the current section data |
| 1665 | current_position = (Elf_Word)string_section->get_size(); |
| 1666 | |
| 1667 | if ( current_position == 0 ) { |
| 1668 | char empty_string = '\0'; |
| 1669 | string_section->append_data( &empty_string, 1 ); |
| 1670 | current_position++; |
| 1671 | } |
| 1672 | string_section->append_data( str, |
| 1673 | (Elf_Word)std::strlen( str ) + 1 ); |
| 1674 | } |
| 1675 | |
| 1676 | return current_position; |
| 1677 | } |
| 1678 | |
| 1679 | //------------------------------------------------------------------------------ |
| 1680 | Elf_Word add_string( const std::string& str ) |
| 1681 | { |
| 1682 | return add_string( str.c_str() ); |
| 1683 | } |
| 1684 | |
| 1685 | //------------------------------------------------------------------------------ |
| 1686 | private: |
| 1687 | S* string_section; |
| 1688 | }; |
| 1689 | |
| 1690 | using string_section_accessor = string_section_accessor_template<section>; |
| 1691 | using const_string_section_accessor = |
| 1692 | string_section_accessor_template<const section>; |
| 1693 | |
| 1694 | } // namespace ELFIO |
| 1695 | |
| 1696 | #endif // ELFIO_STRINGS_HPP |
| 1697 | |
| 1698 | /*** End of inlined file: elfio_strings.hpp ***/ |
| 1699 | |
| 1700 | #define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \ |
| 1701 | TYPE get_##FNAME() const { return header ? ( header->get_##FNAME() ) : 0; } |
| 1702 | |
| 1703 | #define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \ |
| 1704 | TYPE get_##FNAME() const \ |
| 1705 | { \ |
| 1706 | return header ? ( header->get_##FNAME() ) : 0; \ |
| 1707 | } \ |
| 1708 | void set_##FNAME( TYPE val ) \ |
| 1709 | { \ |
| 1710 | if ( header ) { \ |
| 1711 | header->set_##FNAME( val ); \ |
| 1712 | } \ |
| 1713 | } |
| 1714 | |
| 1715 | namespace ELFIO { |
| 1716 | |
| 1717 | //------------------------------------------------------------------------------ |
| 1718 | class elfio |
| 1719 | { |
| 1720 | public: |
| 1721 | //------------------------------------------------------------------------------ |
| 1722 | elfio() : sections( this ), segments( this ) |
| 1723 | { |
| 1724 | header = 0; |
| 1725 | current_file_pos = 0; |
| 1726 | create( ELFCLASS32, ELFDATA2LSB ); |
| 1727 | } |
| 1728 | |
| 1729 | //------------------------------------------------------------------------------ |
| 1730 | ~elfio() { clean(); } |
| 1731 | |
| 1732 | //------------------------------------------------------------------------------ |
| 1733 | void create( unsigned char file_class, unsigned char encoding ) |
| 1734 | { |
| 1735 | clean(); |
| 1736 | convertor.setup( encoding ); |
| 1737 | header = create_header( file_class, encoding ); |
| 1738 | create_mandatory_sections(); |
| 1739 | } |
| 1740 | |
| 1741 | //------------------------------------------------------------------------------ |
| 1742 | bool load( const std::string& file_name ) |
| 1743 | { |
| 1744 | std::ifstream stream; |
| 1745 | stream.open( file_name.c_str(), std::ios::in | std::ios::binary ); |
| 1746 | if ( !stream ) { |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
| 1750 | return load( stream ); |
| 1751 | } |
| 1752 | |
| 1753 | //------------------------------------------------------------------------------ |
| 1754 | bool load( std::istream& stream ) |
| 1755 | { |
| 1756 | clean(); |
| 1757 | |
| 1758 | unsigned char e_ident[EI_NIDENT]; |
| 1759 | // Read ELF file signature |
| 1760 | stream.read( reinterpret_cast<char*>( &e_ident ), sizeof( e_ident ) ); |
| 1761 | |
| 1762 | // Is it ELF file? |
| 1763 | if ( stream.gcount() != sizeof( e_ident ) || |
| 1764 | e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 || |
| 1765 | e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3 ) { |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
| 1769 | if ( ( e_ident[EI_CLASS] != ELFCLASS64 ) && |
| 1770 | ( e_ident[EI_CLASS] != ELFCLASS32 ) ) { |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | convertor.setup( e_ident[EI_DATA] ); |
| 1775 | header = create_header( e_ident[EI_CLASS], e_ident[EI_DATA] ); |
| 1776 | if ( 0 == header ) { |
| 1777 | return false; |
| 1778 | } |
| 1779 | if ( !header->load( stream ) ) { |
| 1780 | return false; |
| 1781 | } |
| 1782 | |
| 1783 | load_sections( stream ); |
| 1784 | bool is_still_good = load_segments( stream ); |
| 1785 | return is_still_good; |
| 1786 | } |
| 1787 | |
| 1788 | //------------------------------------------------------------------------------ |
| 1789 | bool save( const std::string& file_name ) |
| 1790 | { |
| 1791 | std::ofstream stream; |
| 1792 | stream.open( file_name.c_str(), std::ios::out | std::ios::binary ); |
| 1793 | if ( !stream ) { |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
| 1797 | return save( stream ); |
| 1798 | } |
| 1799 | |
| 1800 | //------------------------------------------------------------------------------ |
| 1801 | bool save( std::ostream& stream ) |
| 1802 | { |
| 1803 | if ( !stream || !header ) { |
| 1804 | return false; |
| 1805 | } |
| 1806 | |
| 1807 | bool is_still_good = true; |
| 1808 | // Define layout specific header fields |
| 1809 | // The position of the segment table is fixed after the header. |
| 1810 | // The position of the section table is variable and needs to be fixed |
| 1811 | // before saving. |
| 1812 | header->set_segments_num( segments.size() ); |
| 1813 | header->set_segments_offset( segments.size() ? header->get_header_size() |
| 1814 | : 0 ); |
| 1815 | header->set_sections_num( sections.size() ); |
| 1816 | header->set_sections_offset( 0 ); |
| 1817 | |
| 1818 | // Layout the first section right after the segment table |
| 1819 | current_file_pos = header->get_header_size() + |
| 1820 | header->get_segment_entry_size() * |
| 1821 | (Elf_Xword)header->get_segments_num(); |
| 1822 | |
| 1823 | calc_segment_alignment(); |
| 1824 | |
| 1825 | is_still_good = layout_segments_and_their_sections(); |
| 1826 | is_still_good = is_still_good && layout_sections_without_segments(); |
| 1827 | is_still_good = is_still_good && layout_section_table(); |
| 1828 | |
| 1829 | is_still_good = is_still_good && save_header( stream ); |
| 1830 | is_still_good = is_still_good && save_sections( stream ); |
| 1831 | is_still_good = is_still_good && save_segments( stream ); |
| 1832 | |
| 1833 | return is_still_good; |
| 1834 | } |
| 1835 | |
| 1836 | //------------------------------------------------------------------------------ |
| 1837 | // ELF header access functions |
| 1838 | ELFIO_HEADER_ACCESS_GET( unsigned char, class ); |
| 1839 | ELFIO_HEADER_ACCESS_GET( unsigned char, elf_version ); |
| 1840 | ELFIO_HEADER_ACCESS_GET( unsigned char, encoding ); |
| 1841 | ELFIO_HEADER_ACCESS_GET( Elf_Word, version ); |
| 1842 | ELFIO_HEADER_ACCESS_GET( Elf_Half, header_size ); |
| 1843 | ELFIO_HEADER_ACCESS_GET( Elf_Half, section_entry_size ); |
| 1844 | ELFIO_HEADER_ACCESS_GET( Elf_Half, segment_entry_size ); |
| 1845 | |
| 1846 | ELFIO_HEADER_ACCESS_GET_SET( unsigned char, os_abi ); |
| 1847 | ELFIO_HEADER_ACCESS_GET_SET( unsigned char, abi_version ); |
| 1848 | ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, type ); |
| 1849 | ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, machine ); |
| 1850 | ELFIO_HEADER_ACCESS_GET_SET( Elf_Word, flags ); |
| 1851 | ELFIO_HEADER_ACCESS_GET_SET( Elf64_Addr, entry ); |
| 1852 | ELFIO_HEADER_ACCESS_GET_SET( Elf64_Off, sections_offset ); |
| 1853 | ELFIO_HEADER_ACCESS_GET_SET( Elf64_Off, segments_offset ); |
| 1854 | ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, section_name_str_index ); |
| 1855 | |
| 1856 | //------------------------------------------------------------------------------ |
| 1857 | const endianess_convertor& get_convertor() const { return convertor; } |
| 1858 | |
| 1859 | //------------------------------------------------------------------------------ |
| 1860 | Elf_Xword get_default_entry_size( Elf_Word section_type ) const |
| 1861 | { |
| 1862 | switch ( section_type ) { |
| 1863 | case SHT_RELA: |
| 1864 | if ( header->get_class() == ELFCLASS64 ) { |
| 1865 | return sizeof( Elf64_Rela ); |
| 1866 | } |
| 1867 | else { |
| 1868 | return sizeof( Elf32_Rela ); |
| 1869 | } |
| 1870 | case SHT_REL: |
| 1871 | if ( header->get_class() == ELFCLASS64 ) { |
| 1872 | return sizeof( Elf64_Rel ); |
| 1873 | } |
| 1874 | else { |
| 1875 | return sizeof( Elf32_Rel ); |
| 1876 | } |
| 1877 | case SHT_SYMTAB: |
| 1878 | if ( header->get_class() == ELFCLASS64 ) { |
| 1879 | return sizeof( Elf64_Sym ); |
| 1880 | } |
| 1881 | else { |
| 1882 | return sizeof( Elf32_Sym ); |
| 1883 | } |
| 1884 | case SHT_DYNAMIC: |
| 1885 | if ( header->get_class() == ELFCLASS64 ) { |
| 1886 | return sizeof( Elf64_Dyn ); |
| 1887 | } |
| 1888 | else { |
| 1889 | return sizeof( Elf32_Dyn ); |
| 1890 | } |
| 1891 | default: |
| 1892 | return 0; |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | //------------------------------------------------------------------------------ |
| 1897 | private: |
| 1898 | bool is_offset_in_section( Elf64_Off offset, const section* sec ) const |
| 1899 | { |
| 1900 | return ( offset >= sec->get_offset() ) && |
| 1901 | ( offset < ( sec->get_offset() + sec->get_size() ) ); |
| 1902 | } |
| 1903 | |
| 1904 | //------------------------------------------------------------------------------ |
| 1905 | public: |
| 1906 | //! returns an empty string if no problems are detected, |
| 1907 | //! or a string containing an error message if problems are found |
| 1908 | std::string validate() const |
| 1909 | { |
| 1910 | |
| 1911 | // check for overlapping sections in the file |
| 1912 | for ( int i = 0; i < sections.size(); ++i ) { |
| 1913 | for ( int j = i + 1; j < sections.size(); ++j ) { |
| 1914 | const section* a = sections[i]; |
| 1915 | const section* b = sections[j]; |
| 1916 | if ( !( a->get_type() & SHT_NOBITS ) && |
| 1917 | !( b->get_type() & SHT_NOBITS ) && ( a->get_size() > 0 ) && |
| 1918 | ( b->get_size() > 0 ) && ( a->get_offset() > 0 ) && |
| 1919 | ( b->get_offset() > 0 ) ) { |
| 1920 | if ( is_offset_in_section( a->get_offset(), b ) || |
| 1921 | is_offset_in_section( |
| 1922 | a->get_offset() + a->get_size() - 1, b ) || |
| 1923 | is_offset_in_section( b->get_offset(), a ) || |
| 1924 | is_offset_in_section( |
| 1925 | b->get_offset() + b->get_size() - 1, a ) ) { |
| 1926 | return "Sections " + a->get_name() + " and " + |
| 1927 | b->get_name() + " overlap in file"; |
| 1928 | } |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | // more checks to be added here... |
| 1934 | |
| 1935 | return ""; |
| 1936 | } |
| 1937 | |
| 1938 | //------------------------------------------------------------------------------ |
| 1939 | private: |
| 1940 | //------------------------------------------------------------------------------ |
| 1941 | void clean() |
| 1942 | { |
| 1943 | delete header; |
| 1944 | header = 0; |
| 1945 | |
| 1946 | std::vector<section*>::const_iterator it; |
| 1947 | for ( it = sections_.begin(); it != sections_.end(); ++it ) { |
| 1948 | delete *it; |
| 1949 | } |
| 1950 | sections_.clear(); |
| 1951 | |
| 1952 | std::vector<segment*>::const_iterator it1; |
| 1953 | for ( it1 = segments_.begin(); it1 != segments_.end(); ++it1 ) { |
| 1954 | delete *it1; |
| 1955 | } |
| 1956 | segments_.clear(); |
| 1957 | } |
| 1958 | |
| 1959 | //------------------------------------------------------------------------------ |
| 1960 | elf_header* create_header( unsigned char file_class, |
| 1961 | unsigned char encoding ) |
| 1962 | { |
| 1963 | elf_header* new_header = 0; |
| 1964 | |
| 1965 | if ( file_class == ELFCLASS64 ) { |
| 1966 | new_header = |
| 1967 | new elf_header_impl<Elf64_Ehdr>( &convertor, encoding ); |
| 1968 | } |
| 1969 | else if ( file_class == ELFCLASS32 ) { |
| 1970 | new_header = |
| 1971 | new elf_header_impl<Elf32_Ehdr>( &convertor, encoding ); |
| 1972 | } |
| 1973 | else { |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
| 1977 | return new_header; |
| 1978 | } |
| 1979 | |
| 1980 | //------------------------------------------------------------------------------ |
| 1981 | section* create_section() |
| 1982 | { |
| 1983 | section* new_section; |
| 1984 | unsigned char file_class = get_class(); |
| 1985 | |
| 1986 | if ( file_class == ELFCLASS64 ) { |
| 1987 | new_section = new section_impl<Elf64_Shdr>( &convertor ); |
| 1988 | } |
| 1989 | else if ( file_class == ELFCLASS32 ) { |
| 1990 | new_section = new section_impl<Elf32_Shdr>( &convertor ); |
| 1991 | } |
| 1992 | else { |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
| 1996 | new_section->set_index( (Elf_Half)sections_.size() ); |
| 1997 | sections_.push_back( new_section ); |
| 1998 | |
| 1999 | return new_section; |
| 2000 | } |
| 2001 | |
| 2002 | //------------------------------------------------------------------------------ |
| 2003 | segment* create_segment() |
| 2004 | { |
| 2005 | segment* new_segment; |
| 2006 | unsigned char file_class = header->get_class(); |
| 2007 | |
| 2008 | if ( file_class == ELFCLASS64 ) { |
| 2009 | new_segment = new segment_impl<Elf64_Phdr>( &convertor ); |
| 2010 | } |
| 2011 | else if ( file_class == ELFCLASS32 ) { |
| 2012 | new_segment = new segment_impl<Elf32_Phdr>( &convertor ); |
| 2013 | } |
| 2014 | else { |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
| 2018 | new_segment->set_index( (Elf_Half)segments_.size() ); |
| 2019 | segments_.push_back( new_segment ); |
| 2020 | |
| 2021 | return new_segment; |
| 2022 | } |
| 2023 | |
| 2024 | //------------------------------------------------------------------------------ |
| 2025 | void create_mandatory_sections() |
| 2026 | { |
| 2027 | // Create null section without calling to 'add_section' as no string |
| 2028 | // section containing section names exists yet |
| 2029 | section* sec0 = create_section(); |
| 2030 | sec0->set_index( 0 ); |
| 2031 | sec0->set_name( "" ); |
| 2032 | sec0->set_name_string_offset( 0 ); |
| 2033 | |
| 2034 | set_section_name_str_index( 1 ); |
| 2035 | section* shstrtab = sections.add( ".shstrtab" ); |
| 2036 | shstrtab->set_type( SHT_STRTAB ); |
| 2037 | shstrtab->set_addr_align( 1 ); |
| 2038 | } |
| 2039 | |
| 2040 | //------------------------------------------------------------------------------ |
| 2041 | Elf_Half load_sections( std::istream& stream ) |
| 2042 | { |
| 2043 | Elf_Half entry_size = header->get_section_entry_size(); |
| 2044 | Elf_Half num = header->get_sections_num(); |
| 2045 | Elf64_Off offset = header->get_sections_offset(); |
| 2046 | |
| 2047 | for ( Elf_Half i = 0; i < num; ++i ) { |
| 2048 | section* sec = create_section(); |
| 2049 | sec->load( stream, (std::streamoff)offset + |
| 2050 | (std::streampos)i * entry_size ); |
| 2051 | sec->set_index( i ); |
| 2052 | // To mark that the section is not permitted to reassign address |
| 2053 | // during layout calculation |
| 2054 | sec->set_address( sec->get_address() ); |
| 2055 | } |
| 2056 | |
| 2057 | Elf_Half shstrndx = get_section_name_str_index(); |
| 2058 | |
| 2059 | if ( SHN_UNDEF != shstrndx ) { |
| 2060 | string_section_accessor str_reader( sections[shstrndx] ); |
| 2061 | for ( Elf_Half i = 0; i < num; ++i ) { |
| 2062 | Elf_Word section_offset = sections[i]->get_name_string_offset(); |
| 2063 | const char* p = str_reader.get_string( section_offset ); |
| 2064 | if ( p != 0 ) { |
| 2065 | sections[i]->set_name( p ); |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | return num; |
| 2071 | } |
| 2072 | |
| 2073 | //------------------------------------------------------------------------------ |
| 2074 | //! Checks whether the addresses of the section entirely fall within the given segment. |
| 2075 | //! It doesn't matter if the addresses are memory addresses, or file offsets, |
| 2076 | //! they just need to be in the same address space |
| 2077 | bool is_sect_in_seg( Elf64_Off sect_begin, |
| 2078 | Elf_Xword sect_size, |
| 2079 | Elf64_Off seg_begin, |
| 2080 | Elf64_Off seg_end ) |
| 2081 | { |
| 2082 | return ( seg_begin <= sect_begin ) && |
| 2083 | ( sect_begin + sect_size <= seg_end ) && |
| 2084 | ( sect_begin < |
| 2085 | seg_end ); // this is important criteria when sect_size == 0 |
| 2086 | // Example: seg_begin=10, seg_end=12 (-> covering the bytes 10 and 11) |
| 2087 | // sect_begin=12, sect_size=0 -> shall return false! |
| 2088 | } |
| 2089 | |
| 2090 | //------------------------------------------------------------------------------ |
| 2091 | bool load_segments( std::istream& stream ) |
| 2092 | { |
| 2093 | Elf_Half entry_size = header->get_segment_entry_size(); |
| 2094 | Elf_Half num = header->get_segments_num(); |
| 2095 | Elf64_Off offset = header->get_segments_offset(); |
| 2096 | |
| 2097 | for ( Elf_Half i = 0; i < num; ++i ) { |
| 2098 | segment* seg; |
| 2099 | unsigned char file_class = header->get_class(); |
| 2100 | |
| 2101 | if ( file_class == ELFCLASS64 ) { |
| 2102 | seg = new segment_impl<Elf64_Phdr>( &convertor ); |
| 2103 | } |
| 2104 | else if ( file_class == ELFCLASS32 ) { |
| 2105 | seg = new segment_impl<Elf32_Phdr>( &convertor ); |
| 2106 | } |
| 2107 | else { |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
| 2111 | seg->load( stream, (std::streamoff)offset + |
| 2112 | (std::streampos)i * entry_size ); |
| 2113 | seg->set_index( i ); |
| 2114 | |
| 2115 | // Add sections to the segments (similar to readelfs algorithm) |
| 2116 | Elf64_Off segBaseOffset = seg->get_offset(); |
| 2117 | Elf64_Off segEndOffset = segBaseOffset + seg->get_file_size(); |
| 2118 | Elf64_Off segVBaseAddr = seg->get_virtual_address(); |
| 2119 | Elf64_Off segVEndAddr = segVBaseAddr + seg->get_memory_size(); |
| 2120 | for ( Elf_Half j = 0; j < sections.size(); ++j ) { |
| 2121 | const section* psec = sections[j]; |
| 2122 | |
| 2123 | // SHF_ALLOC sections are matched based on the virtual address |
| 2124 | // otherwise the file offset is matched |
| 2125 | if ( ( psec->get_flags() & SHF_ALLOC ) |
| 2126 | ? is_sect_in_seg( psec->get_address(), |
| 2127 | psec->get_size(), segVBaseAddr, |
| 2128 | segVEndAddr ) |
| 2129 | : is_sect_in_seg( psec->get_offset(), psec->get_size(), |
| 2130 | segBaseOffset, segEndOffset ) ) { |
| 2131 | // Alignment of segment shall not be updated, to preserve original value |
| 2132 | // It will be re-calculated on saving. |
| 2133 | seg->add_section_index( psec->get_index(), 0 ); |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | // Add section into the segments' container |
| 2138 | segments_.push_back( seg ); |
| 2139 | } |
| 2140 | |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
| 2144 | //------------------------------------------------------------------------------ |
| 2145 | bool save_header( std::ostream& stream ) { return header->save( stream ); } |
| 2146 | |
| 2147 | //------------------------------------------------------------------------------ |
| 2148 | bool save_sections( std::ostream& stream ) |
| 2149 | { |
| 2150 | for ( unsigned int i = 0; i < sections_.size(); ++i ) { |
| 2151 | section* sec = sections_.at( i ); |
| 2152 | |
| 2153 | std::streampos headerPosition = |
| 2154 | (std::streamoff)header->get_sections_offset() + |
| 2155 | (std::streampos)header->get_section_entry_size() * |
| 2156 | sec->get_index(); |
| 2157 | |
| 2158 | sec->save( stream, headerPosition, sec->get_offset() ); |
| 2159 | } |
| 2160 | return true; |
| 2161 | } |
| 2162 | |
| 2163 | //------------------------------------------------------------------------------ |
| 2164 | bool save_segments( std::ostream& stream ) |
| 2165 | { |
| 2166 | for ( unsigned int i = 0; i < segments_.size(); ++i ) { |
| 2167 | segment* seg = segments_.at( i ); |
| 2168 | |
| 2169 | std::streampos headerPosition = |
| 2170 | header->get_segments_offset() + |
| 2171 | (std::streampos)header->get_segment_entry_size() * |
| 2172 | seg->get_index(); |
| 2173 | |
| 2174 | seg->save( stream, headerPosition, seg->get_offset() ); |
| 2175 | } |
| 2176 | return true; |
| 2177 | } |
| 2178 | |
| 2179 | //------------------------------------------------------------------------------ |
| 2180 | bool is_section_without_segment( unsigned int section_index ) |
| 2181 | { |
| 2182 | bool found = false; |
| 2183 | |
| 2184 | for ( unsigned int j = 0; !found && ( j < segments.size() ); ++j ) { |
| 2185 | for ( unsigned int k = 0; |
| 2186 | !found && ( k < segments[j]->get_sections_num() ); ++k ) { |
| 2187 | found = segments[j]->get_section_index_at( k ) == section_index; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | return !found; |
| 2192 | } |
| 2193 | |
| 2194 | //------------------------------------------------------------------------------ |
| 2195 | bool is_subsequence_of( segment* seg1, segment* seg2 ) |
| 2196 | { |
| 2197 | // Return 'true' if sections of seg1 are a subset of sections in seg2 |
| 2198 | const std::vector<Elf_Half>& sections1 = seg1->get_sections(); |
| 2199 | const std::vector<Elf_Half>& sections2 = seg2->get_sections(); |
| 2200 | |
| 2201 | bool found = false; |
| 2202 | if ( sections1.size() < sections2.size() ) { |
| 2203 | found = std::includes( sections2.begin(), sections2.end(), |
| 2204 | sections1.begin(), sections1.end() ); |
| 2205 | } |
| 2206 | |
| 2207 | return found; |
| 2208 | } |
| 2209 | |
| 2210 | //------------------------------------------------------------------------------ |
| 2211 | std::vector<segment*> get_ordered_segments() |
| 2212 | { |
| 2213 | std::vector<segment*> res; |
| 2214 | std::deque<segment*> worklist; |
| 2215 | |
| 2216 | res.reserve( segments.size() ); |
| 2217 | std::copy( segments_.begin(), segments_.end(), |
| 2218 | std::back_inserter( worklist ) ); |
| 2219 | |
| 2220 | // Bring the segments which start at address 0 to the front |
| 2221 | size_t nextSlot = 0; |
| 2222 | for ( size_t i = 0; i < worklist.size(); ++i ) { |
| 2223 | if ( i != nextSlot && worklist[i]->is_offset_initialized() && |
| 2224 | worklist[i]->get_offset() == 0 ) { |
| 2225 | if ( worklist[nextSlot]->get_offset() == 0 ) { |
| 2226 | ++nextSlot; |
| 2227 | } |
| 2228 | std::swap( worklist[i], worklist[nextSlot] ); |
| 2229 | ++nextSlot; |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | while ( !worklist.empty() ) { |
| 2234 | segment* seg = worklist.front(); |
| 2235 | worklist.pop_front(); |
| 2236 | |
| 2237 | size_t i = 0; |
| 2238 | for ( ; i < worklist.size(); ++i ) { |
| 2239 | if ( is_subsequence_of( seg, worklist[i] ) ) { |
| 2240 | break; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | if ( i < worklist.size() ) |
| 2245 | worklist.push_back( seg ); |
| 2246 | else |
| 2247 | res.push_back( seg ); |
| 2248 | } |
| 2249 | |
| 2250 | return res; |
| 2251 | } |
| 2252 | |
| 2253 | //------------------------------------------------------------------------------ |
| 2254 | bool layout_sections_without_segments() |
| 2255 | { |
| 2256 | for ( unsigned int i = 0; i < sections_.size(); ++i ) { |
| 2257 | if ( is_section_without_segment( i ) ) { |
| 2258 | section* sec = sections_[i]; |
| 2259 | |
| 2260 | Elf_Xword section_align = sec->get_addr_align(); |
| 2261 | if ( section_align > 1 && |
| 2262 | current_file_pos % section_align != 0 ) { |
| 2263 | current_file_pos += |
| 2264 | section_align - current_file_pos % section_align; |
| 2265 | } |
| 2266 | |
| 2267 | if ( 0 != sec->get_index() ) |
| 2268 | sec->set_offset( current_file_pos ); |
| 2269 | |
| 2270 | if ( SHT_NOBITS != sec->get_type() && |
| 2271 | SHT_NULL != sec->get_type() ) { |
| 2272 | current_file_pos += sec->get_size(); |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | return true; |
| 2278 | } |
| 2279 | |
| 2280 | //------------------------------------------------------------------------------ |
| 2281 | void calc_segment_alignment() |
| 2282 | { |
| 2283 | for ( std::vector<segment*>::iterator s = segments_.begin(); |
| 2284 | s != segments_.end(); ++s ) { |
| 2285 | segment* seg = *s; |
| 2286 | for ( int i = 0; i < seg->get_sections_num(); ++i ) { |
| 2287 | section* sect = sections_[seg->get_section_index_at( i )]; |
| 2288 | if ( sect->get_addr_align() > seg->get_align() ) { |
| 2289 | seg->set_align( sect->get_addr_align() ); |
| 2290 | } |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | //------------------------------------------------------------------------------ |
| 2296 | bool layout_segments_and_their_sections() |
| 2297 | { |
| 2298 | std::vector<segment*> worklist; |
| 2299 | std::vector<bool> section_generated( sections.size(), false ); |
| 2300 | |
| 2301 | // Get segments in a order in where segments which contain a |
| 2302 | // sub sequence of other segments are located at the end |
| 2303 | worklist = get_ordered_segments(); |
| 2304 | |
| 2305 | for ( unsigned int i = 0; i < worklist.size(); ++i ) { |
| 2306 | Elf_Xword segment_memory = 0; |
| 2307 | Elf_Xword segment_filesize = 0; |
| 2308 | Elf_Xword seg_start_pos = current_file_pos; |
| 2309 | segment* seg = worklist[i]; |
| 2310 | |
| 2311 | // Special case: PHDR segment |
| 2312 | // This segment contains the program headers but no sections |
| 2313 | if ( seg->get_type() == PT_PHDR && seg->get_sections_num() == 0 ) { |
| 2314 | seg_start_pos = header->get_segments_offset(); |
| 2315 | segment_memory = segment_filesize = |
| 2316 | header->get_segment_entry_size() * |
| 2317 | (Elf_Xword)header->get_segments_num(); |
| 2318 | } |
| 2319 | // Special case: |
| 2320 | else if ( seg->is_offset_initialized() && seg->get_offset() == 0 ) { |
| 2321 | seg_start_pos = 0; |
| 2322 | if ( seg->get_sections_num() ) { |
| 2323 | segment_memory = segment_filesize = current_file_pos; |
| 2324 | } |
| 2325 | } |
| 2326 | // New segments with not generated sections |
| 2327 | // have to be aligned |
| 2328 | else if ( seg->get_sections_num() && |
| 2329 | !section_generated[seg->get_section_index_at( 0 )] ) { |
| 2330 | Elf_Xword align = seg->get_align() > 0 ? seg->get_align() : 1; |
| 2331 | Elf64_Off cur_page_alignment = current_file_pos % align; |
| 2332 | Elf64_Off req_page_alignment = |
| 2333 | seg->get_virtual_address() % align; |
| 2334 | Elf64_Off error = req_page_alignment - cur_page_alignment; |
| 2335 | |
| 2336 | current_file_pos += ( seg->get_align() + error ) % align; |
| 2337 | seg_start_pos = current_file_pos; |
| 2338 | } |
| 2339 | else if ( seg->get_sections_num() ) { |
| 2340 | seg_start_pos = |
| 2341 | sections[seg->get_section_index_at( 0 )]->get_offset(); |
| 2342 | } |
| 2343 | |
| 2344 | // Write segment's data |
| 2345 | for ( unsigned int j = 0; j < seg->get_sections_num(); ++j ) { |
| 2346 | Elf_Half index = seg->get_section_index_at( j ); |
| 2347 | |
| 2348 | section* sec = sections[index]; |
| 2349 | |
| 2350 | // The NULL section is always generated |
| 2351 | if ( SHT_NULL == sec->get_type() ) { |
| 2352 | section_generated[index] = true; |
| 2353 | continue; |
| 2354 | } |
| 2355 | |
| 2356 | Elf_Xword secAlign = 0; |
| 2357 | // Fix up the alignment |
| 2358 | if ( !section_generated[index] && |
| 2359 | sec->is_address_initialized() && |
| 2360 | SHT_NOBITS != sec->get_type() && |
| 2361 | SHT_NULL != sec->get_type() && 0 != sec->get_size() ) { |
| 2362 | // Align the sections based on the virtual addresses |
| 2363 | // when possible (this is what matters for execution) |
| 2364 | Elf64_Off req_offset = |
| 2365 | sec->get_address() - seg->get_virtual_address(); |
| 2366 | Elf64_Off cur_offset = current_file_pos - seg_start_pos; |
| 2367 | if ( req_offset < cur_offset ) { |
| 2368 | // something has gone awfully wrong, abort! |
| 2369 | // secAlign would turn out negative, seeking backwards and overwriting previous data |
| 2370 | return false; |
| 2371 | } |
| 2372 | secAlign = req_offset - cur_offset; |
| 2373 | } |
| 2374 | else if ( !section_generated[index] && |
| 2375 | !sec->is_address_initialized() ) { |
| 2376 | // If no address has been specified then only the section |
| 2377 | // alignment constraint has to be matched |
| 2378 | Elf_Xword align = sec->get_addr_align(); |
| 2379 | if ( align == 0 ) { |
| 2380 | align = 1; |
| 2381 | } |
| 2382 | Elf64_Off error = current_file_pos % align; |
| 2383 | secAlign = ( align - error ) % align; |
| 2384 | } |
| 2385 | else if ( section_generated[index] ) { |
| 2386 | // Alignment for already generated sections |
| 2387 | secAlign = |
| 2388 | sec->get_offset() - seg_start_pos - segment_filesize; |
| 2389 | } |
| 2390 | |
| 2391 | // Determine the segment file and memory sizes |
| 2392 | // Special case .tbss section (NOBITS) in non TLS segment |
| 2393 | if ( ( sec->get_flags() & SHF_ALLOC ) && |
| 2394 | !( ( sec->get_flags() & SHF_TLS ) && |
| 2395 | ( seg->get_type() != PT_TLS ) && |
| 2396 | ( SHT_NOBITS == sec->get_type() ) ) ) |
| 2397 | segment_memory += sec->get_size() + secAlign; |
| 2398 | |
| 2399 | if ( SHT_NOBITS != sec->get_type() ) |
| 2400 | segment_filesize += sec->get_size() + secAlign; |
| 2401 | |
| 2402 | // Nothing to be done when generating nested segments |
| 2403 | if ( section_generated[index] ) { |
| 2404 | continue; |
| 2405 | } |
| 2406 | |
| 2407 | current_file_pos += secAlign; |
| 2408 | |
| 2409 | // Set the section addresses when missing |
| 2410 | if ( !sec->is_address_initialized() ) |
| 2411 | sec->set_address( seg->get_virtual_address() + |
| 2412 | current_file_pos - seg_start_pos ); |
| 2413 | |
| 2414 | if ( 0 != sec->get_index() ) |
| 2415 | sec->set_offset( current_file_pos ); |
| 2416 | |
| 2417 | if ( SHT_NOBITS != sec->get_type() ) |
| 2418 | current_file_pos += sec->get_size(); |
| 2419 | |
| 2420 | section_generated[index] = true; |
| 2421 | } |
| 2422 | |
| 2423 | seg->set_file_size( segment_filesize ); |
| 2424 | |
| 2425 | // If we already have a memory size from loading an elf file (value > 0), |
| 2426 | // it must not shrink! |
| 2427 | // Memory size may be bigger than file size and it is the loader's job to do something |
| 2428 | // with the surplus bytes in memory, like initializing them with a defined value. |
| 2429 | if ( seg->get_memory_size() < segment_memory ) { |
| 2430 | seg->set_memory_size( segment_memory ); |
| 2431 | } |
| 2432 | |
| 2433 | seg->set_offset( seg_start_pos ); |
| 2434 | } |
| 2435 | |
| 2436 | return true; |
| 2437 | } |
| 2438 | |
| 2439 | //------------------------------------------------------------------------------ |
| 2440 | bool layout_section_table() |
| 2441 | { |
| 2442 | // Simply place the section table at the end for now |
| 2443 | Elf64_Off alignmentError = current_file_pos % 4; |
| 2444 | current_file_pos += ( 4 - alignmentError ) % 4; |
| 2445 | header->set_sections_offset( current_file_pos ); |
| 2446 | return true; |
| 2447 | } |
| 2448 | |
| 2449 | //------------------------------------------------------------------------------ |
| 2450 | public: |
| 2451 | friend class Sections; |
| 2452 | class Sections |
| 2453 | { |
| 2454 | public: |
| 2455 | //------------------------------------------------------------------------------ |
| 2456 | Sections( elfio* parent_ ) : parent( parent_ ) {} |
| 2457 | |
| 2458 | //------------------------------------------------------------------------------ |
| 2459 | Elf_Half size() const { return (Elf_Half)parent->sections_.size(); } |
| 2460 | |
| 2461 | //------------------------------------------------------------------------------ |
| 2462 | section* operator[]( unsigned int index ) const |
| 2463 | { |
| 2464 | section* sec = 0; |
| 2465 | |
| 2466 | if ( index < parent->sections_.size() ) { |
| 2467 | sec = parent->sections_[index]; |
| 2468 | } |
| 2469 | |
| 2470 | return sec; |
| 2471 | } |
| 2472 | |
| 2473 | //------------------------------------------------------------------------------ |
| 2474 | section* operator[]( const std::string& name ) const |
| 2475 | { |
| 2476 | section* sec = 0; |
| 2477 | |
| 2478 | std::vector<section*>::const_iterator it; |
| 2479 | for ( it = parent->sections_.begin(); it != parent->sections_.end(); |
| 2480 | ++it ) { |
| 2481 | if ( ( *it )->get_name() == name ) { |
| 2482 | sec = *it; |
| 2483 | break; |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | return sec; |
| 2488 | } |
| 2489 | |
| 2490 | //------------------------------------------------------------------------------ |
| 2491 | section* add( const std::string& name ) |
| 2492 | { |
| 2493 | section* new_section = parent->create_section(); |
| 2494 | new_section->set_name( name ); |
| 2495 | |
| 2496 | Elf_Half str_index = parent->get_section_name_str_index(); |
| 2497 | section* string_table( parent->sections_[str_index] ); |
| 2498 | string_section_accessor str_writer( string_table ); |
| 2499 | Elf_Word pos = str_writer.add_string( name ); |
| 2500 | new_section->set_name_string_offset( pos ); |
| 2501 | |
| 2502 | return new_section; |
| 2503 | } |
| 2504 | |
| 2505 | //------------------------------------------------------------------------------ |
| 2506 | std::vector<section*>::iterator begin() |
| 2507 | { |
| 2508 | return parent->sections_.begin(); |
| 2509 | } |
| 2510 | |
| 2511 | //------------------------------------------------------------------------------ |
| 2512 | std::vector<section*>::iterator end() |
| 2513 | { |
| 2514 | return parent->sections_.end(); |
| 2515 | } |
| 2516 | |
| 2517 | //------------------------------------------------------------------------------ |
| 2518 | std::vector<section*>::const_iterator begin() const |
| 2519 | { |
| 2520 | return parent->sections_.cbegin(); |
| 2521 | } |
| 2522 | |
| 2523 | //------------------------------------------------------------------------------ |
| 2524 | std::vector<section*>::const_iterator end() const |
| 2525 | { |
| 2526 | return parent->sections_.cend(); |
| 2527 | } |
| 2528 | |
| 2529 | //------------------------------------------------------------------------------ |
| 2530 | private: |
| 2531 | elfio* parent; |
| 2532 | } sections; |
| 2533 | |
| 2534 | //------------------------------------------------------------------------------ |
| 2535 | public: |
| 2536 | friend class Segments; |
| 2537 | class Segments |
| 2538 | { |
| 2539 | public: |
| 2540 | //------------------------------------------------------------------------------ |
| 2541 | Segments( elfio* parent_ ) : parent( parent_ ) {} |
| 2542 | |
| 2543 | //------------------------------------------------------------------------------ |
| 2544 | Elf_Half size() const { return (Elf_Half)parent->segments_.size(); } |
| 2545 | |
| 2546 | //------------------------------------------------------------------------------ |
| 2547 | segment* operator[]( unsigned int index ) const |
| 2548 | { |
| 2549 | return parent->segments_[index]; |
| 2550 | } |
| 2551 | |
| 2552 | //------------------------------------------------------------------------------ |
| 2553 | segment* add() { return parent->create_segment(); } |
| 2554 | |
| 2555 | //------------------------------------------------------------------------------ |
| 2556 | std::vector<segment*>::iterator begin() |
| 2557 | { |
| 2558 | return parent->segments_.begin(); |
| 2559 | } |
| 2560 | |
| 2561 | //------------------------------------------------------------------------------ |
| 2562 | std::vector<segment*>::iterator end() |
| 2563 | { |
| 2564 | return parent->segments_.end(); |
| 2565 | } |
| 2566 | |
| 2567 | //------------------------------------------------------------------------------ |
| 2568 | std::vector<segment*>::const_iterator begin() const |
| 2569 | { |
| 2570 | return parent->segments_.cbegin(); |
| 2571 | } |
| 2572 | |
| 2573 | //------------------------------------------------------------------------------ |
| 2574 | std::vector<segment*>::const_iterator end() const |
| 2575 | { |
| 2576 | return parent->segments_.cend(); |
| 2577 | } |
| 2578 | |
| 2579 | //------------------------------------------------------------------------------ |
| 2580 | private: |
| 2581 | elfio* parent; |
| 2582 | } segments; |
| 2583 | |
| 2584 | //------------------------------------------------------------------------------ |
| 2585 | private: |
| 2586 | elf_header* header; |
| 2587 | std::vector<section*> sections_; |
| 2588 | std::vector<segment*> segments_; |
| 2589 | endianess_convertor convertor; |
| 2590 | |
| 2591 | Elf_Xword current_file_pos; |
| 2592 | }; |
| 2593 | |
| 2594 | } // namespace ELFIO |
| 2595 | |
| 2596 | |
| 2597 | /*** Start of inlined file: elfio_symbols.hpp ***/ |
| 2598 | #ifndef ELFIO_SYMBOLS_HPP |
| 2599 | #define ELFIO_SYMBOLS_HPP |
| 2600 | |
| 2601 | namespace ELFIO { |
| 2602 | |
| 2603 | //------------------------------------------------------------------------------ |
| 2604 | template <class S> class symbol_section_accessor_template |
| 2605 | { |
| 2606 | public: |
| 2607 | //------------------------------------------------------------------------------ |
| 2608 | symbol_section_accessor_template( const elfio& elf_file_, |
| 2609 | S* symbol_section_ ) |
| 2610 | : elf_file( elf_file_ ), symbol_section( symbol_section_ ) |
| 2611 | { |
| 2612 | find_hash_section(); |
| 2613 | } |
| 2614 | |
| 2615 | //------------------------------------------------------------------------------ |
| 2616 | Elf_Xword get_symbols_num() const |
| 2617 | { |
| 2618 | Elf_Xword nRet = 0; |
| 2619 | if ( 0 != symbol_section->get_entry_size() ) { |
| 2620 | nRet = |
| 2621 | symbol_section->get_size() / symbol_section->get_entry_size(); |
| 2622 | } |
| 2623 | |
| 2624 | return nRet; |
| 2625 | } |
| 2626 | |
| 2627 | //------------------------------------------------------------------------------ |
| 2628 | bool get_symbol( Elf_Xword index, |
| 2629 | std::string& name, |
| 2630 | Elf64_Addr& value, |
| 2631 | Elf_Xword& size, |
| 2632 | unsigned char& bind, |
| 2633 | unsigned char& type, |
| 2634 | Elf_Half& section_index, |
| 2635 | unsigned char& other ) const |
| 2636 | { |
| 2637 | bool ret = false; |
| 2638 | |
| 2639 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 2640 | ret = generic_get_symbol<Elf32_Sym>( index, name, value, size, bind, |
| 2641 | type, section_index, other ); |
| 2642 | } |
| 2643 | else { |
| 2644 | ret = generic_get_symbol<Elf64_Sym>( index, name, value, size, bind, |
| 2645 | type, section_index, other ); |
| 2646 | } |
| 2647 | |
| 2648 | return ret; |
| 2649 | } |
| 2650 | |
| 2651 | //------------------------------------------------------------------------------ |
| 2652 | bool get_symbol( const std::string& name, |
| 2653 | Elf64_Addr& value, |
| 2654 | Elf_Xword& size, |
| 2655 | unsigned char& bind, |
| 2656 | unsigned char& type, |
| 2657 | Elf_Half& section_index, |
| 2658 | unsigned char& other ) const |
| 2659 | { |
| 2660 | bool ret = false; |
| 2661 | |
| 2662 | if ( 0 != get_hash_table_index() ) { |
| 2663 | Elf_Word nbucket = *(const Elf_Word*)hash_section->get_data(); |
| 2664 | Elf_Word nchain = *(const Elf_Word*)( hash_section->get_data() + |
| 2665 | sizeof( Elf_Word ) ); |
| 2666 | Elf_Word val = elf_hash( (const unsigned char*)name.c_str() ); |
| 2667 | Elf_Word y = *(const Elf_Word*)( hash_section->get_data() + |
| 2668 | ( 2 + val % nbucket ) * |
| 2669 | sizeof( Elf_Word ) ); |
| 2670 | std::string str; |
| 2671 | get_symbol( y, str, value, size, bind, type, section_index, other ); |
| 2672 | while ( str != name && STN_UNDEF != y && y < nchain ) { |
| 2673 | y = *(const Elf_Word*)( hash_section->get_data() + |
| 2674 | ( 2 + nbucket + y ) * |
| 2675 | sizeof( Elf_Word ) ); |
| 2676 | get_symbol( y, str, value, size, bind, type, section_index, |
| 2677 | other ); |
| 2678 | } |
| 2679 | if ( str == name ) { |
| 2680 | ret = true; |
| 2681 | } |
| 2682 | } |
| 2683 | else { |
| 2684 | for ( Elf_Xword i = 0; i < get_symbols_num() && !ret; i++ ) { |
| 2685 | std::string symbol_name; |
| 2686 | if ( get_symbol( i, symbol_name, value, size, bind, type, |
| 2687 | section_index, other ) ) { |
| 2688 | if ( symbol_name == name ) { |
| 2689 | ret = true; |
| 2690 | } |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | return ret; |
| 2696 | } |
| 2697 | |
| 2698 | //------------------------------------------------------------------------------ |
| 2699 | bool get_symbol( const Elf64_Addr& value, |
| 2700 | std::string& name, |
| 2701 | Elf_Xword& size, |
| 2702 | unsigned char& bind, |
| 2703 | unsigned char& type, |
| 2704 | Elf_Half& section_index, |
| 2705 | unsigned char& other ) const |
| 2706 | { |
| 2707 | |
| 2708 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 2709 | |
| 2710 | Elf_Xword idx = 0; |
| 2711 | bool match = false; |
| 2712 | Elf64_Addr v = 0; |
| 2713 | |
| 2714 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 2715 | match = generic_search_symbols<Elf32_Sym>( |
| 2716 | [&]( const Elf32_Sym* sym ) { |
| 2717 | return convertor( sym->st_value ) == value; |
| 2718 | }, |
| 2719 | idx ); |
| 2720 | } |
| 2721 | else { |
| 2722 | match = generic_search_symbols<Elf64_Sym>( |
| 2723 | [&]( const Elf64_Sym* sym ) { |
| 2724 | return convertor( sym->st_value ) == value; |
| 2725 | }, |
| 2726 | idx ); |
| 2727 | } |
| 2728 | |
| 2729 | if ( match ) { |
| 2730 | return get_symbol( idx, name, v, size, bind, type, section_index, |
| 2731 | other ); |
| 2732 | } |
| 2733 | |
| 2734 | return false; |
| 2735 | } |
| 2736 | |
| 2737 | //------------------------------------------------------------------------------ |
| 2738 | Elf_Word add_symbol( Elf_Word name, |
| 2739 | Elf64_Addr value, |
| 2740 | Elf_Xword size, |
| 2741 | unsigned char info, |
| 2742 | unsigned char other, |
| 2743 | Elf_Half shndx ) |
| 2744 | { |
| 2745 | Elf_Word nRet; |
| 2746 | |
| 2747 | if ( symbol_section->get_size() == 0 ) { |
| 2748 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 2749 | nRet = generic_add_symbol<Elf32_Sym>( 0, 0, 0, 0, 0, 0 ); |
| 2750 | } |
| 2751 | else { |
| 2752 | nRet = generic_add_symbol<Elf64_Sym>( 0, 0, 0, 0, 0, 0 ); |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 2757 | nRet = generic_add_symbol<Elf32_Sym>( name, value, size, info, |
| 2758 | other, shndx ); |
| 2759 | } |
| 2760 | else { |
| 2761 | nRet = generic_add_symbol<Elf64_Sym>( name, value, size, info, |
| 2762 | other, shndx ); |
| 2763 | } |
| 2764 | |
| 2765 | return nRet; |
| 2766 | } |
| 2767 | |
| 2768 | //------------------------------------------------------------------------------ |
| 2769 | Elf_Word add_symbol( Elf_Word name, |
| 2770 | Elf64_Addr value, |
| 2771 | Elf_Xword size, |
| 2772 | unsigned char bind, |
| 2773 | unsigned char type, |
| 2774 | unsigned char other, |
| 2775 | Elf_Half shndx ) |
| 2776 | { |
| 2777 | return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other, |
| 2778 | shndx ); |
| 2779 | } |
| 2780 | |
| 2781 | //------------------------------------------------------------------------------ |
| 2782 | Elf_Word add_symbol( string_section_accessor& pStrWriter, |
| 2783 | const char* str, |
| 2784 | Elf64_Addr value, |
| 2785 | Elf_Xword size, |
| 2786 | unsigned char info, |
| 2787 | unsigned char other, |
| 2788 | Elf_Half shndx ) |
| 2789 | { |
| 2790 | Elf_Word index = pStrWriter.add_string( str ); |
| 2791 | return add_symbol( index, value, size, info, other, shndx ); |
| 2792 | } |
| 2793 | |
| 2794 | //------------------------------------------------------------------------------ |
| 2795 | Elf_Word add_symbol( string_section_accessor& pStrWriter, |
| 2796 | const char* str, |
| 2797 | Elf64_Addr value, |
| 2798 | Elf_Xword size, |
| 2799 | unsigned char bind, |
| 2800 | unsigned char type, |
| 2801 | unsigned char other, |
| 2802 | Elf_Half shndx ) |
| 2803 | { |
| 2804 | return add_symbol( pStrWriter, str, value, size, |
| 2805 | ELF_ST_INFO( bind, type ), other, shndx ); |
| 2806 | } |
| 2807 | |
| 2808 | //------------------------------------------------------------------------------ |
| 2809 | Elf_Xword arrange_local_symbols( |
| 2810 | std::function<void( Elf_Xword first, Elf_Xword second )> func = |
| 2811 | nullptr ) |
| 2812 | { |
| 2813 | int nRet = 0; |
| 2814 | |
| 2815 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 2816 | nRet = generic_arrange_local_symbols<Elf32_Sym>( func ); |
| 2817 | } |
| 2818 | else { |
| 2819 | nRet = generic_arrange_local_symbols<Elf64_Sym>( func ); |
| 2820 | } |
| 2821 | |
| 2822 | return nRet; |
| 2823 | } |
| 2824 | |
| 2825 | //------------------------------------------------------------------------------ |
| 2826 | private: |
| 2827 | //------------------------------------------------------------------------------ |
| 2828 | void find_hash_section() |
| 2829 | { |
| 2830 | hash_section = 0; |
| 2831 | hash_section_index = 0; |
| 2832 | Elf_Half nSecNo = elf_file.sections.size(); |
| 2833 | for ( Elf_Half i = 0; i < nSecNo && 0 == hash_section_index; ++i ) { |
| 2834 | const section* sec = elf_file.sections[i]; |
| 2835 | if ( sec->get_link() == symbol_section->get_index() ) { |
| 2836 | hash_section = sec; |
| 2837 | hash_section_index = i; |
| 2838 | } |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | //------------------------------------------------------------------------------ |
| 2843 | Elf_Half get_string_table_index() const |
| 2844 | { |
| 2845 | return (Elf_Half)symbol_section->get_link(); |
| 2846 | } |
| 2847 | |
| 2848 | //------------------------------------------------------------------------------ |
| 2849 | Elf_Half get_hash_table_index() const { return hash_section_index; } |
| 2850 | |
| 2851 | //------------------------------------------------------------------------------ |
| 2852 | template <class T> const T* generic_get_symbol_ptr( Elf_Xword index ) const |
| 2853 | { |
| 2854 | if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) { |
| 2855 | const T* pSym = reinterpret_cast<const T*>( |
| 2856 | symbol_section->get_data() + |
| 2857 | index * symbol_section->get_entry_size() ); |
| 2858 | |
| 2859 | return pSym; |
| 2860 | } |
| 2861 | |
| 2862 | return nullptr; |
| 2863 | } |
| 2864 | |
| 2865 | //------------------------------------------------------------------------------ |
| 2866 | template <class T> |
| 2867 | bool generic_search_symbols( std::function<bool( const T* )> match, |
| 2868 | Elf_Xword& idx ) const |
| 2869 | { |
| 2870 | for ( Elf_Xword i = 0; i < get_symbols_num(); i++ ) { |
| 2871 | const T* symPtr = generic_get_symbol_ptr<T>( i ); |
| 2872 | |
| 2873 | if ( symPtr == nullptr ) |
| 2874 | return false; |
| 2875 | |
| 2876 | if ( match( symPtr ) ) { |
| 2877 | idx = i; |
| 2878 | return true; |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | return false; |
| 2883 | } |
| 2884 | |
| 2885 | //------------------------------------------------------------------------------ |
| 2886 | template <class T> |
| 2887 | bool generic_get_symbol( Elf_Xword index, |
| 2888 | std::string& name, |
| 2889 | Elf64_Addr& value, |
| 2890 | Elf_Xword& size, |
| 2891 | unsigned char& bind, |
| 2892 | unsigned char& type, |
| 2893 | Elf_Half& section_index, |
| 2894 | unsigned char& other ) const |
| 2895 | { |
| 2896 | bool ret = false; |
| 2897 | |
| 2898 | if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) { |
| 2899 | const T* pSym = reinterpret_cast<const T*>( |
| 2900 | symbol_section->get_data() + |
| 2901 | index * symbol_section->get_entry_size() ); |
| 2902 | |
| 2903 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 2904 | |
| 2905 | section* string_section = |
| 2906 | elf_file.sections[get_string_table_index()]; |
| 2907 | string_section_accessor str_reader( string_section ); |
| 2908 | const char* pStr = |
| 2909 | str_reader.get_string( convertor( pSym->st_name ) ); |
| 2910 | if ( 0 != pStr ) { |
| 2911 | name = pStr; |
| 2912 | } |
| 2913 | value = convertor( pSym->st_value ); |
| 2914 | size = convertor( pSym->st_size ); |
| 2915 | bind = ELF_ST_BIND( pSym->st_info ); |
| 2916 | type = ELF_ST_TYPE( pSym->st_info ); |
| 2917 | section_index = convertor( pSym->st_shndx ); |
| 2918 | other = pSym->st_other; |
| 2919 | |
| 2920 | ret = true; |
| 2921 | } |
| 2922 | |
| 2923 | return ret; |
| 2924 | } |
| 2925 | |
| 2926 | //------------------------------------------------------------------------------ |
| 2927 | template <class T> |
| 2928 | Elf_Word generic_add_symbol( Elf_Word name, |
| 2929 | Elf64_Addr value, |
| 2930 | Elf_Xword size, |
| 2931 | unsigned char info, |
| 2932 | unsigned char other, |
| 2933 | Elf_Half shndx ) |
| 2934 | { |
| 2935 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 2936 | |
| 2937 | T entry; |
| 2938 | entry.st_name = convertor( name ); |
| 2939 | entry.st_value = value; |
| 2940 | entry.st_value = convertor( entry.st_value ); |
| 2941 | entry.st_size = size; |
| 2942 | entry.st_size = convertor( entry.st_size ); |
| 2943 | entry.st_info = convertor( info ); |
| 2944 | entry.st_other = convertor( other ); |
| 2945 | entry.st_shndx = convertor( shndx ); |
| 2946 | |
| 2947 | symbol_section->append_data( reinterpret_cast<char*>( &entry ), |
| 2948 | sizeof( entry ) ); |
| 2949 | |
| 2950 | Elf_Word nRet = symbol_section->get_size() / sizeof( entry ) - 1; |
| 2951 | |
| 2952 | return nRet; |
| 2953 | } |
| 2954 | |
| 2955 | //------------------------------------------------------------------------------ |
| 2956 | template <class T> |
| 2957 | Elf_Xword generic_arrange_local_symbols( |
| 2958 | std::function<void( Elf_Xword first, Elf_Xword second )> func ) |
| 2959 | { |
| 2960 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 2961 | const Elf_Xword size = symbol_section->get_entry_size(); |
| 2962 | |
| 2963 | Elf_Xword first_not_local = |
| 2964 | 1; // Skip the first entry. It is always NOTYPE |
| 2965 | Elf_Xword current = 0; |
| 2966 | Elf_Xword count = get_symbols_num(); |
| 2967 | |
| 2968 | while ( true ) { |
| 2969 | T* p1 = nullptr; |
| 2970 | T* p2 = nullptr; |
| 2971 | |
| 2972 | while ( first_not_local < count ) { |
| 2973 | p1 = const_cast<T*>( |
| 2974 | generic_get_symbol_ptr<T>( first_not_local ) ); |
| 2975 | if ( ELF_ST_BIND( convertor( p1->st_info ) ) != STB_LOCAL ) |
| 2976 | break; |
| 2977 | ++first_not_local; |
| 2978 | } |
| 2979 | |
| 2980 | current = first_not_local + 1; |
| 2981 | while ( current < count ) { |
| 2982 | p2 = const_cast<T*>( generic_get_symbol_ptr<T>( current ) ); |
| 2983 | if ( ELF_ST_BIND( convertor( p2->st_info ) ) == STB_LOCAL ) |
| 2984 | break; |
| 2985 | ++current; |
| 2986 | } |
| 2987 | |
| 2988 | if ( first_not_local < count && current < count ) { |
| 2989 | if ( func ) |
| 2990 | func( first_not_local, current ); |
| 2991 | |
| 2992 | // Swap the symbols |
| 2993 | T tmp; |
| 2994 | std::copy( p1, p1 + 1, &tmp ); |
| 2995 | std::copy( p2, p2 + 1, p1 ); |
| 2996 | std::copy( &tmp, &tmp + 1, p2 ); |
| 2997 | } |
| 2998 | else { |
| 2999 | // Update 'info' field of the section |
| 3000 | symbol_section->set_info( first_not_local ); |
| 3001 | break; |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | // Elf_Word nRet = symbol_section->get_size() / sizeof(entry) - 1; |
| 3006 | |
| 3007 | return first_not_local; |
| 3008 | } |
| 3009 | |
| 3010 | //------------------------------------------------------------------------------ |
| 3011 | private: |
| 3012 | const elfio& elf_file; |
| 3013 | S* symbol_section; |
| 3014 | Elf_Half hash_section_index; |
| 3015 | const section* hash_section; |
| 3016 | }; |
| 3017 | |
| 3018 | using symbol_section_accessor = symbol_section_accessor_template<section>; |
| 3019 | using const_symbol_section_accessor = |
| 3020 | symbol_section_accessor_template<const section>; |
| 3021 | |
| 3022 | } // namespace ELFIO |
| 3023 | |
| 3024 | #endif // ELFIO_SYMBOLS_HPP |
| 3025 | |
| 3026 | /*** End of inlined file: elfio_symbols.hpp ***/ |
| 3027 | |
| 3028 | |
| 3029 | /*** Start of inlined file: elfio_note.hpp ***/ |
| 3030 | #ifndef ELFIO_NOTE_HPP |
| 3031 | #define ELFIO_NOTE_HPP |
| 3032 | |
| 3033 | namespace ELFIO { |
| 3034 | |
| 3035 | //------------------------------------------------------------------------------ |
| 3036 | // There are discrepancies in documentations. SCO documentation |
| 3037 | // (http://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section) |
| 3038 | // requires 8 byte entries alignment for 64-bit ELF file, |
| 3039 | // but Oracle's definition uses the same structure |
| 3040 | // for 32-bit and 64-bit formats. |
| 3041 | // (https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-18048.html) |
| 3042 | // |
| 3043 | // It looks like EM_X86_64 Linux implementation is similar to Oracle's |
| 3044 | // definition. Therefore, the same alignment works for both formats |
| 3045 | //------------------------------------------------------------------------------ |
| 3046 | |
| 3047 | //------------------------------------------------------------------------------ |
| 3048 | template <class S> class note_section_accessor_template |
| 3049 | { |
| 3050 | public: |
| 3051 | //------------------------------------------------------------------------------ |
| 3052 | note_section_accessor_template( const elfio& elf_file_, S* section_ ) |
| 3053 | : elf_file( elf_file_ ), note_section( section_ ) |
| 3054 | { |
| 3055 | process_section(); |
| 3056 | } |
| 3057 | |
| 3058 | //------------------------------------------------------------------------------ |
| 3059 | Elf_Word get_notes_num() const |
| 3060 | { |
| 3061 | return (Elf_Word)note_start_positions.size(); |
| 3062 | } |
| 3063 | |
| 3064 | //------------------------------------------------------------------------------ |
| 3065 | bool get_note( Elf_Word index, |
| 3066 | Elf_Word& type, |
| 3067 | std::string& name, |
| 3068 | void*& desc, |
| 3069 | Elf_Word& descSize ) const |
| 3070 | { |
| 3071 | if ( index >= note_section->get_size() ) { |
| 3072 | return false; |
| 3073 | } |
| 3074 | |
| 3075 | const char* pData = |
| 3076 | note_section->get_data() + note_start_positions[index]; |
| 3077 | int align = sizeof( Elf_Word ); |
| 3078 | |
| 3079 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3080 | type = convertor( *(const Elf_Word*)( pData + 2 * align ) ); |
| 3081 | Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) ); |
| 3082 | descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) ); |
| 3083 | |
| 3084 | Elf_Xword max_name_size = |
| 3085 | note_section->get_size() - note_start_positions[index]; |
| 3086 | if ( namesz < 1 || namesz > max_name_size || |
| 3087 | (Elf_Xword)namesz + descSize > max_name_size ) { |
| 3088 | return false; |
| 3089 | } |
| 3090 | name.assign( pData + 3 * align, namesz - 1 ); |
| 3091 | if ( 0 == descSize ) { |
| 3092 | desc = 0; |
| 3093 | } |
| 3094 | else { |
| 3095 | desc = |
| 3096 | const_cast<char*>( pData + 3 * align + |
| 3097 | ( ( namesz + align - 1 ) / align ) * align ); |
| 3098 | } |
| 3099 | |
| 3100 | return true; |
| 3101 | } |
| 3102 | |
| 3103 | //------------------------------------------------------------------------------ |
| 3104 | void add_note( Elf_Word type, |
| 3105 | const std::string& name, |
| 3106 | const void* desc, |
| 3107 | Elf_Word descSize ) |
| 3108 | { |
| 3109 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3110 | |
| 3111 | int align = sizeof( Elf_Word ); |
| 3112 | Elf_Word nameLen = (Elf_Word)name.size() + 1; |
| 3113 | Elf_Word nameLenConv = convertor( nameLen ); |
| 3114 | std::string buffer( reinterpret_cast<char*>( &nameLenConv ), align ); |
| 3115 | Elf_Word descSizeConv = convertor( descSize ); |
| 3116 | |
| 3117 | buffer.append( reinterpret_cast<char*>( &descSizeConv ), align ); |
| 3118 | type = convertor( type ); |
| 3119 | buffer.append( reinterpret_cast<char*>( &type ), align ); |
| 3120 | buffer.append( name ); |
| 3121 | buffer.append( 1, '\x00' ); |
| 3122 | const char pad[] = { '\0', '\0', '\0', '\0' }; |
| 3123 | if ( nameLen % align != 0 ) { |
| 3124 | buffer.append( pad, align - nameLen % align ); |
| 3125 | } |
| 3126 | if ( desc != 0 && descSize != 0 ) { |
| 3127 | buffer.append( reinterpret_cast<const char*>( desc ), descSize ); |
| 3128 | if ( descSize % align != 0 ) { |
| 3129 | buffer.append( pad, align - descSize % align ); |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | note_start_positions.push_back( note_section->get_size() ); |
| 3134 | note_section->append_data( buffer ); |
| 3135 | } |
| 3136 | |
| 3137 | private: |
| 3138 | //------------------------------------------------------------------------------ |
| 3139 | void process_section() |
| 3140 | { |
| 3141 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3142 | const char* data = note_section->get_data(); |
| 3143 | Elf_Xword size = note_section->get_size(); |
| 3144 | Elf_Xword current = 0; |
| 3145 | |
| 3146 | note_start_positions.clear(); |
| 3147 | |
| 3148 | // Is it empty? |
| 3149 | if ( 0 == data || 0 == size ) { |
| 3150 | return; |
| 3151 | } |
| 3152 | |
| 3153 | Elf_Word align = sizeof( Elf_Word ); |
| 3154 | while ( current + (Elf_Xword)3 * align <= size ) { |
| 3155 | note_start_positions.push_back( current ); |
| 3156 | Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) ); |
| 3157 | Elf_Word descsz = convertor( |
| 3158 | *(const Elf_Word*)( data + current + sizeof( namesz ) ) ); |
| 3159 | |
| 3160 | current += (Elf_Xword)3 * sizeof( Elf_Word ) + |
| 3161 | ( ( namesz + align - 1 ) / align ) * (Elf_Xword)align + |
| 3162 | ( ( descsz + align - 1 ) / align ) * (Elf_Xword)align; |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | //------------------------------------------------------------------------------ |
| 3167 | private: |
| 3168 | const elfio& elf_file; |
| 3169 | S* note_section; |
| 3170 | std::vector<Elf_Xword> note_start_positions; |
| 3171 | }; |
| 3172 | |
| 3173 | using note_section_accessor = note_section_accessor_template<section>; |
| 3174 | using const_note_section_accessor = |
| 3175 | note_section_accessor_template<const section>; |
| 3176 | |
| 3177 | } // namespace ELFIO |
| 3178 | |
| 3179 | #endif // ELFIO_NOTE_HPP |
| 3180 | |
| 3181 | /*** End of inlined file: elfio_note.hpp ***/ |
| 3182 | |
| 3183 | |
| 3184 | /*** Start of inlined file: elfio_relocation.hpp ***/ |
| 3185 | #ifndef ELFIO_RELOCATION_HPP |
| 3186 | #define ELFIO_RELOCATION_HPP |
| 3187 | |
| 3188 | namespace ELFIO { |
| 3189 | |
| 3190 | template <typename T> struct get_sym_and_type; |
| 3191 | template <> struct get_sym_and_type<Elf32_Rel> |
| 3192 | { |
| 3193 | static int get_r_sym( Elf_Xword info ) |
| 3194 | { |
| 3195 | return ELF32_R_SYM( (Elf_Word)info ); |
| 3196 | } |
| 3197 | static int get_r_type( Elf_Xword info ) |
| 3198 | { |
| 3199 | return ELF32_R_TYPE( (Elf_Word)info ); |
| 3200 | } |
| 3201 | }; |
| 3202 | template <> struct get_sym_and_type<Elf32_Rela> |
| 3203 | { |
| 3204 | static int get_r_sym( Elf_Xword info ) |
| 3205 | { |
| 3206 | return ELF32_R_SYM( (Elf_Word)info ); |
| 3207 | } |
| 3208 | static int get_r_type( Elf_Xword info ) |
| 3209 | { |
| 3210 | return ELF32_R_TYPE( (Elf_Word)info ); |
| 3211 | } |
| 3212 | }; |
| 3213 | template <> struct get_sym_and_type<Elf64_Rel> |
| 3214 | { |
| 3215 | static int get_r_sym( Elf_Xword info ) { return ELF64_R_SYM( info ); } |
| 3216 | static int get_r_type( Elf_Xword info ) { return ELF64_R_TYPE( info ); } |
| 3217 | }; |
| 3218 | template <> struct get_sym_and_type<Elf64_Rela> |
| 3219 | { |
| 3220 | static int get_r_sym( Elf_Xword info ) { return ELF64_R_SYM( info ); } |
| 3221 | static int get_r_type( Elf_Xword info ) { return ELF64_R_TYPE( info ); } |
| 3222 | }; |
| 3223 | |
| 3224 | //------------------------------------------------------------------------------ |
| 3225 | template <class S> class relocation_section_accessor_template |
| 3226 | { |
| 3227 | public: |
| 3228 | //------------------------------------------------------------------------------ |
| 3229 | relocation_section_accessor_template( const elfio& elf_file_, S* section_ ) |
| 3230 | : elf_file( elf_file_ ), relocation_section( section_ ) |
| 3231 | { |
| 3232 | } |
| 3233 | |
| 3234 | //------------------------------------------------------------------------------ |
| 3235 | Elf_Xword get_entries_num() const |
| 3236 | { |
| 3237 | Elf_Xword nRet = 0; |
| 3238 | |
| 3239 | if ( 0 != relocation_section->get_entry_size() ) { |
| 3240 | nRet = relocation_section->get_size() / |
| 3241 | relocation_section->get_entry_size(); |
| 3242 | } |
| 3243 | |
| 3244 | return nRet; |
| 3245 | } |
| 3246 | |
| 3247 | //------------------------------------------------------------------------------ |
| 3248 | bool get_entry( Elf_Xword index, |
| 3249 | Elf64_Addr& offset, |
| 3250 | Elf_Word& symbol, |
| 3251 | Elf_Word& type, |
| 3252 | Elf_Sxword& addend ) const |
| 3253 | { |
| 3254 | if ( index >= get_entries_num() ) { // Is index valid |
| 3255 | return false; |
| 3256 | } |
| 3257 | |
| 3258 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3259 | if ( SHT_REL == relocation_section->get_type() ) { |
| 3260 | generic_get_entry_rel<Elf32_Rel>( index, offset, symbol, type, |
| 3261 | addend ); |
| 3262 | } |
| 3263 | else if ( SHT_RELA == relocation_section->get_type() ) { |
| 3264 | generic_get_entry_rela<Elf32_Rela>( index, offset, symbol, type, |
| 3265 | addend ); |
| 3266 | } |
| 3267 | } |
| 3268 | else { |
| 3269 | if ( SHT_REL == relocation_section->get_type() ) { |
| 3270 | generic_get_entry_rel<Elf64_Rel>( index, offset, symbol, type, |
| 3271 | addend ); |
| 3272 | } |
| 3273 | else if ( SHT_RELA == relocation_section->get_type() ) { |
| 3274 | generic_get_entry_rela<Elf64_Rela>( index, offset, symbol, type, |
| 3275 | addend ); |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | return true; |
| 3280 | } |
| 3281 | |
| 3282 | //------------------------------------------------------------------------------ |
| 3283 | bool get_entry( Elf_Xword index, |
| 3284 | Elf64_Addr& offset, |
| 3285 | Elf64_Addr& symbolValue, |
| 3286 | std::string& symbolName, |
| 3287 | Elf_Word& type, |
| 3288 | Elf_Sxword& addend, |
| 3289 | Elf_Sxword& calcValue ) const |
| 3290 | { |
| 3291 | // Do regular job |
| 3292 | Elf_Word symbol; |
| 3293 | bool ret = get_entry( index, offset, symbol, type, addend ); |
| 3294 | |
| 3295 | // Find the symbol |
| 3296 | Elf_Xword size; |
| 3297 | unsigned char bind; |
| 3298 | unsigned char symbolType; |
| 3299 | Elf_Half section; |
| 3300 | unsigned char other; |
| 3301 | |
| 3302 | symbol_section_accessor symbols( |
| 3303 | elf_file, elf_file.sections[get_symbol_table_index()] ); |
| 3304 | ret = ret && symbols.get_symbol( symbol, symbolName, symbolValue, size, |
| 3305 | bind, symbolType, section, other ); |
| 3306 | |
| 3307 | if ( ret ) { // Was it successful? |
| 3308 | switch ( type ) { |
| 3309 | case R_386_NONE: // none |
| 3310 | calcValue = 0; |
| 3311 | break; |
| 3312 | case R_386_32: // S + A |
| 3313 | calcValue = symbolValue + addend; |
| 3314 | break; |
| 3315 | case R_386_PC32: // S + A - P |
| 3316 | calcValue = symbolValue + addend - offset; |
| 3317 | break; |
| 3318 | case R_386_GOT32: // G + A - P |
| 3319 | calcValue = 0; |
| 3320 | break; |
| 3321 | case R_386_PLT32: // L + A - P |
| 3322 | calcValue = 0; |
| 3323 | break; |
| 3324 | case R_386_COPY: // none |
| 3325 | calcValue = 0; |
| 3326 | break; |
| 3327 | case R_386_GLOB_DAT: // S |
| 3328 | case R_386_JMP_SLOT: // S |
| 3329 | calcValue = symbolValue; |
| 3330 | break; |
| 3331 | case R_386_RELATIVE: // B + A |
| 3332 | calcValue = addend; |
| 3333 | break; |
| 3334 | case R_386_GOTOFF: // S + A - GOT |
| 3335 | calcValue = 0; |
| 3336 | break; |
| 3337 | case R_386_GOTPC: // GOT + A - P |
| 3338 | calcValue = 0; |
| 3339 | break; |
| 3340 | default: // Not recognized symbol! |
| 3341 | calcValue = 0; |
| 3342 | break; |
| 3343 | } |
| 3344 | } |
| 3345 | |
| 3346 | return ret; |
| 3347 | } |
| 3348 | |
| 3349 | //------------------------------------------------------------------------------ |
| 3350 | bool set_entry( Elf_Xword index, |
| 3351 | Elf64_Addr offset, |
| 3352 | Elf_Word symbol, |
| 3353 | Elf_Word type, |
| 3354 | Elf_Sxword addend ) |
| 3355 | { |
| 3356 | if ( index >= get_entries_num() ) { // Is index valid |
| 3357 | return false; |
| 3358 | } |
| 3359 | |
| 3360 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3361 | if ( SHT_REL == relocation_section->get_type() ) { |
| 3362 | generic_set_entry_rel<Elf32_Rel>( index, offset, symbol, type, |
| 3363 | addend ); |
| 3364 | } |
| 3365 | else if ( SHT_RELA == relocation_section->get_type() ) { |
| 3366 | generic_set_entry_rela<Elf32_Rela>( index, offset, symbol, type, |
| 3367 | addend ); |
| 3368 | } |
| 3369 | } |
| 3370 | else { |
| 3371 | if ( SHT_REL == relocation_section->get_type() ) { |
| 3372 | generic_set_entry_rel<Elf64_Rel>( index, offset, symbol, type, |
| 3373 | addend ); |
| 3374 | } |
| 3375 | else if ( SHT_RELA == relocation_section->get_type() ) { |
| 3376 | generic_set_entry_rela<Elf64_Rela>( index, offset, symbol, type, |
| 3377 | addend ); |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | return true; |
| 3382 | } |
| 3383 | |
| 3384 | //------------------------------------------------------------------------------ |
| 3385 | void add_entry( Elf64_Addr offset, Elf_Xword info ) |
| 3386 | { |
| 3387 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3388 | generic_add_entry<Elf32_Rel>( offset, info ); |
| 3389 | } |
| 3390 | else { |
| 3391 | generic_add_entry<Elf64_Rel>( offset, info ); |
| 3392 | } |
| 3393 | } |
| 3394 | |
| 3395 | //------------------------------------------------------------------------------ |
| 3396 | void add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type ) |
| 3397 | { |
| 3398 | Elf_Xword info; |
| 3399 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3400 | info = ELF32_R_INFO( (Elf_Xword)symbol, type ); |
| 3401 | } |
| 3402 | else { |
| 3403 | info = ELF64_R_INFO( (Elf_Xword)symbol, type ); |
| 3404 | } |
| 3405 | |
| 3406 | add_entry( offset, info ); |
| 3407 | } |
| 3408 | |
| 3409 | //------------------------------------------------------------------------------ |
| 3410 | void add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend ) |
| 3411 | { |
| 3412 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3413 | generic_add_entry<Elf32_Rela>( offset, info, addend ); |
| 3414 | } |
| 3415 | else { |
| 3416 | generic_add_entry<Elf64_Rela>( offset, info, addend ); |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | //------------------------------------------------------------------------------ |
| 3421 | void add_entry( Elf64_Addr offset, |
| 3422 | Elf_Word symbol, |
| 3423 | unsigned char type, |
| 3424 | Elf_Sxword addend ) |
| 3425 | { |
| 3426 | Elf_Xword info; |
| 3427 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3428 | info = ELF32_R_INFO( (Elf_Xword)symbol, type ); |
| 3429 | } |
| 3430 | else { |
| 3431 | info = ELF64_R_INFO( (Elf_Xword)symbol, type ); |
| 3432 | } |
| 3433 | |
| 3434 | add_entry( offset, info, addend ); |
| 3435 | } |
| 3436 | |
| 3437 | //------------------------------------------------------------------------------ |
| 3438 | void add_entry( string_section_accessor str_writer, |
| 3439 | const char* str, |
| 3440 | symbol_section_accessor sym_writer, |
| 3441 | Elf64_Addr value, |
| 3442 | Elf_Word size, |
| 3443 | unsigned char sym_info, |
| 3444 | unsigned char other, |
| 3445 | Elf_Half shndx, |
| 3446 | Elf64_Addr offset, |
| 3447 | unsigned char type ) |
| 3448 | { |
| 3449 | Elf_Word str_index = str_writer.add_string( str ); |
| 3450 | Elf_Word sym_index = sym_writer.add_symbol( str_index, value, size, |
| 3451 | sym_info, other, shndx ); |
| 3452 | add_entry( offset, sym_index, type ); |
| 3453 | } |
| 3454 | |
| 3455 | //------------------------------------------------------------------------------ |
| 3456 | void swap_symbols( Elf_Xword first, Elf_Xword second ) |
| 3457 | { |
| 3458 | Elf64_Addr offset; |
| 3459 | Elf_Word symbol; |
| 3460 | Elf_Word rtype; |
| 3461 | Elf_Sxword addend; |
| 3462 | for ( Elf_Word i = 0; i < get_entries_num(); i++ ) { |
| 3463 | get_entry( i, offset, symbol, rtype, addend ); |
| 3464 | if ( symbol == first ) { |
| 3465 | set_entry( i, offset, (Elf_Word)second, rtype, addend ); |
| 3466 | } |
| 3467 | if ( symbol == second ) { |
| 3468 | set_entry( i, offset, (Elf_Word)first, rtype, addend ); |
| 3469 | } |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | //------------------------------------------------------------------------------ |
| 3474 | private: |
| 3475 | //------------------------------------------------------------------------------ |
| 3476 | Elf_Half get_symbol_table_index() const |
| 3477 | { |
| 3478 | return (Elf_Half)relocation_section->get_link(); |
| 3479 | } |
| 3480 | |
| 3481 | //------------------------------------------------------------------------------ |
| 3482 | template <class T> |
| 3483 | void generic_get_entry_rel( Elf_Xword index, |
| 3484 | Elf64_Addr& offset, |
| 3485 | Elf_Word& symbol, |
| 3486 | Elf_Word& type, |
| 3487 | Elf_Sxword& addend ) const |
| 3488 | { |
| 3489 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3490 | |
| 3491 | const T* pEntry = reinterpret_cast<const T*>( |
| 3492 | relocation_section->get_data() + |
| 3493 | index * relocation_section->get_entry_size() ); |
| 3494 | offset = convertor( pEntry->r_offset ); |
| 3495 | Elf_Xword tmp = convertor( pEntry->r_info ); |
| 3496 | symbol = get_sym_and_type<T>::get_r_sym( tmp ); |
| 3497 | type = get_sym_and_type<T>::get_r_type( tmp ); |
| 3498 | addend = 0; |
| 3499 | } |
| 3500 | |
| 3501 | //------------------------------------------------------------------------------ |
| 3502 | template <class T> |
| 3503 | void generic_get_entry_rela( Elf_Xword index, |
| 3504 | Elf64_Addr& offset, |
| 3505 | Elf_Word& symbol, |
| 3506 | Elf_Word& type, |
| 3507 | Elf_Sxword& addend ) const |
| 3508 | { |
| 3509 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3510 | |
| 3511 | const T* pEntry = reinterpret_cast<const T*>( |
| 3512 | relocation_section->get_data() + |
| 3513 | index * relocation_section->get_entry_size() ); |
| 3514 | offset = convertor( pEntry->r_offset ); |
| 3515 | Elf_Xword tmp = convertor( pEntry->r_info ); |
| 3516 | symbol = get_sym_and_type<T>::get_r_sym( tmp ); |
| 3517 | type = get_sym_and_type<T>::get_r_type( tmp ); |
| 3518 | addend = convertor( pEntry->r_addend ); |
| 3519 | } |
| 3520 | |
| 3521 | //------------------------------------------------------------------------------ |
| 3522 | template <class T> |
| 3523 | void generic_set_entry_rel( Elf_Xword index, |
| 3524 | Elf64_Addr offset, |
| 3525 | Elf_Word symbol, |
| 3526 | Elf_Word type, |
| 3527 | Elf_Sxword ) |
| 3528 | { |
| 3529 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3530 | |
| 3531 | T* pEntry = const_cast<T*>( reinterpret_cast<const T*>( |
| 3532 | relocation_section->get_data() + |
| 3533 | index * relocation_section->get_entry_size() ) ); |
| 3534 | |
| 3535 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3536 | pEntry->r_info = ELF32_R_INFO( (Elf_Xword)symbol, type ); |
| 3537 | } |
| 3538 | else { |
| 3539 | pEntry->r_info = ELF64_R_INFO( (Elf_Xword)symbol, type ); |
| 3540 | } |
| 3541 | pEntry->r_offset = offset; |
| 3542 | pEntry->r_offset = convertor( pEntry->r_offset ); |
| 3543 | pEntry->r_info = convertor( pEntry->r_info ); |
| 3544 | } |
| 3545 | |
| 3546 | //------------------------------------------------------------------------------ |
| 3547 | template <class T> |
| 3548 | void generic_set_entry_rela( Elf_Xword index, |
| 3549 | Elf64_Addr offset, |
| 3550 | Elf_Word symbol, |
| 3551 | Elf_Word type, |
| 3552 | Elf_Sxword addend ) |
| 3553 | { |
| 3554 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3555 | |
| 3556 | T* pEntry = const_cast<T*>( reinterpret_cast<const T*>( |
| 3557 | relocation_section->get_data() + |
| 3558 | index * relocation_section->get_entry_size() ) ); |
| 3559 | |
| 3560 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3561 | pEntry->r_info = ELF32_R_INFO( (Elf_Xword)symbol, type ); |
| 3562 | } |
| 3563 | else { |
| 3564 | pEntry->r_info = ELF64_R_INFO( (Elf_Xword)symbol, type ); |
| 3565 | } |
| 3566 | pEntry->r_offset = offset; |
| 3567 | pEntry->r_addend = addend; |
| 3568 | pEntry->r_offset = convertor( pEntry->r_offset ); |
| 3569 | pEntry->r_info = convertor( pEntry->r_info ); |
| 3570 | pEntry->r_addend = convertor( pEntry->r_addend ); |
| 3571 | } |
| 3572 | |
| 3573 | //------------------------------------------------------------------------------ |
| 3574 | template <class T> |
| 3575 | void generic_add_entry( Elf64_Addr offset, Elf_Xword info ) |
| 3576 | { |
| 3577 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3578 | |
| 3579 | T entry; |
| 3580 | entry.r_offset = offset; |
| 3581 | entry.r_info = info; |
| 3582 | entry.r_offset = convertor( entry.r_offset ); |
| 3583 | entry.r_info = convertor( entry.r_info ); |
| 3584 | |
| 3585 | relocation_section->append_data( reinterpret_cast<char*>( &entry ), |
| 3586 | sizeof( entry ) ); |
| 3587 | } |
| 3588 | |
| 3589 | //------------------------------------------------------------------------------ |
| 3590 | template <class T> |
| 3591 | void |
| 3592 | generic_add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend ) |
| 3593 | { |
| 3594 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3595 | |
| 3596 | T entry; |
| 3597 | entry.r_offset = offset; |
| 3598 | entry.r_info = info; |
| 3599 | entry.r_addend = addend; |
| 3600 | entry.r_offset = convertor( entry.r_offset ); |
| 3601 | entry.r_info = convertor( entry.r_info ); |
| 3602 | entry.r_addend = convertor( entry.r_addend ); |
| 3603 | |
| 3604 | relocation_section->append_data( reinterpret_cast<char*>( &entry ), |
| 3605 | sizeof( entry ) ); |
| 3606 | } |
| 3607 | |
| 3608 | //------------------------------------------------------------------------------ |
| 3609 | private: |
| 3610 | const elfio& elf_file; |
| 3611 | S* relocation_section; |
| 3612 | }; |
| 3613 | |
| 3614 | using relocation_section_accessor = |
| 3615 | relocation_section_accessor_template<section>; |
| 3616 | using const_relocation_section_accessor = |
| 3617 | relocation_section_accessor_template<const section>; |
| 3618 | |
| 3619 | } // namespace ELFIO |
| 3620 | |
| 3621 | #endif // ELFIO_RELOCATION_HPP |
| 3622 | |
| 3623 | /*** End of inlined file: elfio_relocation.hpp ***/ |
| 3624 | |
| 3625 | |
| 3626 | /*** Start of inlined file: elfio_dynamic.hpp ***/ |
| 3627 | #ifndef ELFIO_DYNAMIC_HPP |
| 3628 | #define ELFIO_DYNAMIC_HPP |
| 3629 | |
| 3630 | namespace ELFIO { |
| 3631 | |
| 3632 | //------------------------------------------------------------------------------ |
| 3633 | template <class S> class dynamic_section_accessor_template |
| 3634 | { |
| 3635 | public: |
| 3636 | //------------------------------------------------------------------------------ |
| 3637 | dynamic_section_accessor_template( const elfio& elf_file_, S* section_ ) |
| 3638 | : elf_file( elf_file_ ), dynamic_section( section_ ) |
| 3639 | { |
| 3640 | } |
| 3641 | |
| 3642 | //------------------------------------------------------------------------------ |
| 3643 | Elf_Xword get_entries_num() const |
| 3644 | { |
| 3645 | Elf_Xword nRet = 0; |
| 3646 | |
| 3647 | if ( 0 != dynamic_section->get_entry_size() ) { |
| 3648 | nRet = |
| 3649 | dynamic_section->get_size() / dynamic_section->get_entry_size(); |
| 3650 | } |
| 3651 | |
| 3652 | return nRet; |
| 3653 | } |
| 3654 | |
| 3655 | //------------------------------------------------------------------------------ |
| 3656 | bool get_entry( Elf_Xword index, |
| 3657 | Elf_Xword& tag, |
| 3658 | Elf_Xword& value, |
| 3659 | std::string& str ) const |
| 3660 | { |
| 3661 | if ( index >= get_entries_num() ) { // Is index valid |
| 3662 | return false; |
| 3663 | } |
| 3664 | |
| 3665 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3666 | generic_get_entry_dyn<Elf32_Dyn>( index, tag, value ); |
| 3667 | } |
| 3668 | else { |
| 3669 | generic_get_entry_dyn<Elf64_Dyn>( index, tag, value ); |
| 3670 | } |
| 3671 | |
| 3672 | // If the tag may have a string table reference, prepare the string |
| 3673 | if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || |
| 3674 | tag == DT_RUNPATH ) { |
| 3675 | string_section_accessor strsec = |
| 3676 | elf_file.sections[get_string_table_index()]; |
| 3677 | const char* result = strsec.get_string( value ); |
| 3678 | if ( 0 == result ) { |
| 3679 | str.clear(); |
| 3680 | return false; |
| 3681 | } |
| 3682 | str = result; |
| 3683 | } |
| 3684 | else { |
| 3685 | str.clear(); |
| 3686 | } |
| 3687 | |
| 3688 | return true; |
| 3689 | } |
| 3690 | |
| 3691 | //------------------------------------------------------------------------------ |
| 3692 | void add_entry( Elf_Xword tag, Elf_Xword value ) |
| 3693 | { |
| 3694 | if ( elf_file.get_class() == ELFCLASS32 ) { |
| 3695 | generic_add_entry<Elf32_Dyn>( tag, value ); |
| 3696 | } |
| 3697 | else { |
| 3698 | generic_add_entry<Elf64_Dyn>( tag, value ); |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | //------------------------------------------------------------------------------ |
| 3703 | void add_entry( Elf_Xword tag, const std::string& str ) |
| 3704 | { |
| 3705 | string_section_accessor strsec = |
| 3706 | elf_file.sections[get_string_table_index()]; |
| 3707 | Elf_Xword value = strsec.add_string( str ); |
| 3708 | add_entry( tag, value ); |
| 3709 | } |
| 3710 | |
| 3711 | //------------------------------------------------------------------------------ |
| 3712 | private: |
| 3713 | //------------------------------------------------------------------------------ |
| 3714 | Elf_Half get_string_table_index() const |
| 3715 | { |
| 3716 | return (Elf_Half)dynamic_section->get_link(); |
| 3717 | } |
| 3718 | |
| 3719 | //------------------------------------------------------------------------------ |
| 3720 | template <class T> |
| 3721 | void generic_get_entry_dyn( Elf_Xword index, |
| 3722 | Elf_Xword& tag, |
| 3723 | Elf_Xword& value ) const |
| 3724 | { |
| 3725 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3726 | |
| 3727 | // Check unusual case when dynamic section has no data |
| 3728 | if ( dynamic_section->get_data() == 0 || |
| 3729 | ( index + 1 ) * dynamic_section->get_entry_size() > |
| 3730 | dynamic_section->get_size() ) { |
| 3731 | tag = DT_NULL; |
| 3732 | value = 0; |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | const T* pEntry = reinterpret_cast<const T*>( |
| 3737 | dynamic_section->get_data() + |
| 3738 | index * dynamic_section->get_entry_size() ); |
| 3739 | tag = convertor( pEntry->d_tag ); |
| 3740 | switch ( tag ) { |
| 3741 | case DT_NULL: |
| 3742 | case DT_SYMBOLIC: |
| 3743 | case DT_TEXTREL: |
| 3744 | case DT_BIND_NOW: |
| 3745 | value = 0; |
| 3746 | break; |
| 3747 | case DT_NEEDED: |
| 3748 | case DT_PLTRELSZ: |
| 3749 | case DT_RELASZ: |
| 3750 | case DT_RELAENT: |
| 3751 | case DT_STRSZ: |
| 3752 | case DT_SYMENT: |
| 3753 | case DT_SONAME: |
| 3754 | case DT_RPATH: |
| 3755 | case DT_RELSZ: |
| 3756 | case DT_RELENT: |
| 3757 | case DT_PLTREL: |
| 3758 | case DT_INIT_ARRAYSZ: |
| 3759 | case DT_FINI_ARRAYSZ: |
| 3760 | case DT_RUNPATH: |
| 3761 | case DT_FLAGS: |
| 3762 | case DT_PREINIT_ARRAYSZ: |
| 3763 | value = convertor( pEntry->d_un.d_val ); |
| 3764 | break; |
| 3765 | case DT_PLTGOT: |
| 3766 | case DT_HASH: |
| 3767 | case DT_STRTAB: |
| 3768 | case DT_SYMTAB: |
| 3769 | case DT_RELA: |
| 3770 | case DT_INIT: |
| 3771 | case DT_FINI: |
| 3772 | case DT_REL: |
| 3773 | case DT_DEBUG: |
| 3774 | case DT_JMPREL: |
| 3775 | case DT_INIT_ARRAY: |
| 3776 | case DT_FINI_ARRAY: |
| 3777 | case DT_PREINIT_ARRAY: |
| 3778 | default: |
| 3779 | value = convertor( pEntry->d_un.d_ptr ); |
| 3780 | break; |
| 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | //------------------------------------------------------------------------------ |
| 3785 | template <class T> void generic_add_entry( Elf_Xword tag, Elf_Xword value ) |
| 3786 | { |
| 3787 | const endianess_convertor& convertor = elf_file.get_convertor(); |
| 3788 | |
| 3789 | T entry; |
| 3790 | |
| 3791 | switch ( tag ) { |
| 3792 | case DT_NULL: |
| 3793 | case DT_SYMBOLIC: |
| 3794 | case DT_TEXTREL: |
| 3795 | case DT_BIND_NOW: |
| 3796 | value = 0; |
| 3797 | case DT_NEEDED: |
| 3798 | case DT_PLTRELSZ: |
| 3799 | case DT_RELASZ: |
| 3800 | case DT_RELAENT: |
| 3801 | case DT_STRSZ: |
| 3802 | case DT_SYMENT: |
| 3803 | case DT_SONAME: |
| 3804 | case DT_RPATH: |
| 3805 | case DT_RELSZ: |
| 3806 | case DT_RELENT: |
| 3807 | case DT_PLTREL: |
| 3808 | case DT_INIT_ARRAYSZ: |
| 3809 | case DT_FINI_ARRAYSZ: |
| 3810 | case DT_RUNPATH: |
| 3811 | case DT_FLAGS: |
| 3812 | case DT_PREINIT_ARRAYSZ: |
| 3813 | entry.d_un.d_val = convertor( value ); |
| 3814 | break; |
| 3815 | case DT_PLTGOT: |
| 3816 | case DT_HASH: |
| 3817 | case DT_STRTAB: |
| 3818 | case DT_SYMTAB: |
| 3819 | case DT_RELA: |
| 3820 | case DT_INIT: |
| 3821 | case DT_FINI: |
| 3822 | case DT_REL: |
| 3823 | case DT_DEBUG: |
| 3824 | case DT_JMPREL: |
| 3825 | case DT_INIT_ARRAY: |
| 3826 | case DT_FINI_ARRAY: |
| 3827 | case DT_PREINIT_ARRAY: |
| 3828 | default: |
| 3829 | entry.d_un.d_ptr = convertor( value ); |
| 3830 | break; |
| 3831 | } |
| 3832 | |
| 3833 | entry.d_tag = convertor( tag ); |
| 3834 | |
| 3835 | dynamic_section->append_data( reinterpret_cast<char*>( &entry ), |
| 3836 | sizeof( entry ) ); |
| 3837 | } |
| 3838 | |
| 3839 | //------------------------------------------------------------------------------ |
| 3840 | private: |
| 3841 | const elfio& elf_file; |
| 3842 | S* dynamic_section; |
| 3843 | }; |
| 3844 | |
| 3845 | using dynamic_section_accessor = dynamic_section_accessor_template<section>; |
| 3846 | using const_dynamic_section_accessor = |
| 3847 | dynamic_section_accessor_template<const section>; |
| 3848 | |
| 3849 | } // namespace ELFIO |
| 3850 | |
| 3851 | #endif // ELFIO_DYNAMIC_HPP |
| 3852 | |
| 3853 | /*** End of inlined file: elfio_dynamic.hpp ***/ |
| 3854 | |
| 3855 | |
| 3856 | /*** Start of inlined file: elfio_modinfo.hpp ***/ |
| 3857 | #ifndef ELFIO_MODINFO_HPP |
| 3858 | #define ELFIO_MODINFO_HPP |
| 3859 | |
| 3860 | #include <string> |
| 3861 | #include <vector> |
| 3862 | |
| 3863 | namespace ELFIO { |
| 3864 | |
| 3865 | //------------------------------------------------------------------------------ |
| 3866 | template <class S> class modinfo_section_accessor_template |
| 3867 | { |
| 3868 | public: |
| 3869 | //------------------------------------------------------------------------------ |
| 3870 | modinfo_section_accessor_template( S* section_ ) |
| 3871 | : modinfo_section( section_ ) |
| 3872 | { |
| 3873 | process_section(); |
| 3874 | } |
| 3875 | |
| 3876 | //------------------------------------------------------------------------------ |
| 3877 | Elf_Word get_attribute_num() const { return (Elf_Word)content.size(); } |
| 3878 | |
| 3879 | //------------------------------------------------------------------------------ |
| 3880 | bool |
| 3881 | get_attribute( Elf_Word no, std::string& field, std::string& value ) const |
| 3882 | { |
| 3883 | if ( no < content.size() ) { |
| 3884 | field = content[no].first; |
| 3885 | value = content[no].second; |
| 3886 | return true; |
| 3887 | } |
| 3888 | |
| 3889 | return false; |
| 3890 | } |
| 3891 | |
| 3892 | //------------------------------------------------------------------------------ |
| 3893 | bool get_attribute( std::string field_name, std::string& value ) const |
| 3894 | { |
| 3895 | for ( auto i = content.begin(); i != content.end(); i++ ) { |
| 3896 | if ( field_name == i->first ) { |
| 3897 | value = i->second; |
| 3898 | return true; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | return false; |
| 3903 | } |
| 3904 | |
| 3905 | //------------------------------------------------------------------------------ |
| 3906 | Elf_Word add_attribute( std::string field, std::string value ) |
| 3907 | { |
| 3908 | Elf_Word current_position = 0; |
| 3909 | |
| 3910 | if ( modinfo_section ) { |
| 3911 | // Strings are addeded to the end of the current section data |
| 3912 | current_position = (Elf_Word)modinfo_section->get_size(); |
| 3913 | |
| 3914 | std::string attribute = field + "=" + value; |
| 3915 | |
| 3916 | modinfo_section->append_data( attribute + '\0' ); |
| 3917 | content.push_back( |
| 3918 | std::pair<std::string, std::string>( field, value ) ); |
| 3919 | } |
| 3920 | |
| 3921 | return current_position; |
| 3922 | } |
| 3923 | |
| 3924 | //------------------------------------------------------------------------------ |
| 3925 | private: |
| 3926 | void process_section() |
| 3927 | { |
| 3928 | const char* pdata = modinfo_section->get_data(); |
| 3929 | if ( pdata ) { |
| 3930 | ELFIO::Elf_Xword i = 0; |
| 3931 | while ( i < modinfo_section->get_size() ) { |
| 3932 | while ( i < modinfo_section->get_size() && !pdata[i] ) |
| 3933 | i++; |
| 3934 | if ( i < modinfo_section->get_size() ) { |
| 3935 | std::string info = pdata + i; |
| 3936 | size_t loc = info.find( '=' ); |
| 3937 | std::pair<std::string, std::string> attribute( |
| 3938 | info.substr( 0, loc ), info.substr( loc + 1 ) ); |
| 3939 | |
| 3940 | content.push_back( attribute ); |
| 3941 | |
| 3942 | i += info.length(); |
| 3943 | } |
| 3944 | } |
| 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | //------------------------------------------------------------------------------ |
| 3949 | private: |
| 3950 | S* modinfo_section; |
| 3951 | std::vector<std::pair<std::string, std::string>> content; |
| 3952 | }; |
| 3953 | |
| 3954 | using modinfo_section_accessor = modinfo_section_accessor_template<section>; |
| 3955 | using const_modinfo_section_accessor = |
| 3956 | modinfo_section_accessor_template<const section>; |
| 3957 | |
| 3958 | } // namespace ELFIO |
| 3959 | |
| 3960 | #endif // ELFIO_MODINFO_HPP |
| 3961 | |
| 3962 | /*** End of inlined file: elfio_modinfo.hpp ***/ |
| 3963 | |
| 3964 | #ifdef _MSC_VER |
| 3965 | #pragma warning( pop ) |
| 3966 | #endif |
| 3967 | |
| 3968 | #endif // ELFIO_HPP |
| 3969 | |
| 3970 | /*** End of inlined file: elfio.hpp ***/ |
| 3971 | |
| 3972 | |
| 3973 | namespace ELFIO { |
| 3974 | |
| 3975 | static struct class_table_t |
| 3976 | { |
| 3977 | const char key; |
| 3978 | const char* str; |
| 3979 | } class_table[] = { |
| 3980 | { ELFCLASS32, "ELF32" }, |
| 3981 | { ELFCLASS64, "ELF64" }, |
| 3982 | }; |
| 3983 | |
| 3984 | static struct endian_table_t |
| 3985 | { |
| 3986 | const char key; |
| 3987 | const char* str; |
| 3988 | } endian_table[] = { |
| 3989 | { ELFDATANONE, "None" }, |
| 3990 | { ELFDATA2LSB, "Little endian" }, |
| 3991 | { ELFDATA2MSB, "Big endian" }, |
| 3992 | }; |
| 3993 | |
| 3994 | static struct version_table_t |
| 3995 | { |
| 3996 | const Elf64_Word key; |
| 3997 | const char* str; |
| 3998 | } version_table[] = { |
| 3999 | { EV_NONE, "None" }, |
| 4000 | { EV_CURRENT, "Current" }, |
| 4001 | }; |
| 4002 | |
| 4003 | static struct type_table_t |
| 4004 | { |
| 4005 | const Elf32_Half key; |
| 4006 | const char* str; |
| 4007 | } type_table[] = { |
| 4008 | { ET_NONE, "No file type" }, { ET_REL, "Relocatable file" }, |
| 4009 | { ET_EXEC, "Executable file" }, { ET_DYN, "Shared object file" }, |
| 4010 | { ET_CORE, "Core file" }, |
| 4011 | }; |
| 4012 | |
| 4013 | static struct machine_table_t |
| 4014 | { |
| 4015 | const Elf64_Half key; |
| 4016 | const char* str; |
| 4017 | } machine_table[] = { |
| 4018 | { EM_NONE, "No machine" }, |
| 4019 | { EM_M32, "AT&T WE 32100" }, |
| 4020 | { EM_SPARC, "SUN SPARC" }, |
| 4021 | { EM_386, "Intel 80386" }, |
| 4022 | { EM_68K, "Motorola m68k family" }, |
| 4023 | { EM_88K, "Motorola m88k family" }, |
| 4024 | { EM_486, "Intel 80486// Reserved for future use" }, |
| 4025 | { EM_860, "Intel 80860" }, |
| 4026 | { EM_MIPS, "MIPS R3000 (officially, big-endian only)" }, |
| 4027 | { EM_S370, "IBM System/370" }, |
| 4028 | { EM_MIPS_RS3_LE, |
| 4029 | "MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated" }, |
| 4030 | { EM_res011, "Reserved" }, |
| 4031 | { EM_res012, "Reserved" }, |
| 4032 | { EM_res013, "Reserved" }, |
| 4033 | { EM_res014, "Reserved" }, |
| 4034 | { EM_PARISC, "HPPA" }, |
| 4035 | { EM_res016, "Reserved" }, |
| 4036 | { EM_VPP550, "Fujitsu VPP500" }, |
| 4037 | { EM_SPARC32PLUS, "Sun's v8plus" }, |
| 4038 | { EM_960, "Intel 80960" }, |
| 4039 | { EM_PPC, "PowerPC" }, |
| 4040 | { EM_PPC64, "64-bit PowerPC" }, |
| 4041 | { EM_S390, "IBM S/390" }, |
| 4042 | { EM_SPU, "Sony/Toshiba/IBM SPU" }, |
| 4043 | { EM_res024, "Reserved" }, |
| 4044 | { EM_res025, "Reserved" }, |
| 4045 | { EM_res026, "Reserved" }, |
| 4046 | { EM_res027, "Reserved" }, |
| 4047 | { EM_res028, "Reserved" }, |
| 4048 | { EM_res029, "Reserved" }, |
| 4049 | { EM_res030, "Reserved" }, |
| 4050 | { EM_res031, "Reserved" }, |
| 4051 | { EM_res032, "Reserved" }, |
| 4052 | { EM_res033, "Reserved" }, |
| 4053 | { EM_res034, "Reserved" }, |
| 4054 | { EM_res035, "Reserved" }, |
| 4055 | { EM_V800, "NEC V800 series" }, |
| 4056 | { EM_FR20, "Fujitsu FR20" }, |
| 4057 | { EM_RH32, "TRW RH32" }, |
| 4058 | { EM_MCORE, "Motorola M*Core // May also be taken by Fujitsu MMA" }, |
| 4059 | { EM_RCE, "Old name for MCore" }, |
| 4060 | { EM_ARM, "ARM" }, |
| 4061 | { EM_OLD_ALPHA, "Digital Alpha" }, |
| 4062 | { EM_SH, "Renesas (formerly Hitachi) / SuperH SH" }, |
| 4063 | { EM_SPARCV9, "SPARC v9 64-bit" }, |
| 4064 | { EM_TRICORE, "Siemens Tricore embedded processor" }, |
| 4065 | { EM_ARC, "ARC Cores" }, |
| 4066 | { EM_H8_300, "Renesas (formerly Hitachi) H8/300" }, |
| 4067 | { EM_H8_300H, "Renesas (formerly Hitachi) H8/300H" }, |
| 4068 | { EM_H8S, "Renesas (formerly Hitachi) H8S" }, |
| 4069 | { EM_H8_500, "Renesas (formerly Hitachi) H8/500" }, |
| 4070 | { EM_IA_64, "Intel IA-64 Processor" }, |
| 4071 | { EM_MIPS_X, "Stanford MIPS-X" }, |
| 4072 | { EM_COLDFIRE, "Motorola Coldfire" }, |
| 4073 | { EM_68HC12, "Motorola M68HC12" }, |
| 4074 | { EM_MMA, "Fujitsu Multimedia Accelerator" }, |
| 4075 | { EM_PCP, "Siemens PCP" }, |
| 4076 | { EM_NCPU, "Sony nCPU embedded RISC processor" }, |
| 4077 | { EM_NDR1, "Denso NDR1 microprocesspr" }, |
| 4078 | { EM_STARCORE, "Motorola Star*Core processor" }, |
| 4079 | { EM_ME16, "Toyota ME16 processor" }, |
| 4080 | { EM_ST100, "STMicroelectronics ST100 processor" }, |
| 4081 | { EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor" }, |
| 4082 | { EM_X86_64, "Advanced Micro Devices X86-64 processor" }, |
| 4083 | { EM_PDSP, "Sony DSP Processor" }, |
| 4084 | { EM_PDP10, "Digital Equipment Corp. PDP-10" }, |
| 4085 | { EM_PDP11, "Digital Equipment Corp. PDP-11" }, |
| 4086 | { EM_FX66, "Siemens FX66 microcontroller" }, |
| 4087 | { EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller" }, |
| 4088 | { EM_ST7, "STMicroelectronics ST7 8-bit microcontroller" }, |
| 4089 | { EM_68HC16, "Motorola MC68HC16 Microcontroller" }, |
| 4090 | { EM_68HC11, "Motorola MC68HC11 Microcontroller" }, |
| 4091 | { EM_68HC08, "Motorola MC68HC08 Microcontroller" }, |
| 4092 | { EM_68HC05, "Motorola MC68HC05 Microcontroller" }, |
| 4093 | { EM_SVX, "Silicon Graphics SVx" }, |
| 4094 | { EM_ST19, "STMicroelectronics ST19 8-bit cpu" }, |
| 4095 | { EM_VAX, "Digital VAX" }, |
| 4096 | { EM_CRIS, "Axis Communications 32-bit embedded processor" }, |
| 4097 | { EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu" }, |
| 4098 | { EM_FIREPATH, "Element 14 64-bit DSP processor" }, |
| 4099 | { EM_ZSP, "LSI Logic's 16-bit DSP processor" }, |
| 4100 | { EM_MMIX, "Donald Knuth's educational 64-bit processor" }, |
| 4101 | { EM_HUANY, "Harvard's machine-independent format" }, |
| 4102 | { EM_PRISM, "SiTera Prism" }, |
| 4103 | { EM_AVR, "Atmel AVR 8-bit microcontroller" }, |
| 4104 | { EM_FR30, "Fujitsu FR30" }, |
| 4105 | { EM_D10V, "Mitsubishi D10V" }, |
| 4106 | { EM_D30V, "Mitsubishi D30V" }, |
| 4107 | { EM_V850, "NEC v850" }, |
| 4108 | { EM_M32R, "Renesas M32R (formerly Mitsubishi M32R)" }, |
| 4109 | { EM_MN10300, "Matsushita MN10300" }, |
| 4110 | { EM_MN10200, "Matsushita MN10200" }, |
| 4111 | { EM_PJ, "picoJava" }, |
| 4112 | { EM_OPENRISC, "OpenRISC 32-bit embedded processor" }, |
| 4113 | { EM_ARC_A5, "ARC Cores Tangent-A5" }, |
| 4114 | { EM_XTENSA, "Tensilica Xtensa Architecture" }, |
| 4115 | { EM_VIDEOCORE, "Alphamosaic VideoCore processor" }, |
| 4116 | { EM_TMM_GPP, "Thompson Multimedia General Purpose Processor" }, |
| 4117 | { EM_NS32K, "National Semiconductor 32000 series" }, |
| 4118 | { EM_TPC, "Tenor Network TPC processor" }, |
| 4119 | { EM_SNP1K, "Trebia SNP 1000 processor" }, |
| 4120 | { EM_ST200, "STMicroelectronics ST200 microcontroller" }, |
| 4121 | { EM_IP2K, "Ubicom IP2022 micro controller" }, |
| 4122 | { EM_MAX, "MAX Processor" }, |
| 4123 | { EM_CR, "National Semiconductor CompactRISC" }, |
| 4124 | { EM_F2MC16, "Fujitsu F2MC16" }, |
| 4125 | { EM_MSP430, "TI msp430 micro controller" }, |
| 4126 | { EM_BLACKFIN, "ADI Blackfin" }, |
| 4127 | { EM_SE_C33, "S1C33 Family of Seiko Epson processors" }, |
| 4128 | { EM_SEP, "Sharp embedded microprocessor" }, |
| 4129 | { EM_ARCA, "Arca RISC Microprocessor" }, |
| 4130 | { EM_UNICORE, "Microprocessor series from PKU-Unity Ltd. and MPRC of " |
| 4131 | "Peking University" }, |
| 4132 | { EM_EXCESS, "eXcess: 16/32/64-bit configurable embedded CPU" }, |
| 4133 | { EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor" }, |
| 4134 | { EM_ALTERA_NIOS2, "Altera Nios II soft-core processor" }, |
| 4135 | { EM_CRX, "National Semiconductor CRX" }, |
| 4136 | { EM_XGATE, "Motorola XGATE embedded processor" }, |
| 4137 | { EM_C166, "Infineon C16x/XC16x processor" }, |
| 4138 | { EM_M16C, "Renesas M16C series microprocessors" }, |
| 4139 | { EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller" }, |
| 4140 | { EM_CE, "Freescale Communication Engine RISC core" }, |
| 4141 | { EM_M32C, "Renesas M32C series microprocessors" }, |
| 4142 | { EM_res121, "Reserved" }, |
| 4143 | { EM_res122, "Reserved" }, |
| 4144 | { EM_res123, "Reserved" }, |
| 4145 | { EM_res124, "Reserved" }, |
| 4146 | { EM_res125, "Reserved" }, |
| 4147 | { EM_res126, "Reserved" }, |
| 4148 | { EM_res127, "Reserved" }, |
| 4149 | { EM_res128, "Reserved" }, |
| 4150 | { EM_res129, "Reserved" }, |
| 4151 | { EM_res130, "Reserved" }, |
| 4152 | { EM_TSK3000, "Altium TSK3000 core" }, |
| 4153 | { EM_RS08, "Freescale RS08 embedded processor" }, |
| 4154 | { EM_res133, "Reserved" }, |
| 4155 | { EM_ECOG2, "Cyan Technology eCOG2 microprocessor" }, |
| 4156 | { EM_SCORE, "Sunplus Score" }, |
| 4157 | { EM_SCORE7, "Sunplus S+core7 RISC processor" }, |
| 4158 | { EM_DSP24, "New Japan Radio (NJR) 24-bit DSP Processor" }, |
| 4159 | { EM_VIDEOCORE3, "Broadcom VideoCore III processor" }, |
| 4160 | { EM_LATTICEMICO32, "RISC processor for Lattice FPGA architecture" }, |
| 4161 | { EM_SE_C17, "Seiko Epson C17 family" }, |
| 4162 | { EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family" }, |
| 4163 | { EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family" }, |
| 4164 | { EM_TI_C5500, "Texas Instruments TMS320C55x DSP family" }, |
| 4165 | { EM_res143, "Reserved" }, |
| 4166 | { EM_res144, "Reserved" }, |
| 4167 | { EM_res145, "Reserved" }, |
| 4168 | { EM_res146, "Reserved" }, |
| 4169 | { EM_res147, "Reserved" }, |
| 4170 | { EM_res148, "Reserved" }, |
| 4171 | { EM_res149, "Reserved" }, |
| 4172 | { EM_res150, "Reserved" }, |
| 4173 | { EM_res151, "Reserved" }, |
| 4174 | { EM_res152, "Reserved" }, |
| 4175 | { EM_res153, "Reserved" }, |
| 4176 | { EM_res154, "Reserved" }, |
| 4177 | { EM_res155, "Reserved" }, |
| 4178 | { EM_res156, "Reserved" }, |
| 4179 | { EM_res157, "Reserved" }, |
| 4180 | { EM_res158, "Reserved" }, |
| 4181 | { EM_res159, "Reserved" }, |
| 4182 | { EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor" }, |
| 4183 | { EM_CYPRESS_M8C, "Cypress M8C microprocessor" }, |
| 4184 | { EM_R32C, "Renesas R32C series microprocessors" }, |
| 4185 | { EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family" }, |
| 4186 | { EM_QDSP6, "QUALCOMM DSP6 Processor" }, |
| 4187 | { EM_8051, "Intel 8051 and variants" }, |
| 4188 | { EM_STXP7X, "STMicroelectronics STxP7x family" }, |
| 4189 | { EM_NDS32, |
| 4190 | "Andes Technology compact code size embedded RISC processor family" }, |
| 4191 | { EM_ECOG1, "Cyan Technology eCOG1X family" }, |
| 4192 | { EM_ECOG1X, "Cyan Technology eCOG1X family" }, |
| 4193 | { EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core Micro-controllers" }, |
| 4194 | { EM_XIMO16, "New Japan Radio (NJR) 16-bit DSP Processor" }, |
| 4195 | { EM_MANIK, "M2000 Reconfigurable RISC Microprocessor" }, |
| 4196 | { EM_CRAYNV2, "Cray Inc. NV2 vector architecture" }, |
| 4197 | { EM_RX, "Renesas RX family" }, |
| 4198 | { EM_METAG, "Imagination Technologies META processor architecture" }, |
| 4199 | { EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture" }, |
| 4200 | { EM_ECOG16, "Cyan Technology eCOG16 family" }, |
| 4201 | { EM_CR16, "National Semiconductor CompactRISC 16-bit processor" }, |
| 4202 | { EM_ETPU, "Freescale Extended Time Processing Unit" }, |
| 4203 | { EM_SLE9X, "Infineon Technologies SLE9X core" }, |
| 4204 | { EM_L1OM, "Intel L1OM" }, |
| 4205 | { EM_INTEL181, "Reserved by Intel" }, |
| 4206 | { EM_INTEL182, "Reserved by Intel" }, |
| 4207 | { EM_res183, "Reserved by ARM" }, |
| 4208 | { EM_res184, "Reserved by ARM" }, |
| 4209 | { EM_AVR32, "Atmel Corporation 32-bit microprocessor family" }, |
| 4210 | { EM_STM8, "STMicroeletronics STM8 8-bit microcontroller" }, |
| 4211 | { EM_TILE64, "Tilera TILE64 multicore architecture family" }, |
| 4212 | { EM_TILEPRO, "Tilera TILEPro multicore architecture family" }, |
| 4213 | { EM_MICROBLAZE, "Xilinx MicroBlaze 32-bit RISC soft processor core" }, |
| 4214 | { EM_CUDA, "NVIDIA CUDA architecture " }, |
| 4215 | }; |
| 4216 | |
| 4217 | static struct section_type_table_t |
| 4218 | { |
| 4219 | const Elf64_Half key; |
| 4220 | const char* str; |
| 4221 | } section_type_table[] = { |
| 4222 | { SHT_NULL, "NULL" }, |
| 4223 | { SHT_PROGBITS, "PROGBITS" }, |
| 4224 | { SHT_SYMTAB, "SYMTAB" }, |
| 4225 | { SHT_STRTAB, "STRTAB" }, |
| 4226 | { SHT_RELA, "RELA" }, |
| 4227 | { SHT_HASH, "HASH" }, |
| 4228 | { SHT_DYNAMIC, "DYNAMIC" }, |
| 4229 | { SHT_NOTE, "NOTE" }, |
| 4230 | { SHT_NOBITS, "NOBITS" }, |
| 4231 | { SHT_REL, "REL" }, |
| 4232 | { SHT_SHLIB, "SHLIB" }, |
| 4233 | { SHT_DYNSYM, "DYNSYM" }, |
| 4234 | { SHT_INIT_ARRAY, "INIT_ARRAY" }, |
| 4235 | { SHT_FINI_ARRAY, "FINI_ARRAY" }, |
| 4236 | { SHT_PREINIT_ARRAY, "PREINIT_ARRAY" }, |
| 4237 | { SHT_GROUP, "GROUP" }, |
| 4238 | { SHT_SYMTAB_SHNDX, "SYMTAB_SHNDX " }, |
| 4239 | }; |
| 4240 | |
| 4241 | static struct segment_type_table_t |
| 4242 | { |
| 4243 | const Elf_Word key; |
| 4244 | const char* str; |
| 4245 | } segment_type_table[] = { |
| 4246 | { PT_NULL, "NULL" }, { PT_LOAD, "LOAD" }, { PT_DYNAMIC, "DYNAMIC" }, |
| 4247 | { PT_INTERP, "INTERP" }, { PT_NOTE, "NOTE" }, { PT_SHLIB, "SHLIB" }, |
| 4248 | { PT_PHDR, "PHDR" }, { PT_TLS, "TLS" }, |
| 4249 | }; |
| 4250 | |
| 4251 | static struct segment_flag_table_t |
| 4252 | { |
| 4253 | const Elf_Word key; |
| 4254 | const char* str; |
| 4255 | } segment_flag_table[] = { |
| 4256 | { 0, "" }, { 1, "X" }, { 2, "W" }, { 3, "WX" }, |
| 4257 | { 4, "R" }, { 5, "RX" }, { 6, "RW" }, { 7, "RWX" }, |
| 4258 | }; |
| 4259 | |
| 4260 | static struct symbol_bind_t |
| 4261 | { |
| 4262 | const Elf_Word key; |
| 4263 | const char* str; |
| 4264 | } symbol_bind_table[] = { |
| 4265 | { STB_LOCAL, "LOCAL" }, { STB_GLOBAL, "GLOBAL" }, |
| 4266 | { STB_WEAK, "WEAK" }, { STB_LOOS, "LOOS" }, |
| 4267 | { STB_HIOS, "HIOS" }, { STB_MULTIDEF, "MULTIDEF" }, |
| 4268 | { STB_LOPROC, "LOPROC" }, { STB_HIPROC, "HIPROC" }, |
| 4269 | }; |
| 4270 | |
| 4271 | static struct symbol_type_t |
| 4272 | { |
| 4273 | const Elf_Word key; |
| 4274 | const char* str; |
| 4275 | } symbol_type_table[] = { |
| 4276 | { STT_NOTYPE, "NOTYPE" }, { STT_OBJECT, "OBJECT" }, |
| 4277 | { STT_FUNC, "FUNC" }, { STT_SECTION, "SECTION" }, |
| 4278 | { STT_FILE, "FILE" }, { STT_COMMON, "COMMON" }, |
| 4279 | { STT_TLS, "TLS" }, { STT_LOOS, "LOOS" }, |
| 4280 | { STT_HIOS, "HIOS" }, { STT_LOPROC, "LOPROC" }, |
| 4281 | { STT_HIPROC, "HIPROC" }, |
| 4282 | }; |
| 4283 | |
| 4284 | static struct dynamic_tag_t |
| 4285 | { |
| 4286 | const Elf_Word key; |
| 4287 | const char* str; |
| 4288 | } dynamic_tag_table[] = { |
| 4289 | { DT_NULL, "NULL" }, |
| 4290 | { DT_NEEDED, "NEEDED" }, |
| 4291 | { DT_PLTRELSZ, "PLTRELSZ" }, |
| 4292 | { DT_PLTGOT, "PLTGOT" }, |
| 4293 | { DT_HASH, "HASH" }, |
| 4294 | { DT_STRTAB, "STRTAB" }, |
| 4295 | { DT_SYMTAB, "SYMTAB" }, |
| 4296 | { DT_RELA, "RELA" }, |
| 4297 | { DT_RELASZ, "RELASZ" }, |
| 4298 | { DT_RELAENT, "RELAENT" }, |
| 4299 | { DT_STRSZ, "STRSZ" }, |
| 4300 | { DT_SYMENT, "SYMENT" }, |
| 4301 | { DT_INIT, "INIT" }, |
| 4302 | { DT_FINI, "FINI" }, |
| 4303 | { DT_SONAME, "SONAME" }, |
| 4304 | { DT_RPATH, "RPATH" }, |
| 4305 | { DT_SYMBOLIC, "SYMBOLIC" }, |
| 4306 | { DT_REL, "REL" }, |
| 4307 | { DT_RELSZ, "RELSZ" }, |
| 4308 | { DT_RELENT, "RELENT" }, |
| 4309 | { DT_PLTREL, "PLTREL" }, |
| 4310 | { DT_DEBUG, "DEBUG" }, |
| 4311 | { DT_TEXTREL, "TEXTREL" }, |
| 4312 | { DT_JMPREL, "JMPREL" }, |
| 4313 | { DT_BIND_NOW, "BIND_NOW" }, |
| 4314 | { DT_INIT_ARRAY, "INIT_ARRAY" }, |
| 4315 | { DT_FINI_ARRAY, "FINI_ARRAY" }, |
| 4316 | { DT_INIT_ARRAYSZ, "INIT_ARRAYSZ" }, |
| 4317 | { DT_FINI_ARRAYSZ, "FINI_ARRAYSZ" }, |
| 4318 | { DT_RUNPATH, "RUNPATH" }, |
| 4319 | { DT_FLAGS, "FLAGS" }, |
| 4320 | { DT_ENCODING, "ENCODING" }, |
| 4321 | { DT_PREINIT_ARRAY, "PREINIT_ARRAY" }, |
| 4322 | { DT_PREINIT_ARRAYSZ, "PREINIT_ARRAYSZ" }, |
| 4323 | { DT_MAXPOSTAGS, "MAXPOSTAGS" }, |
| 4324 | }; |
| 4325 | |
| 4326 | static const ELFIO::Elf_Xword MAX_DATA_ENTRIES = 64; |
| 4327 | |
| 4328 | //------------------------------------------------------------------------------ |
| 4329 | class dump |
| 4330 | { |
| 4331 | #define DUMP_DEC_FORMAT( width ) \ |
| 4332 | std::setw( width ) << std::setfill( ' ' ) << std::dec << std::right |
| 4333 | #define DUMP_HEX_FORMAT( width ) \ |
| 4334 | std::setw( width ) << std::setfill( '0' ) << std::hex << std::right |
| 4335 | #define DUMP_STR_FORMAT( width ) \ |
| 4336 | std::setw( width ) << std::setfill( ' ' ) << std::hex << std::left |
| 4337 | |
| 4338 | public: |
| 4339 | //------------------------------------------------------------------------------ |
| 4340 | static void header( std::ostream& out, const elfio& reader ) |
| 4341 | { |
| 4342 | if ( !reader.get_header_size() ) { |
| 4343 | return; |
| 4344 | } |
| 4345 | out << "ELF Header" << std::endl |
| 4346 | << std::endl |
| 4347 | << " Class: " << str_class( reader.get_class() ) << std::endl |
| 4348 | << " Encoding: " << str_endian( reader.get_encoding() ) |
| 4349 | << std::endl |
| 4350 | << " ELFVersion: " << str_version( reader.get_elf_version() ) |
| 4351 | << std::endl |
| 4352 | << " Type: " << str_type( reader.get_type() ) << std::endl |
| 4353 | << " Machine: " << str_machine( reader.get_machine() ) |
| 4354 | << std::endl |
| 4355 | << " Version: " << str_version( reader.get_version() ) |
| 4356 | << std::endl |
| 4357 | << " Entry: " |
| 4358 | << "0x" << std::hex << reader.get_entry() << std::endl |
| 4359 | << " Flags: " |
| 4360 | << "0x" << std::hex << reader.get_flags() << std::endl |
| 4361 | << std::endl; |
| 4362 | } |
| 4363 | |
| 4364 | //------------------------------------------------------------------------------ |
| 4365 | static void section_headers( std::ostream& out, const elfio& reader ) |
| 4366 | { |
| 4367 | Elf_Half n = reader.sections.size(); |
| 4368 | |
| 4369 | if ( n == 0 ) { |
| 4370 | return; |
| 4371 | } |
| 4372 | |
| 4373 | out << "Section Headers:" << std::endl; |
| 4374 | if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit |
| 4375 | out << "[ Nr ] Type Addr Size ES Flg Lk Inf " |
| 4376 | "Al Name" |
| 4377 | << std::endl; |
| 4378 | } |
| 4379 | else { // Output for 64-bit |
| 4380 | out << "[ Nr ] Type Addr Size " |
| 4381 | " ES Flg" |
| 4382 | << std::endl |
| 4383 | << " Lk Inf Al Name" << std::endl; |
| 4384 | } |
| 4385 | |
| 4386 | for ( Elf_Half i = 0; i < n; ++i ) { // For all sections |
| 4387 | section* sec = reader.sections[i]; |
| 4388 | section_header( out, i, sec, reader.get_class() ); |
| 4389 | } |
| 4390 | |
| 4391 | out << "Key to Flags: W (write), A (alloc), X (execute)\n\n" |
| 4392 | << std::endl; |
| 4393 | } |
| 4394 | |
| 4395 | //------------------------------------------------------------------------------ |
| 4396 | static void section_header( std::ostream& out, |
| 4397 | Elf_Half no, |
| 4398 | const section* sec, |
| 4399 | unsigned char elf_class ) |
| 4400 | { |
| 4401 | std::ios_base::fmtflags original_flags = out.flags(); |
| 4402 | |
| 4403 | if ( elf_class == ELFCLASS32 ) { // Output for 32-bit |
| 4404 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4405 | << DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) |
| 4406 | << " " << DUMP_HEX_FORMAT( 8 ) << sec->get_address() << " " |
| 4407 | << DUMP_HEX_FORMAT( 8 ) << sec->get_size() << " " |
| 4408 | << DUMP_HEX_FORMAT( 2 ) << sec->get_entry_size() << " " |
| 4409 | << DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) |
| 4410 | << " " << DUMP_HEX_FORMAT( 2 ) << sec->get_link() << " " |
| 4411 | << DUMP_HEX_FORMAT( 3 ) << sec->get_info() << " " |
| 4412 | << DUMP_HEX_FORMAT( 2 ) << sec->get_addr_align() << " " |
| 4413 | << DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " << std::endl; |
| 4414 | } |
| 4415 | else { // Output for 64-bit |
| 4416 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4417 | << DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) |
| 4418 | << " " << DUMP_HEX_FORMAT( 16 ) << sec->get_address() << " " |
| 4419 | << DUMP_HEX_FORMAT( 16 ) << sec->get_size() << " " |
| 4420 | << DUMP_HEX_FORMAT( 4 ) << sec->get_entry_size() << " " |
| 4421 | << DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) |
| 4422 | << " " << std::endl |
| 4423 | << " " << DUMP_HEX_FORMAT( 4 ) << sec->get_link() << " " |
| 4424 | << DUMP_HEX_FORMAT( 4 ) << sec->get_info() << " " |
| 4425 | << DUMP_HEX_FORMAT( 4 ) << sec->get_addr_align() << " " |
| 4426 | << DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " << std::endl; |
| 4427 | } |
| 4428 | |
| 4429 | out.flags( original_flags ); |
| 4430 | |
| 4431 | return; |
| 4432 | } |
| 4433 | |
| 4434 | //------------------------------------------------------------------------------ |
| 4435 | static void segment_headers( std::ostream& out, const elfio& reader ) |
| 4436 | { |
| 4437 | Elf_Half n = reader.segments.size(); |
| 4438 | if ( n == 0 ) { |
| 4439 | return; |
| 4440 | } |
| 4441 | |
| 4442 | out << "Segment headers:" << std::endl; |
| 4443 | if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit |
| 4444 | out << "[ Nr ] Type VirtAddr PhysAddr FileSize Mem.Size " |
| 4445 | "Flags Align" |
| 4446 | << std::endl; |
| 4447 | } |
| 4448 | else { // Output for 64-bit |
| 4449 | out << "[ Nr ] Type VirtAddr PhysAddr " |
| 4450 | "Flags" |
| 4451 | << std::endl |
| 4452 | << " FileSize Mem.Size " |
| 4453 | "Align" |
| 4454 | << std::endl; |
| 4455 | } |
| 4456 | |
| 4457 | for ( Elf_Half i = 0; i < n; ++i ) { |
| 4458 | segment* seg = reader.segments[i]; |
| 4459 | segment_header( out, i, seg, reader.get_class() ); |
| 4460 | } |
| 4461 | |
| 4462 | out << std::endl; |
| 4463 | } |
| 4464 | |
| 4465 | //------------------------------------------------------------------------------ |
| 4466 | static void segment_header( std::ostream& out, |
| 4467 | Elf_Half no, |
| 4468 | const segment* seg, |
| 4469 | unsigned int elf_class ) |
| 4470 | { |
| 4471 | std::ios_base::fmtflags original_flags = out.flags(); |
| 4472 | |
| 4473 | if ( elf_class == ELFCLASS32 ) { // Output for 32-bit |
| 4474 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4475 | << DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) |
| 4476 | << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_virtual_address() |
| 4477 | << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_physical_address() |
| 4478 | << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_file_size() << " " |
| 4479 | << DUMP_HEX_FORMAT( 8 ) << seg->get_memory_size() << " " |
| 4480 | << DUMP_STR_FORMAT( 8 ) << str_segment_flag( seg->get_flags() ) |
| 4481 | << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_align() << " " |
| 4482 | << std::endl; |
| 4483 | } |
| 4484 | else { // Output for 64-bit |
| 4485 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4486 | << DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) |
| 4487 | << " " << DUMP_HEX_FORMAT( 16 ) << seg->get_virtual_address() |
| 4488 | << " " << DUMP_HEX_FORMAT( 16 ) << seg->get_physical_address() |
| 4489 | << " " << DUMP_STR_FORMAT( 16 ) |
| 4490 | << str_segment_flag( seg->get_flags() ) << " " << std::endl |
| 4491 | << " " << DUMP_HEX_FORMAT( 16 ) |
| 4492 | << seg->get_file_size() << " " << DUMP_HEX_FORMAT( 16 ) |
| 4493 | << seg->get_memory_size() << " " << DUMP_HEX_FORMAT( 16 ) |
| 4494 | << seg->get_align() << " " << std::endl; |
| 4495 | } |
| 4496 | |
| 4497 | out.flags( original_flags ); |
| 4498 | } |
| 4499 | |
| 4500 | //------------------------------------------------------------------------------ |
| 4501 | static void symbol_tables( std::ostream& out, const elfio& reader ) |
| 4502 | { |
| 4503 | Elf_Half n = reader.sections.size(); |
| 4504 | for ( Elf_Half i = 0; i < n; ++i ) { // For all sections |
| 4505 | section* sec = reader.sections[i]; |
| 4506 | if ( SHT_SYMTAB == sec->get_type() || |
| 4507 | SHT_DYNSYM == sec->get_type() ) { |
| 4508 | symbol_section_accessor symbols( reader, sec ); |
| 4509 | |
| 4510 | Elf_Xword sym_no = symbols.get_symbols_num(); |
| 4511 | if ( sym_no > 0 ) { |
| 4512 | out << "Symbol table (" << sec->get_name() << ")" |
| 4513 | << std::endl; |
| 4514 | if ( reader.get_class() == |
| 4515 | ELFCLASS32 ) { // Output for 32-bit |
| 4516 | out << "[ Nr ] Value Size Type Bind " |
| 4517 | "Sect Name" |
| 4518 | << std::endl; |
| 4519 | } |
| 4520 | else { // Output for 64-bit |
| 4521 | out << "[ Nr ] Value Size Type " |
| 4522 | " Bind Sect" |
| 4523 | << std::endl |
| 4524 | << " Name" << std::endl; |
| 4525 | } |
| 4526 | for ( Elf_Xword i = 0; i < sym_no; ++i ) { |
| 4527 | std::string name; |
| 4528 | Elf64_Addr value = 0; |
| 4529 | Elf_Xword size = 0; |
| 4530 | unsigned char bind = 0; |
| 4531 | unsigned char type = 0; |
| 4532 | Elf_Half section = 0; |
| 4533 | unsigned char other = 0; |
| 4534 | symbols.get_symbol( i, name, value, size, bind, type, |
| 4535 | section, other ); |
| 4536 | symbol_table( out, i, name, value, size, bind, type, |
| 4537 | section, reader.get_class() ); |
| 4538 | } |
| 4539 | |
| 4540 | out << std::endl; |
| 4541 | } |
| 4542 | } |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | //------------------------------------------------------------------------------ |
| 4547 | static void symbol_table( std::ostream& out, |
| 4548 | Elf_Xword no, |
| 4549 | std::string& name, |
| 4550 | Elf64_Addr value, |
| 4551 | Elf_Xword size, |
| 4552 | unsigned char bind, |
| 4553 | unsigned char type, |
| 4554 | Elf_Half section, |
| 4555 | unsigned int elf_class ) |
| 4556 | { |
| 4557 | std::ios_base::fmtflags original_flags = out.flags(); |
| 4558 | |
| 4559 | if ( elf_class == ELFCLASS32 ) { // Output for 32-bit |
| 4560 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4561 | << DUMP_HEX_FORMAT( 8 ) << value << " " << DUMP_HEX_FORMAT( 8 ) |
| 4562 | << size << " " << DUMP_STR_FORMAT( 7 ) |
| 4563 | << str_symbol_type( type ) << " " << DUMP_STR_FORMAT( 8 ) |
| 4564 | << str_symbol_bind( bind ) << " " << DUMP_DEC_FORMAT( 5 ) |
| 4565 | << section << " " << DUMP_STR_FORMAT( 1 ) << name << " " |
| 4566 | << std::endl; |
| 4567 | } |
| 4568 | else { // Output for 64-bit |
| 4569 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4570 | << DUMP_HEX_FORMAT( 16 ) << value << " " |
| 4571 | << DUMP_HEX_FORMAT( 16 ) << size << " " << DUMP_STR_FORMAT( 7 ) |
| 4572 | << str_symbol_type( type ) << " " << DUMP_STR_FORMAT( 8 ) |
| 4573 | << str_symbol_bind( bind ) << " " << DUMP_DEC_FORMAT( 5 ) |
| 4574 | << section << " " << std::endl |
| 4575 | << " " << DUMP_STR_FORMAT( 1 ) << name << " " |
| 4576 | << std::endl; |
| 4577 | } |
| 4578 | |
| 4579 | out.flags( original_flags ); |
| 4580 | } |
| 4581 | |
| 4582 | //------------------------------------------------------------------------------ |
| 4583 | static void notes( std::ostream& out, const elfio& reader ) |
| 4584 | { |
| 4585 | Elf_Half no = reader.sections.size(); |
| 4586 | for ( Elf_Half i = 0; i < no; ++i ) { // For all sections |
| 4587 | section* sec = reader.sections[i]; |
| 4588 | if ( SHT_NOTE == sec->get_type() ) { // Look at notes |
| 4589 | note_section_accessor notes( reader, sec ); |
| 4590 | Elf_Word no_notes = notes.get_notes_num(); |
| 4591 | if ( no > 0 ) { |
| 4592 | out << "Note section (" << sec->get_name() << ")" |
| 4593 | << std::endl |
| 4594 | << " No Type Name" << std::endl; |
| 4595 | for ( Elf_Word j = 0; j < no_notes; ++j ) { // For all notes |
| 4596 | Elf_Word type; |
| 4597 | std::string name; |
| 4598 | void* desc; |
| 4599 | Elf_Word descsz; |
| 4600 | |
| 4601 | if ( notes.get_note( j, type, name, desc, descsz ) ) { |
| 4602 | // 'name' usually contains \0 at the end. Try to fix it |
| 4603 | name = name.c_str(); |
| 4604 | note( out, j, type, name ); |
| 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | out << std::endl; |
| 4609 | } |
| 4610 | } |
| 4611 | } |
| 4612 | } |
| 4613 | |
| 4614 | //------------------------------------------------------------------------------ |
| 4615 | static void modinfo( std::ostream& out, const elfio& reader ) |
| 4616 | { |
| 4617 | Elf_Half no = reader.sections.size(); |
| 4618 | for ( Elf_Half i = 0; i < no; ++i ) { // For all sections |
| 4619 | section* sec = reader.sections[i]; |
| 4620 | if ( ".modinfo" == sec->get_name() ) { // Look for the section |
| 4621 | out << "Section .modinfo" << std::endl; |
| 4622 | |
| 4623 | const_modinfo_section_accessor modinfo( sec ); |
| 4624 | for ( Elf_Word i = 0; i < modinfo.get_attribute_num(); i++ ) { |
| 4625 | std::string field; |
| 4626 | std::string value; |
| 4627 | if ( modinfo.get_attribute( i, field, value ) ) { |
| 4628 | out << " " << std::setw( 20 ) << field |
| 4629 | << std::setw( 0 ) << " = " << value << std::endl; |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | out << std::endl; |
| 4634 | break; |
| 4635 | } |
| 4636 | } |
| 4637 | } |
| 4638 | |
| 4639 | //------------------------------------------------------------------------------ |
| 4640 | static void |
| 4641 | note( std::ostream& out, int no, Elf_Word type, const std::string& name ) |
| 4642 | { |
| 4643 | out << " [" << DUMP_DEC_FORMAT( 2 ) << no << "] " |
| 4644 | << DUMP_HEX_FORMAT( 8 ) << type << " " << DUMP_STR_FORMAT( 1 ) |
| 4645 | << name << std::endl; |
| 4646 | } |
| 4647 | |
| 4648 | //------------------------------------------------------------------------------ |
| 4649 | static void dynamic_tags( std::ostream& out, const elfio& reader ) |
| 4650 | { |
| 4651 | Elf_Half n = reader.sections.size(); |
| 4652 | for ( Elf_Half i = 0; i < n; ++i ) { // For all sections |
| 4653 | section* sec = reader.sections[i]; |
| 4654 | if ( SHT_DYNAMIC == sec->get_type() ) { |
| 4655 | dynamic_section_accessor dynamic( reader, sec ); |
| 4656 | |
| 4657 | Elf_Xword dyn_no = dynamic.get_entries_num(); |
| 4658 | if ( dyn_no > 0 ) { |
| 4659 | out << "Dynamic section (" << sec->get_name() << ")" |
| 4660 | << std::endl; |
| 4661 | out << "[ Nr ] Tag Name/Value" << std::endl; |
| 4662 | for ( Elf_Xword i = 0; i < dyn_no; ++i ) { |
| 4663 | Elf_Xword tag = 0; |
| 4664 | Elf_Xword value = 0; |
| 4665 | std::string str; |
| 4666 | dynamic.get_entry( i, tag, value, str ); |
| 4667 | dynamic_tag( out, i, tag, value, str, |
| 4668 | reader.get_class() ); |
| 4669 | if ( DT_NULL == tag ) { |
| 4670 | break; |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | out << std::endl; |
| 4675 | } |
| 4676 | } |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | //------------------------------------------------------------------------------ |
| 4681 | static void dynamic_tag( std::ostream& out, |
| 4682 | Elf_Xword no, |
| 4683 | Elf_Xword tag, |
| 4684 | Elf_Xword value, |
| 4685 | std::string str, |
| 4686 | unsigned int /*elf_class*/ ) |
| 4687 | { |
| 4688 | out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " |
| 4689 | << DUMP_STR_FORMAT( 16 ) << str_dynamic_tag( tag ) << " "; |
| 4690 | if ( str.empty() ) { |
| 4691 | out << DUMP_HEX_FORMAT( 16 ) << value << " "; |
| 4692 | } |
| 4693 | else { |
| 4694 | out << DUMP_STR_FORMAT( 32 ) << str << " "; |
| 4695 | } |
| 4696 | out << std::endl; |
| 4697 | } |
| 4698 | |
| 4699 | //------------------------------------------------------------------------------ |
| 4700 | static void section_data( std::ostream& out, const section* sec ) |
| 4701 | { |
| 4702 | std::ios_base::fmtflags original_flags = out.flags(); |
| 4703 | |
| 4704 | out << sec->get_name() << std::endl; |
| 4705 | const char* pdata = sec->get_data(); |
| 4706 | if ( pdata ) { |
| 4707 | ELFIO::Elf_Xword i; |
| 4708 | for ( i = 0; i < std::min( sec->get_size(), MAX_DATA_ENTRIES ); |
| 4709 | ++i ) { |
| 4710 | if ( i % 16 == 0 ) { |
| 4711 | out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; |
| 4712 | } |
| 4713 | |
| 4714 | out << " " << DUMP_HEX_FORMAT( 2 ) << ( pdata[i] & 0x000000FF ); |
| 4715 | |
| 4716 | if ( i % 16 == 15 ) { |
| 4717 | out << std::endl; |
| 4718 | } |
| 4719 | } |
| 4720 | if ( i % 16 != 0 ) { |
| 4721 | out << std::endl; |
| 4722 | } |
| 4723 | |
| 4724 | out.flags( original_flags ); |
| 4725 | } |
| 4726 | |
| 4727 | return; |
| 4728 | } |
| 4729 | |
| 4730 | //------------------------------------------------------------------------------ |
| 4731 | static void section_datas( std::ostream& out, const elfio& reader ) |
| 4732 | { |
| 4733 | Elf_Half n = reader.sections.size(); |
| 4734 | |
| 4735 | if ( n == 0 ) { |
| 4736 | return; |
| 4737 | } |
| 4738 | |
| 4739 | out << "Section Data:" << std::endl; |
| 4740 | |
| 4741 | for ( Elf_Half i = 1; i < n; ++i ) { // For all sections |
| 4742 | section* sec = reader.sections[i]; |
| 4743 | if ( sec->get_type() == SHT_NOBITS ) { |
| 4744 | continue; |
| 4745 | } |
| 4746 | section_data( out, sec ); |
| 4747 | } |
| 4748 | |
| 4749 | out << std::endl; |
| 4750 | } |
| 4751 | |
| 4752 | //------------------------------------------------------------------------------ |
| 4753 | static void |
| 4754 | segment_data( std::ostream& out, Elf_Half no, const segment* seg ) |
| 4755 | { |
| 4756 | std::ios_base::fmtflags original_flags = out.flags(); |
| 4757 | |
| 4758 | out << "Segment # " << no << std::endl; |
| 4759 | const char* pdata = seg->get_data(); |
| 4760 | if ( pdata ) { |
| 4761 | ELFIO::Elf_Xword i; |
| 4762 | for ( i = 0; i < std::min( seg->get_file_size(), MAX_DATA_ENTRIES ); |
| 4763 | ++i ) { |
| 4764 | if ( i % 16 == 0 ) { |
| 4765 | out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; |
| 4766 | } |
| 4767 | |
| 4768 | out << " " << DUMP_HEX_FORMAT( 2 ) << ( pdata[i] & 0x000000FF ); |
| 4769 | |
| 4770 | if ( i % 16 == 15 ) { |
| 4771 | out << std::endl; |
| 4772 | } |
| 4773 | } |
| 4774 | if ( i % 16 != 0 ) { |
| 4775 | out << std::endl; |
| 4776 | } |
| 4777 | |
| 4778 | out.flags( original_flags ); |
| 4779 | } |
| 4780 | |
| 4781 | return; |
| 4782 | } |
| 4783 | |
| 4784 | //------------------------------------------------------------------------------ |
| 4785 | static void segment_datas( std::ostream& out, const elfio& reader ) |
| 4786 | { |
| 4787 | Elf_Half n = reader.segments.size(); |
| 4788 | |
| 4789 | if ( n == 0 ) { |
| 4790 | return; |
| 4791 | } |
| 4792 | |
| 4793 | out << "Segment Data:" << std::endl; |
| 4794 | |
| 4795 | for ( Elf_Half i = 0; i < n; ++i ) { // For all sections |
| 4796 | segment* seg = reader.segments[i]; |
| 4797 | segment_data( out, i, seg ); |
| 4798 | } |
| 4799 | |
| 4800 | out << std::endl; |
| 4801 | } |
| 4802 | |
| 4803 | private: |
| 4804 | //------------------------------------------------------------------------------ |
| 4805 | template <typename T, typename K> |
| 4806 | std::string static find_value_in_table( const T& table, const K& key ) |
| 4807 | { |
| 4808 | std::string res = "?"; |
| 4809 | for ( unsigned int i = 0; i < sizeof( table ) / sizeof( table[0] ); |
| 4810 | ++i ) { |
| 4811 | if ( table[i].key == key ) { |
| 4812 | res = table[i].str; |
| 4813 | break; |
| 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | return res; |
| 4818 | } |
| 4819 | |
| 4820 | //------------------------------------------------------------------------------ |
| 4821 | template <typename T, typename K> |
| 4822 | static std::string format_assoc( const T& table, const K& key ) |
| 4823 | { |
| 4824 | std::string str = find_value_in_table( table, key ); |
| 4825 | if ( str == "?" ) { |
| 4826 | std::ostringstream oss; |
| 4827 | oss << str << " (0x" << std::hex << key << ")"; |
| 4828 | str = oss.str(); |
| 4829 | } |
| 4830 | |
| 4831 | return str; |
| 4832 | } |
| 4833 | |
| 4834 | //------------------------------------------------------------------------------ |
| 4835 | template <typename T> |
| 4836 | static std::string format_assoc( const T& table, const char key ) |
| 4837 | { |
| 4838 | return format_assoc( table, (const int)key ); |
| 4839 | } |
| 4840 | |
| 4841 | //------------------------------------------------------------------------------ |
| 4842 | static std::string section_flags( Elf_Xword flags ) |
| 4843 | { |
| 4844 | std::string ret = ""; |
| 4845 | if ( flags & SHF_WRITE ) { |
| 4846 | ret += "W"; |
| 4847 | } |
| 4848 | if ( flags & SHF_ALLOC ) { |
| 4849 | ret += "A"; |
| 4850 | } |
| 4851 | if ( flags & SHF_EXECINSTR ) { |
| 4852 | ret += "X"; |
| 4853 | } |
| 4854 | |
| 4855 | return ret; |
| 4856 | } |
| 4857 | |
| 4858 | //------------------------------------------------------------------------------ |
| 4859 | #define STR_FUNC_TABLE( name ) \ |
| 4860 | template <typename T> static std::string str_##name( const T key ) \ |
| 4861 | { \ |
| 4862 | return format_assoc( name##_table, key ); \ |
| 4863 | } |
| 4864 | |
| 4865 | STR_FUNC_TABLE( class ) |
| 4866 | STR_FUNC_TABLE( endian ) |
| 4867 | STR_FUNC_TABLE( version ) |
| 4868 | STR_FUNC_TABLE( type ) |
| 4869 | STR_FUNC_TABLE( machine ) |
| 4870 | STR_FUNC_TABLE( section_type ) |
| 4871 | STR_FUNC_TABLE( segment_type ) |
| 4872 | STR_FUNC_TABLE( segment_flag ) |
| 4873 | STR_FUNC_TABLE( symbol_bind ) |
| 4874 | STR_FUNC_TABLE( symbol_type ) |
| 4875 | STR_FUNC_TABLE( dynamic_tag ) |
| 4876 | |
| 4877 | #undef STR_FUNC_TABLE |
| 4878 | #undef DUMP_DEC_FORMAT |
| 4879 | #undef DUMP_HEX_FORMAT |
| 4880 | #undef DUMP_STR_FORMAT |
| 4881 | }; // class dump |
| 4882 | |
| 4883 | }; // namespace ELFIO |
| 4884 | |
| 4885 | #endif // ELFIO_DUMP_HPP |
| 4886 | |
| 4887 | /*** End of inlined file: elfio_dump.hpp ***/ |
| 4888 |