| 1 |
/* ------------------------------------------------------------------------ */ |
| 2 |
/* Bogus LIBrary Generator */ |
| 3 |
/* for H8/300H C COMPILER(Evaluation software) Ver. 1.0 */ |
| 4 |
/* H8S,H8/300 SERIES C Compiler Ver. 2.0D Evaluation software */ |
| 5 |
/* SH SERIES C/C++ Compiler Ver. 5.0 Evaluation software */ |
| 6 |
/* */ |
| 7 |
/* Copyright (C) 2002,2003 by Project HOS */ |
| 8 |
/* ------------------------------------------------------------------------ */ |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <ctype.h> |
| 12 |
|
| 13 |
//#define DEBUG |
| 14 |
|
| 15 |
#define MAXEXPORT 2000 /* max export symbol number */ |
| 16 |
#define MAXOBJS 256 /* max object file number */ |
| 17 |
|
| 18 |
/* fake time stamp string */ |
| 19 |
const char *pseudo_stamp = "020916113234"; |
| 20 |
#define TSTAMPLEN (strlen(pseudo_stamp)) |
| 21 |
|
| 22 |
/* Tool ID string "not official B-)" */ |
| 23 |
const char *tool_name = "BLIBG-01"; |
| 24 |
#define TOOLNAMELEN (strlen(tool_name)) |
| 25 |
|
| 26 |
/* struct for Object data */ |
| 27 |
typedef struct objcell { |
| 28 |
char *fname; /* filename */ |
| 29 |
char *pname; /* program name (filename - suffix) */ |
| 30 |
int exp_tblidx; /* EXPORT symbol table index */ |
| 31 |
int exp_num; /* number of export symbols */ |
| 32 |
int fsize; /* file size (before padding) */ |
| 33 |
int offset; /* location offset in library file */ |
| 34 |
} OBJCELL; |
| 35 |
|
| 36 |
OBJCELL Obj_table[MAXOBJS]; |
| 37 |
int O_ed = 0; |
| 38 |
|
| 39 |
/* list of objects (for sorting) */ |
| 40 |
int Prog_list[MAXOBJS]; |
| 41 |
|
| 42 |
/* struct for EXPORT symbol */ |
| 43 |
typedef struct expcell { |
| 44 |
char *exp_str; /* symbol string */ |
| 45 |
int prog_num; /* # of program that this symbol belongs to */ |
| 46 |
} EXPCELL; |
| 47 |
|
| 48 |
EXPCELL Exp_table[MAXEXPORT]; |
| 49 |
int E_ed = 0; |
| 50 |
|
| 51 |
/* list of export symbols (for sorting) */ |
| 52 |
int Exp_list[MAXEXPORT]; |
| 53 |
|
| 54 |
/* strings(filename,program name,symbol) buffer size */ |
| 55 |
#define SBUFFSIZE 32*1024 |
| 56 |
|
| 57 |
/* strings(filename,program name,symbol) buffer */ |
| 58 |
char Sbuf[SBUFFSIZE]; |
| 59 |
int S_ed = 0; |
| 60 |
|
| 61 |
#ifdef DEBUG |
| 62 |
#define debc(c) putchar((c)) |
| 63 |
|
| 64 |
void print_Objtable( void) |
| 65 |
{ |
| 66 |
int i,k,l; |
| 67 |
|
| 68 |
putchar('\n'); |
| 69 |
for ( i=0; i<O_ed; i++) { |
| 70 |
printf( "OBJ #%d\n\tfname = %s\n\tpname = %s\n" |
| 71 |
"\texp_num = %d\n\tfsize = %d\n", |
| 72 |
i, Obj_table[i].fname, Obj_table[i].pname, |
| 73 |
Obj_table[i].exp_num, Obj_table[i].fsize ); |
| 74 |
printf( "\tEXPORT ->\n"); |
| 75 |
for ( k=0,l=Obj_table[i].exp_tblidx; k<Obj_table[i].exp_num; k++) |
| 76 |
printf( "\t\t%s\n", Exp_table[l+k].exp_str); |
| 77 |
|
| 78 |
putchar( '\n'); |
| 79 |
} |
| 80 |
|
| 81 |
return; |
| 82 |
} |
| 83 |
#else |
| 84 |
#define debc(c) |
| 85 |
#define print_Objtable() |
| 86 |
#endif |
| 87 |
|
| 88 |
#define OBJ_OK 0 |
| 89 |
#define OBJ_OPEN_ERR 1 |
| 90 |
#define OBJ_FSTAT_ERR 2 |
| 91 |
#define OBJ_UNS_TYPE 3 |
| 92 |
#define OBJ_STR_OVR 4 |
| 93 |
#define OBJ_EXP_OVR 5 |
| 94 |
#define OBJ_NON_EXP 6 |
| 95 |
|
| 96 |
/* sort PROGRAM. key = program name, dictionary order */ |
| 97 |
void prog_sort( void) /* (T_T) poor */ |
| 98 |
{ |
| 99 |
int i,j,k; |
| 100 |
|
| 101 |
/* init list */ |
| 102 |
for ( i=0; i<O_ed; i++) Prog_list[i] = i; |
| 103 |
|
| 104 |
for ( i=0; i<O_ed-1; i++) { |
| 105 |
for ( j=i+1; j<O_ed; j++) { |
| 106 |
if ( strcmp( Obj_table[Prog_list[i]].pname, |
| 107 |
Obj_table[Prog_list[j]].pname ) > 0 ) { |
| 108 |
k = Prog_list[i]; |
| 109 |
Prog_list[i] = Prog_list[j]; |
| 110 |
Prog_list[j] = k; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#ifdef DEBUG |
| 116 |
printf("\n == PROGRAM TABLE ==\n\n"); |
| 117 |
for ( i=0; i<O_ed; i++) |
| 118 |
printf("\tPROGRAM #%d\t%s\n", i, Obj_table[Prog_list[i]].pname); |
| 119 |
printf("\n == END ==\n"); |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/* sort EXPORT. key = symbol string, dictionary order */ |
| 125 |
void export_sort( void) /* (T_T) poor */ |
| 126 |
{ |
| 127 |
int i,j,k,l; |
| 128 |
|
| 129 |
/* init list */ |
| 130 |
for ( i=0; i<E_ed; i++) Exp_list[i] = i; |
| 131 |
|
| 132 |
/* set program # */ |
| 133 |
for ( i=0; i<O_ed; i++) { |
| 134 |
j = Obj_table[Prog_list[i]].exp_tblidx; |
| 135 |
k = Obj_table[Prog_list[i]].exp_num; |
| 136 |
for ( l=0; l<k; l++ ) { |
| 137 |
Exp_table[j+l].prog_num = i; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* sort */ |
| 142 |
for ( i=0; i<E_ed-1; i++) { |
| 143 |
for ( j=i+1; j<E_ed; j++) { |
| 144 |
if ( strcmp( Exp_table[Exp_list[i]].exp_str, |
| 145 |
Exp_table[Exp_list[j]].exp_str) > 0 ) { |
| 146 |
k = Exp_list[i]; |
| 147 |
Exp_list[i] = Exp_list[j]; |
| 148 |
Exp_list[j] = k; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#ifdef DEBUG |
| 154 |
printf("\n == EXPORT TABLE ==\n\n"); |
| 155 |
for ( i=0; i<E_ed; i++) |
| 156 |
printf("\t PROGRAM #%d \t<- %s\n", |
| 157 |
Exp_table[Exp_list[i]].prog_num, |
| 158 |
Exp_table[Exp_list[i]].exp_str ); |
| 159 |
printf("\n == END ==\n"); |
| 160 |
#endif |
| 161 |
} |
| 162 |
|
| 163 |
/* ! never change ! */ |
| 164 |
#define BLOCKSIZE 256 |
| 165 |
|
| 166 |
/* read object file. then make object table and return its size */ |
| 167 |
int make_objtbl( int num, char *fname) |
| 168 |
{ |
| 169 |
int i,j,ofsize; |
| 170 |
unsigned char rbuf[BLOCKSIZE]; |
| 171 |
FILE *fp; |
| 172 |
|
| 173 |
/* open check */ |
| 174 |
if ( ( fp = fopen( fname, "rb")) == NULL ) |
| 175 |
return OBJ_OPEN_ERR; |
| 176 |
|
| 177 |
Obj_table[num].fname = fname; |
| 178 |
Obj_table[num].exp_num = 0; |
| 179 |
Obj_table[num].exp_tblidx = E_ed; |
| 180 |
ofsize = 0; |
| 181 |
|
| 182 |
/* 1st BYTE = type, 2nd BYTE = BLOCK size */ |
| 183 |
while ( fread( rbuf, 1, 2, fp) == 2 ) { |
| 184 |
int sum,type,size; |
| 185 |
|
| 186 |
sum = rbuf[0]+rbuf[1]; type = rbuf[0]; size = rbuf[1]-2; |
| 187 |
|
| 188 |
/* check read blocksize */ |
| 189 |
if ( fread( rbuf, 1, size, fp) != size ) { |
| 190 |
/* trunacated or unsuported type object */ |
| 191 |
fclose( fp); |
| 192 |
return OBJ_UNS_TYPE; |
| 193 |
} |
| 194 |
ofsize += size +2 ; |
| 195 |
|
| 196 |
/* check sum */ |
| 197 |
for ( i=0; i<size; i++) sum += rbuf[i]; |
| 198 |
if (( sum&0xff ) != 0xff ) { |
| 199 |
/* crashed or unsuported type object */ |
| 200 |
fclose( fp); |
| 201 |
return OBJ_UNS_TYPE; |
| 202 |
} |
| 203 |
|
| 204 |
switch ( type) { |
| 205 |
case 0x83: /* timestamp/PROGRAM/environment block */ |
| 206 |
case 0x84: /* timestamp/PROGRAM/environment block */ |
| 207 |
/* offset to program name. rule is unknown. */ |
| 208 |
switch ( rbuf[0]) { |
| 209 |
case 0xc0: |
| 210 |
i = 0x1c; break; |
| 211 |
case 0xb0: |
| 212 |
i = 0x1c; break; |
| 213 |
case 0xa0: |
| 214 |
i = 0x20; break; |
| 215 |
case 0x20: |
| 216 |
i = 0x24; break; |
| 217 |
default: |
| 218 |
/* program name (suposed) is't printable char. |
| 219 |
it means this is unknown(unseen) object type */ |
| 220 |
fclose( fp); |
| 221 |
return OBJ_UNS_TYPE; |
| 222 |
} |
| 223 |
|
| 224 |
/* check program name */ |
| 225 |
for ( j=0; j<rbuf[i]; j++) |
| 226 |
if (!isprint(rbuf[i+1+j])) break; |
| 227 |
|
| 228 |
if ( j != rbuf[i]) { |
| 229 |
/* program name (suposed) is't printable char. |
| 230 |
it means this is unknown(unseen) object type */ |
| 231 |
fclose( fp); |
| 232 |
return OBJ_UNS_TYPE; |
| 233 |
} |
| 234 |
|
| 235 |
/* check buffer over flow */ |
| 236 |
if ( S_ed + rbuf[i] + 1 > SBUFFSIZE) { |
| 237 |
fclose( fp); |
| 238 |
return OBJ_STR_OVR; |
| 239 |
} |
| 240 |
|
| 241 |
/* store program name */ |
| 242 |
strncpy( &Sbuf[S_ed], &rbuf[i+1], rbuf[i]); |
| 243 |
Obj_table[num].pname = &Sbuf[S_ed]; |
| 244 |
S_ed += rbuf[i]+1; |
| 245 |
|
| 246 |
break; |
| 247 |
|
| 248 |
case 0x14: /* EXPORT symbol block */ |
| 249 |
case 0x94: /* EXPORT symbol block */ |
| 250 |
for ( i=0; i<size-1; i += rbuf[i] + 1) { |
| 251 |
/* i found 3 types */ |
| 252 |
switch ( rbuf[i+2]) { |
| 253 |
case 0x00: |
| 254 |
case 0x20: |
| 255 |
i += 7; |
| 256 |
break; |
| 257 |
|
| 258 |
case 0x40: |
| 259 |
i += 8; |
| 260 |
break; |
| 261 |
|
| 262 |
default: |
| 263 |
fclose( fp); |
| 264 |
return OBJ_UNS_TYPE; |
| 265 |
} |
| 266 |
|
| 267 |
/* check buffer overflow */ |
| 268 |
if ( S_ed + rbuf[i] + 1 >= SBUFFSIZE ) { |
| 269 |
fclose( fp); |
| 270 |
return OBJ_STR_OVR; |
| 271 |
} |
| 272 |
|
| 273 |
/* check export symbol number */ |
| 274 |
if ( E_ed == MAXEXPORT-1 ) { |
| 275 |
fclose( fp); |
| 276 |
return OBJ_EXP_OVR; |
| 277 |
} |
| 278 |
|
| 279 |
/* store export symbol string */ |
| 280 |
Exp_table[E_ed].exp_str = strncpy( &Sbuf[S_ed], &rbuf[i+1], rbuf[i]); |
| 281 |
S_ed += rbuf[i] + 1; |
| 282 |
Obj_table[num].exp_num++; |
| 283 |
E_ed++; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
fclose( fp); |
| 288 |
Obj_table[num].fsize = ofsize; |
| 289 |
|
| 290 |
return Obj_table[num].exp_num ? OBJ_OK: OBJ_NON_EXP; |
| 291 |
} |
| 292 |
|
| 293 |
/* joint object files with zero padding */ |
| 294 |
int joint_objfile( FILE *fpw) |
| 295 |
{ |
| 296 |
int i,j,sz,offset; |
| 297 |
char jbuf[BLOCKSIZE]; |
| 298 |
FILE *fpr; |
| 299 |
|
| 300 |
for ( i=offset=0; i<O_ed; i++) { |
| 301 |
fpr = fopen( Obj_table[Prog_list[i]].fname, "rb"); |
| 302 |
Obj_table[Prog_list[i]].offset = offset; |
| 303 |
|
| 304 |
do { |
| 305 |
sz = fread( jbuf, 1, BLOCKSIZE, fpr); |
| 306 |
fwrite( jbuf, 1, sz, fpw); |
| 307 |
offset++; |
| 308 |
} while ( sz == BLOCKSIZE ); |
| 309 |
|
| 310 |
if ( sz ) { |
| 311 |
/* zero padding */ |
| 312 |
for ( j=0; j<BLOCKSIZE-sz; j++) jbuf[j] = 0; |
| 313 |
fwrite( jbuf, 1, 256-sz, fpw); |
| 314 |
} else { |
| 315 |
/* decrement for last empty read */ |
| 316 |
offset--; |
| 317 |
} |
| 318 |
|
| 319 |
fclose( fpr); |
| 320 |
} |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
/* writes EXPORT TABLE temporally file, and return its size */ |
| 326 |
int write_exptmp( FILE *fpw) |
| 327 |
{ |
| 328 |
int i,j,k,l,s,expblk_size; |
| 329 |
char ebbuf[BLOCKSIZE]; |
| 330 |
|
| 331 |
for ( i=expblk_size=0,j=2; i<E_ed; i++) { |
| 332 |
/* 1 block size < 256 byte */ |
| 333 |
if ( j + 2 + 1 + ( l = strlen( Exp_table[Exp_list[i]].exp_str)) |
| 334 |
> BLOCKSIZE-2 ) { |
| 335 |
if ( i == 0) return 0; /* excess */ |
| 336 |
|
| 337 |
/* ebbuf[0] = type(0x64: normal block), ebbuf[1] = size */ |
| 338 |
ebbuf[0] = 0x64; ebbuf[1] = j+1; |
| 339 |
|
| 340 |
/* last 1 BYTE is checksum */ |
| 341 |
for ( k=s=0; k<j; k++) s += ebbuf[k]; |
| 342 |
ebbuf[j] = 0xff - ( s & 0xff); |
| 343 |
|
| 344 |
fwrite( ebbuf, 1, j+1, fpw); |
| 345 |
expblk_size += j+1; |
| 346 |
j = 2; |
| 347 |
} |
| 348 |
|
| 349 |
/* # of program that this export symbol belongs to. |
| 350 |
BIG ENDIAN WORD */ |
| 351 |
ebbuf[j++] = Exp_table[Exp_list[i]].prog_num >> 8; |
| 352 |
ebbuf[j++] = Exp_table[Exp_list[i]].prog_num & 0xff; |
| 353 |
|
| 354 |
/* BYTE(length),"symbol string" */ |
| 355 |
ebbuf[j++] = l; |
| 356 |
strcpy( &ebbuf[j], Exp_table[Exp_list[i]].exp_str); |
| 357 |
j += l; |
| 358 |
} |
| 359 |
|
| 360 |
/* ebbuf[0] = type(0xe4: last block), ebbuf[1] = size */ |
| 361 |
ebbuf[0] = 0xe4; ebbuf[1] = j+1; |
| 362 |
|
| 363 |
/* last 1 BYTE is checksum */ |
| 364 |
for ( k=s=0; k<j; k++) s += ebbuf[k]; |
| 365 |
ebbuf[j] = 255 - ( s & 0xff); |
| 366 |
|
| 367 |
fwrite( ebbuf, 1, j+1, fpw); |
| 368 |
expblk_size += j+1; |
| 369 |
|
| 370 |
/* zero padding */ |
| 371 |
if ( expblk_size & 0xff ) { |
| 372 |
k = BLOCKSIZE - ( expblk_size & 0xff); |
| 373 |
for ( i=0; i<k; i++) ebbuf[i] = 0 ; |
| 374 |
fwrite( ebbuf, 1, k, fpw); |
| 375 |
expblk_size += k; |
| 376 |
} |
| 377 |
|
| 378 |
/* returns block size */ |
| 379 |
return expblk_size; |
| 380 |
} |
| 381 |
|
| 382 |
/* write head block (0x00-0xff) */ |
| 383 |
int write_head( int progblk_size, FILE *fpw) |
| 384 |
{ |
| 385 |
int j,k; |
| 386 |
char hbbuf[BLOCKSIZE]; |
| 387 |
|
| 388 |
/* 1st BYTE : library ID ? |
| 389 |
2nd BYTE : block length(before zero padding) |
| 390 |
3rd BYTE : ?? maybe zero */ |
| 391 |
hbbuf[0] = 0xe0; hbbuf[1] = 35+TOOLNAMELEN; hbbuf[2] = 0x00; |
| 392 |
|
| 393 |
/* 2 time stamps */ |
| 394 |
strcpy( &hbbuf[ 3], pseudo_stamp); |
| 395 |
strcpy( &hbbuf[15], pseudo_stamp); |
| 396 |
|
| 397 |
/* number of object files, BIG ENDIAN WORD */ |
| 398 |
hbbuf[27] = O_ed >> 8; |
| 399 |
hbbuf[28] = O_ed & 0xff; |
| 400 |
|
| 401 |
/* number of export symbol strings, BIG ENDIAN WORD */ |
| 402 |
hbbuf[29] = E_ed >> 8; |
| 403 |
hbbuf[30] = E_ed & 0xff; |
| 404 |
|
| 405 |
/* offset to object file block / 0x100, BIG ENDIAN WORD */ |
| 406 |
j = (0x100 + progblk_size) >> 8; |
| 407 |
hbbuf[31] = j >> 8; |
| 408 |
hbbuf[32] = j & 0xff; |
| 409 |
|
| 410 |
/* librarian tool ID string */ |
| 411 |
hbbuf[33] = TOOLNAMELEN; |
| 412 |
strcpy( &hbbuf[34], tool_name); |
| 413 |
|
| 414 |
/* last BYTE is checksum */ |
| 415 |
for ( j=k=0; j<34+TOOLNAMELEN; j++) k += hbbuf[j]; |
| 416 |
hbbuf[34+TOOLNAMELEN] = 255 - ( k & 0xff); |
| 417 |
|
| 418 |
/* zero padding */ |
| 419 |
for ( j=35+TOOLNAMELEN; j<BLOCKSIZE; hbbuf[j++] = 0); |
| 420 |
|
| 421 |
fwrite( hbbuf, 1, BLOCKSIZE, fpw); |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
/* writes PROGRAM TABLE temporally file and returns its size */ |
| 427 |
int write_progtmp( int expblk_size, int progblk_size, FILE *fpw) |
| 428 |
{ |
| 429 |
int i,j,k,l,s,new_size; |
| 430 |
char pbbuf[BLOCKSIZE]; |
| 431 |
|
| 432 |
/* 0x2-0xd is time stamp string */ |
| 433 |
strcpy( &pbbuf[2], pseudo_stamp); |
| 434 |
|
| 435 |
for ( i=new_size=0,j=2+TSTAMPLEN; i<O_ed; i++) { |
| 436 |
/* block size < 256 byte */ |
| 437 |
if ( j + 2 + 2 + 1 + ( l = strlen(Obj_table[Prog_list[i]].pname)) |
| 438 |
+ TSTAMPLEN > BLOCKSIZE-2 ) { |
| 439 |
if ( i == 0) return 0; /* excess 256 bytes */ |
| 440 |
|
| 441 |
/* 1st BYTE: type (0x62: noraml block) |
| 442 |
2nd BYTE: size */ |
| 443 |
pbbuf[0] = 0x62; pbbuf[1] = j+1 - TSTAMPLEN; |
| 444 |
|
| 445 |
/* last BYTE is checksum */ |
| 446 |
for ( k=s=0; k<j-TSTAMPLEN; k++) s += pbbuf[k]; |
| 447 |
pbbuf[k] = 0xff - ( s & 0xff); |
| 448 |
|
| 449 |
/* determination of offset to object requires size of |
| 450 |
PROGRAM TABLE's own. |
| 451 |
so, 1st call is dummy. no write. */ |
| 452 |
if ( progblk_size != 0) |
| 453 |
fwrite( pbbuf, 1, j+1-TSTAMPLEN, fpw); |
| 454 |
|
| 455 |
new_size += j+1-TSTAMPLEN; |
| 456 |
j = 2 + TSTAMPLEN; |
| 457 |
} |
| 458 |
|
| 459 |
/* (offset of object)/0x100. BIGENDIAN WORD */ |
| 460 |
s = Obj_table[Prog_list[i]].offset |
| 461 |
+ 1 + ((progblk_size + expblk_size)>>8); |
| 462 |
/* 1 = sizeof(HEADBLOCK)>>8 */ |
| 463 |
|
| 464 |
pbbuf[j++] = s >> 8 ; |
| 465 |
pbbuf[j++] = s & 0xff; |
| 466 |
|
| 467 |
/* (object size (zero padded))/0x100. BIGENDIAN WORD */ |
| 468 |
s = ( Obj_table[Prog_list[i]].fsize >> 8 ) |
| 469 |
+ ( Obj_table[Prog_list[i]].fsize & 0xff ? 1: 0 ); |
| 470 |
pbbuf[j++] = s >> 8 ; |
| 471 |
pbbuf[j++] = s & 0xff; |
| 472 |
|
| 473 |
/* [program name length],"programname",time stamp string */ |
| 474 |
pbbuf[j++] = l; |
| 475 |
strcpy( &pbbuf[j], Obj_table[Prog_list[i]].pname); |
| 476 |
j += l; |
| 477 |
strcpy( &pbbuf[j], pseudo_stamp); |
| 478 |
j += TSTAMPLEN; |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
/* 1st BYTE: type (0xe2: last block) |
| 483 |
2nd BYTE: size */ |
| 484 |
pbbuf[0] = 0xe2; pbbuf[1] = j+1-TSTAMPLEN; |
| 485 |
|
| 486 |
/* last BYTE is checksum */ |
| 487 |
for ( k=s=0; k<j-TSTAMPLEN; k++) s += pbbuf[k]; |
| 488 |
pbbuf[k] = 0xff - ( s & 0xff); |
| 489 |
|
| 490 |
/* determination of offset to object requires size of PROGRAM TABLE's own. |
| 491 |
so, 1st call is dummy. no write. */ |
| 492 |
if ( progblk_size != 0) |
| 493 |
fwrite( pbbuf, 1, j+1-TSTAMPLEN, fpw); |
| 494 |
new_size += j+1-TSTAMPLEN; |
| 495 |
|
| 496 |
if ( new_size & 0xff ) { |
| 497 |
k = BLOCKSIZE - ( new_size & 0xff); |
| 498 |
/* zero padding */ |
| 499 |
if ( progblk_size != 0) { |
| 500 |
/* determination of offset to object requires size of |
| 501 |
PROGRAM TABLE's own. |
| 502 |
so, 1st call is dummy. no write. */ |
| 503 |
for ( i=0; i<k; i++) pbbuf[i] = 0; |
| 504 |
fwrite( pbbuf, 1, k, fpw); |
| 505 |
} |
| 506 |
new_size += k; |
| 507 |
} |
| 508 |
|
| 509 |
return new_size; |
| 510 |
} |
| 511 |
|
| 512 |
void cat_file( FILE *fp, FILE *add) |
| 513 |
{ |
| 514 |
int i; |
| 515 |
|
| 516 |
fseek( fp, 0, SEEK_END); |
| 517 |
fseek( add, 0, SEEK_SET); |
| 518 |
|
| 519 |
do { |
| 520 |
i = fread( Sbuf, 1, SBUFFSIZE, add); |
| 521 |
fwrite( Sbuf, 1, i, fp); |
| 522 |
} while ( i == SBUFFSIZE); |
| 523 |
} |
| 524 |
|
| 525 |
void usage( void) |
| 526 |
{ |
| 527 |
fputs( |
| 528 |
"usage:\n\tblibg libfile objfile1 [ objfile2 ... ]\n" |
| 529 |
"\tblibg -sub=subcmdfile\n" |
| 530 |
, stderr); |
| 531 |
} |
| 532 |
|
| 533 |
/* read subcommand file. |
| 534 |
get input objet file list and output library filename */ |
| 535 |
int read_subcmd( char *fname, char *infiles[]) |
| 536 |
{ |
| 537 |
int i,j,k; |
| 538 |
FILE *fp; |
| 539 |
char sbuf[256],str1[32],str2[32],dmy[2]; |
| 540 |
|
| 541 |
if (( fp = fopen( fname, "r")) == NULL ) { |
| 542 |
fprintf( stderr, "can't open %s\n", fname); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
/* output filename is unset. */ |
| 547 |
infiles[1] = NULL; |
| 548 |
|
| 549 |
for ( i=2; fgets( sbuf, 256, fp) != NULL; ) { |
| 550 |
/* drop except 2 word line */ |
| 551 |
if ( sscanf( sbuf, "%31s%31s%1s", str1, str2, dmy) != 2) continue; |
| 552 |
|
| 553 |
/* comannds are case insensitive */ |
| 554 |
for ( j=0; str1[j]!='\0'; j++) str1[j] = tolower(str1[j]); |
| 555 |
|
| 556 |
k = strlen( str2); |
| 557 |
if ( strcmp( str1, "input") == 0 ) { |
| 558 |
if ( S_ed + k +1 > SBUFFSIZE ) { |
| 559 |
fputs( "string buffer overflow.\n", stderr); |
| 560 |
return 0; |
| 561 |
} |
| 562 |
/* store input object filename */ |
| 563 |
strcpy( &Sbuf[S_ed], str2); |
| 564 |
infiles[i++] = &Sbuf[S_ed]; |
| 565 |
S_ed += k + 1; |
| 566 |
continue; |
| 567 |
} |
| 568 |
|
| 569 |
if ( strcmp( str1, "output") == 0 ) { |
| 570 |
if ( S_ed + k +1 > SBUFFSIZE ) { |
| 571 |
fputs( "string buffer overflow.\n", stderr); |
| 572 |
return 0; |
| 573 |
} |
| 574 |
/* store output library filename */ |
| 575 |
strcpy( &Sbuf[S_ed], str2); |
| 576 |
infiles[1] = &Sbuf[S_ed]; |
| 577 |
S_ed += k + 1; |
| 578 |
} |
| 579 |
|
| 580 |
} |
| 581 |
|
| 582 |
/* returns number of object files + 2 */ |
| 583 |
return i; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
#define OPTSUBCMD "-sub=" |
| 588 |
|
| 589 |
int main( int agc, char *agv[]) |
| 590 |
{ |
| 591 |
int i,j; |
| 592 |
char *infiles[MAXOBJS+2]; |
| 593 |
FILE *fpl,*fpp,*fpe,*fpo; |
| 594 |
|
| 595 |
fputs( "Bogus LIBrary Generator\n" |
| 596 |
"\tfor\tH8/300H C COMPILER(Evaluation software) Ver.1.0\n" |
| 597 |
"\t\tH8S,H8/300 SERIES C Compiler Ver. 2.0D Evaluation software\n" |
| 598 |
"\t\tSH SERIES C/C++ Compiler Ver. 5.0 Evaluation software\n\n" |
| 599 |
"WARNING!! There is NO WARRANTIES on this software.\n" |
| 600 |
"Please use at YOUR OWN RISK.\n\n", stderr); |
| 601 |
|
| 602 |
if ( agc == 2 ) { |
| 603 |
/* allows -SUB=,-sub=,-Sub=, ... */ |
| 604 |
strncpy( Sbuf, agv[1], SBUFFSIZE-1); |
| 605 |
for ( i=0; Sbuf[i] != '\0' && i<sizeof(OPTSUBCMD); i++) |
| 606 |
Sbuf[i] = tolower(Sbuf[i]); |
| 607 |
|
| 608 |
if ( strncmp( Sbuf, OPTSUBCMD, sizeof(OPTSUBCMD)-1) == 0) { |
| 609 |
/* setup agc and agv as "blibg libraryname object1 object2 ... */ |
| 610 |
|
| 611 |
agc = read_subcmd( agv[1] + sizeof(OPTSUBCMD) - 1, infiles); |
| 612 |
if ( infiles[1] == NULL ) { |
| 613 |
fprintf( stderr,"no 'output' in %s.\n", agv[1]+sizeof(OPTSUBCMD)-1); |
| 614 |
return 1; |
| 615 |
} |
| 616 |
if ( agc == 2 ) { |
| 617 |
fprintf( stderr,"no 'input' in %s.\n", agv[1]+sizeof(OPTSUBCMD)-1); |
| 618 |
return 1; |
| 619 |
} |
| 620 |
agv = infiles; |
| 621 |
|
| 622 |
} else { |
| 623 |
usage(); |
| 624 |
return 1; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
if ( agc < 3 ) { |
| 629 |
usage(); |
| 630 |
return 1; |
| 631 |
} |
| 632 |
|
| 633 |
if ( agc > MAXOBJS + 2) { |
| 634 |
fputs( "too many object files\n", stderr); |
| 635 |
return 1; |
| 636 |
} |
| 637 |
|
| 638 |
for ( i=0; i<agc-2; i++) { |
| 639 |
#ifdef DEBUG |
| 640 |
fprintf( stdout, "\tarchive -> %s\n", agv[i+2]); |
| 641 |
#endif |
| 642 |
switch ( make_objtbl( i, agv[i+2])) { |
| 643 |
case OBJ_OPEN_ERR: |
| 644 |
fprintf( stderr, "\ncan't open %s.\n", agv[i+2]); |
| 645 |
return 1; |
| 646 |
|
| 647 |
case OBJ_UNS_TYPE: |
| 648 |
fprintf( stderr, "\n%s is unsupported file type.\n", agv[i+2]); |
| 649 |
return 1; |
| 650 |
|
| 651 |
case OBJ_STR_OVR: |
| 652 |
fprintf( stderr, "\nstrings buffer overflow in %s.\n", agv[i+2]); |
| 653 |
return 1; |
| 654 |
|
| 655 |
case OBJ_EXP_OVR: |
| 656 |
fprintf( stderr, "\nexport symbol counter overflow in %s.\n", agv[i+2]); |
| 657 |
return 1; |
| 658 |
|
| 659 |
case OBJ_NON_EXP: |
| 660 |
fprintf( stderr, "\nno export symbols in %s.\n", agv[i+2]); |
| 661 |
return 1; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
O_ed = i; |
| 666 |
|
| 667 |
print_Objtable(); |
| 668 |
|
| 669 |
/* sort programs and export symbols */ |
| 670 |
prog_sort(); export_sort(); |
| 671 |
|
| 672 |
if (( fpo = tmpfile()) == NULL) { |
| 673 |
fputs( "can't create temporaly file.\n", stderr); |
| 674 |
return 1; |
| 675 |
} |
| 676 |
joint_objfile( fpo); |
| 677 |
|
| 678 |
if (( fpe = tmpfile()) == NULL) { |
| 679 |
fputs( "can't create temporaly file.\n", stderr); |
| 680 |
return 1; |
| 681 |
} |
| 682 |
i = write_exptmp( fpe); |
| 683 |
|
| 684 |
if (( fpp = tmpfile()) == NULL) { |
| 685 |
fputs( "can't create temporaly file.\n", stderr); |
| 686 |
return 1; |
| 687 |
} |
| 688 |
/* determin of PROGRAM TABLE requires its own size. |
| 689 |
so, 1st call is dummy. no write. get its size only. */ |
| 690 |
j = write_progtmp( i, 0, fpp); |
| 691 |
write_progtmp( i, j, fpp); |
| 692 |
|
| 693 |
if (( fpl = fopen( agv[1], "wb")) == NULL ) { |
| 694 |
fprintf( stderr, "can't create %s\n", agv[1]); |
| 695 |
return 1; |
| 696 |
} |
| 697 |
/* HEAD BLOCK requires PROGRAM TABLE size,too */ |
| 698 |
write_head( j, fpl); |
| 699 |
|
| 700 |
#ifdef DEBUG |
| 701 |
fprintf( stdout, "\n\toutput library -> %s\n", agv[1]); |
| 702 |
#endif |
| 703 |
cat_file( fpl, fpp); |
| 704 |
cat_file( fpl, fpe); |
| 705 |
cat_file( fpl, fpo); |
| 706 |
|
| 707 |
fclose( fpl); |
| 708 |
fclose( fpp); |
| 709 |
fclose( fpe); |
| 710 |
fclose( fpo); |
| 711 |
|
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
/* ------------------------------------------------------------------------- */ |
| 716 |
/* Copyright (C) 2002,2003 by Project HOS */ |
| 717 |
/* ------------------------------------------------------------------------- */ |