github.com/primecitizens/pcz/std@v0.2.1/encoding/binfmt/elf/elf64.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2009 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 /* 9 * ELF constants and data structures 10 * 11 * Derived from: 12 * $FreeBSD: src/sys/sys/elf32.h,v 1.8.14.1 2005/12/30 22:13:58 marcel Exp $ 13 * $FreeBSD: src/sys/sys/elf64.h,v 1.10.14.1 2005/12/30 22:13:58 marcel Exp $ 14 * $FreeBSD: src/sys/sys/elf_common.h,v 1.15.8.1 2005/12/30 22:13:58 marcel Exp $ 15 * $FreeBSD: src/sys/alpha/include/elf.h,v 1.14 2003/09/25 01:10:22 peter Exp $ 16 * $FreeBSD: src/sys/amd64/include/elf.h,v 1.18 2004/08/03 08:21:48 dfr Exp $ 17 * $FreeBSD: src/sys/arm/include/elf.h,v 1.5.2.1 2006/06/30 21:42:52 cognet Exp $ 18 * $FreeBSD: src/sys/i386/include/elf.h,v 1.16 2004/08/02 19:12:17 dfr Exp $ 19 * $FreeBSD: src/sys/powerpc/include/elf.h,v 1.7 2004/11/02 09:47:01 ssouhlal Exp $ 20 * $FreeBSD: src/sys/sparc64/include/elf.h,v 1.12 2003/09/25 01:10:26 peter Exp $ 21 * "System V ABI" (http://www.sco.com/developers/gabi/latest/ch4.eheader.html) 22 * "ELF for the ARMĀ® 64-bit Architecture (AArch64)" (ARM IHI 0056B) 23 * "RISC-V ELF psABI specification" (https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc) 24 * llvm/BinaryFormat/ELF.h - ELF constants and structures 25 * 26 * Copyright (c) 1996-1998 John D. Polstra. All rights reserved. 27 * Copyright (c) 2001 David E. O'Brien 28 * Portions Copyright 2009 The Go Authors. All rights reserved. 29 * 30 * Redistribution and use in source and binary forms, with or without 31 * modification, are permitted provided that the following conditions 32 * are met: 33 * 1. Redistributions of source code must retain the above copyright 34 * notice, this list of conditions and the following disclaimer. 35 * 2. Redistributions in binary form must reproduce the above copyright 36 * notice, this list of conditions and the following disclaimer in the 37 * documentation and/or other materials provided with the distribution. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 42 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49 * SUCH DAMAGE. 50 */ 51 52 package elf 53 54 /* 55 * ELF64 56 */ 57 58 // ELF64 file header. 59 type Header64 struct { 60 Ident [EI_NIDENT]byte /* File identification. */ 61 Type uint16 /* File type. */ 62 Machine uint16 /* Machine architecture. */ 63 Version uint32 /* ELF format version. */ 64 Entry uint64 /* Entry point. */ 65 Phoff uint64 /* Program header file offset. */ 66 Shoff uint64 /* Section header file offset. */ 67 Flags uint32 /* Architecture-specific flags. */ 68 Ehsize uint16 /* Size of ELF header in bytes. */ 69 Phentsize uint16 /* Size of program header entry. */ 70 Phnum uint16 /* Number of program header entries. */ 71 Shentsize uint16 /* Size of section header entry. */ 72 Shnum uint16 /* Number of section header entries. */ 73 Shstrndx uint16 /* Section name strings section. */ 74 } 75 76 // ELF64 Section header. 77 type Section64 struct { 78 Name uint32 /* Section name (index into the section header string table). */ 79 Type uint32 /* Section type. */ 80 Flags uint64 /* Section flags. */ 81 Addr uint64 /* Address in memory image. */ 82 Off uint64 /* Offset in file. */ 83 Size uint64 /* Size in bytes. */ 84 Link uint32 /* Index of a related section. */ 85 Info uint32 /* Depends on section type. */ 86 Addralign uint64 /* Alignment in bytes. */ 87 Entsize uint64 /* Size of each entry in section. */ 88 } 89 90 // ELF64 Program header. 91 type Prog64 struct { 92 Type uint32 /* Entry type. */ 93 Flags uint32 /* Access permission flags. */ 94 Off uint64 /* File offset of contents. */ 95 Vaddr uint64 /* Virtual address in memory image. */ 96 Paddr uint64 /* Physical address (not used). */ 97 Filesz uint64 /* Size of contents in file. */ 98 Memsz uint64 /* Size of contents in memory. */ 99 Align uint64 /* Alignment in memory and file. */ 100 } 101 102 // ELF64 Dynamic structure. The ".dynamic" section contains an array of them. 103 type Dyn64 struct { 104 Tag int64 /* Entry type. */ 105 Val uint64 /* Integer/address value */ 106 } 107 108 // ELF64 Compression header. 109 type Chdr64 struct { 110 Type uint32 111 _ uint32 /* Reserved. */ 112 Size uint64 113 Addralign uint64 114 } 115 116 /* 117 * Relocation entries. 118 */ 119 120 /* ELF64 relocations that don't need an addend field. */ 121 type Rel64 struct { 122 Off uint64 /* Location to be relocated. */ 123 Info uint64 /* Relocation type and symbol index. */ 124 } 125 126 /* ELF64 relocations that need an addend field. */ 127 type Rela64 struct { 128 Off uint64 /* Location to be relocated. */ 129 Info uint64 /* Relocation type and symbol index. */ 130 Addend int64 /* Addend. */ 131 } 132 133 func R_SYM64(info uint64) uint32 { return uint32(info >> 32) } 134 func R_TYPE64(info uint64) uint32 { return uint32(info) } 135 func R_INFO(sym, typ uint32) uint64 { return uint64(sym)<<32 | uint64(typ) } 136 137 // ELF64 symbol table entries. 138 type Sym64 struct { 139 Name uint32 /* String table index of name. */ 140 Info uint8 /* Type and binding information. */ 141 Other uint8 /* Reserved (not used). */ 142 Shndx uint16 /* Section index of symbol. */ 143 Value uint64 /* Symbol value. */ 144 Size uint64 /* Size of associated object. */ 145 } 146 147 const Sym64Size = 24