IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

probleme section elf32


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé Avatar de gandalfar
    Inscrit en
    Novembre 2004
    Messages
    145
    Détails du profil
    Informations forums :
    Inscription : Novembre 2004
    Messages : 145
    Par défaut probleme section elf32
    Bonjour a tous.

    Je suis en ce moment en train de developper un petit binaire s'amusant avec le format elf32. Mais j ai un problème: Je n'arrive pas a récuperer mon header de section.

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    ** Developped by syl
    ** contact : syl@evilkittens.org, ccna.syl@gmail.com
    ** THE BEER-WARE LICENSE
    **
    ** syl@evilkittens.org wrote this file.  As long as you retain this notice you
    ** can do whatever you want with this stuff.  If we meet some day, and you
    ** think this stuff is worth it, you can buy me a beer in return.
    **
    ** Sylvestre Gallon
    */
     
    #include <sys/exec_elf.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/mman.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <stdio.h>
     
    const char      *archtype[] = {
            "No Machine",
            "AT&T WE 32100",
            "SPARC",
            "Intel 80386",
            "Motorola 68000",
    */
     
    #include <sys/exec_elf.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/mman.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <stdio.h>
     
    const char      *archtype[] = {
            "No Machine",
            "AT&T WE 32100",
            "SPARC",
            "Intel 80386",
            "Motorola 68000",
            "Motorola 88000",
            "Intel 80486 - unused?",
            "Intel 80860",
            "MIPS R3000 Big-Endian only",
            "unknow",
            "unknow",
            "MIPS R4000 Big-Endian",
            "SPARC v9 64-bit unoffical"
            "",
            "",
            "",
            "HPPA"};
     
    const char      *version[] = {
            "Invalid",
            "Current"
            };
     
    const char      *filetype[] = {
            "No file type",
            "relocatable file",
            "executable file",
            "shared object file",
            "core file"
            };
     
     
    int     myerror(char *str)
    {
            perror(str);
            exit(EXIT_FAILURE);
    }
     
    int     elf_search_section(void *mem)
    {
            Elf32_Ehdr      *bin;
            Elf32_Shdr      *sec;
     
            bin = mem;
            printf("file                    ==> %s\n", filetype[bin->e_type]);
            printf("arch                    ==> %s\n", archtype[bin->e_machine]);
            printf("vers                    ==> %s\n", version[bin->e_version]);
            printf("nb section header       ==> %i\n", bin->e_shnum);
            printf("Section header offset   ==> %08x\n", bin->e_shoff);
            printf("Section header size     ==> %i\n", bin->e_shentsize);
            memcpy(sec,mem+bin->e_shoff, bin->e_shentsize);
            printf("First Section name      ====>%s\n", sec->sh_name);
            printf("First Section type      ====>%i\n", sec->sh_type);
            return (EXIT_SUCCESS);
    }
     
    int     main(int ac, char **av)
    {
            int             fd;
            struct  stat    s;
            void            *mem;
     
            if (ac < 2)
                    myerror("usage: ./bite file");
            if ((fd = open(av[1], O_RDONLY)) < 0)
                    myerror("open error");
            if (fstat(fd, &s) < 0)
                    myerror("stat error");
            if ((mem = mmap(NULL, s.st_size, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED)
                    myerror("mmap error");
            printf("mmap(%s): %08x\n", av[1], mem);
            elf_search_section(mem);
            if (munmap(mem, s.st_size) != 0)
                    myerror("munmap error");
            if (close(fd) != 0)
                    myerror("close error"); 
            return (EXIT_SUCCESS);
    }
    Le probleme se situe donc au niveau du memcpy. sec ne recupere pas le header de section. En esperant votre aide précieuse, je vous remercie.

  2. #2
    Membre émérite Avatar de crocodilex
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    697
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 697
    Par défaut
    Tu as oublié d'allouer de la mémoire pointée par sec.

  3. #3
    Membre éprouvé Avatar de gandalfar
    Inscrit en
    Novembre 2004
    Messages
    145
    Détails du profil
    Informations forums :
    Inscription : Novembre 2004
    Messages : 145
    Par défaut
    oui je viens de m en rendre compte et j ai changer ce code par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    int     elf_search_section(void *mem)
    {
            Elf32_Ehdr      *bin;
            Elf32_Shdr      *sec;
     
            bin = mem;
            printf("file                    ==> %s\n", filetype[bin->e_type]);
            printf("arch                    ==> %s\n", archtype[bin->e_machine]);
            printf("vers                    ==> %s\n", version[bin->e_version]);
            printf("nb section header       ==> %i\n", bin->e_shnum);
            printf("Section header offset   ==> %08x\n", bin->e_shoff);
            printf("Section header size     ==> %i\n", bin->e_shentsize);
            sec = (Elf32_Shdr *)((u_int8_t *)mem + bin->e_shoff, (bin->e_shstrndx * bin->e_shentsize));
            printf("First Section name      ====>%s\n", sec->sh_name);
            printf("First Section type      ====>%i\n", sec->sh_type);
            return (EXIT_SUCCESS);
    }
    mais le probleme persiste.

    voici la sortie de mon programme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    mmap(/bin/ls): 869f0000
    file                    ==> executable file
    arch                    ==> Intel 80386
    vers                    ==> Current
    nb section header       ==> 14
    Section header offset   ==> 0002a7f0
    Section header size     ==> 40
    First Section name      ====>(null)
    First Section type      ====>0

  4. #4
    Membre émérite Avatar de crocodilex
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    697
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 697
    Par défaut
    Citation Envoyé par gandalfar
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    sec = (Elf32_Shdr *)((u_int8_t *)mem + bin->e_shoff, (bin->e_shstrndx * bin->e_shentsize));
    Je ne saisi pas bien ce que fait cette ligne de code !!!
    Peux-tu expliquer STP ?

  5. #5
    Membre éprouvé Avatar de gandalfar
    Inscrit en
    Novembre 2004
    Messages
    145
    Détails du profil
    Informations forums :
    Inscription : Novembre 2004
    Messages : 145
    Par défaut
    En fait j avais mis ca désolé pour le copier coller du mauvais code.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    sec = (Elf32_Shdr *)((u_int8_t *)mem + bin->e_shoff);
    en fait mem et mon binaire mappé en mémoire.
    J essaie de récuperer mon premier header de section.
    bin->e_shoff est l offset du premier header de section.

Discussions similaires

  1. [WD-2007] Probleme en-tête et saut de section
    Par prince013 dans le forum Word
    Réponses: 3
    Dernier message: 23/12/2009, 09h14
  2. Probleme avec utilisation de la class SWT Section
    Par L4BiN dans le forum SWT/JFace
    Réponses: 0
    Dernier message: 26/06/2009, 16h37
  3. Probleme de section en Roman dans la table des matières
    Par GPZ{^_^} dans le forum Mise en forme
    Réponses: 6
    Dernier message: 09/05/2009, 00h46
  4. probleme de sauts de section
    Par cacoune82 dans le forum Word
    Réponses: 1
    Dernier message: 04/09/2007, 14h52
  5. Réponses: 4
    Dernier message: 27/04/2007, 21h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo