PS2SDK
PS2 Homebrew Libraries
elftypes.h
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2022, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
11 #ifndef __ELF_TYPES_H__
12 #define __ELF_TYPES_H__
13 
14 #include "types.h"
15 
16 #define ELF_MACHINE_MIPS 0x0008
17 #define ELF_SH_STRTAB ".shstrtab"
18 
19 #define ELF_SECT_MAX_NAME 128
20 
21 /* Structure defining a single elf section */
22 struct ElfSection
23 {
24  /* Name index */
25  uint32_t iName;
26  /* Type of section */
27  uint32_t iType;
28  /* Section flags */
29  uint32_t iFlags;
30  /* Addr of section when loaded */
31  uint32_t iAddr;
32  /* Offset of the section in the elf */
33  uint32_t iOffset;
34  /* Size of the sections data */
35  uint32_t iSize;
36  /* Link info */
37  uint32_t iLink;
38  /* Info */
39  uint32_t iInfo;
40  /* Address alignment */
41  uint32_t iAddralign;
42  /* Entry size */
43  uint32_t iEntsize;
44 
45  /* Aliased pointer to the data (in the original Elf) */
46  uint8_t *pData;
47  /* Name of the section */
48  char szName[ELF_SECT_MAX_NAME];
49  /* Index */
50  int iIndex;
51  /* Section Ref. Used for relocations */
52  struct ElfSection *pRef;
53  /* Indicates if this section is to be outputted */
54  int blOutput;
55 };
56 
57 struct ElfProgram
58 {
59  uint32_t iType;
60  uint32_t iOffset;
61  uint32_t iVaddr;
62  uint32_t iPaddr;
63  uint32_t iFilesz;
64  uint32_t iMemsz;
65  uint32_t iFlags;
66  uint32_t iAlign;
67 
68  /* Aliased pointer to the data (in the original Elf)*/
69  uint8_t *pData;
70 };
71 
72 /* Structure to hold elf header data, in native format */
73 struct ElfHeader
74 {
75  uint32_t iMagic;
76  uint32_t iClass;
77  uint32_t iData;
78  uint32_t iIdver;
79  uint32_t iType;
80  uint32_t iMachine;
81  uint32_t iVersion;
82  uint32_t iEntry;
83  uint32_t iPhoff;
84  uint32_t iShoff;
85  uint32_t iFlags;
86  uint32_t iEhsize;
87  uint32_t iPhentsize;
88  uint32_t iPhnum;
89  uint32_t iShentsize;
90  uint32_t iShnum;
91  uint32_t iShstrndx;
92 };
93 
94 struct ElfReloc
95 {
96  /* Pointer to the section name */
97  const char* secname;
98  /* Base address */
99  uint32_t base;
100  /* Type */
101  uint32_t type;
102  /* Symbol (if known) */
103  uint32_t symbol;
104  /* Offset into the file */
105  uint32_t offset;
106  /* New Address for the relocation (to do with what you will) */
107  uint32_t addr;
108 };
109 
110 /* Define ELF types */
111 typedef uint32_t Elf32_Addr;
112 typedef uint16_t Elf32_Half;
113 typedef uint32_t Elf32_Off;
114 typedef int32_t Elf32_Sword;
115 typedef uint32_t Elf32_Word;
116 
117 #define ELF_MAGIC 0x464C457F
118 
119 #define ELF_EXEC_TYPE 0x0002
120 #define ELF_IRX_TYPE 0xFF80
121 
122 #define SHT_NULL 0
123 #define SHT_PROGBITS 1
124 #define SHT_SYMTAB 2
125 #define SHT_STRTAB 3
126 #define SHT_RELA 4
127 #define SHT_HASH 5
128 #define SHT_DYNAMIC 6
129 #define SHT_NOTE 7
130 #define SHT_NOBITS 8
131 #define SHT_REL 9
132 #define SHT_SHLIB 10
133 #define SHT_DYNSYM 11
134 #define SHT_LOPROC 0x70000000
135 #define SHT_HIPROC 0x7fffffff
136 #define SHT_LOUSER 0x80000000
137 #define SHT_HIUSER 0xffffffff
138 
139 #define SHT_LOPROC_EE_IMPORT_TAB 0x90
140 #define SHT_LOPROC_IOPMOD 0x80
141 
142 // MIPS Reloc Entry Types
143 #define R_MIPS_NONE 0
144 #define R_MIPS_16 1
145 #define R_MIPS_32 2
146 #define R_MIPS_REL32 3
147 #define R_MIPS_26 4
148 #define R_MIPS_HI16 5
149 #define R_MIPS_LO16 6
150 #define R_MIPS_GPREL16 7
151 #define R_MIPS_LITERAL 8
152 #define R_MIPS_GOT16 9
153 #define R_MIPS_PC16 10
154 #define R_MIPS_CALL16 11
155 #define R_MIPS_GPREL32 12
156 
157 #define SHF_WRITE 1
158 #define SHF_ALLOC 2
159 #define SHF_EXECINSTR 4
160 
161 #define PT_NULL 0
162 #define PT_LOAD 1
163 #define PT_DYNAMIC 2
164 #define PT_INTERP 3
165 #define PT_NOTE 4
166 #define PT_SHLIB 5
167 #define PT_PHDR 6
168 #define PT_LOPROC 0x70000000
169 #define PT_HIPROC 0x7fffffff
170 
171 /* ELF file header */
172 typedef struct {
173  Elf32_Word e_magic;
174  uint8_t e_class;
175  uint8_t e_data;
176  uint8_t e_idver;
177  uint8_t e_pad[9];
178  Elf32_Half e_type;
179  Elf32_Half e_machine;
180  Elf32_Word e_version;
181  Elf32_Addr e_entry;
182  Elf32_Off e_phoff;
183  Elf32_Off e_shoff;
184  Elf32_Word e_flags;
185  Elf32_Half e_ehsize;
186  Elf32_Half e_phentsize;
187  Elf32_Half e_phnum;
188  Elf32_Half e_shentsize;
189  Elf32_Half e_shnum;
190  Elf32_Half e_shstrndx;
191 } __attribute__((packed)) Elf32_Ehdr;
192 
193 /* ELF section header */
194 typedef struct {
195  Elf32_Word sh_name;
196  Elf32_Word sh_type;
197  Elf32_Word sh_flags;
198  Elf32_Addr sh_addr;
199  Elf32_Off sh_offset;
200  Elf32_Word sh_size;
201  Elf32_Word sh_link;
202  Elf32_Word sh_info;
203  Elf32_Word sh_addralign;
204  Elf32_Word sh_entsize;
205 } __attribute__((packed)) Elf32_Shdr;
206 
207 typedef struct {
208  Elf32_Word p_type;
209  Elf32_Off p_offset;
210  Elf32_Addr p_vaddr;
211  Elf32_Addr p_paddr;
212  Elf32_Word p_filesz;
213  Elf32_Word p_memsz;
214  Elf32_Word p_flags;
215  Elf32_Word p_align;
216 } Elf32_Phdr;
217 
218 #define ELF32_R_SYM(i) ((i)>>8)
219 #define ELF32_R_TYPE(i) ((uint8_t)(i&0xFF))
220 
221 typedef struct {
222  Elf32_Addr r_offset;
223  Elf32_Word r_info;
224 } Elf32_Rel;
225 
226 typedef struct {
227  Elf32_Word st_name;
228  Elf32_Addr st_value;
229  Elf32_Word st_size;
230  unsigned char st_info;
231  unsigned char st_other;
232  Elf32_Half st_shndx;
233 } __attribute__((packed)) Elf32_Sym;
234 
235 #define STB_LOCAL 0
236 #define STB_GLOBAL 1
237 #define STB_WEAK 2
238 #define STB_LOPROC 13
239 #define STB_HIPROC 15
240 
241 #define ELF32_ST_BIND(i) ((i)>>4)
242 #define ELF32_ST_TYPE(i) ((i)&0xf)
243 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
244 
245 #endif
ElfProgram
Definition: elftypes.h:57
_Elf32_ehdr
Definition: srxfixup_internal.h:25
_Elf32_Shdr
Definition: srxfixup_internal.h:53
Elf32_Rel
Definition: elftypes.h:221
ElfReloc
Definition: elftypes.h:94
Elf32_Phdr
Definition: elftypes.h:207
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
_Elf32_Sym
Definition: srxfixup_internal.h:66
ElfSection
Definition: elftypes.h:22
ElfHeader
Definition: elftypes.h:73
symbol
Definition: main.c:241