-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-section-content.c
More file actions
101 lines (82 loc) · 2.34 KB
/
read-section-content.c
File metadata and controls
101 lines (82 loc) · 2.34 KB
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
#include <stdlib.h>
#include <string.h>
#include "elf_header.h"
#include "elf_section_table.h"
#include "elf_section_content.h"
typedef enum {
SELSECTION_OPT_NONE,
SELSECTION_OPT_NAME,
SELSECTION_OPT_NUM
} SelSection_Option;
SelSection_Option get_option(int argc, char *argv[], int *argopt, int *argfile){
SelSection_Option opt = SELSECTION_OPT_NONE;
for (int i=1; i < argc-1; i++){
if (!strcmp(argv[i], "-s")){
*argopt = i + 1;
opt = SELSECTION_OPT_NAME;
} else if (!strcmp(argv[i], "-i")) {
*argopt = i + 1;
opt = SELSECTION_OPT_NUM;
}
}
*argfile = (*argopt == 2) ? 3 : 1;
return opt;
}
int main(int argc, char *argv[]){
if (argc != 4){
printf("Format : %s <nom du fichier> -<s|i> <section>\n", argv[0]);
return -1;
}
int sel_num = -1;
int sel_file = -1;
SelSection_Option opt = get_option(argc, argv, &sel_num, &sel_file);
if (opt == SELSECTION_OPT_NONE){
printf("Option manquante :\n");
printf("-s : section donnée par son nom\n");
printf("-i : section donnée par son numéro\n");
return -1;
}
FILE *f = fopen(argv[sel_file], "r");
if (f == NULL){
printf("Impossible d'ouvrir le fichier \"%s\".\n", argv[sel_file]);
return -1;
}
Elf32_Ehdr hdr;
Err_ELF_Header err_hdr = read_elf_header(f, &hdr);
if (err_hdr != ERR_EH_NONE){
printf("Erreur de lecture du header : %s\n.", str_Err_ELF_Header(err_hdr));
fclose(f);
return -1;
}
Elf32_Shdr sh_tab[hdr.e_shnum];
read_elf_section_table(f, &hdr, sh_tab);
int num = -1;
if (opt == SELSECTION_OPT_NAME){
unsigned char* string_table = read_elf_section_content(f, sh_tab[hdr.e_shstrndx]);
if (string_table == NULL){
printf("Erreur de lecture de la string table.\n");
fclose(f);
return -1;
}
num = search_elf_section_num(hdr.e_shnum, sh_tab, argv[sel_num], string_table);
free(string_table);
} else {
num = atoi(argv[sel_num]);
}
if (num < 0 || hdr.e_shnum <= num){
printf("La section n'existe pas.\n");
fclose(f);
return -1;
}
unsigned char *content = NULL;
content = read_elf_section_content(f, sh_tab[num]);
if (content == NULL){
printf("Erreur de lecture du contenu de section.\n");
fclose(f);
return -1;
}
display_elf_section_content(content, sh_tab[num].sh_size);
free(content);
fclose(f);
return 0;
}