/* Chrysalide - Outil d'analyse de fichiers binaires
 * constants.c - équivalent Python partiel du fichier "plugins/elf/elf_def.h"
 *
 * Copyright (C) 2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "constants.h"


#include <plugins/pychrysalide/helpers.h>


#include "../elf_def.h"



/* Définit les constantes communes pour le format Elf. */
static bool define_python_binary_format_common_constants(PyTypeObject *);


/******************************************************************************
*                                                                             *
*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
*                                                                             *
*  Description : Définit les constantes communes pour le format Elf.          *
*                                                                             *
*  Retour      : true en cas de succès de l'opération, false sinon.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool define_python_binary_format_common_constants(PyTypeObject *obj_type)
{
    bool result;                            /* Bilan à retourner           */

    result = true;

    /**
     * En-tête de fichier ELF (32 et 64 bits)
     */

    if (result) result = PyDict_AddIntMacro(obj_type, EI_NIDENT);

    /* Composition du champ e_ident */

    if (result) result = PyDict_AddIntMacro(obj_type, EI_MAG0);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_MAG1);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_MAG2);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_MAG3);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_CLASS);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_DATA);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_VERSION);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_OSABI);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_ABIVERSION);
    if (result) result = PyDict_AddIntMacro(obj_type, EI_PAD);

    /* ... EI_CLASS */

    if (result) result = PyDict_AddIntMacro(obj_type, ELFCLASSNONE);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFCLASS32);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFCLASS64);

    /* ... EI_DATA */

    if (result) result = PyDict_AddIntMacro(obj_type, ELFDATANONE);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFDATA2LSB);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFDATA2MSB);

    /* ... EI_VERSION */

    if (result) result = PyDict_AddIntMacro(obj_type, EV_NONE);
    if (result) result = PyDict_AddIntMacro(obj_type, EV_CURRENT);

    /* ... EI_OSABI */

    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_NONE);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_SYSV);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_HPUX);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_NETBSD);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_GNU);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_LINUX);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_SOLARIS);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_AIX);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_IRIX);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_FREEBSD);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_TRU64);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_MODESTO);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_OPENBSD);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_ARM_AEABI);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_ARM);
    if (result) result = PyDict_AddIntMacro(obj_type, ELFOSABI_STANDALONE);

    /* Valeurs possibles pour e_type */

    if (result) result = PyDict_AddIntMacro(obj_type, ET_NONE);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_REL);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_EXEC);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_DYN);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_CORE);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_LOOS);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_HIOS);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_LOPROC);
    if (result) result = PyDict_AddIntMacro(obj_type, ET_HIPROC);

    /* Valeurs possibles pour e_machine */

    if (result) result = PyDict_AddIntMacro(obj_type, EM_NONE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_M32);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_SPARC);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_386);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_68K);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_88K);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_860);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MIPS);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_S370);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MIPS_RS3_LE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PARISC);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_VPP500);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_SPARC32PLUS);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_960);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PPC);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PPC64);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_S390);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_V800);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_FR20);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_RH32);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_RCE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ARM);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_FAKE_ALPHA);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_SH);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_SPARCV9);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_TRICORE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ARC);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_H8_300);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_H8_300H);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_H8S);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_H8_500);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_IA_64);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MIPS_X);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_COLDFIRE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_68HC12);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MMA);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PCP);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_NCPU);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_NDR1);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_STARCORE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ME16);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ST100);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_TINYJ);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_X86_64);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PDSP);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_FX66);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ST9PLUS);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ST7);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_68HC16);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_68HC11);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_68HC08);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_68HC05);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_SVX);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ST19);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_VAX);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_CRIS);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_JAVELIN);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_FIREPATH);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ZSP);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MMIX);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_HUANY);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PRISM);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_AVR);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_FR30);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_D10V);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_D30V);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_V850);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_M32R);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MN10300);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MN10200);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_PJ);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_OPENRISC);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_ARC_A5);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_XTENSA);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_AARCH64);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_TILEPRO);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_MICROBLAZE);
    if (result) result = PyDict_AddIntMacro(obj_type, EM_TILEGX);

    /**
     * En-tête des programmes Elf
     */

    /* Valeurs possibles pour p_type */

    if (result) result = PyDict_AddIntMacro(obj_type, PT_NULL);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_LOAD);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_DYNAMIC);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_INTERP);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_NOTE);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_SHLIB);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_PHDR);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_TLS);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_NUM);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_LOOS);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_GNU_EH_FRAME);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_GNU_STACK);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_GNU_RELRO);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_LOSUNW);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_SUNWBSS);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_SUNWSTACK);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_HISUNW);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_HIOS);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_LOPROC);
    if (result) result = PyDict_AddIntMacro(obj_type, PT_HIPROC);

    /* Valeurs possibles pour p_flags */

    if (result) result = PyDict_AddIntMacro(obj_type, PF_X);
    if (result) result = PyDict_AddIntMacro(obj_type, PF_W);
    if (result) result = PyDict_AddIntMacro(obj_type, PF_R);
    if (result) result = PyDict_AddIntMacro(obj_type, PF_MASKOS);
    if (result) result = PyDict_AddIntMacro(obj_type, PF_MASKPROC);

    /**
     * En-tête des sections Elf
     */
 
    /* Valeurs possibles pour sh_type */

    if (result) result = PyDict_AddIntMacro(obj_type, SHT_NULL);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_PROGBITS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_SYMTAB);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_STRTAB);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_RELA);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_HASH);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_DYNAMIC);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_NOTE);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_NOBITS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_REL);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_SHLIB);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_DYNSYM);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_INIT_ARRAY);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_FINI_ARRAY);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_PREINIT_ARRAY);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GROUP);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_SYMTAB_SHNDX);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_NUM);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_LOOS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GNU_ATTRIBUTES);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GNU_HASH);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GNU_LIBLIST);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_CHECKSUM);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_LOSUNW);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_SUNW_move);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_SUNW_COMDAT);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_SUNW_syminfo);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GNU_verdef);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GNU_verneed);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_GNU_versym);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_HISUNW);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_HIOS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_LOPROC);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_HIPROC);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_LOUSER);
    if (result) result = PyDict_AddIntMacro(obj_type, SHT_HIUSER);

    /* Valeurs possibles pour sh_flags */

    if (result) result = PyDict_AddIntMacro(obj_type, SHF_WRITE);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_ALLOC);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_EXECINSTR);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_MERGE);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_STRINGS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_INFO_LINK);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_LINK_ORDER);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_OS_NONCONFORMING);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_GROUP);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_TLS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_MASKOS);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_MASKPROC);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_ORDERED);
    if (result) result = PyDict_AddIntMacro(obj_type, SHF_EXCLUDE);

    /**
     * Données pour le linker
     */

    /* Valeurs possibles pour d_tag */

    if (result) result = PyDict_AddIntMacro(obj_type, DT_NULL);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_NEEDED);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_PLTRELSZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_PLTGOT);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_HASH);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_STRTAB);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_SYMTAB);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RELA);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RELASZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RELAENT);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_STRSZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_SYMENT);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_INIT);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_FINI);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_SONAME);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RPATH);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_SYMBOLIC);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_REL);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RELSZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RELENT);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_PLTREL);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_DEBUG);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_TEXTREL);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_JMPREL);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_BIND_NOW);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_INIT_ARRAY);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_FINI_ARRAY);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_INIT_ARRAYSZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_FINI_ARRAYSZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_RUNPATH);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_FLAGS);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_ENCODING);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_PREINIT_ARRAY);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_PREINIT_ARRAYSZ);
    if (result) result = PyDict_AddIntMacro(obj_type, DT_NUM);

    /**
     * Symboles de binaires Elf
     */

    /* Valeurs pour le sous-champ ST_TYPE de st_info  */

    if (result) result = PyDict_AddIntMacro(obj_type, STT_NOTYPE);
    if (result) result = PyDict_AddIntMacro(obj_type, STT_OBJECT);
    if (result) result = PyDict_AddIntMacro(obj_type, STT_FUNC);

    /**
     * Informations de relocalisation
     */

    /* Type de relocalisation (x86) */

    if (result) result = PyDict_AddIntMacro(obj_type, R_386_NONE);
    if (result) result = PyDict_AddIntMacro(obj_type, R_386_JMP_SLOT);

    /* Type de relocalisation (ARM) */

    if (result) result = PyDict_AddIntMacro(obj_type, R_ARM_JUMP_SLOT);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
*                                                                             *
*  Description : Définit les constantes pour le format Elf.                   *
*                                                                             *
*  Retour      : true en cas de succès de l'opération, false sinon.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool define_python_binary_format_constants(PyTypeObject *obj_type)
{
    bool result;                            /* Bilan à retourner           */

    result = define_python_binary_format_common_constants(obj_type);

    return result;

}