OSDN Git Service

2019/01/19(Sat) 19:36
authorKoine Yuusuke(koinec) <koinec@users.osdn.me>
Sat, 19 Jan 2019 10:36:45 +0000 (19:36 +0900)
committerKoine Yuusuke(koinec) <koinec@users.osdn.me>
Sat, 19 Jan 2019 10:36:45 +0000 (19:36 +0900)
 (LibGoblin)
  * Use cflow app for Output Call-Graph
  * Create ELF ProgramHeader read program files.

libgoblin/Makefile
libgoblin/drd64_libgoblin.h
libgoblin/drd64_libgoblin_api.c
libgoblin/drd64_libgoblin_elf.c
libgoblin/drd64_libgoblin_elf.h
libgoblin/drd64_libgoblin_elf_proghdr.c [new file with mode: 0644]
libgoblin/drd64_libgoblin_elf_proghdr.h [new file with mode: 0644]

index a30ece0..9e46945 100644 (file)
@@ -60,6 +60,7 @@ OBJS = drd64_libgoblin_api.o \
                drd64_libgoblin_elf.o \
                drd64_libgoblin_elf_dynver.o \
                drd64_libgoblin_elf_symtab.o \
+               drd64_libgoblin_elf_proghdr.o \
                drd64_libgoblin_elf64.o \
                drd64_libgoblin_elf32.o \
                drd64_libgoblin_dwarf.o \
@@ -84,6 +85,7 @@ HEADER = drd64_libgoblin.h \
                drd64_libgoblin_elf.h \
                drd64_libgoblin_elf_dynver.h \
                drd64_libgoblin_elf_symtab.h \
+               drd64_libgoblin_elf_proghdr.h \
                drd64_libgoblin_elf64.h \
                drd64_libgoblin_elf32.h \
                drd64_libgoblin_dwarf.h \
@@ -114,9 +116,11 @@ TEST_OBJS = test_libgoblin.o \
 
 TESTPROG = ../testdata/dwarftest
 
+CALLGRAPH = CallGraph.txt
+
 
 all:   $(TARGET) test
-build: $(TARGET)
+build: $(TARGET) $(CALLGRAPH)
 test:  $(TEST_TARGET) $(TESTPROG)
        ./$(TEST_TARGET)
 
@@ -166,6 +170,9 @@ drd64_libgoblin_elf_dynver.o: drd64_libgoblin_elf_dynver.c $(HEADER)
 drd64_libgoblin_elf_symtab.o: drd64_libgoblin_elf_symtab.c $(HEADER)
        $(CC) -c -o drd64_libgoblin_elf_symtab.o $(FLAGS_DEBUG) \
                                drd64_libgoblin_elf_symtab.c
+drd64_libgoblin_elf_proghdr.o: drd64_libgoblin_elf_proghdr.c $(HEADER)
+       $(CC) -c -o drd64_libgoblin_elf_proghdr.o $(FLAGS_DEBUG) \
+                               drd64_libgoblin_elf_proghdr.c
 drd64_libgoblin_elf64.o: drd64_libgoblin_elf64.c $(HEADER)
        $(CC) -c -o drd64_libgoblin_elf64.o $(FLAGS_DEBUG) \
                                drd64_libgoblin_elf64.c
@@ -250,11 +257,14 @@ $(TESTPROG):
        $(MAKE) -C ../testdata all clean
 #      $(MAKE) -C ../testpg/testpg02 all clean
 
+$(CALLGRAPH):
+       env CC=cc cflow -A -P -g -X -n drd64_*.c $(HEADER) > $(CALLGRAPH)
 
 clean:
        rm -f *.o
        rm -f *.xml
        rm -f *.core
+       rm -f $(CALLGRAPH)
        rm -f $(TARGET)
        rm -f $(TEST_TARGET)
 
index e15b6bc..06428e1 100644 (file)
@@ -82,6 +82,7 @@ Comment:
 #include"drd64_libgoblin_elf.h"
 #include"drd64_libgoblin_elf_dynver.h"
 #include"drd64_libgoblin_elf_symtab.h"
+#include"drd64_libgoblin_elf_proghdr.h"
 #include"drd64_libgoblin_elf64.h"
 #include"drd64_libgoblin_elf32.h"
 #include"drd64_libgoblin_dwarf_common.h"
index 2165e52..b36894f 100644 (file)
@@ -53,7 +53,7 @@ int
                return -0x01;
        }
 
-       i_result        = ELF_ReadELF_Phase2( p_pginfo->p_binfo[LIBGOBLIN_BINFO_FILE_EXEC] );
+       i_result        = ELF_ReadELF_Phase2( p_pginfo, p_pginfo->p_binfo[LIBGOBLIN_BINFO_FILE_EXEC] );
        if( 0x00 != i_result )  {
                return -0x02;
        }
@@ -238,6 +238,7 @@ int
        LibGoblin_AllocProgInfo(
                void )
 {
+       int             i_result;
        LibGoblin_ProgramInfo   *p_pginfo       = NULL;
  
        p_pginfo        = ProgInfo_AllocProgInfo();
@@ -245,6 +246,12 @@ int
                return -0x01;
        }
 
+       i_result        = ObjectInfo_Init( p_pginfo, 0 );
+       if( 0x00 != i_result )  {
+               ProgInfo_FreeProgInfo( p_pginfo );      // None use return value.
+               return -0x02;
+       }
+
        return p_pginfo->i_id;
 }
 
@@ -265,6 +272,8 @@ int
        }
        assert( p_pginfo->i_id == i_pgid );
 
+       ObjectInfo_Term( p_pginfo );    // None use retrun value.
+
        i_result        = ProgInfo_FreeProgInfo( p_pginfo );
        if( 0x00 != i_result )  {
                return -0x02;
index 98276e7..fe1976a 100644 (file)
@@ -132,6 +132,7 @@ int
 LIBGOBLIN_ELF_EXTERN
 int
        ELF_ReadELF_Phase2(
+               LibGoblin_ProgramInfo   *p_pginfo,
                LibGoblin_BinaryInfo    *p_binfo )
 {
        int             i_result;
@@ -144,8 +145,13 @@ int
        pb_data = p_bfile->pb_binary;
        assert( NULL != pb_data );
 
-       /* Create Symbol-Table ------------------*/
+       /* Read Program Header to ObjectInfo ------------------*/
+
+       /* Read Program Header to ObjectInfo ------------------*/
 
+       /* Read SymbolTable to ObjectInfo ---------------------*/
+
+       /* Create Symbol-Table ------------------*/
        i_result        = LibGoblin_Section_ReadSymtabSection_toWorkTable(
                                                                        p_binfo, &t_symwork );
        if( 0x00 != i_result )  {
index b8b58eb..e187957 100644 (file)
@@ -54,7 +54,7 @@ LIBGOBLIN_ELF_EXTERN
 LIBGOBLIN_ELF_EXTERN
        int ELF_ReadELF_Phase1( LibGoblin_BinaryInfo *p_binfo );
 LIBGOBLIN_ELF_EXTERN
-       int ELF_ReadELF_Phase2( LibGoblin_BinaryInfo *p_binfo );
+       int ELF_ReadELF_Phase2( LibGoblin_ProgramInfo *p_pginfo, LibGoblin_BinaryInfo *p_binfo );
 
 
 #endif /* DRD64_HEADER_LIBGOBLIN_BINFO */
diff --git a/libgoblin/drd64_libgoblin_elf_proghdr.c b/libgoblin/drd64_libgoblin_elf_proghdr.c
new file mode 100644 (file)
index 0000000..e87ff86
--- /dev/null
@@ -0,0 +1,122 @@
+/*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
+
+                         D r . D e a m o n  6 4
+                        for INTEL64(R), AMD64(R)
+       
+   Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+    this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+
+DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
+
+/* File Info -----------------------------------------------------------
+File: drd64_.c
+Function: 
+Comment: 
+----------------------------------------------------------------------*/
+
+#define        DRD64_SRC_LIBGOBLIN_ELF_PROGHDR
+#include"drd64_libgoblin.h"
+
+
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
+LIBGOBLIN_ELF_PROGHDR_EXTERN
+int
+       ELF_ProgramHeader_Read(
+               LibGoblin_ProgramInfo   *p_pginfo,
+               LibGoblin_BinaryInfo    *p_binfo )
+{
+       int                     i_cnt;
+       int                     i_secnum;
+       int                     i_index;
+       int                     i_user_section;
+       char            *pstr_secname;
+       Byte            *pb_data;
+       Elf64_Ehdr      *p_elfhdr;
+       Elf64_Shdr      *p_sechdr;
+       Elf64_Shdr      *p_secstrhdr;
+       LibGoblin_BinaryFile    *p_bfile;
+       LibGoblin_SectionInfo   *p_sectbl;
+
+       assert( NULL != p_binfo );
+       p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
+       pb_data = p_bfile->pb_binary;
+       assert( NULL != pb_data );
+
+       p_elfhdr                = (Elf64_Ehdr *)pb_data;
+       i_user_section  = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
+
+       /* Get Section Header of the "Section-Name Section" */
+       p_secstrhdr     = (Elf64_Shdr *)(pb_data + p_elfhdr->e_shoff
+                                               + (p_elfhdr->e_shentsize * p_elfhdr->e_shstrndx));
+
+       i_secnum        = p_elfhdr->e_shnum;
+       for( i_cnt = 0; i_cnt < i_secnum; i_cnt++ )     {
+               p_sechdr        = (Elf64_Shdr *)(pb_data + (p_elfhdr->e_shoff)
+                                                       + (p_elfhdr->e_shentsize * i_cnt));
+
+               pstr_secname    = (char *)pb_data + p_secstrhdr->sh_offset
+                                                                       + p_sechdr->sh_name;
+
+               for( i_index = 0;
+                               i_index < LIBGOBLIN_SECTION_ID_DEFAULT_MAX; i_index++ ) {
+
+                       if( !strncmp( pstr_secname,
+                                               gstr_section_name[i_index], 20 ))       {
+
+                               p_sectbl        = &(p_bfile->t_section[i_index]);
+
+                               p_sectbl->pb_sechdr             = (Byte *)p_sechdr;
+                               p_sectbl->pb_data               = (Byte *)(pb_data + p_sechdr->sh_offset);
+                               p_sectbl->pstr_secname  = pstr_secname;
+                               p_sectbl->qw_size               = p_sechdr->sh_size;
+                               p_sectbl->i_fid                 = p_binfo->i_filetype;
+                               break;
+                       }
+               }
+
+               if( LIBGOBLIN_SECTION_ID_DEFAULT_MAX == i_index )       {
+                       p_sectbl        = &(p_bfile->t_section[i_user_section]);
+
+                       p_sectbl->pb_sechdr             = (Byte *)p_sechdr;
+                       p_sectbl->pb_data               = (Byte *)(pb_data + p_sechdr->sh_offset);
+                       p_sectbl->pstr_secname  = pstr_secname;
+                       p_sectbl->qw_size               = p_sechdr->sh_size;
+                       p_sectbl->i_fid                 = p_binfo->i_filetype;
+                               
+                       i_user_section++;
+               }
+
+       }
+
+       /* Update Global SectionTable ---*/
+       if( 0 < p_binfo->i_pginfo )     {
+               //p_pginfo      = ProgInfo_GetProgInfo( p_binfo->i_pginfo );
+               Section_UpdateSectionInfo_inProgInfo( p_pginfo, p_binfo );
+       }
+
+       return 0x00;
+}
+
+
+/* EOF of drd64_.c ----------------------------------- */
diff --git a/libgoblin/drd64_libgoblin_elf_proghdr.h b/libgoblin/drd64_libgoblin_elf_proghdr.h
new file mode 100644 (file)
index 0000000..96c81b3
--- /dev/null
@@ -0,0 +1,54 @@
+/*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
+
+                         D r . D e a m o n  6 4
+                        for INTEL64(R), AMD64(R)
+       
+   Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+    this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+
+DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
+
+/* File Info -----------------------------------------------------------
+File: drd64_.h
+Function: Header 
+Comment: 
+----------------------------------------------------------------------*/
+
+#ifndef DRD64_HEADER_LIBGOBLIN_ELF_PROGHDR
+#define DRD64_HEADER_LIBGOBLIN_ELF_PROGHDR
+
+#include"drd64_libgoblin.h"
+
+#ifdef DRD64_SRC_LIBGOBLIN_ELF_PROGHDR
+       #define LIBGOBLIN_ELF_PROGHDR_EXTERN
+#else
+       #define LIBGOBLIN_ELF_PROGHDR_EXTERN    extern
+#endif
+
+LIBGOBLIN_ELF_PROGHDR_EXTERN
+       int ELF_ProgramHeader_Read( LibGoblin_ProgramInfo *p_pginfo, LibGoblin_BinaryInfo *p_binfo );
+
+
+#endif /* DRD64_HEADER_LIBGOBLIN_BINFO */
+
+/* EOF of drd64_.h ----------------------------------- */