-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.c
89 lines (77 loc) · 2.36 KB
/
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
/*
** header.c
**
** Read header and store info in exec struct.
**
** Also, provide a function to print Header information
**
** Author: Jiaqi Gu ([email protected])
*/
#define _BSD_SOURCE
#include "endian.h"
#include <stdio.h> //standard input/output
#include <stdlib.h> //standard c library
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <stdint.h>
#include "header.h"
/// print data info in header
///
/// @param void
/// @returns 0 if no error
int printData()
{
if(header.data[0]!=0){printf("Section text is %d bytes long\n",header.data[0]);}
if(header.data[1]!=0){printf("Section rdata is %d bytes long\n",header.data[1]);}
if(header.data[2]!=0){printf("Section data is %d bytes long\n",header.data[2]);}
if(header.data[3]!=0){printf("Section sdata is %d bytes long\n",header.data[3]);}
if(header.data[4]!=0){printf("Section sbss is %d bytes long\n",header.data[4]);}
if(header.data[5]!=0){printf("Section bss is %d bytes long\n",header.data[5]);}
if(header.data[6]!=0){printf("Section reltab is %d entries long\n",header.data[6]);}
if(header.data[7]!=0){printf("Section reftab is %d entries long\n",header.data[7]);}
if(header.data[8]!=0){printf("Section symtab is %d entries long\n",header.data[8]);}
if(header.data[9]!=0){printf("Section strings is %d bytes long\n",header.data[9]);}
return 0;
}
/// initialize header struct
///
/// @param void
/// @returns 0 if no error
int iniheader()
{
header.magic = 0;
header.version = 0;
header.flags = 0;
header.entry = 0;
for(int i=0;i<10;++i)
{
header.data[i]=0;
}
return 0;
}
/// a function to analysis Header
///
/// @param char* file
/// @returns 0 if no error
int HeaderAnalysis(char* file){
fread(&(header.magic),2,1,pFile);
header.magic = be16toh(header.magic);
if(header.magic!=HDR_MAGIC){
fprintf(stderr, "error: %s is not an R2K object module (magic number 0x%08x)\n", file, header.magic);
return 1;
}
/// get data for version flags and entry
fread(&(header.version),2,1,pFile);
header.version = be16toh(header.version);
fread(&(header.flags),4,1,pFile);
header.flags = be32toh(header.flags);
fread(&(header.entry),4,1,pFile);
header.entry = be32toh(header.entry);
for(int i=0;i<10;++i)
{
fread(&(header.data[i]),4,1,pFile);
header.data[i]=be32toh(header.data[i]);
}
return 0;
}