/* OpenIDA - Outil d'analyse de fichiers binaires
* binary.c - traitement des flots de code binaire
*
* Copyright (C) 2008 Cyrille Bagard
*
* This file is part of OpenIDA.
*
* OpenIDA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* OpenIDA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see .
*/
#include "binary.h"
#include
#include
#include
#include
#include
#include "arch/processor.h"
#include "format/dbg_format.h"
#include "format/exe_format.h"
#include "format/elf/e_elf.h"
#include "format/dwarf/d_dwarf.h"
extern bool find_line_info(const uint8_t *content, off_t *size);
/* Charge en mémoire le contenu d'un fichier. */
uint8_t *map_binary_file(const char *, size_t *);
/******************************************************************************
* *
* Paramètres : filename = nom du fichier à charger. *
* length = taille des données mises en mémoire. [OUT] *
* *
* Description : Charge en mémoire le contenu d'un fichier. *
* *
* Retour : Adresse du contenu binaire ou NULL en cas d'échec. *
* *
* Remarques : - *
* *
******************************************************************************/
uint8_t *map_binary_file(const char *filename, size_t *length)
{
uint8_t *result; /* Données à retourner */
int fd; /* Fichier ouvert en lecture */
struct stat info; /* Informations sur le fichier */
int ret; /* Bilan d'un appel */
fd = open(filename, 0, O_RDONLY);
if (fd == -1)
{
perror("open()");
return NULL;
}
ret = fstat(fd, &info);
if (ret == -1)
{
perror("fstat()");
close(fd);
return NULL;
}
*length = info.st_size;
result = (uint8_t *)mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
if (result == MAP_FAILED)
{
perror("mmap()");
result = NULL;
}
ret = close(fd);
if (ret == -1)
perror("close()");
return result;
}
void fill_snippet(GtkSnippet *snippet)
{
off_t length;
uint8_t *bin_data;
int ret;
exe_format *format;
dbg_format *dformat;
asm_processor *proc;
asm_instr *instr;
char **comments;
uint64_t *offsets;
size_t comments_count;
code_line_info **comments_list;
code_line_info **list;
size_t list_len;
code_line_info *item;
off_t start;
off_t pos;
off_t len;
char buffer[64];
uint64_t base = 0;
uint64_t offset = 0;
size_t i;
proc = create_x86_processor();
pos = 0;
len = 0x28;
bin_data = map_binary_file("/tmp/hello", &length);
printf(" ~~ bin_data ~~ :: %p (%d)\n", bin_data, length);
if (bin_data == NULL) return;
format = load_elf(bin_data, length);
dformat = load_dwarf(bin_data, length, format);
comments_count = get_dwarf_comments(dformat, &comments, &offsets);
comments_list = (code_line_info **)calloc(comments_count, sizeof(code_line_info *));
for (i = 0; i < comments_count; i++)
comments_list[i] = create_code_line_info(offsets[i], NULL, strdup(comments[i]));
i = 0;
find_exe_section(format, ".text", &pos, &len, &base);
/*find_line_info(bin_data, &len);*/
/*
printf("Exiting...\n");
exit(0);
*/
offset = base;
gtk_snippet_set_processor(snippet, proc);
gtk_snippet_add_line(snippet, create_code_line_info(offset, NULL, "Simple HelloWorld !"));
start = pos;
pos = 0;
list = NULL;
list_len = 0;
while (pos < len)
{
offset = base + pos;
/* Si on a un commentaire pour cette ligne... */
if (comments_count > 0 && offsets[i] == offset)
{
list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *));
list[list_len - 1] = comments_list[i++];
}
instr = decode_instruction(proc, &bin_data[start], &pos, len, offset);
item = create_code_line_info(offset, instr, NULL);
list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *));
list[list_len - 1] = item;
//gtk_snippet_add_line(snippet, offset, instr, NULL);
}
for (i = 0; i < list_len; i++)
{
gtk_snippet_add_line(snippet, list[i]);
/* TODO: free() */
}
ret = munmap(bin_data, length);
/*
gtk_snippet_build_content(snippet);
*/
}