Ignore:
Timestamp:
Feb 26, 2000, 1:46:31 AM (26 years ago)
Author:
bird
Message:

More elf definitions and declarations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/win32k/elf2lx/elfdumper.cpp

    r2497 r2899  
    1 /* $Id: elfdumper.cpp,v 1.1 2000-01-22 00:49:21 bird Exp $
     1/* $Id: elfdumper.cpp,v 1.2 2000-02-26 00:46:30 bird Exp $
    22 *
    33 * ELF dumper utility
    44 *
    5  * Copyright (c) 1999 knut st. osmundsen
     5 * Copyright (c) 1999-2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
    66 *
    77 * Project Odin Software License can be found in LICENSE.TXT
     
    5757int             dumpELFImage(void *pv);
    5858int             dumpELFHdr(Elf32_Ehdr *pHdr32);
     59
    5960int             dumpELFSectionHeader(Elf32_Ehdr * pHdr32, int iShdr);
    6061int             dumpELFSectionHeaderTable(Elf32_Ehdr * pHdr32);
     
    6364int             dumpELFRelcations(Elf32_Ehdr * pHdr32,  Elf32_Shdr * pShdr);
    6465int             dumpELFRelcationsA(Elf32_Ehdr * pHdr32,  Elf32_Shdr * pShdr);
     66
     67int             dumpELFProgramHeader(Elf32_Ehdr * pHdr32, int iPhdr);
     68int             dumpELFProgramHeaderTable(Elf32_Ehdr * pHdr32);
     69int             dumpELFDynamicSegment(Elf32_Ehdr * pHdr32,  Elf32_Phdr * pPhdr);
     70int             getELFDynmaicTagValue(Elf32_Dyn *pDyn, Elf32_Sword tag, Elf32_Word *pword);
     71int             dumpELFInterpreterSegment(Elf32_Ehdr * pHdr32,  Elf32_Phdr * pPhdr);
     72int             dumpELFNoteSegment(Elf32_Ehdr * pHdr32,  Elf32_Phdr * pPhdr);
     73
     74
    6575
    6676/* output helpers */
     
    201211        if (rc == 0)
    202212            rc = dumpELFSectionHeaderTable(pHdr32);
     213        if (rc == 0)
     214            rc = dumpELFProgramHeaderTable(pHdr32);
    203215    }
    204216    else
     
    390402        PRINTCASE(1,SHT_NUM);
    391403        default:
    392             if (pShdr->sh_type >= SHT_LOPROC && pShdr->sh_type >= SHT_HIPROC)
     404            if (pShdr->sh_type >= SHT_LOPROC && pShdr->sh_type <= SHT_HIPROC)
    393405                printData(1, "Processor-specific");
    394406            else if (pShdr->sh_type >= SHT_LOUSER && pShdr->sh_type <= SHT_HIUSER)
     
    587599
    588600
     601/**
     602 * Dumps the program header tables.
     603 * @returns   0 on success. Errorcode on error (-1).
     604 * @param     pHdr32  Pointer to the image.
     605 * @author    knut st. osmundsen
     606 */
     607int dumpELFProgramHeaderTable(Elf32_Ehdr * pHdr32)
     608{
     609    int          i;
     610    int          rc = 0;
     611
     612    printHeader("Program Header Table");
     613
     614    for (i = 0; i < pHdr32->e_phnum && rc == 0; i++)
     615    {
     616        if (i > 0)
     617            print(0,"");
     618        rc = dumpELFProgramHeader(pHdr32, i);
     619    }
     620    return rc;
     621}
     622
     623
     624/**
     625 * Dumps a program header of an ELF image.
     626 * @returns   0 on success. Errorcode on error (-1).
     627 * @param     pHdr32  Pointer to the image.
     628 * @param     iPhdr   Program header number.
     629 * @author    knut st. osmundsen
     630 */
     631int dumpELFProgramHeader(Elf32_Ehdr * pHdr32, int iPhdr)
     632{
     633    Elf32_Phdr * pPhdr = (Elf32_Phdr*)((unsigned)pHdr32 + pHdr32->e_phoff + pHdr32->e_phentsize*iPhdr);
     634
     635
     636    print(0, "Program Header", "no. %d  offset 0x%08x", iPhdr, (unsigned)pPhdr - (unsigned)pHdr32);
     637
     638    print(1, "p_type",      "0x%08x", pPhdr->p_type);
     639    switch (pPhdr->p_type)
     640    {
     641        PRINTCASE(1,PT_NULL   );
     642        PRINTCASE(1,PT_LOAD   );
     643        PRINTCASE(1,PT_DYNAMIC);
     644        PRINTCASE(1,PT_INTERP );
     645        PRINTCASE(1,PT_NOTE   );
     646        PRINTCASE(1,PT_SHLIB  );
     647        PRINTCASE(1,PT_PHDR   );
     648        default:
     649            if (pPhdr->p_type >= PT_LOPROC && pPhdr->p_type <= PT_HIPROC)
     650                printData(1, "Processor-specific");
     651            else
     652                printData(1, "unknown");
     653    }
     654    print(1, "p_offset",    "0x%08x", pPhdr->p_offset);
     655    print(1, "p_vaddr",     "0x%08x", pPhdr->p_vaddr);
     656    print(1, "p_paddr",     "0x%08x", pPhdr->p_paddr);
     657    print(1, "p_filesz",    "0x%08x", pPhdr->p_filesz);
     658    print(1, "p_memsz",     "0x%08x", pPhdr->p_memsz);
     659    print(1, "p_flags",     "0x%08x", pPhdr->p_flags);
     660    printBeginLongData(1);
     661    PRINTFLAG(pPhdr->p_flags, PF_X);
     662    PRINTFLAG(pPhdr->p_flags, PF_W);
     663    PRINTFLAG(pPhdr->p_flags, PF_R);
     664    printEndLongData();
     665    print(1, "p_align",     "0x%08x", pPhdr->p_align);
     666    printBeginLongData(1);
     667    if (pPhdr->p_align &&
     668        (pPhdr->p_vaddr % pPhdr->p_align != pPhdr->p_offset % pPhdr->p_align))
     669        printData(1, "alignment error?");
     670    printEndLongData();
     671
     672
     673    switch (pPhdr->p_type)
     674    {
     675        case PT_DYNAMIC:
     676            dumpELFDynamicSegment(pHdr32, pPhdr);
     677            break;
     678
     679        case PT_INTERP:
     680            dumpELFInterpreterSegment(pHdr32, pPhdr);
     681            break;
     682
     683        case PT_NOTE:
     684            dumpELFNoteSegment(pHdr32,  pPhdr);
     685            break;
     686    }
     687    return 0;
     688}
     689
     690
     691/**
     692 * Dumps a dynamic segment.
     693 * @returns   0 on success.
     694 * @param     pHdr32  Pointer to ELF header (and base of the loaded file).
     695 * @param     pPhdr   Pointer to program header.
     696 * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     697 */
     698int dumpELFDynamicSegment(Elf32_Ehdr * pHdr32,  Elf32_Phdr * pPhdr)
     699{
     700    Elf32_Word      word;
     701    Elf32_Addr      base;
     702    int             i;
     703    Elf32_Dyn *     pDyn = (Elf32_Dyn*) ((unsigned)pHdr32 + pPhdr->p_offset);
     704    Elf32_Phdr *    pPhdrs = (Elf32_Phdr*)((unsigned)pHdr32 + pHdr32->e_phoff);
     705    char *          paszStrings;
     706
     707    /* find base address */
     708    i = 0;
     709    while (i < pHdr32->e_phnum && pPhdrs->p_type != PT_LOAD)
     710    {
     711        i++;
     712        pPhdrs = (Elf32_Phdr*)((unsigned)pPhdrs + pHdr32->e_phentsize);
     713    }
     714    if (i < pHdr32->e_phnum)
     715        base = pPhdrs->p_vaddr;
     716
     717    /* find string table */
     718    if (getELFDynmaicTagValue(pDyn, DT_STRTAB, &word) == 0)
     719        paszStrings = (char*)(word - base + (int)pHdr32);
     720    else
     721        paszStrings = NULL;             /* no string table - funny! */
     722
     723    /*
     724     * Loop thru the entries.
     725     */
     726    i = 0;
     727    while (pDyn[i].d_tag != DT_NULL)
     728    {
     729        switch (pDyn[i].d_tag)
     730        {
     731            case DT_NULL:       /*   -    -   -   Marks the end of the dynamic array. */
     732                break;
     733            case DT_NEEDED:     /* d_val  *   *   Holds the string table offset of a null-terminated
     734                                                  string, giving the name of a needed library. The
     735                                                  offset is an index into the table recoreded in the
     736                                                  DT_STRTAB entry. */
     737                print(2, "DT_NEEDED", "0x%08x", pDyn[i].d_un.d_val);
     738                if (paszStrings != NULL)
     739                    printData(2, "%s", paszStrings + pDyn[i].d_un.d_val);
     740                else
     741                    printData(2, "(no string table)");
     742                break;
     743
     744            case DT_PLTRELSZ:
     745                print(2, "DT_PLTRELSZ", "0x%08x", pDyn[i].d_un.d_val);
     746                break;
     747
     748            case DT_PLTGOT:
     749                print(2, "DT_PLTGOT", "0x%08x", pDyn[i].d_un.d_val);
     750                break;
     751            case DT_HASH:
     752                print(2, "DT_HASH", "0x%08x", pDyn[i].d_un.d_val);
     753                break;
     754            case DT_STRTAB:
     755                print(2, "DT_STRTAB", "0x%08x", pDyn[i].d_un.d_val);
     756                break;
     757            case DT_SYMTAB:
     758                print(2, "DT_SYMTAB", "0x%08x", pDyn[i].d_un.d_val);
     759                break;
     760            case DT_RELA:
     761                print(2, "DT_RELA", "0x%08x", pDyn[i].d_un.d_val);
     762                break;
     763            case DT_RELASZ:
     764                print(2, "DT_RELASZ", "0x%08x", pDyn[i].d_un.d_val);
     765                break;
     766            case DT_RELAENT:
     767                print(2, "DT_RELAENT", "0x%08x", pDyn[i].d_un.d_val);
     768                break;
     769
     770            case DT_STRSZ:      /* d_ptr  +   +   This element holds the address of the string table.
     771                                                  Symbol names, library names, and other strings reside
     772                                                  in this table. */
     773                print(2, "DT_STRSZ", "0x%08x", pDyn[i].d_un.d_ptr);
     774                break;
     775
     776            case DT_SYMENT:
     777                print(2, "DT_SYMENT", "0x%08x", pDyn[i].d_un.d_val);
     778                break;
     779
     780            case DT_INIT:       /* d_ptr  *   *   This element holds the address of the initialization function. */
     781                print(2, "DT_INIT", "0x%08x (init function)", pDyn[i].d_un.d_val);
     782                break;
     783
     784            case DT_FINI:       /* d_ptr  *   *   This element holds the address of the termination function. */
     785                print(2, "DT_FINI", "0x%08x (term function)", pDyn[i].d_un.d_val);
     786                break;
     787
     788            case DT_SONAME:     /* d_val  -   *   This element holds the string table offset of a
     789                                                  null-terminated string, giving the name of the shared
     790                                                  object. The offset is an index into the table recorded
     791                                                  in the DT_STRTAB entry. */
     792
     793                print(2, "DT_SONAME", "0x%08x", pDyn[i].d_un.d_val);
     794                if (paszStrings != NULL)
     795                    printData(2, "%s", paszStrings + pDyn[i].d_un.d_val);
     796                else
     797                    printData(2, "(no string table)");
     798                break;
     799
     800            case DT_RPATH:      /* d_val  +   +   This element holds the string table offset of a null-terminated
     801                                                  search library search path string. The offset is an index int
     802                                                  the table recorded in the DT_STRTAB entry. */
     803                print(2, "DT_RPATH", "0x%08x", pDyn[i].d_un.d_val);
     804                if (paszStrings != NULL)
     805                    printData(2, "%s", paszStrings + pDyn[i].d_un.d_val);
     806                else
     807                    printData(2, "(no string table)");
     808                break;
     809
     810            case DT_SYMBOLIC:   /*   -    -   *   This element's presence in a shared object library alters the
     811                                                  dynamic linker's symbol resolution algorithm for references
     812                                                  within the library. Instead of starting a symbol search with
     813                                                  the executable file, the dynamic linker starts from the shared
     814                                                  object file itself. If the shared object fails to supply the
     815                                                  referenced symbol, the dynamic linker then searches the
     816                                                  executable file and other shared objects as usual. */
     817                print(2, "DT_SYMBOLIC", "(%d)", pDyn[i].d_un.d_val);
     818                printData(2, "Searches within the library before the executable.");
     819                break;
     820
     821            case DT_REL:
     822                print(2, "DT_REL", "0x%08x", pDyn[i].d_un.d_val);
     823                break;
     824            case DT_RELSZ:
     825                print(2, "DT_RELSZ", "0x%08x", pDyn[i].d_un.d_val);
     826                break;
     827            case DT_RELENT:
     828                print(2, "DT_RELENT", "0x%08x", pDyn[i].d_un.d_val);
     829                break;
     830            case DT_PLTREL:
     831                print(2, "DT_PLTREL", "0x%08x", pDyn[i].d_un.d_val);
     832                break;
     833            case DT_DEBUG:
     834                print(2, "DT_DEBUG", "0x%08x", pDyn[i].d_un.d_val);
     835                break;
     836            case DT_TEXTREL:
     837                print(2, "DT_TEXTREL", "0x%08x", pDyn[i].d_un.d_val);
     838                break;
     839            case DT_JMPREL:
     840                print(2, "DT_JMPREL", "0x%08x", pDyn[i].d_un.d_val);
     841                break;
     842
     843        }
     844        /* next */
     845        i++;
     846    }
     847    return 0;
     848}
     849
     850
     851/**
     852 *
     853 * @returns   0 on succes.
     854 * @param     pDyn   Pointer to start of tag array.
     855 * @param     tag    Tag type to find.
     856 * @param     pword  Pointer to Elf32_Word which will hold the tag value if found.
     857 * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     858 */
     859int getELFDynmaicTagValue(Elf32_Dyn *pDyn, Elf32_Sword tag, Elf32_Word *pword)
     860{
     861    while (pDyn->d_tag != tag && pDyn->d_tag != DT_NULL)
     862        pDyn++;
     863    if (pDyn->d_tag != DT_NULL)
     864    {
     865        *pword = pDyn->d_un.d_val;
     866        return 0;
     867    }
     868    return -1;
     869}
     870
     871
     872/**
     873 * Dumps a Interpreter segment.
     874 * @returns   0 on success.
     875 * @param     pHdr32  Pointer to ELF header (and base of the loaded file).
     876 * @param     pPhdr   Pointer to program header.
     877 * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     878 */
     879int dumpELFInterpreterSegment(Elf32_Ehdr * pHdr32,  Elf32_Phdr * pPhdr)
     880{
     881    print(2, "Interpreter name",  "%s",     (char*)pHdr32 + pPhdr->p_offset);
     882    return 0;
     883}
     884
     885
     886/**
     887 * Dumps a note segment.
     888 * @returns   0 on success.
     889 * @param     pHdr32  Pointer to ELF header (and base of the loaded file).
     890 * @param     pPhdr   Pointer to program header.
     891 * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     892 */
     893int dumpELFNoteSegment(Elf32_Ehdr * pHdr32,  Elf32_Phdr * pPhdr)
     894{
     895    pPhdr = pPhdr;
     896    pHdr32 = pHdr32;
     897    return 0;
     898}
     899
     900
    589901
    590902
Note: See TracChangeset for help on using the changeset viewer.