18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _PPC_BOOT_ELF_H_
38c2ecf20Sopenharmony_ci#define _PPC_BOOT_ELF_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/* 32-bit ELF base types. */
68c2ecf20Sopenharmony_citypedef unsigned int Elf32_Addr;
78c2ecf20Sopenharmony_citypedef unsigned short Elf32_Half;
88c2ecf20Sopenharmony_citypedef unsigned int Elf32_Off;
98c2ecf20Sopenharmony_citypedef signed int Elf32_Sword;
108c2ecf20Sopenharmony_citypedef unsigned int Elf32_Word;
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* 64-bit ELF base types. */
138c2ecf20Sopenharmony_citypedef unsigned long long Elf64_Addr;
148c2ecf20Sopenharmony_citypedef unsigned short Elf64_Half;
158c2ecf20Sopenharmony_citypedef signed short Elf64_SHalf;
168c2ecf20Sopenharmony_citypedef unsigned long long Elf64_Off;
178c2ecf20Sopenharmony_citypedef signed int Elf64_Sword;
188c2ecf20Sopenharmony_citypedef unsigned int Elf64_Word;
198c2ecf20Sopenharmony_citypedef unsigned long long Elf64_Xword;
208c2ecf20Sopenharmony_citypedef signed long long Elf64_Sxword;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* These constants are for the segment types stored in the image headers */
238c2ecf20Sopenharmony_ci#define PT_NULL    0
248c2ecf20Sopenharmony_ci#define PT_LOAD    1
258c2ecf20Sopenharmony_ci#define PT_DYNAMIC 2
268c2ecf20Sopenharmony_ci#define PT_INTERP  3
278c2ecf20Sopenharmony_ci#define PT_NOTE    4
288c2ecf20Sopenharmony_ci#define PT_SHLIB   5
298c2ecf20Sopenharmony_ci#define PT_PHDR    6
308c2ecf20Sopenharmony_ci#define PT_TLS     7		/* Thread local storage segment */
318c2ecf20Sopenharmony_ci#define PT_LOOS    0x60000000	/* OS-specific */
328c2ecf20Sopenharmony_ci#define PT_HIOS    0x6fffffff	/* OS-specific */
338c2ecf20Sopenharmony_ci#define PT_LOPROC  0x70000000
348c2ecf20Sopenharmony_ci#define PT_HIPROC  0x7fffffff
358c2ecf20Sopenharmony_ci#define PT_GNU_EH_FRAME		0x6474e550
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define PT_GNU_STACK	(PT_LOOS + 0x474e551)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* These constants define the different elf file types */
408c2ecf20Sopenharmony_ci#define ET_NONE   0
418c2ecf20Sopenharmony_ci#define ET_REL    1
428c2ecf20Sopenharmony_ci#define ET_EXEC   2
438c2ecf20Sopenharmony_ci#define ET_DYN    3
448c2ecf20Sopenharmony_ci#define ET_CORE   4
458c2ecf20Sopenharmony_ci#define ET_LOPROC 0xff00
468c2ecf20Sopenharmony_ci#define ET_HIPROC 0xffff
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* These constants define the various ELF target machines */
498c2ecf20Sopenharmony_ci#define EM_NONE  0
508c2ecf20Sopenharmony_ci#define EM_PPC	       20	/* PowerPC */
518c2ecf20Sopenharmony_ci#define EM_PPC64       21	/* PowerPC64 */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define EI_NIDENT	16
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_citypedef struct elf32_hdr {
568c2ecf20Sopenharmony_ci	unsigned char e_ident[EI_NIDENT];
578c2ecf20Sopenharmony_ci	Elf32_Half e_type;
588c2ecf20Sopenharmony_ci	Elf32_Half e_machine;
598c2ecf20Sopenharmony_ci	Elf32_Word e_version;
608c2ecf20Sopenharmony_ci	Elf32_Addr e_entry;	/* Entry point */
618c2ecf20Sopenharmony_ci	Elf32_Off e_phoff;
628c2ecf20Sopenharmony_ci	Elf32_Off e_shoff;
638c2ecf20Sopenharmony_ci	Elf32_Word e_flags;
648c2ecf20Sopenharmony_ci	Elf32_Half e_ehsize;
658c2ecf20Sopenharmony_ci	Elf32_Half e_phentsize;
668c2ecf20Sopenharmony_ci	Elf32_Half e_phnum;
678c2ecf20Sopenharmony_ci	Elf32_Half e_shentsize;
688c2ecf20Sopenharmony_ci	Elf32_Half e_shnum;
698c2ecf20Sopenharmony_ci	Elf32_Half e_shstrndx;
708c2ecf20Sopenharmony_ci} Elf32_Ehdr;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_citypedef struct elf64_hdr {
738c2ecf20Sopenharmony_ci	unsigned char e_ident[16];	/* ELF "magic number" */
748c2ecf20Sopenharmony_ci	Elf64_Half e_type;
758c2ecf20Sopenharmony_ci	Elf64_Half e_machine;
768c2ecf20Sopenharmony_ci	Elf64_Word e_version;
778c2ecf20Sopenharmony_ci	Elf64_Addr e_entry;	/* Entry point virtual address */
788c2ecf20Sopenharmony_ci	Elf64_Off e_phoff;	/* Program header table file offset */
798c2ecf20Sopenharmony_ci	Elf64_Off e_shoff;	/* Section header table file offset */
808c2ecf20Sopenharmony_ci	Elf64_Word e_flags;
818c2ecf20Sopenharmony_ci	Elf64_Half e_ehsize;
828c2ecf20Sopenharmony_ci	Elf64_Half e_phentsize;
838c2ecf20Sopenharmony_ci	Elf64_Half e_phnum;
848c2ecf20Sopenharmony_ci	Elf64_Half e_shentsize;
858c2ecf20Sopenharmony_ci	Elf64_Half e_shnum;
868c2ecf20Sopenharmony_ci	Elf64_Half e_shstrndx;
878c2ecf20Sopenharmony_ci} Elf64_Ehdr;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* These constants define the permissions on sections in the program
908c2ecf20Sopenharmony_ci   header, p_flags. */
918c2ecf20Sopenharmony_ci#define PF_R		0x4
928c2ecf20Sopenharmony_ci#define PF_W		0x2
938c2ecf20Sopenharmony_ci#define PF_X		0x1
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_citypedef struct elf32_phdr {
968c2ecf20Sopenharmony_ci	Elf32_Word p_type;
978c2ecf20Sopenharmony_ci	Elf32_Off p_offset;
988c2ecf20Sopenharmony_ci	Elf32_Addr p_vaddr;
998c2ecf20Sopenharmony_ci	Elf32_Addr p_paddr;
1008c2ecf20Sopenharmony_ci	Elf32_Word p_filesz;
1018c2ecf20Sopenharmony_ci	Elf32_Word p_memsz;
1028c2ecf20Sopenharmony_ci	Elf32_Word p_flags;
1038c2ecf20Sopenharmony_ci	Elf32_Word p_align;
1048c2ecf20Sopenharmony_ci} Elf32_Phdr;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_citypedef struct elf64_phdr {
1078c2ecf20Sopenharmony_ci	Elf64_Word p_type;
1088c2ecf20Sopenharmony_ci	Elf64_Word p_flags;
1098c2ecf20Sopenharmony_ci	Elf64_Off p_offset;	/* Segment file offset */
1108c2ecf20Sopenharmony_ci	Elf64_Addr p_vaddr;	/* Segment virtual address */
1118c2ecf20Sopenharmony_ci	Elf64_Addr p_paddr;	/* Segment physical address */
1128c2ecf20Sopenharmony_ci	Elf64_Xword p_filesz;	/* Segment size in file */
1138c2ecf20Sopenharmony_ci	Elf64_Xword p_memsz;	/* Segment size in memory */
1148c2ecf20Sopenharmony_ci	Elf64_Xword p_align;	/* Segment alignment, file & memory */
1158c2ecf20Sopenharmony_ci} Elf64_Phdr;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define	EI_MAG0		0	/* e_ident[] indexes */
1188c2ecf20Sopenharmony_ci#define	EI_MAG1		1
1198c2ecf20Sopenharmony_ci#define	EI_MAG2		2
1208c2ecf20Sopenharmony_ci#define	EI_MAG3		3
1218c2ecf20Sopenharmony_ci#define	EI_CLASS	4
1228c2ecf20Sopenharmony_ci#define	EI_DATA		5
1238c2ecf20Sopenharmony_ci#define	EI_VERSION	6
1248c2ecf20Sopenharmony_ci#define	EI_OSABI	7
1258c2ecf20Sopenharmony_ci#define	EI_PAD		8
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define	ELFMAG0		0x7f	/* EI_MAG */
1288c2ecf20Sopenharmony_ci#define	ELFMAG1		'E'
1298c2ecf20Sopenharmony_ci#define	ELFMAG2		'L'
1308c2ecf20Sopenharmony_ci#define	ELFMAG3		'F'
1318c2ecf20Sopenharmony_ci#define	ELFMAG		"\177ELF"
1328c2ecf20Sopenharmony_ci#define	SELFMAG		4
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define	ELFCLASSNONE	0	/* EI_CLASS */
1358c2ecf20Sopenharmony_ci#define	ELFCLASS32	1
1368c2ecf20Sopenharmony_ci#define	ELFCLASS64	2
1378c2ecf20Sopenharmony_ci#define	ELFCLASSNUM	3
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define ELFDATANONE	0	/* e_ident[EI_DATA] */
1408c2ecf20Sopenharmony_ci#define ELFDATA2LSB	1
1418c2ecf20Sopenharmony_ci#define ELFDATA2MSB	2
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define EV_NONE		0	/* e_version, EI_VERSION */
1448c2ecf20Sopenharmony_ci#define EV_CURRENT	1
1458c2ecf20Sopenharmony_ci#define EV_NUM		2
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define ELFOSABI_NONE	0
1488c2ecf20Sopenharmony_ci#define ELFOSABI_LINUX	3
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistruct elf_info {
1518c2ecf20Sopenharmony_ci	unsigned long loadsize;
1528c2ecf20Sopenharmony_ci	unsigned long memsize;
1538c2ecf20Sopenharmony_ci	unsigned long elfoffset;
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ciint parse_elf64(void *hdr, struct elf_info *info);
1568c2ecf20Sopenharmony_ciint parse_elf32(void *hdr, struct elf_info *info);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#endif				/* _PPC_BOOT_ELF_H_ */
159