Skip to content

Commit ae40609

Browse files
still need to fix dynamic linling string table
1 parent 6b59d47 commit ae40609

File tree

9 files changed

+395
-10
lines changed

9 files changed

+395
-10
lines changed

0x06-readelf/Makefile

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CC = gcc-4.8
2-
CFLAGS = -Wall -Werror -Wextra -pedantic -g
1+
# CC = gcc-4.8
2+
# CFLAGS = -Wall -Werror -Wextra -pedantic -g
33

44
_SRC = 0-hreadelf.c \
55
validate_elf.c \
@@ -25,12 +25,24 @@ _SRC2 = 2-hreadelf.c \
2525
process_elf_section_header.c \
2626
process_elf_program_header.c
2727

28+
_SRC3 = 100-hreadelf.c \
29+
validate_elf.c \
30+
elf_utils.c \
31+
elf_utils2.c \
32+
elf_utils3.c \
33+
elf_utils4.c \
34+
process_elf_header.c \
35+
process_elf_section_header.c \
36+
process_elf_symbol_tables.c
37+
2838
SRC = $(patsubst %,$(SDIR)/%,$(_SRC))
2939

3040
SRC1 = $(patsubst %,$(SDIR)/%,$(_SRC1))
3141

3242
SRC2 = $(patsubst %,$(SDIR)/%,$(_SRC2))
3343

44+
SRC3 = $(patsubst %,$(SDIR)/%,$(_SRC3))
45+
3446
_OBJ = $(_SRC:.c=.o)
3547
OBJECTS = $(patsubst %,$(ODIR)/%,$(_OBJ))
3648

@@ -40,6 +52,9 @@ OBJECTS1 = $(patsubst %,$(ODIR)/%,$(_OBJ1))
4052
_OBJ2 = $(_SRC2:.c=.o)
4153
OBJECTS2 = $(patsubst %,$(ODIR)/%,$(_OBJ2))
4254

55+
_OBJ3 = $(_SRC3:.c=.o)
56+
OBJECTS3 = $(patsubst %,$(ODIR)/%,$(_OBJ3))
57+
4358
_DEPS = readelf.h
4459
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
4560

@@ -50,11 +65,12 @@ ODIR = .
5065
OUTPUT = 0-hreadelf
5166
OUTPUT1 = 1-hreadelf
5267
OUTPUT2 = 2-hreadelf
68+
OUTPUT3 = 100-hreadelf
5369

5470
$(ODIR)/%.o : $(SDIR)/%.c
5571
$(CC) $(CFLAGS) -c -o $@ $< -I$(IDIR)
5672

57-
all : $(OUTPUT1) $(OUTPUT) $(OUTPUT2)
73+
all : $(OUTPUT1) $(OUTPUT) $(OUTPUT2) $(OUTPUT3)
5874

5975
$(OUTPUT) : $(OBJECTS)
6076
$(CC) -o $@ $^ $(LINKS)
@@ -64,9 +80,13 @@ $(OUTPUT1) : $(OBJECTS1)
6480

6581
$(OUTPUT2) : $(OBJECTS2)
6682
$(CC) -o $@ $^ $(LINKS)
83+
84+
$(OUTPUT3) : $(OBJECTS3)
85+
$(CC) -o $@ $^ $(LINKS)
86+
6787
.PHONY : clean
6888

6989
clean :
70-
rm -f $(OUTPUT) $(OUTPUT1) $(OBJECTS) $(OBJECTS1) $(OUTPUT2) $(OBJECTS2)
90+
rm -f $(OUTPUT) $(OUTPUT1) $(OUTPUT3) $(OBJECTS) $(OBJECTS1) $(OUTPUT2) $(OBJECTS2) $(OBJECTS3)
7191

72-
re: $(OBJ) $(OBJ1) $(OBJ2)
92+
re: $(OBJ) $(OBJ1) $(OBJ2) $(OBJ3)

0x06-readelf/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ Usage: 2-hreadelf elf_filename
1919
```
2020
standard output, error output and status are the exact same as readelf -W -l
2121

22+
100-hreadelf - a program that displays the information contained in the ELF symbol tables of an ELF file.
23+
```
24+
Usage: 100-hreadelf elf_filename
25+
```
26+
standard output, error output and status are the exact same as readelf -W -s
27+
2228
## Useful Resources:
2329
* man elf(5), man readelf(1)
2430
* See also /usr/include/elf.h

0x06-readelf/inc/readelf.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,30 @@ typedef struct phdr
111111
uint64_t p_align;
112112
} ElfN_Phdr;
113113

114+
/**
115+
* struct symtab- Symbol table entry
116+
* @st_name : Symbol name, index in string tbl
117+
* @st_info : Type and binding attributes
118+
* @st_other : No defined meaning, 0
119+
* @st_shndx : Associated section index
120+
* @st_value : Value of the symbol
121+
* @st_size : Associated symbol size
122+
*/
123+
typedef struct symtab
124+
{
125+
uint64_t st_name;
126+
unsigned char st_info;
127+
unsigned char st_other;
128+
uint16_t st_shndx;
129+
uint64_t st_value;
130+
uint64_t st_size;
131+
} ElfN_Sym;
132+
114133
#define SHF_X86_64_LARGE 0x10000000
115134
#define GET_BYTE(field) get_byte(field, sizeof(field))
116135
#define E "Error: Not an ELF file - it has the wrong magic bytes at the start"
117-
136+
#define ELF_ST_BIND(x) ((x) >> 4)
137+
#define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf)
118138
#define ELF_SECTION_SIZE(sec_hdr, segment) \
119139
(((sec_hdr.sh_flags & SHF_TLS) == 0 \
120140
|| sec_hdr.sh_type != SHT_NOBITS \
@@ -147,6 +167,10 @@ char *get_elf_class(unsigned int elf_class);
147167
char *get_section_type_name(unsigned int sh_type);
148168
char *get_elf_section_flags(ElfN_Ehdr elf_header, unsigned long sh_flags);
149169
char *get_segment_type(unsigned long p_type);
170+
char *get_symbol_binding(unsigned int binding);
171+
char *get_symbol_type(unsigned int type);
172+
char *get_symbol_visibility(unsigned int visibility);
173+
char *get_symbol_index_type(unsigned int type);
150174

151175
char *get_vma(unsigned int no, char format, char end);
152176
uint64_t get_byte_big_endian(uint64_t data, int size);
@@ -165,8 +189,13 @@ void read_elf_program_header_N(ElfN_Ehdr *ehdr, FILE *file, int arch);
165189
void read_elf_program_header_32(ElfN_Ehdr eh, ElfN_Phdr *phdr_tbl, int fd);
166190
void read_elf_program_header_64(ElfN_Ehdr eh, ElfN_Phdr *phdr_tbl, int fd);
167191

192+
void read_elf_symbol_table_N(ElfN_Ehdr *ehdr, FILE *file, int arch);
193+
168194
void print_elf_header(ElfN_Ehdr ehdr);
169195
void print_elf_section_header(ElfN_Shdr *shdr_tbl, ElfN_Ehdr eh, int fd);
170196
void print_elf_program_header(ElfN_Phdr ph_tbl[], ElfN_Shdr sh_tbl[],
171197
ElfN_Ehdr ehdr, FILE *file);
198+
void print_elf_symbol_table(ElfN_Shdr sh_tbl[], ElfN_Sym sym_tbl[],
199+
ElfN_Ehdr ehdr, int sym_tbl_indx, uint64_t count,
200+
int fd);
172201
#endif

0x06-readelf/src/1-hreadelf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* main - A program that takes the name of a elf file as a parameter and
5-
* displays the information contained in the ELF file header of an ELF file.
5+
* displays the information contained in section headers of an ELF file.
66
* @argc: no of inputs.
77
* @argv: inputs.
88
*

0x06-readelf/src/100-hreadelf.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "readelf.h"
2+
3+
/**
4+
* main - A program that takes the name of a elf file as a parameter and
5+
* displays the information contained in Symbol tables of an ELF file.
6+
* @argc: no of inputs.
7+
* @argv: inputs.
8+
*
9+
* Return: on success: 0 , on Failure: 1.
10+
*/
11+
int main(int argc, char *argv[])
12+
{
13+
ElfN_Ehdr ehdr;
14+
int exit_status = 0, arch = 0;
15+
FILE *file;
16+
17+
/* validate user input */
18+
if (argc < 2)
19+
{
20+
printf("Usage: %s elf_filename\n", argv[0]);
21+
exit(1);
22+
}
23+
/* open the elf file */
24+
file = fopen(argv[1], "rb");
25+
if (file)
26+
{
27+
if (fread(ehdr.e_ident, EI_NIDENT, 1, file) &&
28+
elf_check_file(ehdr.e_ident))
29+
{
30+
get_architecture(ehdr.e_ident[EI_CLASS], &arch);
31+
read_elf_symbol_table_N(&ehdr, file, arch);
32+
} else
33+
printf("%s: %s\n", E, argv[0]);
34+
exit_status = 1;
35+
/* finally close the file*/
36+
fclose(file);
37+
} else
38+
{
39+
printf("%s: Error: '%s': No such file\n", argv[0], argv[1]);
40+
exit_status = 1;
41+
}
42+
return (exit_status);
43+
}

0x06-readelf/src/2-hreadelf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* main - A program that takes the name of a elf file as a parameter and
5-
* displays the information contained in the ELF file header of an ELF file.
5+
* displays the information contained in the program header of an ELF file.
66
* @argc: no of inputs.
77
* @argv: inputs.
88
*

0x06-readelf/src/elf_utils4.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "readelf.h"
2+
3+
/**
4+
* get_symbol_type - gets the symbol type
5+
* @type: symbol type of elf
6+
* Return: symbol type of elf
7+
*/
8+
char *get_symbol_type(unsigned int type)
9+
{
10+
char buff[32];
11+
12+
switch (type)
13+
{
14+
case STT_NOTYPE:
15+
return ("NOTYPE");
16+
case STT_OBJECT:
17+
return ("OBJECT");
18+
case STT_FUNC:
19+
return ("FUNC");
20+
case STT_SECTION:
21+
return ("SECTION");
22+
case STT_FILE:
23+
return ("FILE");
24+
case STT_COMMON:
25+
return ("COMMON");
26+
case STT_TLS:
27+
return ("TLS");
28+
default:
29+
return ("NOTYPE");
30+
}
31+
}
32+
33+
/**
34+
* get_symbol_binding - gets the binding type
35+
* @binding: binding type of elf
36+
* Return: binding type of elf
37+
*/
38+
char *get_symbol_binding(unsigned int binding)
39+
{
40+
char buff[32];
41+
42+
switch (binding)
43+
{
44+
case STB_LOCAL:
45+
return ("LOCAL");
46+
case STB_GLOBAL:
47+
return ("GLOBAL");
48+
case STB_WEAK:
49+
return ("WEAK");
50+
default:
51+
snprintf(buff, sizeof(buff), ("<unknown>: %d"), binding);
52+
return (strdup(buff));
53+
}
54+
}
55+
56+
/**
57+
* get_symbol_visibility - gets the visibility type
58+
* @visibility: visibility type of elf
59+
* Return: visibility type of elf
60+
*/
61+
char *get_symbol_visibility(unsigned int visibility)
62+
{
63+
switch (visibility)
64+
{
65+
case STV_DEFAULT:
66+
return ("DEFAULT");
67+
case STV_INTERNAL:
68+
return ("INTERNAL");
69+
case STV_HIDDEN:
70+
return ("HIDDEN");
71+
case STV_PROTECTED:
72+
return ("PROTECTED");
73+
default:
74+
abort();
75+
}
76+
}
77+
78+
/**
79+
* get_symbol_index_type - gets the symbol index type
80+
* @type: type of symbol
81+
* Return: symbol index type
82+
*/
83+
char *get_symbol_index_type(unsigned int type)
84+
{
85+
char buff[32];
86+
87+
switch (type)
88+
{
89+
case SHN_UNDEF:
90+
return ("UND");
91+
case SHN_ABS:
92+
return ("ABS");
93+
case SHN_COMMON:
94+
return ("COM");
95+
default:
96+
if (type >= SHN_LOPROC && type <= SHN_HIPROC)
97+
sprintf(buff, "PRC[0x%04x]", type);
98+
else if (type >= SHN_LOOS && type <= SHN_HIOS)
99+
sprintf(buff, "OS [0x%04x]", type);
100+
else if (type >= SHN_LORESERVE && type <= SHN_HIRESERVE)
101+
sprintf(buff, "RSV[0x%04x]", type);
102+
else
103+
sprintf(buff, "%3d", type);
104+
break;
105+
}
106+
107+
return (strdup(buff));
108+
}

0 commit comments

Comments
 (0)