371 lines
9.3 KiB
C
371 lines
9.3 KiB
C
// Copyright (c) 2025, Zak Fenton
|
|
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
|
// software according to the terms and conditions of the Mulan PSL v2.
|
|
// You may obtain a copy of Mulan PSL v2 at:
|
|
// http://license.coscl.org.cn/MulanPSL2
|
|
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
|
// any kind, either express or implied, including but not limited to
|
|
// non-infringement, merchantability or fit for a particular purpose.
|
|
// See the Mulan PSL v2 for more details.
|
|
#ifndef _LIBC_ELF
|
|
#define _LIBC_ELF
|
|
|
|
// For the purposes of the audit program this is NEW CODE (but was written by me a while ago)
|
|
|
|
/* NOTE: The specification used as a reference is the draft at https://refspecs.linuxfoundation.org/elf/gabi4+/contents.html
|
|
* This header includes both 32-bit and 64-bit types.
|
|
* For x86-64 specific information I used as a reference https://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf
|
|
*/
|
|
|
|
/*
|
|
#include <stdint.h>
|
|
|
|
typedef uint32_t Elf32_Addr;
|
|
typedef uint32_t Elf32_Off;
|
|
typedef uint32_t Elf32_Word;
|
|
typedef int32_t Elf32_Sword;
|
|
typedef uint16_t Elf32_Half;
|
|
|
|
typedef uint64_t Elf64_Addr;
|
|
typedef uint64_t Elf64_Off;
|
|
typedef uint32_t Elf64_Word;
|
|
typedef int32_t Elf64_Sword;
|
|
typedef uint64_t Elf64_Xword;
|
|
typedef int64_t Elf64_Sxword;
|
|
typedef uint16_t Elf64_Half;
|
|
*/
|
|
typedef unsigned int Elf32_Addr;
|
|
typedef unsigned int Elf32_Off;
|
|
typedef unsigned int Elf32_Word;
|
|
typedef int Elf32_Sword;
|
|
typedef unsigned short Elf32_Half;
|
|
|
|
typedef unsigned long long Elf64_Addr;
|
|
typedef unsigned long long Elf64_Off;
|
|
typedef unsigned int Elf64_Word;
|
|
typedef int Elf64_Sword;
|
|
typedef unsigned long long Elf64_Xword;
|
|
typedef long long Elf64_Sxword;
|
|
typedef unsigned short Elf64_Half;
|
|
|
|
#define EV_NONE 0
|
|
#define EV_CURRENT 1
|
|
|
|
#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
|
|
|
|
#define ELFMAG0 0x7F
|
|
#define ELFMAG1 'E'
|
|
#define ELFMAG2 'L'
|
|
#define ELFMAG3 'F'
|
|
|
|
#define ELFCLASSNONE 0
|
|
#define ELFCLASS32 1
|
|
#define ELFCLASS64 2
|
|
|
|
#define ELFDATANONE 0
|
|
#define ELFDATA2LSB 1
|
|
#define ELFDATA2MSB 2
|
|
|
|
|
|
#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_X86_64 62
|
|
|
|
#define ELFOSABI_NONE 0
|
|
/* NOTE: SYSV is apparently the same as "NONE". */
|
|
#define ELFOSABI_SYSV 0
|
|
#define ELFOSABI_NETBSD 2
|
|
#define ELFOSABI_LINUX 3
|
|
#define ELFOSABI_SOLARIS 6
|
|
#define ELFOSABI_FREEBSD 9
|
|
#define ELFOSABI_OPENBSD 12
|
|
|
|
#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
|
|
#define SHT_HIPROC 0x7fffffff
|
|
#define SHT_LOUSER 0x80000000
|
|
#define SHT_HIUSER 0xffffffff
|
|
|
|
#define SHN_UNDEF 0
|
|
#define SHN_LORESERVE 0xff00
|
|
#define SHN_LOPROC 0xff00
|
|
#define SHN_HIPROC 0xff1f
|
|
#define SHN_LOOS 0xff20
|
|
#define SHN_HIOS 0xff3f
|
|
#define SHN_ABS 0xfff1
|
|
#define SHN_COMMON 0xfff2
|
|
#define SHN_XINDEX 0xffff
|
|
#define SHN_HIRESERVE 0xffff
|
|
|
|
#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
|
|
|
|
|
|
#define STN_UNDEF 0
|
|
|
|
#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
|
|
|
|
#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_HIOS 12
|
|
#define STT_LOPROC 13
|
|
#define STT_HIPROC 15
|
|
|
|
#define PF_X 0x1
|
|
#define PF_W 0x2
|
|
#define PF_R 0x4
|
|
#define PF_MASKOS 0x0ff00000
|
|
#define PF_MASKPROC 0xf0000000
|
|
|
|
|
|
/* These macros are defined in the draft specification: */
|
|
/* NOTE: I think at least my _INFO is broken here. */
|
|
#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) (((s)<<32)+((t)&0xffffffffL))
|
|
|
|
#define ELF32_ST_BIND(i) ((i)>>4)
|
|
#define ELF32_ST_TYPE(i) ((i)&0xf)
|
|
#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
|
|
|
|
#define ELF64_ST_BIND(i) ((i)>>4)
|
|
#define ELF64_ST_TYPE(i) ((i)&0xf)
|
|
#define ELF64_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
|
|
|
|
#define ELF32_ST_VISIBILITY(o) ((o)&0x3)
|
|
#define ELF64_ST_VISIBILITY(o) ((o)&0x3)
|
|
|
|
/* These (relocation types?) are specific to x86-64. I still find it weird that ELF works that way (why not just encode the expressions so it'd be portable?) */
|
|
#define R_X86_64_NONE 0 //none none
|
|
#define R_X86_64_64 1 //word64 S + A
|
|
#define R_X86_64_PC32 2 //word32 S + A - P
|
|
#define R_X86_64_GOT32 3 //word32 G + A
|
|
#define R_X86_64_PLT32 4 //word32 L + A - P
|
|
#define R_X86_64_COPY 5 //none none
|
|
#define R_X86_64_GLOB_DAT 6 //word64 S
|
|
#define R_X86_64_JUMP_SLOT 7 //word64 S
|
|
#define R_X86_64_RELATIVE 8 //word64 B + A
|
|
#define R_X86_64_GOTPCREL 9 //word32 G + GOT + A - P
|
|
#define R_X86_64_32 10 //word32 S + A
|
|
#define R_X86_64_32S 11 //word32 S + A
|
|
#define R_X86_64_16 12 //word16 S + A
|
|
#define R_X86_64_PC16 13 //word16 S + A - P
|
|
#define R_X86_64_8 14 //word8 S + A
|
|
#define R_X86_64_PC8 15 //word8 S + A - P
|
|
#define R_X86_64_DPTMOD64 16 //word64
|
|
#define R_X86_64_DTPOFF64 17 //word64
|
|
#define R_X86_64_TPOFF64 18 //word64
|
|
#define R_X86_64_TLSGD 19 //word32
|
|
#define R_X86_64_TLSLD 20 //word32
|
|
#define R_X86_64_DTPOFF32 21 //word32
|
|
#define R_X86_64_GOTTPOFF 22 //word32
|
|
#define R_X86_64_TPOFF32 23 //word32
|
|
#define R_X86_64_PC64 24 //word64 S + A - P
|
|
#define R_X86_64_GOTOFF64 25 //word64 S + A - GOT
|
|
#define R_X86_64_GOTPC32 26 //word32 GOT + A - P
|
|
|
|
typedef struct Elf32_Ehdr_struct Elf32_Ehdr;
|
|
typedef struct Elf64_Ehdr_struct Elf64_Ehdr;
|
|
typedef struct Elf32_Shdr_struct Elf32_Shdr;
|
|
typedef struct Elf64_Shdr_struct Elf64_Shdr;
|
|
typedef struct Elf32_Sym_struct Elf32_Sym;
|
|
typedef struct Elf64_Sym_struct Elf64_Sym;
|
|
typedef struct Elf32_Rel_struct Elf32_Rel;
|
|
typedef struct Elf64_Rel_struct Elf64_Rel;
|
|
typedef struct Elf32_Rela_struct Elf32_Rela;
|
|
typedef struct Elf64_Rela_struct Elf64_Rela;
|
|
typedef struct Elf32_Phdr_struct Elf32_Phdr;
|
|
typedef struct Elf64_Phdr_struct Elf64_Phdr;
|
|
|
|
struct Elf32_Ehdr_struct {
|
|
unsigned char e_ident[EI_NIDENT];
|
|
Elf32_Half e_type;
|
|
Elf32_Half e_machine;
|
|
Elf32_Word e_version;
|
|
Elf32_Addr e_entry;
|
|
Elf32_Off e_phoff;
|
|
Elf32_Off e_shoff;
|
|
Elf32_Word e_flags;
|
|
Elf32_Half e_ehsize;
|
|
Elf32_Half e_phentsize;
|
|
Elf32_Half e_phnum;
|
|
Elf32_Half e_shentsize;
|
|
Elf32_Half e_shnum;
|
|
Elf32_Half e_shstrndx;
|
|
};
|
|
|
|
struct Elf64_Ehdr_struct {
|
|
unsigned char e_ident[EI_NIDENT];
|
|
Elf64_Half e_type;
|
|
Elf64_Half e_machine;
|
|
Elf64_Word e_version;
|
|
Elf64_Addr e_entry;
|
|
Elf64_Off e_phoff;
|
|
Elf64_Off e_shoff;
|
|
Elf64_Word e_flags;
|
|
Elf64_Half e_ehsize;
|
|
Elf64_Half e_phentsize;
|
|
Elf64_Half e_phnum;
|
|
Elf64_Half e_shentsize;
|
|
Elf64_Half e_shnum;
|
|
Elf64_Half e_shstrndx;
|
|
};
|
|
|
|
struct Elf32_Shdr_struct {
|
|
Elf32_Word sh_name;
|
|
Elf32_Word sh_type;
|
|
Elf32_Word sh_flags;
|
|
Elf32_Addr sh_addr;
|
|
Elf32_Off sh_offset;
|
|
Elf32_Word sh_size;
|
|
Elf32_Word sh_link;
|
|
Elf32_Word sh_info;
|
|
Elf32_Word sh_addralign;
|
|
Elf32_Word sh_entsize;
|
|
};
|
|
|
|
struct Elf64_Shdr_struct {
|
|
Elf64_Word sh_name;
|
|
Elf64_Word sh_type;
|
|
Elf64_Xword sh_flags;
|
|
Elf64_Addr sh_addr;
|
|
Elf64_Off sh_offset;
|
|
Elf64_Xword sh_size;
|
|
Elf64_Word sh_link;
|
|
Elf64_Word sh_info;
|
|
Elf64_Xword sh_addralign;
|
|
Elf64_Xword sh_entsize;
|
|
};
|
|
|
|
struct Elf32_Sym_struct {
|
|
Elf32_Word st_name;
|
|
Elf32_Addr st_value;
|
|
Elf32_Word st_size;
|
|
unsigned char st_info;
|
|
unsigned char st_other;
|
|
Elf32_Half st_shndx;
|
|
};
|
|
|
|
struct Elf64_Sym_struct {
|
|
Elf64_Word st_name;
|
|
unsigned char st_info;
|
|
unsigned char st_other;
|
|
Elf64_Half st_shndx;
|
|
Elf64_Addr st_value;
|
|
Elf64_Xword st_size;
|
|
};
|
|
|
|
struct Elf32_Rel_struct {
|
|
Elf32_Addr r_offset;
|
|
Elf32_Word r_info;
|
|
};
|
|
|
|
struct Elf32_Rela_struct {
|
|
Elf32_Addr r_offset;
|
|
Elf32_Word r_info;
|
|
Elf32_Sword r_addend;
|
|
};
|
|
|
|
struct Elf64_Rel_struct {
|
|
Elf64_Addr r_offset;
|
|
Elf64_Xword r_info;
|
|
};
|
|
|
|
struct Elf64_Rela_struct {
|
|
Elf64_Addr r_offset;
|
|
Elf64_Xword r_info;
|
|
Elf64_Sxword r_addend;
|
|
};
|
|
|
|
struct Elf32_Phdr_struct {
|
|
Elf32_Word p_type;
|
|
Elf32_Off p_offset;
|
|
Elf32_Addr p_vaddr;
|
|
Elf32_Addr p_paddr;
|
|
Elf32_Word p_filesz;
|
|
Elf32_Word p_memsz;
|
|
Elf32_Word p_flags;
|
|
Elf32_Word p_align;
|
|
};
|
|
|
|
struct Elf64_Phdr_struct {
|
|
Elf64_Word p_type;
|
|
Elf64_Word p_flags;
|
|
Elf64_Off p_offset;
|
|
Elf64_Addr p_vaddr;
|
|
Elf64_Addr p_paddr;
|
|
Elf64_Xword p_filesz;
|
|
Elf64_Xword p_memsz;
|
|
Elf64_Xword p_align;
|
|
};
|
|
|
|
/* From ifndef at top of file: */
|
|
#endif
|