Skip to content

Commit 9d8ae9f

Browse files
authored
Merge pull request #25 from johnthesecond/master
Small changes:
2 parents 0cd8148 + ae59256 commit 9d8ae9f

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

src/parse_elf.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,51 +67,45 @@ int has_elf_magic_bytes(File_Info *fi)
6767
// Wrong with it
6868
// Check the size of the file first
6969
// Get rid of the endian stuff
70-
// Use memcpy
7170
// Get the architecture from the ELF headers
7271
/* ============================ TODO ============================== */
73-
const int magic_size = 5;
72+
const int magic_size = 4;
7473

75-
unsigned char values[5] = {0x00, 0x00, 0x00, 0x00};
76-
unsigned char little_endian[4] = {0x45, 0x7f, 0x46, 0x4c};
77-
unsigned char big_endian[4] = {0x7f, 0x45, 0x4c, 0x46};
74+
unsigned char values[magic_size + 1] = {0x00, 0x00, 0x00, 0x00};
75+
const unsigned char little_endian[magic_size] = {0x45, 0x7f, 0x46, 0x4c};
76+
const unsigned char big_endian[magic_size] = {0x7f, 0x45, 0x4c, 0x46};
7877

7978
FILE *fp;
8079
bool little_found, big_found;
80+
size_t bytes_read;
8181

82-
little_found = big_found = true;
82+
little_found = big_found = false;
8383

8484
fp = fopen(fi->location, "rb");
85-
if (fp == NULL)
85+
if (fp == NULL) {
8686
return 0;
87+
}
8788

88-
fread(values, 1, magic_size, fp);
89+
bytes_read = fread(values, 1, magic_size + 1, fp);
8990
fclose(fp);
9091

91-
// TODO USE memcpy
92-
for (int i = 0; i < magic_size - 1; i++)
93-
{
94-
if (little_endian[i] != values[i])
95-
{
96-
little_found = false;
97-
break;
98-
}
92+
if (magic_size + 1 != bytes_read) {
93+
return 0;
9994
}
10095

101-
for (int i = 0; i < magic_size - 1; i++)
102-
{
103-
if (big_endian[i] != values[i])
104-
{
105-
big_found = false;
106-
break;
107-
}
96+
if (!memcmp(values, little_endian)) {
97+
little_found = true;
98+
}
99+
if (!memcmp(values, big_endian)) {
100+
big_found = true;
108101
}
102+
109103
if (little_found || big_found)
110104
{
111-
if (values[4] == ELFCLASS32)
105+
if (values[magic_size] == ELFCLASS32)
112106
return X86;
113107

114-
else if (values[4] == ELFCLASS64)
108+
if (values[magic_size] == ELFCLASS64)
115109
return X64;
116110
}
117111
return NOT_ELF;
@@ -234,7 +228,6 @@ Tag_Array *search_dynamic_for_value(Elf_File *elf, Tag tag)
234228
{
235229
int number_of_elements = 0; /* Total number of tags */
236230
int number_of_findings = 0; /* Total number of matching tags */
237-
int current_findings = 0;
238231

239232
/* Make sure that the Elf_File has been parsed */
240233
if (elf->dynamic_strings == 0 || elf->dynamic_header == NULL)
@@ -245,15 +238,16 @@ Tag_Array *search_dynamic_for_value(Elf_File *elf, Tag tag)
245238
/* ============================ TODO ============================== */
246239

247240
/* We search twice so we need two pointers */
248-
Elf_Internal_Dyn *entry = elf->dynamic_header->p_offset + elf->address;
249-
Elf_Internal_Dyn *entry2 = elf->dynamic_header->p_offset + elf->address;
241+
Elf_Internal_Dyn *entry = Elf_Internal_Dyn* (elf->dynamic_header->p_offset + (char*)(elf->address));
242+
Elf_Internal_Dyn *entry2 = entry;
243+
Elf_Internal_Dyn *entry_end = Elf_Internal_Dyn* (elf->dynamic_header->p_filesz + (char*)(entry));
250244

251245
/* ============================ TODO ============================== */
252246
/* This function could fail is no DT_NULL is found (corrupted elf) */
253247
/* ============================ TODO ============================== */
254248

255249
/* Loop through the dynamic section until we find DT_NULL, this signifies that we've reached the end */
256-
for (; (char *)(entry + 2) <= (char *)(elf->dynamic_header->p_offset + elf->address + elf->dynamic_header->p_filesz); entry++)
250+
for (; entry + 2 <= entry_end; entry++)
257251
{
258252
/* Current tag is equal to the search value */
259253
if (entry->d_tag == tag)
@@ -286,9 +280,9 @@ Tag_Array *search_dynamic_for_value(Elf_File *elf, Tag tag)
286280
{
287281
if (entry2->d_tag == tag)
288282
{
289-
findings[current_findings].tag_value = elf->address + elf->dynamic_strings + entry2->d_un.d_ptr;
290-
current_findings++;
283+
findings[i].tag_value = elf->address + elf->dynamic_strings + entry2->d_un.d_ptr;
291284
}
285+
// John: Think there should be a: [if (entry2+1 > entry_end){break;}] here, but am not sure.
292286
entry2++;
293287
}
294288
return findings;
@@ -302,8 +296,12 @@ Tag_Array *search_dynamic_for_value(Elf_File *elf, Tag tag)
302296
*/
303297
void close_elf(Elf_File *elf, File_Info *fi)
304298
{
305-
munmap((void *)elf->address, fi->stat->st_size);
306-
free(elf);
299+
if (elf) {
300+
if (MAP_FAILED != elf->address) {
301+
munmap((void *)elf->address, fi->stat->st_size);
302+
}
303+
free(elf);
304+
}
307305
}
308306

309307
/* ============================ STATIC FUNCTIONS ============================== */

0 commit comments

Comments
 (0)