CHDK_DE Vorschauversion  Trunk Rev. 5766
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
makeexport.c-Dateireferenz
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
+ Include-Abhängigkeitsdiagramm für makeexport.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  hash_val
 

Makrodefinitionen

#define MAX_SYM   2048
 

Funktionen

char * load_from_file (const char *filename)
 
void cut_export_token (char *sym)
 
char * find_last_token (char *sym)
 
void add_hash (unsigned int val, char *sym)
 
int cmp_hash (const void *a, const void *b)
 
void sort_hash ()
 
unsigned int hash (unsigned char *str)
 
int process_file (const char *name, FILE *out_txt)
 
int main (int argc, char **argv)
 

Variablen

hash_val hash_vals [MAX_SYM]
 
int hash_idx = 0
 

Makro-Dokumentation

#define MAX_SYM   2048

Definiert in Zeile 14 der Datei makeexport.c.

Dokumentation der Funktionen

void add_hash ( unsigned int  val,
char *  sym 
)

Definiert in Zeile 26 der Datei makeexport.c.

27 {
28  int i;
29  for (i=0; i<hash_idx; i++)
30  {
31  if (hash_vals[i].hash == val)
32  {
33  hash_vals[i].count++;
34  fprintf(stderr,"Hash collision for 0x%08x (%s and %s)\n",val, sym, hash_vals[i].symbol);
35  exit(-3);
36  }
37  }
38 
39  hash_vals[hash_idx].hash = val;
41  strcpy(hash_vals[hash_idx].symbol,sym);
42  hash_idx++;
43 }
int cmp_hash ( const void *  a,
const void *  b 
)

Definiert in Zeile 45 der Datei makeexport.c.

46 {
47  hash_val *ha = (hash_val*)a;
48  hash_val *hb = (hash_val*)b;
49  if (ha->hash < hb->hash) return -1;
50  else if (ha->hash > hb->hash) return 1;
51  else return 0;
52 }
void cut_export_token ( char *  sym)

Definiert in Zeile 250 der Datei makeexport.c.

251 {
252  const char* token="_EXPORTEDSYM_";
253  int sizetoken = strlen(token);
254  char* src, *fin;
255 
256  fin=sym+strlen(sym)-sizetoken;
257  for(;sym<=fin;sym++) {
258  if (!memcmp(sym,token,sizetoken)) {
259  for (src=sym+strlen(token); *src; src++,sym++)
260  *sym=*src;
261  *sym=0;
262  return;
263  }
264  }
265 }
char * find_last_token ( char *  sym)

Definiert in Zeile 267 der Datei makeexport.c.

268 {
269  char* token=sym;
270 
271  for (;*sym;sym++)
272  {
273  if ( *sym==' ' && sym[1]>' ')
274  token=sym+1;
275  }
276  return token;
277 }
unsigned int hash ( unsigned char *  str)

Definiert in Zeile 59 der Datei makeexport.c.

60 {
61  unsigned int hash = 5381;
62  int c;
63 
64  // djb2 hash algorithm (Dan Bernstein - http://cr.yp.to/djb.html)
65  while ((c = *str++) != 0)
66  hash = ((hash << 5) + hash) ^ c; /* hash * 33 xor c */
67 
68  return hash;
69 }
char * load_from_file ( const char *  filename)

Definiert in Zeile 229 der Datei makeexport.c.

230 {
231  int f, size;
232  static struct stat st;
233  char *buf = 0;
234 
235  f = open(filename, O_RDONLY, 0777);
236  if (f>=0) {
237  size = (stat((char*)filename, &st)==0)?st.st_size:0;
238  if (size) {
239  buf = (char*)malloc(size+1);
240  if (buf) {
241  size = read(f, buf, size);
242  buf[size]=0;
243  }
244  }
245  close(f);
246  }
247  return buf;
248 }
int main ( int  argc,
char **  argv 
)

Definiert in Zeile 143 der Datei makeexport.c.

144 {
145  if ( argc < 5 )
146  {
147  printf("#error Not enough arguments for export list maker.\n");
148  exit(-1);
149  }
150 
151  FILE* out_h = fopen(argv[1],"wb");
152  FILE* out_txt = fopen(argv[2],"wb");
153  FILE* out_hash = fopen(argv[3],"wb");
154 
155  if (!out_h)
156  {
157  printf("#error Error creation exportlist.h.\n");
158  exit(-1);
159  }
160  if (!out_txt)
161  {
162  printf("#error Error creation exportlist.txt.\n");
163  exit(-1);
164  }
165 
166  fprintf(out_h,"//Auto generated file. Do not edit the contents of this file.\n");
167  fprintf(out_h,"//Update the modules/module_exportlist.c file\n\n");
168  fprintf(out_h,"#ifndef MODULE_EXPORTLIST_H\n");
169  fprintf(out_h,"#define MODULE_EXPORTLIST_H\n\n");
170 
171  // Separate CHDK build num
172  char* build = BUILD_NUMBER;
173  char* e;
174  int build_num=0;
175  int mult=10000;
176  for ( ; *build; build++) {
177  if ( *build<'0' || *build>'9') continue;
178  build_num += mult*strtol(build, &e, 0/*autodetect base oct-dec-hex*/);
179  if ( mult==1 ) break;
180  build=e;
181  mult/=100;
182  }
183  if ( *build )
184  fprintf(out_h,"#define CHDK_BUILD_NUM %d\n\n",build_num);
185 
186  if (!process_file(argv[4], out_txt)) exit(-1); // CHDK exports
187  if (!process_file(argv[5], out_txt)) exit(-1); // GCC library exports
188 
189  int n;
190  sort_hash();
191  fprintf(out_hash,"// This is an automatically generated file. DO NOT EDIT!\n");
192  fprintf(out_hash, "\n#include \"module_hash.h\"\n\n");
193  fprintf(out_hash, "// Address references so that symbol table will compile and link.\n// Don't need correct signatures here, just the name for linking.\n");
194  for (n=0; n<hash_idx; n++)
195  {
196  if (hash_vals[n].symbol[0] == '&') // variable
197  {
198  fprintf(out_hash,"extern int %s;\n",hash_vals[n].symbol+1);
199  }
200  else // function
201  {
202  fprintf(out_hash,"extern void %s(void);\n",hash_vals[n].symbol);
203  }
204  }
205  fprintf(out_hash, "\n// Symbol hash table for resolving exported symbol references\nsym_hash symbol_hash_table[] =\n{\n");
206  for (n=0; n<hash_idx; n++)
207  {
208  fprintf(out_hash,"{ 0x%08x, %s },\n",hash_vals[n].hash,hash_vals[n].symbol);
209  }
210  fprintf(out_hash, "};\n");
211 
212  if (hash_idx>=1)
213  fprintf(out_h,"#define EXPORTLIST_COUNT %d\n\n",hash_idx);
214  else {
215  fprintf(out_h,"#error Malformed export list. Only %d valid records\n\n",hash_idx);
216  exit(-2);
217  }
218  fprintf(out_h,"#endif\n");
219 
220  fclose(out_h);
221  fclose(out_txt);
222  fclose(out_hash);
223 
224  return 0;
225 }
int process_file ( const char *  name,
FILE out_txt 
)

Definiert in Zeile 71 der Datei makeexport.c.

72 {
73  char *cursym;
74 
75  char *cur = load_from_file(name);
76 
77  if (cur == 0)
78  {
79  printf("makeexport: file not found '%s'\n", name);
80  return 0;
81  }
82 
83  // Main cycle
84  for(;*cur; )
85  {
86  for(; *cur==9 || *cur==' '; cur++)
87  ;
88  if (*cur=='(') {
89  for(cur++; *cur && *cur!=')'; cur++);
90  for(; *cur==9 || *cur==' ' || *cur==')'; cur++);
91  }
92 
93  int is_address = 0;
94  if (*cur=='&')
95  {
96  is_address = 1;
97  for(cur++; *cur==9 || *cur==' '; cur++);
98  }
99 
100  cursym=cur;
101  for(; (*cur>='A' && *cur<='Z') ||
102  (*cur>='a' && *cur<='z') ||
103  (*cur>='0' && *cur<='9') ||
104  *cur=='_';
105  cur++);
106 
107  if (cursym!=cur) {
108  char symbol[256], full_symbol[257];
109  int size=cur-cursym;
110 
111  if ( size>255) {size=255;}
112  memcpy(symbol,cursym,size);
113  symbol[size]=0;
114  full_symbol[0] = 0;
115  if (is_address) strcpy(full_symbol,"&");
116  strcat(full_symbol,symbol);
117  cut_export_token(symbol);
118 
119  unsigned int hash_val = hash((unsigned char*)symbol);
120  add_hash(hash_val,full_symbol);
121  fprintf(out_txt,"%08x %s\n",hash_val,symbol);
122  for(; size>=0; size--)
123  {
124  if ( symbol[size]>='a' && symbol[size]<='z')
125  symbol[size]-=0x20;
126  }
127  }
128 
129  for(; *cur && *cur!=10; cur++);
130  for(; *cur==10; cur++);
131  }
132 
133  return 1;
134 }
void sort_hash ( )

Definiert in Zeile 54 der Datei makeexport.c.

55 {
57 }

Variablen-Dokumentation

int hash_idx = 0

Definiert in Zeile 24 der Datei makeexport.c.

hash_val hash_vals[MAX_SYM]

Definiert in Zeile 23 der Datei makeexport.c.