diff --git a/.clang-format b/.clang-format index 14aba60b24017ec75f29940f401dbd8db29b97fd..c20c6b5c4cbd717c0deea6495a729901ac79a1e4 100644 --- a/.clang-format +++ b/.clang-format @@ -1,27 +1,34 @@ -{ - BasedOnStyle : LLVM, +BasedOnStyle : LLVM - AlignConsecutiveAssignments : true, - AlignConsecutiveDeclarations : true, - AlignConsecutiveMacros : true, - AlignEscapedNewlines : true, - AlignOperands : true, - AlignTrailingComments : true, - - UseTab : Never, - IndentWidth : 4, - TabWidth : 4, - BreakBeforeBraces : Stroustrup, - AllowShortIfStatementsOnASingleLine : false, - IndentCaseLabels : false, - ColumnLimit : 90, - AccessModifierOffset : -4, - NamespaceIndentation : Inner, - PointerAlignment : Left, - FixNamespaceComments : true, - SortIncludes : false, - - ReflowComments : false, - SpacesInConditionalStatement : true, - SpacesInParentheses : true, -} +AccessModifierOffset : -2 +AlignAfterOpenBracket : true +AlignConsecutiveAssignments : true +AlignConsecutiveDeclarations : true +AlignConsecutiveMacros : true +AlignEscapedNewlines : true +AlignOperands : true +AlignTrailingComments : true +BinPackParameters : false +BraceWrapping: + AfterCaseLabel : true + AfterClass : true + AfterEnum : true + AfterFunction : true + AfterNamespace : false + AfterStruct : true + AfterExternBlock : true + BeforeCatch : true + BeforeElse : true +BreakBeforeBraces : Custom +ColumnLimit : 80 +FixNamespaceComments : true +IndentCaseLabels : false +IndentWidth : 4 +NamespaceIndentation : Inner +PointerAlignment : Left +ReflowComments : false +SortIncludes : false +SpacesInConditionalStatement : true +SpacesInParentheses : true +TabWidth : 4 +UseTab : Never diff --git a/elfio/elf_types.hpp b/elfio/elf_types.hpp index 36ce5dd7cb788f2a60966dd4bbec1b085f35d8e5..5c32bfedf95e2dd247847e683ebf7a595bfcc300 100644 --- a/elfio/elf_types.hpp +++ b/elfio/elf_types.hpp @@ -24,25 +24,25 @@ THE SOFTWARE. #define ELFTYPES_H #ifndef ELFIO_NO_OWN_TYPES - #if !defined(ELFIO_NO_CSTDINT) && !defined(ELFIO_NO_INTTYPES) - #include <stdint.h> - #else - typedef unsigned char uint8_t; - typedef signed char int8_t; - typedef unsigned short uint16_t; - typedef signed short int16_t; - #ifdef _MSC_VER - typedef unsigned __int32 uint32_t; - typedef signed __int32 int32_t; - typedef unsigned __int64 uint64_t; - typedef signed __int64 int64_t; - #else - typedef unsigned int uint32_t; - typedef signed int int32_t; - typedef unsigned long long uint64_t; - typedef signed long long int64_t; - #endif // _MSC_VER - #endif // ELFIO_NO_CSTDINT +#if !defined( ELFIO_NO_CSTDINT ) && !defined( ELFIO_NO_INTTYPES ) +#include <stdint.h> +#else +typedef unsigned char uint8_t; +typedef signed char int8_t; +typedef unsigned short uint16_t; +typedef signed short int16_t; +#ifdef _MSC_VER +typedef unsigned __int32 uint32_t; +typedef signed __int32 int32_t; +typedef unsigned __int64 uint64_t; +typedef signed __int64 int64_t; +#else +typedef unsigned int uint32_t; +typedef signed int int32_t; +typedef unsigned long long uint64_t; +typedef signed long long int64_t; +#endif // _MSC_VER +#endif // ELFIO_NO_CSTDINT #endif // ELFIO_NO_OWN_TYPES namespace ELFIO { @@ -59,10 +59,10 @@ typedef uint32_t Elf32_Off; typedef uint64_t Elf64_Addr; typedef uint64_t Elf64_Off; -#define Elf32_Half Elf_Half -#define Elf64_Half Elf_Half -#define Elf32_Word Elf_Word -#define Elf64_Word Elf_Word +#define Elf32_Half Elf_Half +#define Elf64_Half Elf_Half +#define Elf32_Word Elf_Word +#define Elf64_Word Elf_Word #define Elf32_Sword Elf_Sword #define Elf64_Sword Elf_Sword @@ -70,137 +70,139 @@ typedef uint64_t Elf64_Off; // ELF Header Constants // File type -#define ET_NONE 0 -#define ET_REL 1 -#define ET_EXEC 2 -#define ET_DYN 3 -#define ET_CORE 4 +#define ET_NONE 0 +#define ET_REL 1 +#define ET_EXEC 2 +#define ET_DYN 3 +#define ET_CORE 4 #define ET_LOOS 0xFE00 #define ET_HIOS 0xFEFF #define ET_LOPROC 0xFF00 #define ET_HIPROC 0xFFFF - -#define EM_NONE 0 // No machine -#define EM_M32 1 // AT&T WE 32100 -#define EM_SPARC 2 // SUN SPARC -#define EM_386 3 // Intel 80386 -#define EM_68K 4 // Motorola m68k family -#define EM_88K 5 // Motorola m88k family -#define EM_486 6 // Intel 80486// Reserved for future use -#define EM_860 7 // Intel 80860 -#define EM_MIPS 8 // MIPS R3000 (officially, big-endian only) -#define EM_S370 9 // IBM System/370 -#define EM_MIPS_RS3_LE 10 // MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated -#define EM_res011 11 // Reserved -#define EM_res012 12 // Reserved -#define EM_res013 13 // Reserved -#define EM_res014 14 // Reserved -#define EM_PARISC 15 // HPPA -#define EM_res016 16 // Reserved -#define EM_VPP550 17 // Fujitsu VPP500 -#define EM_SPARC32PLUS 18 // Sun's "v8plus" -#define EM_960 19 // Intel 80960 -#define EM_PPC 20 // PowerPC -#define EM_PPC64 21 // 64-bit PowerPC -#define EM_S390 22 // IBM S/390 -#define EM_SPU 23 // Sony/Toshiba/IBM SPU -#define EM_res024 24 // Reserved -#define EM_res025 25 // Reserved -#define EM_res026 26 // Reserved -#define EM_res027 27 // Reserved -#define EM_res028 28 // Reserved -#define EM_res029 29 // Reserved -#define EM_res030 30 // Reserved -#define EM_res031 31 // Reserved -#define EM_res032 32 // Reserved -#define EM_res033 33 // Reserved -#define EM_res034 34 // Reserved -#define EM_res035 35 // Reserved -#define EM_V800 36 // NEC V800 series -#define EM_FR20 37 // Fujitsu FR20 -#define EM_RH32 38 // TRW RH32 -#define EM_MCORE 39 // Motorola M*Core // May also be taken by Fujitsu MMA -#define EM_RCE 39 // Old name for MCore -#define EM_ARM 40 // ARM -#define EM_OLD_ALPHA 41 // Digital Alpha -#define EM_SH 42 // Renesas (formerly Hitachi) / SuperH SH -#define EM_SPARCV9 43 // SPARC v9 64-bit -#define EM_TRICORE 44 // Siemens Tricore embedded processor -#define EM_ARC 45 // ARC Cores -#define EM_H8_300 46 // Renesas (formerly Hitachi) H8/300 -#define EM_H8_300H 47 // Renesas (formerly Hitachi) H8/300H -#define EM_H8S 48 // Renesas (formerly Hitachi) H8S -#define EM_H8_500 49 // Renesas (formerly Hitachi) H8/500 -#define EM_IA_64 50 // Intel IA-64 Processor -#define EM_MIPS_X 51 // Stanford MIPS-X -#define EM_COLDFIRE 52 // Motorola Coldfire -#define EM_68HC12 53 // Motorola M68HC12 -#define EM_MMA 54 // Fujitsu Multimedia Accelerator -#define EM_PCP 55 // Siemens PCP -#define EM_NCPU 56 // Sony nCPU embedded RISC processor -#define EM_NDR1 57 // Denso NDR1 microprocesspr -#define EM_STARCORE 58 // Motorola Star*Core processor -#define EM_ME16 59 // Toyota ME16 processor -#define EM_ST100 60 // STMicroelectronics ST100 processor -#define EM_TINYJ 61 // Advanced Logic Corp. TinyJ embedded processor -#define EM_X86_64 62 // Advanced Micro Devices X86-64 processor -#define EM_PDSP 63 // Sony DSP Processor -#define EM_PDP10 64 // Digital Equipment Corp. PDP-10 -#define EM_PDP11 65 // Digital Equipment Corp. PDP-11 -#define EM_FX66 66 // Siemens FX66 microcontroller -#define EM_ST9PLUS 67 // STMicroelectronics ST9+ 8/16 bit microcontroller -#define EM_ST7 68 // STMicroelectronics ST7 8-bit microcontroller -#define EM_68HC16 69 // Motorola MC68HC16 Microcontroller -#define EM_68HC11 70 // Motorola MC68HC11 Microcontroller -#define EM_68HC08 71 // Motorola MC68HC08 Microcontroller -#define EM_68HC05 72 // Motorola MC68HC05 Microcontroller -#define EM_SVX 73 // Silicon Graphics SVx -#define EM_ST19 74 // STMicroelectronics ST19 8-bit cpu -#define EM_VAX 75 // Digital VAX -#define EM_CRIS 76 // Axis Communications 32-bit embedded processor -#define EM_JAVELIN 77 // Infineon Technologies 32-bit embedded cpu -#define EM_FIREPATH 78 // Element 14 64-bit DSP processor -#define EM_ZSP 79 // LSI Logic's 16-bit DSP processor -#define EM_MMIX 80 // Donald Knuth's educational 64-bit processor -#define EM_HUANY 81 // Harvard's machine-independent format -#define EM_PRISM 82 // SiTera Prism -#define EM_AVR 83 // Atmel AVR 8-bit microcontroller -#define EM_FR30 84 // Fujitsu FR30 -#define EM_D10V 85 // Mitsubishi D10V -#define EM_D30V 86 // Mitsubishi D30V -#define EM_V850 87 // NEC v850 -#define EM_M32R 88 // Renesas M32R (formerly Mitsubishi M32R) -#define EM_MN10300 89 // Matsushita MN10300 -#define EM_MN10200 90 // Matsushita MN10200 -#define EM_PJ 91 // picoJava -#define EM_OPENRISC 92 // OpenRISC 32-bit embedded processor -#define EM_ARC_A5 93 // ARC Cores Tangent-A5 -#define EM_XTENSA 94 // Tensilica Xtensa Architecture -#define EM_VIDEOCORE 95 // Alphamosaic VideoCore processor -#define EM_TMM_GPP 96 // Thompson Multimedia General Purpose Processor -#define EM_NS32K 97 // National Semiconductor 32000 series -#define EM_TPC 98 // Tenor Network TPC processor -#define EM_SNP1K 99 // Trebia SNP 1000 processor -#define EM_ST200 100 // STMicroelectronics ST200 microcontroller -#define EM_IP2K 101 // Ubicom IP2022 micro controller -#define EM_MAX 102 // MAX Processor -#define EM_CR 103 // National Semiconductor CompactRISC -#define EM_F2MC16 104 // Fujitsu F2MC16 -#define EM_MSP430 105 // TI msp430 micro controller -#define EM_BLACKFIN 106 // ADI Blackfin -#define EM_SE_C33 107 // S1C33 Family of Seiko Epson processors -#define EM_SEP 108 // Sharp embedded microprocessor -#define EM_ARCA 109 // Arca RISC Microprocessor -#define EM_UNICORE 110 // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University -#define EM_EXCESS 111 // eXcess: 16/32/64-bit configurable embedded CPU -#define EM_DXP 112 // Icera Semiconductor Inc. Deep Execution Processor -#define EM_ALTERA_NIOS2 113 // Altera Nios II soft-core processor -#define EM_CRX 114 // National Semiconductor CRX -#define EM_XGATE 115 // Motorola XGATE embedded processor -#define EM_C166 116 // Infineon C16x/XC16x processor -#define EM_M16C 117 // Renesas M16C series microprocessors -#define EM_DSPIC30F 118 // Microchip Technology dsPIC30F Digital Signal Controller +#define EM_NONE 0 // No machine +#define EM_M32 1 // AT&T WE 32100 +#define EM_SPARC 2 // SUN SPARC +#define EM_386 3 // Intel 80386 +#define EM_68K 4 // Motorola m68k family +#define EM_88K 5 // Motorola m88k family +#define EM_486 6 // Intel 80486// Reserved for future use +#define EM_860 7 // Intel 80860 +#define EM_MIPS 8 // MIPS R3000 (officially, big-endian only) +#define EM_S370 9 // IBM System/370 +#define EM_MIPS_RS3_LE \ + 10 // MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated +#define EM_res011 11 // Reserved +#define EM_res012 12 // Reserved +#define EM_res013 13 // Reserved +#define EM_res014 14 // Reserved +#define EM_PARISC 15 // HPPA +#define EM_res016 16 // Reserved +#define EM_VPP550 17 // Fujitsu VPP500 +#define EM_SPARC32PLUS 18 // Sun's "v8plus" +#define EM_960 19 // Intel 80960 +#define EM_PPC 20 // PowerPC +#define EM_PPC64 21 // 64-bit PowerPC +#define EM_S390 22 // IBM S/390 +#define EM_SPU 23 // Sony/Toshiba/IBM SPU +#define EM_res024 24 // Reserved +#define EM_res025 25 // Reserved +#define EM_res026 26 // Reserved +#define EM_res027 27 // Reserved +#define EM_res028 28 // Reserved +#define EM_res029 29 // Reserved +#define EM_res030 30 // Reserved +#define EM_res031 31 // Reserved +#define EM_res032 32 // Reserved +#define EM_res033 33 // Reserved +#define EM_res034 34 // Reserved +#define EM_res035 35 // Reserved +#define EM_V800 36 // NEC V800 series +#define EM_FR20 37 // Fujitsu FR20 +#define EM_RH32 38 // TRW RH32 +#define EM_MCORE 39 // Motorola M*Core // May also be taken by Fujitsu MMA +#define EM_RCE 39 // Old name for MCore +#define EM_ARM 40 // ARM +#define EM_OLD_ALPHA 41 // Digital Alpha +#define EM_SH 42 // Renesas (formerly Hitachi) / SuperH SH +#define EM_SPARCV9 43 // SPARC v9 64-bit +#define EM_TRICORE 44 // Siemens Tricore embedded processor +#define EM_ARC 45 // ARC Cores +#define EM_H8_300 46 // Renesas (formerly Hitachi) H8/300 +#define EM_H8_300H 47 // Renesas (formerly Hitachi) H8/300H +#define EM_H8S 48 // Renesas (formerly Hitachi) H8S +#define EM_H8_500 49 // Renesas (formerly Hitachi) H8/500 +#define EM_IA_64 50 // Intel IA-64 Processor +#define EM_MIPS_X 51 // Stanford MIPS-X +#define EM_COLDFIRE 52 // Motorola Coldfire +#define EM_68HC12 53 // Motorola M68HC12 +#define EM_MMA 54 // Fujitsu Multimedia Accelerator +#define EM_PCP 55 // Siemens PCP +#define EM_NCPU 56 // Sony nCPU embedded RISC processor +#define EM_NDR1 57 // Denso NDR1 microprocesspr +#define EM_STARCORE 58 // Motorola Star*Core processor +#define EM_ME16 59 // Toyota ME16 processor +#define EM_ST100 60 // STMicroelectronics ST100 processor +#define EM_TINYJ 61 // Advanced Logic Corp. TinyJ embedded processor +#define EM_X86_64 62 // Advanced Micro Devices X86-64 processor +#define EM_PDSP 63 // Sony DSP Processor +#define EM_PDP10 64 // Digital Equipment Corp. PDP-10 +#define EM_PDP11 65 // Digital Equipment Corp. PDP-11 +#define EM_FX66 66 // Siemens FX66 microcontroller +#define EM_ST9PLUS 67 // STMicroelectronics ST9+ 8/16 bit microcontroller +#define EM_ST7 68 // STMicroelectronics ST7 8-bit microcontroller +#define EM_68HC16 69 // Motorola MC68HC16 Microcontroller +#define EM_68HC11 70 // Motorola MC68HC11 Microcontroller +#define EM_68HC08 71 // Motorola MC68HC08 Microcontroller +#define EM_68HC05 72 // Motorola MC68HC05 Microcontroller +#define EM_SVX 73 // Silicon Graphics SVx +#define EM_ST19 74 // STMicroelectronics ST19 8-bit cpu +#define EM_VAX 75 // Digital VAX +#define EM_CRIS 76 // Axis Communications 32-bit embedded processor +#define EM_JAVELIN 77 // Infineon Technologies 32-bit embedded cpu +#define EM_FIREPATH 78 // Element 14 64-bit DSP processor +#define EM_ZSP 79 // LSI Logic's 16-bit DSP processor +#define EM_MMIX 80 // Donald Knuth's educational 64-bit processor +#define EM_HUANY 81 // Harvard's machine-independent format +#define EM_PRISM 82 // SiTera Prism +#define EM_AVR 83 // Atmel AVR 8-bit microcontroller +#define EM_FR30 84 // Fujitsu FR30 +#define EM_D10V 85 // Mitsubishi D10V +#define EM_D30V 86 // Mitsubishi D30V +#define EM_V850 87 // NEC v850 +#define EM_M32R 88 // Renesas M32R (formerly Mitsubishi M32R) +#define EM_MN10300 89 // Matsushita MN10300 +#define EM_MN10200 90 // Matsushita MN10200 +#define EM_PJ 91 // picoJava +#define EM_OPENRISC 92 // OpenRISC 32-bit embedded processor +#define EM_ARC_A5 93 // ARC Cores Tangent-A5 +#define EM_XTENSA 94 // Tensilica Xtensa Architecture +#define EM_VIDEOCORE 95 // Alphamosaic VideoCore processor +#define EM_TMM_GPP 96 // Thompson Multimedia General Purpose Processor +#define EM_NS32K 97 // National Semiconductor 32000 series +#define EM_TPC 98 // Tenor Network TPC processor +#define EM_SNP1K 99 // Trebia SNP 1000 processor +#define EM_ST200 100 // STMicroelectronics ST200 microcontroller +#define EM_IP2K 101 // Ubicom IP2022 micro controller +#define EM_MAX 102 // MAX Processor +#define EM_CR 103 // National Semiconductor CompactRISC +#define EM_F2MC16 104 // Fujitsu F2MC16 +#define EM_MSP430 105 // TI msp430 micro controller +#define EM_BLACKFIN 106 // ADI Blackfin +#define EM_SE_C33 107 // S1C33 Family of Seiko Epson processors +#define EM_SEP 108 // Sharp embedded microprocessor +#define EM_ARCA 109 // Arca RISC Microprocessor +#define EM_UNICORE \ + 110 // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University +#define EM_EXCESS 111 // eXcess: 16/32/64-bit configurable embedded CPU +#define EM_DXP 112 // Icera Semiconductor Inc. Deep Execution Processor +#define EM_ALTERA_NIOS2 113 // Altera Nios II soft-core processor +#define EM_CRX 114 // National Semiconductor CRX +#define EM_XGATE 115 // Motorola XGATE embedded processor +#define EM_C166 116 // Infineon C16x/XC16x processor +#define EM_M16C 117 // Renesas M16C series microprocessors +#define EM_DSPIC30F \ + 118 // Microchip Technology dsPIC30F Digital Signal Controller #define EM_CE 119 // Freescale Communication Engine RISC core #define EM_M32C 120 // Renesas M32C series microprocessors #define EM_res121 121 // Reserved @@ -250,7 +252,8 @@ typedef uint64_t Elf64_Off; #define EM_QDSP6 164 // QUALCOMM DSP6 Processor #define EM_8051 165 // Intel 8051 and variants #define EM_STXP7X 166 // STMicroelectronics STxP7x family -#define EM_NDS32 167 // Andes Technology compact code size embedded RISC processor family +#define EM_NDS32 \ + 167 // Andes Technology compact code size embedded RISC processor family #define EM_ECOG1 168 // Cyan Technology eCOG1X family #define EM_ECOG1X 168 // Cyan Technology eCOG1X family #define EM_MAXQ30 169 // Dallas Semiconductor MAXQ30 Core Micro-controllers @@ -274,7 +277,7 @@ typedef uint64_t Elf64_Off; #define EM_TILE64 187 // Tilera TILE64 multicore architecture family #define EM_TILEPRO 188 // Tilera TILEPro multicore architecture family #define EM_MICROBLAZE 189 // Xilinx MicroBlaze 32-bit RISC soft processor core -#define EM_CUDA 190 // NVIDIA CUDA architecture +#define EM_CUDA 190 // NVIDIA CUDA architecture #define EM_TILEGX 191 // Tilera TILE-Gx multicore architecture family #define EM_CLOUDSHIELD 192 // CloudShield architecture family #define EM_COREA_1ST 193 // KIPO-KAIST Core-A 1st generation processor family @@ -324,23 +327,23 @@ typedef uint64_t Elf64_Off; #define EV_CURRENT 1 // Identification index -#define EI_MAG0 0 -#define EI_MAG1 1 -#define EI_MAG2 2 -#define EI_MAG3 3 -#define EI_CLASS 4 -#define EI_DATA 5 -#define EI_VERSION 6 -#define EI_OSABI 7 -#define EI_ABIVERSION 8 -#define EI_PAD 9 +#define EI_MAG0 0 +#define EI_MAG1 1 +#define EI_MAG2 2 +#define EI_MAG3 3 +#define EI_CLASS 4 +#define EI_DATA 5 +#define EI_VERSION 6 +#define EI_OSABI 7 +#define EI_ABIVERSION 8 +#define EI_PAD 9 #define EI_NIDENT 16 // Magic number #define ELFMAG0 0x7F -#define ELFMAG1 'E' -#define ELFMAG2 'L' -#define ELFMAG3 'F' +#define ELFMAG1 'E' +#define ELFMAG2 'L' +#define ELFMAG3 'F' // File class #define ELFCLASSNONE 0 @@ -353,34 +356,37 @@ typedef uint64_t Elf64_Off; #define ELFDATA2MSB 2 // OS extensions -#define ELFOSABI_NONE 0 // No extensions or unspecified -#define ELFOSABI_HPUX 1 // Hewlett-Packard HP-UX -#define ELFOSABI_NETBSD 2 // NetBSD -#define ELFOSABI_LINUX 3 // Linux -#define ELFOSABI_SOLARIS 6 // Sun Solaris -#define ELFOSABI_AIX 7 // AIX -#define ELFOSABI_IRIX 8 // IRIX -#define ELFOSABI_FREEBSD 9 // FreeBSD -#define ELFOSABI_TRU64 10 // Compaq TRU64 UNIX -#define ELFOSABI_MODESTO 11 // Novell Modesto -#define ELFOSABI_OPENBSD 12 // Open BSD -#define ELFOSABI_OPENVMS 13 // Open VMS -#define ELFOSABI_NSK 14 // Hewlett-Packard Non-Stop Kernel -#define ELFOSABI_AROS 15 // Amiga Research OS -#define ELFOSABI_FENIXOS 16 // The FenixOS highly scalable multi-core OS +#define ELFOSABI_NONE 0 // No extensions or unspecified +#define ELFOSABI_HPUX 1 // Hewlett-Packard HP-UX +#define ELFOSABI_NETBSD 2 // NetBSD +#define ELFOSABI_LINUX 3 // Linux +#define ELFOSABI_SOLARIS 6 // Sun Solaris +#define ELFOSABI_AIX 7 // AIX +#define ELFOSABI_IRIX 8 // IRIX +#define ELFOSABI_FREEBSD 9 // FreeBSD +#define ELFOSABI_TRU64 10 // Compaq TRU64 UNIX +#define ELFOSABI_MODESTO 11 // Novell Modesto +#define ELFOSABI_OPENBSD 12 // Open BSD +#define ELFOSABI_OPENVMS 13 // Open VMS +#define ELFOSABI_NSK 14 // Hewlett-Packard Non-Stop Kernel +#define ELFOSABI_AROS 15 // Amiga Research OS +#define ELFOSABI_FENIXOS 16 // The FenixOS highly scalable multi-core OS // 64-255 Architecture-specific value range -#define ELFOSABI_AMDGPU_HSA 64 // AMDGPU OS for HSA compatible compute - // kernels. -#define ELFOSABI_AMDGPU_PAL 65 // AMDGPU OS for AMD PAL compatible graphics - // shaders and compute kernels. -#define ELFOSABI_AMDGPU_MESA3D 66 // AMDGPU OS for Mesa3D compatible graphics - // shaders and compute kernels. - +#define ELFOSABI_AMDGPU_HSA \ + 64 // AMDGPU OS for HSA compatible compute \ + // kernels. +#define ELFOSABI_AMDGPU_PAL \ + 65 // AMDGPU OS for AMD PAL compatible graphics \ + // shaders and compute kernels. +#define ELFOSABI_AMDGPU_MESA3D \ + 66 // AMDGPU OS for Mesa3D compatible graphics \ + // shaders and compute kernels. // AMDGPU specific e_flags -#define EF_AMDGPU_MACH 0x0ff // AMDGPU processor selection mask. -#define EF_AMDGPU_XNACK 0x100 // Indicates if the XNACK target feature is - // enabled for all code contained in the ELF. +#define EF_AMDGPU_MACH 0x0ff // AMDGPU processor selection mask. +#define EF_AMDGPU_XNACK \ + 0x100 // Indicates if the XNACK target feature is \ + // enabled for all code contained in the ELF. // AMDGPU processors #define EF_AMDGPU_MACH_NONE 0x000 // Unspecified processor. #define EF_AMDGPU_MACH_R600_R600 0x001 @@ -427,7 +433,7 @@ typedef uint64_t Elf64_Off; // Sections constants // Section indexes -#define SHN_UNDEF 0 +#define SHN_UNDEF 0 #define SHN_LORESERVE 0xFF00 #define SHN_LOPROC 0xFF00 #define SHN_HIPROC 0xFF1F @@ -439,23 +445,23 @@ typedef uint64_t Elf64_Off; #define SHN_HIRESERVE 0xFFFF // Section types -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_HASH 5 -#define SHT_DYNAMIC 6 -#define SHT_NOTE 7 -#define SHT_NOBITS 8 -#define SHT_REL 9 -#define SHT_SHLIB 10 -#define SHT_DYNSYM 11 -#define SHT_INIT_ARRAY 14 -#define SHT_FINI_ARRAY 15 -#define SHT_PREINIT_ARRAY 16 -#define SHT_GROUP 17 -#define SHT_SYMTAB_SHNDX 18 +#define SHT_NULL 0 +#define SHT_PROGBITS 1 +#define SHT_SYMTAB 2 +#define SHT_STRTAB 3 +#define SHT_RELA 4 +#define SHT_HASH 5 +#define SHT_DYNAMIC 6 +#define SHT_NOTE 7 +#define SHT_NOBITS 8 +#define SHT_REL 9 +#define SHT_SHLIB 10 +#define SHT_DYNSYM 11 +#define SHT_INIT_ARRAY 14 +#define SHT_FINI_ARRAY 15 +#define SHT_PREINIT_ARRAY 16 +#define SHT_GROUP 17 +#define SHT_SYMTAB_SHNDX 18 #define SHT_LOOS 0x60000000 #define SHT_HIOS 0x6fffffff #define SHT_LOPROC 0x70000000 @@ -464,28 +470,28 @@ typedef uint64_t Elf64_Off; #define SHT_HIUSER 0xFFFFFFFF // Section attribute flags -#define SHF_WRITE 0x1 -#define SHF_ALLOC 0x2 -#define SHF_EXECINSTR 0x4 -#define SHF_MERGE 0x10 -#define SHF_STRINGS 0x20 -#define SHF_INFO_LINK 0x40 -#define SHF_LINK_ORDER 0x80 -#define SHF_OS_NONCONFORMING 0x100 -#define SHF_GROUP 0x200 -#define SHF_TLS 0x400 +#define SHF_WRITE 0x1 +#define SHF_ALLOC 0x2 +#define SHF_EXECINSTR 0x4 +#define SHF_MERGE 0x10 +#define SHF_STRINGS 0x20 +#define SHF_INFO_LINK 0x40 +#define SHF_LINK_ORDER 0x80 +#define SHF_OS_NONCONFORMING 0x100 +#define SHF_GROUP 0x200 +#define SHF_TLS 0x400 #define SHF_MASKOS 0x0ff00000 #define SHF_MASKPROC 0xF0000000 // Section group flags -#define GRP_COMDAT 0x1 +#define GRP_COMDAT 0x1 #define GRP_MASKOS 0x0ff00000 #define GRP_MASKPROC 0xf0000000 // Symbol binding -#define STB_LOCAL 0 -#define STB_GLOBAL 1 -#define STB_WEAK 2 +#define STB_LOCAL 0 +#define STB_GLOBAL 1 +#define STB_WEAK 2 #define STB_LOOS 10 #define STB_HIOS 12 #define STB_MULTIDEF 13 @@ -493,19 +499,19 @@ typedef uint64_t Elf64_Off; #define STB_HIPROC 15 // Note types -#define NT_AMDGPU_METADATA 1 +#define NT_AMDGPU_METADATA 1 #define NT_AMD_AMDGPU_HSA_METADATA 10 #define NT_AMD_AMDGPU_ISA 11 #define NT_AMD_AMDGPU_PAL_METADATA 12 // Symbol types -#define STT_NOTYPE 0 -#define STT_OBJECT 1 -#define STT_FUNC 2 -#define STT_SECTION 3 -#define STT_FILE 4 -#define STT_COMMON 5 -#define STT_TLS 6 +#define STT_NOTYPE 0 +#define STT_OBJECT 1 +#define STT_FUNC 2 +#define STT_SECTION 3 +#define STT_FILE 4 +#define STT_COMMON 5 +#define STT_TLS 6 #define STT_LOOS 10 #define STT_AMDGPU_HSA_KERNEL 10 #define STT_HIOS 12 @@ -522,161 +528,161 @@ typedef uint64_t Elf64_Off; #define STN_UNDEF 0 // Relocation types -#define R_386_NONE 0 -#define R_X86_64_NONE 0 -#define R_AMDGPU_NONE 0 -#define R_386_32 1 -#define R_X86_64_64 1 -#define R_AMDGPU_ABS32_LO 1 -#define R_386_PC32 2 -#define R_X86_64_PC32 2 -#define R_AMDGPU_ABS32_HI 2 -#define R_386_GOT32 3 -#define R_X86_64_GOT32 3 -#define R_AMDGPU_ABS64 3 -#define R_386_PLT32 4 -#define R_X86_64_PLT32 4 -#define R_AMDGPU_REL32 4 -#define R_386_COPY 5 -#define R_X86_64_COPY 5 -#define R_AMDGPU_REL64 5 -#define R_386_GLOB_DAT 6 -#define R_X86_64_GLOB_DAT 6 -#define R_AMDGPU_ABS32 6 -#define R_386_JMP_SLOT 7 -#define R_X86_64_JUMP_SLOT 7 -#define R_AMDGPU_GOTPCREL 7 -#define R_386_RELATIVE 8 -#define R_X86_64_RELATIVE 8 -#define R_AMDGPU_GOTPCREL32_LO 8 -#define R_386_GOTOFF 9 -#define R_X86_64_GOTPCREL 9 -#define R_AMDGPU_GOTPCREL32_HI 9 -#define R_386_GOTPC 10 -#define R_X86_64_32 10 -#define R_AMDGPU_REL32_LO 10 -#define R_386_32PLT 11 -#define R_X86_64_32S 11 -#define R_AMDGPU_REL32_HI 11 -#define R_X86_64_16 12 -#define R_X86_64_PC16 13 -#define R_AMDGPU_RELATIVE64 13 -#define R_386_TLS_TPOFF 14 -#define R_X86_64_8 14 -#define R_386_TLS_IE 15 -#define R_X86_64_PC8 15 -#define R_386_TLS_GOTIE 16 -#define R_X86_64_DTPMOD64 16 -#define R_386_TLS_LE 17 -#define R_X86_64_DTPOFF64 17 -#define R_386_TLS_GD 18 -#define R_X86_64_TPOFF64 18 -#define R_386_TLS_LDM 19 -#define R_X86_64_TLSGD 19 -#define R_386_16 20 -#define R_X86_64_TLSLD 20 -#define R_386_PC16 21 -#define R_X86_64_DTPOFF32 21 -#define R_386_8 22 -#define R_X86_64_GOTTPOFF 22 -#define R_386_PC8 23 -#define R_X86_64_TPOFF32 23 -#define R_386_TLS_GD_32 24 -#define R_X86_64_PC64 24 -#define R_386_TLS_GD_PUSH 25 -#define R_X86_64_GOTOFF64 25 -#define R_386_TLS_GD_CALL 26 -#define R_X86_64_GOTPC32 26 -#define R_386_TLS_GD_POP 27 -#define R_X86_64_GOT64 27 -#define R_386_TLS_LDM_32 28 -#define R_X86_64_GOTPCREL64 28 -#define R_386_TLS_LDM_PUSH 29 -#define R_X86_64_GOTPC64 29 -#define R_386_TLS_LDM_CALL 30 -#define R_X86_64_GOTPLT64 30 -#define R_386_TLS_LDM_POP 31 -#define R_X86_64_PLTOFF64 31 -#define R_386_TLS_LDO_32 32 -#define R_386_TLS_IE_32 33 -#define R_386_TLS_LE_32 34 -#define R_X86_64_GOTPC32_TLSDESC 34 -#define R_386_TLS_DTPMOD32 35 -#define R_X86_64_TLSDESC_CALL 35 -#define R_386_TLS_DTPOFF32 36 -#define R_X86_64_TLSDESC 36 -#define R_386_TLS_TPOFF32 37 -#define R_X86_64_IRELATIVE 37 -#define R_386_SIZE32 38 -#define R_386_TLS_GOTDESC 39 -#define R_386_TLS_DESC_CALL 40 -#define R_386_TLS_DESC 41 -#define R_386_IRELATIVE 42 -#define R_386_GOT32X 43 -#define R_X86_64_GNU_VTINHERIT 250 -#define R_X86_64_GNU_VTENTRY 251 +#define R_386_NONE 0 +#define R_X86_64_NONE 0 +#define R_AMDGPU_NONE 0 +#define R_386_32 1 +#define R_X86_64_64 1 +#define R_AMDGPU_ABS32_LO 1 +#define R_386_PC32 2 +#define R_X86_64_PC32 2 +#define R_AMDGPU_ABS32_HI 2 +#define R_386_GOT32 3 +#define R_X86_64_GOT32 3 +#define R_AMDGPU_ABS64 3 +#define R_386_PLT32 4 +#define R_X86_64_PLT32 4 +#define R_AMDGPU_REL32 4 +#define R_386_COPY 5 +#define R_X86_64_COPY 5 +#define R_AMDGPU_REL64 5 +#define R_386_GLOB_DAT 6 +#define R_X86_64_GLOB_DAT 6 +#define R_AMDGPU_ABS32 6 +#define R_386_JMP_SLOT 7 +#define R_X86_64_JUMP_SLOT 7 +#define R_AMDGPU_GOTPCREL 7 +#define R_386_RELATIVE 8 +#define R_X86_64_RELATIVE 8 +#define R_AMDGPU_GOTPCREL32_LO 8 +#define R_386_GOTOFF 9 +#define R_X86_64_GOTPCREL 9 +#define R_AMDGPU_GOTPCREL32_HI 9 +#define R_386_GOTPC 10 +#define R_X86_64_32 10 +#define R_AMDGPU_REL32_LO 10 +#define R_386_32PLT 11 +#define R_X86_64_32S 11 +#define R_AMDGPU_REL32_HI 11 +#define R_X86_64_16 12 +#define R_X86_64_PC16 13 +#define R_AMDGPU_RELATIVE64 13 +#define R_386_TLS_TPOFF 14 +#define R_X86_64_8 14 +#define R_386_TLS_IE 15 +#define R_X86_64_PC8 15 +#define R_386_TLS_GOTIE 16 +#define R_X86_64_DTPMOD64 16 +#define R_386_TLS_LE 17 +#define R_X86_64_DTPOFF64 17 +#define R_386_TLS_GD 18 +#define R_X86_64_TPOFF64 18 +#define R_386_TLS_LDM 19 +#define R_X86_64_TLSGD 19 +#define R_386_16 20 +#define R_X86_64_TLSLD 20 +#define R_386_PC16 21 +#define R_X86_64_DTPOFF32 21 +#define R_386_8 22 +#define R_X86_64_GOTTPOFF 22 +#define R_386_PC8 23 +#define R_X86_64_TPOFF32 23 +#define R_386_TLS_GD_32 24 +#define R_X86_64_PC64 24 +#define R_386_TLS_GD_PUSH 25 +#define R_X86_64_GOTOFF64 25 +#define R_386_TLS_GD_CALL 26 +#define R_X86_64_GOTPC32 26 +#define R_386_TLS_GD_POP 27 +#define R_X86_64_GOT64 27 +#define R_386_TLS_LDM_32 28 +#define R_X86_64_GOTPCREL64 28 +#define R_386_TLS_LDM_PUSH 29 +#define R_X86_64_GOTPC64 29 +#define R_386_TLS_LDM_CALL 30 +#define R_X86_64_GOTPLT64 30 +#define R_386_TLS_LDM_POP 31 +#define R_X86_64_PLTOFF64 31 +#define R_386_TLS_LDO_32 32 +#define R_386_TLS_IE_32 33 +#define R_386_TLS_LE_32 34 +#define R_X86_64_GOTPC32_TLSDESC 34 +#define R_386_TLS_DTPMOD32 35 +#define R_X86_64_TLSDESC_CALL 35 +#define R_386_TLS_DTPOFF32 36 +#define R_X86_64_TLSDESC 36 +#define R_386_TLS_TPOFF32 37 +#define R_X86_64_IRELATIVE 37 +#define R_386_SIZE32 38 +#define R_386_TLS_GOTDESC 39 +#define R_386_TLS_DESC_CALL 40 +#define R_386_TLS_DESC 41 +#define R_386_IRELATIVE 42 +#define R_386_GOT32X 43 +#define R_X86_64_GNU_VTINHERIT 250 +#define R_X86_64_GNU_VTENTRY 251 // Segment types -#define PT_NULL 0 -#define PT_LOAD 1 -#define PT_DYNAMIC 2 -#define PT_INTERP 3 -#define PT_NOTE 4 -#define PT_SHLIB 5 -#define PT_PHDR 6 -#define PT_TLS 7 +#define PT_NULL 0 +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_TLS 7 #define PT_LOOS 0x60000000 #define PT_HIOS 0x6fffffff #define PT_LOPROC 0x70000000 #define PT_HIPROC 0x7FFFFFFF // Segment flags -#define PF_X 1 // Execute -#define PF_W 2 // Write -#define PF_R 4 // Read +#define PF_X 1 // Execute +#define PF_W 2 // Write +#define PF_R 4 // Read #define PF_MASKOS 0x0ff00000 // Unspecified #define PF_MASKPROC 0xf0000000 // Unspecified // Dynamic Array Tags -#define DT_NULL 0 -#define DT_NEEDED 1 -#define DT_PLTRELSZ 2 -#define DT_PLTGOT 3 -#define DT_HASH 4 -#define DT_STRTAB 5 -#define DT_SYMTAB 6 -#define DT_RELA 7 -#define DT_RELASZ 8 -#define DT_RELAENT 9 -#define DT_STRSZ 10 -#define DT_SYMENT 11 -#define DT_INIT 12 -#define DT_FINI 13 -#define DT_SONAME 14 -#define DT_RPATH 15 -#define DT_SYMBOLIC 16 -#define DT_REL 17 -#define DT_RELSZ 18 -#define DT_RELENT 19 -#define DT_PLTREL 20 -#define DT_DEBUG 21 -#define DT_TEXTREL 22 -#define DT_JMPREL 23 -#define DT_BIND_NOW 24 -#define DT_INIT_ARRAY 25 -#define DT_FINI_ARRAY 26 -#define DT_INIT_ARRAYSZ 27 -#define DT_FINI_ARRAYSZ 28 -#define DT_RUNPATH 29 -#define DT_FLAGS 30 -#define DT_ENCODING 32 -#define DT_PREINIT_ARRAY 32 -#define DT_PREINIT_ARRAYSZ 33 -#define DT_MAXPOSTAGS 34 -#define DT_LOOS 0x6000000D -#define DT_HIOS 0x6ffff000 -#define DT_LOPROC 0x70000000 -#define DT_HIPROC 0x7FFFFFFF +#define DT_NULL 0 +#define DT_NEEDED 1 +#define DT_PLTRELSZ 2 +#define DT_PLTGOT 3 +#define DT_HASH 4 +#define DT_STRTAB 5 +#define DT_SYMTAB 6 +#define DT_RELA 7 +#define DT_RELASZ 8 +#define DT_RELAENT 9 +#define DT_STRSZ 10 +#define DT_SYMENT 11 +#define DT_INIT 12 +#define DT_FINI 13 +#define DT_SONAME 14 +#define DT_RPATH 15 +#define DT_SYMBOLIC 16 +#define DT_REL 17 +#define DT_RELSZ 18 +#define DT_RELENT 19 +#define DT_PLTREL 20 +#define DT_DEBUG 21 +#define DT_TEXTREL 22 +#define DT_JMPREL 23 +#define DT_BIND_NOW 24 +#define DT_INIT_ARRAY 25 +#define DT_FINI_ARRAY 26 +#define DT_INIT_ARRAYSZ 27 +#define DT_FINI_ARRAYSZ 28 +#define DT_RUNPATH 29 +#define DT_FLAGS 30 +#define DT_ENCODING 32 +#define DT_PREINIT_ARRAY 32 +#define DT_PREINIT_ARRAYSZ 33 +#define DT_MAXPOSTAGS 34 +#define DT_LOOS 0x6000000D +#define DT_HIOS 0x6ffff000 +#define DT_LOPROC 0x70000000 +#define DT_HIPROC 0x7FFFFFFF // DT_FLAGS values #define DF_ORIGIN 0x1 @@ -685,45 +691,46 @@ typedef uint64_t Elf64_Off; #define DF_BIND_NOW 0x8 #define DF_STATIC_TLS 0x10 - // ELF file header -struct Elf32_Ehdr { +struct Elf32_Ehdr +{ unsigned char e_ident[EI_NIDENT]; - Elf_Half e_type; - Elf_Half e_machine; - Elf_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf_Word e_flags; - Elf_Half e_ehsize; - Elf_Half e_phentsize; - Elf_Half e_phnum; - Elf_Half e_shentsize; - Elf_Half e_shnum; - Elf_Half e_shstrndx; + Elf_Half e_type; + Elf_Half e_machine; + Elf_Word e_version; + Elf32_Addr e_entry; + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf_Word e_flags; + Elf_Half e_ehsize; + Elf_Half e_phentsize; + Elf_Half e_phnum; + Elf_Half e_shentsize; + Elf_Half e_shnum; + Elf_Half e_shstrndx; }; -struct Elf64_Ehdr { +struct Elf64_Ehdr +{ unsigned char e_ident[EI_NIDENT]; - Elf_Half e_type; - Elf_Half e_machine; - Elf_Word e_version; - Elf64_Addr e_entry; - Elf64_Off e_phoff; - Elf64_Off e_shoff; - Elf_Word e_flags; - Elf_Half e_ehsize; - Elf_Half e_phentsize; - Elf_Half e_phnum; - Elf_Half e_shentsize; - Elf_Half e_shnum; - Elf_Half e_shstrndx; + Elf_Half e_type; + Elf_Half e_machine; + Elf_Word e_version; + Elf64_Addr e_entry; + Elf64_Off e_phoff; + Elf64_Off e_shoff; + Elf_Word e_flags; + Elf_Half e_ehsize; + Elf_Half e_phentsize; + Elf_Half e_phnum; + Elf_Half e_shentsize; + Elf_Half e_shnum; + Elf_Half e_shstrndx; }; - // Section header -struct Elf32_Shdr { +struct Elf32_Shdr +{ Elf_Word sh_name; Elf_Word sh_type; Elf_Word sh_flags; @@ -736,7 +743,8 @@ struct Elf32_Shdr { Elf_Word sh_entsize; }; -struct Elf64_Shdr { +struct Elf64_Shdr +{ Elf_Word sh_name; Elf_Word sh_type; Elf_Xword sh_flags; @@ -749,9 +757,9 @@ struct Elf64_Shdr { Elf_Xword sh_entsize; }; - // Segment header -struct Elf32_Phdr { +struct Elf32_Phdr +{ Elf_Word p_type; Elf32_Off p_offset; Elf32_Addr p_vaddr; @@ -762,7 +770,8 @@ struct Elf32_Phdr { Elf_Word p_align; }; -struct Elf64_Phdr { +struct Elf64_Phdr +{ Elf_Word p_type; Elf_Word p_flags; Elf64_Off p_offset; @@ -773,9 +782,9 @@ struct Elf64_Phdr { Elf_Xword p_align; }; - // Symbol table entry -struct Elf32_Sym { +struct Elf32_Sym +{ Elf_Word st_name; Elf32_Addr st_value; Elf_Word st_size; @@ -784,7 +793,8 @@ struct Elf32_Sym { Elf_Half st_shndx; }; -struct Elf64_Sym { +struct Elf64_Sym +{ Elf_Word st_name; unsigned char st_info; unsigned char st_other; @@ -793,48 +803,51 @@ struct Elf64_Sym { Elf_Xword st_size; }; +#define ELF_ST_BIND( i ) ( ( i ) >> 4 ) +#define ELF_ST_TYPE( i ) ( (i)&0xf ) +#define ELF_ST_INFO( b, t ) ( ( ( b ) << 4 ) + ( (t)&0xf ) ) -#define ELF_ST_BIND(i) ((i)>>4) -#define ELF_ST_TYPE(i) ((i)&0xf) -#define ELF_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) - -#define ELF_ST_VISIBILITY(o) ((o)&0x3) - +#define ELF_ST_VISIBILITY( o ) ( (o)&0x3 ) // Relocation entries -struct Elf32_Rel { +struct Elf32_Rel +{ Elf32_Addr r_offset; Elf_Word r_info; }; -struct Elf32_Rela { +struct Elf32_Rela +{ Elf32_Addr r_offset; Elf_Word r_info; Elf_Sword r_addend; }; -struct Elf64_Rel { +struct Elf64_Rel +{ Elf64_Addr r_offset; Elf_Xword r_info; }; -struct Elf64_Rela { +struct Elf64_Rela +{ Elf64_Addr r_offset; Elf_Xword r_info; Elf_Sxword r_addend; }; +#define ELF32_R_SYM( i ) ( ( i ) >> 8 ) +#define ELF32_R_TYPE( i ) ( (unsigned char)( i ) ) +#define ELF32_R_INFO( s, t ) ( ( ( s ) << 8 ) + (unsigned char)( t ) ) -#define ELF32_R_SYM(i) ((i)>>8) -#define ELF32_R_TYPE(i) ((unsigned char)(i)) -#define ELF32_R_INFO(s,t) (((s)<<8 )+(unsigned char)(t)) - -#define ELF64_R_SYM(i) ((i)>>32) -#define ELF64_R_TYPE(i) ((i)&0xffffffffL) -#define ELF64_R_INFO(s,t) ((((int64_t)(s))<<32)+((t)&0xffffffffL)) +#define ELF64_R_SYM( i ) ( ( i ) >> 32 ) +#define ELF64_R_TYPE( i ) ( (i)&0xffffffffL ) +#define ELF64_R_INFO( s, t ) \ + ( ( ( ( int64_t )( s ) ) << 32 ) + ( (t)&0xffffffffL ) ) // Dynamic structure -struct Elf32_Dyn { +struct Elf32_Dyn +{ Elf_Sword d_tag; union { Elf_Word d_val; @@ -842,7 +855,8 @@ struct Elf32_Dyn { } d_un; }; -struct Elf64_Dyn { +struct Elf64_Dyn +{ Elf_Sxword d_tag; union { Elf_Xword d_val; diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 3c05d3f4697cc96f99e24400fe796aa0b71f2deb..d8c222578376976707adbe380af02c1675d2ac31 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -24,10 +24,10 @@ THE SOFTWARE. #define ELFIO_HPP #ifdef _MSC_VER -#pragma warning ( push ) -#pragma warning(disable:4996) -#pragma warning(disable:4355) -#pragma warning(disable:4244) +#pragma warning( push ) +#pragma warning( disable : 4996 ) +#pragma warning( disable : 4355 ) +#pragma warning( disable : 4244 ) #endif #include <string> @@ -47,25 +47,19 @@ THE SOFTWARE. #include <elfio/elfio_strings.hpp> #define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \ -TYPE \ -get_##FNAME() const \ -{ \ - return header ? (header->get_##FNAME()) : 0; \ -} - -#define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \ -TYPE \ -get_##FNAME() const \ -{ \ - return header ? (header->get_##FNAME()) : 0; \ -} \ -void \ -set_##FNAME( TYPE val ) \ -{ \ - if (header) { \ - header->set_##FNAME( val ); \ - } \ -} \ + TYPE get_##FNAME() const { return header ? ( header->get_##FNAME() ) : 0; } + +#define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \ + TYPE get_##FNAME() const \ + { \ + return header ? ( header->get_##FNAME() ) : 0; \ + } \ + void set_##FNAME( TYPE val ) \ + { \ + if ( header ) { \ + header->set_##FNAME( val ); \ + } \ + } namespace ELFIO { @@ -73,7 +67,7 @@ namespace ELFIO { class elfio { public: -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ elfio() : sections( this ), segments( this ) { header = 0; @@ -81,13 +75,10 @@ class elfio create( ELFCLASS32, ELFDATA2LSB ); } -//------------------------------------------------------------------------------ - ~elfio() - { - clean(); - } + //------------------------------------------------------------------------------ + ~elfio() { clean(); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ void create( unsigned char file_class, unsigned char encoding ) { clean(); @@ -96,7 +87,7 @@ class elfio create_mandatory_sections(); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool load( const std::string& file_name ) { std::ifstream stream; @@ -105,11 +96,11 @@ class elfio return false; } - return load(stream); + return load( stream ); } -//------------------------------------------------------------------------------ - bool load( std::istream &stream ) + //------------------------------------------------------------------------------ + bool load( std::istream& stream ) { clean(); @@ -119,15 +110,13 @@ class elfio // Is it ELF file? if ( stream.gcount() != sizeof( e_ident ) || - e_ident[EI_MAG0] != ELFMAG0 || - e_ident[EI_MAG1] != ELFMAG1 || - e_ident[EI_MAG2] != ELFMAG2 || - e_ident[EI_MAG3] != ELFMAG3 ) { + e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 || + e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3 ) { return false; } if ( ( e_ident[EI_CLASS] != ELFCLASS64 ) && - ( e_ident[EI_CLASS] != ELFCLASS32 )) { + ( e_ident[EI_CLASS] != ELFCLASS32 ) ) { return false; } @@ -145,7 +134,7 @@ class elfio return is_still_good; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool save( const std::string& file_name ) { std::ofstream stream; @@ -154,13 +143,13 @@ class elfio return false; } - return save(stream); + return save( stream ); } -//------------------------------------------------------------------------------ - bool save( std::ostream &stream ) + //------------------------------------------------------------------------------ + bool save( std::ostream& stream ) { - if ( !stream || !header) { + if ( !stream || !header ) { return false; } @@ -170,13 +159,15 @@ class elfio // The position of the section table is variable and needs to be fixed // before saving. header->set_segments_num( segments.size() ); - header->set_segments_offset( segments.size() ? header->get_header_size() : 0 ); + header->set_segments_offset( segments.size() ? header->get_header_size() + : 0 ); header->set_sections_num( sections.size() ); header->set_sections_offset( 0 ); // Layout the first section right after the segment table current_file_pos = header->get_header_size() + - header->get_segment_entry_size() * (Elf_Xword)header->get_segments_num(); + header->get_segment_entry_size() * + (Elf_Xword)header->get_segments_num(); calc_segment_alignment(); @@ -191,36 +182,33 @@ class elfio return is_still_good; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ // ELF header access functions - ELFIO_HEADER_ACCESS_GET( unsigned char, class ); - ELFIO_HEADER_ACCESS_GET( unsigned char, elf_version ); - ELFIO_HEADER_ACCESS_GET( unsigned char, encoding ); - ELFIO_HEADER_ACCESS_GET( Elf_Word, version ); - ELFIO_HEADER_ACCESS_GET( Elf_Half, header_size ); - ELFIO_HEADER_ACCESS_GET( Elf_Half, section_entry_size ); - ELFIO_HEADER_ACCESS_GET( Elf_Half, segment_entry_size ); - - ELFIO_HEADER_ACCESS_GET_SET( unsigned char, os_abi ); - ELFIO_HEADER_ACCESS_GET_SET( unsigned char, abi_version ); - ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, type ); - ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, machine ); - ELFIO_HEADER_ACCESS_GET_SET( Elf_Word, flags ); - ELFIO_HEADER_ACCESS_GET_SET( Elf64_Addr, entry ); - ELFIO_HEADER_ACCESS_GET_SET( Elf64_Off, sections_offset ); - ELFIO_HEADER_ACCESS_GET_SET( Elf64_Off, segments_offset ); - ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, section_name_str_index ); - -//------------------------------------------------------------------------------ - const endianess_convertor& get_convertor() const - { - return convertor; - } - -//------------------------------------------------------------------------------ + ELFIO_HEADER_ACCESS_GET( unsigned char, class ); + ELFIO_HEADER_ACCESS_GET( unsigned char, elf_version ); + ELFIO_HEADER_ACCESS_GET( unsigned char, encoding ); + ELFIO_HEADER_ACCESS_GET( Elf_Word, version ); + ELFIO_HEADER_ACCESS_GET( Elf_Half, header_size ); + ELFIO_HEADER_ACCESS_GET( Elf_Half, section_entry_size ); + ELFIO_HEADER_ACCESS_GET( Elf_Half, segment_entry_size ); + + ELFIO_HEADER_ACCESS_GET_SET( unsigned char, os_abi ); + ELFIO_HEADER_ACCESS_GET_SET( unsigned char, abi_version ); + ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, type ); + ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, machine ); + ELFIO_HEADER_ACCESS_GET_SET( Elf_Word, flags ); + ELFIO_HEADER_ACCESS_GET_SET( Elf64_Addr, entry ); + ELFIO_HEADER_ACCESS_GET_SET( Elf64_Off, sections_offset ); + ELFIO_HEADER_ACCESS_GET_SET( Elf64_Off, segments_offset ); + ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, section_name_str_index ); + + //------------------------------------------------------------------------------ + const endianess_convertor& get_convertor() const { return convertor; } + + //------------------------------------------------------------------------------ Elf_Xword get_default_entry_size( Elf_Word section_type ) const { - switch( section_type ) { + switch ( section_type ) { case SHT_RELA: if ( header->get_class() == ELFCLASS64 ) { return sizeof( Elf64_Rela ); @@ -254,48 +242,51 @@ class elfio } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: - bool is_offset_in_section( Elf64_Off offset, const section* sec ) const { - return offset >= sec->get_offset() && offset < sec->get_offset()+sec->get_size(); - } + bool is_offset_in_section( Elf64_Off offset, const section* sec ) const + { + return ( offset >= sec->get_offset() ) && + ( offset < ( sec->get_offset() + sec->get_size() ) ); + } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ public: + //! returns an empty string if no problems are detected, + //! or a string containing an error message if problems are found + std::string validate() const + { + + // check for overlapping sections in the file + for ( int i = 0; i < sections.size(); ++i ) { + for ( int j = i + 1; j < sections.size(); ++j ) { + const section* a = sections[i]; + const section* b = sections[j]; + if ( !( a->get_type() & SHT_NOBITS ) && + !( b->get_type() & SHT_NOBITS ) && ( a->get_size() > 0 ) && + ( b->get_size() > 0 ) && ( a->get_offset() > 0 ) && + ( b->get_offset() > 0 ) ) { + if ( is_offset_in_section( a->get_offset(), b ) || + is_offset_in_section( + a->get_offset() + a->get_size() - 1, b ) || + is_offset_in_section( b->get_offset(), a ) || + is_offset_in_section( + b->get_offset() + b->get_size() - 1, a ) ) { + return "Sections " + a->get_name() + " and " + + b->get_name() + " overlap in file"; + } + } + } + } - //! returns an empty string if no problems are detected, - //! or a string containing an error message if problems are found - std::string validate() const { - - // check for overlapping sections in the file - for ( int i = 0; i < sections.size(); ++i) { - for ( int j = i+1; j < sections.size(); ++j ) { - const section* a = sections[i]; - const section* b = sections[j]; - if ( !(a->get_type() & SHT_NOBITS) - && !(b->get_type() & SHT_NOBITS) - && (a->get_size() > 0) - && (b->get_size() > 0) - && (a->get_offset() > 0) - && (b->get_offset() > 0)) { - if ( is_offset_in_section( a->get_offset(), b ) - || is_offset_in_section( a->get_offset()+a->get_size()-1, b ) - || is_offset_in_section( b->get_offset(), a ) - || is_offset_in_section( b->get_offset()+b->get_size()-1, a )) { - return "Sections " + a->get_name() + " and " + b->get_name() + " overlap in file"; - } - } - } - } - - // more checks to be added here... - - return ""; - } + // more checks to be added here... -//------------------------------------------------------------------------------ + return ""; + } + + //------------------------------------------------------------------------------ private: -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ void clean() { delete header; @@ -314,18 +305,19 @@ class elfio segments_.clear(); } -//------------------------------------------------------------------------------ - elf_header* create_header( unsigned char file_class, unsigned char encoding ) + //------------------------------------------------------------------------------ + elf_header* create_header( unsigned char file_class, + unsigned char encoding ) { elf_header* new_header = 0; if ( file_class == ELFCLASS64 ) { - new_header = new elf_header_impl< Elf64_Ehdr >( &convertor, - encoding ); + new_header = + new elf_header_impl<Elf64_Ehdr>( &convertor, encoding ); } else if ( file_class == ELFCLASS32 ) { - new_header = new elf_header_impl< Elf32_Ehdr >( &convertor, - encoding ); + new_header = + new elf_header_impl<Elf32_Ehdr>( &convertor, encoding ); } else { return 0; @@ -334,7 +326,7 @@ class elfio return new_header; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ section* create_section() { section* new_section; @@ -356,8 +348,7 @@ class elfio return new_section; } - -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ segment* create_segment() { segment* new_segment; @@ -379,7 +370,7 @@ class elfio return new_segment; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ void create_mandatory_sections() { // Create null section without calling to 'add_section' as no string @@ -395,7 +386,7 @@ class elfio shstrtab->set_addr_align( 1 ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ Elf_Half load_sections( std::istream& stream ) { Elf_Half entry_size = header->get_section_entry_size(); @@ -404,7 +395,8 @@ class elfio for ( Elf_Half i = 0; i < num; ++i ) { section* sec = create_section(); - sec->load( stream, (std::streamoff)offset + (std::streampos)i * entry_size ); + sec->load( stream, (std::streamoff)offset + + (std::streampos)i * entry_size ); sec->set_index( i ); // To mark that the section is not permitted to reassign address // during layout calculation @@ -427,19 +419,24 @@ class elfio return num; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ //! Checks whether the addresses of the section entirely fall within the given segment. //! It doesn't matter if the addresses are memory addresses, or file offsets, //! they just need to be in the same address space - bool is_sect_in_seg ( Elf64_Off sect_begin, Elf_Xword sect_size, Elf64_Off seg_begin, Elf64_Off seg_end ) { - return seg_begin <= sect_begin - && sect_begin + sect_size <= seg_end - && sect_begin < seg_end; // this is important criteria when sect_size == 0 - // Example: seg_begin=10, seg_end=12 (-> covering the bytes 10 and 11) - // sect_begin=12, sect_size=0 -> shall return false! + bool is_sect_in_seg( Elf64_Off sect_begin, + Elf_Xword sect_size, + Elf64_Off seg_begin, + Elf64_Off seg_end ) + { + return ( seg_begin <= sect_begin ) && + ( sect_begin + sect_size <= seg_end ) && + ( sect_begin < + seg_end ); // this is important criteria when sect_size == 0 + // Example: seg_begin=10, seg_end=12 (-> covering the bytes 10 and 11) + // sect_begin=12, sect_size=0 -> shall return false! } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool load_segments( std::istream& stream ) { Elf_Half entry_size = header->get_segment_entry_size(); @@ -447,7 +444,7 @@ class elfio Elf64_Off offset = header->get_segments_offset(); for ( Elf_Half i = 0; i < num; ++i ) { - segment* seg; + segment* seg; unsigned char file_class = header->get_class(); if ( file_class == ELFCLASS64 ) { @@ -460,7 +457,8 @@ class elfio return false; } - seg->load( stream, (std::streamoff)offset + (std::streampos)i * entry_size ); + seg->load( stream, (std::streamoff)offset + + (std::streampos)i * entry_size ); seg->set_index( i ); // Add sections to the segments (similar to readelfs algorithm) @@ -468,17 +466,20 @@ class elfio Elf64_Off segEndOffset = segBaseOffset + seg->get_file_size(); Elf64_Off segVBaseAddr = seg->get_virtual_address(); Elf64_Off segVEndAddr = segVBaseAddr + seg->get_memory_size(); - for( Elf_Half j = 0; j < sections.size(); ++j ) { + for ( Elf_Half j = 0; j < sections.size(); ++j ) { const section* psec = sections[j]; // SHF_ALLOC sections are matched based on the virtual address // otherwise the file offset is matched - if( psec->get_flags() & SHF_ALLOC - ? is_sect_in_seg( psec->get_address(), psec->get_size(), segVBaseAddr, segVEndAddr ) - : is_sect_in_seg( psec->get_offset(), psec->get_size(), segBaseOffset, segEndOffset )) { - // Alignment of segment shall not be updated, to preserve original value - // It will be re-calculated on saving. - seg->add_section_index( psec->get_index(), 0 ); + if ( ( psec->get_flags() & SHF_ALLOC ) + ? is_sect_in_seg( psec->get_address(), + psec->get_size(), segVBaseAddr, + segVEndAddr ) + : is_sect_in_seg( psec->get_offset(), psec->get_size(), + segBaseOffset, segEndOffset ) ) { + // Alignment of segment shall not be updated, to preserve original value + // It will be re-calculated on saving. + seg->add_section_index( psec->get_index(), 0 ); } } @@ -489,50 +490,49 @@ class elfio return true; } -//------------------------------------------------------------------------------ - bool save_header( std::ostream& stream ) - { - return header->save( stream ); - } + //------------------------------------------------------------------------------ + bool save_header( std::ostream& stream ) { return header->save( stream ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool save_sections( std::ostream& stream ) { for ( unsigned int i = 0; i < sections_.size(); ++i ) { - section *sec = sections_.at(i); + section* sec = sections_.at( i ); std::streampos headerPosition = (std::streamoff)header->get_sections_offset() + - (std::streampos)header->get_section_entry_size() * sec->get_index(); + (std::streampos)header->get_section_entry_size() * + sec->get_index(); - sec->save(stream,headerPosition,sec->get_offset()); + sec->save( stream, headerPosition, sec->get_offset() ); } return true; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool save_segments( std::ostream& stream ) { for ( unsigned int i = 0; i < segments_.size(); ++i ) { - segment *seg = segments_.at(i); + segment* seg = segments_.at( i ); - std::streampos headerPosition = header->get_segments_offset() + - (std::streampos)header->get_segment_entry_size()*seg->get_index(); + std::streampos headerPosition = + header->get_segments_offset() + + (std::streampos)header->get_segment_entry_size() * + seg->get_index(); seg->save( stream, headerPosition, seg->get_offset() ); } return true; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool is_section_without_segment( unsigned int section_index ) { bool found = false; for ( unsigned int j = 0; !found && ( j < segments.size() ); ++j ) { for ( unsigned int k = 0; - !found && ( k < segments[j]->get_sections_num() ); - ++k ) { + !found && ( k < segments[j]->get_sections_num() ); ++k ) { found = segments[j]->get_section_index_at( k ) == section_index; } } @@ -540,7 +540,7 @@ class elfio return !found; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool is_subsequence_of( segment* seg1, segment* seg2 ) { // Return 'true' if sections of seg1 are a subset of sections in seg2 @@ -548,7 +548,7 @@ class elfio const std::vector<Elf_Half>& sections2 = seg2->get_sections(); bool found = false; - if ( sections1.size() < sections2.size() ) { + if ( sections1.size() < sections2.size() ) { found = std::includes( sections2.begin(), sections2.end(), sections1.begin(), sections1.end() ); } @@ -556,31 +556,31 @@ class elfio return found; } -//------------------------------------------------------------------------------ - std::vector<segment*> get_ordered_segments( ) + //------------------------------------------------------------------------------ + std::vector<segment*> get_ordered_segments() { std::vector<segment*> res; std::deque<segment*> worklist; - res.reserve(segments.size()); + res.reserve( segments.size() ); std::copy( segments_.begin(), segments_.end(), - std::back_inserter( worklist )) ; + std::back_inserter( worklist ) ); // Bring the segments which start at address 0 to the front size_t nextSlot = 0; - for( size_t i = 0; i < worklist.size(); ++i ) { - if( i != nextSlot && worklist[i]->is_offset_initialized() - && worklist[i]->get_offset() == 0 ) { - if (worklist[nextSlot]->get_offset() == 0) { + for ( size_t i = 0; i < worklist.size(); ++i ) { + if ( i != nextSlot && worklist[i]->is_offset_initialized() && + worklist[i]->get_offset() == 0 ) { + if ( worklist[nextSlot]->get_offset() == 0 ) { ++nextSlot; } - std::swap(worklist[i],worklist[nextSlot]); + std::swap( worklist[i], worklist[nextSlot] ); ++nextSlot; } } while ( !worklist.empty() ) { - segment *seg = worklist.front(); + segment* seg = worklist.front(); worklist.pop_front(); size_t i = 0; @@ -591,33 +591,33 @@ class elfio } if ( i < worklist.size() ) - worklist.push_back(seg); + worklist.push_back( seg ); else - res.push_back(seg); + res.push_back( seg ); } return res; } - -//------------------------------------------------------------------------------ - bool layout_sections_without_segments( ) + //------------------------------------------------------------------------------ + bool layout_sections_without_segments() { for ( unsigned int i = 0; i < sections_.size(); ++i ) { if ( is_section_without_segment( i ) ) { - section *sec = sections_[i]; + section* sec = sections_[i]; Elf_Xword section_align = sec->get_addr_align(); - if ( section_align > 1 && current_file_pos % section_align != 0 ) { - current_file_pos += section_align - - current_file_pos % section_align; + if ( section_align > 1 && + current_file_pos % section_align != 0 ) { + current_file_pos += + section_align - current_file_pos % section_align; } if ( 0 != sec->get_index() ) - sec->set_offset(current_file_pos); + sec->set_offset( current_file_pos ); if ( SHT_NOBITS != sec->get_type() && - SHT_NULL != sec->get_type() ) { + SHT_NULL != sec->get_type() ) { current_file_pos += sec->get_size(); } } @@ -626,14 +626,14 @@ class elfio return true; } - -//------------------------------------------------------------------------------ - void calc_segment_alignment( ) + //------------------------------------------------------------------------------ + void calc_segment_alignment() { - for( std::vector<segment*>::iterator s = segments_.begin(); s != segments_.end(); ++s ) { + for ( std::vector<segment*>::iterator s = segments_.begin(); + s != segments_.end(); ++s ) { segment* seg = *s; for ( int i = 0; i < seg->get_sections_num(); ++i ) { - section* sect = sections_[ seg->get_section_index_at(i) ]; + section* sect = sections_[seg->get_section_index_at( i )]; if ( sect->get_addr_align() > seg->get_align() ) { seg->set_align( sect->get_addr_align() ); } @@ -641,11 +641,11 @@ class elfio } } -//------------------------------------------------------------------------------ - bool layout_segments_and_their_sections( ) + //------------------------------------------------------------------------------ + bool layout_segments_and_their_sections() { - std::vector<segment*> worklist; - std::vector<bool> section_generated(sections.size(),false); + std::vector<segment*> worklist; + std::vector<bool> section_generated( sections.size(), false ); // Get segments in a order in where segments which contain a // sub sequence of other segments are located at the end @@ -655,14 +655,15 @@ class elfio Elf_Xword segment_memory = 0; Elf_Xword segment_filesize = 0; Elf_Xword seg_start_pos = current_file_pos; - segment* seg = worklist[i]; + segment* seg = worklist[i]; // Special case: PHDR segment // This segment contains the program headers but no sections if ( seg->get_type() == PT_PHDR && seg->get_sections_num() == 0 ) { - seg_start_pos = header->get_segments_offset(); + seg_start_pos = header->get_segments_offset(); segment_memory = segment_filesize = - header->get_segment_entry_size() * (Elf_Xword)header->get_segments_num(); + header->get_segment_entry_size() * + (Elf_Xword)header->get_segments_num(); } // Special case: else if ( seg->is_offset_initialized() && seg->get_offset() == 0 ) { @@ -673,76 +674,82 @@ class elfio } // New segments with not generated sections // have to be aligned - else if ( seg->get_sections_num() - && !section_generated[seg->get_section_index_at( 0 )] ) { + else if ( seg->get_sections_num() && + !section_generated[seg->get_section_index_at( 0 )] ) { Elf_Xword align = seg->get_align() > 0 ? seg->get_align() : 1; Elf64_Off cur_page_alignment = current_file_pos % align; - Elf64_Off req_page_alignment = seg->get_virtual_address() % align; - Elf64_Off error = req_page_alignment - cur_page_alignment; + Elf64_Off req_page_alignment = + seg->get_virtual_address() % align; + Elf64_Off error = req_page_alignment - cur_page_alignment; current_file_pos += ( seg->get_align() + error ) % align; seg_start_pos = current_file_pos; } else if ( seg->get_sections_num() ) { - seg_start_pos = sections[seg->get_section_index_at( 0 )]->get_offset(); + seg_start_pos = + sections[seg->get_section_index_at( 0 )]->get_offset(); } // Write segment's data for ( unsigned int j = 0; j < seg->get_sections_num(); ++j ) { Elf_Half index = seg->get_section_index_at( j ); - section* sec = sections[ index ]; + section* sec = sections[index]; // The NULL section is always generated - if ( SHT_NULL == sec->get_type()) { + if ( SHT_NULL == sec->get_type() ) { section_generated[index] = true; continue; } Elf_Xword secAlign = 0; // Fix up the alignment - if ( !section_generated[index] && sec->is_address_initialized() - && SHT_NOBITS != sec->get_type() - && SHT_NULL != sec->get_type() - && 0 != sec->get_size() ) { + if ( !section_generated[index] && + sec->is_address_initialized() && + SHT_NOBITS != sec->get_type() && + SHT_NULL != sec->get_type() && 0 != sec->get_size() ) { // Align the sections based on the virtual addresses // when possible (this is what matters for execution) - Elf64_Off req_offset = sec->get_address() - seg->get_virtual_address(); + Elf64_Off req_offset = + sec->get_address() - seg->get_virtual_address(); Elf64_Off cur_offset = current_file_pos - seg_start_pos; - if ( req_offset < cur_offset) { - // something has gone awfully wrong, abort! - // secAlign would turn out negative, seeking backwards and overwriting previous data - return false; + if ( req_offset < cur_offset ) { + // something has gone awfully wrong, abort! + // secAlign would turn out negative, seeking backwards and overwriting previous data + return false; } - secAlign = req_offset - cur_offset; + secAlign = req_offset - cur_offset; } - else if (!section_generated[index] && !sec->is_address_initialized() ) { + else if ( !section_generated[index] && + !sec->is_address_initialized() ) { // If no address has been specified then only the section // alignment constraint has to be matched Elf_Xword align = sec->get_addr_align(); - if (align == 0) { + if ( align == 0 ) { align = 1; } Elf64_Off error = current_file_pos % align; - secAlign = ( align - error ) % align; + secAlign = ( align - error ) % align; } - else if (section_generated[index] ) { + else if ( section_generated[index] ) { // Alignment for already generated sections - secAlign = sec->get_offset() - seg_start_pos - segment_filesize; + secAlign = + sec->get_offset() - seg_start_pos - segment_filesize; } // Determine the segment file and memory sizes // Special case .tbss section (NOBITS) in non TLS segment - if ( (sec->get_flags() & SHF_ALLOC) - && !( (sec->get_flags() & SHF_TLS) && (seg->get_type() != PT_TLS) - && ( SHT_NOBITS == sec->get_type())) ) + if ( ( sec->get_flags() & SHF_ALLOC ) && + !( ( sec->get_flags() & SHF_TLS ) && + ( seg->get_type() != PT_TLS ) && + ( SHT_NOBITS == sec->get_type() ) ) ) segment_memory += sec->get_size() + secAlign; if ( SHT_NOBITS != sec->get_type() ) segment_filesize += sec->get_size() + secAlign; // Nothing to be done when generating nested segments - if(section_generated[index]) { + if ( section_generated[index] ) { continue; } @@ -750,14 +757,14 @@ class elfio // Set the section addresses when missing if ( !sec->is_address_initialized() ) - sec->set_address( seg->get_virtual_address() - + current_file_pos - seg_start_pos); + sec->set_address( seg->get_virtual_address() + + current_file_pos - seg_start_pos ); if ( 0 != sec->get_index() ) - sec->set_offset(current_file_pos); + sec->set_offset( current_file_pos ); if ( SHT_NOBITS != sec->get_type() ) - current_file_pos += sec->get_size(); + current_file_pos += sec->get_size(); section_generated[index] = true; } @@ -772,41 +779,35 @@ class elfio seg->set_memory_size( segment_memory ); } - seg->set_offset(seg_start_pos); + seg->set_offset( seg_start_pos ); } return true; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ bool layout_section_table() { // Simply place the section table at the end for now Elf64_Off alignmentError = current_file_pos % 4; current_file_pos += ( 4 - alignmentError ) % 4; - header->set_sections_offset(current_file_pos); + header->set_sections_offset( current_file_pos ); return true; } - -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ public: friend class Sections; - class Sections { + class Sections + { public: -//------------------------------------------------------------------------------ - Sections( elfio* parent_ ) : - parent( parent_ ) - { - } + //------------------------------------------------------------------------------ + Sections( elfio* parent_ ) : parent( parent_ ) {} -//------------------------------------------------------------------------------ - Elf_Half size() const - { - return (Elf_Half)parent->sections_.size(); - } + //------------------------------------------------------------------------------ + Elf_Half size() const { return (Elf_Half)parent->sections_.size(); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ section* operator[]( unsigned int index ) const { section* sec = 0; @@ -818,16 +819,15 @@ class elfio return sec; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ section* operator[]( const std::string& name ) const { section* sec = 0; std::vector<section*>::const_iterator it; - for ( it = parent->sections_.begin(); - it != parent->sections_.end(); + for ( it = parent->sections_.begin(); it != parent->sections_.end(); ++it ) { - if ( (*it)->get_name() == name ) { + if ( ( *it )->get_name() == name ) { sec = *it; break; } @@ -836,7 +836,7 @@ class elfio return sec; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ section* add( const std::string& name ) { section* new_section = parent->create_section(); @@ -845,93 +845,92 @@ class elfio Elf_Half str_index = parent->get_section_name_str_index(); section* string_table( parent->sections_[str_index] ); string_section_accessor str_writer( string_table ); - Elf_Word pos = str_writer.add_string( name ); + Elf_Word pos = str_writer.add_string( name ); new_section->set_name_string_offset( pos ); return new_section; } -//------------------------------------------------------------------------------ - std::vector<section*>::iterator begin() { + //------------------------------------------------------------------------------ + std::vector<section*>::iterator begin() + { return parent->sections_.begin(); } -//------------------------------------------------------------------------------ - std::vector<section*>::iterator end() { + //------------------------------------------------------------------------------ + std::vector<section*>::iterator end() + { return parent->sections_.end(); } -//------------------------------------------------------------------------------ - std::vector<section*>::const_iterator begin() const { + //------------------------------------------------------------------------------ + std::vector<section*>::const_iterator begin() const + { return parent->sections_.cbegin(); } -//------------------------------------------------------------------------------ - std::vector<section*>::const_iterator end() const { + //------------------------------------------------------------------------------ + std::vector<section*>::const_iterator end() const + { return parent->sections_.cend(); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: elfio* parent; } sections; -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ public: friend class Segments; - class Segments { + class Segments + { public: -//------------------------------------------------------------------------------ - Segments( elfio* parent_ ) : - parent( parent_ ) - { - } + //------------------------------------------------------------------------------ + Segments( elfio* parent_ ) : parent( parent_ ) {} -//------------------------------------------------------------------------------ - Elf_Half size() const - { - return (Elf_Half)parent->segments_.size(); - } + //------------------------------------------------------------------------------ + Elf_Half size() const { return (Elf_Half)parent->segments_.size(); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ segment* operator[]( unsigned int index ) const { return parent->segments_[index]; } + //------------------------------------------------------------------------------ + segment* add() { return parent->create_segment(); } -//------------------------------------------------------------------------------ - segment* add() + //------------------------------------------------------------------------------ + std::vector<segment*>::iterator begin() { - return parent->create_segment(); - } - -//------------------------------------------------------------------------------ - std::vector<segment*>::iterator begin() { return parent->segments_.begin(); } -//------------------------------------------------------------------------------ - std::vector<segment*>::iterator end() { + //------------------------------------------------------------------------------ + std::vector<segment*>::iterator end() + { return parent->segments_.end(); } -//------------------------------------------------------------------------------ - std::vector<segment*>::const_iterator begin() const { + //------------------------------------------------------------------------------ + std::vector<segment*>::const_iterator begin() const + { return parent->segments_.cbegin(); } -//------------------------------------------------------------------------------ - std::vector<segment*>::const_iterator end() const { + //------------------------------------------------------------------------------ + std::vector<segment*>::const_iterator end() const + { return parent->segments_.cend(); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: elfio* parent; } segments; -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: elf_header* header; std::vector<section*> sections_; @@ -949,7 +948,7 @@ class elfio #include <elfio/elfio_dynamic.hpp> #ifdef _MSC_VER -#pragma warning ( pop ) +#pragma warning( pop ) #endif #endif // ELFIO_HPP diff --git a/elfio/elfio_dump.hpp b/elfio/elfio_dump.hpp index a66486d61906c98ee540f48a07872fb2baef0fbe..ce9094297be1d01cdc47920773a009fd7e049ea8 100644 --- a/elfio/elfio_dump.hpp +++ b/elfio/elfio_dump.hpp @@ -32,385 +32,355 @@ THE SOFTWARE. namespace ELFIO { - -static struct class_table_t { +static struct class_table_t +{ const char key; const char* str; -} class_table [] = -{ +} class_table[] = { { ELFCLASS32, "ELF32" }, { ELFCLASS64, "ELF64" }, }; - -static struct endian_table_t { +static struct endian_table_t +{ const char key; const char* str; -} endian_table [] = -{ - { ELFDATANONE, "None" }, +} endian_table[] = { + { ELFDATANONE, "None" }, { ELFDATA2LSB, "Little endian" }, - { ELFDATA2MSB, "Big endian" }, + { ELFDATA2MSB, "Big endian" }, }; - -static struct version_table_t { +static struct version_table_t +{ const Elf64_Word key; const char* str; -} version_table [] = -{ - { EV_NONE , "None" }, +} version_table[] = { + { EV_NONE, "None" }, { EV_CURRENT, "Current" }, }; - -static struct type_table_t { +static struct type_table_t +{ const Elf32_Half key; const char* str; -} type_table [] = -{ - { ET_NONE, "No file type" }, - { ET_REL , "Relocatable file" }, - { ET_EXEC, "Executable file" }, - { ET_DYN , "Shared object file" }, - { ET_CORE, "Core file" }, +} type_table[] = { + { ET_NONE, "No file type" }, { ET_REL, "Relocatable file" }, + { ET_EXEC, "Executable file" }, { ET_DYN, "Shared object file" }, + { ET_CORE, "Core file" }, }; - -static struct machine_table_t { +static struct machine_table_t +{ const Elf64_Half key; const char* str; -} machine_table [] = -{ - { EM_NONE , "No machine" }, - { EM_M32 , "AT&T WE 32100" }, - { EM_SPARC , "SUN SPARC" }, - { EM_386 , "Intel 80386" }, - { EM_68K , "Motorola m68k family" }, - { EM_88K , "Motorola m88k family" }, - { EM_486 , "Intel 80486// Reserved for future use" }, - { EM_860 , "Intel 80860" }, - { EM_MIPS , "MIPS R3000 (officially, big-endian only)" }, - { EM_S370 , "IBM System/370" }, - { EM_MIPS_RS3_LE , "MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated" }, - { EM_res011 , "Reserved" }, - { EM_res012 , "Reserved" }, - { EM_res013 , "Reserved" }, - { EM_res014 , "Reserved" }, - { EM_PARISC , "HPPA" }, - { EM_res016 , "Reserved" }, - { EM_VPP550 , "Fujitsu VPP500" }, - { EM_SPARC32PLUS , "Sun's v8plus" }, - { EM_960 , "Intel 80960" }, - { EM_PPC , "PowerPC" }, - { EM_PPC64 , "64-bit PowerPC" }, - { EM_S390 , "IBM S/390" }, - { EM_SPU , "Sony/Toshiba/IBM SPU" }, - { EM_res024 , "Reserved" }, - { EM_res025 , "Reserved" }, - { EM_res026 , "Reserved" }, - { EM_res027 , "Reserved" }, - { EM_res028 , "Reserved" }, - { EM_res029 , "Reserved" }, - { EM_res030 , "Reserved" }, - { EM_res031 , "Reserved" }, - { EM_res032 , "Reserved" }, - { EM_res033 , "Reserved" }, - { EM_res034 , "Reserved" }, - { EM_res035 , "Reserved" }, - { EM_V800 , "NEC V800 series" }, - { EM_FR20 , "Fujitsu FR20" }, - { EM_RH32 , "TRW RH32" }, - { EM_MCORE , "Motorola M*Core // May also be taken by Fujitsu MMA" }, - { EM_RCE , "Old name for MCore" }, - { EM_ARM , "ARM" }, - { EM_OLD_ALPHA , "Digital Alpha" }, - { EM_SH , "Renesas (formerly Hitachi) / SuperH SH" }, - { EM_SPARCV9 , "SPARC v9 64-bit" }, - { EM_TRICORE , "Siemens Tricore embedded processor" }, - { EM_ARC , "ARC Cores" }, - { EM_H8_300 , "Renesas (formerly Hitachi) H8/300" }, - { EM_H8_300H , "Renesas (formerly Hitachi) H8/300H" }, - { EM_H8S , "Renesas (formerly Hitachi) H8S" }, - { EM_H8_500 , "Renesas (formerly Hitachi) H8/500" }, - { EM_IA_64 , "Intel IA-64 Processor" }, - { EM_MIPS_X , "Stanford MIPS-X" }, - { EM_COLDFIRE , "Motorola Coldfire" }, - { EM_68HC12 , "Motorola M68HC12" }, - { EM_MMA , "Fujitsu Multimedia Accelerator" }, - { EM_PCP , "Siemens PCP" }, - { EM_NCPU , "Sony nCPU embedded RISC processor" }, - { EM_NDR1 , "Denso NDR1 microprocesspr" }, - { EM_STARCORE , "Motorola Star*Core processor" }, - { EM_ME16 , "Toyota ME16 processor" }, - { EM_ST100 , "STMicroelectronics ST100 processor" }, - { EM_TINYJ , "Advanced Logic Corp. TinyJ embedded processor" }, - { EM_X86_64 , "Advanced Micro Devices X86-64 processor" }, - { EM_PDSP , "Sony DSP Processor" }, - { EM_PDP10 , "Digital Equipment Corp. PDP-10" }, - { EM_PDP11 , "Digital Equipment Corp. PDP-11" }, - { EM_FX66 , "Siemens FX66 microcontroller" }, - { EM_ST9PLUS , "STMicroelectronics ST9+ 8/16 bit microcontroller" }, - { EM_ST7 , "STMicroelectronics ST7 8-bit microcontroller" }, - { EM_68HC16 , "Motorola MC68HC16 Microcontroller" }, - { EM_68HC11 , "Motorola MC68HC11 Microcontroller" }, - { EM_68HC08 , "Motorola MC68HC08 Microcontroller" }, - { EM_68HC05 , "Motorola MC68HC05 Microcontroller" }, - { EM_SVX , "Silicon Graphics SVx" }, - { EM_ST19 , "STMicroelectronics ST19 8-bit cpu" }, - { EM_VAX , "Digital VAX" }, - { EM_CRIS , "Axis Communications 32-bit embedded processor" }, - { EM_JAVELIN , "Infineon Technologies 32-bit embedded cpu" }, - { EM_FIREPATH , "Element 14 64-bit DSP processor" }, - { EM_ZSP , "LSI Logic's 16-bit DSP processor" }, - { EM_MMIX , "Donald Knuth's educational 64-bit processor" }, - { EM_HUANY , "Harvard's machine-independent format" }, - { EM_PRISM , "SiTera Prism" }, - { EM_AVR , "Atmel AVR 8-bit microcontroller" }, - { EM_FR30 , "Fujitsu FR30" }, - { EM_D10V , "Mitsubishi D10V" }, - { EM_D30V , "Mitsubishi D30V" }, - { EM_V850 , "NEC v850" }, - { EM_M32R , "Renesas M32R (formerly Mitsubishi M32R)" }, - { EM_MN10300 , "Matsushita MN10300" }, - { EM_MN10200 , "Matsushita MN10200" }, - { EM_PJ , "picoJava" }, - { EM_OPENRISC , "OpenRISC 32-bit embedded processor" }, - { EM_ARC_A5 , "ARC Cores Tangent-A5" }, - { EM_XTENSA , "Tensilica Xtensa Architecture" }, - { EM_VIDEOCORE , "Alphamosaic VideoCore processor" }, - { EM_TMM_GPP , "Thompson Multimedia General Purpose Processor" }, - { EM_NS32K , "National Semiconductor 32000 series" }, - { EM_TPC , "Tenor Network TPC processor" }, - { EM_SNP1K , "Trebia SNP 1000 processor" }, - { EM_ST200 , "STMicroelectronics ST200 microcontroller" }, - { EM_IP2K , "Ubicom IP2022 micro controller" }, - { EM_MAX , "MAX Processor" }, - { EM_CR , "National Semiconductor CompactRISC" }, - { EM_F2MC16 , "Fujitsu F2MC16" }, - { EM_MSP430 , "TI msp430 micro controller" }, - { EM_BLACKFIN , "ADI Blackfin" }, - { EM_SE_C33 , "S1C33 Family of Seiko Epson processors" }, - { EM_SEP , "Sharp embedded microprocessor" }, - { EM_ARCA , "Arca RISC Microprocessor" }, - { EM_UNICORE , "Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University" }, - { EM_EXCESS , "eXcess: 16/32/64-bit configurable embedded CPU" }, - { EM_DXP , "Icera Semiconductor Inc. Deep Execution Processor" }, - { EM_ALTERA_NIOS2 , "Altera Nios II soft-core processor" }, - { EM_CRX , "National Semiconductor CRX" }, - { EM_XGATE , "Motorola XGATE embedded processor" }, - { EM_C166 , "Infineon C16x/XC16x processor" }, - { EM_M16C , "Renesas M16C series microprocessors" }, - { EM_DSPIC30F , "Microchip Technology dsPIC30F Digital Signal Controller" }, - { EM_CE , "Freescale Communication Engine RISC core" }, - { EM_M32C , "Renesas M32C series microprocessors" }, - { EM_res121 , "Reserved" }, - { EM_res122 , "Reserved" }, - { EM_res123 , "Reserved" }, - { EM_res124 , "Reserved" }, - { EM_res125 , "Reserved" }, - { EM_res126 , "Reserved" }, - { EM_res127 , "Reserved" }, - { EM_res128 , "Reserved" }, - { EM_res129 , "Reserved" }, - { EM_res130 , "Reserved" }, - { EM_TSK3000 , "Altium TSK3000 core" }, - { EM_RS08 , "Freescale RS08 embedded processor" }, - { EM_res133 , "Reserved" }, - { EM_ECOG2 , "Cyan Technology eCOG2 microprocessor" }, - { EM_SCORE , "Sunplus Score" }, - { EM_SCORE7 , "Sunplus S+core7 RISC processor" }, - { EM_DSP24 , "New Japan Radio (NJR) 24-bit DSP Processor" }, - { EM_VIDEOCORE3 , "Broadcom VideoCore III processor" }, - { EM_LATTICEMICO32, "RISC processor for Lattice FPGA architecture" }, - { EM_SE_C17 , "Seiko Epson C17 family" }, - { EM_TI_C6000 , "Texas Instruments TMS320C6000 DSP family" }, - { EM_TI_C2000 , "Texas Instruments TMS320C2000 DSP family" }, - { EM_TI_C5500 , "Texas Instruments TMS320C55x DSP family" }, - { EM_res143 , "Reserved" }, - { EM_res144 , "Reserved" }, - { EM_res145 , "Reserved" }, - { EM_res146 , "Reserved" }, - { EM_res147 , "Reserved" }, - { EM_res148 , "Reserved" }, - { EM_res149 , "Reserved" }, - { EM_res150 , "Reserved" }, - { EM_res151 , "Reserved" }, - { EM_res152 , "Reserved" }, - { EM_res153 , "Reserved" }, - { EM_res154 , "Reserved" }, - { EM_res155 , "Reserved" }, - { EM_res156 , "Reserved" }, - { EM_res157 , "Reserved" }, - { EM_res158 , "Reserved" }, - { EM_res159 , "Reserved" }, - { EM_MMDSP_PLUS , "STMicroelectronics 64bit VLIW Data Signal Processor" }, - { EM_CYPRESS_M8C , "Cypress M8C microprocessor" }, - { EM_R32C , "Renesas R32C series microprocessors" }, - { EM_TRIMEDIA , "NXP Semiconductors TriMedia architecture family" }, - { EM_QDSP6 , "QUALCOMM DSP6 Processor" }, - { EM_8051 , "Intel 8051 and variants" }, - { EM_STXP7X , "STMicroelectronics STxP7x family" }, - { EM_NDS32 , "Andes Technology compact code size embedded RISC processor family" }, - { EM_ECOG1 , "Cyan Technology eCOG1X family" }, - { EM_ECOG1X , "Cyan Technology eCOG1X family" }, - { EM_MAXQ30 , "Dallas Semiconductor MAXQ30 Core Micro-controllers" }, - { EM_XIMO16 , "New Japan Radio (NJR) 16-bit DSP Processor" }, - { EM_MANIK , "M2000 Reconfigurable RISC Microprocessor" }, - { EM_CRAYNV2 , "Cray Inc. NV2 vector architecture" }, - { EM_RX , "Renesas RX family" }, - { EM_METAG , "Imagination Technologies META processor architecture" }, - { EM_MCST_ELBRUS , "MCST Elbrus general purpose hardware architecture" }, - { EM_ECOG16 , "Cyan Technology eCOG16 family" }, - { EM_CR16 , "National Semiconductor CompactRISC 16-bit processor" }, - { EM_ETPU , "Freescale Extended Time Processing Unit" }, - { EM_SLE9X , "Infineon Technologies SLE9X core" }, - { EM_L1OM , "Intel L1OM" }, - { EM_INTEL181 , "Reserved by Intel" }, - { EM_INTEL182 , "Reserved by Intel" }, - { EM_res183 , "Reserved by ARM" }, - { EM_res184 , "Reserved by ARM" }, - { EM_AVR32 , "Atmel Corporation 32-bit microprocessor family" }, - { EM_STM8 , "STMicroeletronics STM8 8-bit microcontroller" }, - { EM_TILE64 , "Tilera TILE64 multicore architecture family" }, - { EM_TILEPRO , "Tilera TILEPro multicore architecture family" }, - { EM_MICROBLAZE , "Xilinx MicroBlaze 32-bit RISC soft processor core" }, - { EM_CUDA , "NVIDIA CUDA architecture " }, +} machine_table[] = { + { EM_NONE, "No machine" }, + { EM_M32, "AT&T WE 32100" }, + { EM_SPARC, "SUN SPARC" }, + { EM_386, "Intel 80386" }, + { EM_68K, "Motorola m68k family" }, + { EM_88K, "Motorola m88k family" }, + { EM_486, "Intel 80486// Reserved for future use" }, + { EM_860, "Intel 80860" }, + { EM_MIPS, "MIPS R3000 (officially, big-endian only)" }, + { EM_S370, "IBM System/370" }, + { EM_MIPS_RS3_LE, + "MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated" }, + { EM_res011, "Reserved" }, + { EM_res012, "Reserved" }, + { EM_res013, "Reserved" }, + { EM_res014, "Reserved" }, + { EM_PARISC, "HPPA" }, + { EM_res016, "Reserved" }, + { EM_VPP550, "Fujitsu VPP500" }, + { EM_SPARC32PLUS, "Sun's v8plus" }, + { EM_960, "Intel 80960" }, + { EM_PPC, "PowerPC" }, + { EM_PPC64, "64-bit PowerPC" }, + { EM_S390, "IBM S/390" }, + { EM_SPU, "Sony/Toshiba/IBM SPU" }, + { EM_res024, "Reserved" }, + { EM_res025, "Reserved" }, + { EM_res026, "Reserved" }, + { EM_res027, "Reserved" }, + { EM_res028, "Reserved" }, + { EM_res029, "Reserved" }, + { EM_res030, "Reserved" }, + { EM_res031, "Reserved" }, + { EM_res032, "Reserved" }, + { EM_res033, "Reserved" }, + { EM_res034, "Reserved" }, + { EM_res035, "Reserved" }, + { EM_V800, "NEC V800 series" }, + { EM_FR20, "Fujitsu FR20" }, + { EM_RH32, "TRW RH32" }, + { EM_MCORE, "Motorola M*Core // May also be taken by Fujitsu MMA" }, + { EM_RCE, "Old name for MCore" }, + { EM_ARM, "ARM" }, + { EM_OLD_ALPHA, "Digital Alpha" }, + { EM_SH, "Renesas (formerly Hitachi) / SuperH SH" }, + { EM_SPARCV9, "SPARC v9 64-bit" }, + { EM_TRICORE, "Siemens Tricore embedded processor" }, + { EM_ARC, "ARC Cores" }, + { EM_H8_300, "Renesas (formerly Hitachi) H8/300" }, + { EM_H8_300H, "Renesas (formerly Hitachi) H8/300H" }, + { EM_H8S, "Renesas (formerly Hitachi) H8S" }, + { EM_H8_500, "Renesas (formerly Hitachi) H8/500" }, + { EM_IA_64, "Intel IA-64 Processor" }, + { EM_MIPS_X, "Stanford MIPS-X" }, + { EM_COLDFIRE, "Motorola Coldfire" }, + { EM_68HC12, "Motorola M68HC12" }, + { EM_MMA, "Fujitsu Multimedia Accelerator" }, + { EM_PCP, "Siemens PCP" }, + { EM_NCPU, "Sony nCPU embedded RISC processor" }, + { EM_NDR1, "Denso NDR1 microprocesspr" }, + { EM_STARCORE, "Motorola Star*Core processor" }, + { EM_ME16, "Toyota ME16 processor" }, + { EM_ST100, "STMicroelectronics ST100 processor" }, + { EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor" }, + { EM_X86_64, "Advanced Micro Devices X86-64 processor" }, + { EM_PDSP, "Sony DSP Processor" }, + { EM_PDP10, "Digital Equipment Corp. PDP-10" }, + { EM_PDP11, "Digital Equipment Corp. PDP-11" }, + { EM_FX66, "Siemens FX66 microcontroller" }, + { EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller" }, + { EM_ST7, "STMicroelectronics ST7 8-bit microcontroller" }, + { EM_68HC16, "Motorola MC68HC16 Microcontroller" }, + { EM_68HC11, "Motorola MC68HC11 Microcontroller" }, + { EM_68HC08, "Motorola MC68HC08 Microcontroller" }, + { EM_68HC05, "Motorola MC68HC05 Microcontroller" }, + { EM_SVX, "Silicon Graphics SVx" }, + { EM_ST19, "STMicroelectronics ST19 8-bit cpu" }, + { EM_VAX, "Digital VAX" }, + { EM_CRIS, "Axis Communications 32-bit embedded processor" }, + { EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu" }, + { EM_FIREPATH, "Element 14 64-bit DSP processor" }, + { EM_ZSP, "LSI Logic's 16-bit DSP processor" }, + { EM_MMIX, "Donald Knuth's educational 64-bit processor" }, + { EM_HUANY, "Harvard's machine-independent format" }, + { EM_PRISM, "SiTera Prism" }, + { EM_AVR, "Atmel AVR 8-bit microcontroller" }, + { EM_FR30, "Fujitsu FR30" }, + { EM_D10V, "Mitsubishi D10V" }, + { EM_D30V, "Mitsubishi D30V" }, + { EM_V850, "NEC v850" }, + { EM_M32R, "Renesas M32R (formerly Mitsubishi M32R)" }, + { EM_MN10300, "Matsushita MN10300" }, + { EM_MN10200, "Matsushita MN10200" }, + { EM_PJ, "picoJava" }, + { EM_OPENRISC, "OpenRISC 32-bit embedded processor" }, + { EM_ARC_A5, "ARC Cores Tangent-A5" }, + { EM_XTENSA, "Tensilica Xtensa Architecture" }, + { EM_VIDEOCORE, "Alphamosaic VideoCore processor" }, + { EM_TMM_GPP, "Thompson Multimedia General Purpose Processor" }, + { EM_NS32K, "National Semiconductor 32000 series" }, + { EM_TPC, "Tenor Network TPC processor" }, + { EM_SNP1K, "Trebia SNP 1000 processor" }, + { EM_ST200, "STMicroelectronics ST200 microcontroller" }, + { EM_IP2K, "Ubicom IP2022 micro controller" }, + { EM_MAX, "MAX Processor" }, + { EM_CR, "National Semiconductor CompactRISC" }, + { EM_F2MC16, "Fujitsu F2MC16" }, + { EM_MSP430, "TI msp430 micro controller" }, + { EM_BLACKFIN, "ADI Blackfin" }, + { EM_SE_C33, "S1C33 Family of Seiko Epson processors" }, + { EM_SEP, "Sharp embedded microprocessor" }, + { EM_ARCA, "Arca RISC Microprocessor" }, + { EM_UNICORE, "Microprocessor series from PKU-Unity Ltd. and MPRC of " + "Peking University" }, + { EM_EXCESS, "eXcess: 16/32/64-bit configurable embedded CPU" }, + { EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor" }, + { EM_ALTERA_NIOS2, "Altera Nios II soft-core processor" }, + { EM_CRX, "National Semiconductor CRX" }, + { EM_XGATE, "Motorola XGATE embedded processor" }, + { EM_C166, "Infineon C16x/XC16x processor" }, + { EM_M16C, "Renesas M16C series microprocessors" }, + { EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller" }, + { EM_CE, "Freescale Communication Engine RISC core" }, + { EM_M32C, "Renesas M32C series microprocessors" }, + { EM_res121, "Reserved" }, + { EM_res122, "Reserved" }, + { EM_res123, "Reserved" }, + { EM_res124, "Reserved" }, + { EM_res125, "Reserved" }, + { EM_res126, "Reserved" }, + { EM_res127, "Reserved" }, + { EM_res128, "Reserved" }, + { EM_res129, "Reserved" }, + { EM_res130, "Reserved" }, + { EM_TSK3000, "Altium TSK3000 core" }, + { EM_RS08, "Freescale RS08 embedded processor" }, + { EM_res133, "Reserved" }, + { EM_ECOG2, "Cyan Technology eCOG2 microprocessor" }, + { EM_SCORE, "Sunplus Score" }, + { EM_SCORE7, "Sunplus S+core7 RISC processor" }, + { EM_DSP24, "New Japan Radio (NJR) 24-bit DSP Processor" }, + { EM_VIDEOCORE3, "Broadcom VideoCore III processor" }, + { EM_LATTICEMICO32, "RISC processor for Lattice FPGA architecture" }, + { EM_SE_C17, "Seiko Epson C17 family" }, + { EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family" }, + { EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family" }, + { EM_TI_C5500, "Texas Instruments TMS320C55x DSP family" }, + { EM_res143, "Reserved" }, + { EM_res144, "Reserved" }, + { EM_res145, "Reserved" }, + { EM_res146, "Reserved" }, + { EM_res147, "Reserved" }, + { EM_res148, "Reserved" }, + { EM_res149, "Reserved" }, + { EM_res150, "Reserved" }, + { EM_res151, "Reserved" }, + { EM_res152, "Reserved" }, + { EM_res153, "Reserved" }, + { EM_res154, "Reserved" }, + { EM_res155, "Reserved" }, + { EM_res156, "Reserved" }, + { EM_res157, "Reserved" }, + { EM_res158, "Reserved" }, + { EM_res159, "Reserved" }, + { EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor" }, + { EM_CYPRESS_M8C, "Cypress M8C microprocessor" }, + { EM_R32C, "Renesas R32C series microprocessors" }, + { EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family" }, + { EM_QDSP6, "QUALCOMM DSP6 Processor" }, + { EM_8051, "Intel 8051 and variants" }, + { EM_STXP7X, "STMicroelectronics STxP7x family" }, + { EM_NDS32, + "Andes Technology compact code size embedded RISC processor family" }, + { EM_ECOG1, "Cyan Technology eCOG1X family" }, + { EM_ECOG1X, "Cyan Technology eCOG1X family" }, + { EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core Micro-controllers" }, + { EM_XIMO16, "New Japan Radio (NJR) 16-bit DSP Processor" }, + { EM_MANIK, "M2000 Reconfigurable RISC Microprocessor" }, + { EM_CRAYNV2, "Cray Inc. NV2 vector architecture" }, + { EM_RX, "Renesas RX family" }, + { EM_METAG, "Imagination Technologies META processor architecture" }, + { EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture" }, + { EM_ECOG16, "Cyan Technology eCOG16 family" }, + { EM_CR16, "National Semiconductor CompactRISC 16-bit processor" }, + { EM_ETPU, "Freescale Extended Time Processing Unit" }, + { EM_SLE9X, "Infineon Technologies SLE9X core" }, + { EM_L1OM, "Intel L1OM" }, + { EM_INTEL181, "Reserved by Intel" }, + { EM_INTEL182, "Reserved by Intel" }, + { EM_res183, "Reserved by ARM" }, + { EM_res184, "Reserved by ARM" }, + { EM_AVR32, "Atmel Corporation 32-bit microprocessor family" }, + { EM_STM8, "STMicroeletronics STM8 8-bit microcontroller" }, + { EM_TILE64, "Tilera TILE64 multicore architecture family" }, + { EM_TILEPRO, "Tilera TILEPro multicore architecture family" }, + { EM_MICROBLAZE, "Xilinx MicroBlaze 32-bit RISC soft processor core" }, + { EM_CUDA, "NVIDIA CUDA architecture " }, }; - -static struct section_type_table_t { +static struct section_type_table_t +{ const Elf64_Half key; const char* str; -} section_type_table [] = -{ - { SHT_NULL , "NULL" }, - { SHT_PROGBITS , "PROGBITS" }, - { SHT_SYMTAB , "SYMTAB" }, - { SHT_STRTAB , "STRTAB" }, - { SHT_RELA , "RELA" }, - { SHT_HASH , "HASH" }, - { SHT_DYNAMIC , "DYNAMIC" }, - { SHT_NOTE , "NOTE" }, - { SHT_NOBITS , "NOBITS" }, - { SHT_REL , "REL" }, - { SHT_SHLIB , "SHLIB" }, - { SHT_DYNSYM , "DYNSYM" }, - { SHT_INIT_ARRAY , "INIT_ARRAY" }, - { SHT_FINI_ARRAY , "FINI_ARRAY" }, +} section_type_table[] = { + { SHT_NULL, "NULL" }, + { SHT_PROGBITS, "PROGBITS" }, + { SHT_SYMTAB, "SYMTAB" }, + { SHT_STRTAB, "STRTAB" }, + { SHT_RELA, "RELA" }, + { SHT_HASH, "HASH" }, + { SHT_DYNAMIC, "DYNAMIC" }, + { SHT_NOTE, "NOTE" }, + { SHT_NOBITS, "NOBITS" }, + { SHT_REL, "REL" }, + { SHT_SHLIB, "SHLIB" }, + { SHT_DYNSYM, "DYNSYM" }, + { SHT_INIT_ARRAY, "INIT_ARRAY" }, + { SHT_FINI_ARRAY, "FINI_ARRAY" }, { SHT_PREINIT_ARRAY, "PREINIT_ARRAY" }, - { SHT_GROUP , "GROUP" }, - { SHT_SYMTAB_SHNDX , "SYMTAB_SHNDX " }, + { SHT_GROUP, "GROUP" }, + { SHT_SYMTAB_SHNDX, "SYMTAB_SHNDX " }, }; - -static struct segment_type_table_t { +static struct segment_type_table_t +{ const Elf_Word key; const char* str; -} segment_type_table [] = -{ - { PT_NULL , "NULL" }, - { PT_LOAD , "LOAD" }, - { PT_DYNAMIC, "DYNAMIC" }, - { PT_INTERP , "INTERP" }, - { PT_NOTE , "NOTE" }, - { PT_SHLIB , "SHLIB" }, - { PT_PHDR , "PHDR" }, - { PT_TLS , "TLS" }, +} segment_type_table[] = { + { PT_NULL, "NULL" }, { PT_LOAD, "LOAD" }, { PT_DYNAMIC, "DYNAMIC" }, + { PT_INTERP, "INTERP" }, { PT_NOTE, "NOTE" }, { PT_SHLIB, "SHLIB" }, + { PT_PHDR, "PHDR" }, { PT_TLS, "TLS" }, }; - -static struct segment_flag_table_t { +static struct segment_flag_table_t +{ const Elf_Word key; const char* str; -} segment_flag_table [] = -{ - { 0, "" }, - { 1, "X" }, - { 2, "W" }, - { 3, "WX" }, - { 4, "R" }, - { 5, "RX" }, - { 6, "RW" }, - { 7, "RWX" }, +} segment_flag_table[] = { + { 0, "" }, { 1, "X" }, { 2, "W" }, { 3, "WX" }, + { 4, "R" }, { 5, "RX" }, { 6, "RW" }, { 7, "RWX" }, }; - -static struct symbol_bind_t { +static struct symbol_bind_t +{ const Elf_Word key; const char* str; -} symbol_bind_table [] = -{ - { STB_LOCAL , "LOCAL" }, - { STB_GLOBAL , "GLOBAL" }, - { STB_WEAK , "WEAK" }, - { STB_LOOS , "LOOS" }, - { STB_HIOS , "HIOS" }, - { STB_MULTIDEF, "MULTIDEF" }, - { STB_LOPROC , "LOPROC" }, - { STB_HIPROC , "HIPROC" }, +} symbol_bind_table[] = { + { STB_LOCAL, "LOCAL" }, { STB_GLOBAL, "GLOBAL" }, + { STB_WEAK, "WEAK" }, { STB_LOOS, "LOOS" }, + { STB_HIOS, "HIOS" }, { STB_MULTIDEF, "MULTIDEF" }, + { STB_LOPROC, "LOPROC" }, { STB_HIPROC, "HIPROC" }, }; - -static struct symbol_type_t { +static struct symbol_type_t +{ const Elf_Word key; const char* str; -} symbol_type_table [] = -{ - { STT_NOTYPE , "NOTYPE" }, - { STT_OBJECT , "OBJECT" }, - { STT_FUNC , "FUNC" }, - { STT_SECTION, "SECTION" }, - { STT_FILE , "FILE" }, - { STT_COMMON , "COMMON" }, - { STT_TLS , "TLS" }, - { STT_LOOS , "LOOS" }, - { STT_HIOS , "HIOS" }, - { STT_LOPROC , "LOPROC" }, - { STT_HIPROC , "HIPROC" }, +} symbol_type_table[] = { + { STT_NOTYPE, "NOTYPE" }, { STT_OBJECT, "OBJECT" }, + { STT_FUNC, "FUNC" }, { STT_SECTION, "SECTION" }, + { STT_FILE, "FILE" }, { STT_COMMON, "COMMON" }, + { STT_TLS, "TLS" }, { STT_LOOS, "LOOS" }, + { STT_HIOS, "HIOS" }, { STT_LOPROC, "LOPROC" }, + { STT_HIPROC, "HIPROC" }, }; - -static struct dynamic_tag_t { +static struct dynamic_tag_t +{ const Elf_Word key; const char* str; -} dynamic_tag_table [] = -{ - { DT_NULL , "NULL" }, - { DT_NEEDED , "NEEDED" }, - { DT_PLTRELSZ , "PLTRELSZ" }, - { DT_PLTGOT , "PLTGOT" }, - { DT_HASH , "HASH" }, - { DT_STRTAB , "STRTAB" }, - { DT_SYMTAB , "SYMTAB" }, - { DT_RELA , "RELA" }, - { DT_RELASZ , "RELASZ" }, - { DT_RELAENT , "RELAENT" }, - { DT_STRSZ , "STRSZ" }, - { DT_SYMENT , "SYMENT" }, - { DT_INIT , "INIT" }, - { DT_FINI , "FINI" }, - { DT_SONAME , "SONAME" }, - { DT_RPATH , "RPATH" }, - { DT_SYMBOLIC , "SYMBOLIC" }, - { DT_REL , "REL" }, - { DT_RELSZ , "RELSZ" }, - { DT_RELENT , "RELENT" }, - { DT_PLTREL , "PLTREL" }, - { DT_DEBUG , "DEBUG" }, - { DT_TEXTREL , "TEXTREL" }, - { DT_JMPREL , "JMPREL" }, - { DT_BIND_NOW , "BIND_NOW" }, - { DT_INIT_ARRAY , "INIT_ARRAY" }, - { DT_FINI_ARRAY , "FINI_ARRAY" }, - { DT_INIT_ARRAYSZ , "INIT_ARRAYSZ" }, - { DT_FINI_ARRAYSZ , "FINI_ARRAYSZ" }, - { DT_RUNPATH , "RUNPATH" }, - { DT_FLAGS , "FLAGS" }, - { DT_ENCODING , "ENCODING" }, - { DT_PREINIT_ARRAY , "PREINIT_ARRAY" }, +} dynamic_tag_table[] = { + { DT_NULL, "NULL" }, + { DT_NEEDED, "NEEDED" }, + { DT_PLTRELSZ, "PLTRELSZ" }, + { DT_PLTGOT, "PLTGOT" }, + { DT_HASH, "HASH" }, + { DT_STRTAB, "STRTAB" }, + { DT_SYMTAB, "SYMTAB" }, + { DT_RELA, "RELA" }, + { DT_RELASZ, "RELASZ" }, + { DT_RELAENT, "RELAENT" }, + { DT_STRSZ, "STRSZ" }, + { DT_SYMENT, "SYMENT" }, + { DT_INIT, "INIT" }, + { DT_FINI, "FINI" }, + { DT_SONAME, "SONAME" }, + { DT_RPATH, "RPATH" }, + { DT_SYMBOLIC, "SYMBOLIC" }, + { DT_REL, "REL" }, + { DT_RELSZ, "RELSZ" }, + { DT_RELENT, "RELENT" }, + { DT_PLTREL, "PLTREL" }, + { DT_DEBUG, "DEBUG" }, + { DT_TEXTREL, "TEXTREL" }, + { DT_JMPREL, "JMPREL" }, + { DT_BIND_NOW, "BIND_NOW" }, + { DT_INIT_ARRAY, "INIT_ARRAY" }, + { DT_FINI_ARRAY, "FINI_ARRAY" }, + { DT_INIT_ARRAYSZ, "INIT_ARRAYSZ" }, + { DT_FINI_ARRAYSZ, "FINI_ARRAYSZ" }, + { DT_RUNPATH, "RUNPATH" }, + { DT_FLAGS, "FLAGS" }, + { DT_ENCODING, "ENCODING" }, + { DT_PREINIT_ARRAY, "PREINIT_ARRAY" }, { DT_PREINIT_ARRAYSZ, "PREINIT_ARRAYSZ" }, - { DT_MAXPOSTAGS , "MAXPOSTAGS" }, + { DT_MAXPOSTAGS, "MAXPOSTAGS" }, }; static const ELFIO::Elf_Xword MAX_DATA_ENTRIES = 64; @@ -418,37 +388,41 @@ static const ELFIO::Elf_Xword MAX_DATA_ENTRIES = 64; //------------------------------------------------------------------------------ class dump { -#define DUMP_DEC_FORMAT( width ) std::setw(width) << std::setfill( ' ' ) << \ - std::dec << std::right -#define DUMP_HEX_FORMAT( width ) std::setw(width) << std::setfill( '0' ) << \ - std::hex << std::right -#define DUMP_STR_FORMAT( width ) std::setw(width) << std::setfill( ' ' ) << \ - std::hex << std::left +#define DUMP_DEC_FORMAT( width ) \ + std::setw( width ) << std::setfill( ' ' ) << std::dec << std::right +#define DUMP_HEX_FORMAT( width ) \ + std::setw( width ) << std::setfill( '0' ) << std::hex << std::right +#define DUMP_STR_FORMAT( width ) \ + std::setw( width ) << std::setfill( ' ' ) << std::hex << std::left public: -//------------------------------------------------------------------------------ - static void - header( std::ostream& out, const elfio& reader ) + //------------------------------------------------------------------------------ + static void header( std::ostream& out, const elfio& reader ) { - if (!reader.get_header_size()) - { + if ( !reader.get_header_size() ) { return; } - out << "ELF Header" << std::endl << std::endl - << " Class: " << str_class( reader.get_class() ) << std::endl - << " Encoding: " << str_endian( reader.get_encoding() ) << std::endl - << " ELFVersion: " << str_version( reader.get_elf_version() ) << std::endl - << " Type: " << str_type( reader.get_type() ) << std::endl - << " Machine: " << str_machine( reader.get_machine() ) << std::endl - << " Version: " << str_version( reader.get_version() ) << std::endl - << " Entry: " << "0x" << std::hex << reader.get_entry() << std::endl - << " Flags: " << "0x" << std::hex << reader.get_flags() << std::endl + out << "ELF Header" << std::endl + << std::endl + << " Class: " << str_class( reader.get_class() ) << std::endl + << " Encoding: " << str_endian( reader.get_encoding() ) + << std::endl + << " ELFVersion: " << str_version( reader.get_elf_version() ) + << std::endl + << " Type: " << str_type( reader.get_type() ) << std::endl + << " Machine: " << str_machine( reader.get_machine() ) + << std::endl + << " Version: " << str_version( reader.get_version() ) + << std::endl + << " Entry: " + << "0x" << std::hex << reader.get_entry() << std::endl + << " Flags: " + << "0x" << std::hex << reader.get_flags() << std::endl << std::endl; } -//------------------------------------------------------------------------------ - static void - section_headers( std::ostream& out, const elfio& reader ) + //------------------------------------------------------------------------------ + static void section_headers( std::ostream& out, const elfio& reader ) { Elf_Half n = reader.sections.size(); @@ -458,70 +432,67 @@ class dump out << "Section Headers:" << std::endl; if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit - out << "[ Nr ] Type Addr Size ES Flg Lk Inf Al Name" << std::endl; + out << "[ Nr ] Type Addr Size ES Flg Lk Inf " + "Al Name" + << std::endl; } - else { // Output for 64-bit - out << "[ Nr ] Type Addr Size ES Flg" << std::endl + else { // Output for 64-bit + out << "[ Nr ] Type Addr Size " + " ES Flg" + << std::endl << " Lk Inf Al Name" << std::endl; } - + for ( Elf_Half i = 0; i < n; ++i ) { // For all sections section* sec = reader.sections[i]; section_header( out, i, sec, reader.get_class() ); } - + out << "Key to Flags: W (write), A (alloc), X (execute)\n\n" << std::endl; } -//------------------------------------------------------------------------------ - static void - section_header( std::ostream& out, Elf_Half no, const section* sec, - unsigned char elf_class ) + //------------------------------------------------------------------------------ + static void section_header( std::ostream& out, + Elf_Half no, + const section* sec, + unsigned char elf_class ) { std::ios_base::fmtflags original_flags = out.flags(); if ( elf_class == ELFCLASS32 ) { // Output for 32-bit - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) << " " - << DUMP_HEX_FORMAT( 8 ) << sec->get_address() << " " - << DUMP_HEX_FORMAT( 8 ) << sec->get_size() << " " - << DUMP_HEX_FORMAT( 2 ) << sec->get_entry_size() << " " - << DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) << " " - << DUMP_HEX_FORMAT( 2 ) << sec->get_link() << " " - << DUMP_HEX_FORMAT( 3 ) << sec->get_info() << " " - << DUMP_HEX_FORMAT( 2 ) << sec->get_addr_align() << " " - << DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " - << std::endl; + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) + << " " << DUMP_HEX_FORMAT( 8 ) << sec->get_address() << " " + << DUMP_HEX_FORMAT( 8 ) << sec->get_size() << " " + << DUMP_HEX_FORMAT( 2 ) << sec->get_entry_size() << " " + << DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) + << " " << DUMP_HEX_FORMAT( 2 ) << sec->get_link() << " " + << DUMP_HEX_FORMAT( 3 ) << sec->get_info() << " " + << DUMP_HEX_FORMAT( 2 ) << sec->get_addr_align() << " " + << DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " << std::endl; } - else { // Output for 64-bit - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) << " " - << DUMP_HEX_FORMAT( 16 ) << sec->get_address() << " " - << DUMP_HEX_FORMAT( 16 ) << sec->get_size() << " " - << DUMP_HEX_FORMAT( 4 ) << sec->get_entry_size() << " " - << DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) << " " - << std::endl - << " " - << DUMP_HEX_FORMAT( 4 ) << sec->get_link() << " " - << DUMP_HEX_FORMAT( 4 ) << sec->get_info() << " " - << DUMP_HEX_FORMAT( 4 ) << sec->get_addr_align() << " " - << DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " - << std::endl; + else { // Output for 64-bit + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) + << " " << DUMP_HEX_FORMAT( 16 ) << sec->get_address() << " " + << DUMP_HEX_FORMAT( 16 ) << sec->get_size() << " " + << DUMP_HEX_FORMAT( 4 ) << sec->get_entry_size() << " " + << DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) + << " " << std::endl + << " " << DUMP_HEX_FORMAT( 4 ) << sec->get_link() << " " + << DUMP_HEX_FORMAT( 4 ) << sec->get_info() << " " + << DUMP_HEX_FORMAT( 4 ) << sec->get_addr_align() << " " + << DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " << std::endl; } - out.flags(original_flags); + out.flags( original_flags ); - return; + return; } -//------------------------------------------------------------------------------ - static void - segment_headers( std::ostream& out, const elfio& reader ) + //------------------------------------------------------------------------------ + static void segment_headers( std::ostream& out, const elfio& reader ) { Elf_Half n = reader.segments.size(); if ( n == 0 ) { @@ -530,83 +501,87 @@ class dump out << "Segment headers:" << std::endl; if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit - out << "[ Nr ] Type VirtAddr PhysAddr FileSize Mem.Size Flags Align" + out << "[ Nr ] Type VirtAddr PhysAddr FileSize Mem.Size " + "Flags Align" << std::endl; } - else { // Output for 64-bit - out << "[ Nr ] Type VirtAddr PhysAddr Flags" << std::endl - << " FileSize Mem.Size Align" + else { // Output for 64-bit + out << "[ Nr ] Type VirtAddr PhysAddr " + "Flags" + << std::endl + << " FileSize Mem.Size " + "Align" << std::endl; } - + for ( Elf_Half i = 0; i < n; ++i ) { segment* seg = reader.segments[i]; segment_header( out, i, seg, reader.get_class() ); } - + out << std::endl; } -//------------------------------------------------------------------------------ - static void - segment_header( std::ostream& out, Elf_Half no, const segment* seg, - unsigned int elf_class ) + //------------------------------------------------------------------------------ + static void segment_header( std::ostream& out, + Elf_Half no, + const segment* seg, + unsigned int elf_class ) { std::ios_base::fmtflags original_flags = out.flags(); if ( elf_class == ELFCLASS32 ) { // Output for 32-bit - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) << " " - << DUMP_HEX_FORMAT( 8 ) << seg->get_virtual_address() << " " - << DUMP_HEX_FORMAT( 8 ) << seg->get_physical_address() << " " - << DUMP_HEX_FORMAT( 8 ) << seg->get_file_size() << " " - << DUMP_HEX_FORMAT( 8 ) << seg->get_memory_size() << " " - << DUMP_STR_FORMAT( 8 ) << str_segment_flag( seg->get_flags() ) << " " - << DUMP_HEX_FORMAT( 8 ) << seg->get_align() << " " + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) + << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_virtual_address() + << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_physical_address() + << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_file_size() << " " + << DUMP_HEX_FORMAT( 8 ) << seg->get_memory_size() << " " + << DUMP_STR_FORMAT( 8 ) << str_segment_flag( seg->get_flags() ) + << " " << DUMP_HEX_FORMAT( 8 ) << seg->get_align() << " " << std::endl; } - else { // Output for 64-bit - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) << " " - << DUMP_HEX_FORMAT( 16 ) << seg->get_virtual_address() << " " - << DUMP_HEX_FORMAT( 16 ) << seg->get_physical_address() << " " - << DUMP_STR_FORMAT( 16 ) << str_segment_flag( seg->get_flags() ) << " " - << std::endl - << " " - << DUMP_HEX_FORMAT( 16 ) << seg->get_file_size() << " " - << DUMP_HEX_FORMAT( 16 ) << seg->get_memory_size() << " " - << DUMP_HEX_FORMAT( 16 ) << seg->get_align() << " " - << std::endl; + else { // Output for 64-bit + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) + << " " << DUMP_HEX_FORMAT( 16 ) << seg->get_virtual_address() + << " " << DUMP_HEX_FORMAT( 16 ) << seg->get_physical_address() + << " " << DUMP_STR_FORMAT( 16 ) + << str_segment_flag( seg->get_flags() ) << " " << std::endl + << " " << DUMP_HEX_FORMAT( 16 ) + << seg->get_file_size() << " " << DUMP_HEX_FORMAT( 16 ) + << seg->get_memory_size() << " " << DUMP_HEX_FORMAT( 16 ) + << seg->get_align() << " " << std::endl; } - out.flags(original_flags); + out.flags( original_flags ); } - -//------------------------------------------------------------------------------ - static void - symbol_tables( std::ostream& out, const elfio& reader ) + + //------------------------------------------------------------------------------ + static void symbol_tables( std::ostream& out, const elfio& reader ) { Elf_Half n = reader.sections.size(); - for ( Elf_Half i = 0; i < n; ++i ) { // For all sections + for ( Elf_Half i = 0; i < n; ++i ) { // For all sections section* sec = reader.sections[i]; - if ( SHT_SYMTAB == sec->get_type() || SHT_DYNSYM == sec->get_type() ) { + if ( SHT_SYMTAB == sec->get_type() || + SHT_DYNSYM == sec->get_type() ) { symbol_section_accessor symbols( reader, sec ); Elf_Xword sym_no = symbols.get_symbols_num(); if ( sym_no > 0 ) { - out << "Symbol table (" << sec->get_name() << ")" << std::endl; - if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit - out << "[ Nr ] Value Size Type Bind Sect Name" + out << "Symbol table (" << sec->get_name() << ")" + << std::endl; + if ( reader.get_class() == + ELFCLASS32 ) { // Output for 32-bit + out << "[ Nr ] Value Size Type Bind " + "Sect Name" << std::endl; } - else { // Output for 64-bit - out << "[ Nr ] Value Size Type Bind Sect" << std::endl - << " Name" - << std::endl; + else { // Output for 64-bit + out << "[ Nr ] Value Size Type " + " Bind Sect" + << std::endl + << " Name" << std::endl; } for ( Elf_Xword i = 0; i < sym_no; ++i ) { std::string name; @@ -616,8 +591,10 @@ class dump unsigned char type = 0; Elf_Half section = 0; unsigned char other = 0; - symbols.get_symbol( i, name, value, size, bind, type, section, other ); - symbol_table( out, i, name, value, size, bind, type, section, reader.get_class() ); + symbols.get_symbol( i, name, value, size, bind, type, + section, other ); + symbol_table( out, i, name, value, size, bind, type, + section, reader.get_class() ); } out << std::endl; @@ -625,119 +602,105 @@ class dump } } } - -//------------------------------------------------------------------------------ - static void - symbol_table( std::ostream& out, - Elf_Xword no, - std::string& name, - Elf64_Addr value, - Elf_Xword size, - unsigned char bind, - unsigned char type, - Elf_Half section, - unsigned int elf_class ) + + //------------------------------------------------------------------------------ + static void symbol_table( std::ostream& out, + Elf_Xword no, + std::string& name, + Elf64_Addr value, + Elf_Xword size, + unsigned char bind, + unsigned char type, + Elf_Half section, + unsigned int elf_class ) { std::ios_base::fmtflags original_flags = out.flags(); if ( elf_class == ELFCLASS32 ) { // Output for 32-bit - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_HEX_FORMAT( 8 ) << value << " " - << DUMP_HEX_FORMAT( 8 ) << size << " " - << DUMP_STR_FORMAT( 7 ) << str_symbol_type( type ) << " " - << DUMP_STR_FORMAT( 8 ) << str_symbol_bind( bind ) << " " - << DUMP_DEC_FORMAT( 5 ) << section << " " - << DUMP_STR_FORMAT( 1 ) << name << " " + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_HEX_FORMAT( 8 ) << value << " " << DUMP_HEX_FORMAT( 8 ) + << size << " " << DUMP_STR_FORMAT( 7 ) + << str_symbol_type( type ) << " " << DUMP_STR_FORMAT( 8 ) + << str_symbol_bind( bind ) << " " << DUMP_DEC_FORMAT( 5 ) + << section << " " << DUMP_STR_FORMAT( 1 ) << name << " " << std::endl; } - else { // Output for 64-bit - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_HEX_FORMAT( 16 ) << value << " " - << DUMP_HEX_FORMAT( 16 ) << size << " " - << DUMP_STR_FORMAT( 7 ) << str_symbol_type( type ) << " " - << DUMP_STR_FORMAT( 8 ) << str_symbol_bind( bind ) << " " - << DUMP_DEC_FORMAT( 5 ) << section << " " - << std::endl - << " " - << DUMP_STR_FORMAT( 1 ) << name << " " + else { // Output for 64-bit + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_HEX_FORMAT( 16 ) << value << " " + << DUMP_HEX_FORMAT( 16 ) << size << " " << DUMP_STR_FORMAT( 7 ) + << str_symbol_type( type ) << " " << DUMP_STR_FORMAT( 8 ) + << str_symbol_bind( bind ) << " " << DUMP_DEC_FORMAT( 5 ) + << section << " " << std::endl + << " " << DUMP_STR_FORMAT( 1 ) << name << " " << std::endl; } - out.flags(original_flags); + out.flags( original_flags ); } - -//------------------------------------------------------------------------------ - static void - notes( std::ostream& out, const elfio& reader ) + + //------------------------------------------------------------------------------ + static void notes( std::ostream& out, const elfio& reader ) { Elf_Half no = reader.sections.size(); - for ( Elf_Half i = 0; i < no; ++i ) { // For all sections + for ( Elf_Half i = 0; i < no; ++i ) { // For all sections section* sec = reader.sections[i]; - if ( SHT_NOTE == sec->get_type() ) { // Look at notes + if ( SHT_NOTE == sec->get_type() ) { // Look at notes note_section_accessor notes( reader, sec ); - Elf_Word no_notes = notes.get_notes_num(); + Elf_Word no_notes = notes.get_notes_num(); if ( no > 0 ) { - out << "Note section (" << sec->get_name() << ")" << std::endl - << " No Type Name" - << std::endl; - for ( Elf_Word j = 0; j < no_notes; ++j ) { // For all notes + out << "Note section (" << sec->get_name() << ")" + << std::endl + << " No Type Name" << std::endl; + for ( Elf_Word j = 0; j < no_notes; ++j ) { // For all notes Elf_Word type; std::string name; void* desc; Elf_Word descsz; - + if ( notes.get_note( j, type, name, desc, descsz ) ) { // 'name' usually contains \0 at the end. Try to fix it name = name.c_str(); note( out, j, type, name ); } } - + out << std::endl; } } } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ static void - note( std::ostream& out, - int no, - Elf_Word type, - const std::string& name ) + note( std::ostream& out, int no, Elf_Word type, const std::string& name ) { - out << " [" - << DUMP_DEC_FORMAT( 2 ) << no - << "] " - << DUMP_HEX_FORMAT( 8 ) << type << " " - << DUMP_STR_FORMAT( 1 ) << name - << std::endl; + out << " [" << DUMP_DEC_FORMAT( 2 ) << no << "] " + << DUMP_HEX_FORMAT( 8 ) << type << " " << DUMP_STR_FORMAT( 1 ) + << name << std::endl; } - -//------------------------------------------------------------------------------ - static void - dynamic_tags( std::ostream& out, const elfio& reader ) + + //------------------------------------------------------------------------------ + static void dynamic_tags( std::ostream& out, const elfio& reader ) { Elf_Half n = reader.sections.size(); - for ( Elf_Half i = 0; i < n; ++i ) { // For all sections + for ( Elf_Half i = 0; i < n; ++i ) { // For all sections section* sec = reader.sections[i]; if ( SHT_DYNAMIC == sec->get_type() ) { dynamic_section_accessor dynamic( reader, sec ); Elf_Xword dyn_no = dynamic.get_entries_num(); if ( dyn_no > 0 ) { - out << "Dynamic section (" << sec->get_name() << ")" << std::endl; + out << "Dynamic section (" << sec->get_name() << ")" + << std::endl; out << "[ Nr ] Tag Name/Value" << std::endl; for ( Elf_Xword i = 0; i < dyn_no; ++i ) { Elf_Xword tag = 0; Elf_Xword value = 0; std::string str; dynamic.get_entry( i, tag, value, str ); - dynamic_tag( out, i, tag, value, str, reader.get_class() ); + dynamic_tag( out, i, tag, value, str, + reader.get_class() ); if ( DT_NULL == tag ) { break; } @@ -748,42 +711,39 @@ class dump } } } - -//------------------------------------------------------------------------------ - static void - dynamic_tag( std::ostream& out, - Elf_Xword no, - Elf_Xword tag, - Elf_Xword value, - std::string str, - unsigned int /*elf_class*/ ) + + //------------------------------------------------------------------------------ + static void dynamic_tag( std::ostream& out, + Elf_Xword no, + Elf_Xword tag, + Elf_Xword value, + std::string str, + unsigned int /*elf_class*/ ) { - out << "[" - << DUMP_DEC_FORMAT( 5 ) << no - << "] " - << DUMP_STR_FORMAT( 16 ) << str_dynamic_tag( tag ) << " "; - if ( str.empty() ) { - out << DUMP_HEX_FORMAT( 16 ) << value << " "; - } - else { - out << DUMP_STR_FORMAT( 32 ) << str << " "; - } - out << std::endl; + out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] " + << DUMP_STR_FORMAT( 16 ) << str_dynamic_tag( tag ) << " "; + if ( str.empty() ) { + out << DUMP_HEX_FORMAT( 16 ) << value << " "; + } + else { + out << DUMP_STR_FORMAT( 32 ) << str << " "; + } + out << std::endl; } -//------------------------------------------------------------------------------ - static void - section_data( std::ostream& out, const section* sec ) + //------------------------------------------------------------------------------ + static void section_data( std::ostream& out, const section* sec ) { std::ios_base::fmtflags original_flags = out.flags(); out << sec->get_name() << std::endl; const char* pdata = sec->get_data(); - if ( pdata ){ + if ( pdata ) { ELFIO::Elf_Xword i; - for ( i = 0; i < std::min( sec->get_size(), MAX_DATA_ENTRIES ); ++i ) { + for ( i = 0; i < std::min( sec->get_size(), MAX_DATA_ENTRIES ); + ++i ) { if ( i % 16 == 0 ) { - out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; + out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; } out << " " << DUMP_HEX_FORMAT( 2 ) << ( pdata[i] & 0x000000FF ); @@ -796,15 +756,14 @@ class dump out << std::endl; } - out.flags(original_flags); + out.flags( original_flags ); } - return; + return; } -//------------------------------------------------------------------------------ - static void - section_datas( std::ostream& out, const elfio& reader ) + //------------------------------------------------------------------------------ + static void section_datas( std::ostream& out, const elfio& reader ) { Elf_Half n = reader.sections.size(); @@ -825,7 +784,7 @@ class dump out << std::endl; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ static void segment_data( std::ostream& out, Elf_Half no, const segment* seg ) { @@ -835,9 +794,10 @@ class dump const char* pdata = seg->get_data(); if ( pdata ) { ELFIO::Elf_Xword i; - for ( i = 0; i < std::min( seg->get_file_size(), MAX_DATA_ENTRIES ); ++i ) { + for ( i = 0; i < std::min( seg->get_file_size(), MAX_DATA_ENTRIES ); + ++i ) { if ( i % 16 == 0 ) { - out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; + out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; } out << " " << DUMP_HEX_FORMAT( 2 ) << ( pdata[i] & 0x000000FF ); @@ -850,15 +810,14 @@ class dump out << std::endl; } - out.flags(original_flags); + out.flags( original_flags ); } - return; + return; } -//------------------------------------------------------------------------------ - static void - segment_datas( std::ostream& out, const elfio& reader ) + //------------------------------------------------------------------------------ + static void segment_datas( std::ostream& out, const elfio& reader ) { Elf_Half n = reader.segments.size(); @@ -875,16 +834,15 @@ class dump out << std::endl; } - + private: -//------------------------------------------------------------------------------ - template< typename T, typename K > - std::string - static - find_value_in_table( const T& table, const K& key ) + //------------------------------------------------------------------------------ + template <typename T, typename K> + std::string static find_value_in_table( const T& table, const K& key ) { std::string res = "?"; - for ( unsigned int i = 0; i < sizeof( table )/sizeof( table[0] ); ++i ) { + for ( unsigned int i = 0; i < sizeof( table ) / sizeof( table[0] ); + ++i ) { if ( table[i].key == key ) { res = table[i].str; break; @@ -894,12 +852,9 @@ class dump return res; } - -//------------------------------------------------------------------------------ - template< typename T, typename K > - static - std::string - format_assoc( const T& table, const K& key ) + //------------------------------------------------------------------------------ + template <typename T, typename K> + static std::string format_assoc( const T& table, const K& key ) { std::string str = find_value_in_table( table, key ); if ( str == "?" ) { @@ -911,21 +866,15 @@ class dump return str; } - -//------------------------------------------------------------------------------ - template< typename T > - static - std::string - format_assoc( const T& table, const char key ) + //------------------------------------------------------------------------------ + template <typename T> + static std::string format_assoc( const T& table, const char key ) { return format_assoc( table, (const int)key ); } - -//------------------------------------------------------------------------------ - static - std::string - section_flags( Elf_Xword flags ) + //------------------------------------------------------------------------------ + static std::string section_flags( Elf_Xword flags ) { std::string ret = ""; if ( flags & SHF_WRITE ) { @@ -941,15 +890,11 @@ class dump return ret; } - //------------------------------------------------------------------------------ -#define STR_FUNC_TABLE( name ) \ - template< typename T > \ - static \ - std::string \ - str_##name( const T key ) \ - { \ - return format_assoc( name##_table, key ); \ +#define STR_FUNC_TABLE( name ) \ + template <typename T> static std::string str_##name( const T key ) \ + { \ + return format_assoc( name##_table, key ); \ } STR_FUNC_TABLE( class ) @@ -969,7 +914,6 @@ class dump #undef DUMP_HEX_FORMAT #undef DUMP_STR_FORMAT }; // class dump - }; // namespace ELFIO diff --git a/elfio/elfio_dynamic.hpp b/elfio/elfio_dynamic.hpp index a7b8860eb76e2cb5ff04e9924999ce839bb2dfc8..a0c483cad0cfa5891e992e5a4dcdcb34cee40dff 100644 --- a/elfio/elfio_dynamic.hpp +++ b/elfio/elfio_dynamic.hpp @@ -26,55 +26,50 @@ THE SOFTWARE. namespace ELFIO { //------------------------------------------------------------------------------ -template< class S > -class dynamic_section_accessor_template +template <class S> class dynamic_section_accessor_template { public: -//------------------------------------------------------------------------------ - dynamic_section_accessor_template( const elfio& elf_file_, S* section_ ) : - elf_file( elf_file_ ), - dynamic_section( section_ ) + //------------------------------------------------------------------------------ + dynamic_section_accessor_template( const elfio& elf_file_, S* section_ ) + : elf_file( elf_file_ ), dynamic_section( section_ ) { } -//------------------------------------------------------------------------------ - Elf_Xword - get_entries_num() const + //------------------------------------------------------------------------------ + Elf_Xword get_entries_num() const { Elf_Xword nRet = 0; if ( 0 != dynamic_section->get_entry_size() ) { - nRet = dynamic_section->get_size() / dynamic_section->get_entry_size(); + nRet = + dynamic_section->get_size() / dynamic_section->get_entry_size(); } return nRet; } -//------------------------------------------------------------------------------ - bool - get_entry( Elf_Xword index, - Elf_Xword& tag, - Elf_Xword& value, - std::string& str ) const + //------------------------------------------------------------------------------ + bool get_entry( Elf_Xword index, + Elf_Xword& tag, + Elf_Xword& value, + std::string& str ) const { - if ( index >= get_entries_num() ) { // Is index valid + if ( index >= get_entries_num() ) { // Is index valid return false; } if ( elf_file.get_class() == ELFCLASS32 ) { - generic_get_entry_dyn< Elf32_Dyn >( index, tag, value ); + generic_get_entry_dyn<Elf32_Dyn>( index, tag, value ); } else { - generic_get_entry_dyn< Elf64_Dyn >( index, tag, value ); + generic_get_entry_dyn<Elf64_Dyn>( index, tag, value ); } // If the tag may have a string table reference, prepare the string - if ( tag == DT_NEEDED || - tag == DT_SONAME || - tag == DT_RPATH || + if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || tag == DT_RUNPATH ) { string_section_accessor strsec = - elf_file.sections[ get_string_table_index() ]; + elf_file.sections[get_string_table_index()]; const char* result = strsec.get_string( value ); if ( 0 == result ) { str.clear(); @@ -89,59 +84,54 @@ class dynamic_section_accessor_template return true; } -//------------------------------------------------------------------------------ - void - add_entry( Elf_Xword tag, - Elf_Xword value ) + //------------------------------------------------------------------------------ + void add_entry( Elf_Xword tag, Elf_Xword value ) { if ( elf_file.get_class() == ELFCLASS32 ) { - generic_add_entry< Elf32_Dyn >( tag, value ); + generic_add_entry<Elf32_Dyn>( tag, value ); } else { - generic_add_entry< Elf64_Dyn >( tag, value ); + generic_add_entry<Elf64_Dyn>( tag, value ); } } -//------------------------------------------------------------------------------ - void - add_entry( Elf_Xword tag, - const std::string& str ) + //------------------------------------------------------------------------------ + void add_entry( Elf_Xword tag, const std::string& str ) { string_section_accessor strsec = - elf_file.sections[ get_string_table_index() ]; + elf_file.sections[get_string_table_index()]; Elf_Xword value = strsec.add_string( str ); add_entry( tag, value ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: -//------------------------------------------------------------------------------ - Elf_Half - get_string_table_index() const + //------------------------------------------------------------------------------ + Elf_Half get_string_table_index() const { return (Elf_Half)dynamic_section->get_link(); } -//------------------------------------------------------------------------------ - template< class T > - void - generic_get_entry_dyn( Elf_Xword index, - Elf_Xword& tag, - Elf_Xword& value ) const + //------------------------------------------------------------------------------ + template <class T> + void generic_get_entry_dyn( Elf_Xword index, + Elf_Xword& tag, + Elf_Xword& value ) const { const endianess_convertor& convertor = elf_file.get_convertor(); // Check unusual case when dynamic section has no data - if( dynamic_section->get_data() == 0 || - ( index + 1 ) * dynamic_section->get_entry_size() > dynamic_section->get_size() ) { + if ( dynamic_section->get_data() == 0 || + ( index + 1 ) * dynamic_section->get_entry_size() > + dynamic_section->get_size() ) { tag = DT_NULL; value = 0; return; } const T* pEntry = reinterpret_cast<const T*>( - dynamic_section->get_data() + - index * dynamic_section->get_entry_size() ); + dynamic_section->get_data() + + index * dynamic_section->get_entry_size() ); tag = convertor( pEntry->d_tag ); switch ( tag ) { case DT_NULL: @@ -187,10 +177,8 @@ class dynamic_section_accessor_template } } -//------------------------------------------------------------------------------ - template< class T > - void - generic_add_entry( Elf_Xword tag, Elf_Xword value ) + //------------------------------------------------------------------------------ + template <class T> void generic_add_entry( Elf_Xword tag, Elf_Xword value ) { const endianess_convertor& convertor = elf_file.get_convertor(); @@ -240,17 +228,19 @@ class dynamic_section_accessor_template entry.d_tag = convertor( tag ); - dynamic_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) ); + dynamic_section->append_data( reinterpret_cast<char*>( &entry ), + sizeof( entry ) ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: const elfio& elf_file; S* dynamic_section; }; -using dynamic_section_accessor = dynamic_section_accessor_template<section>; -using const_dynamic_section_accessor = dynamic_section_accessor_template<const section>; +using dynamic_section_accessor = dynamic_section_accessor_template<section>; +using const_dynamic_section_accessor = + dynamic_section_accessor_template<const section>; } // namespace ELFIO diff --git a/elfio/elfio_header.hpp b/elfio/elfio_header.hpp index fc809c848b16a6088e80fafce6d325e3a237113d..5df41ffce9ed7deede271406de390a80f462a37d 100644 --- a/elfio/elfio_header.hpp +++ b/elfio/elfio_header.hpp @@ -30,55 +30,56 @@ namespace ELFIO { class elf_header { public: - virtual ~elf_header() {}; + virtual ~elf_header(){}; virtual bool load( std::istream& stream ) = 0; virtual bool save( std::ostream& stream ) const = 0; // ELF header functions - ELFIO_GET_ACCESS_DECL( unsigned char, class ); - ELFIO_GET_ACCESS_DECL( unsigned char, elf_version ); - ELFIO_GET_ACCESS_DECL( unsigned char, encoding ); - ELFIO_GET_ACCESS_DECL( Elf_Half, header_size ); - ELFIO_GET_ACCESS_DECL( Elf_Half, section_entry_size ); - ELFIO_GET_ACCESS_DECL( Elf_Half, segment_entry_size ); - - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, version ); - ELFIO_GET_SET_ACCESS_DECL( unsigned char, os_abi ); - ELFIO_GET_SET_ACCESS_DECL( unsigned char, abi_version ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Half, type ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Half, machine ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags ); - ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, entry ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Half, sections_num ); - ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, sections_offset ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Half, segments_num ); - ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, segments_offset ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Half, section_name_str_index ); + ELFIO_GET_ACCESS_DECL( unsigned char, class ); + ELFIO_GET_ACCESS_DECL( unsigned char, elf_version ); + ELFIO_GET_ACCESS_DECL( unsigned char, encoding ); + ELFIO_GET_ACCESS_DECL( Elf_Half, header_size ); + ELFIO_GET_ACCESS_DECL( Elf_Half, section_entry_size ); + ELFIO_GET_ACCESS_DECL( Elf_Half, segment_entry_size ); + + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, version ); + ELFIO_GET_SET_ACCESS_DECL( unsigned char, os_abi ); + ELFIO_GET_SET_ACCESS_DECL( unsigned char, abi_version ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Half, type ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Half, machine ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags ); + ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, entry ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Half, sections_num ); + ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, sections_offset ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Half, segments_num ); + ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, segments_offset ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Half, section_name_str_index ); }; - -template< class T > struct elf_header_impl_types; -template<> struct elf_header_impl_types<Elf32_Ehdr> { - typedef Elf32_Phdr Phdr_type; - typedef Elf32_Shdr Shdr_type; +template <class T> struct elf_header_impl_types; +template <> struct elf_header_impl_types<Elf32_Ehdr> +{ + typedef Elf32_Phdr Phdr_type; + typedef Elf32_Shdr Shdr_type; static const unsigned char file_class = ELFCLASS32; }; -template<> struct elf_header_impl_types<Elf64_Ehdr> { - typedef Elf64_Phdr Phdr_type; - typedef Elf64_Shdr Shdr_type; +template <> struct elf_header_impl_types<Elf64_Ehdr> +{ + typedef Elf64_Phdr Phdr_type; + typedef Elf64_Shdr Shdr_type; static const unsigned char file_class = ELFCLASS64; }; -template< class T > class elf_header_impl : public elf_header +template <class T> class elf_header_impl : public elf_header { public: - //------------------------------------------------------------------------------ - elf_header_impl( endianess_convertor* convertor_, - unsigned char encoding ) + //------------------------------------------------------------------------------ + elf_header_impl( endianess_convertor* convertor_, unsigned char encoding ) { convertor = convertor_; - std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' ); + std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), + '\0' ); header.e_ident[EI_MAG0] = ELFMAG0; header.e_ident[EI_MAG1] = ELFMAG1; @@ -87,60 +88,63 @@ template< class T > class elf_header_impl : public elf_header header.e_ident[EI_CLASS] = elf_header_impl_types<T>::file_class; header.e_ident[EI_DATA] = encoding; header.e_ident[EI_VERSION] = EV_CURRENT; - header.e_version = (*convertor)( (Elf_Word)EV_CURRENT ); + header.e_version = ( *convertor )( (Elf_Word)EV_CURRENT ); header.e_ehsize = ( sizeof( header ) ); - header.e_ehsize = (*convertor)( header.e_ehsize ); - header.e_shstrndx = (*convertor)( (Elf_Half)1 ); - header.e_phentsize = sizeof( typename elf_header_impl_types<T>::Phdr_type ); - header.e_shentsize = sizeof( typename elf_header_impl_types<T>::Shdr_type ); - header.e_phentsize = (*convertor)( header.e_phentsize ); - header.e_shentsize = (*convertor)( header.e_shentsize ); + header.e_ehsize = ( *convertor )( header.e_ehsize ); + header.e_shstrndx = ( *convertor )( (Elf_Half)1 ); + header.e_phentsize = + sizeof( typename elf_header_impl_types<T>::Phdr_type ); + header.e_shentsize = + sizeof( typename elf_header_impl_types<T>::Shdr_type ); + header.e_phentsize = ( *convertor )( header.e_phentsize ); + header.e_shentsize = ( *convertor )( header.e_shentsize ); } //------------------------------------------------------------------------------ - bool - load( std::istream& stream ) + bool load( std::istream& stream ) { stream.seekg( 0 ); stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) ); - return (stream.gcount() == sizeof( header ) ); + return ( stream.gcount() == sizeof( header ) ); } //------------------------------------------------------------------------------ - bool - save( std::ostream& stream ) const + bool save( std::ostream& stream ) const { stream.seekp( 0 ); - stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) ); + stream.write( reinterpret_cast<const char*>( &header ), + sizeof( header ) ); return stream.good(); } //------------------------------------------------------------------------------ // ELF header functions - ELFIO_GET_ACCESS( unsigned char, class, header.e_ident[EI_CLASS] ); - ELFIO_GET_ACCESS( unsigned char, elf_version, header.e_ident[EI_VERSION] ); - ELFIO_GET_ACCESS( unsigned char, encoding, header.e_ident[EI_DATA] ); - ELFIO_GET_ACCESS( Elf_Half, header_size, header.e_ehsize ); - ELFIO_GET_ACCESS( Elf_Half, section_entry_size, header.e_shentsize ); - ELFIO_GET_ACCESS( Elf_Half, segment_entry_size, header.e_phentsize ); - - ELFIO_GET_SET_ACCESS( Elf_Word, version, header.e_version); - ELFIO_GET_SET_ACCESS( unsigned char, os_abi, header.e_ident[EI_OSABI] ); - ELFIO_GET_SET_ACCESS( unsigned char, abi_version, header.e_ident[EI_ABIVERSION] ); - ELFIO_GET_SET_ACCESS( Elf_Half, type, header.e_type ); - ELFIO_GET_SET_ACCESS( Elf_Half, machine, header.e_machine ); - ELFIO_GET_SET_ACCESS( Elf_Word, flags, header.e_flags ); - ELFIO_GET_SET_ACCESS( Elf_Half, section_name_str_index, header.e_shstrndx ); - ELFIO_GET_SET_ACCESS( Elf64_Addr, entry, header.e_entry ); - ELFIO_GET_SET_ACCESS( Elf_Half, sections_num, header.e_shnum ); - ELFIO_GET_SET_ACCESS( Elf64_Off, sections_offset, header.e_shoff ); - ELFIO_GET_SET_ACCESS( Elf_Half, segments_num, header.e_phnum ); - ELFIO_GET_SET_ACCESS( Elf64_Off, segments_offset, header.e_phoff ); + ELFIO_GET_ACCESS( unsigned char, class, header.e_ident[EI_CLASS] ); + ELFIO_GET_ACCESS( unsigned char, elf_version, header.e_ident[EI_VERSION] ); + ELFIO_GET_ACCESS( unsigned char, encoding, header.e_ident[EI_DATA] ); + ELFIO_GET_ACCESS( Elf_Half, header_size, header.e_ehsize ); + ELFIO_GET_ACCESS( Elf_Half, section_entry_size, header.e_shentsize ); + ELFIO_GET_ACCESS( Elf_Half, segment_entry_size, header.e_phentsize ); + + ELFIO_GET_SET_ACCESS( Elf_Word, version, header.e_version ); + ELFIO_GET_SET_ACCESS( unsigned char, os_abi, header.e_ident[EI_OSABI] ); + ELFIO_GET_SET_ACCESS( unsigned char, + abi_version, + header.e_ident[EI_ABIVERSION] ); + ELFIO_GET_SET_ACCESS( Elf_Half, type, header.e_type ); + ELFIO_GET_SET_ACCESS( Elf_Half, machine, header.e_machine ); + ELFIO_GET_SET_ACCESS( Elf_Word, flags, header.e_flags ); + ELFIO_GET_SET_ACCESS( Elf_Half, section_name_str_index, header.e_shstrndx ); + ELFIO_GET_SET_ACCESS( Elf64_Addr, entry, header.e_entry ); + ELFIO_GET_SET_ACCESS( Elf_Half, sections_num, header.e_shnum ); + ELFIO_GET_SET_ACCESS( Elf64_Off, sections_offset, header.e_shoff ); + ELFIO_GET_SET_ACCESS( Elf_Half, segments_num, header.e_phnum ); + ELFIO_GET_SET_ACCESS( Elf64_Off, segments_offset, header.e_phoff ); private: - T header; + T header; endianess_convertor* convertor; }; diff --git a/elfio/elfio_note.hpp b/elfio/elfio_note.hpp index 409679384dd8c8f71f17eefc51eec13990a809dd..fa25a7e112f00b6ca396c05dce39c93eedeb6ed7 100644 --- a/elfio/elfio_note.hpp +++ b/elfio/elfio_note.hpp @@ -38,63 +38,62 @@ namespace ELFIO { //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -template< class S > -class note_section_accessor_template +template <class S> class note_section_accessor_template { public: -//------------------------------------------------------------------------------ - note_section_accessor_template( const elfio& elf_file_, S* section_ ) : - elf_file( elf_file_ ), note_section( section_ ) + //------------------------------------------------------------------------------ + note_section_accessor_template( const elfio& elf_file_, S* section_ ) + : elf_file( elf_file_ ), note_section( section_ ) { process_section(); } -//------------------------------------------------------------------------------ - Elf_Word - get_notes_num() const + //------------------------------------------------------------------------------ + Elf_Word get_notes_num() const { return (Elf_Word)note_start_positions.size(); } -//------------------------------------------------------------------------------ - bool - get_note( Elf_Word index, - Elf_Word& type, - std::string& name, - void*& desc, - Elf_Word& descSize ) const + //------------------------------------------------------------------------------ + bool get_note( Elf_Word index, + Elf_Word& type, + std::string& name, + void*& desc, + Elf_Word& descSize ) const { if ( index >= note_section->get_size() ) { return false; } - const char* pData = note_section->get_data() + note_start_positions[index]; + const char* pData = + note_section->get_data() + note_start_positions[index]; int align = sizeof( Elf_Word ); const endianess_convertor& convertor = elf_file.get_convertor(); - type = convertor( *(const Elf_Word*)( pData + 2*align ) ); + type = convertor( *(const Elf_Word*)( pData + 2 * align ) ); Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) ); - descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) ); + descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) ); - Elf_Xword max_name_size = note_section->get_size() - note_start_positions[index]; - if ( namesz < 1 || - namesz > max_name_size || - (Elf_Xword)namesz + descSize > max_name_size ) { + Elf_Xword max_name_size = + note_section->get_size() - note_start_positions[index]; + if ( namesz < 1 || namesz > max_name_size || + (Elf_Xword)namesz + descSize > max_name_size ) { return false; } - name.assign( pData + 3*align, namesz - 1); + name.assign( pData + 3 * align, namesz - 1 ); if ( 0 == descSize ) { desc = 0; } else { - desc = const_cast<char*> ( pData + 3*align + - ( ( namesz + align - 1 )/align )*align ); + desc = + const_cast<char*>( pData + 3 * align + + ( ( namesz + align - 1 ) / align ) * align ); } return true; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ void add_note( Elf_Word type, const std::string& name, const void* desc, @@ -102,11 +101,12 @@ class note_section_accessor_template { const endianess_convertor& convertor = elf_file.get_convertor(); - int align = sizeof( Elf_Word ); - Elf_Word nameLen = (Elf_Word)name.size() + 1; - Elf_Word nameLenConv = convertor( nameLen ); + int align = sizeof( Elf_Word ); + Elf_Word nameLen = (Elf_Word)name.size() + 1; + Elf_Word nameLenConv = convertor( nameLen ); std::string buffer( reinterpret_cast<char*>( &nameLenConv ), align ); - Elf_Word descSizeConv = convertor( descSize ); + Elf_Word descSizeConv = convertor( descSize ); + buffer.append( reinterpret_cast<char*>( &descSizeConv ), align ); type = convertor( type ); buffer.append( reinterpret_cast<char*>( &type ), align ); @@ -128,13 +128,13 @@ class note_section_accessor_template } private: -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ void process_section() { const endianess_convertor& convertor = elf_file.get_convertor(); - const char* data = note_section->get_data(); - Elf_Xword size = note_section->get_size(); - Elf_Xword current = 0; + const char* data = note_section->get_data(); + Elf_Xword size = note_section->get_size(); + Elf_Xword current = 0; note_start_positions.clear(); @@ -144,28 +144,28 @@ class note_section_accessor_template } Elf_Word align = sizeof( Elf_Word ); - while ( current + (Elf_Xword)3*align <= size ) { + while ( current + (Elf_Xword)3 * align <= size ) { note_start_positions.push_back( current ); - Elf_Word namesz = convertor( - *(const Elf_Word*)( data + current ) ); + Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) ); Elf_Word descsz = convertor( - *(const Elf_Word*)( data + current + sizeof( namesz ) ) ); + *(const Elf_Word*)( data + current + sizeof( namesz ) ) ); - current += (Elf_Xword)3*sizeof( Elf_Word ) + + current += (Elf_Xword)3 * sizeof( Elf_Word ) + ( ( namesz + align - 1 ) / align ) * (Elf_Xword)align + ( ( descsz + align - 1 ) / align ) * (Elf_Xword)align; } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: const elfio& elf_file; S* note_section; std::vector<Elf_Xword> note_start_positions; }; -using note_section_accessor = note_section_accessor_template<section>; -using const_note_section_accessor = note_section_accessor_template<const section>; +using note_section_accessor = note_section_accessor_template<section>; +using const_note_section_accessor = + note_section_accessor_template<const section>; } // namespace ELFIO diff --git a/elfio/elfio_relocation.hpp b/elfio/elfio_relocation.hpp index a31fa239cb01ef79501b33252f0a8b3a6970d35c..574e5b67b54801b44a38fc6356d79e6dc2357df6 100644 --- a/elfio/elfio_relocation.hpp +++ b/elfio/elfio_relocation.hpp @@ -25,8 +25,8 @@ THE SOFTWARE. namespace ELFIO { -template<typename T> struct get_sym_and_type; -template<> struct get_sym_and_type< Elf32_Rel > +template <typename T> struct get_sym_and_type; +template <> struct get_sym_and_type<Elf32_Rel> { static int get_r_sym( Elf_Xword info ) { @@ -37,7 +37,7 @@ template<> struct get_sym_and_type< Elf32_Rel > return ELF32_R_TYPE( (Elf_Word)info ); } }; -template<> struct get_sym_and_type< Elf32_Rela > +template <> struct get_sym_and_type<Elf32_Rela> { static int get_r_sym( Elf_Xword info ) { @@ -48,104 +48,87 @@ template<> struct get_sym_and_type< Elf32_Rela > return ELF32_R_TYPE( (Elf_Word)info ); } }; -template<> struct get_sym_and_type< Elf64_Rel > +template <> struct get_sym_and_type<Elf64_Rel> { - static int get_r_sym( Elf_Xword info ) - { - return ELF64_R_SYM( info ); - } - static int get_r_type( Elf_Xword info ) - { - return ELF64_R_TYPE( info ); - } + static int get_r_sym( Elf_Xword info ) { return ELF64_R_SYM( info ); } + static int get_r_type( Elf_Xword info ) { return ELF64_R_TYPE( info ); } }; -template<> struct get_sym_and_type< Elf64_Rela > +template <> struct get_sym_and_type<Elf64_Rela> { - static int get_r_sym( Elf_Xword info ) - { - return ELF64_R_SYM( info ); - } - static int get_r_type( Elf_Xword info ) - { - return ELF64_R_TYPE( info ); - } + static int get_r_sym( Elf_Xword info ) { return ELF64_R_SYM( info ); } + static int get_r_type( Elf_Xword info ) { return ELF64_R_TYPE( info ); } }; - //------------------------------------------------------------------------------ -template< class S > -class relocation_section_accessor_template +template <class S> class relocation_section_accessor_template { public: -//------------------------------------------------------------------------------ - relocation_section_accessor_template( const elfio& elf_file_, S* section_ ) : - elf_file( elf_file_ ), - relocation_section( section_ ) + //------------------------------------------------------------------------------ + relocation_section_accessor_template( const elfio& elf_file_, S* section_ ) + : elf_file( elf_file_ ), relocation_section( section_ ) { } -//------------------------------------------------------------------------------ - Elf_Xword - get_entries_num() const + //------------------------------------------------------------------------------ + Elf_Xword get_entries_num() const { Elf_Xword nRet = 0; if ( 0 != relocation_section->get_entry_size() ) { - nRet = relocation_section->get_size() / relocation_section->get_entry_size(); + nRet = relocation_section->get_size() / + relocation_section->get_entry_size(); } return nRet; } -//------------------------------------------------------------------------------ - bool - get_entry( Elf_Xword index, - Elf64_Addr& offset, - Elf_Word& symbol, - Elf_Word& type, - Elf_Sxword& addend ) const + //------------------------------------------------------------------------------ + bool get_entry( Elf_Xword index, + Elf64_Addr& offset, + Elf_Word& symbol, + Elf_Word& type, + Elf_Sxword& addend ) const { - if ( index >= get_entries_num() ) { // Is index valid + if ( index >= get_entries_num() ) { // Is index valid return false; } if ( elf_file.get_class() == ELFCLASS32 ) { if ( SHT_REL == relocation_section->get_type() ) { - generic_get_entry_rel< Elf32_Rel >( index, offset, symbol, - type, addend ); + generic_get_entry_rel<Elf32_Rel>( index, offset, symbol, type, + addend ); } else if ( SHT_RELA == relocation_section->get_type() ) { - generic_get_entry_rela< Elf32_Rela >( index, offset, symbol, - type, addend ); + generic_get_entry_rela<Elf32_Rela>( index, offset, symbol, type, + addend ); } } else { if ( SHT_REL == relocation_section->get_type() ) { - generic_get_entry_rel< Elf64_Rel >( index, offset, symbol, - type, addend ); + generic_get_entry_rel<Elf64_Rel>( index, offset, symbol, type, + addend ); } else if ( SHT_RELA == relocation_section->get_type() ) { - generic_get_entry_rela< Elf64_Rela >( index, offset, symbol, - type, addend ); + generic_get_entry_rela<Elf64_Rela>( index, offset, symbol, type, + addend ); } } return true; } -//------------------------------------------------------------------------------ - bool - get_entry( Elf_Xword index, - Elf64_Addr& offset, - Elf64_Addr& symbolValue, - std::string& symbolName, - Elf_Word& type, - Elf_Sxword& addend, - Elf_Sxword& calcValue ) const + //------------------------------------------------------------------------------ + bool get_entry( Elf_Xword index, + Elf64_Addr& offset, + Elf64_Addr& symbolValue, + std::string& symbolName, + Elf_Word& type, + Elf_Sxword& addend, + Elf_Sxword& calcValue ) const { // Do regular job Elf_Word symbol; - bool ret = get_entry( index, offset, symbol, type, addend ); + bool ret = get_entry( index, offset, symbol, type, addend ); // Find the symbol Elf_Xword size; @@ -154,44 +137,45 @@ class relocation_section_accessor_template Elf_Half section; unsigned char other; - symbol_section_accessor symbols( elf_file, elf_file.sections[get_symbol_table_index()] ); - ret = ret && symbols.get_symbol( symbol, symbolName, symbolValue, - size, bind, symbolType, section, other ); + symbol_section_accessor symbols( + elf_file, elf_file.sections[get_symbol_table_index()] ); + ret = ret && symbols.get_symbol( symbol, symbolName, symbolValue, size, + bind, symbolType, section, other ); if ( ret ) { // Was it successful? switch ( type ) { - case R_386_NONE: // none + case R_386_NONE: // none calcValue = 0; break; - case R_386_32: // S + A + case R_386_32: // S + A calcValue = symbolValue + addend; break; - case R_386_PC32: // S + A - P + case R_386_PC32: // S + A - P calcValue = symbolValue + addend - offset; break; - case R_386_GOT32: // G + A - P + case R_386_GOT32: // G + A - P calcValue = 0; break; - case R_386_PLT32: // L + A - P + case R_386_PLT32: // L + A - P calcValue = 0; break; - case R_386_COPY: // none + case R_386_COPY: // none calcValue = 0; break; - case R_386_GLOB_DAT: // S - case R_386_JMP_SLOT: // S + case R_386_GLOB_DAT: // S + case R_386_JMP_SLOT: // S calcValue = symbolValue; break; - case R_386_RELATIVE: // B + A + case R_386_RELATIVE: // B + A calcValue = addend; break; - case R_386_GOTOFF: // S + A - GOT + case R_386_GOTOFF: // S + A - GOT calcValue = 0; break; - case R_386_GOTPC: // GOT + A - P + case R_386_GOTPC: // GOT + A - P calcValue = 0; break; - default: // Not recognized symbol! + default: // Not recognized symbol! calcValue = 0; break; } @@ -201,80 +185,81 @@ class relocation_section_accessor_template } //------------------------------------------------------------------------------ - bool - set_entry(Elf_Xword index, - Elf64_Addr offset, - Elf_Word symbol, - Elf_Word type, - Elf_Sxword addend) + bool set_entry( Elf_Xword index, + Elf64_Addr offset, + Elf_Word symbol, + Elf_Word type, + Elf_Sxword addend ) { - if (index >= get_entries_num()) { // Is index valid + if ( index >= get_entries_num() ) { // Is index valid return false; } - if (elf_file.get_class() == ELFCLASS32) { - if (SHT_REL == relocation_section->get_type()) { - generic_set_entry_rel<Elf32_Rel>(index, offset, symbol, type, addend); + if ( elf_file.get_class() == ELFCLASS32 ) { + if ( SHT_REL == relocation_section->get_type() ) { + generic_set_entry_rel<Elf32_Rel>( index, offset, symbol, type, + addend ); } - else if (SHT_RELA == relocation_section->get_type()) { - generic_set_entry_rela<Elf32_Rela>(index, offset, symbol, type, addend); + else if ( SHT_RELA == relocation_section->get_type() ) { + generic_set_entry_rela<Elf32_Rela>( index, offset, symbol, type, + addend ); } } else { - if (SHT_REL == relocation_section->get_type()) { - generic_set_entry_rel<Elf64_Rel>(index, offset, symbol, type, addend); + if ( SHT_REL == relocation_section->get_type() ) { + generic_set_entry_rel<Elf64_Rel>( index, offset, symbol, type, + addend ); } - else if (SHT_RELA == relocation_section->get_type()) { - generic_set_entry_rela<Elf64_Rela>(index, offset, symbol, type, addend); + else if ( SHT_RELA == relocation_section->get_type() ) { + generic_set_entry_rela<Elf64_Rela>( index, offset, symbol, type, + addend ); } } return true; } -//------------------------------------------------------------------------------ - void - add_entry( Elf64_Addr offset, Elf_Xword info ) + //------------------------------------------------------------------------------ + void add_entry( Elf64_Addr offset, Elf_Xword info ) { if ( elf_file.get_class() == ELFCLASS32 ) { - generic_add_entry< Elf32_Rel >( offset, info ); + generic_add_entry<Elf32_Rel>( offset, info ); } else { - generic_add_entry< Elf64_Rel >( offset, info ); + generic_add_entry<Elf64_Rel>( offset, info ); } } -//------------------------------------------------------------------------------ - void - add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type ) + //------------------------------------------------------------------------------ + void add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type ) { Elf_Xword info; if ( elf_file.get_class() == ELFCLASS32 ) { info = ELF32_R_INFO( (Elf_Xword)symbol, type ); } else { - info = ELF64_R_INFO((Elf_Xword)symbol, type ); + info = ELF64_R_INFO( (Elf_Xword)symbol, type ); } add_entry( offset, info ); } -//------------------------------------------------------------------------------ - void - add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend ) + //------------------------------------------------------------------------------ + void add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend ) { if ( elf_file.get_class() == ELFCLASS32 ) { - generic_add_entry< Elf32_Rela >( offset, info, addend ); + generic_add_entry<Elf32_Rela>( offset, info, addend ); } else { - generic_add_entry< Elf64_Rela >( offset, info, addend ); + generic_add_entry<Elf64_Rela>( offset, info, addend ); } } -//------------------------------------------------------------------------------ - void - add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type, - Elf_Sxword addend ) + //------------------------------------------------------------------------------ + void add_entry( Elf64_Addr offset, + Elf_Word symbol, + unsigned char type, + Elf_Sxword addend ) { Elf_Xword info; if ( elf_file.get_class() == ELFCLASS32 ) { @@ -287,70 +272,63 @@ class relocation_section_accessor_template add_entry( offset, info, addend ); } -//------------------------------------------------------------------------------ - void - add_entry( string_section_accessor str_writer, - const char* str, - symbol_section_accessor sym_writer, - Elf64_Addr value, - Elf_Word size, - unsigned char sym_info, - unsigned char other, - Elf_Half shndx, - Elf64_Addr offset, - unsigned char type ) + //------------------------------------------------------------------------------ + void add_entry( string_section_accessor str_writer, + const char* str, + symbol_section_accessor sym_writer, + Elf64_Addr value, + Elf_Word size, + unsigned char sym_info, + unsigned char other, + Elf_Half shndx, + Elf64_Addr offset, + unsigned char type ) { Elf_Word str_index = str_writer.add_string( str ); Elf_Word sym_index = sym_writer.add_symbol( str_index, value, size, - sym_info, other, shndx ); + sym_info, other, shndx ); add_entry( offset, sym_index, type ); } //------------------------------------------------------------------------------ - void - swap_symbols(Elf_Xword first, Elf_Xword second) + void swap_symbols( Elf_Xword first, Elf_Xword second ) { Elf64_Addr offset; Elf_Word symbol; Elf_Word rtype; Elf_Sxword addend; - for (Elf_Word i = 0; i < get_entries_num(); i++) - { - get_entry(i, offset, symbol, rtype, addend); - if (symbol == first) - { - set_entry(i, offset, (Elf_Word)second, rtype, addend); + for ( Elf_Word i = 0; i < get_entries_num(); i++ ) { + get_entry( i, offset, symbol, rtype, addend ); + if ( symbol == first ) { + set_entry( i, offset, (Elf_Word)second, rtype, addend ); } - if (symbol == second) - { - set_entry(i, offset, (Elf_Word)first, rtype, addend); + if ( symbol == second ) { + set_entry( i, offset, (Elf_Word)first, rtype, addend ); } } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: -//------------------------------------------------------------------------------ - Elf_Half - get_symbol_table_index() const + //------------------------------------------------------------------------------ + Elf_Half get_symbol_table_index() const { return (Elf_Half)relocation_section->get_link(); } -//------------------------------------------------------------------------------ - template< class T > - void - generic_get_entry_rel( Elf_Xword index, - Elf64_Addr& offset, - Elf_Word& symbol, - Elf_Word& type, - Elf_Sxword& addend ) const + //------------------------------------------------------------------------------ + template <class T> + void generic_get_entry_rel( Elf_Xword index, + Elf64_Addr& offset, + Elf_Word& symbol, + Elf_Word& type, + Elf_Sxword& addend ) const { const endianess_convertor& convertor = elf_file.get_convertor(); const T* pEntry = reinterpret_cast<const T*>( - relocation_section->get_data() + - index * relocation_section->get_entry_size() ); + relocation_section->get_data() + + index * relocation_section->get_entry_size() ); offset = convertor( pEntry->r_offset ); Elf_Xword tmp = convertor( pEntry->r_info ); symbol = get_sym_and_type<T>::get_r_sym( tmp ); @@ -358,20 +336,19 @@ class relocation_section_accessor_template addend = 0; } -//------------------------------------------------------------------------------ - template< class T > - void - generic_get_entry_rela( Elf_Xword index, - Elf64_Addr& offset, - Elf_Word& symbol, - Elf_Word& type, - Elf_Sxword& addend ) const + //------------------------------------------------------------------------------ + template <class T> + void generic_get_entry_rela( Elf_Xword index, + Elf64_Addr& offset, + Elf_Word& symbol, + Elf_Word& type, + Elf_Sxword& addend ) const { const endianess_convertor& convertor = elf_file.get_convertor(); const T* pEntry = reinterpret_cast<const T*>( - relocation_section->get_data() + - index * relocation_section->get_entry_size() ); + relocation_section->get_data() + + index * relocation_section->get_entry_size() ); offset = convertor( pEntry->r_offset ); Elf_Xword tmp = convertor( pEntry->r_info ); symbol = get_sym_and_type<T>::get_r_sym( tmp ); @@ -381,60 +358,59 @@ class relocation_section_accessor_template //------------------------------------------------------------------------------ template <class T> - void - generic_set_entry_rel(Elf_Xword index, - Elf64_Addr offset, - Elf_Word symbol, - Elf_Word type, - Elf_Sxword) + void generic_set_entry_rel( Elf_Xword index, + Elf64_Addr offset, + Elf_Word symbol, + Elf_Word type, + Elf_Sxword ) { - const endianess_convertor &convertor = elf_file.get_convertor(); + const endianess_convertor& convertor = elf_file.get_convertor(); - T *pEntry = const_cast<T *>(reinterpret_cast<const T *>(relocation_section->get_data() + - index * relocation_section->get_entry_size())); + T* pEntry = const_cast<T*>( reinterpret_cast<const T*>( + relocation_section->get_data() + + index * relocation_section->get_entry_size() ) ); - if (elf_file.get_class() == ELFCLASS32) { - pEntry->r_info = ELF32_R_INFO((Elf_Xword)symbol, type); + if ( elf_file.get_class() == ELFCLASS32 ) { + pEntry->r_info = ELF32_R_INFO( (Elf_Xword)symbol, type ); } else { - pEntry->r_info = ELF64_R_INFO((Elf_Xword)symbol, type); + pEntry->r_info = ELF64_R_INFO( (Elf_Xword)symbol, type ); } pEntry->r_offset = offset; - pEntry->r_offset = convertor(pEntry->r_offset); - pEntry->r_info = convertor(pEntry->r_info); + pEntry->r_offset = convertor( pEntry->r_offset ); + pEntry->r_info = convertor( pEntry->r_info ); } //------------------------------------------------------------------------------ template <class T> - void - generic_set_entry_rela(Elf_Xword index, - Elf64_Addr offset, - Elf_Word symbol, - Elf_Word type, - Elf_Sxword addend) + void generic_set_entry_rela( Elf_Xword index, + Elf64_Addr offset, + Elf_Word symbol, + Elf_Word type, + Elf_Sxword addend ) { - const endianess_convertor &convertor = elf_file.get_convertor(); + const endianess_convertor& convertor = elf_file.get_convertor(); - T *pEntry = const_cast<T *>(reinterpret_cast<const T *>(relocation_section->get_data() + - index * relocation_section->get_entry_size())); + T* pEntry = const_cast<T*>( reinterpret_cast<const T*>( + relocation_section->get_data() + + index * relocation_section->get_entry_size() ) ); - if (elf_file.get_class() == ELFCLASS32) { - pEntry->r_info = ELF32_R_INFO((Elf_Xword)symbol, type); + if ( elf_file.get_class() == ELFCLASS32 ) { + pEntry->r_info = ELF32_R_INFO( (Elf_Xword)symbol, type ); } else { - pEntry->r_info = ELF64_R_INFO((Elf_Xword)symbol, type); + pEntry->r_info = ELF64_R_INFO( (Elf_Xword)symbol, type ); } pEntry->r_offset = offset; pEntry->r_addend = addend; - pEntry->r_offset = convertor(pEntry->r_offset); - pEntry->r_info = convertor(pEntry->r_info); - pEntry->r_addend = convertor(pEntry->r_addend); + pEntry->r_offset = convertor( pEntry->r_offset ); + pEntry->r_info = convertor( pEntry->r_info ); + pEntry->r_addend = convertor( pEntry->r_addend ); } -//------------------------------------------------------------------------------ - template< class T > - void - generic_add_entry( Elf64_Addr offset, Elf_Xword info ) + //------------------------------------------------------------------------------ + template <class T> + void generic_add_entry( Elf64_Addr offset, Elf_Xword info ) { const endianess_convertor& convertor = elf_file.get_convertor(); @@ -444,11 +420,12 @@ class relocation_section_accessor_template entry.r_offset = convertor( entry.r_offset ); entry.r_info = convertor( entry.r_info ); - relocation_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) ); + relocation_section->append_data( reinterpret_cast<char*>( &entry ), + sizeof( entry ) ); } -//------------------------------------------------------------------------------ - template< class T > + //------------------------------------------------------------------------------ + template <class T> void generic_add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend ) { @@ -462,17 +439,20 @@ class relocation_section_accessor_template entry.r_info = convertor( entry.r_info ); entry.r_addend = convertor( entry.r_addend ); - relocation_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) ); + relocation_section->append_data( reinterpret_cast<char*>( &entry ), + sizeof( entry ) ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: const elfio& elf_file; S* relocation_section; }; -using relocation_section_accessor = relocation_section_accessor_template<section>; -using const_relocation_section_accessor = relocation_section_accessor_template<const section>; +using relocation_section_accessor = + relocation_section_accessor_template<section>; +using const_relocation_section_accessor = + relocation_section_accessor_template<const section>; } // namespace ELFIO diff --git a/elfio/elfio_section.hpp b/elfio/elfio_section.hpp index 04abebe2b114c07462e4b721eaf31b8a8868055c..6771296ca022e1d1ec8cf4e4c30626620582bd12 100644 --- a/elfio/elfio_section.hpp +++ b/elfio/elfio_section.hpp @@ -31,21 +31,22 @@ namespace ELFIO { class section { friend class elfio; + public: - virtual ~section() {}; - - ELFIO_GET_ACCESS_DECL ( Elf_Half, index ); - ELFIO_GET_SET_ACCESS_DECL( std::string, name ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, flags ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, info ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, link ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, addr_align ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, entry_size ); - ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, address ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, size ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset ); - ELFIO_GET_ACCESS_DECL ( Elf64_Off, offset ); + virtual ~section(){}; + + ELFIO_GET_ACCESS_DECL( Elf_Half, index ); + ELFIO_GET_SET_ACCESS_DECL( std::string, name ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, flags ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, info ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, link ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, addr_align ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, entry_size ); + ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, address ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, size ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset ); + ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); virtual const char* get_data() const = 0; virtual void set_data( const char* pData, Elf_Word size ) = 0; @@ -55,27 +56,26 @@ class section virtual size_t get_stream_size() const = 0; virtual void set_stream_size( size_t value ) = 0; -protected: + protected: ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); - ELFIO_SET_ACCESS_DECL( Elf_Half, index ); - - virtual void load( std::istream& stream, - std::streampos header_offset ) = 0; + ELFIO_SET_ACCESS_DECL( Elf_Half, index ); + + virtual void load( std::istream& stream, std::streampos header_offset ) = 0; virtual void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) = 0; - virtual bool is_address_initialized() const = 0; + std::streampos data_offset ) = 0; + virtual bool is_address_initialized() const = 0; }; - -template< class T > -class section_impl : public section +template <class T> class section_impl : public section { public: -//------------------------------------------------------------------------------ - section_impl( const endianess_convertor* convertor_ ) : convertor( convertor_ ) + //------------------------------------------------------------------------------ + section_impl( const endianess_convertor* convertor_ ) + : convertor( convertor_ ) { - std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' ); + std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), + '\0' ); is_address_set = false; data = 0; data_size = 0; @@ -83,78 +83,53 @@ class section_impl : public section stream_size = 0; } -//------------------------------------------------------------------------------ - ~section_impl() - { - delete [] data; - } + //------------------------------------------------------------------------------ + ~section_impl() { delete[] data; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ // Section info functions - ELFIO_GET_SET_ACCESS( Elf_Word, type, header.sh_type ); - ELFIO_GET_SET_ACCESS( Elf_Xword, flags, header.sh_flags ); - ELFIO_GET_SET_ACCESS( Elf_Xword, size, header.sh_size ); - ELFIO_GET_SET_ACCESS( Elf_Word, link, header.sh_link ); - ELFIO_GET_SET_ACCESS( Elf_Word, info, header.sh_info ); - ELFIO_GET_SET_ACCESS( Elf_Xword, addr_align, header.sh_addralign ); - ELFIO_GET_SET_ACCESS( Elf_Xword, entry_size, header.sh_entsize ); - ELFIO_GET_SET_ACCESS( Elf_Word, name_string_offset, header.sh_name ); - ELFIO_GET_ACCESS ( Elf64_Addr, address, header.sh_addr ); - -//------------------------------------------------------------------------------ - Elf_Half - get_index() const - { - return index; - } + ELFIO_GET_SET_ACCESS( Elf_Word, type, header.sh_type ); + ELFIO_GET_SET_ACCESS( Elf_Xword, flags, header.sh_flags ); + ELFIO_GET_SET_ACCESS( Elf_Xword, size, header.sh_size ); + ELFIO_GET_SET_ACCESS( Elf_Word, link, header.sh_link ); + ELFIO_GET_SET_ACCESS( Elf_Word, info, header.sh_info ); + ELFIO_GET_SET_ACCESS( Elf_Xword, addr_align, header.sh_addralign ); + ELFIO_GET_SET_ACCESS( Elf_Xword, entry_size, header.sh_entsize ); + ELFIO_GET_SET_ACCESS( Elf_Word, name_string_offset, header.sh_name ); + ELFIO_GET_ACCESS( Elf64_Addr, address, header.sh_addr ); + //------------------------------------------------------------------------------ + Elf_Half get_index() const { return index; } -//------------------------------------------------------------------------------ - std::string - get_name() const - { - return name; - } + //------------------------------------------------------------------------------ + std::string get_name() const { return name; } -//------------------------------------------------------------------------------ - void - set_name( std::string name_ ) - { - name = name_; - } + //------------------------------------------------------------------------------ + void set_name( std::string name_ ) { name = name_; } -//------------------------------------------------------------------------------ - void - set_address( Elf64_Addr value ) + //------------------------------------------------------------------------------ + void set_address( Elf64_Addr value ) { header.sh_addr = value; - header.sh_addr = (*convertor)( header.sh_addr ); + header.sh_addr = ( *convertor )( header.sh_addr ); is_address_set = true; } -//------------------------------------------------------------------------------ - bool - is_address_initialized() const - { - return is_address_set; - } + //------------------------------------------------------------------------------ + bool is_address_initialized() const { return is_address_set; } -//------------------------------------------------------------------------------ - const char* - get_data() const - { - return data; - } + //------------------------------------------------------------------------------ + const char* get_data() const { return data; } -//------------------------------------------------------------------------------ - void - set_data( const char* raw_data, Elf_Word size ) + //------------------------------------------------------------------------------ + void set_data( const char* raw_data, Elf_Word size ) { if ( get_type() != SHT_NOBITS ) { - delete [] data; + delete[] data; try { data = new char[size]; - } catch (const std::bad_alloc&) { + } + catch ( const std::bad_alloc& ) { data = 0; data_size = 0; size = 0; @@ -168,34 +143,34 @@ class section_impl : public section set_size( size ); } -//------------------------------------------------------------------------------ - void - set_data( const std::string& str_data ) + //------------------------------------------------------------------------------ + void set_data( const std::string& str_data ) { return set_data( str_data.c_str(), (Elf_Word)str_data.size() ); } -//------------------------------------------------------------------------------ - void - append_data( const char* raw_data, Elf_Word size ) + //------------------------------------------------------------------------------ + void append_data( const char* raw_data, Elf_Word size ) { if ( get_type() != SHT_NOBITS ) { if ( get_size() + size < data_size ) { std::copy( raw_data, raw_data + size, data + get_size() ); } else { - data_size = 2*( data_size + size); + data_size = 2 * ( data_size + size ); char* new_data; try { new_data = new char[data_size]; - } catch (const std::bad_alloc&) { + } + catch ( const std::bad_alloc& ) { new_data = 0; size = 0; } if ( 0 != new_data ) { std::copy( data, data + get_size(), new_data ); - std::copy( raw_data, raw_data + size, new_data + get_size() ); - delete [] data; + std::copy( raw_data, raw_data + size, + new_data + get_size() ); + delete[] data; data = new_data; } } @@ -203,66 +178,60 @@ class section_impl : public section } } -//------------------------------------------------------------------------------ - void - append_data( const std::string& str_data ) + //------------------------------------------------------------------------------ + void append_data( const std::string& str_data ) { return append_data( str_data.c_str(), (Elf_Word)str_data.size() ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ protected: -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ ELFIO_GET_SET_ACCESS( Elf64_Off, offset, header.sh_offset ); -//------------------------------------------------------------------------------ - void - set_index( Elf_Half value ) - { - index = value; - } + //------------------------------------------------------------------------------ + void set_index( Elf_Half value ) { index = value; } -//------------------------------------------------------------------------------ - void - load( std::istream& stream, - std::streampos header_offset ) + //------------------------------------------------------------------------------ + void load( std::istream& stream, std::streampos header_offset ) { - std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' ); + std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), + '\0' ); - stream.seekg ( 0, stream.end ); - set_stream_size ( stream.tellg() ); + stream.seekg( 0, stream.end ); + set_stream_size( stream.tellg() ); stream.seekg( header_offset ); stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) ); - Elf_Xword size = get_size(); - if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() && size < get_stream_size()) { + if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() && + size < get_stream_size() ) { try { data = new char[size + 1]; - } catch (const std::bad_alloc&) { + } + catch ( const std::bad_alloc& ) { data = 0; data_size = 0; } if ( ( 0 != size ) && ( 0 != data ) ) { - stream.seekg( (*convertor)( header.sh_offset ) ); + stream.seekg( ( *convertor )( header.sh_offset ) ); stream.read( data, size ); data[size] = 0; // Ensure data is ended with 0 to avoid oob read - data_size = size; + data_size = size; } } } -//------------------------------------------------------------------------------ - void - save( std::ostream& stream, - std::streampos header_offset, - std::streampos data_offset ) + //------------------------------------------------------------------------------ + void save( std::ostream& stream, + std::streampos header_offset, + std::streampos data_offset ) { if ( 0 != get_index() ) { header.sh_offset = data_offset; - header.sh_offset = (*convertor)( header.sh_offset ); + header.sh_offset = ( *convertor )( header.sh_offset ); } save_header( stream, header_offset ); @@ -272,39 +241,30 @@ class section_impl : public section } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: -//------------------------------------------------------------------------------ - void - save_header( std::ostream& stream, - std::streampos header_offset ) const + //------------------------------------------------------------------------------ + void save_header( std::ostream& stream, std::streampos header_offset ) const { stream.seekp( header_offset ); - stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) ); + stream.write( reinterpret_cast<const char*>( &header ), + sizeof( header ) ); } -//------------------------------------------------------------------------------ - void - save_data( std::ostream& stream, - std::streampos data_offset ) const + //------------------------------------------------------------------------------ + void save_data( std::ostream& stream, std::streampos data_offset ) const { stream.seekp( data_offset ); stream.write( get_data(), get_size() ); } //------------------------------------------------------------------------------ - size_t get_stream_size() const - { - return stream_size; - } + size_t get_stream_size() const { return stream_size; } //------------------------------------------------------------------------------ - void set_stream_size(size_t value) - { - stream_size = value; - } + void set_stream_size( size_t value ) { stream_size = value; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: T header; Elf_Half index; diff --git a/elfio/elfio_segment.hpp b/elfio/elfio_segment.hpp index 76cf93da5226532f89635eec5a112189ab01db25..763341f288fd3ecba9e4e7ac649b08545fbacb70 100644 --- a/elfio/elfio_segment.hpp +++ b/elfio/elfio_segment.hpp @@ -31,99 +31,80 @@ namespace ELFIO { class segment { friend class elfio; + public: - virtual ~segment() {}; + virtual ~segment(){}; - ELFIO_GET_ACCESS_DECL ( Elf_Half, index ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align ); - ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, virtual_address ); + ELFIO_GET_ACCESS_DECL( Elf_Half, index ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align ); + ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, virtual_address ); ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, physical_address ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size ); - ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size ); + ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size ); ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); virtual const char* get_data() const = 0; - virtual Elf_Half add_section_index( Elf_Half index, Elf_Xword addr_align ) = 0; - virtual Elf_Half get_sections_num() const = 0; - virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0; - virtual bool is_offset_initialized() const = 0; + virtual Elf_Half add_section_index( Elf_Half index, + Elf_Xword addr_align ) = 0; + virtual Elf_Half get_sections_num() const = 0; + virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0; + virtual bool is_offset_initialized() const = 0; protected: ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); - ELFIO_SET_ACCESS_DECL( Elf_Half, index ); - + ELFIO_SET_ACCESS_DECL( Elf_Half, index ); + virtual const std::vector<Elf_Half>& get_sections() const = 0; virtual void load( std::istream& stream, std::streampos header_offset ) = 0; - virtual void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) = 0; + virtual void save( std::ostream& stream, + std::streampos header_offset, + std::streampos data_offset ) = 0; }; - //------------------------------------------------------------------------------ -template< class T > -class segment_impl : public segment +template <class T> class segment_impl : public segment { public: -//------------------------------------------------------------------------------ - segment_impl( endianess_convertor* convertor_ ) : - stream_size( 0 ), index( 0 ), data( 0 ), convertor( convertor_ ) + //------------------------------------------------------------------------------ + segment_impl( endianess_convertor* convertor_ ) + : stream_size( 0 ), index( 0 ), data( 0 ), convertor( convertor_ ) { is_offset_set = false; std::fill_n( reinterpret_cast<char*>( &ph ), sizeof( ph ), '\0' ); } -//------------------------------------------------------------------------------ - virtual ~segment_impl() - { - delete [] data; - } + //------------------------------------------------------------------------------ + virtual ~segment_impl() { delete[] data; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ // Section info functions - ELFIO_GET_SET_ACCESS( Elf_Word, type, ph.p_type ); - ELFIO_GET_SET_ACCESS( Elf_Word, flags, ph.p_flags ); - ELFIO_GET_SET_ACCESS( Elf_Xword, align, ph.p_align ); - ELFIO_GET_SET_ACCESS( Elf64_Addr, virtual_address, ph.p_vaddr ); - ELFIO_GET_SET_ACCESS( Elf64_Addr, physical_address, ph.p_paddr ); - ELFIO_GET_SET_ACCESS( Elf_Xword, file_size, ph.p_filesz ); - ELFIO_GET_SET_ACCESS( Elf_Xword, memory_size, ph.p_memsz ); + ELFIO_GET_SET_ACCESS( Elf_Word, type, ph.p_type ); + ELFIO_GET_SET_ACCESS( Elf_Word, flags, ph.p_flags ); + ELFIO_GET_SET_ACCESS( Elf_Xword, align, ph.p_align ); + ELFIO_GET_SET_ACCESS( Elf64_Addr, virtual_address, ph.p_vaddr ); + ELFIO_GET_SET_ACCESS( Elf64_Addr, physical_address, ph.p_paddr ); + ELFIO_GET_SET_ACCESS( Elf_Xword, file_size, ph.p_filesz ); + ELFIO_GET_SET_ACCESS( Elf_Xword, memory_size, ph.p_memsz ); ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset ); size_t stream_size; -//------------------------------------------------------------------------------ - size_t - get_stream_size() const - { - return stream_size; - } + //------------------------------------------------------------------------------ + size_t get_stream_size() const { return stream_size; } -//------------------------------------------------------------------------------ - void - set_stream_size(size_t value) - { - stream_size = value; - } + //------------------------------------------------------------------------------ + void set_stream_size( size_t value ) { stream_size = value; } -//------------------------------------------------------------------------------ - Elf_Half - get_index() const - { - return index; - } + //------------------------------------------------------------------------------ + Elf_Half get_index() const { return index; } -//------------------------------------------------------------------------------ - const char* - get_data() const - { - return data; - } + //------------------------------------------------------------------------------ + const char* get_data() const { return data; } -//------------------------------------------------------------------------------ - Elf_Half - add_section_index( Elf_Half sec_index, Elf_Xword addr_align ) + //------------------------------------------------------------------------------ + Elf_Half add_section_index( Elf_Half sec_index, Elf_Xword addr_align ) { sections.push_back( sec_index ); if ( addr_align > get_align() ) { @@ -133,73 +114,53 @@ class segment_impl : public segment return (Elf_Half)sections.size(); } -//------------------------------------------------------------------------------ - Elf_Half - get_sections_num() const - { - return (Elf_Half)sections.size(); - } + //------------------------------------------------------------------------------ + Elf_Half get_sections_num() const { return (Elf_Half)sections.size(); } -//------------------------------------------------------------------------------ - Elf_Half - get_section_index_at( Elf_Half num ) const + //------------------------------------------------------------------------------ + Elf_Half get_section_index_at( Elf_Half num ) const { if ( num < sections.size() ) { return sections[num]; } - return Elf_Half(-1); + return Elf_Half( -1 ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ protected: -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ - void - set_offset( Elf64_Off value ) + //------------------------------------------------------------------------------ + void set_offset( Elf64_Off value ) { - ph.p_offset = value; - ph.p_offset = (*convertor)( ph.p_offset ); + ph.p_offset = value; + ph.p_offset = ( *convertor )( ph.p_offset ); is_offset_set = true; } -//------------------------------------------------------------------------------ - bool - is_offset_initialized() const - { - return is_offset_set; - } + //------------------------------------------------------------------------------ + bool is_offset_initialized() const { return is_offset_set; } -//------------------------------------------------------------------------------ - const std::vector<Elf_Half>& - get_sections() const - { - return sections; - } - -//------------------------------------------------------------------------------ - void - set_index( Elf_Half value ) - { - index = value; - } + //------------------------------------------------------------------------------ + const std::vector<Elf_Half>& get_sections() const { return sections; } -//------------------------------------------------------------------------------ - void - load( std::istream& stream, - std::streampos header_offset ) + //------------------------------------------------------------------------------ + void set_index( Elf_Half value ) { index = value; } + + //------------------------------------------------------------------------------ + void load( std::istream& stream, std::streampos header_offset ) { - stream.seekg ( 0, stream.end ); - set_stream_size ( stream.tellg() ); + stream.seekg( 0, stream.end ); + set_stream_size( stream.tellg() ); stream.seekg( header_offset ); stream.read( reinterpret_cast<char*>( &ph ), sizeof( ph ) ); is_offset_set = true; if ( PT_NULL != get_type() && 0 != get_file_size() ) { - stream.seekg( (*convertor)( ph.p_offset ) ); + stream.seekg( ( *convertor )( ph.p_offset ) ); Elf_Xword size = get_file_size(); if ( size > get_stream_size() ) { @@ -208,10 +169,11 @@ class segment_impl : public segment else { try { data = new char[size + 1]; - } catch (const std::bad_alloc&) { + } + catch ( const std::bad_alloc& ) { data = 0; } - + if ( 0 != data ) { stream.read( data, size ); data[size] = 0; @@ -220,18 +182,18 @@ class segment_impl : public segment } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ void save( std::ostream& stream, std::streampos header_offset, std::streampos data_offset ) { ph.p_offset = data_offset; - ph.p_offset = (*convertor)(ph.p_offset); + ph.p_offset = ( *convertor )( ph.p_offset ); stream.seekp( header_offset ); stream.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: T ph; Elf_Half index; diff --git a/elfio/elfio_strings.hpp b/elfio/elfio_strings.hpp index 94e7724a8c048d2a7a0cc4b2a0e7ac891716e466..720dd9ea4127527ea565872d93159b022dfb4c00 100644 --- a/elfio/elfio_strings.hpp +++ b/elfio/elfio_strings.hpp @@ -30,20 +30,16 @@ THE SOFTWARE. namespace ELFIO { //------------------------------------------------------------------------------ -template< class S > -class string_section_accessor_template +template <class S> class string_section_accessor_template { public: -//------------------------------------------------------------------------------ - string_section_accessor_template( S* section_ ) : - string_section( section_ ) + //------------------------------------------------------------------------------ + string_section_accessor_template( S* section_ ) : string_section( section_ ) { } - -//------------------------------------------------------------------------------ - const char* - get_string( Elf_Word index ) const + //------------------------------------------------------------------------------ + const char* get_string( Elf_Word index ) const { if ( string_section ) { if ( index < string_section->get_size() ) { @@ -57,14 +53,12 @@ class string_section_accessor_template return 0; } - -//------------------------------------------------------------------------------ - Elf_Word - add_string( const char* str ) + //------------------------------------------------------------------------------ + Elf_Word add_string( const char* str ) { Elf_Word current_position = 0; - - if (string_section) { + + if ( string_section ) { // Strings are addeded to the end of the current section data current_position = (Elf_Word)string_section->get_size(); @@ -73,27 +67,27 @@ class string_section_accessor_template string_section->append_data( &empty_string, 1 ); current_position++; } - string_section->append_data( str, (Elf_Word)std::strlen( str ) + 1 ); + string_section->append_data( str, + (Elf_Word)std::strlen( str ) + 1 ); } return current_position; } - -//------------------------------------------------------------------------------ - Elf_Word - add_string( const std::string& str ) + //------------------------------------------------------------------------------ + Elf_Word add_string( const std::string& str ) { return add_string( str.c_str() ); } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: S* string_section; }; -using string_section_accessor = string_section_accessor_template<section>; -using const_string_section_accessor = string_section_accessor_template<const section>; +using string_section_accessor = string_section_accessor_template<section>; +using const_string_section_accessor = + string_section_accessor_template<const section>; } // namespace ELFIO diff --git a/elfio/elfio_symbols.hpp b/elfio/elfio_symbols.hpp index 33a34107f3b02a5ece7bb010dfa7a1e28b20e928..b0b6d6e948ecad1708359dddbd510ea0bf840de8 100644 --- a/elfio/elfio_symbols.hpp +++ b/elfio/elfio_symbols.hpp @@ -26,40 +26,38 @@ THE SOFTWARE. namespace ELFIO { //------------------------------------------------------------------------------ -template< class S > -class symbol_section_accessor_template +template <class S> class symbol_section_accessor_template { public: -//------------------------------------------------------------------------------ - symbol_section_accessor_template( const elfio& elf_file_, S* symbol_section_ ) : - elf_file( elf_file_ ), - symbol_section( symbol_section_ ) + //------------------------------------------------------------------------------ + symbol_section_accessor_template( const elfio& elf_file_, + S* symbol_section_ ) + : elf_file( elf_file_ ), symbol_section( symbol_section_ ) { find_hash_section(); } -//------------------------------------------------------------------------------ - Elf_Xword - get_symbols_num() const + //------------------------------------------------------------------------------ + Elf_Xword get_symbols_num() const { Elf_Xword nRet = 0; if ( 0 != symbol_section->get_entry_size() ) { - nRet = symbol_section->get_size() / symbol_section->get_entry_size(); + nRet = + symbol_section->get_size() / symbol_section->get_entry_size(); } return nRet; } -//------------------------------------------------------------------------------ - bool - get_symbol( Elf_Xword index, - std::string& name, - Elf64_Addr& value, - Elf_Xword& size, - unsigned char& bind, - unsigned char& type, - Elf_Half& section_index, - unsigned char& other ) const + //------------------------------------------------------------------------------ + bool get_symbol( Elf_Xword index, + std::string& name, + Elf64_Addr& value, + Elf_Xword& size, + unsigned char& bind, + unsigned char& type, + Elf_Half& section_index, + unsigned char& other ) const { bool ret = false; @@ -75,33 +73,35 @@ class symbol_section_accessor_template return ret; } -//------------------------------------------------------------------------------ - bool - get_symbol( const std::string& name, - Elf64_Addr& value, - Elf_Xword& size, - unsigned char& bind, - unsigned char& type, - Elf_Half& section_index, - unsigned char& other ) const + //------------------------------------------------------------------------------ + bool get_symbol( const std::string& name, + Elf64_Addr& value, + Elf_Xword& size, + unsigned char& bind, + unsigned char& type, + Elf_Half& section_index, + unsigned char& other ) const { bool ret = false; if ( 0 != get_hash_table_index() ) { - Elf_Word nbucket = *(const Elf_Word*)hash_section->get_data(); - Elf_Word nchain = *(const Elf_Word*)( hash_section->get_data() + - sizeof( Elf_Word ) ); - Elf_Word val = elf_hash( (const unsigned char*)name.c_str() ); - Elf_Word y = *(const Elf_Word*)( hash_section->get_data() + - ( 2 + val % nbucket ) * sizeof( Elf_Word ) ); - std::string str; + Elf_Word nbucket = *(const Elf_Word*)hash_section->get_data(); + Elf_Word nchain = *(const Elf_Word*)( hash_section->get_data() + + sizeof( Elf_Word ) ); + Elf_Word val = elf_hash( (const unsigned char*)name.c_str() ); + Elf_Word y = *(const Elf_Word*)( hash_section->get_data() + + ( 2 + val % nbucket ) * + sizeof( Elf_Word ) ); + std::string str; get_symbol( y, str, value, size, bind, type, section_index, other ); while ( str != name && STN_UNDEF != y && y < nchain ) { y = *(const Elf_Word*)( hash_section->get_data() + - ( 2 + nbucket + y ) * sizeof( Elf_Word ) ); - get_symbol( y, str, value, size, bind, type, section_index, other ); + ( 2 + nbucket + y ) * + sizeof( Elf_Word ) ); + get_symbol( y, str, value, size, bind, type, section_index, + other ); } - if ( str == name ) { + if ( str == name ) { ret = true; } } @@ -109,7 +109,7 @@ class symbol_section_accessor_template for ( Elf_Xword i = 0; i < get_symbols_num() && !ret; i++ ) { std::string symbol_name; if ( get_symbol( i, symbol_name, value, size, bind, type, - section_index, other) ) { + section_index, other ) ) { if ( symbol_name == name ) { ret = true; } @@ -120,18 +120,16 @@ class symbol_section_accessor_template return ret; } - //------------------------------------------------------------------------------ - bool - get_symbol( const Elf64_Addr& value, - std::string& name, - Elf_Xword& size, - unsigned char& bind, - unsigned char& type, - Elf_Half& section_index, - unsigned char& other ) const + //------------------------------------------------------------------------------ + bool get_symbol( const Elf64_Addr& value, + std::string& name, + Elf_Xword& size, + unsigned char& bind, + unsigned char& type, + Elf_Half& section_index, + unsigned char& other ) const { - const endianess_convertor& convertor = elf_file.get_convertor(); Elf_Xword idx = 0; @@ -139,27 +137,35 @@ class symbol_section_accessor_template Elf64_Addr v = 0; if ( elf_file.get_class() == ELFCLASS32 ) { - match = generic_search_symbols<Elf32_Sym>([&](const Elf32_Sym* sym) { - return convertor(sym->st_value) == value; - }, idx); - } else { - match = generic_search_symbols<Elf64_Sym>([&](const Elf64_Sym* sym) { - return convertor(sym->st_value) == value; - }, idx); + match = generic_search_symbols<Elf32_Sym>( + [&]( const Elf32_Sym* sym ) { + return convertor( sym->st_value ) == value; + }, + idx ); + } + else { + match = generic_search_symbols<Elf64_Sym>( + [&]( const Elf64_Sym* sym ) { + return convertor( sym->st_value ) == value; + }, + idx ); } if ( match ) { - return get_symbol( idx, name, v, size, bind, type, section_index, other ); + return get_symbol( idx, name, v, size, bind, type, section_index, + other ); } return false; } -//------------------------------------------------------------------------------ - Elf_Word - add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size, - unsigned char info, unsigned char other, - Elf_Half shndx ) + //------------------------------------------------------------------------------ + Elf_Word add_symbol( Elf_Word name, + Elf64_Addr value, + Elf_Xword size, + unsigned char info, + unsigned char other, + Elf_Half shndx ) { Elf_Word nRet; @@ -173,72 +179,82 @@ class symbol_section_accessor_template } if ( elf_file.get_class() == ELFCLASS32 ) { - nRet = generic_add_symbol<Elf32_Sym>( name, value, size, info, other, - shndx ); + nRet = generic_add_symbol<Elf32_Sym>( name, value, size, info, + other, shndx ); } else { - nRet = generic_add_symbol<Elf64_Sym>( name, value, size, info, other, - shndx ); + nRet = generic_add_symbol<Elf64_Sym>( name, value, size, info, + other, shndx ); } return nRet; } -//------------------------------------------------------------------------------ - Elf_Word - add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size, - unsigned char bind, unsigned char type, unsigned char other, - Elf_Half shndx ) + //------------------------------------------------------------------------------ + Elf_Word add_symbol( Elf_Word name, + Elf64_Addr value, + Elf_Xword size, + unsigned char bind, + unsigned char type, + unsigned char other, + Elf_Half shndx ) { - return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other, shndx ); + return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other, + shndx ); } -//------------------------------------------------------------------------------ - Elf_Word - add_symbol( string_section_accessor& pStrWriter, const char* str, - Elf64_Addr value, Elf_Xword size, - unsigned char info, unsigned char other, - Elf_Half shndx ) + //------------------------------------------------------------------------------ + Elf_Word add_symbol( string_section_accessor& pStrWriter, + const char* str, + Elf64_Addr value, + Elf_Xword size, + unsigned char info, + unsigned char other, + Elf_Half shndx ) { Elf_Word index = pStrWriter.add_string( str ); return add_symbol( index, value, size, info, other, shndx ); } -//------------------------------------------------------------------------------ - Elf_Word - add_symbol( string_section_accessor& pStrWriter, const char* str, - Elf64_Addr value, Elf_Xword size, - unsigned char bind, unsigned char type, unsigned char other, - Elf_Half shndx ) + //------------------------------------------------------------------------------ + Elf_Word add_symbol( string_section_accessor& pStrWriter, + const char* str, + Elf64_Addr value, + Elf_Xword size, + unsigned char bind, + unsigned char type, + unsigned char other, + Elf_Half shndx ) { - return add_symbol( pStrWriter, str, value, size, ELF_ST_INFO( bind, type ), other, shndx ); + return add_symbol( pStrWriter, str, value, size, + ELF_ST_INFO( bind, type ), other, shndx ); } -//------------------------------------------------------------------------------ - Elf_Xword - arrange_local_symbols(std::function<void(Elf_Xword first, Elf_Xword second)> func = nullptr) + //------------------------------------------------------------------------------ + Elf_Xword arrange_local_symbols( + std::function<void( Elf_Xword first, Elf_Xword second )> func = + nullptr ) { int nRet = 0; - if (elf_file.get_class() == ELFCLASS32) { - nRet = generic_arrange_local_symbols<Elf32_Sym>(func); + if ( elf_file.get_class() == ELFCLASS32 ) { + nRet = generic_arrange_local_symbols<Elf32_Sym>( func ); } else { - nRet = generic_arrange_local_symbols<Elf64_Sym>(func); + nRet = generic_arrange_local_symbols<Elf64_Sym>( func ); } return nRet; } -//------------------------------------------------------------------------------ - private : -//------------------------------------------------------------------------------ - void - find_hash_section() + //------------------------------------------------------------------------------ + private: + //------------------------------------------------------------------------------ + void find_hash_section() { hash_section = 0; hash_section_index = 0; - Elf_Half nSecNo = elf_file.sections.size(); + Elf_Half nSecNo = elf_file.sections.size(); for ( Elf_Half i = 0; i < nSecNo && 0 == hash_section_index; ++i ) { const section* sec = elf_file.sections[i]; if ( sec->get_link() == symbol_section->get_index() ) { @@ -248,28 +264,22 @@ class symbol_section_accessor_template } } -//------------------------------------------------------------------------------ - Elf_Half - get_string_table_index() const + //------------------------------------------------------------------------------ + Elf_Half get_string_table_index() const { return (Elf_Half)symbol_section->get_link(); } -//------------------------------------------------------------------------------ - Elf_Half - get_hash_table_index() const - { - return hash_section_index; - } + //------------------------------------------------------------------------------ + Elf_Half get_hash_table_index() const { return hash_section_index; } -//------------------------------------------------------------------------------ - template< class T > - const T* - generic_get_symbol_ptr(Elf_Xword index) const { + //------------------------------------------------------------------------------ + template <class T> const T* generic_get_symbol_ptr( Elf_Xword index ) const + { if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) { const T* pSym = reinterpret_cast<const T*>( symbol_section->get_data() + - index * symbol_section->get_entry_size() ); + index * symbol_section->get_entry_size() ); return pSym; } @@ -277,17 +287,18 @@ class symbol_section_accessor_template return nullptr; } -//------------------------------------------------------------------------------ - template< class T > - bool - generic_search_symbols(std::function<bool(const T*)> match, Elf_Xword& idx) const { - for (Elf_Xword i = 0; i < get_symbols_num(); i++){ - const T* symPtr = generic_get_symbol_ptr<T>(i); + //------------------------------------------------------------------------------ + template <class T> + bool generic_search_symbols( std::function<bool( const T* )> match, + Elf_Xword& idx ) const + { + for ( Elf_Xword i = 0; i < get_symbols_num(); i++ ) { + const T* symPtr = generic_get_symbol_ptr<T>( i ); - if (symPtr == nullptr) + if ( symPtr == nullptr ) return false; - if (match(symPtr)) { + if ( match( symPtr ) ) { idx = i; return true; } @@ -296,37 +307,40 @@ class symbol_section_accessor_template return false; } -//------------------------------------------------------------------------------ - template< class T > - bool - generic_get_symbol( Elf_Xword index, - std::string& name, Elf64_Addr& value, - Elf_Xword& size, - unsigned char& bind, unsigned char& type, - Elf_Half& section_index, - unsigned char& other ) const + //------------------------------------------------------------------------------ + template <class T> + bool generic_get_symbol( Elf_Xword index, + std::string& name, + Elf64_Addr& value, + Elf_Xword& size, + unsigned char& bind, + unsigned char& type, + Elf_Half& section_index, + unsigned char& other ) const { bool ret = false; if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) { const T* pSym = reinterpret_cast<const T*>( symbol_section->get_data() + - index * symbol_section->get_entry_size() ); + index * symbol_section->get_entry_size() ); const endianess_convertor& convertor = elf_file.get_convertor(); - section* string_section = elf_file.sections[get_string_table_index()]; + section* string_section = + elf_file.sections[get_string_table_index()]; string_section_accessor str_reader( string_section ); - const char* pStr = str_reader.get_string( convertor( pSym->st_name ) ); + const char* pStr = + str_reader.get_string( convertor( pSym->st_name ) ); if ( 0 != pStr ) { name = pStr; } - value = convertor( pSym->st_value ); - size = convertor( pSym->st_size ); - bind = ELF_ST_BIND( pSym->st_info ); - type = ELF_ST_TYPE( pSym->st_info ); + value = convertor( pSym->st_value ); + size = convertor( pSym->st_size ); + bind = ELF_ST_BIND( pSym->st_info ); + type = ELF_ST_TYPE( pSym->st_info ); section_index = convertor( pSym->st_shndx ); - other = pSym->st_other; + other = pSym->st_other; ret = true; } @@ -334,12 +348,14 @@ class symbol_section_accessor_template return ret; } -//------------------------------------------------------------------------------ - template< class T > - Elf_Word - generic_add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size, - unsigned char info, unsigned char other, - Elf_Half shndx ) + //------------------------------------------------------------------------------ + template <class T> + Elf_Word generic_add_symbol( Elf_Word name, + Elf64_Addr value, + Elf_Xword size, + unsigned char info, + unsigned char other, + Elf_Half shndx ) { const endianess_convertor& convertor = elf_file.get_convertor(); @@ -363,53 +379,50 @@ class symbol_section_accessor_template //------------------------------------------------------------------------------ template <class T> - Elf_Xword - generic_arrange_local_symbols(std::function<void (Elf_Xword first, Elf_Xword second)> func) + Elf_Xword generic_arrange_local_symbols( + std::function<void( Elf_Xword first, Elf_Xword second )> func ) { - const endianess_convertor &convertor = elf_file.get_convertor(); - const Elf_Xword size = symbol_section->get_entry_size(); - - Elf_Xword first_not_local = 1; // Skip the first entry. It is always NOTYPE - Elf_Xword current = 0; - Elf_Xword count = get_symbols_num(); - - while (true) - { - T *p1 = nullptr; - T *p2 = nullptr; - - while (first_not_local < count) - { - p1 = const_cast<T *>(generic_get_symbol_ptr<T>(first_not_local)); - if (ELF_ST_BIND(convertor(p1->st_info)) != STB_LOCAL) + const endianess_convertor& convertor = elf_file.get_convertor(); + const Elf_Xword size = symbol_section->get_entry_size(); + + Elf_Xword first_not_local = + 1; // Skip the first entry. It is always NOTYPE + Elf_Xword current = 0; + Elf_Xword count = get_symbols_num(); + + while ( true ) { + T* p1 = nullptr; + T* p2 = nullptr; + + while ( first_not_local < count ) { + p1 = const_cast<T*>( + generic_get_symbol_ptr<T>( first_not_local ) ); + if ( ELF_ST_BIND( convertor( p1->st_info ) ) != STB_LOCAL ) break; ++first_not_local; } - + current = first_not_local + 1; - while (current < count) - { - p2 = const_cast<T *>(generic_get_symbol_ptr<T>(current)); - if (ELF_ST_BIND(convertor(p2->st_info)) == STB_LOCAL) + while ( current < count ) { + p2 = const_cast<T*>( generic_get_symbol_ptr<T>( current ) ); + if ( ELF_ST_BIND( convertor( p2->st_info ) ) == STB_LOCAL ) break; ++current; } - if (first_not_local < count && current < count) - { - if (func) - func(first_not_local, current); + if ( first_not_local < count && current < count ) { + if ( func ) + func( first_not_local, current ); // Swap the symbols T tmp; - std::copy(p1, p1 + 1, &tmp); - std::copy(p2, p2 + 1, p1); - std::copy(&tmp, &tmp + 1, p2); + std::copy( p1, p1 + 1, &tmp ); + std::copy( p2, p2 + 1, p1 ); + std::copy( &tmp, &tmp + 1, p2 ); } - else - { + else { // Update 'info' field of the section - symbol_section->set_info(first_not_local); + symbol_section->set_info( first_not_local ); break; } } @@ -419,7 +432,7 @@ class symbol_section_accessor_template return first_not_local; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: const elfio& elf_file; S* symbol_section; @@ -428,7 +441,8 @@ class symbol_section_accessor_template }; using symbol_section_accessor = symbol_section_accessor_template<section>; -using const_symbol_section_accessor = symbol_section_accessor_template<const section>; +using const_symbol_section_accessor = + symbol_section_accessor_template<const section>; } // namespace ELFIO diff --git a/elfio/elfio_utils.hpp b/elfio/elfio_utils.hpp index 2d522a2dfc7a1222722a52c9cb22cc3c22046f57..1a0f65ce720973ba2521db73f7d66b2cb7b296b0 100644 --- a/elfio/elfio_utils.hpp +++ b/elfio/elfio_utils.hpp @@ -24,154 +24,124 @@ THE SOFTWARE. #define ELFIO_UTILS_HPP #define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \ - TYPE get_##NAME() const \ - { \ - return (*convertor)( FIELD ); \ - } + TYPE get_##NAME() const { return ( *convertor )( FIELD ); } #define ELFIO_SET_ACCESS( TYPE, NAME, FIELD ) \ void set_##NAME( TYPE value ) \ { \ FIELD = value; \ - FIELD = (*convertor)( FIELD ); \ + FIELD = ( *convertor )( FIELD ); \ } -#define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \ - TYPE get_##NAME() const \ - { \ - return (*convertor)( FIELD ); \ - } \ - void set_##NAME( TYPE value ) \ - { \ - FIELD = value; \ - FIELD = (*convertor)( FIELD ); \ +#define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \ + TYPE get_##NAME() const { return ( *convertor )( FIELD ); } \ + void set_##NAME( TYPE value ) \ + { \ + FIELD = value; \ + FIELD = ( *convertor )( FIELD ); \ } -#define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) \ - virtual TYPE get_##NAME() const = 0 +#define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) virtual TYPE get_##NAME() const = 0 #define ELFIO_SET_ACCESS_DECL( TYPE, NAME ) \ virtual void set_##NAME( TYPE value ) = 0 #define ELFIO_GET_SET_ACCESS_DECL( TYPE, NAME ) \ - virtual TYPE get_##NAME() const = 0; \ + virtual TYPE get_##NAME() const = 0; \ virtual void set_##NAME( TYPE value ) = 0 namespace ELFIO { //------------------------------------------------------------------------------ -class endianess_convertor { +class endianess_convertor +{ public: -//------------------------------------------------------------------------------ - endianess_convertor() - { - need_conversion = false; - } + //------------------------------------------------------------------------------ + endianess_convertor() { need_conversion = false; } -//------------------------------------------------------------------------------ - void - setup( unsigned char elf_file_encoding ) + //------------------------------------------------------------------------------ + void setup( unsigned char elf_file_encoding ) { need_conversion = ( elf_file_encoding != get_host_encoding() ); } -//------------------------------------------------------------------------------ - uint64_t - operator()( uint64_t value ) const + //------------------------------------------------------------------------------ + uint64_t operator()( uint64_t value ) const { if ( !need_conversion ) { return value; } - value = - ( ( value & 0x00000000000000FFull ) << 56 ) | - ( ( value & 0x000000000000FF00ull ) << 40 ) | - ( ( value & 0x0000000000FF0000ull ) << 24 ) | - ( ( value & 0x00000000FF000000ull ) << 8 ) | - ( ( value & 0x000000FF00000000ull ) >> 8 ) | - ( ( value & 0x0000FF0000000000ull ) >> 24 ) | - ( ( value & 0x00FF000000000000ull ) >> 40 ) | - ( ( value & 0xFF00000000000000ull ) >> 56 ); + value = ( ( value & 0x00000000000000FFull ) << 56 ) | + ( ( value & 0x000000000000FF00ull ) << 40 ) | + ( ( value & 0x0000000000FF0000ull ) << 24 ) | + ( ( value & 0x00000000FF000000ull ) << 8 ) | + ( ( value & 0x000000FF00000000ull ) >> 8 ) | + ( ( value & 0x0000FF0000000000ull ) >> 24 ) | + ( ( value & 0x00FF000000000000ull ) >> 40 ) | + ( ( value & 0xFF00000000000000ull ) >> 56 ); return value; } -//------------------------------------------------------------------------------ - int64_t - operator()( int64_t value ) const + //------------------------------------------------------------------------------ + int64_t operator()( int64_t value ) const { if ( !need_conversion ) { return value; } - return (int64_t)(*this)( (uint64_t)value ); + return ( int64_t )( *this )( (uint64_t)value ); } -//------------------------------------------------------------------------------ - uint32_t - operator()( uint32_t value ) const + //------------------------------------------------------------------------------ + uint32_t operator()( uint32_t value ) const { if ( !need_conversion ) { return value; } value = - ( ( value & 0x000000FF ) << 24 ) | - ( ( value & 0x0000FF00 ) << 8 ) | - ( ( value & 0x00FF0000 ) >> 8 ) | - ( ( value & 0xFF000000 ) >> 24 ); + ( ( value & 0x000000FF ) << 24 ) | ( ( value & 0x0000FF00 ) << 8 ) | + ( ( value & 0x00FF0000 ) >> 8 ) | ( ( value & 0xFF000000 ) >> 24 ); return value; } -//------------------------------------------------------------------------------ - int32_t - operator()( int32_t value ) const + //------------------------------------------------------------------------------ + int32_t operator()( int32_t value ) const { if ( !need_conversion ) { return value; } - return (int32_t)(*this)( (uint32_t)value ); + return ( int32_t )( *this )( (uint32_t)value ); } -//------------------------------------------------------------------------------ - uint16_t - operator()( uint16_t value ) const + //------------------------------------------------------------------------------ + uint16_t operator()( uint16_t value ) const { if ( !need_conversion ) { return value; } - value = - ( ( value & 0x00FF ) << 8 ) | - ( ( value & 0xFF00 ) >> 8 ); + value = ( ( value & 0x00FF ) << 8 ) | ( ( value & 0xFF00 ) >> 8 ); return value; } -//------------------------------------------------------------------------------ - int16_t - operator()( int16_t value ) const + //------------------------------------------------------------------------------ + int16_t operator()( int16_t value ) const { if ( !need_conversion ) { return value; } - return (int16_t)(*this)( (uint16_t)value ); + return ( int16_t )( *this )( (uint16_t)value ); } -//------------------------------------------------------------------------------ - int8_t - operator()( int8_t value ) const - { - return value; - } + //------------------------------------------------------------------------------ + int8_t operator()( int8_t value ) const { return value; } -//------------------------------------------------------------------------------ - uint8_t - operator()( uint8_t value ) const - { - return value; - } + //------------------------------------------------------------------------------ + uint8_t operator()( uint8_t value ) const { return value; } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: -//------------------------------------------------------------------------------ - unsigned char - get_host_encoding() const + //------------------------------------------------------------------------------ + unsigned char get_host_encoding() const { static const int tmp = 1; if ( 1 == *(const char*)&tmp ) { @@ -182,20 +152,17 @@ class endianess_convertor { } } -//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ private: bool need_conversion; }; - //------------------------------------------------------------------------------ -inline -uint32_t -elf_hash( const unsigned char *name ) +inline uint32_t elf_hash( const unsigned char* name ) { uint32_t h = 0, g; while ( *name ) { - h = (h << 4) + *name++; + h = ( h << 4 ) + *name++; g = h & 0xf0000000; if ( g != 0 ) h ^= g >> 24; diff --git a/examples/anonymizer/anonymizer.cpp b/examples/anonymizer/anonymizer.cpp index fcec4b761ebfa5d4ef93397b051a9b231a8b95bb..c91f1a5b9bd18a2136c0a0967ef61636a0f29e01 100644 --- a/examples/anonymizer/anonymizer.cpp +++ b/examples/anonymizer/anonymizer.cpp @@ -49,36 +49,36 @@ diff before.txt after.txt using namespace ELFIO; -void overwrite_data(const std::string &filename, long offset, std::string &str) +void overwrite_data( const std::string& filename, + long offset, + std::string& str ) { - std::ofstream file(filename, std::ios::in | std::ios::out | std::ios::binary); - if (!file) + std::ofstream file( filename, + std::ios::in | std::ios::out | std::ios::binary ); + if ( !file ) throw "Error opening file" + filename; - std::string data(str.length(), '-'); - file.seekp(offset); - file.write(data.c_str(), data.length() + 1); + std::string data( str.length(), '-' ); + file.seekp( offset ); + file.write( data.c_str(), data.length() + 1 ); } -void process_string_table(const section *s, const std::string &filename) +void process_string_table( const section* s, const std::string& filename ) { std::cout << "Info: processing string table section" << std::endl; int index = 1; - while (index < s->get_size()) - { - auto str = std::string(s->get_data() + index); + while ( index < s->get_size() ) { + auto str = std::string( s->get_data() + index ); // For the example purpose, we rename main function name only - if (str == "main") - overwrite_data(filename, s->get_offset() + index, str); + if ( str == "main" ) + overwrite_data( filename, s->get_offset() + index, str ); index += str.length() + 1; } } -int main(int argc, char **argv) +int main( int argc, char** argv ) { - try - { - if (argc != 2) - { + try { + if ( argc != 2 ) { std::cout << "Usage: anonymizer <file_name>\n"; return 1; } @@ -87,28 +87,26 @@ int main(int argc, char **argv) elfio reader; - if (!reader.load(filename)) - { - std::cerr << "File " << filename << " is not found or it is not an ELF file\n"; + if ( !reader.load( filename ) ) { + std::cerr << "File " << filename + << " is not found or it is not an ELF file\n"; return 1; } - for (auto section = reader.sections.begin(); section != reader.sections.end(); ++section) - { - if ((*section)->get_type() == SHT_STRTAB && - std::string((*section)->get_name()) == std::string(".strtab")) - { - process_string_table(*section, filename); + for ( auto section = reader.sections.begin(); + section != reader.sections.end(); ++section ) { + if ( ( *section )->get_type() == SHT_STRTAB && + std::string( ( *section )->get_name() ) == + std::string( ".strtab" ) ) { + process_string_table( *section, filename ); } } return 0; } - catch (const std::string &s) - { + catch ( const std::string& s ) { std::cerr << s << std::endl; } - catch (const char *s) - { + catch ( const char* s ) { std::cerr << s << std::endl; } return 1; diff --git a/examples/elfdump/elfdump.cpp b/examples/elfdump/elfdump.cpp index f0e3242e7a82e095909de5872e09653045f49f1e..466adc9ebfed91b27385ba51e88373e1ba3fcc70 100644 --- a/examples/elfdump/elfdump.cpp +++ b/examples/elfdump/elfdump.cpp @@ -23,8 +23,8 @@ THE SOFTWARE. */ #ifdef _MSC_VER - #define _SCL_SECURE_NO_WARNINGS - #define ELFIO_NO_INTTYPES +#define _SCL_SECURE_NO_WARNINGS +#define ELFIO_NO_INTTYPES #endif #include <iostream> @@ -40,20 +40,20 @@ int main( int argc, char** argv ) } elfio reader; - + if ( !reader.load( argv[1] ) ) { printf( "File %s is not found or it is not an ELF file\n", argv[1] ); return 1; } - dump::header ( std::cout, reader ); + dump::header( std::cout, reader ); dump::section_headers( std::cout, reader ); dump::segment_headers( std::cout, reader ); - dump::symbol_tables ( std::cout, reader ); - dump::notes ( std::cout, reader ); - dump::dynamic_tags ( std::cout, reader ); - dump::section_datas ( std::cout, reader ); - dump::segment_datas ( std::cout, reader ); + dump::symbol_tables( std::cout, reader ); + dump::notes( std::cout, reader ); + dump::dynamic_tags( std::cout, reader ); + dump::section_datas( std::cout, reader ); + dump::segment_datas( std::cout, reader ); return 0; } diff --git a/examples/tutorial/tutorial.cpp b/examples/tutorial/tutorial.cpp index 1e62cde35f1fef0736542d83c7389398659fb7e8..02d259de54f53fa811983bef939777a9207371e9 100644 --- a/examples/tutorial/tutorial.cpp +++ b/examples/tutorial/tutorial.cpp @@ -9,10 +9,10 @@ int main( int argc, char** argv ) std::cout << "Usage: tutorial <elf_file>" << std::endl; return 1; } - + // Create an elfio reader elfio reader; - + // Load ELF data if ( !reader.load( argv[1] ) ) { std::cout << "Can't find or process ELF file " << argv[1] << std::endl; @@ -25,40 +25,32 @@ int main( int argc, char** argv ) std::cout << "ELF32" << std::endl; else std::cout << "ELF64" << std::endl; - + std::cout << "ELF file encoding : "; if ( reader.get_encoding() == ELFDATA2LSB ) std::cout << "Little endian" << std::endl; else std::cout << "Big endian" << std::endl; - + // Print ELF file sections info Elf_Half sec_num = reader.sections.size(); std::cout << "Number of sections: " << sec_num << std::endl; for ( int i = 0; i < sec_num; ++i ) { section* psec = reader.sections[i]; - std::cout << " [" << i << "] " - << psec->get_name() - << "\t" - << psec->get_size() - << std::endl; + std::cout << " [" << i << "] " << psec->get_name() << "\t" + << psec->get_size() << std::endl; // Access to section's data // const char* p = reader.sections[i]->get_data() } - + // Print ELF file segments info Elf_Half seg_num = reader.segments.size(); std::cout << "Number of segments: " << seg_num << std::endl; for ( int i = 0; i < seg_num; ++i ) { const segment* pseg = reader.segments[i]; - std::cout << " [" << i << "] 0x" << std::hex - << pseg->get_flags() - << "\t0x" - << pseg->get_virtual_address() - << "\t0x" - << pseg->get_file_size() - << "\t0x" - << pseg->get_memory_size() + std::cout << " [" << i << "] 0x" << std::hex << pseg->get_flags() + << "\t0x" << pseg->get_virtual_address() << "\t0x" + << pseg->get_file_size() << "\t0x" << pseg->get_memory_size() << std::endl; // Access to segments's data // const char* p = reader.segments[i]->get_data() @@ -77,14 +69,14 @@ int main( int argc, char** argv ) unsigned char type; Elf_Half section_index; unsigned char other; - + // Read symbol properties - symbols.get_symbol( j, name, value, size, bind, - type, section_index, other ); - std::cout << j << " " << name << " " << value << std::endl; + symbols.get_symbol( j, name, value, size, bind, type, + section_index, other ); + std::cout << j << " " << name << " " << value << std::endl; } } } - + return 0; } diff --git a/examples/write_obj/write_obj.cpp b/examples/write_obj/write_obj.cpp index 241c9fa9267fe019265b19f93989eb27eb1bf0f9..d4cce6c73f8c5ad29178a897b42e6591b0fe4ff7 100644 --- a/examples/write_obj/write_obj.cpp +++ b/examples/write_obj/write_obj.cpp @@ -25,32 +25,32 @@ int main( void ) writer.set_os_abi( ELFOSABI_LINUX ); writer.set_type( ET_REL ); - writer.set_machine(EM_X86_64); + writer.set_machine( EM_X86_64 ); // This is our code - char text[] = { '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4 - '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1 - '\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg - '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14 - '\xCD', '\x80', // int 0x80 - '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 - '\xCD', '\x80', // int 0x80 - '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 - '\x2C', '\x20', '\x57', '\x6F', '\x72', - '\x6C', '\x64', '\x21', '\x0A' - }; + char text[] = { + '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4 + '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1 + '\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg + '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14 + '\xCD', '\x80', // int 0x80 + '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 + '\xCD', '\x80', // int 0x80 + '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 + '\x2C', '\x20', '\x57', '\x6F', '\x72', + '\x6C', '\x64', '\x21', '\x0A' }; Elf64_Addr place_to_adjust = 11; // Create code section section* text_sec = writer.sections.add( ".text" ); - text_sec->set_type ( SHT_PROGBITS ); - text_sec->set_flags ( SHF_ALLOC | SHF_EXECINSTR ); + text_sec->set_type( SHT_PROGBITS ); + text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR ); text_sec->set_addr_align( 0x10 ); - text_sec->set_data ( text, sizeof( text ) ); + text_sec->set_data( text, sizeof( text ) ); // Create string table section section* str_sec = writer.sections.add( ".strtab" ); - str_sec->set_type ( SHT_STRTAB ); + str_sec->set_type( SHT_STRTAB ); // Create string table writer string_section_accessor stra( str_sec ); @@ -59,29 +59,28 @@ int main( void ) // Create symbol table section section* sym_sec = writer.sections.add( ".symtab" ); - sym_sec->set_type ( SHT_SYMTAB ); - sym_sec->set_info ( 1 ); + sym_sec->set_type( SHT_SYMTAB ); + sym_sec->set_info( 1 ); sym_sec->set_addr_align( 0x4 ); sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) ); - sym_sec->set_link ( str_sec->get_index() ); + sym_sec->set_link( str_sec->get_index() ); // Create symbol table writer symbol_section_accessor syma( writer, sym_sec ); // Add symbol entry (msg has offset == 29) - Elf_Word sym_to_adjust = syma.add_symbol( str_index, 29, 0, STB_GLOBAL, - STT_OBJECT, 0, - text_sec->get_index() ); + Elf_Word sym_to_adjust = syma.add_symbol( + str_index, 29, 0, STB_GLOBAL, STT_OBJECT, 0, text_sec->get_index() ); // Another way to add symbol syma.add_symbol( stra, "_start", 0x00000000, 0, STB_WEAK, STT_FUNC, 0, text_sec->get_index() ); // Create relocation table section section* rel_sec = writer.sections.add( ".rel.text" ); - rel_sec->set_type ( SHT_REL ); - rel_sec->set_info ( text_sec->get_index() ); + rel_sec->set_type( SHT_REL ); + rel_sec->set_info( text_sec->get_index() ); rel_sec->set_addr_align( 0x4 ); rel_sec->set_entry_size( writer.get_default_entry_size( SHT_REL ) ); - rel_sec->set_link ( sym_sec->get_index() ); + rel_sec->set_link( sym_sec->get_index() ); // Create relocation table writer relocation_section_accessor rela( writer, rel_sec ); @@ -102,7 +101,7 @@ int main( void ) note_section_accessor note_writer( writer, note_sec ); note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 ); - char descr[6] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36}; + char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 }; note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) ); // Create ELF object file diff --git a/examples/writer/writer.cpp b/examples/writer/writer.cpp index 08e4d170b515257d62f02df7ec3708d6fedde16f..e9edf79c51ca90163a68a5594b4fc50435e134f9 100644 --- a/examples/writer/writer.cpp +++ b/examples/writer/writer.cpp @@ -5,7 +5,7 @@ using namespace ELFIO; int main( void ) { elfio writer; - + // You can't proceed without this function call! writer.create( ELFCLASS64, ELFDATA2LSB ); @@ -18,16 +18,17 @@ int main( void ) text_sec->set_type( SHT_PROGBITS ); text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR ); text_sec->set_addr_align( 0x10 ); - + // Add data into it - char text[] = { '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4 - '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1 - '\xB9', '\x20', '\x80', '\x04', '\x08', // mov ecx, msg - '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14 - '\xCD', '\x80', // int 0x80 - '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 - '\xCD', '\x80' // int 0x80 - }; + char text[] = { + '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4 + '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1 + '\xB9', '\x20', '\x80', '\x04', '\x08', // mov ecx, msg + '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14 + '\xCD', '\x80', // int 0x80 + '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 + '\xCD', '\x80' // int 0x80 + }; text_sec->set_data( text, sizeof( text ) ); // Create a loadable segment @@ -37,9 +38,10 @@ int main( void ) text_seg->set_physical_address( 0x08048000 ); text_seg->set_flags( PF_X | PF_R ); text_seg->set_align( 0x1000 ); - + // Add code section into program segment - text_seg->add_section_index( text_sec->get_index(), text_sec->get_addr_align() ); + text_seg->add_section_index( text_sec->get_index(), + text_sec->get_addr_align() ); // Create data section section* data_sec = writer.sections.add( ".data" ); @@ -47,10 +49,10 @@ int main( void ) data_sec->set_flags( SHF_ALLOC | SHF_WRITE ); data_sec->set_addr_align( 0x4 ); - char data[] = { '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 - '\x2C', '\x20', '\x57', '\x6F', '\x72', - '\x6C', '\x64', '\x21', '\x0A' - }; + char data[] = { + '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 + '\x2C', '\x20', '\x57', '\x6F', '\x72', + '\x6C', '\x64', '\x21', '\x0A' }; data_sec->set_data( data, sizeof( data ) ); // Create a read/write segment @@ -62,7 +64,8 @@ int main( void ) data_seg->set_align( 0x10 ); // Add code section into program segment - data_seg->add_section_index( data_sec->get_index(), data_sec->get_addr_align() ); + data_seg->add_section_index( data_sec->get_index(), + data_sec->get_addr_align() ); // Add optional signature for the file producer section* note_sec = writer.sections.add( ".note" ); @@ -70,14 +73,14 @@ int main( void ) note_sec->set_addr_align( 1 ); note_section_accessor note_writer( writer, note_sec ); note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 ); - char descr[6] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36}; + char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 }; note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) ); // Setup entry point writer.set_entry( 0x08048000 ); // Create ELF file - writer.save("hello_x86_64"); + writer.save( "hello_x86_64" ); return 0; } diff --git a/tests/ELFIOTest.cpp b/tests/ELFIOTest.cpp index dc5286e0e96feb1cb548f16478307e1a0edd77e4..f37b9dbf614cb3f2cca7531438e3f7d90fabe97b 100644 --- a/tests/ELFIOTest.cpp +++ b/tests/ELFIOTest.cpp @@ -10,109 +10,104 @@ using namespace ELFIO; //////////////////////////////////////////////////////////////////////////////// -void -checkHeader( elfio& reader, - unsigned char nClass, - unsigned char encoding, - unsigned char elfVersion, - Elf_Half type, - Elf_Half machine, - Elf_Word version, - Elf64_Addr entry, - Elf_Word flags, - Elf_Half secNum, - Elf_Half segNum, - unsigned char OSABI, - unsigned char ABIVersion ) +void checkHeader( elfio& reader, + unsigned char nClass, + unsigned char encoding, + unsigned char elfVersion, + Elf_Half type, + Elf_Half machine, + Elf_Word version, + Elf64_Addr entry, + Elf_Word flags, + Elf_Half secNum, + Elf_Half segNum, + unsigned char OSABI, + unsigned char ABIVersion ) { - BOOST_CHECK_EQUAL( reader.get_class(), nClass ); - BOOST_CHECK_EQUAL( reader.get_encoding(), encoding ); + BOOST_CHECK_EQUAL( reader.get_class(), nClass ); + BOOST_CHECK_EQUAL( reader.get_encoding(), encoding ); BOOST_CHECK_EQUAL( reader.get_elf_version(), elfVersion ); - BOOST_CHECK_EQUAL( reader.get_os_abi(), OSABI ); + BOOST_CHECK_EQUAL( reader.get_os_abi(), OSABI ); BOOST_CHECK_EQUAL( reader.get_abi_version(), ABIVersion ); - BOOST_CHECK_EQUAL( reader.get_type(), type ); - BOOST_CHECK_EQUAL( reader.get_machine(), machine ); - BOOST_CHECK_EQUAL( reader.get_version(), version ); - BOOST_CHECK_EQUAL( reader.get_entry(), entry ); - BOOST_CHECK_EQUAL( reader.get_flags(), flags ); - BOOST_CHECK_EQUAL( reader.sections.size(), secNum ); - BOOST_CHECK_EQUAL( reader.segments.size(), segNum ); + BOOST_CHECK_EQUAL( reader.get_type(), type ); + BOOST_CHECK_EQUAL( reader.get_machine(), machine ); + BOOST_CHECK_EQUAL( reader.get_version(), version ); + BOOST_CHECK_EQUAL( reader.get_entry(), entry ); + BOOST_CHECK_EQUAL( reader.get_flags(), flags ); + BOOST_CHECK_EQUAL( reader.sections.size(), secNum ); + BOOST_CHECK_EQUAL( reader.segments.size(), segNum ); } - //////////////////////////////////////////////////////////////////////////////// -void -checkSection( const section* sec, - Elf_Half index, - std::string name, - Elf_Word type, - Elf_Xword flags, - Elf64_Addr address, - Elf_Xword size, - Elf_Word link, - Elf_Word info, - Elf_Xword addrAlign, - Elf_Xword entrySize ) +void checkSection( const section* sec, + Elf_Half index, + std::string name, + Elf_Word type, + Elf_Xword flags, + Elf64_Addr address, + Elf_Xword size, + Elf_Word link, + Elf_Word info, + Elf_Xword addrAlign, + Elf_Xword entrySize ) { - BOOST_CHECK_EQUAL( sec->get_index(), index ); - BOOST_CHECK_EQUAL( sec->get_name(), name ); - BOOST_CHECK_EQUAL( sec->get_type(), type ); - BOOST_CHECK_EQUAL( sec->get_flags(), flags ); - BOOST_CHECK_EQUAL( sec->get_address(), address ); - BOOST_CHECK_EQUAL( sec->get_size(), size ); - BOOST_CHECK_EQUAL( sec->get_link(), link ); - BOOST_CHECK_EQUAL( sec->get_info(), info ); + BOOST_CHECK_EQUAL( sec->get_index(), index ); + BOOST_CHECK_EQUAL( sec->get_name(), name ); + BOOST_CHECK_EQUAL( sec->get_type(), type ); + BOOST_CHECK_EQUAL( sec->get_flags(), flags ); + BOOST_CHECK_EQUAL( sec->get_address(), address ); + BOOST_CHECK_EQUAL( sec->get_size(), size ); + BOOST_CHECK_EQUAL( sec->get_link(), link ); + BOOST_CHECK_EQUAL( sec->get_info(), info ); BOOST_CHECK_EQUAL( sec->get_addr_align(), addrAlign ); BOOST_CHECK_EQUAL( sec->get_entry_size(), entrySize ); } - //////////////////////////////////////////////////////////////////////////////// -void -checkSection( const section* sec, - const std::string &name, - Elf_Word type, - Elf_Xword flags, - Elf64_Addr address, - Elf_Xword size, - Elf_Word link, - Elf_Word info, - Elf_Xword addrAlign, - Elf_Xword entrySize ) +void checkSection( const section* sec, + const std::string& name, + Elf_Word type, + Elf_Xword flags, + Elf64_Addr address, + Elf_Xword size, + Elf_Word link, + Elf_Word info, + Elf_Xword addrAlign, + Elf_Xword entrySize ) { checkSection( sec, sec->get_index(), name, type, flags, address, size, link, info, addrAlign, entrySize ); } - //////////////////////////////////////////////////////////////////////////////// -void -checkSegment( const segment* seg, - Elf_Word type, - Elf64_Addr vaddr, - Elf64_Addr paddr, - Elf_Xword fsize, - Elf_Xword msize, - Elf_Word flags, - Elf_Xword align ) +void checkSegment( const segment* seg, + Elf_Word type, + Elf64_Addr vaddr, + Elf64_Addr paddr, + Elf_Xword fsize, + Elf_Xword msize, + Elf_Word flags, + Elf_Xword align ) { - BOOST_CHECK_EQUAL( seg->get_type(), type ); - BOOST_CHECK_EQUAL( seg->get_virtual_address(), vaddr ); + BOOST_CHECK_EQUAL( seg->get_type(), type ); + BOOST_CHECK_EQUAL( seg->get_virtual_address(), vaddr ); BOOST_CHECK_EQUAL( seg->get_physical_address(), paddr ); - BOOST_CHECK_EQUAL( seg->get_file_size(), fsize ); - BOOST_CHECK_EQUAL( seg->get_memory_size(), msize ); - BOOST_CHECK_EQUAL( seg->get_flags(), flags ); - BOOST_CHECK_EQUAL( seg->get_align(), align ); + BOOST_CHECK_EQUAL( seg->get_file_size(), fsize ); + BOOST_CHECK_EQUAL( seg->get_memory_size(), msize ); + BOOST_CHECK_EQUAL( seg->get_flags(), flags ); + BOOST_CHECK_EQUAL( seg->get_align(), align ); } - //////////////////////////////////////////////////////////////////////////////// -void -checkSymbol( const symbol_section_accessor& sr, Elf_Xword index, - std::string name_, Elf64_Addr value_, - Elf_Xword size_, - unsigned char bind_, unsigned char type_, - Elf_Half section_, unsigned char other_ ) +void checkSymbol( const symbol_section_accessor& sr, + Elf_Xword index, + std::string name_, + Elf64_Addr value_, + Elf_Xword size_, + unsigned char bind_, + unsigned char type_, + Elf_Half section_, + unsigned char other_ ) { std::string name; Elf64_Addr value; @@ -122,25 +117,27 @@ checkSymbol( const symbol_section_accessor& sr, Elf_Xword index, Elf_Half section; unsigned char other; - BOOST_REQUIRE_EQUAL( sr.get_symbol( index, name, value, size, bind, type, section, other ), - true ); - BOOST_CHECK_EQUAL( name, name_ ); - BOOST_CHECK_EQUAL( value, value_ ); - BOOST_CHECK_EQUAL( size, size_ ); - BOOST_CHECK_EQUAL( bind, bind_ ); - BOOST_CHECK_EQUAL( type, type_ ); + BOOST_REQUIRE_EQUAL( + sr.get_symbol( index, name, value, size, bind, type, section, other ), + true ); + BOOST_CHECK_EQUAL( name, name_ ); + BOOST_CHECK_EQUAL( value, value_ ); + BOOST_CHECK_EQUAL( size, size_ ); + BOOST_CHECK_EQUAL( bind, bind_ ); + BOOST_CHECK_EQUAL( type, type_ ); BOOST_CHECK_EQUAL( section, section_ ); - BOOST_CHECK_EQUAL( other, other_ ); + BOOST_CHECK_EQUAL( other, other_ ); } - //////////////////////////////////////////////////////////////////////////////// -void -checkRelocation( const relocation_section_accessor* pRT, Elf_Xword index, - Elf64_Addr offset_, Elf64_Addr symbolValue_, - std::string symbolName_, - unsigned char type_, Elf_Sxword addend_, - Elf_Sxword calcValue_ ) +void checkRelocation( const relocation_section_accessor* pRT, + Elf_Xword index, + Elf64_Addr offset_, + Elf64_Addr symbolValue_, + std::string symbolName_, + unsigned char type_, + Elf_Sxword addend_, + Elf_Sxword calcValue_ ) { Elf64_Addr offset; Elf64_Addr symbolValue; @@ -150,22 +147,22 @@ checkRelocation( const relocation_section_accessor* pRT, Elf_Xword index, Elf_Sxword calcValue; BOOST_REQUIRE_EQUAL( pRT->get_entry( index, offset, symbolValue, symbolName, - type, addend, calcValue ), + type, addend, calcValue ), true ); - BOOST_CHECK_EQUAL( offset, offset_ ); + BOOST_CHECK_EQUAL( offset, offset_ ); BOOST_CHECK_EQUAL( symbolValue, symbolValue_ ); - BOOST_CHECK_EQUAL( symbolName, symbolName_ ); - BOOST_CHECK_EQUAL( type, type_ ); - BOOST_CHECK_EQUAL( addend, addend_ ); - BOOST_CHECK_EQUAL( calcValue, calcValue_ ); + BOOST_CHECK_EQUAL( symbolName, symbolName_ ); + BOOST_CHECK_EQUAL( type, type_ ); + BOOST_CHECK_EQUAL( addend, addend_ ); + BOOST_CHECK_EQUAL( calcValue, calcValue_ ); } - //////////////////////////////////////////////////////////////////////////////// -void - checkNote( const note_section_accessor& notes, Elf_Word index, - Elf_Word type_, std::string name_, - Elf_Word descSize_ ) +void checkNote( const note_section_accessor& notes, + Elf_Word index, + Elf_Word type_, + std::string name_, + Elf_Word descSize_ ) { Elf_Word type; std::string name; @@ -174,19 +171,18 @@ void BOOST_REQUIRE_EQUAL( notes.get_note( index, type, name, desc, descSize ), true ); - BOOST_CHECK_EQUAL( type, type_ ); - BOOST_CHECK_EQUAL( name, name_ ); + BOOST_CHECK_EQUAL( type, type_ ); + BOOST_CHECK_EQUAL( name, name_ ); BOOST_CHECK_EQUAL( descSize, descSize_ ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( load32 ) { elfio reader; BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/hello_32" ), true ); - checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_EXEC, - EM_386, 1, 0x80482b0, 0, 28, 7, 0, 0 ); + checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_EXEC, EM_386, + 1, 0x80482b0, 0, 28, 7, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections @@ -194,54 +190,51 @@ BOOST_AUTO_TEST_CASE( load32 ) checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); sec = reader.sections[1]; - checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, - 0x08048114, 0x13, 0, 0, 1, 0 ); + checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 0x08048114, 0x13, + 0, 0, 1, 0 ); sec = reader.sections[9]; - checkSection( sec, 9, ".rel.plt", SHT_REL, SHF_ALLOC, - 0x08048234, 0x18, 4, 11, 4, 8 ); + checkSection( sec, 9, ".rel.plt", SHT_REL, SHF_ALLOC, 0x08048234, 0x18, 4, + 11, 4, 8 ); sec = reader.sections[19]; checkSection( sec, 19, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC, - 0x080494a0, 0xc8, 5, 0, 4, 8 ); + 0x080494a0, 0xc8, 5, 0, 4, 8 ); sec = reader.sections[27]; - checkSection( sec, 27, ".strtab", SHT_STRTAB, 0, - 0x0, 0x259, 0, 0, 1, 0 ); + checkSection( sec, 27, ".strtab", SHT_STRTAB, 0, 0x0, 0x259, 0, 0, 1, 0 ); - const section* sec1 = reader.sections[ ".strtab" ]; + const section* sec1 = reader.sections[".strtab"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); //////////////////////////////////////////////////////////////////////////// // Check segments segment* seg = reader.segments[0]; - checkSegment( seg, PT_PHDR, 0x08048034, 0x08048034, - 0x000e0, 0x000e0, PF_R + PF_X, 4 ); + checkSegment( seg, PT_PHDR, 0x08048034, 0x08048034, 0x000e0, 0x000e0, + PF_R + PF_X, 4 ); seg = reader.segments[4]; - checkSegment( seg, PT_DYNAMIC, 0x080494a0, 0x080494a0, - 0x000c8, 0x000c8, PF_R + PF_W, 4 ); + checkSegment( seg, PT_DYNAMIC, 0x080494a0, 0x080494a0, 0x000c8, 0x000c8, + PF_R + PF_W, 4 ); seg = reader.segments[6]; - checkSegment( seg, 0x6474E551, 0x0, 0x0, - 0x0, 0x0, PF_R + PF_W, 4 ); - + checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 4 ); + //////////////////////////////////////////////////////////////////////////// // Check symbol table - sec = reader.sections[ ".symtab" ]; - + sec = reader.sections[".symtab"]; symbol_section_accessor sr( reader, sec ); BOOST_CHECK_EQUAL( sr.get_symbols_num(), 68 ); - checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, + checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 1, "", 0x08048114, 0, STB_LOCAL, STT_SECTION, 1, + checkSymbol( sr, 1, "", 0x08048114, 0, STB_LOCAL, STT_SECTION, 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); checkSymbol( sr, 39, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 65, "__i686.get_pc_thunk.bx", 0x08048429, 0, STB_GLOBAL, STT_FUNC, 12, - ELF_ST_VISIBILITY( STV_HIDDEN ) ); + checkSymbol( sr, 65, "__i686.get_pc_thunk.bx", 0x08048429, 0, STB_GLOBAL, + STT_FUNC, 12, ELF_ST_VISIBILITY( STV_HIDDEN ) ); checkSymbol( sr, 66, "main", 0x08048384, 43, STB_GLOBAL, STT_FUNC, 12, ELF_ST_VISIBILITY( STV_DEFAULT ) ); checkSymbol( sr, 67, "_init", 0x0804824c, 0, STB_GLOBAL, STT_FUNC, 10, @@ -249,28 +242,29 @@ BOOST_AUTO_TEST_CASE( load32 ) //////////////////////////////////////////////////////////////////////////// // Check relocation table - sec = reader.sections[ ".rel.dyn" ]; - + sec = reader.sections[".rel.dyn"]; relocation_section_accessor reloc( reader, sec ); BOOST_CHECK_EQUAL( reloc.get_entries_num(), 1 ); - checkRelocation( &reloc, 0, 0x08049568, 0x0, "__gmon_start__", R_386_GLOB_DAT, 0, 0 ); + checkRelocation( &reloc, 0, 0x08049568, 0x0, "__gmon_start__", + R_386_GLOB_DAT, 0, 0 ); - sec = reader.sections[ ".rel.plt" ]; - + sec = reader.sections[".rel.plt"]; relocation_section_accessor reloc1( reader, sec ); BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 3 ); - checkRelocation( &reloc1, 0, 0x08049578, 0x0, "__gmon_start__", R_X86_64_JUMP_SLOT, 0, 0 ); - checkRelocation( &reloc1, 1, 0x0804957c, 0x0, "__libc_start_main", R_X86_64_JUMP_SLOT, 0, 0 ); - checkRelocation( &reloc1, 2, 0x08049580, 0x0, "puts", R_X86_64_JUMP_SLOT, 0, 0 ); - + checkRelocation( &reloc1, 0, 0x08049578, 0x0, "__gmon_start__", + R_X86_64_JUMP_SLOT, 0, 0 ); + checkRelocation( &reloc1, 1, 0x0804957c, 0x0, "__libc_start_main", + R_X86_64_JUMP_SLOT, 0, 0 ); + checkRelocation( &reloc1, 2, 0x08049580, 0x0, "puts", R_X86_64_JUMP_SLOT, 0, + 0 ); + //////////////////////////////////////////////////////////////////////////// // Check note reader - sec = reader.sections[ ".note.ABI-tag" ]; - + sec = reader.sections[".note.ABI-tag"]; note_section_accessor notes( reader, sec ); BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u ); @@ -278,7 +272,6 @@ BOOST_AUTO_TEST_CASE( load32 ) checkNote( notes, 0, 1, std::string( "GNU" ), 16 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( load64 ) { @@ -293,31 +286,30 @@ BOOST_AUTO_TEST_CASE( load64 ) //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ 0 ]; - + section* sec = reader.sections[0]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ 1 ]; - + sec = reader.sections[1]; + checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 0x0000000000400200, 0x1c, 0, 0, 1, 0 ); - sec =reader.sections[ 9 ]; - - checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, - 0x0000000000400340, 0x30, 4, 11, 8, 0x18 ); + sec = reader.sections[9]; + + checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x0000000000400340, + 0x30, 4, 11, 8, 0x18 ); + + sec = reader.sections[20]; - sec =reader.sections[ 20 ]; - checkSection( sec, 20, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC, 0x0000000000600698, 0x190, 5, 0, 8, 0x10 ); - sec =reader.sections[ 28 ]; - - checkSection( sec, 28, ".strtab", SHT_STRTAB, 0, - 0x0, 0x23f, 0, 0, 1, 0 ); + sec = reader.sections[28]; + + checkSection( sec, 28, ".strtab", SHT_STRTAB, 0, 0x0, 0x23f, 0, 0, 1, 0 ); - const section* sec1 = reader.sections[ ".strtab" ]; + const section* sec1 = reader.sections[".strtab"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); //////////////////////////////////////////////////////////////////////////// @@ -328,30 +320,29 @@ BOOST_AUTO_TEST_CASE( load64 ) seg = reader.segments[2]; checkSegment( seg, PT_LOAD, 0x0000000000400000, 0x0000000000400000, - 0x000000000000066c, 0x000000000000066c, PF_R + PF_X, 0x200000 ); + 0x000000000000066c, 0x000000000000066c, PF_R + PF_X, + 0x200000 ); seg = reader.segments[7]; - checkSegment( seg, 0x6474E551, 0x0, 0x0, - 0x0, 0x0, PF_R + PF_W, 8 ); + checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 8 ); //////////////////////////////////////////////////////////////////////////// // Check symbol table - sec =reader.sections[ ".symtab" ]; - + sec = reader.sections[".symtab"]; symbol_section_accessor sr( reader, sec ); BOOST_CHECK_EQUAL( sr.get_symbols_num(), 67 ); - checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, + checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 1, "", 0x00400200, 0, STB_LOCAL, STT_SECTION, 1, + checkSymbol( sr, 1, "", 0x00400200, 0, STB_LOCAL, STT_SECTION, 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); checkSymbol( sr, 40, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 52, "__gmon_start__", 0x00000000, 0, STB_WEAK, STT_NOTYPE, STN_UNDEF, - ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 64, "_edata", 0x0060085c, 0, STB_GLOBAL, STT_NOTYPE, SHN_ABS, - ELF_ST_VISIBILITY( STV_DEFAULT ) ); + checkSymbol( sr, 52, "__gmon_start__", 0x00000000, 0, STB_WEAK, STT_NOTYPE, + STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) ); + checkSymbol( sr, 64, "_edata", 0x0060085c, 0, STB_GLOBAL, STT_NOTYPE, + SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) ); checkSymbol( sr, 65, "main", 0x00400498, 21, STB_GLOBAL, STT_FUNC, 12, ELF_ST_VISIBILITY( STV_DEFAULT ) ); checkSymbol( sr, 66, "_init", 0x00400370, 0, STB_GLOBAL, STT_FUNC, 10, @@ -359,27 +350,27 @@ BOOST_AUTO_TEST_CASE( load64 ) //////////////////////////////////////////////////////////////////////////// // Check relocation table - sec =reader.sections[ ".rela.dyn" ]; - + sec = reader.sections[".rela.dyn"]; relocation_section_accessor reloc( reader, sec ); BOOST_CHECK_EQUAL( reloc.get_entries_num(), 1 ); - checkRelocation( &reloc, 0, 0x00600828, 0x0, "__gmon_start__", R_X86_64_GLOB_DAT, 0, 0 ); + checkRelocation( &reloc, 0, 0x00600828, 0x0, "__gmon_start__", + R_X86_64_GLOB_DAT, 0, 0 ); - sec =reader.sections[ ".rela.plt" ]; - + sec = reader.sections[".rela.plt"]; relocation_section_accessor reloc1( reader, sec ); BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 2 ); - checkRelocation( &reloc1, 0, 0x00600848, 0x0, "puts", R_X86_64_JUMP_SLOT, 0, 0 ); - checkRelocation( &reloc1, 1, 0x00600850, 0x0, "__libc_start_main", R_X86_64_JUMP_SLOT, 0, 0 ); + checkRelocation( &reloc1, 0, 0x00600848, 0x0, "puts", R_X86_64_JUMP_SLOT, 0, + 0 ); + checkRelocation( &reloc1, 1, 0x00600850, 0x0, "__libc_start_main", + R_X86_64_JUMP_SLOT, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check note reader - sec =reader.sections[ ".note.ABI-tag" ]; - + sec = reader.sections[".note.ABI-tag"]; note_section_accessor notes( reader, sec ); BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u ); @@ -387,7 +378,6 @@ BOOST_AUTO_TEST_CASE( load64 ) checkNote( notes, 0, 1, std::string( "GNU" ), 16 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( hello_64_o ) { @@ -397,54 +387,51 @@ BOOST_AUTO_TEST_CASE( hello_64_o ) //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, - EM_X86_64, 1, 0, 0, 13, 0, 0, 0 ); + checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64, + 1, 0, 0, 13, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ 0 ]; - + section* sec = reader.sections[0]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ 1 ]; - - checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, - 0x0, 0x15, 0, 0, 4, 0 ); + sec = reader.sections[1]; + + checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, + 0x15, 0, 0, 4, 0 ); - section* sec1 = reader.sections[ ".text" ]; + section* sec1 = reader.sections[".text"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); sec = reader.sections[12]; - checkSection( sec, 12, ".strtab", SHT_STRTAB, 0, - 0x0, 0x13, 0, 0, 1, 0 ); + checkSection( sec, 12, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 ); - sec1 = reader.sections[ ".strtab" ]; + sec1 = reader.sections[".strtab"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); //////////////////////////////////////////////////////////////////////////// // Check symbol table - sec =reader.sections[ ".symtab" ]; - + sec = reader.sections[".symtab"]; symbol_section_accessor sr( reader, sec ); BOOST_CHECK_EQUAL( sr.get_symbols_num(), 11 ); - checkSymbol( sr, 9, "main", 0x00000000, 21, STB_GLOBAL, STT_FUNC, 1, + checkSymbol( sr, 9, "main", 0x00000000, 21, STB_GLOBAL, STT_FUNC, 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); //////////////////////////////////////////////////////////////////////////// // Check relocation table - sec =reader.sections[ ".rela.text" ]; - + sec = reader.sections[".rela.text"]; relocation_section_accessor reloc( reader, sec ); BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 ); checkRelocation( &reloc, 0, 0x00000005, 0x0, "", R_X86_64_32, 0, 0 ); - checkRelocation( &reloc, 1, 0x0000000A, 0x0, "puts", R_X86_64_PC32, 0xfffffffffffffffcULL, -14 ); + checkRelocation( &reloc, 1, 0x0000000A, 0x0, "puts", R_X86_64_PC32, + 0xfffffffffffffffcULL, -14 ); - sec =reader.sections[ ".rela.eh_frame" ]; - + sec = reader.sections[".rela.eh_frame"]; relocation_section_accessor reloc1( reader, sec ); BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 1 ); @@ -452,7 +439,6 @@ BOOST_AUTO_TEST_CASE( hello_64_o ) checkRelocation( &reloc1, 0, 0x00000020, 0x0, "", R_X86_64_32, 0, 0 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( hello_32_o ) { @@ -462,47 +448,43 @@ BOOST_AUTO_TEST_CASE( hello_32_o ) //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, - EM_386, 1, 0, 0, 11, 0, 0, 0 ); + checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386, 1, + 0, 0, 11, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ 0 ]; - + section* sec = reader.sections[0]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ 1 ]; - - checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, - 0x0, 0x2b, 0, 0, 4, 0 ); + sec = reader.sections[1]; + + checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, + 0x2b, 0, 0, 4, 0 ); - section* sec1 = reader.sections[ ".text" ]; + section* sec1 = reader.sections[".text"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); + sec = reader.sections[10]; - sec = reader.sections[ 10 ]; - - checkSection( sec, 10, ".strtab", SHT_STRTAB, 0, - 0x0, 0x13, 0, 0, 1, 0 ); + checkSection( sec, 10, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 ); - sec1 = reader.sections[ ".strtab" ]; + sec1 = reader.sections[".strtab"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); //////////////////////////////////////////////////////////////////////////// // Check symbol table - sec =reader.sections[ ".symtab" ]; - + sec = reader.sections[".symtab"]; symbol_section_accessor sr( reader, sec ); BOOST_CHECK_EQUAL( sr.get_symbols_num(), 10 ); - checkSymbol( sr, 8, "main", 0x00000000, 43, STB_GLOBAL, STT_FUNC, 1, + checkSymbol( sr, 8, "main", 0x00000000, 43, STB_GLOBAL, STT_FUNC, 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); //////////////////////////////////////////////////////////////////////////// // Check relocation table - sec =reader.sections[ ".rel.text" ]; - + sec = reader.sections[".rel.text"]; relocation_section_accessor reloc( reader, sec ); BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 ); @@ -511,7 +493,6 @@ BOOST_AUTO_TEST_CASE( hello_32_o ) checkRelocation( &reloc, 1, 0x00000019, 0x0, "puts", R_386_PC32, 0x0, -25 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_ppc_o ) { @@ -521,74 +502,68 @@ BOOST_AUTO_TEST_CASE( test_ppc_o ) //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, - EM_PPC, 1, 0, 0, 16, 0, 0, 0 ); + checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC, 1, + 0, 0, 16, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ 0 ]; - + section* sec = reader.sections[0]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ 1 ]; - - checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, - 0x0, 0x118, 0, 0, 4, 0 ); + sec = reader.sections[1]; + + checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, + 0x118, 0, 0, 4, 0 ); - section* sec1 = reader.sections[ ".text" ]; + section* sec1 = reader.sections[".text"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); - sec =reader.sections[ 15 ]; - - checkSection( sec, 15, ".strtab", SHT_STRTAB, 0, - 0x0, 0x14f, 0, 0, 1, 0 ); + sec = reader.sections[15]; + + checkSection( sec, 15, ".strtab", SHT_STRTAB, 0, 0x0, 0x14f, 0, 0, 1, 0 ); - sec1 = reader.sections[ ".strtab" ]; + sec1 = reader.sections[".strtab"]; BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); //////////////////////////////////////////////////////////////////////////// // Check symbol table - sec =reader.sections[ ".symtab" ]; - + sec = reader.sections[".symtab"]; symbol_section_accessor sr( reader, sec ); BOOST_CHECK_EQUAL( sr.get_symbols_num(), 24 ); - checkSymbol( sr, 14, "main", 0x00000000, 92, STB_GLOBAL, STT_FUNC, 1, - ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 8, "_GLOBAL__I_main", 0x000000DC, 60, STB_LOCAL, STT_FUNC, 1, + checkSymbol( sr, 14, "main", 0x00000000, 92, STB_GLOBAL, STT_FUNC, 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); + checkSymbol( sr, 8, "_GLOBAL__I_main", 0x000000DC, 60, STB_LOCAL, STT_FUNC, + 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); //////////////////////////////////////////////////////////////////////////// // Check relocation table - sec =reader.sections[ ".rela.text" ]; - + sec = reader.sections[".rela.text"]; relocation_section_accessor reloc( reader, sec ); BOOST_CHECK_EQUAL( reloc.get_entries_num(), 18 ); - checkRelocation( &reloc, 0, 0x00000016, 0x0, "_ZSt4cout", 6, 0, 0 ); - checkRelocation( &reloc, 1, 0x0000001a, 0x0, "_ZSt4cout", 4, 0x0, 0 ); + checkRelocation( &reloc, 0, 0x00000016, 0x0, "_ZSt4cout", 6, 0, 0 ); + checkRelocation( &reloc, 1, 0x0000001a, 0x0, "_ZSt4cout", 4, 0x0, 0 ); checkRelocation( &reloc, 17, 0x000000c0, 0x0, "__cxa_atexit", 10, 0x0, 0 ); - sec =reader.sections[ ".rela.ctors" ]; - + sec = reader.sections[".rela.ctors"]; relocation_section_accessor reloc1( reader, sec ); BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 1 ); - checkRelocation( &reloc1, 0, 0x00000000, 0x0, "", 1, 0xDC, 0xDC ); + checkRelocation( &reloc1, 0, 0x00000000, 0x0, "", 1, 0xDC, 0xDC ); - sec =reader.sections[ ".rela.eh_frame" ]; - + sec = reader.sections[".rela.eh_frame"]; relocation_section_accessor reloc2( reader, sec ); BOOST_CHECK_EQUAL( reloc2.get_entries_num(), 3 ); - checkRelocation( &reloc2, 1, 0x00000020, 0x0, "", 1, 0x0, 0x0 ); + checkRelocation( &reloc2, 1, 0x00000020, 0x0, "", 1, 0x0, 0x0 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_ppc ) { @@ -598,70 +573,67 @@ BOOST_AUTO_TEST_CASE( test_ppc ) //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_EXEC, - EM_PPC, 1, 0x10000550, 0, 31, 8, 0, 0 ); + checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_EXEC, EM_PPC, + 1, 0x10000550, 0, 31, 8, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ 0 ]; - + section* sec = reader.sections[0]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ 1 ]; - + sec = reader.sections[1]; + checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, - 0x0000000010000134, 0xd, 0, 0, 1, 0 ); + 0x0000000010000134, 0xd, 0, 0, 1, 0 ); + + sec = reader.sections[9]; + + checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x00000000010000494, + 0x6c, 4, 22, 4, 0xc ); - sec =reader.sections[ 9 ]; - - checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, - 0x00000000010000494, 0x6c, 4, 22, 4, 0xc ); + sec = reader.sections[20]; - sec =reader.sections[ 20 ]; - checkSection( sec, 20, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC, - 0x0000000010010aec, 0xe8, 5, 0, 4, 0x8 ); + 0x0000000010010aec, 0xe8, 5, 0, 4, 0x8 ); - sec =reader.sections[ 28 ]; - - checkSection( sec, 28, ".shstrtab", SHT_STRTAB, 0, - 0x0, 0x101, 0, 0, 1, 0 ); + sec = reader.sections[28]; - const section* sec1 = reader.sections[ ".shstrtab" ]; - BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); + checkSection( sec, 28, ".shstrtab", SHT_STRTAB, 0, 0x0, 0x101, 0, 0, 1, 0 ); + const section* sec1 = reader.sections[".shstrtab"]; + BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() ); //////////////////////////////////////////////////////////////////////////// // Check segments segment* seg = reader.segments[0]; - checkSegment( seg, PT_PHDR, 0x10000034, 0x10000034, - 0x00100, 0x00100, PF_R + PF_X, 4 ); + checkSegment( seg, PT_PHDR, 0x10000034, 0x10000034, 0x00100, 0x00100, + PF_R + PF_X, 4 ); seg = reader.segments[2]; - checkSegment( seg, PT_LOAD, 0x10000000, 0x10000000, - 0x00acc, 0x00acc, PF_R + PF_X, 0x10000 ); + checkSegment( seg, PT_LOAD, 0x10000000, 0x10000000, 0x00acc, 0x00acc, + PF_R + PF_X, 0x10000 ); seg = reader.segments[7]; - checkSegment( seg, 0x6474E551, 0x0, 0x0, - 0x0, 0x0, PF_R + PF_W, 0x4 ); + checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 0x4 ); //////////////////////////////////////////////////////////////////////////// // Check symbol table - sec =reader.sections[ ".symtab" ]; - + sec = reader.sections[".symtab"]; symbol_section_accessor sr( reader, sec ); BOOST_CHECK_EQUAL( sr.get_symbols_num(), 80 ); - checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, - ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 1, "", 0x10000134, 0, STB_LOCAL, STT_SECTION, 1, + checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 40, "__CTOR_END__", 0x10010AD4, 0, STB_LOCAL, STT_OBJECT, 16, + checkSymbol( sr, 1, "", 0x10000134, 0, STB_LOCAL, STT_SECTION, 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); - checkSymbol( sr, 52, "__init_array_start", 0x10010acc, 0, STB_LOCAL, STT_NOTYPE, 16, - ELF_ST_VISIBILITY( STV_HIDDEN ) ); - checkSymbol( sr, 64, "_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4", 0x10000920, 204, STB_GLOBAL, STT_FUNC, SHN_UNDEF, + checkSymbol( sr, 40, "__CTOR_END__", 0x10010AD4, 0, STB_LOCAL, STT_OBJECT, + 16, ELF_ST_VISIBILITY( STV_DEFAULT ) ); + checkSymbol( sr, 52, "__init_array_start", 0x10010acc, 0, STB_LOCAL, + STT_NOTYPE, 16, ELF_ST_VISIBILITY( STV_HIDDEN ) ); + checkSymbol( sr, 64, "_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4", 0x10000920, + 204, STB_GLOBAL, STT_FUNC, SHN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) ); checkSymbol( sr, 78, "main", 0x1000069c, 92, STB_GLOBAL, STT_FUNC, 11, ELF_ST_VISIBILITY( STV_DEFAULT ) ); @@ -670,27 +642,25 @@ BOOST_AUTO_TEST_CASE( test_ppc ) //////////////////////////////////////////////////////////////////////////// // Check relocation table - sec =reader.sections[ ".rela.dyn" ]; - + sec = reader.sections[".rela.dyn"]; relocation_section_accessor reloc( reader, sec ); BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 ); checkRelocation( &reloc, 1, 0x10010c0c, 0x10010c0c, "_ZSt4cout", 19, 0, 0 ); - sec =reader.sections[ ".rela.plt" ]; - + sec = reader.sections[".rela.plt"]; relocation_section_accessor reloc1( reader, sec ); BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 9 ); - checkRelocation( &reloc1, 0, 0x10010be4, 0x100008e0, "__cxa_atexit", 21, 0, 0 ); + checkRelocation( &reloc1, 0, 0x10010be4, 0x100008e0, "__cxa_atexit", 21, 0, + 0 ); checkRelocation( &reloc1, 1, 0x10010be8, 0x0, "__gmon_start__", 21, 0, 0 ); - + //////////////////////////////////////////////////////////////////////////// // Check note reader - sec =reader.sections[ ".note.ABI-tag" ]; - + sec = reader.sections[".note.ABI-tag"]; note_section_accessor notes( reader, sec ); BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u ); @@ -698,14 +668,13 @@ BOOST_AUTO_TEST_CASE( test_ppc ) checkNote( notes, 0, 1, std::string( "GNU" ), 16 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 ) { elfio writer; writer.create( ELFCLASS32, ELFDATA2LSB ); - + writer.set_os_abi( 0 ); writer.set_abi_version( 0 ); writer.set_type( ET_REL ); @@ -721,7 +690,7 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 ) note_sec->set_flags( SHF_ALLOC ); note_sec->set_addr_align( 4 ); note_section_accessor note_writer( writer, note_sec ); - char descr[6] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; + char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; note_writer.add_note( 0x77, "Hello", &descr, 6 ); BOOST_CHECK_EQUAL( note_sec->get_index(), 2 ); @@ -729,32 +698,29 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 ) writer.save( "elf_examples/elf_dummy_header_i386_32.elf" ); elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_i386_32.elf" ), - true ); + BOOST_REQUIRE_EQUAL( + reader.load( "elf_examples/elf_dummy_header_i386_32.elf" ), true ); //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, - EM_386, EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 ); + checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386, + EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ "" ]; - + section* sec = reader.sections[""]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec = reader.sections[ ".shstrtab" ]; - - checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, - 0, 17, 0, 0, 1, 0 ); + sec = reader.sections[".shstrtab"]; + + checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); + + sec = reader.sections[".note"]; - sec =reader.sections[ ".note" ]; - BOOST_CHECK_EQUAL( sec->get_index(), 2 ); - checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, - 0, 28, 0, 0, 4, 0 ); + checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 ) { @@ -777,7 +743,7 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 ) note_sec->set_flags( SHF_ALLOC ); note_sec->set_addr_align( 4 ); note_section_accessor note_writer( writer, note_sec ); - char descr[6] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; + char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; note_writer.add_note( 0x77, "Hello", &descr, 6 ); BOOST_CHECK_EQUAL( note_sec->get_index(), 2 ); @@ -785,31 +751,28 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 ) writer.save( "elf_examples/elf_dummy_header_ppc_32.elf" ); elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_ppc_32.elf" ), - true ); + BOOST_REQUIRE_EQUAL( + reader.load( "elf_examples/elf_dummy_header_ppc_32.elf" ), true ); //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, - EM_PPC, EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 ); + checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC, + EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ "" ]; - + section* sec = reader.sections[""]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ ".note" ]; - + sec = reader.sections[".note"]; + BOOST_CHECK_EQUAL( sec->get_index(), 2 ); - checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, - 0, 28, 0, 0, 4, 0 ); + checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); - sec =reader.sections[ ".shstrtab" ]; - - checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, - 0, 17, 0, 0, 1, 0 ); -} + sec = reader.sections[".shstrtab"]; + checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); +} //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 ) @@ -833,7 +796,7 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 ) note_sec->set_flags( SHF_ALLOC ); note_sec->set_addr_align( 4 ); note_section_accessor note_writer( writer, note_sec ); - char descr[6] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; + char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; note_writer.add_note( 0x77, "Hello", &descr, 6 ); BOOST_CHECK_EQUAL( note_sec->get_index(), 2 ); @@ -841,31 +804,28 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 ) writer.save( "elf_examples/elf_dummy_header_i386_64.elf" ); elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_i386_64.elf" ), - true ); + BOOST_REQUIRE_EQUAL( + reader.load( "elf_examples/elf_dummy_header_i386_64.elf" ), true ); //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, - EM_X86_64, EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 ); + checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64, + EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ "" ]; - + section* sec = reader.sections[""]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ ".note" ]; - + sec = reader.sections[".note"]; + BOOST_CHECK_EQUAL( sec->get_index(), 2 ); - checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, - 0, 28, 0, 0, 4, 0 ); + checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); - sec =reader.sections[ ".shstrtab" ]; - - checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, - 0, 17, 0, 0, 1, 0 ); -} + sec = reader.sections[".shstrtab"]; + checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); +} //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 ) @@ -889,7 +849,7 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 ) note_sec->set_flags( SHF_ALLOC ); note_sec->set_addr_align( 4 ); note_section_accessor note_writer( writer, note_sec ); - char descr[6] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; + char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; note_writer.add_note( 0x77, "Hello", &descr, 6 ); BOOST_CHECK_EQUAL( note_sec->get_index(), 2 ); @@ -897,42 +857,38 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 ) writer.save( "elf_examples/elf_dummy_header_ppc_64.elf" ); elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_ppc_64.elf" ), - true ); + BOOST_REQUIRE_EQUAL( + reader.load( "elf_examples/elf_dummy_header_ppc_64.elf" ), true ); //////////////////////////////////////////////////////////////////////////// // Check ELF header - checkHeader( reader, ELFCLASS64, ELFDATA2MSB, EV_CURRENT, ET_REL, - EM_PPC64, EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 ); + checkHeader( reader, ELFCLASS64, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC64, + EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 ); //////////////////////////////////////////////////////////////////////////// // Check sections - section* sec = reader.sections[ "" ]; - + section* sec = reader.sections[""]; + checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); - sec =reader.sections[ ".shstrtab" ]; - - checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, - 0, 17, 0, 0, 1, 0 ); + sec = reader.sections[".shstrtab"]; + + checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); + + sec = reader.sections[".note"]; - sec = reader.sections[ ".note" ]; - BOOST_CHECK_EQUAL( sec->get_index(), 2 ); - checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, - 0, 28, 0, 0, 4, 0 ); + checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dynamic_64_1 ) { elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), - true ); + BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), true ); section* dynsec = reader.sections[".dynamic"]; - BOOST_REQUIRE( dynsec != NULL); + BOOST_REQUIRE( dynsec != NULL ); dynamic_section_accessor da( reader, dynsec ); @@ -958,17 +914,15 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_1 ) BOOST_CHECK_EQUAL( value, 0 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dynamic_64_2 ) { elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/libfunc.so" ), - true ); + BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/libfunc.so" ), true ); section* dynsec = reader.sections[".dynamic"]; - BOOST_REQUIRE( dynsec != NULL); + BOOST_REQUIRE( dynsec != NULL ); dynamic_section_accessor da( reader, dynsec ); @@ -991,17 +945,15 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_2 ) BOOST_CHECK_EQUAL( value, 0 ); } - //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_dynamic_64_3 ) { elfio reader; - BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), - true ); + BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), true ); section* dynsec = reader.sections[".dynamic"]; - BOOST_REQUIRE( dynsec != NULL); + BOOST_REQUIRE( dynsec != NULL ); dynamic_section_accessor da( reader, dynsec ); BOOST_CHECK_EQUAL( da.get_entries_num(), 26 ); @@ -1016,15 +968,13 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 ) dynsec1->set_link( strsec1->get_index() ); dynamic_section_accessor da1( reader, dynsec1 ); - Elf_Xword tag, tag1; + Elf_Xword tag, tag1; Elf_Xword value, value1; - std::string str, str1; - + std::string str, str1; + for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) { da.get_entry( i, tag, value, str ); - if ( tag == DT_NEEDED || - tag == DT_SONAME || - tag == DT_RPATH || + if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || tag == DT_RUNPATH ) { da1.add_entry( tag, str ); } @@ -1032,15 +982,13 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 ) da1.add_entry( tag, value ); } } - + for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) { da.get_entry( i, tag, value, str ); da1.get_entry( i, tag1, value1, str1 ); BOOST_CHECK_EQUAL( tag, tag1 ); - if ( tag == DT_NEEDED || - tag == DT_SONAME || - tag == DT_RPATH || + if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || tag == DT_RUNPATH ) { BOOST_CHECK_EQUAL( str, str1 ); } diff --git a/tests/ELFIOTest1.cpp b/tests/ELFIOTest1.cpp index a101224dc315eb2540238da5bc64f945de1d6e74..9511515b69edd90d18cc7722dbd3e5434bd98a98 100644 --- a/tests/ELFIOTest1.cpp +++ b/tests/ELFIOTest1.cpp @@ -17,20 +17,20 @@ enum Tests }; //////////////////////////////////////////////////////////////////////////////// -bool write_obj_i386(bool is64bit) +bool write_obj_i386( bool is64bit ) { elfio writer; - writer.create(is64bit ? ELFCLASS64 : ELFCLASS32, ELFDATA2LSB); - writer.set_type(ET_REL); - writer.set_os_abi(ELFOSABI_LINUX); - writer.set_machine(is64bit ? EM_X86_64 : EM_386); + writer.create( is64bit ? ELFCLASS64 : ELFCLASS32, ELFDATA2LSB ); + writer.set_type( ET_REL ); + writer.set_os_abi( ELFOSABI_LINUX ); + writer.set_machine( is64bit ? EM_X86_64 : EM_386 ); // Create code section* - section *text_sec = writer.sections.add(".text"); - text_sec->set_type(SHT_PROGBITS); - text_sec->set_flags(SHF_ALLOC | SHF_EXECINSTR); - text_sec->set_addr_align(0x10); + section* text_sec = writer.sections.add( ".text" ); + text_sec->set_type( SHT_PROGBITS ); + text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + text_sec->set_addr_align( 0x10 ); // Add data into it char text[] = { @@ -42,53 +42,52 @@ bool write_obj_i386(bool is64bit) '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 '\xCD', '\x80' // int 0x80 }; - text_sec->set_data(text, sizeof(text)); + text_sec->set_data( text, sizeof( text ) ); // Create data section* - section *data_sec = writer.sections.add(".data"); - data_sec->set_type(SHT_PROGBITS); - data_sec->set_flags(SHF_ALLOC | SHF_WRITE); - data_sec->set_addr_align(4); - - char data[] = {'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 - '\x2C', '\x20', '\x57', '\x6F', '\x72', - '\x6C', '\x64', '\x21', '\x0A'}; - data_sec->set_data(data, sizeof(data)); - - section *str_sec = writer.sections.add(".strtab"); - str_sec->set_type(SHT_STRTAB); - str_sec->set_addr_align(0x1); - - string_section_accessor str_writer(str_sec); - Elf_Word nStrIndex = str_writer.add_string("msg"); - - section *sym_sec = writer.sections.add(".symtab"); - sym_sec->set_type(SHT_SYMTAB); - sym_sec->set_info(2); - sym_sec->set_link(str_sec->get_index()); - sym_sec->set_addr_align(4); - sym_sec->set_entry_size(writer.get_default_entry_size(SHT_SYMTAB)); - - symbol_section_accessor symbol_writer(writer, sym_sec); - Elf_Word nSymIndex = symbol_writer.add_symbol(nStrIndex, 0, 0, - STB_LOCAL, STT_NOTYPE, 0, - data_sec->get_index()); + section* data_sec = writer.sections.add( ".data" ); + data_sec->set_type( SHT_PROGBITS ); + data_sec->set_flags( SHF_ALLOC | SHF_WRITE ); + data_sec->set_addr_align( 4 ); + + char data[] = { + '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 + '\x2C', '\x20', '\x57', '\x6F', '\x72', + '\x6C', '\x64', '\x21', '\x0A' }; + data_sec->set_data( data, sizeof( data ) ); + + section* str_sec = writer.sections.add( ".strtab" ); + str_sec->set_type( SHT_STRTAB ); + str_sec->set_addr_align( 0x1 ); + + string_section_accessor str_writer( str_sec ); + Elf_Word nStrIndex = str_writer.add_string( "msg" ); + + section* sym_sec = writer.sections.add( ".symtab" ); + sym_sec->set_type( SHT_SYMTAB ); + sym_sec->set_info( 2 ); + sym_sec->set_link( str_sec->get_index() ); + sym_sec->set_addr_align( 4 ); + sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) ); + + symbol_section_accessor symbol_writer( writer, sym_sec ); + Elf_Word nSymIndex = symbol_writer.add_symbol( + nStrIndex, 0, 0, STB_LOCAL, STT_NOTYPE, 0, data_sec->get_index() ); // Another way to add symbol - symbol_writer.add_symbol(str_writer, "_start", 0x00000000, 0, - STB_WEAK, STT_FUNC, 0, - text_sec->get_index()); + symbol_writer.add_symbol( str_writer, "_start", 0x00000000, 0, STB_WEAK, + STT_FUNC, 0, text_sec->get_index() ); // Create relocation table section* - section *rel_sec = writer.sections.add(".rel.text"); - rel_sec->set_type(SHT_REL); - rel_sec->set_info(text_sec->get_index()); - rel_sec->set_link(sym_sec->get_index()); - rel_sec->set_addr_align(4); - rel_sec->set_entry_size(writer.get_default_entry_size(SHT_REL)); + section* rel_sec = writer.sections.add( ".rel.text" ); + rel_sec->set_type( SHT_REL ); + rel_sec->set_info( text_sec->get_index() ); + rel_sec->set_link( sym_sec->get_index() ); + rel_sec->set_addr_align( 4 ); + rel_sec->set_entry_size( writer.get_default_entry_size( SHT_REL ) ); - relocation_section_accessor rel_writer(writer, rel_sec); - rel_writer.add_entry(11, nSymIndex, (unsigned char)R_386_RELATIVE); + relocation_section_accessor rel_writer( writer, rel_sec ); + rel_writer.add_entry( 11, nSymIndex, (unsigned char)R_386_RELATIVE ); // Another method to add the same relocation entry // pRelWriter->AddEntry( pStrWriter, "msg", @@ -98,39 +97,41 @@ bool write_obj_i386(bool is64bit) // 0, (unsigned char)R_386_RELATIVE ); // Create note section* - section *note_sec = writer.sections.add(".note"); - note_sec->set_type(SHT_NOTE); - note_sec->set_addr_align(1); + section* note_sec = writer.sections.add( ".note" ); + note_sec->set_type( SHT_NOTE ); + note_sec->set_addr_align( 1 ); // Create notes writer - note_section_accessor note_writer(writer, note_sec); - note_writer.add_note(0x77, "Created by ELFIO", 0, 0); + note_section_accessor note_writer( writer, note_sec ); + note_writer.add_note( 0x77, "Created by ELFIO", 0, 0 ); // Create ELF file - writer.save( - is64bit ? "elf_examples/write_obj_i386_64.o" : "elf_examples/write_obj_i386_32.o"); + writer.save( is64bit ? "elf_examples/write_obj_i386_64.o" + : "elf_examples/write_obj_i386_32.o" ); return true; } //////////////////////////////////////////////////////////////////////////////// -bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = false, Elf64_Addr addr = 0) +bool write_exe_i386( const std::string& filename, + bool is64bit, + bool set_addr = false, + Elf64_Addr addr = 0 ) { elfio writer; - writer.create(is64bit ? ELFCLASS64 : ELFCLASS32, ELFDATA2LSB); - writer.set_os_abi(ELFOSABI_LINUX); - writer.set_type(ET_EXEC); - writer.set_machine(is64bit ? EM_X86_64 : EM_386); + writer.create( is64bit ? ELFCLASS64 : ELFCLASS32, ELFDATA2LSB ); + writer.set_os_abi( ELFOSABI_LINUX ); + writer.set_type( ET_EXEC ); + writer.set_machine( is64bit ? EM_X86_64 : EM_386 ); // Create code section* - section *text_sec = writer.sections.add(".text"); - text_sec->set_type(SHT_PROGBITS); - text_sec->set_flags(SHF_ALLOC | SHF_EXECINSTR); - text_sec->set_addr_align(0x10); - if (set_addr) - { - text_sec->set_address(addr); + section* text_sec = writer.sections.add( ".text" ); + text_sec->set_type( SHT_PROGBITS ); + text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + text_sec->set_addr_align( 0x10 ); + if ( set_addr ) { + text_sec->set_address( addr ); } // Add data into it @@ -143,255 +144,259 @@ bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = f '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 '\xCD', '\x80' // int 0x80 }; - text_sec->set_data(text, sizeof(text)); + text_sec->set_data( text, sizeof( text ) ); - segment *text_seg = writer.segments.add(); - text_seg->set_type(PT_LOAD); - text_seg->set_virtual_address(0x08048000); - text_seg->set_physical_address(0x08048000); - text_seg->set_flags(PF_X | PF_R); - text_seg->set_align(0x1000); - text_seg->add_section_index(text_sec->get_index(), text_sec->get_addr_align()); + segment* text_seg = writer.segments.add(); + text_seg->set_type( PT_LOAD ); + text_seg->set_virtual_address( 0x08048000 ); + text_seg->set_physical_address( 0x08048000 ); + text_seg->set_flags( PF_X | PF_R ); + text_seg->set_align( 0x1000 ); + text_seg->add_section_index( text_sec->get_index(), + text_sec->get_addr_align() ); // Create data section* - section *data_sec = writer.sections.add(".data"); - data_sec->set_type(SHT_PROGBITS); - data_sec->set_flags(SHF_ALLOC | SHF_WRITE); - data_sec->set_addr_align(0x4); - - char data[] = {'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 - '\x2C', '\x20', '\x57', '\x6F', '\x72', - '\x6C', '\x64', '\x21', '\x0A'}; - data_sec->set_data(data, sizeof(data)); - - segment *data_seg = writer.segments.add(); - data_seg->set_type(PT_LOAD); - data_seg->set_virtual_address(0x08048020); - data_seg->set_physical_address(0x08048020); - data_seg->set_flags(PF_W | PF_R); - data_seg->set_align(0x10); - data_seg->add_section_index(data_sec->get_index(), data_sec->get_addr_align()); - - section *note_sec = writer.sections.add(".note"); - note_sec->set_type(SHT_NOTE); - note_sec->set_addr_align(1); - note_section_accessor note_writer(writer, note_sec); - note_writer.add_note(0x01, "Created by ELFIO", 0, 0); - char descr[6] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36}; - note_writer.add_note(0x01, "Never easier!", descr, sizeof(descr)); + section* data_sec = writer.sections.add( ".data" ); + data_sec->set_type( SHT_PROGBITS ); + data_sec->set_flags( SHF_ALLOC | SHF_WRITE ); + data_sec->set_addr_align( 0x4 ); + + char data[] = { + '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 + '\x2C', '\x20', '\x57', '\x6F', '\x72', + '\x6C', '\x64', '\x21', '\x0A' }; + data_sec->set_data( data, sizeof( data ) ); + + segment* data_seg = writer.segments.add(); + data_seg->set_type( PT_LOAD ); + data_seg->set_virtual_address( 0x08048020 ); + data_seg->set_physical_address( 0x08048020 ); + data_seg->set_flags( PF_W | PF_R ); + data_seg->set_align( 0x10 ); + data_seg->add_section_index( data_sec->get_index(), + data_sec->get_addr_align() ); + + section* note_sec = writer.sections.add( ".note" ); + note_sec->set_type( SHT_NOTE ); + note_sec->set_addr_align( 1 ); + note_section_accessor note_writer( writer, note_sec ); + note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 ); + char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 }; + note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) ); // Create ELF file - writer.set_entry(0x08048000); + writer.set_entry( 0x08048000 ); - writer.save(filename); + writer.save( filename ); return true; } //////////////////////////////////////////////////////////////////////////////// -void checkObjestsAreEqual(std::string file_name1, std::string file_name2) +void checkObjestsAreEqual( std::string file_name1, std::string file_name2 ) { elfio file1; elfio file2; - BOOST_REQUIRE_EQUAL(file1.load(file_name1), true); - BOOST_CHECK_EQUAL(file1.save(file_name2), true); - BOOST_REQUIRE_EQUAL(file1.load(file_name1), true); - BOOST_REQUIRE_EQUAL(file2.load(file_name2), true); - - for (int i = 0; i < file1.sections.size(); ++i) - { - BOOST_CHECK_EQUAL(file1.sections[i]->get_address(), - file2.sections[i]->get_address()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_addr_align(), - file2.sections[i]->get_addr_align()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_entry_size(), - file2.sections[i]->get_entry_size()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_flags(), - file2.sections[i]->get_flags()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_index(), - file2.sections[i]->get_index()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_info(), - file2.sections[i]->get_info()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_link(), - file2.sections[i]->get_link()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_name(), - file2.sections[i]->get_name()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_name_string_offset(), - file2.sections[i]->get_name_string_offset()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_size(), - file2.sections[i]->get_size()); - BOOST_CHECK_EQUAL(file1.sections[i]->get_type(), - file2.sections[i]->get_type()); - - if (file1.sections[i]->get_type() == SHT_NULL || - file1.sections[i]->get_type() == SHT_NOBITS) - { + BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true ); + BOOST_CHECK_EQUAL( file1.save( file_name2 ), true ); + BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true ); + BOOST_REQUIRE_EQUAL( file2.load( file_name2 ), true ); + + for ( int i = 0; i < file1.sections.size(); ++i ) { + BOOST_CHECK_EQUAL( file1.sections[i]->get_address(), + file2.sections[i]->get_address() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_addr_align(), + file2.sections[i]->get_addr_align() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_entry_size(), + file2.sections[i]->get_entry_size() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_flags(), + file2.sections[i]->get_flags() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_index(), + file2.sections[i]->get_index() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_info(), + file2.sections[i]->get_info() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_link(), + file2.sections[i]->get_link() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_name(), + file2.sections[i]->get_name() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_name_string_offset(), + file2.sections[i]->get_name_string_offset() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_size(), + file2.sections[i]->get_size() ); + BOOST_CHECK_EQUAL( file1.sections[i]->get_type(), + file2.sections[i]->get_type() ); + + if ( file1.sections[i]->get_type() == SHT_NULL || + file1.sections[i]->get_type() == SHT_NOBITS ) { continue; } - BOOST_REQUIRE_NE(file1.sections[i]->get_data(), (const char *)0); - BOOST_REQUIRE_NE(file2.sections[i]->get_data(), (const char *)0); - std::string pdata1(file1.sections[i]->get_data(), - file1.sections[i]->get_data() + - file1.sections[i]->get_size()); - std::string pdata2(file2.sections[i]->get_data(), - file2.sections[i]->get_data() + - file2.sections[i]->get_size()); - - BOOST_CHECK_EQUAL(file1.sections[i]->get_size(), - file2.sections[i]->get_size()); - if ((file2.sections[i]->get_type() != SHT_NULL) && - (file2.sections[i]->get_type() != SHT_NOBITS)) - { - BOOST_CHECK_EQUAL_COLLECTIONS(pdata1.begin(), pdata1.end(), - pdata2.begin(), pdata2.end()); + BOOST_REQUIRE_NE( file1.sections[i]->get_data(), (const char*)0 ); + BOOST_REQUIRE_NE( file2.sections[i]->get_data(), (const char*)0 ); + std::string pdata1( file1.sections[i]->get_data(), + file1.sections[i]->get_data() + + file1.sections[i]->get_size() ); + std::string pdata2( file2.sections[i]->get_data(), + file2.sections[i]->get_data() + + file2.sections[i]->get_size() ); + + BOOST_CHECK_EQUAL( file1.sections[i]->get_size(), + file2.sections[i]->get_size() ); + if ( ( file2.sections[i]->get_type() != SHT_NULL ) && + ( file2.sections[i]->get_type() != SHT_NOBITS ) ) { + BOOST_CHECK_EQUAL_COLLECTIONS( pdata1.begin(), pdata1.end(), + pdata2.begin(), pdata2.end() ); } } } //////////////////////////////////////////////////////////////////////////////// -void checkExeAreEqual(std::string file_name1, std::string file_name2, int skipTests = 0) +void checkExeAreEqual( std::string file_name1, + std::string file_name2, + int skipTests = 0 ) { - checkObjestsAreEqual(file_name1, file_name2); + checkObjestsAreEqual( file_name1, file_name2 ); elfio file1; elfio file2; - BOOST_REQUIRE_EQUAL(file1.load(file_name1), true); - BOOST_REQUIRE_EQUAL(file2.load(file_name2), true); - - for (int i = 0; i < file1.segments.size(); ++i) - { - if (!(skipTests & SEG_ALIGN)) - BOOST_CHECK_EQUAL(file1.segments[i]->get_align(), - file2.segments[i]->get_align()); - BOOST_CHECK_EQUAL(file1.segments[i]->get_file_size(), - file2.segments[i]->get_file_size()); - BOOST_CHECK_EQUAL(file1.segments[i]->get_memory_size(), - file2.segments[i]->get_memory_size()); - BOOST_CHECK_EQUAL(file1.segments[i]->get_type(), - file2.segments[i]->get_type()); + BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true ); + BOOST_REQUIRE_EQUAL( file2.load( file_name2 ), true ); + + for ( int i = 0; i < file1.segments.size(); ++i ) { + if ( !( skipTests & SEG_ALIGN ) ) + BOOST_CHECK_EQUAL( file1.segments[i]->get_align(), + file2.segments[i]->get_align() ); + BOOST_CHECK_EQUAL( file1.segments[i]->get_file_size(), + file2.segments[i]->get_file_size() ); + BOOST_CHECK_EQUAL( file1.segments[i]->get_memory_size(), + file2.segments[i]->get_memory_size() ); + BOOST_CHECK_EQUAL( file1.segments[i]->get_type(), + file2.segments[i]->get_type() ); // skip data comparisons of the program header and of empty segments - if (file1.segments[i]->get_type() == PT_PHDR || !file1.segments[i]->get_file_size()) + if ( file1.segments[i]->get_type() == PT_PHDR || + !file1.segments[i]->get_file_size() ) continue; - BOOST_REQUIRE_NE(file1.segments[i]->get_data(), (const char *)0); - BOOST_REQUIRE_NE(file2.segments[i]->get_data(), (const char *)0); + BOOST_REQUIRE_NE( file1.segments[i]->get_data(), (const char*)0 ); + BOOST_REQUIRE_NE( file2.segments[i]->get_data(), (const char*)0 ); - std::string pdata1(file1.segments[i]->get_data(), - file1.segments[i]->get_data() + - file1.segments[i]->get_file_size()); - std::string pdata2(file2.segments[i]->get_data(), - file2.segments[i]->get_data() + - file2.segments[i]->get_file_size()); + std::string pdata1( file1.segments[i]->get_data(), + file1.segments[i]->get_data() + + file1.segments[i]->get_file_size() ); + std::string pdata2( file2.segments[i]->get_data(), + file2.segments[i]->get_data() + + file2.segments[i]->get_file_size() ); // truncate the data if the header and the segment table is // part of the segment - Elf64_Off afterPHDR = file1.get_segments_offset() + - file1.get_segment_entry_size() * (Elf64_Off)file1.segments.size(); - if (file1.segments[i]->get_offset() < afterPHDR) - { - pdata1 = pdata1.substr((unsigned int)afterPHDR); - pdata2 = pdata2.substr((unsigned int)afterPHDR); + Elf64_Off afterPHDR = + file1.get_segments_offset() + + file1.get_segment_entry_size() * (Elf64_Off)file1.segments.size(); + if ( file1.segments[i]->get_offset() < afterPHDR ) { + pdata1 = pdata1.substr( (unsigned int)afterPHDR ); + pdata2 = pdata2.substr( (unsigned int)afterPHDR ); } - BOOST_CHECK_EQUAL_COLLECTIONS(pdata1.begin(), pdata1.end(), - pdata2.begin(), pdata2.end()); + BOOST_CHECK_EQUAL_COLLECTIONS( pdata1.begin(), pdata1.end(), + pdata2.begin(), pdata2.end() ); } } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(write_obj_i386_32) +BOOST_AUTO_TEST_CASE( write_obj_i386_32 ) { - BOOST_CHECK_EQUAL(true, write_obj_i386(false)); - output_test_stream output("elf_examples/write_obj_i386_32_match.o", true, false); - std::ifstream input("elf_examples/write_obj_i386_32.o", std::ios::binary); + BOOST_CHECK_EQUAL( true, write_obj_i386( false ) ); + output_test_stream output( "elf_examples/write_obj_i386_32_match.o", true, + false ); + std::ifstream input( "elf_examples/write_obj_i386_32.o", std::ios::binary ); output << input.rdbuf(); - BOOST_CHECK(output.match_pattern()); + BOOST_CHECK( output.match_pattern() ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(write_obj_i386_64) +BOOST_AUTO_TEST_CASE( write_obj_i386_64 ) { - BOOST_CHECK_EQUAL(true, write_obj_i386(true)); - output_test_stream output("elf_examples/write_obj_i386_64_match.o", true, false); - std::ifstream input("elf_examples/write_obj_i386_64.o", std::ios::binary); + BOOST_CHECK_EQUAL( true, write_obj_i386( true ) ); + output_test_stream output( "elf_examples/write_obj_i386_64_match.o", true, + false ); + std::ifstream input( "elf_examples/write_obj_i386_64.o", std::ios::binary ); output << input.rdbuf(); - BOOST_CHECK(output.match_pattern()); + BOOST_CHECK( output.match_pattern() ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(write_exe_i386_32) +BOOST_AUTO_TEST_CASE( write_exe_i386_32 ) { - const std::string generated_file("elf_examples/write_exe_i386_32"); - const std::string reference_file("elf_examples/write_exe_i386_32_match"); - BOOST_CHECK_EQUAL(true, write_exe_i386(generated_file, false)); - output_test_stream output(reference_file, true, false); - std::ifstream input(generated_file, std::ios::binary); + const std::string generated_file( "elf_examples/write_exe_i386_32" ); + const std::string reference_file( "elf_examples/write_exe_i386_32_match" ); + BOOST_CHECK_EQUAL( true, write_exe_i386( generated_file, false ) ); + output_test_stream output( reference_file, true, false ); + std::ifstream input( generated_file, std::ios::binary ); output << input.rdbuf(); - BOOST_CHECK_MESSAGE(output.match_pattern(), "Comparing " + generated_file + " and " + reference_file); + BOOST_CHECK_MESSAGE( output.match_pattern(), "Comparing " + generated_file + + " and " + reference_file ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(elf_object_copy_32) +BOOST_AUTO_TEST_CASE( elf_object_copy_32 ) { - checkObjestsAreEqual("elf_examples/hello_32.o", - "elf_examples/hello_32_copy.o"); - checkObjestsAreEqual("elf_examples/hello_64.o", - "elf_examples/hello_64_copy.o"); - checkObjestsAreEqual("elf_examples/test_ppc.o", - "elf_examples/test_ppc_copy.o"); - checkObjestsAreEqual("elf_examples/write_obj_i386_32.o", - "elf_examples/write_obj_i386_32_copy.o"); - checkObjestsAreEqual("elf_examples/write_obj_i386_64.o", - "elf_examples/write_obj_i386_64_copy.o"); + checkObjestsAreEqual( "elf_examples/hello_32.o", + "elf_examples/hello_32_copy.o" ); + checkObjestsAreEqual( "elf_examples/hello_64.o", + "elf_examples/hello_64_copy.o" ); + checkObjestsAreEqual( "elf_examples/test_ppc.o", + "elf_examples/test_ppc_copy.o" ); + checkObjestsAreEqual( "elf_examples/write_obj_i386_32.o", + "elf_examples/write_obj_i386_32_copy.o" ); + checkObjestsAreEqual( "elf_examples/write_obj_i386_64.o", + "elf_examples/write_obj_i386_64_copy.o" ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(section_header_address_update) +BOOST_AUTO_TEST_CASE( section_header_address_update ) { elfio reader; - const std::string file_w_addr("elf_examples/write_exe_i386_32_w_addr"); - write_exe_i386(file_w_addr, false, true, 0x08048100); - reader.load(file_w_addr); - section *sec = reader.sections[".text"]; - BOOST_REQUIRE_NE(sec, (section *)0); - BOOST_CHECK_EQUAL(sec->get_address(), 0x08048100); + const std::string file_w_addr( "elf_examples/write_exe_i386_32_w_addr" ); + write_exe_i386( file_w_addr, false, true, 0x08048100 ); + reader.load( file_w_addr ); + section* sec = reader.sections[".text"]; + BOOST_REQUIRE_NE( sec, (section*)0 ); + BOOST_CHECK_EQUAL( sec->get_address(), 0x08048100 ); - const std::string file_wo_addr("elf_examples/write_exe_i386_32_wo_addr"); - write_exe_i386(file_wo_addr, false, false, 0); - reader.load(file_wo_addr); + const std::string file_wo_addr( "elf_examples/write_exe_i386_32_wo_addr" ); + write_exe_i386( file_wo_addr, false, false, 0 ); + reader.load( file_wo_addr ); sec = reader.sections[".text"]; - BOOST_REQUIRE_NE(sec, (section *)0); - BOOST_CHECK_EQUAL(sec->get_address(), 0x08048000); + BOOST_REQUIRE_NE( sec, (section*)0 ); + BOOST_CHECK_EQUAL( sec->get_address(), 0x08048000 ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(elfio_copy) +BOOST_AUTO_TEST_CASE( elfio_copy ) { elfio e; - const std::string filename("elf_examples/write_exe_i386_32_section_added"); - write_exe_i386(filename, false, true, 0x08048100); + const std::string filename( + "elf_examples/write_exe_i386_32_section_added" ); + write_exe_i386( filename, false, true, 0x08048100 ); - e.load(filename); + e.load( filename ); Elf_Half num = e.sections.size(); //section* new_sec = - e.sections.add("new"); - e.save(filename); - BOOST_CHECK_EQUAL(num + 1, e.sections.size()); + e.sections.add( "new" ); + e.save( filename ); + BOOST_CHECK_EQUAL( num + 1, e.sections.size() ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(elf_exe_copy_64) +BOOST_AUTO_TEST_CASE( elf_exe_copy_64 ) { - checkExeAreEqual("elf_examples/64bitLOAD.elf", - "elf_examples/64bitLOAD_copy.elf"); - checkExeAreEqual("elf_examples/asm64", - "elf_examples/asm64_copy"); - checkExeAreEqual("elf_examples/hello_64", - "elf_examples/hello_64_copy"); + checkExeAreEqual( "elf_examples/64bitLOAD.elf", + "elf_examples/64bitLOAD_copy.elf" ); + checkExeAreEqual( "elf_examples/asm64", "elf_examples/asm64_copy" ); + checkExeAreEqual( "elf_examples/hello_64", "elf_examples/hello_64_copy" ); // The last segment (GNU_RELRO) is bigger than necessary. // I don't see why but it contains a few bits of the .got.plt section. @@ -403,89 +408,83 @@ BOOST_AUTO_TEST_CASE(elf_exe_copy_64) } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(elf_exe_copy_32) +BOOST_AUTO_TEST_CASE( elf_exe_copy_32 ) { - checkExeAreEqual("elf_examples/asm", - "elf_examples/asm_copy"); - checkExeAreEqual("elf_examples/arm_v7m_test_debug.elf", - "elf_examples/arm_v7m_test_debug_copy.elf"); - checkExeAreEqual("elf_examples/arm_v7m_test_release.elf", - "elf_examples/arm_v7m_test_release_copy.elf"); - checkExeAreEqual("elf_examples/hello_32", - "elf_examples/hello_32_copy"); - checkExeAreEqual("elf_examples/hello_arm", - "elf_examples/hello_arm_copy"); - checkExeAreEqual("elf_examples/hello_arm_stripped", - "elf_examples/hello_arm_stripped_copy"); - checkExeAreEqual("elf_examples/read_write_arm_elf32_input", - "elf_examples/read_write_arm_elf32_input_copy"); - checkExeAreEqual("elf_examples/test_ppc", - "elf_examples/test_ppc_copy"); + checkExeAreEqual( "elf_examples/asm", "elf_examples/asm_copy" ); + checkExeAreEqual( "elf_examples/arm_v7m_test_debug.elf", + "elf_examples/arm_v7m_test_debug_copy.elf" ); + checkExeAreEqual( "elf_examples/arm_v7m_test_release.elf", + "elf_examples/arm_v7m_test_release_copy.elf" ); + checkExeAreEqual( "elf_examples/hello_32", "elf_examples/hello_32_copy" ); + checkExeAreEqual( "elf_examples/hello_arm", "elf_examples/hello_arm_copy" ); + checkExeAreEqual( "elf_examples/hello_arm_stripped", + "elf_examples/hello_arm_stripped_copy" ); + checkExeAreEqual( "elf_examples/read_write_arm_elf32_input", + "elf_examples/read_write_arm_elf32_input_copy" ); + checkExeAreEqual( "elf_examples/test_ppc", "elf_examples/test_ppc_copy" ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(elf_exe_loadsave_ppc32big3) +BOOST_AUTO_TEST_CASE( elf_exe_loadsave_ppc32big3 ) { - std::string in = "elf_examples/ppc-32bit-specimen3.elf"; + std::string in = "elf_examples/ppc-32bit-specimen3.elf"; std::string out = "elf_examples/ppc-32bit-testcopy3.elf"; - elfio elf; - BOOST_REQUIRE_EQUAL(elf.load(in), true); - BOOST_REQUIRE_EQUAL(elf.save(out), true); + elfio elf; + BOOST_REQUIRE_EQUAL( elf.load( in ), true ); + BOOST_REQUIRE_EQUAL( elf.save( out ), true ); - checkObjestsAreEqual(in, out); - checkExeAreEqual(in, out, SEG_ALIGN); + checkObjestsAreEqual( in, out ); + checkExeAreEqual( in, out, SEG_ALIGN ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(get_symbol_32) +BOOST_AUTO_TEST_CASE( get_symbol_32 ) { - elfio elf; - std::string name; + elfio elf; + std::string name; ELFIO::Elf_Xword size; - unsigned char bind; - unsigned char type; - ELFIO::Elf_Half section_index; - unsigned char other; - std::string in = "elf_examples/hello_32"; - - BOOST_REQUIRE_EQUAL(elf.load(in), true); - section *psymsec = elf.sections[".symtab"]; - const symbol_section_accessor symbols(elf, psymsec); - - BOOST_CHECK_EQUAL(true, - symbols.get_symbol(0x08048478, name, size, bind, - type, section_index, other)); - BOOST_CHECK_EQUAL("_IO_stdin_used", name); - BOOST_CHECK_EQUAL(14, section_index); - BOOST_CHECK_EQUAL(4, size); + unsigned char bind; + unsigned char type; + ELFIO::Elf_Half section_index; + unsigned char other; + std::string in = "elf_examples/hello_32"; + + BOOST_REQUIRE_EQUAL( elf.load( in ), true ); + section* psymsec = elf.sections[".symtab"]; + const symbol_section_accessor symbols( elf, psymsec ); + + BOOST_CHECK_EQUAL( true, symbols.get_symbol( 0x08048478, name, size, bind, + type, section_index, other ) ); + BOOST_CHECK_EQUAL( "_IO_stdin_used", name ); + BOOST_CHECK_EQUAL( 14, section_index ); + BOOST_CHECK_EQUAL( 4, size ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(get_symbol_64) +BOOST_AUTO_TEST_CASE( get_symbol_64 ) { - elfio elf; - std::string name; + elfio elf; + std::string name; ELFIO::Elf_Xword size; - unsigned char bind; - unsigned char type; - ELFIO::Elf_Half section_index; - unsigned char other; - std::string in = "elf_examples/hello_64"; - - BOOST_REQUIRE_EQUAL(elf.load(in), true); - section *psymsec = elf.sections[".symtab"]; - const symbol_section_accessor symbols(elf, psymsec); - - BOOST_CHECK_EQUAL(true, - symbols.get_symbol(0x00400498, name, size, bind, - type, section_index, other)); - BOOST_CHECK_EQUAL("main", name); - BOOST_CHECK_EQUAL(12, section_index); - BOOST_CHECK_EQUAL(21, size); + unsigned char bind; + unsigned char type; + ELFIO::Elf_Half section_index; + unsigned char other; + std::string in = "elf_examples/hello_64"; + + BOOST_REQUIRE_EQUAL( elf.load( in ), true ); + section* psymsec = elf.sections[".symtab"]; + const symbol_section_accessor symbols( elf, psymsec ); + + BOOST_CHECK_EQUAL( true, symbols.get_symbol( 0x00400498, name, size, bind, + type, section_index, other ) ); + BOOST_CHECK_EQUAL( "main", name ); + BOOST_CHECK_EQUAL( 12, section_index ); + BOOST_CHECK_EQUAL( 21, size ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(null_section_inside_segment) +BOOST_AUTO_TEST_CASE( null_section_inside_segment ) { // This test case checks the load/save of SHT_NULL sections in a segment // See https://github.com/serge1/ELFIO/issues/19 @@ -495,164 +494,176 @@ BOOST_AUTO_TEST_CASE(null_section_inside_segment) // Create an ELF file with SHT_NULL sections at the beginning/middle/end of a segment elfio writer; - writer.create(ELFCLASS32, ELFDATA2LSB); - writer.set_os_abi(ELFOSABI_LINUX); - writer.set_type(ET_EXEC); - writer.set_machine(EM_386); + writer.create( ELFCLASS32, ELFDATA2LSB ); + writer.set_os_abi( ELFOSABI_LINUX ); + writer.set_type( ET_EXEC ); + writer.set_machine( EM_386 ); // Create code section 1 - section *text_sec1 = writer.sections.add(".text1"); - text_sec1->set_type(SHT_PROGBITS); - text_sec1->set_flags(SHF_ALLOC | SHF_EXECINSTR); - text_sec1->set_addr_align(0x10); - text_sec1->set_address(0x08048000); - char text[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - text_sec1->set_data(text, sizeof(text)); + section* text_sec1 = writer.sections.add( ".text1" ); + text_sec1->set_type( SHT_PROGBITS ); + text_sec1->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + text_sec1->set_addr_align( 0x10 ); + text_sec1->set_address( 0x08048000 ); + char text[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + text_sec1->set_data( text, sizeof( text ) ); // Create code section 2 - section *text_sec2 = writer.sections.add(".text2"); - text_sec2->set_type(SHT_PROGBITS); - text_sec2->set_flags(SHF_ALLOC | SHF_EXECINSTR); - text_sec2->set_addr_align(0x10); - text_sec2->set_address(0x08048010); - text_sec2->set_data(text, sizeof(text)); + section* text_sec2 = writer.sections.add( ".text2" ); + text_sec2->set_type( SHT_PROGBITS ); + text_sec2->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + text_sec2->set_addr_align( 0x10 ); + text_sec2->set_address( 0x08048010 ); + text_sec2->set_data( text, sizeof( text ) ); // Create null sections - section *null_sec1 = writer.sections.add("null"); - null_sec1->set_type(SHT_NULL); - null_sec1->set_flags(SHF_ALLOC | SHF_EXECINSTR); - null_sec1->set_address(0x08048000); - section *null_sec2 = writer.sections.add("null"); - null_sec2->set_type(SHT_NULL); - null_sec2->set_flags(SHF_ALLOC | SHF_EXECINSTR); - null_sec2->set_address(0x08048010); - section *null_sec3 = writer.sections.add("null"); - null_sec3->set_type(SHT_NULL); - null_sec3->set_flags(SHF_ALLOC | SHF_EXECINSTR); - null_sec3->set_address(0x08048020); + section* null_sec1 = writer.sections.add( "null" ); + null_sec1->set_type( SHT_NULL ); + null_sec1->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + null_sec1->set_address( 0x08048000 ); + section* null_sec2 = writer.sections.add( "null" ); + null_sec2->set_type( SHT_NULL ); + null_sec2->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + null_sec2->set_address( 0x08048010 ); + section* null_sec3 = writer.sections.add( "null" ); + null_sec3->set_type( SHT_NULL ); + null_sec3->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + null_sec3->set_address( 0x08048020 ); // Create a loadable segment - segment *text_seg = writer.segments.add(); - text_seg->set_type(PT_LOAD); - text_seg->set_virtual_address(0x08048000); - text_seg->set_physical_address(0x08048000); - text_seg->set_flags(PF_X | PF_R); - text_seg->set_align(0x1000); + segment* text_seg = writer.segments.add(); + text_seg->set_type( PT_LOAD ); + text_seg->set_virtual_address( 0x08048000 ); + text_seg->set_physical_address( 0x08048000 ); + text_seg->set_flags( PF_X | PF_R ); + text_seg->set_align( 0x1000 ); // Add sections into the loadable segment - text_seg->add_section_index(null_sec1->get_index(), null_sec1->get_addr_align()); - text_seg->add_section_index(text_sec1->get_index(), text_sec1->get_addr_align()); - text_seg->add_section_index(null_sec2->get_index(), null_sec2->get_addr_align()); - text_seg->add_section_index(text_sec2->get_index(), text_sec2->get_addr_align()); - text_seg->add_section_index(null_sec3->get_index(), null_sec3->get_addr_align()); + text_seg->add_section_index( null_sec1->get_index(), + null_sec1->get_addr_align() ); + text_seg->add_section_index( text_sec1->get_index(), + text_sec1->get_addr_align() ); + text_seg->add_section_index( null_sec2->get_index(), + null_sec2->get_addr_align() ); + text_seg->add_section_index( text_sec2->get_index(), + text_sec2->get_addr_align() ); + text_seg->add_section_index( null_sec3->get_index(), + null_sec3->get_addr_align() ); // Setup entry point - writer.set_entry(0x08048000); + writer.set_entry( 0x08048000 ); // Create ELF file std::string f1 = "elf_examples/null_section_inside_segment1"; std::string f2 = "elf_examples/null_section_inside_segment2"; - BOOST_CHECK_EQUAL(writer.save(f1), true); + BOOST_CHECK_EQUAL( writer.save( f1 ), true ); // Load and check the ELF file created above elfio elf; - BOOST_CHECK_EQUAL(elf.load(f1), true); - BOOST_CHECK_EQUAL(elf.save(f2), true); + BOOST_CHECK_EQUAL( elf.load( f1 ), true ); + BOOST_CHECK_EQUAL( elf.save( f2 ), true ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(invalid_file) +BOOST_AUTO_TEST_CASE( invalid_file ) { - elfio elf; - std::string name; + elfio elf; + std::string name; ELFIO::Elf64_Addr value; - ELFIO::Elf_Xword size; - unsigned char bind; - unsigned char type; - ELFIO::Elf_Half section_index; - unsigned char other; - std::string in = "elf_examples/crash.elf"; - - BOOST_REQUIRE_EQUAL(elf.load(in), true); - section *psymsec = elf.sections[".symtab"]; - BOOST_REQUIRE_NE(psymsec, (void *)0); - const symbol_section_accessor symbols(elf, psymsec); - - BOOST_CHECK_EQUAL(true, - symbols.get_symbol("main", value, size, bind, - type, section_index, other)); - BOOST_CHECK_EQUAL(0x402560, value); - - BOOST_CHECK_EQUAL( - true, symbols.get_symbol("frame_dummy", value, size, bind, - type, section_index, other)); - BOOST_CHECK_EQUAL(0x402550, value); - - BOOST_CHECK_EQUAL(false, - symbols.get_symbol(0x00400498, name, size, bind, - type, section_index, other)); + ELFIO::Elf_Xword size; + unsigned char bind; + unsigned char type; + ELFIO::Elf_Half section_index; + unsigned char other; + std::string in = "elf_examples/crash.elf"; + + BOOST_REQUIRE_EQUAL( elf.load( in ), true ); + section* psymsec = elf.sections[".symtab"]; + BOOST_REQUIRE_NE( psymsec, (void*)0 ); + const symbol_section_accessor symbols( elf, psymsec ); + + BOOST_CHECK_EQUAL( true, symbols.get_symbol( "main", value, size, bind, + type, section_index, other ) ); + BOOST_CHECK_EQUAL( 0x402560, value ); + + BOOST_CHECK_EQUAL( true, + symbols.get_symbol( "frame_dummy", value, size, bind, + type, section_index, other ) ); + BOOST_CHECK_EQUAL( 0x402550, value ); + + BOOST_CHECK_EQUAL( false, + symbols.get_symbol( 0x00400498, name, size, bind, type, + section_index, other ) ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(rearrange_local_symbols) +BOOST_AUTO_TEST_CASE( rearrange_local_symbols ) { - std::string name = ""; - ELFIO::Elf64_Addr value = 0; - ELFIO::Elf_Xword size = 0; - unsigned char bind = STB_LOCAL; - unsigned char type = STT_FUNC; - ELFIO::Elf_Half section_index = 0; - unsigned char other = 0; - const std::string file_name = "elf_examples/test_symbols_order.elf"; + std::string name = ""; + ELFIO::Elf64_Addr value = 0; + ELFIO::Elf_Xword size = 0; + unsigned char bind = STB_LOCAL; + unsigned char type = STT_FUNC; + ELFIO::Elf_Half section_index = 0; + unsigned char other = 0; + const std::string file_name = "elf_examples/test_symbols_order.elf"; elfio writer; - writer.create(ELFCLASS64, ELFDATA2LSB); - writer.set_os_abi(ELFOSABI_LINUX); - writer.set_type(ET_EXEC); - writer.set_machine(EM_X86_64); - - section *str_sec = writer.sections.add(".strtab"); - str_sec->set_type(SHT_STRTAB); - str_sec->set_addr_align(0x1); - string_section_accessor str_writer(str_sec); - - section *sym_sec = writer.sections.add(".symtab"); - sym_sec->set_type(SHT_SYMTAB); - sym_sec->set_info(0); - sym_sec->set_link(str_sec->get_index()); - sym_sec->set_addr_align(4); - sym_sec->set_entry_size(writer.get_default_entry_size(SHT_SYMTAB)); - symbol_section_accessor symbols(writer, sym_sec); - - name = "Str1"; - bind = STB_GLOBAL; + writer.create( ELFCLASS64, ELFDATA2LSB ); + writer.set_os_abi( ELFOSABI_LINUX ); + writer.set_type( ET_EXEC ); + writer.set_machine( EM_X86_64 ); + + section* str_sec = writer.sections.add( ".strtab" ); + str_sec->set_type( SHT_STRTAB ); + str_sec->set_addr_align( 0x1 ); + string_section_accessor str_writer( str_sec ); + + section* sym_sec = writer.sections.add( ".symtab" ); + sym_sec->set_type( SHT_SYMTAB ); + sym_sec->set_info( 0 ); + sym_sec->set_link( str_sec->get_index() ); + sym_sec->set_addr_align( 4 ); + sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) ); + symbol_section_accessor symbols( writer, sym_sec ); + + name = "Str1"; + bind = STB_GLOBAL; value = 1; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str2"; bind = STB_LOCAL; value = 2; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str3"; bind = STB_WEAK; value = 3; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str4"; bind = STB_LOCAL; value = 4; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str5"; bind = STB_LOCAL; value = 5; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str6"; bind = STB_GLOBAL; value = 6; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str7"; bind = STB_LOCAL; value = 7; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); name = "Str8"; bind = STB_WEAK; value = 8; - symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); + symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type, + other, section_index ); - symbols.arrange_local_symbols([&](Elf_Xword first, Elf_Xword) -> void { + symbols.arrange_local_symbols( [&]( Elf_Xword first, Elf_Xword ) -> void { static int counter = 0; - BOOST_CHECK_EQUAL(first, ++counter); + BOOST_CHECK_EQUAL( first, ++counter ); // std::string name = ""; // ELFIO::Elf64_Addr value = 0; // ELFIO::Elf_Xword size = 0; @@ -666,185 +677,194 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols) // std::cout << " " << name; // symbols.get_symbol(second, name, value, size, bind, type, section_index, other); // std::cout << " " << name << std::endl; - }); + } ); - BOOST_REQUIRE_EQUAL(writer.save(file_name), true); + BOOST_REQUIRE_EQUAL( writer.save( file_name ), true ); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - elfio reader; - BOOST_REQUIRE_EQUAL(reader.load(file_name), true); + BOOST_REQUIRE_EQUAL( reader.load( file_name ), true ); auto psymsec = reader.sections[".symtab"]; - BOOST_REQUIRE_NE(psymsec, nullptr); + BOOST_REQUIRE_NE( psymsec, nullptr ); - const_symbol_section_accessor rsymbols(reader, psymsec); + const_symbol_section_accessor rsymbols( reader, psymsec ); auto bound = psymsec->get_info(); auto num = rsymbols.get_symbols_num(); - BOOST_CHECK_LE((Elf_Xword)bound, num); + BOOST_CHECK_LE( (Elf_Xword)bound, num ); // Check that all symbols are LOCAL until the bound value - for (Elf_Word i = 0; i < bound; i++) - { - rsymbols.get_symbol(i, name, value, size, bind, type, section_index, other); - BOOST_CHECK_EQUAL(bind, (unsigned char)STB_LOCAL); + for ( Elf_Word i = 0; i < bound; i++ ) { + rsymbols.get_symbol( i, name, value, size, bind, type, section_index, + other ); + BOOST_CHECK_EQUAL( bind, (unsigned char)STB_LOCAL ); } - BOOST_CHECK_EQUAL(name, "Str7"); + BOOST_CHECK_EQUAL( name, "Str7" ); // Check that all symbols are not LOCAL after the bound value - for (Elf_Word i = bound; i < num; i++) - { - rsymbols.get_symbol(i, name, value, size, bind, type, section_index, other); + for ( Elf_Word i = bound; i < num; i++ ) { + rsymbols.get_symbol( i, name, value, size, bind, type, section_index, + other ); - BOOST_CHECK_NE(bind, (unsigned char)STB_LOCAL); + BOOST_CHECK_NE( bind, (unsigned char)STB_LOCAL ); } - BOOST_CHECK_EQUAL(name, "Str8"); + BOOST_CHECK_EQUAL( name, "Str8" ); } //////////////////////////////////////////////////////////////////////////////// -BOOST_AUTO_TEST_CASE(rearrange_local_symbols_with_reallocation) +BOOST_AUTO_TEST_CASE( rearrange_local_symbols_with_reallocation ) { - std::string name = ""; - ELFIO::Elf64_Addr value = 0; - ELFIO::Elf_Xword size = 0; - unsigned char bind = STB_LOCAL; - unsigned char type = STT_FUNC; - ELFIO::Elf_Half section_index = 0; - unsigned char other = 0; - const std::string file_name = "elf_examples/test_symbols_order.elf"; + std::string name = ""; + ELFIO::Elf64_Addr value = 0; + ELFIO::Elf_Xword size = 0; + unsigned char bind = STB_LOCAL; + unsigned char type = STT_FUNC; + ELFIO::Elf_Half section_index = 0; + unsigned char other = 0; + const std::string file_name = "elf_examples/test_symbols_order.elf"; elfio writer; - writer.create(ELFCLASS64, ELFDATA2LSB); - writer.set_os_abi(ELFOSABI_LINUX); - writer.set_type(ET_EXEC); - writer.set_machine(EM_X86_64); - - section *text_sec = writer.sections.add(".text"); - text_sec->set_type(SHT_PROGBITS); - text_sec->set_flags(SHF_ALLOC | SHF_EXECINSTR); - text_sec->set_addr_align(0x10); - - section* str_sec = writer.sections.add(".strtab"); - str_sec->set_type(SHT_STRTAB); - str_sec->set_addr_align(0x1); - string_section_accessor str_writer(str_sec); - - section* sym_sec = writer.sections.add(".symtab"); - sym_sec->set_type(SHT_SYMTAB); - sym_sec->set_info(0); - sym_sec->set_link(str_sec->get_index()); - sym_sec->set_addr_align(4); - sym_sec->set_entry_size(writer.get_default_entry_size(SHT_SYMTAB)); - symbol_section_accessor symbols(writer, sym_sec); - - name = "Str1"; - bind = STB_GLOBAL; - value = 1; - Elf_Word sym1 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str2"; - bind = STB_LOCAL; - value = 2; - Elf_Word sym2 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str3"; - bind = STB_WEAK; - value = 3; - Elf_Word sym3 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str4"; - bind = STB_LOCAL; - value = 4; - Elf_Word sym4 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str5"; - bind = STB_LOCAL; - value = 5; - Elf_Word sym5 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str6"; - bind = STB_GLOBAL; - value = 6; - Elf_Word sym6 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str7"; - bind = STB_LOCAL; - value = 7; - Elf_Word sym7 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - name = "Str8"; - bind = STB_WEAK; - value = 8; - Elf_Word sym8 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index); - - section* rel_sec = writer.sections.add(".rel.text"); - rel_sec->set_type(SHT_REL); - rel_sec->set_info(text_sec->get_index()); - rel_sec->set_addr_align(0x4); - rel_sec->set_entry_size(writer.get_default_entry_size(SHT_REL)); - rel_sec->set_link(sym_sec->get_index()); - - relocation_section_accessor rela(writer, rel_sec); + writer.create( ELFCLASS64, ELFDATA2LSB ); + writer.set_os_abi( ELFOSABI_LINUX ); + writer.set_type( ET_EXEC ); + writer.set_machine( EM_X86_64 ); + + section* text_sec = writer.sections.add( ".text" ); + text_sec->set_type( SHT_PROGBITS ); + text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR ); + text_sec->set_addr_align( 0x10 ); + + section* str_sec = writer.sections.add( ".strtab" ); + str_sec->set_type( SHT_STRTAB ); + str_sec->set_addr_align( 0x1 ); + string_section_accessor str_writer( str_sec ); + + section* sym_sec = writer.sections.add( ".symtab" ); + sym_sec->set_type( SHT_SYMTAB ); + sym_sec->set_info( 0 ); + sym_sec->set_link( str_sec->get_index() ); + sym_sec->set_addr_align( 4 ); + sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) ); + symbol_section_accessor symbols( writer, sym_sec ); + + name = "Str1"; + bind = STB_GLOBAL; + value = 1; + Elf_Word sym1 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str2"; + bind = STB_LOCAL; + value = 2; + Elf_Word sym2 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str3"; + bind = STB_WEAK; + value = 3; + Elf_Word sym3 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str4"; + bind = STB_LOCAL; + value = 4; + Elf_Word sym4 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str5"; + bind = STB_LOCAL; + value = 5; + Elf_Word sym5 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str6"; + bind = STB_GLOBAL; + value = 6; + Elf_Word sym6 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str7"; + bind = STB_LOCAL; + value = 7; + Elf_Word sym7 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + name = "Str8"; + bind = STB_WEAK; + value = 8; + Elf_Word sym8 = symbols.add_symbol( str_writer, name.c_str(), value, size, + bind, type, other, section_index ); + + section* rel_sec = writer.sections.add( ".rel.text" ); + rel_sec->set_type( SHT_REL ); + rel_sec->set_info( text_sec->get_index() ); + rel_sec->set_addr_align( 0x4 ); + rel_sec->set_entry_size( writer.get_default_entry_size( SHT_REL ) ); + rel_sec->set_link( sym_sec->get_index() ); + + relocation_section_accessor rela( writer, rel_sec ); // Add relocation entry (adjust address at offset 11) - rela.add_entry(1, sym1, (unsigned char)R_386_RELATIVE); - rela.add_entry(8, sym8, (unsigned char)R_386_RELATIVE); - rela.add_entry(6, sym6, (unsigned char)R_386_RELATIVE); - rela.add_entry(2, sym2, (unsigned char)R_386_RELATIVE); - rela.add_entry(3, sym3, (unsigned char)R_386_RELATIVE); - rela.add_entry(8, sym8, (unsigned char)R_386_RELATIVE); - rela.add_entry(7, sym7, (unsigned char)R_386_RELATIVE); - rela.add_entry(2, sym2, (unsigned char)R_386_RELATIVE); - rela.add_entry(11, sym1, (unsigned char)R_386_RELATIVE); - rela.add_entry(18, sym8, (unsigned char)R_386_RELATIVE); - rela.add_entry(16, sym6, (unsigned char)R_386_RELATIVE); - rela.add_entry(12, sym2, (unsigned char)R_386_RELATIVE); - rela.add_entry(13, sym3, (unsigned char)R_386_RELATIVE); - rela.add_entry(17, sym7, (unsigned char)R_386_RELATIVE); - rela.add_entry(14, sym4, (unsigned char)R_386_RELATIVE); - rela.add_entry(15, sym5, (unsigned char)R_386_RELATIVE); + rela.add_entry( 1, sym1, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 8, sym8, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 6, sym6, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 2, sym2, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 3, sym3, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 8, sym8, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 7, sym7, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 2, sym2, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 11, sym1, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 18, sym8, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 16, sym6, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 12, sym2, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 13, sym3, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 17, sym7, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 14, sym4, (unsigned char)R_386_RELATIVE ); + rela.add_entry( 15, sym5, (unsigned char)R_386_RELATIVE ); std::vector<std::string> before; - for (Elf_Word i = 0; i < rela.get_entries_num(); i++) - { + for ( Elf_Word i = 0; i < rela.get_entries_num(); i++ ) { Elf64_Addr offset; Elf_Word symbol; Elf_Word rtype; Elf_Sxword addend; - rela.get_entry(i, offset, symbol, rtype, addend); - symbols.get_symbol(symbol, name, value, size, bind, type, section_index, other); - before.push_back(name); + rela.get_entry( i, offset, symbol, rtype, addend ); + symbols.get_symbol( symbol, name, value, size, bind, type, + section_index, other ); + before.push_back( name ); } - symbols.arrange_local_symbols([&](Elf_Xword first, Elf_Xword second) { - rela.swap_symbols(first, second); - }); + symbols.arrange_local_symbols( [&]( Elf_Xword first, Elf_Xword second ) { + rela.swap_symbols( first, second ); + } ); - BOOST_REQUIRE_EQUAL(writer.save(file_name), true); + BOOST_REQUIRE_EQUAL( writer.save( file_name ), true ); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - elfio reader; - BOOST_REQUIRE_EQUAL(reader.load(file_name), true); + BOOST_REQUIRE_EQUAL( reader.load( file_name ), true ); auto prelsec = reader.sections[".rel.text"]; auto psyms = reader.sections[".symtab"]; - BOOST_REQUIRE_NE(prelsec, nullptr); - BOOST_REQUIRE_NE(psyms, nullptr); + BOOST_REQUIRE_NE( prelsec, nullptr ); + BOOST_REQUIRE_NE( psyms, nullptr ); - const_relocation_section_accessor rel(reader, prelsec); - const_symbol_section_accessor syms(reader, psyms); + const_relocation_section_accessor rel( reader, prelsec ); + const_symbol_section_accessor syms( reader, psyms ); std::vector<std::string> after; - for (Elf_Word i = 0; i < rel.get_entries_num(); i++) - { + for ( Elf_Word i = 0; i < rel.get_entries_num(); i++ ) { Elf64_Addr offset; Elf_Word symbol; Elf_Word rtype; Elf_Sxword addend; - rel.get_entry(i, offset, symbol, rtype, addend); - syms.get_symbol(symbol, name, value, size, bind, type, section_index, other); - after.push_back(name); + rel.get_entry( i, offset, symbol, rtype, addend ); + syms.get_symbol( symbol, name, value, size, bind, type, section_index, + other ); + after.push_back( name ); } - BOOST_CHECK_EQUAL_COLLECTIONS(before.begin(), before.end(), after.begin(), after.end()); + BOOST_CHECK_EQUAL_COLLECTIONS( before.begin(), before.end(), after.begin(), + after.end() ); }