├── README.md ├── pch.cpp ├── selfutil.h ├── pch.h ├── self.h ├── selfutil.cpp ├── .gitignore └── elf.h /README.md: -------------------------------------------------------------------------------- 1 | # selfutil 2 | signed elf conversion util 3 | -------------------------------------------------------------------------------- /pch.cpp: -------------------------------------------------------------------------------- 1 | // pch.cpp: source file corresponding to pre-compiled header; necessary for compilation to succeed 2 | 3 | #include "pch.h" 4 | 5 | // In general, ignore this file, but keep it around if you are using pre-compiled headers. 6 | -------------------------------------------------------------------------------- /selfutil.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "elf.h" 4 | #include "self.h" 5 | 6 | 7 | using namespace std; 8 | 9 | class SelfUtil 10 | { 11 | vector data, save; 12 | 13 | Self_Hdr * seHead; 14 | vector entries; 15 | 16 | elf64_hdr* eHead; 17 | unat elfHOffs; 18 | 19 | vector phdrs; 20 | 21 | 22 | public: 23 | SelfUtil() 24 | { } 25 | 26 | SelfUtil(string filePath) 27 | { 28 | if(!Load(filePath)) 29 | printf("Error, Load() failed!\n"); 30 | } 31 | 32 | 33 | bool Parse(); 34 | bool TestIdent(); 35 | 36 | bool Load(string filePath); 37 | bool SaveToELF(string savePath); 38 | }; 39 | 40 | -------------------------------------------------------------------------------- /pch.h: -------------------------------------------------------------------------------- 1 | //pch.h 2 | 3 | #ifndef PCH_H 4 | #define PCH_H 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | 17 | 18 | 19 | 20 | 21 | typedef uint8_t u8, U8; 22 | typedef uint16_t u16, U16; 23 | typedef uint32_t u32, U32; 24 | typedef uint64_t u64, U64; 25 | 26 | typedef int8_t s8, S8; 27 | typedef int16_t s16, S16; 28 | typedef int32_t s32, S32; 29 | typedef int64_t s64, S64; 30 | 31 | typedef float f32, F32; 32 | typedef double f64, F64; 33 | 34 | 35 | #if UINTPTR_MAX > UINT_MAX 36 | typedef U64 unat, unat_t; 37 | typedef S64 snat, snat_t; 38 | #else 39 | typedef U32 unat, unat_t; 40 | typedef S32 snat, snat_t; 41 | #endif 42 | 43 | 44 | // constexpr helpers 45 | 46 | template constexpr T KB(const T n) { return 1024 * n; } 47 | template constexpr T MB(const T n) { return 1024 * KB(n); } 48 | template constexpr T GB(const T n) { return 1024 * MB(n); } 49 | template constexpr T TB(const T n) { return 1024 * GB(n); } 50 | template constexpr T PB(const T n) { return 1024 * TB(n); } 51 | 52 | 53 | 54 | #define INLINE inline // __forceinline__ 55 | 56 | 57 | // Generic Alignment for non pow2, up is abuseable since it's an integer :) 58 | 59 | template INLINE T Align(const T addr, const T align, unat up=0) 60 | { 61 | return (addr / align + up) * align; 62 | } 63 | 64 | // Alignment for unsigned integers of any type, align must be pow2! 65 | 66 | #define POW2_MASK (align - static_cast(1)) 67 | 68 | template INLINE T AlignUp(const T addr, const T align) 69 | { 70 | return (addr + POW2_MASK) & ~POW2_MASK; 71 | } 72 | 73 | template INLINE T AlignDown(const T addr, const T align) 74 | { 75 | return addr & ~POW2_MASK; 76 | } 77 | 78 | //#define alignTo Align 79 | #define alignUp AlignUp 80 | #define alignDown AlignDown 81 | 82 | 83 | 84 | 85 | 86 | 87 | #endif //PCH_H 88 | -------------------------------------------------------------------------------- /self.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | struct Self_Hdr // '<4s4B' 5 | { 6 | u32 magic; 7 | u8 version; 8 | u8 mode; 9 | u8 endian; 10 | u8 attribs; 11 | //}; 12 | //struct Self_ExtHdr // ' args; 18 | 19 | if(argc<2) { 20 | print_usage(); 21 | exit(0); 22 | } 23 | 24 | for(int i=1; ip_offset); 83 | if (0 != ph->p_offset) 84 | first = std::min(first, ph->p_offset); 85 | saveSize = std::max(saveSize, (size_t)(ph->p_offset+ph->p_filesz)); 86 | } 87 | saveSize = AlignUp(saveSize, PS4_PAGE_SIZE); 88 | 89 | printf("Save Size: %d bytes (0x%X) \n", saveSize, saveSize); 90 | printf("first , last : %llX , %llX \n", first, last); 91 | 92 | save.clear(); 93 | save.resize(saveSize); 94 | 95 | u8* pd = &save[0]; 96 | memset(pd, 0, saveSize); 97 | 98 | 99 | #if 1 100 | memcpy(pd, eHead, first); // just copy everything from head to what should be first seg offs // 101 | #else 102 | memcpy(pd, eHead, sizeof(elf64_hdr)); 103 | 104 | #if 0 // and hope it took care of all of this 105 | Elf64_Half e_ehsize; 106 | 107 | Elf64_Half e_phentsize; 108 | Elf64_Half e_phnum; 109 | 110 | Elf64_Half e_shentsize; 111 | Elf64_Half e_shnum; 112 | #endif 113 | #endif 114 | 115 | for (auto ee : entries) 116 | { 117 | if(0==(ee->props & 0x800)) 118 | continue; 119 | 120 | unat phIdx = (ee->props >> 20) & 0xFFF; 121 | 122 | Elf64_Phdr* ph = phdrs.at(phIdx); 123 | 124 | if (ph->p_filesz != ee->memSz) 125 | printf("idx: %d SEGMENT size: %d != phdr size: %d \n", phIdx, ee->memSz, ph->p_filesz); 126 | 127 | void *srcp = (void*)((unat)&data[0] + ee->offs); 128 | void *dstp = (void*)((unat)pd + ph->p_offset); 129 | memcpy(dstp, srcp, ee->fileSz); 130 | } 131 | 132 | 133 | 134 | FILE *f=nullptr; 135 | fopen_s(&f, savePath.c_str(), "wb"); 136 | if(f) { 137 | fwrite(pd, 1,saveSize, f); 138 | fclose(f); 139 | } 140 | else return false; 141 | 142 | return true; 143 | } 144 | 145 | 146 | 147 | 148 | 149 | 150 | bool SelfUtil::Parse() 151 | { 152 | if (data.size() < PS4_PAGE_SIZE) { 153 | printf("Bad file size! (%d)\n", data.size()); 154 | return false; 155 | } 156 | 157 | seHead = (Self_Hdr*)&data[0]; 158 | 159 | if (SELF_MAGIC != seHead->magic) { 160 | printf("Invalid Magic! (0x%08X)\n", seHead->magic); 161 | return false; 162 | } 163 | 164 | entries.clear(); 165 | for (unat seIdx=0; seIdxnum_entries; seIdx++) 166 | { 167 | entries.push_back(&((Self_Entry*)&data[0])[1 + seIdx]); 168 | 169 | const auto se = entries.back(); 170 | 171 | printf("Segment[%02d] P:%08X ", 172 | seIdx, se->props); 173 | 174 | printf(" (id: %X) ", (se->props>>20)); 175 | printf("@ 0x%016llX +%llX (mem: %llX)\n", 176 | se->offs, se->fileSz, se->memSz); 177 | } 178 | 179 | elfHOffs = (1 + seHead->num_entries) * 0x20; 180 | 181 | eHead = (elf64_hdr*)(&data[0] + elfHOffs); 182 | 183 | if (!TestIdent()) { 184 | printf("Elf e_ident invalid!\n"); 185 | return false; 186 | } 187 | 188 | 189 | for (unat phIdx=0; phIdxe_phnum; phIdx++) 190 | phdrs.push_back(&((Elf64_Phdr*)(&data[0] + elfHOffs + eHead->e_phoff))[phIdx]); 191 | 192 | 193 | return true; 194 | } 195 | 196 | 197 | 198 | 199 | 200 | 201 | bool SelfUtil::TestIdent() 202 | { 203 | if (ELF_MAGIC != ((u32*)eHead->e_ident)[0]) { 204 | printf("File is invalid! e_ident magic: %08X\n", ((u32*)eHead->e_ident)[0]); 205 | return false; 206 | } 207 | if (!( 208 | (eHead->e_ident[EI_CLASS] == ELFCLASS64) && 209 | (eHead->e_ident[EI_DATA] == ELFDATA2LSB) && 210 | (eHead->e_ident[EI_VERSION] == EV_CURRENT) && 211 | (eHead->e_ident[EI_OSABI] == ELFOSABI_FREEBSD))) 212 | return false; 213 | 214 | if ((eHead->e_type>>8) != 0xFE) // != ET_SCE_EXEC) 215 | printf(" Elf64::e_type: 0x%04X \n", eHead->e_type); 216 | 217 | if(!( (eHead->e_machine == EM_X86_64) && (eHead->e_version == EV_CURRENT) )) 218 | return false; 219 | 220 | return true; 221 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /elf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** elf.h, shamelessly ripped from linux 3 | */ 4 | #pragma once 5 | 6 | 7 | 8 | /* 64-bit ELF base types. */ 9 | 10 | typedef u64 Elf64_Addr, Elf64_Off, Elf64_Offs; 11 | 12 | typedef u16 Elf64_Half; 13 | typedef s16 Elf64_SHalf; 14 | typedef u32 Elf64_Word; 15 | typedef s32 Elf64_Sword; 16 | typedef u64 Elf64_Xword; 17 | typedef s64 Elf64_Sxword; 18 | 19 | /* These constants are for the segment types stored in the image headers */ 20 | #define PT_NULL 0 21 | #define PT_LOAD 1 22 | #define PT_DYNAMIC 2 23 | #define PT_INTERP 3 24 | #define PT_NOTE 4 25 | #define PT_SHLIB 5 26 | #define PT_PHDR 6 27 | #define PT_TLS 7 /* Thread local storage segment */ 28 | #define PT_LOOS 0x60000000 /* OS-specific */ 29 | #define PT_HIOS 0x6fffffff /* OS-specific */ 30 | #define PT_LOPROC 0x70000000 31 | #define PT_HIPROC 0x7fffffff 32 | //#define PT_GNU_EH_FRAME 0x6474e550 33 | //#define PT_GNU_STACK (PT_LOOS + 0x474e551) 34 | 35 | /* These constants define the different elf file types */ 36 | #define ET_NONE 0 37 | #define ET_REL 1 38 | #define ET_EXEC 2 39 | #define ET_DYN 3 40 | #define ET_CORE 4 41 | #define ET_LOPROC 0xff00 42 | #define ET_HIPROC 0xffff 43 | 44 | /* These constants define the various ELF target machines */ 45 | #define EM_NONE 0 46 | #define EM_M32 1 47 | #define EM_SPARC 2 48 | #define EM_386 3 49 | #define EM_68K 4 50 | #define EM_88K 5 51 | #define EM_486 6 /* Perhaps disused */ 52 | #define EM_860 7 53 | 54 | #define EM_MIPS 8 /* MIPS R3000 (officially, big-endian only) */ 55 | #define EM_MIPS_RS4_BE 10 /* MIPS R4000 big-endian */ 56 | #define EM_PARISC 15 /* HPPA */ 57 | #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ 58 | #define EM_PPC 20 /* PowerPC */ 59 | #define EM_PPC64 21 /* PowerPC64 */ 60 | #define EM_SH 42 /* SuperH */ 61 | #define EM_SPARCV9 43 /* SPARC v9 64-bit */ 62 | #define EM_IA_64 50 /* HP/Intel IA-64 */ 63 | #define EM_X86_64 62 /* AMD x86-64 */ 64 | #define EM_S390 22 /* IBM S/390 */ 65 | #define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ 66 | #define EM_V850 87 /* NEC v850 */ 67 | #define EM_M32R 88 /* Renesas M32R */ 68 | #define EM_H8_300 46 /* Renesas H8/300,300H,H8S */ 69 | 70 | #define EM_ALPHA 0x9026 71 | #define EM_CYGNUS_V850 0x9080 /* Bogus old v850 magic number, used by old tools. */ 72 | #define EM_CYGNUS_M32R 0x9041 /* Bogus old m32r magic number, used by old tools. */ 73 | #define EM_S390_OLD 0xA390 /* This is the old interim value for S/390 architecture */ 74 | #define EM_FRV 0x5441 /* Fujitsu FR-V */ 75 | 76 | /* This is the info that is needed to parse the dynamic section of the file */ 77 | #define DT_NULL 0 78 | #define DT_NEEDED 1 79 | #define DT_PLTRELSZ 2 80 | #define DT_PLTGOT 3 81 | #define DT_HASH 4 82 | #define DT_STRTAB 5 83 | #define DT_SYMTAB 6 84 | #define DT_RELA 7 85 | #define DT_RELASZ 8 86 | #define DT_RELAENT 9 87 | #define DT_STRSZ 10 88 | #define DT_SYMENT 11 89 | #define DT_INIT 12 90 | #define DT_FINI 13 91 | #define DT_SONAME 14 92 | #define DT_RPATH 15 93 | #define DT_SYMBOLIC 16 94 | #define DT_REL 17 95 | #define DT_RELSZ 18 96 | #define DT_RELENT 19 97 | #define DT_PLTREL 20 98 | #define DT_DEBUG 21 99 | #define DT_TEXTREL 22 100 | #define DT_JMPREL 23 101 | #define DT_LOPROC 0x70000000 102 | #define DT_HIPROC 0x7fffffff 103 | 104 | /* This info is needed when parsing the symbol table */ 105 | #define STB_LOCAL 0 106 | #define STB_GLOBAL 1 107 | #define STB_WEAK 2 108 | 109 | #define STT_NOTYPE 0 110 | #define STT_OBJECT 1 111 | #define STT_FUNC 2 112 | #define STT_SECTION 3 113 | #define STT_FILE 4 114 | 115 | #define ELF_ST_BIND(x) ((x) >> 4) 116 | #define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf) 117 | #define ELF64_ST_BIND(x) ELF_ST_BIND(x) 118 | #define ELF64_ST_TYPE(x) ELF_ST_TYPE(x) 119 | 120 | /* Symbolic values for the entries in the auxiliary table 121 | put on the initial stack */ 122 | #define AT_NULL 0 /* end of vector */ 123 | #define AT_IGNORE 1 /* entry should be ignored */ 124 | #define AT_EXECFD 2 /* file descriptor of program */ 125 | #define AT_PHDR 3 /* program headers for program */ 126 | #define AT_PHENT 4 /* size of program header entry */ 127 | #define AT_PHNUM 5 /* number of program headers */ 128 | #define AT_PAGESZ 6 /* system page size */ 129 | #define AT_BASE 7 /* base address of interpreter */ 130 | #define AT_FLAGS 8 /* flags */ 131 | #define AT_ENTRY 9 /* entry point of program */ 132 | #define AT_NOTELF 10 /* program is not ELF */ 133 | #define AT_UID 11 /* real uid */ 134 | #define AT_EUID 12 /* effective uid */ 135 | #define AT_GID 13 /* real gid */ 136 | #define AT_EGID 14 /* effective gid */ 137 | #define AT_PLATFORM 15 /* string identifying CPU for optimizations */ 138 | #define AT_HWCAP 16 /* arch dependent hints at CPU capabilities */ 139 | #define AT_CLKTCK 17 /* frequency at which times() increments */ 140 | 141 | #define AT_SECURE 23 /* secure mode boolean */ 142 | 143 | 144 | typedef struct { 145 | Elf64_Sxword d_tag; /* entry tag value */ 146 | union { 147 | Elf64_Xword d_val; 148 | Elf64_Addr d_ptr; 149 | } d_un; 150 | } Elf64_Dyn; 151 | 152 | /* The following are used with relocations */ 153 | 154 | #define ELF64_R_SYM(i) ((i) >> 32) 155 | #define ELF64_R_TYPE(i) ((i) & 0xffffffff) 156 | #define ELF_R_SYM(i) ELF64_R_SYM((i)) 157 | #define ELF_R_TYPE(i) ELF64_R_TYPE((i)) 158 | 159 | typedef struct elf64_rel { 160 | Elf64_Addr r_offset; /* Location at which to apply the action */ 161 | Elf64_Xword r_info; /* index and type of relocation */ 162 | } Elf64_Rel; 163 | 164 | typedef struct elf64_rela { 165 | Elf64_Addr r_offset; /* Location at which to apply the action */ 166 | Elf64_Xword r_info; /* index and type of relocation */ 167 | Elf64_Sxword r_addend; /* Constant addend used to compute value */ 168 | } Elf64_Rela; 169 | 170 | 171 | typedef struct elf64_sym { 172 | Elf64_Word st_name; /* Symbol name, index in string tbl */ 173 | unsigned char st_info; /* Type and binding attributes */ 174 | unsigned char st_other; /* No defined meaning, 0 */ 175 | Elf64_Half st_shndx; /* Associated section index */ 176 | Elf64_Addr st_value; /* Value of the symbol */ 177 | Elf64_Xword st_size; /* Associated symbol size */ 178 | } Elf64_Sym; 179 | 180 | 181 | #define EI_NIDENT 16 182 | 183 | 184 | typedef struct elf64_hdr { 185 | unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ 186 | Elf64_Half e_type; 187 | Elf64_Half e_machine; 188 | Elf64_Word e_version; 189 | Elf64_Addr e_entry; /* Entry point virtual address */ 190 | Elf64_Off e_phoff; /* Program header table file offset */ 191 | Elf64_Off e_shoff; /* Section header table file offset */ 192 | Elf64_Word e_flags; 193 | Elf64_Half e_ehsize; 194 | Elf64_Half e_phentsize; 195 | Elf64_Half e_phnum; 196 | Elf64_Half e_shentsize; 197 | Elf64_Half e_shnum; 198 | Elf64_Half e_shstrndx; 199 | } Elf64_Ehdr; 200 | 201 | /* These constants define the permissions on sections in the program 202 | header, p_flags. */ 203 | #define PF_R 0x4 204 | #define PF_W 0x2 205 | #define PF_X 0x1 206 | 207 | 208 | typedef struct elf64_phdr { 209 | Elf64_Word p_type; 210 | Elf64_Word p_flags; 211 | Elf64_Off p_offset; /* Segment file offset */ 212 | Elf64_Addr p_vaddr; /* Segment virtual address */ 213 | Elf64_Addr p_paddr; /* Segment physical address */ 214 | Elf64_Xword p_filesz; /* Segment size in file */ 215 | Elf64_Xword p_memsz; /* Segment size in memory */ 216 | Elf64_Xword p_align; /* Segment alignment, file & memory */ 217 | } Elf64_Phdr; 218 | 219 | /* sh_type */ 220 | #define SHT_NULL 0 221 | #define SHT_PROGBITS 1 222 | #define SHT_SYMTAB 2 223 | #define SHT_STRTAB 3 224 | #define SHT_RELA 4 225 | #define SHT_HASH 5 226 | #define SHT_DYNAMIC 6 227 | #define SHT_NOTE 7 228 | #define SHT_NOBITS 8 229 | #define SHT_REL 9 230 | #define SHT_SHLIB 10 231 | #define SHT_DYNSYM 11 232 | #define SHT_NUM 12 233 | #define SHT_LOPROC 0x70000000 234 | #define SHT_HIPROC 0x7fffffff 235 | #define SHT_LOUSER 0x80000000 236 | #define SHT_HIUSER 0xffffffff 237 | 238 | /* sh_flags */ 239 | #define SHF_WRITE 0x1 240 | #define SHF_ALLOC 0x2 241 | #define SHF_EXECINSTR 0x4 242 | #define SHF_MASKPROC 0xf0000000 243 | 244 | /* special section indexes */ 245 | #define SHN_UNDEF 0 246 | #define SHN_LORESERVE 0xff00 247 | #define SHN_LOPROC 0xff00 248 | #define SHN_HIPROC 0xff1f 249 | #define SHN_ABS 0xfff1 250 | #define SHN_COMMON 0xfff2 251 | #define SHN_HIRESERVE 0xffff 252 | 253 | 254 | typedef struct elf64_shdr { 255 | Elf64_Word sh_name; /* Section name, index in string tbl */ 256 | Elf64_Word sh_type; /* Type of section */ 257 | Elf64_Xword sh_flags; /* Miscellaneous section attributes */ 258 | Elf64_Addr sh_addr; /* Section virtual addr at execution */ 259 | Elf64_Off sh_offset; /* Section file offset */ 260 | Elf64_Xword sh_size; /* Size of section in bytes */ 261 | Elf64_Word sh_link; /* Index of another section */ 262 | Elf64_Word sh_info; /* Additional section information */ 263 | Elf64_Xword sh_addralign; /* Section alignment */ 264 | Elf64_Xword sh_entsize; /* Entry size if section holds table */ 265 | } Elf64_Shdr; 266 | 267 | #define EI_MAG0 0 /* e_ident[] indexes */ 268 | #define EI_MAG1 1 269 | #define EI_MAG2 2 270 | #define EI_MAG3 3 271 | #define EI_CLASS 4 272 | #define EI_DATA 5 273 | #define EI_VERSION 6 274 | #define EI_OSABI 7 275 | #define EI_ABIVER 8 276 | #define EI_PAD 9 277 | 278 | #define ELFMAG0 0x7f /* EI_MAG */ 279 | #define ELFMAG1 'E' 280 | #define ELFMAG2 'L' 281 | #define ELFMAG3 'F' 282 | #define ELFMAG "\177ELF" 283 | #define SELFMAG 4 284 | 285 | #define ELFCLASSNONE 0 /* EI_CLASS */ 286 | #define ELFCLASS32 1 287 | #define ELFCLASS64 2 288 | #define ELFCLASSNUM 3 289 | 290 | #define ELFDATANONE 0 /* e_ident[EI_DATA] */ 291 | #define ELFDATA2LSB 1 292 | #define ELFDATA2MSB 2 293 | 294 | 295 | #define EV_NONE 0 /* e_version, EI_VERSION */ 296 | #define EV_CURRENT 1 297 | #define EV_NUM 2 298 | 299 | #define ELFOSABI_NONE 0 300 | #define ELFOSABI_LINUX 3 301 | #define ELFOSABI_FREEBSD 9 /* e_ident[IE_OSABI] */ 302 | 303 | #ifndef ELF_OSABI 304 | #define ELF_OSABI ELFOSABI_FREEBSD 305 | #endif 306 | 307 | /* Notes used in ET_CORE */ 308 | #define NT_PRSTATUS 1 309 | #define NT_PRFPREG 2 310 | #define NT_PRPSINFO 3 311 | #define NT_TASKSTRUCT 4 312 | #define NT_AUXV 6 313 | #define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */ 314 | 315 | /* Note header in a PT_NOTE section */ 316 | typedef struct elf64_note { 317 | Elf64_Word n_namesz; /* Name size */ 318 | Elf64_Word n_descsz; /* Content size */ 319 | Elf64_Word n_type; /* Content type */ 320 | } Elf64_Nhdr; 321 | 322 | 323 | 324 | #define ELF_CLASS ELFCLASS64 325 | 326 | extern Elf64_Dyn _DYNAMIC []; 327 | 328 | typedef elf64_hdr elf_hdr, ElfHdr; 329 | typedef elf64_phdr elf_phdr, ElfPhdr; 330 | typedef elf64_note elf_node, ElfNote; 331 | 332 | 333 | --------------------------------------------------------------------------------