-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-elf_header.c
246 lines (205 loc) · 3.98 KB
/
100-elf_header.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <elf.h>
/**
* print_addr - prints address
* @ptr: magic.
* Return: no return.
*/
void print_addr(char *ptr)
{
int i;
int begin;
char sys;
printf(" Entry point address: 0x");
sys = ptr[4] + '0';
if (sys == '1')
{
begin = 26;
printf("80");
for (i = begin; i >= 22; i--)
{
if (ptr[i] > 0)
printf("%x", ptr[i]);
else if (ptr[i] < 0)
printf("%x", 256 + ptr[i]);
}
if (ptr[7] == 6)
printf("00");
}
if (sys == '2')
{
begin = 26;
for (i = begin; i > 23; i--)
{
if (ptr[i] >= 0)
printf("%02x", ptr[i]);
else if (ptr[i] < 0)
printf("%02x", 256 + ptr[i]);
}
}
printf("\n");
}
/**
* print_type - prints type
* @ptr: magic.
* Return: no return.
*/
void print_type(char *ptr)
{
char type = ptr[16];
if (ptr[5] == 1)
type = ptr[16];
else
type = ptr[17];
printf(" Type: ");
if (type == 0)
printf("NONE (No file type)\n");
else if (type == 1)
printf("REL (Relocatable file)\n");
else if (type == 2)
printf("EXEC (Executable file)\n");
else if (type == 3)
printf("DYN (Shared object file)\n");
else if (type == 4)
printf("CORE (Core file)\n");
else
printf("<unknown: %x>\n", type);
}
/**
* print_osabi - prints osabi
* @ptr: magic.
* Return: no return.
*/
void print_osabi(char *ptr)
{
char osabi = ptr[7];
printf(" OS/ABI: ");
if (osabi == 0)
printf("UNIX - System V\n");
else if (osabi == 2)
printf("UNIX - NetBSD\n");
else if (osabi == 6)
printf("UNIX - Solaris\n");
else
printf("<unknown: %x>\n", osabi);
printf(" ABI Version: %d\n", ptr[8]);
}
/**
* print_version - prints version
* @ptr: magic.
* Return: no return.
*/
void print_version(char *ptr)
{
int version = ptr[6];
printf(" Version: %d", version);
if (version == EV_CURRENT)
printf(" (current)");
printf("\n");
}
/**
* print_data - prints data
* @ptr: magic.
* Return: no return.
*/
void print_data(char *ptr)
{
char data = ptr[5];
printf(" Data: 2's complement");
if (data == 1)
printf(", little endian\n");
if (data == 2)
printf(", big endian\n");
}
/**
* print_magic - prints magic info.
* @ptr: magic.
* Return: no return.
*/
void print_magic(char *ptr)
{
int bytes;
printf(" Magic: ");
for (bytes = 0; bytes < 16; bytes++)
printf(" %02x", ptr[bytes]);
printf("\n");
}
/**
* check_sys - check the version system.
* @ptr: magic.
* Return: no return.
*/
void check_sys(char *ptr)
{
char sys = ptr[4] + '0';
if (sys == '0')
exit(98);
printf("ELF Header:\n");
print_magic(ptr);
if (sys == '1')
printf(" Class: ELF32\n");
if (sys == '2')
printf(" Class: ELF64\n");
print_data(ptr);
print_version(ptr);
print_osabi(ptr);
print_type(ptr);
print_addr(ptr);
}
/**
* check_elf - check if it is an elf file.
* @ptr: magic.
* Return: 1 if it is an elf file. 0 if not.
*/
int check_elf(char *ptr)
{
int addr = (int)ptr[0];
char E = ptr[1];
char L = ptr[2];
char F = ptr[3];
if (addr == 127 && E == 'E' && L == 'L' && F == 'F')
return (1);
return (0);
}
/**
* main - check the code for Holberton School students.
* @argc: number of arguments.
* @argv: arguments vector.
* Return: Always 0.
*/
int main(int argc, char *argv[])
{
int fd, ret_read;
char ptr[27];
if (argc != 2)
{
dprintf(STDERR_FILENO, "Usage: elf_header elf_filename\n");
exit(98);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
dprintf(STDERR_FILENO, "Err: file can not be open\n");
exit(98);
}
lseek(fd, 0, SEEK_SET);
ret_read = read(fd, ptr, 27);
if (ret_read == -1)
{
dprintf(STDERR_FILENO, "Err: The file can not be read\n");
exit(98);
}
if (!check_elf(ptr))
{
dprintf(STDERR_FILENO, "Err: It is not an ELF\n");
exit(98);
}
check_sys(ptr);
close(fd);
return (0);
}