forked from AppImage/AppImageKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetsection.c
86 lines (80 loc) · 2.59 KB
/
getsection.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
#include <elf.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
/* Return the offset, and the length of an ELF section with a given name in a given ELF file */
int get_elf_section_offset_and_lenghth(char* fname, char* section_name, unsigned long *offset, unsigned long *length)
{
uint8_t *data;
int i;
int fd = open(fname, O_RDONLY);
data = mmap(NULL, lseek(fd, 0, SEEK_END), PROT_READ, MAP_SHARED, fd, 0);
#ifdef __i386__
Elf32_Ehdr *elf;
Elf32_Shdr *shdr;
elf = (Elf32_Ehdr *) data;
shdr = (Elf32_Shdr *) (data + elf->e_shoff);
#else // Default to x86_64
Elf64_Ehdr *elf;
Elf64_Shdr *shdr;
elf = (Elf64_Ehdr *) data;
shdr = (Elf64_Shdr *) (data + elf->e_shoff);
#endif
char *strTab = (char *)(data + shdr[elf->e_shstrndx].sh_offset);
for(i = 0; i < elf->e_shnum; i++) {
if(strcmp(&strTab[shdr[i].sh_name], section_name) == 0) {
*offset = shdr[i].sh_offset;
*length = shdr[i].sh_size;
}
}
close(fd);
return(0);
}
void print_hex(char* fname, unsigned long offset, unsigned long length){
uint8_t *data;
unsigned long k;
int fd = open(fname, O_RDONLY);
data = mmap(NULL, lseek(fd, 0, SEEK_END), PROT_READ, MAP_SHARED, fd, 0);
close(fd);
for (k = offset; k < offset + length; k++) {
printf("%x", data[k]);
}
printf("\n");
}
void print_binary(char* fname, unsigned long offset, unsigned long length){
uint8_t *data;
unsigned long k;
int fd = open(fname, O_RDONLY);
data = mmap(NULL, lseek(fd, 0, SEEK_END), PROT_READ, MAP_SHARED, fd, 0);
close(fd);
for (k = offset; k < offset + length; k++) {
printf("%c", data[k]);
}
printf("\n");
}
/*
int main(int ac, char **av)
{
unsigned long offset = 0;
unsigned long length = 0; // Where the function will store the result
char* segment_name;
segment_name = ".upd_info";
int res = get_elf_section_offset_and_lenghth(av[1], segment_name, &offset, &length);
printf("segment_name: %s\n", segment_name);
printf("offset: %lu\n", offset);
printf("length: %lu\n", length);
print_hex(av[1], offset, length);
print_binary(av[1], offset, length);
segment_name = ".sha256_sig";
printf("segment_name: %s\n", segment_name);
res = get_elf_section_offset_and_lenghth(av[1], segment_name, &offset, &length);
printf("offset: %lu\n", offset);
printf("length: %lu\n", length);
print_hex(av[1], offset, length);
print_binary(av[1], offset, length);
return res;
}
*/