Browse Source

changed all printf and fprintf to mine

master
nicolas-arnaud 2 years ago
parent
commit
29f8c9fd63
  1. 1
      --force
  2. BIN
      .cache/clangd/index/ft_printf.h.E66E29B51655512D.idx
  3. BIN
      .cache/clangd/index/libft.h.4AAB37F6DDB83F16.idx
  4. BIN
      .cache/clangd/index/main.c.3F920B18C55EC32E.idx
  5. 21
      Makefile
  6. 2
      ft_printf
  7. 33
      includes/ft_printf.h
  8. 2
      includes/ft_re.h
  9. 2
      includes/libft.h
  10. BIN
      lib/libftprintf.a
  11. 29
      main.c
  12. 100
      re/re32.c
  13. 100
      re/re64.c

1
--force

@ -0,0 +1 @@
Subproject commit d2d5c969e820e0350d1d70c2977e2b6b1f48e5a5

BIN
.cache/clangd/index/ft_printf.h.E66E29B51655512D.idx

Binary file not shown.

BIN
.cache/clangd/index/libft.h.4AAB37F6DDB83F16.idx

Binary file not shown.

BIN
.cache/clangd/index/main.c.3F920B18C55EC32E.idx

Binary file not shown.

21
Makefile

@ -5,28 +5,31 @@ PRINTF=lib/libftprintf.a
all: $(NAME) all: $(NAME)
$(NAME): $(PRINTF) $(NAME): $(PRINTF)
@gcc -Wall -Wextra -Werror -o $(NAME) main.c nm/nm32.c nm/nm64.c nm/is.c -I includes -L lib -lftprintf gcc -Wall -Wextra -Werror -o $(NAME) main.c nm/nm32.c nm/nm64.c nm/is.c -I includes -L lib -lftprintf
$(PRINTF): $(PRINTF):
@make -C ft_printf make -C ft_printf
cp ft_printf/libftprintf.a lib/ cp ft_printf/libftprintf.a lib/
cp ft_printf/libft/libft.h includes/ cp ft_printf/includes/libft.h includes/
cp ft_printf/ft_printf.h includes/ cp ft_printf/includes/ft_printf.h includes/
tests: tests:
@./testster.sh ./testster.sh
debug: debug:
@gcc -Wall -Wextra -Werror -g -o $(NAME) main.c nm/nm32.c nm/nm64.c nm/is.c -I includes gcc -Wall -Wextra -Werror -g -o $(NAME) main.c nm/nm32.c nm/nm64.c nm/is.c -I includes
readelf: readelf:
@gcc -DREADELF -g -Wall -Wextra -Werror -o ft_re main.c re/re32.c re/re64.c re/gets.c -I includes gcc -DREADELF -g -Wall -Wextra -Werror -o ft_re main.c re/re32.c re/re64.c re/gets.c -I includes
clean: clean:
@rm -rf ./exterminatests/*/*/logs ${MAKE} -C ft_printf clean
@rm -f $(NAME) rm -rf ./exterminatests/*/*/logs
rm -f $(NAME)
fclean: clean fclean: clean
${MAKE} -C ft_printf fclean
rm -rf lib/libftprintf.a
re: fclean all re: fclean all

2
ft_printf

@ -1 +1 @@
Subproject commit 55944fc02b4309928c94b6be904beebf315b7985 Subproject commit d2d5c969e820e0350d1d70c2977e2b6b1f48e5a5

33
includes/ft_printf.h

@ -16,7 +16,7 @@
# include "libft.h" # include "libft.h"
# include <stdarg.h> # include <stdarg.h>
# include <stdio.h>
typedef struct s_opts { typedef struct s_opts {
int len; int len;
@ -30,19 +30,30 @@ typedef struct s_opts {
int plus : 1; int plus : 1;
} t_opts; } t_opts;
void add_string(char *str, size_t size);
t_dlist *get_last(void);
t_dlist *get_first(void);
void clean_list(void);
char *int_opts_transform(int n, char *nbr, t_opts *opts); char *int_opts_transform(int n, char *nbr, t_opts *opts);
char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts); char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts);
char *ptr_opts_transform(char *ptr, t_opts *opts); char *ptr_opts_transform(char *ptr, t_opts *opts);
char *str_opts_transform(char *str, t_opts *opts); char *str_opts_transform(char *str, t_opts *opts);
int ft_print_char(int ch, t_opts *opts); void ft_print_char(int ch, t_opts *opts);
int ft_print_str(char *str, t_opts *opts); void ft_print_str(char *str, t_opts *opts);
int va_print_char(va_list va_ch, const char *str, t_opts *opts); void va_print_char(va_list va_ch, const char *str, t_opts *opts);
int va_print_str(va_list va_str, const char *str, t_opts *opts); void va_print_str(va_list va_str, const char *str, t_opts *opts);
int va_print_ptr(va_list va_ptr, const char *str, t_opts *opts); void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts);
int va_print_nbr(va_list va_int, const char *str, t_opts *opts); void va_print_nbr(va_list va_int, const char *str, t_opts *opts);
int va_print_unsign(va_list va_uint, const char *str, t_opts *opts); void va_print_unsign(va_list va_uint, const char *str, t_opts *opts);
int va_print_x(va_list va_uint, const char *str, t_opts *opts); void va_print_x(va_list va_uint, const char *str, t_opts *opts);
int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts); void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts);
int va_print_perc(va_list va, const char *str, t_opts *opts); void va_print_perc(va_list va, const char *str, t_opts *opts);
void ft_format(const char *str, va_list args);
int ft_printf(const char *str, ...); int ft_printf(const char *str, ...);
int ft_fprintf(FILE *file, const char *format, ...);
int ft_sprintf(char *ret, const char *format, ...);

2
includes/ft_re.h

@ -5,6 +5,8 @@
#include "ctype.h" #include "ctype.h"
#include "unistd.h" #include "unistd.h"
#include "ft_printf.h"
int ft_re32(char *filename, int filesize, char *file, t_opts opts); int ft_re32(char *filename, int filesize, char *file, t_opts opts);
int ft_re64(char *filename, int filesize, char *file, t_opts opts); int ft_re64(char *filename, int filesize, char *file, t_opts opts);

2
includes/libft.h

@ -31,6 +31,7 @@ typedef struct s_i_slist
typedef struct s_dlist typedef struct s_dlist
{ {
void *content; void *content;
size_t size;
struct s_dlist *next; struct s_dlist *next;
struct s_dlist *previous; struct s_dlist *previous;
} t_dlist; } t_dlist;
@ -84,6 +85,7 @@ char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*)); void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd); void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd); void ft_putstr_fd(char *s, int fd);
void ft_putnstr_fd(size_t n, char *s, int fd);
void ft_putendl_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd); void ft_putnbr_fd(int n, int fd);
char *ft_append(char *s1, char *s2); char *ft_append(char *s1, char *s2);

BIN
lib/libftprintf.a

Binary file not shown.

29
main.c

@ -51,12 +51,13 @@ int get_filename_pos(char **argv) {
} }
int is_valid_file(char *file) { int is_valid_file(char *file) {
return file[EI_MAG0] == ELFMAG0 && file[EI_MAG1] == ELFMAG1 && file[EI_MAG2] == ELFMAG2 && file[EI_MAG3] == ELFMAG3 return file[EI_MAG0] == ELFMAG0 && file[EI_MAG1] == ELFMAG1 && file[EI_MAG2] == ELFMAG2 &&
&& (file[EI_CLASS] == ELFCLASS32 || file[EI_CLASS] == ELFCLASS64) file[EI_MAG3] == ELFMAG3 &&
&& (file[EI_DATA] == ELFDATA2LSB || file[EI_DATA] == ELFDATA2MSB) (file[EI_CLASS] == ELFCLASS32 || file[EI_CLASS] == ELFCLASS64) &&
&& (file[EI_VERSION] == EV_CURRENT) (file[EI_DATA] == ELFDATA2LSB || file[EI_DATA] == ELFDATA2MSB) &&
&& (file[16] == ET_REL || file[16] == ET_EXEC || file[16] == ET_DYN) (file[EI_VERSION] == EV_CURRENT) &&
&& (file[18] == EM_386 || file[18] == 6 || file[18] == EM_X86_64); (file[16] == ET_REL || file[16] == ET_EXEC || file[16] == ET_DYN) &&
(file[18] == EM_386 || file[18] == 6 || file[18] == EM_X86_64);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -69,17 +70,21 @@ int main(int argc, char **argv) {
if ((fd = open(filename, O_RDONLY)) < 0) return (perror("open"), EXIT_FAILURE); if ((fd = open(filename, O_RDONLY)) < 0) return (perror("open"), EXIT_FAILURE);
if (fstat(fd, &buf) < 0) return (perror("fstat"), EXIT_FAILURE); if (fstat(fd, &buf) < 0) return (perror("fstat"), EXIT_FAILURE);
if (S_ISDIR(buf.st_mode)) return (fprintf(stderr, "nm: Attention : « %s » est un répertoire\n", filename), EXIT_FAILURE); if (S_ISDIR(buf.st_mode))
return (ft_fprintf(stderr, "nm: Attention : « %s » est un répertoire\n", filename),
EXIT_FAILURE);
file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (is_valid_file(file)) { if (is_valid_file(file)) {
if (file[EI_CLASS] == ELFCLASS32 ) ret = LAUNCH32; if (file[EI_CLASS] == ELFCLASS32) ret = LAUNCH32;
else if (file[EI_CLASS] == ELFCLASS64) ret = LAUNCH64; else if (file[EI_CLASS] == ELFCLASS64) ret = LAUNCH64;
else ret = -1; else ret = -1;
} else ret = -1; } else ret = -1;
//printf("%d %d %d %d %d %d %d %d %d %d %d %d %d", file[8], file[9], file[10], file[11], file[12], file[13], file[14], file[15], file[16], file[17], file[18], file[19], file[20]); // printf("%d %d %d %d %d %d %d %d %d %d %d %d %d", file[8], file[9], file[10], file[11],
if (ret == -3)fprintf(stderr, "bfd plugin: %s: file too short\n", filename); // file[12], file[13], file[14], file[15], file[16], file[17], file[18], file[19], file[20]);
if (ret == -1 || ret == -3) fprintf(stderr, "nm: %s: format de fichier non reconnu\n", filename); if (ret == -3) ft_fprintf(stderr, "bfd plugin: %s: file too short\n", filename);
if (ret == -2) fprintf(stderr, "nm: %s: aucun symbole\n", filename); if (ret == -1 || ret == -3)
ft_fprintf(stderr, "nm: %s: format de fichier non reconnu\n", filename);
if (ret == -2) ft_fprintf(stderr, "nm: %s: aucun symbole\n", filename);
if (munmap(file, buf.st_size) < 0) return (perror("munmap"), EXIT_FAILURE); if (munmap(file, buf.st_size) < 0) return (perror("munmap"), EXIT_FAILURE);
return (close(fd), EXIT_SUCCESS); return (close(fd), EXIT_SUCCESS);
} }

100
re/re32.c

@ -1,85 +1,85 @@
#include "ft_re.h" #include "ft_re.h"
void display_32_section_header(t_elf32 *nm, int i) { voi display_32_section_header(t_elf32 *nm, int i) {
printf( ft_printf(
" [Nr] Name Type Addr Off Size " " [Nr] Name Type Addr Off Size "
" " " "
"ES Flg Lk Inf Al\n"); "ES Flg Lk Inf Al\n");
printf(" [%2d] ", i); ft_printf(" [%2d] ", i);
printf("%-18s", nm->string_table + nm->shdr[i].sh_name); ft_printf("%-18s", nm->string_table + nm->shdr[i].sh_name);
printf("%-8s", get_section_type(nm->shdr[i].sh_type)); ft_printf("%-8s", get_section_type(nm->shdr[i].sh_type));
printf("%08x ", nm->shdr[i].sh_addr); ft_printf("%08x ", nm->shdr[i].sh_addr);
printf("%06x ", nm->shdr[i].sh_offset); ft_printf("%06x ", nm->shdr[i].sh_offset);
printf("%06x ", nm->shdr[i].sh_size); ft_printf("%06x ", nm->shdr[i].sh_size);
printf("%02x ", nm->shdr[i].sh_entsize); ft_printf("%02x ", nm->shdr[i].sh_entsize);
printf("%3s ", get_section_flags(nm->shdr[i].sh_flags)); ft_printf("%3s ", get_section_flags(nm->shdr[i].sh_flags));
printf("%02x ", nm->shdr[i].sh_link); ft_printf("%02x ", nm->shdr[i].sh_link);
printf("%02x ", nm->shdr[i].sh_info); ft_printf("%02x ", nm->shdr[i].sh_info);
printf("%02x\n", nm->shdr[i].sh_addralign); ft_printf("%02x\n", nm->shdr[i].sh_addralign);
} }
void display_32_header(t_elf32 *nm) { void display_32_header(t_elf32 *nm) {
printf("ELF Header:\n"); ft_printf("ELF Header:\n");
printf(" Magic: "); ft_printf(" Magic: ");
for (int i = 0; i < EI_NIDENT; i++) printf("%02x ", nm->ehdr->e_ident[i]); for (int i = 0; i < EI_NIDENT; i++)ft_printf("%02x ", nm->ehdr->e_ident[i]);
printf("\n Class: ELF32\n"); ft_printf("\n Class: ELF32\n");
printf( ft_printf(
" Data: 2's complement, little endian\n"); " Data: 2's complement, little endian\n");
printf(" Version: 1 (current)\n"); ft_printf(" Version: 1 (current)\n");
printf(" OS/ABI: UNIX - System V\n"); ft_printf(" OS/ABI: UNIX - System V\n");
printf(" ABI Version: 0\n"); ft_printf(" ABI Version: 0\n");
printf(" Version: 0x%08x\n", ft_printf(" Version: 0x%08x\n",
nm->ehdr->e_version); nm->ehdr->e_version);
printf(" Entry point address: 0x%08x\n", ft_printf(" Entry point address: 0x%08x\n",
nm->ehdr->e_entry); nm->ehdr->e_entry);
printf(" Start of program headers: %d (bytes into file)\n", ft_printf(" Start of program headers: %d (bytes into file)\n",
nm->ehdr->e_phoff); nm->ehdr->e_phoff);
printf(" Start of section headers: %d (bytes into file)\n", ft_printf(" Start of section headers: %d (bytes into file)\n",
nm->ehdr->e_shoff); nm->ehdr->e_shoff);
printf(" Flags: 0x%08x\n", nm->ehdr->e_flags); ft_printf(" Flags: 0x%08x\n", nm->ehdr->e_flags);
printf(" Size of this header: %d (bytes)\n", ft_printf(" Size of this header: %d (bytes)\n",
nm->ehdr->e_ehsize); nm->ehdr->e_ehsize);
printf(" Size of program headers: %d (bytes)\n", ft_printf(" Size of program headers: %d (bytes)\n",
nm->ehdr->e_phentsize); nm->ehdr->e_phentsize);
printf(" Number of program headers: %d\n", nm->ehdr->e_phnum); ft_printf(" Number of program headers: %d\n", nm->ehdr->e_phnum);
printf(" Size of section headers: %d (bytes)\n", ft_printf(" Size of section headers: %d (bytes)\n",
nm->ehdr->e_shentsize); nm->ehdr->e_shentsize);
printf(" Number of section headers: %d\n", nm->ehdr->e_shnum); ft_printf(" Number of section headers: %d\n", nm->ehdr->e_shnum);
printf(" Section header string table index: %d\n", nm->ehdr->e_shstrndx); ft_printf(" Section header string table index: %d\n", nm->ehdr->e_shstrndx);
} }
int print_32_symtab(t_elf32 *nm) { int print_32_symtab(t_elf32 *nm) {
int sym_num = nm->symtab_end - nm->symtab; int sym_num = nm->symtab_end - nm->symtab;
printf("Symbol table '%s' contains %d entries:\n", nm->file, sym_num); ft_printf("Symbol table '%s' contains %d entries:\n", nm->file, sym_num);
printf("Contents of the (%s) section: \n", ft_printf("Contents of the (%s) section: \n",
nm->string_table + nm->shdr[nm->ehdr->e_shstrndx].sh_name); nm->string_table + nm->shdr[nm->ehdr->e_shstrndx].sh_name);
printf( ft_printf(
"| Num: |Value |Size |Type |Bind |Vis " "| Num: |Value |Size |Type |Bind |Vis "
" |Ndx0x |Ndx " " |Ndx0x |Ndx "
" |shndxty |shndxfl|nmChar |Name\n"); " |shndxty |shndxfl|nmChar |Name\n");
for (int i = 0; i < sym_num; i++) { for (int i = 0; i < sym_num; i++) {
int id = nm->opts.p == 0 ? nm->ordered_sym_ids[i] : i; int id = nm->opts.p == 0 ? nm->ordered_sym_ids[i] : i;
printf("| %4d: ", id); ft_printf("| %4d: ", id);
printf("|%8x ", nm->symtab[id].st_value); ft_printf("|%8x ", nm->symtab[id].st_value);
printf("|%8x ", nm->symtab[id].st_size); ft_printf("|%8x ", nm->symtab[id].st_size);
printf("|%-6s ", get_sym_type(ELF32_ST_TYPE(nm->symtab[id].st_info))); ft_printf("|%-6s ", get_sym_type(ELF32_ST_TYPE(nm->symtab[id].st_info)));
printf("|%-6s ", get_sym_bind(ELF32_ST_BIND(nm->symtab[id].st_info))); ft_printf("|%-6s ", get_sym_bind(ELF32_ST_BIND(nm->symtab[id].st_info)));
printf("|%-7s ", ft_printf("|%-7s ",
get_sym_vis(ELF32_ST_VISIBILITY(nm->symtab[id].st_other))); get_sym_vis(ELF32_ST_VISIBILITY(nm->symtab[id].st_other)));
printf("|%5d", nm->symtab[id].st_shndx); ft_printf("|%5d", nm->symtab[id].st_shndx);
printf("|%7s ", get_sym_shndx(nm->symtab[id].st_shndx)); ft_printf("|%7s ", get_sym_shndx(nm->symtab[id].st_shndx));
if (nm->symtab[id].st_shndx < nm->ehdr->e_shnum) { if (nm->symtab[id].st_shndx < nm->ehdr->e_shnum) {
printf("|%8s", ft_printf("|%8s",
get_sh_type(nm->shdr[nm->symtab[id].st_shndx].sh_type)); get_sh_type(nm->shdr[nm->symtab[id].st_shndx].sh_type));
printf("|%8s", ft_printf("|%8s",
get_sh_flag(nm->shdr[nm->symtab[id].st_shndx].sh_flags)); get_sh_flag(nm->shdr[nm->symtab[id].st_shndx].sh_flags));
} else { } else {
printf("|%8c", ' '); ft_printf("|%8c", ' ');
printf("|%8c", ' '); ft_printf("|%8c", ' ');
} }
printf("|%6c ", nm->sym_types[id]); ft_printf("|%6c ", nm->sym_types[id]);
printf("|%s\n", nm->sym_names[id]); ft_printf("|%s\n", nm->sym_names[id]);
} }
return 1; return 1;
} }
@ -97,7 +97,7 @@ int ft_re32(char *filename, int filesize, char *file, t_opts opts) {
// check if file is complete // check if file is complete
if (nm.ehdr->e_shoff + nm.ehdr->e_shnum * nm.ehdr->e_shentsize > nm.size) { if (nm.ehdr->e_shoff + nm.ehdr->e_shnum * nm.ehdr->e_shentsize > nm.size) {
fprintf(stderr, "bfd plugin: %s: file too short\n", nm.file); ft_fprintf(stderr, "bfd plugin: %s: file too short\n", nm.file);
return -1; return -1;
} }

100
re/re64.c

@ -1,90 +1,90 @@
#include "ft_re.h" #include "ft_re.h"
void display_64_section_header(t_elf64 *nm, int i) { void display_64_section_header(t_elf64 *nm, int i) {
printf( ft_printf(
" [Nr] Name Type Addr Off Size " " [Nr] Name Type Addr Off Size "
" " " "
"ES Flg Lk Inf Al\n"); "ES Flg Lk Inf Al\n");
printf(" [%2d] ", i); ft_printf(" [%2d] ", i);
printf("%-18s", nm->string_table + nm->shdr[i].sh_name); ft_printf("%-18s", nm->string_table + nm->shdr[i].sh_name);
printf("%-16s", get_section_type(nm->shdr[i].sh_type)); ft_printf("%-16s", get_section_type(nm->shdr[i].sh_type));
printf("%016lx ", nm->shdr[i].sh_addr); ft_printf("%016lx ", nm->shdr[i].sh_addr);
printf("%06lx ", nm->shdr[i].sh_offset); ft_printf("%06lx ", nm->shdr[i].sh_offset);
printf("%06lx ", nm->shdr[i].sh_size); ft_printf("%06lx ", nm->shdr[i].sh_size);
printf("%02lx ", nm->shdr[i].sh_entsize); ft_printf("%02lx ", nm->shdr[i].sh_entsize);
printf("%3s ", get_section_flags(nm->shdr[i].sh_flags)); ft_printf("%3s ", get_section_flags(nm->shdr[i].sh_flags));
printf("%02x ", nm->shdr[i].sh_link); ft_printf("%02x ", nm->shdr[i].sh_link);
printf("%02x ", nm->shdr[i].sh_info); ft_printf("%02x ", nm->shdr[i].sh_info);
printf("%02lx\n", nm->shdr[i].sh_addralign); ft_printf("%02lx\n", nm->shdr[i].sh_addralign);
} }
void display_64_header(t_elf64 *nm) { void display_64_header(t_elf64 *nm) {
printf("ELF Header:\n"); ft_printf("ELF Header:\n");
printf(" Magic: "); ft_printf(" Magic: ");
for (int i = 0; i < EI_NIDENT; i++) printf("%02x ", nm->ehdr->e_ident[i]); for (int i = 0; i < EI_NIDENT; i++)ft_printf("%02x ", nm->ehdr->e_ident[i]);
printf("\n Class: ELF64\n"); ft_printf("\n Class: ELF64\n");
printf( ft_printf(
" Data: 2's complement, little endian\n"); " Data: 2's complement, little endian\n");
printf(" Version: 1 (current)\n"); ft_printf(" Version: 1 (current)\n");
printf(" OS/ABI: UNIX - System V\n"); ft_printf(" OS/ABI: UNIX - System V\n");
printf(" ABI Version: 0\n"); ft_printf(" ABI Version: 0\n");
printf(" Version: 0x%016x\n", ft_printf(" Version: 0x%016x\n",
nm->ehdr->e_version); nm->ehdr->e_version);
printf(" Entry point address: 0x%016lx\n", ft_printf(" Entry point address: 0x%016lx\n",
nm->ehdr->e_entry); nm->ehdr->e_entry);
printf(" Start of program headers: %ld (bytes into file)\n", ft_printf(" Start of program headers: %ld (bytes into file)\n",
nm->ehdr->e_phoff); nm->ehdr->e_phoff);
printf(" Start of section headers: %ld (bytes into file)\n", ft_printf(" Start of section headers: %ld (bytes into file)\n",
nm->ehdr->e_shoff); nm->ehdr->e_shoff);
printf(" Flags: 0x%016x\n", nm->ehdr->e_flags); ft_printf(" Flags: 0x%016x\n", nm->ehdr->e_flags);
printf(" Size of this header: %d (bytes)\n", ft_printf(" Size of this header: %d (bytes)\n",
nm->ehdr->e_ehsize); nm->ehdr->e_ehsize);
printf(" Size of program headers: %d (bytes)\n", ft_printf(" Size of program headers: %d (bytes)\n",
nm->ehdr->e_phentsize); nm->ehdr->e_phentsize);
printf(" Number of program headers: %d\n", nm->ehdr->e_phnum); ft_printf(" Number of program headers: %d\n", nm->ehdr->e_phnum);
printf(" Size of section headers: %d (bytes)\n", ft_printf(" Size of section headers: %d (bytes)\n",
nm->ehdr->e_shentsize); nm->ehdr->e_shentsize);
printf(" Number of section headers: %d\n", nm->ehdr->e_shnum); ft_printf(" Number of section headers: %d\n", nm->ehdr->e_shnum);
printf(" Section header string table index: %d\n", nm->ehdr->e_shstrndx); ft_printf(" Section header string table index: %d\n", nm->ehdr->e_shstrndx);
} }
int print_64_symtab(t_elf64 *nm) { int print_64_symtab(t_elf64 *nm) {
int sym_num = nm->symtab_end - nm->symtab; int sym_num = nm->symtab_end - nm->symtab;
printf("Symbol table '%s' contains %d entries:\n", nm->file, sym_num); ft_printf("Symbol table '%s' contains %d entries:\n", nm->file, sym_num);
printf("Contents of the (%s) section: \n", ft_printf("Contents of the (%s) section: \n",
nm->string_table + nm->shdr[nm->ehdr->e_shstrndx].sh_name); nm->string_table + nm->shdr[nm->ehdr->e_shstrndx].sh_name);
printf( ft_printf(
"| Num: |Value |Size |Type |Bind |Vis " "| Num: |Value |Size |Type |Bind |Vis "
" |Ndx0x |Ndx " " |Ndx0x |Ndx "
" |shndxty |shndxfl|nmChar |Name\n"); " |shndxty |shndxfl|nmChar |Name\n");
for (int i = 0; i < sym_num; i++) { for (int i = 0; i < sym_num; i++) {
int id = nm->opts.p == 0 ? nm->ordered_sym_ids[i] : i; int id = nm->opts.p == 0 ? nm->ordered_sym_ids[i] : i;
printf("| %4d: ", id); ft_printf("| %4d: ", id);
printf("|%16lx ", nm->symtab[id].st_value); ft_printf("|%16lx ", nm->symtab[id].st_value);
printf("|%16lx ", nm->symtab[id].st_size); ft_printf("|%16lx ", nm->symtab[id].st_size);
printf("|%-6s ", get_sym_type(ELF64_ST_TYPE(nm->symtab[id].st_info))); ft_printf("|%-6s ", get_sym_type(ELF64_ST_TYPE(nm->symtab[id].st_info)));
printf("|%-6s ", get_sym_bind(ELF64_ST_BIND(nm->symtab[id].st_info))); ft_printf("|%-6s ", get_sym_bind(ELF64_ST_BIND(nm->symtab[id].st_info)));
printf("|%-7s ", ft_printf("|%-7s ",
get_sym_vis(ELF64_ST_VISIBILITY(nm->symtab[id].st_other))); get_sym_vis(ELF64_ST_VISIBILITY(nm->symtab[id].st_other)));
printf("|%5d", nm->symtab[id].st_shndx); ft_printf("|%5d", nm->symtab[id].st_shndx);
printf("|%7s ", get_sym_shndx(nm->symtab[id].st_shndx)); ft_printf("|%7s ", get_sym_shndx(nm->symtab[id].st_shndx));
if (nm->symtab[id].st_shndx < nm->ehdr->e_shnum) { if (nm->symtab[id].st_shndx < nm->ehdr->e_shnum) {
printf("|%8s", ft_printf("|%8s",
get_sh_type(nm->shdr[nm->symtab[id].st_shndx].sh_type)); get_sh_type(nm->shdr[nm->symtab[id].st_shndx].sh_type));
printf("|%8s", ft_printf("|%8s",
get_sh_flag(nm->shdr[nm->symtab[id].st_shndx].sh_flags)); get_sh_flag(nm->shdr[nm->symtab[id].st_shndx].sh_flags));
} else { } else {
printf("|%8c", ' '); ft_printf("|%8c", ' ');
printf("|%8c", ' '); ft_printf("|%8c", ' ');
} }
printf("|%6c ", nm->sym_types[id]); ft_printf("|%6c ", nm->sym_types[id]);
printf("|%s\n", nm->sym_names[id]); ft_printf("|%s\n", nm->sym_names[id]);
} }
return 1; return 1;
} }
int parse_64_symtab(t_elf64 *nm) { nt parse_64_symtab(t_elf64 *nm) {
Elf64_Shdr *shdr = &nm->shdr[nm->ehdr->e_shstrndx]; Elf64_Shdr *shdr = &nm->shdr[nm->ehdr->e_shstrndx];
nm->string_table = (char *)(nm->ptr + shdr->sh_offset); nm->string_table = (char *)(nm->ptr + shdr->sh_offset);
for (int i = 0; i < nm->ehdr->e_shnum; i++) { for (int i = 0; i < nm->ehdr->e_shnum; i++) {
@ -136,7 +136,7 @@ int ft_re64(char *filename, int filesize, char *file, t_opts opts) {
// check if file is complete // check if file is complete
if (nm.ehdr->e_shoff + nm.ehdr->e_shnum * nm.ehdr->e_shentsize > nm.size) { if (nm.ehdr->e_shoff + nm.ehdr->e_shnum * nm.ehdr->e_shentsize > nm.size) {
fprintf(stderr, "bfd plugin: %s: file too short\n", nm.file); ft_fprintf(stderr, "bfd plugin: %s: file too short\n", nm.file);
return -1; return -1;
} }

Loading…
Cancel
Save