1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - - makelst
3   File:     makelst.c
4 
5   Copyright 2006-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include    <stdio.h>
18 #include    <stdlib.h>              // atoi()
19 #include    <string.h>
20 #include    <ctype.h>
21 #include    <getopt.h>              // getopt()
22 #include    "types.h"
23 #include    "elf.h"
24 #include    "elf_loader.h"
25 #include    "searcharg.h"
26 
27 
28 
29 #define  DS_ROM_HEADER_SIZE    0x4000
30 
31 
32 
33 
34 extern int    modc;
35 extern char** modv;
36 extern char** dll_argv;
37 extern char** static_argv;
38 extern char** header_argv;
39 extern int    dll_index;
40 extern int    static_index;
41 extern int    header_index;
42 extern char*  output_fname;
43 
44 
45 
46 
47 char     c_source_line_str[256];
48 
49 #define  C_SOURCE_FILENAME    "staticsymlist.c"
50 FILE*    CSourceFilep;
51 
52 /*---------------------------------------------------------------------------*
53  *
54  *---------------------------------------------------------------------------*/
55 u32  adr_ALIGN( u32 addr, u32 align_size);
56 void file_write( char* c_str, FILE* Fp);
57 
58 
59 /*---------------------------------------------------------------------------*
60  *
61  *---------------------------------------------------------------------------*/
62 u16        dbg_print_flag;
63 u16        unresolved_table_block_flag = 0;
64 /*---------------------------------------------------------------------------*
65  *  MAIN
66  *---------------------------------------------------------------------------*/
67 
main(int argc,char * argv[])68 int main(int argc, char *argv[])
69 {
70     int             i;
71     FILE            *FHp;
72     u32             binbuf[4];
73     ELHandle        ElfH;
74     char*           c_filename;
75     u16             result;
76 
77 //    printf( "binbuf : %x\n", binbuf);
78 
79     /*-----------------------------------------------------*/
80     dbg_print_flag = 0;
81     SA_searchopt( argc, argv);
82 
83     EL_Init();
84     unresolved_table_block_flag = 0;    //Release prohibition against adding to unresolved table
85     /*----------- Parse the dll file ----------------*/
86     {
87         for( i=0; i<dll_index; i++) {
88             FHp = fopen( dll_argv[i], "rb");
89             if( FHp == NULL) {
90                 printf( "cannot open file \"%s\".\n", dll_argv[i]);
91                     exit( 1);
92             }
93             EL_InitHandle( &ElfH);
94             result = EL_LoadLibraryfromFile( &ElfH,  FHp, binbuf);
95             fclose( FHp);
96         }
97     }
98     /*--------------------------------------------*/
99 
100     EL_ResolveAllLibrary();
101 
102 
103     /*----------- Parse the static file ----------------*/
104     unresolved_table_block_flag = 1;    //Blck adding to unresolved table
105     {
106         for( i=0; i<static_index; i++) {
107             FHp = fopen( static_argv[i], "rb");
108             if( FHp == NULL) {
109                 printf( "cannot open file \"%s\".\n", static_argv[i]);
110                 exit( 1);
111             }
112             EL_InitHandle( &ElfH);
113             result = EL_LoadLibraryfromFile( &ElfH,  FHp, binbuf);
114             fclose( FHp);
115         }
116     }
117     /*-----------------------------------------------*/
118 
119 
120     /*If there is an -o option, change to specified filename*/
121     if( output_fname) {
122         c_filename = output_fname;
123     }else{
124         /*Generate C source filename*/
125         c_filename = malloc( strlen( C_SOURCE_FILENAME));
126         strcpy( c_filename, C_SOURCE_FILENAME);
127     }
128 
129     CSourceFilep = fopen( c_filename, "w");
130     if( !CSourceFilep) {
131         printf( "error : cannot create file \"%s\".\n\n", c_filename);
132         exit( 1);
133     }
134     file_write( "/*This file generated automatically by the \"makelst\".*/\n", CSourceFilep);
135     file_write( "\n", CSourceFilep);
136     file_write( "#ifndef __STATIC_SYM_LIST__\n", CSourceFilep);
137     file_write( "#define __STATIC_SYM_LIST__\n", CSourceFilep);
138     file_write( "\n", CSourceFilep);
139 #ifndef SDK_TWL
140     file_write( "#include <nitro.h>\n", CSourceFilep);
141 #else
142     file_write( "#include <twl.h>\n", CSourceFilep);
143 #endif
144     file_write( "#include <twl/el.h>\n", CSourceFilep);
145     {
146         for( i=0; i<header_index; i++) {
147             const char* header_name = header_argv[i];
148             if (header_name[0] != '<' && header_name[0] != '"')
149             {
150                 fprintf( CSourceFilep, "#include <%s>\n", header_name);
151             }
152             else
153             {
154                 fprintf( CSourceFilep, "#include %s\n", header_name);
155             }
156         }
157     }
158 
159     file_write( "\n", CSourceFilep);
160 
161     EL_ResolveAllLibrary();                //Marking of the extracted symbol
162     EL_ExtractStaticSym1();                //Structure configuration portion
163     file_write( "\n\n", CSourceFilep);
164 
165     file_write( "/*--------------------------------\n", CSourceFilep);
166     file_write( "  API\n", CSourceFilep);
167     file_write( " --------------------------------*/\n", CSourceFilep);
168     file_write( "void EL_AddStaticSym( void)\n", CSourceFilep);
169     file_write( "{\n", CSourceFilep);
170     EL_ExtractStaticSym2();                //API calling portion
171     file_write( "}\n", CSourceFilep);
172 
173     unresolved_table_block_flag = 0;    //Release prohibition against adding to unresolved table
174 
175     file_write( "\n", CSourceFilep);
176     file_write( "#endif /*__STATIC_SYM_LIST__*/\n", CSourceFilep);
177     fclose( CSourceFilep);
178     /*---------------------------------------------*/
179 
180     printf( "\"C\" source file \"%s\" is generated.\n\n", c_filename);
181     exit( 0);
182 
183     /*-----------------------------------------------------*/
184 
185     return 0;
186 }
187 
188 /*---------------------------------------------------------------------------*
189  *  Align addresses
190  *---------------------------------------------------------------------------*/
adr_ALIGN(u32 addr,u32 align_size)191 u32 adr_ALIGN( u32 addr, u32 align_size)
192 {
193     u32 aligned_addr;
194 
195     if( (addr % align_size) == 0) {
196         aligned_addr = addr;
197     }else{
198         aligned_addr = (((addr) & ~((align_size) - 1)) + (align_size));
199     }
200 
201     return aligned_addr;
202 }
203 
204 /*---------------------------------------------------------------------------*
205  *  Write string to file
206  *---------------------------------------------------------------------------*/
file_write(char * c_str,FILE * Fp)207 void file_write( char* c_str, FILE* Fp)
208 {
209     fwrite( c_str, 1, strlen( c_str), Fp);
210 }
211