-
Notifications
You must be signed in to change notification settings - Fork 0
/
eux_readelf.c
186 lines (162 loc) · 4.98 KB
/
eux_readelf.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <elf.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <locale.h>
#include <assert.h>
#include <stdarg.h>
#include "color.h"
#include "error.h"
#include "eux_readelf_cmdline.h"
#include "symtab.h"
#include "strtab.h"
#include "section.h"
#include "program.h"
#include "i18n.h"
#include "elf.h"
#include "reloc.h"
#include "dyn.h"
static void eux_init_locale_settings()
{
setlocale(LC_MESSAGES, "");
setlocale(LC_TIME, "");
textdomain(DOMAIN);
bind_textdomain_codeset(DOMAIN, "UTF-8");
}
static void eux64_print_memory(struct eux_args_info *info, Elf64_Ehdr *ehdr)
{
if (info->file_header_given) {
eux64_elf_print_header(ehdr);
}
Elf64_Phdr *phdr = (Elf64_Phdr*) (((uint8_t*)ehdr) + ehdr->e_phoff);
Elf64_Shdr *shdr = (Elf64_Shdr*) (((uint8_t*)ehdr) + ehdr->e_shoff);
if (info->program_headers_given) {
if ( ehdr->e_phoff != 0 ) {
eux64_program_print_header(ehdr, phdr,
ehdr->e_phnum, ehdr->e_phentsize);
} else {
fprintf(stderr, _("No program header in this file.\n"));
}
}
if (info->section_headers_given) {
if (ehdr->e_shoff != 0) {
eux64_section_print_header(ehdr, shdr,
ehdr->e_shnum, ehdr->e_shentsize);
} else {
fprintf(stderr, _("No section header in this file.\n"));
}
}
if (info->symbols_given) {
Elf64_Shdr *symtab_hdr = eux64_sym_get_symtab_hdr(ehdr, shdr);
Elf64_Shdr *sym_strtab_hdr = eux64_sym_get_strtab_hdr(ehdr, shdr);
if ( symtab_hdr != NULL ) {
eux64_sym_print_symtab(ehdr, shdr, symtab_hdr, sym_strtab_hdr);
} else {
fprintf(stderr, _("No symbol table in this file.\n"));
}
}
if (info->dyn_syms_given) {
Elf64_Shdr *dynsym_hdr = eux64_sym_get_dynsym_hdr(ehdr, shdr);
Elf64_Shdr *dynstr_hdr = eux64_sym_get_dynstr_hdr(ehdr, shdr);
if ( dynsym_hdr != NULL ) {
eux64_sym_print_symtab(ehdr, shdr, dynsym_hdr, dynstr_hdr);
} else {
fprintf(stderr, _("No dynamic symbol table in this file.\n"));
}
}
if (info->relocs_given) {
eux64_reloc_print_all_relocations(ehdr, shdr);
}
if (info->dynamic_given) {
eux64_dyn_print_all_tags(ehdr, shdr);
}
if (info->string_tables_given) {
eux64_str_print_all_tables(ehdr, shdr);
}
}
static void eux_print_filename(struct eux_args_info *info, const char *filename)
{
int err;
struct stat st;
err = stat(filename, &st);
if (err != 0) {
eux_error("'%s': %s\n", filename, strerror(errno));
return;
}
if (st.st_size < (long)sizeof(Elf32_Ehdr)) {
eux_error(_("Invalid file format: '%s' have not the ELF file format\n"), filename);
return;
}
int fd = open(filename, 0);
if (fd < 0) {
eux_fatal_error(_("Cannot open '%s': %s\n"), filename, strerror(errno));
}
void *addr;
addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
eux_fatal_error(_("Cannot mmap '%s': %s\n"), filename, strerror(errno));
}
close(fd);
uint8_t *ptr = addr;
if ( ptr[EI_MAG0] != ELFMAG0
|| ptr[EI_MAG1] != ELFMAG1
|| ptr[EI_MAG2] != ELFMAG2
|| ptr[EI_MAG3] != ELFMAG3 ) {
eux_error(_("Invalid file format: '%s' (Invalid ELF magic bytes)\n"), filename);
return;
}
if ( ptr[EI_CLASS] == ELFCLASS32 ) {
eux_error(_("Elf 32bit not handled!\n"));
return;
} else if ( ptr[EI_CLASS] == ELFCLASS64 ) {
// printf("Elf 64bit!\n");
eux64_print_memory(info, addr);
} else {
eux_error(_("Elf NOCLASS not handled!\n"));
return;
}
err = munmap(addr, st.st_size);
if (err != 0) {
eux_fatal_error(_("Cannot munmap '%s': %s\n"), filename, strerror(errno));
}
}
static void update_args_info(struct eux_args_info *info)
{
if (info->all_given) {
info->headers_given = 1;
info->symbols_given = 1;
info->relocs_given = 1;
info->dynamic_given = 1;
}
if (info->headers_given) {
info->file_header_given = 1;
info->program_headers_given = 1;
info->section_headers_given = 1;
}
}
int main(int argc, char *argv[])
{
eux_init_locale_settings();
struct eux_args_info info;
eux_cmdline_parser(argc, argv, &info);
update_args_info(&info);
if (info.help_given || info.inputs_num == 0) {
eux_cmdline_parser_print_help();
exit(EXIT_FAILURE);
} else if (info.version_given) {
eux_cmdline_parser_print_version();
exit(EXIT_SUCCESS);
}
for (unsigned i = 0; i < info.inputs_num; ++i) {
eux_print_filename(&info, info.inputs[i]);
}
return 0;
}