intl/icu/source/tools/toolutil/pkg_genc.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /******************************************************************************
michael@0 2 * Copyright (C) 2009-2013, International Business Machines
michael@0 3 * Corporation and others. All Rights Reserved.
michael@0 4 *******************************************************************************
michael@0 5 */
michael@0 6 #include "unicode/utypes.h"
michael@0 7
michael@0 8 #if U_PLATFORM_HAS_WIN32_API
michael@0 9 # define VC_EXTRALEAN
michael@0 10 # define WIN32_LEAN_AND_MEAN
michael@0 11 # define NOUSER
michael@0 12 # define NOSERVICE
michael@0 13 # define NOIME
michael@0 14 # define NOMCX
michael@0 15 #include <windows.h>
michael@0 16 #include <time.h>
michael@0 17 # ifdef __GNUC__
michael@0 18 # define WINDOWS_WITH_GNUC
michael@0 19 # endif
michael@0 20 #endif
michael@0 21
michael@0 22 #if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H
michael@0 23 # define U_ELF
michael@0 24 #endif
michael@0 25
michael@0 26 #ifdef U_ELF
michael@0 27 # include <elf.h>
michael@0 28 # if defined(ELFCLASS64)
michael@0 29 # define U_ELF64
michael@0 30 # endif
michael@0 31 /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */
michael@0 32 # ifndef EM_X86_64
michael@0 33 # define EM_X86_64 62
michael@0 34 # endif
michael@0 35 # define ICU_ENTRY_OFFSET 0
michael@0 36 #endif
michael@0 37
michael@0 38 #include <stdio.h>
michael@0 39 #include <stdlib.h>
michael@0 40 #include "unicode/putil.h"
michael@0 41 #include "cmemory.h"
michael@0 42 #include "cstring.h"
michael@0 43 #include "filestrm.h"
michael@0 44 #include "toolutil.h"
michael@0 45 #include "unicode/uclean.h"
michael@0 46 #include "uoptions.h"
michael@0 47 #include "pkg_genc.h"
michael@0 48
michael@0 49 #define MAX_COLUMN ((uint32_t)(0xFFFFFFFFU))
michael@0 50
michael@0 51 #define HEX_0X 0 /* 0x1234 */
michael@0 52 #define HEX_0H 1 /* 01234h */
michael@0 53
michael@0 54 /* prototypes --------------------------------------------------------------- */
michael@0 55 static void
michael@0 56 getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix, const char *optFilename);
michael@0 57
michael@0 58 static uint32_t
michael@0 59 write8(FileStream *out, uint8_t byte, uint32_t column);
michael@0 60
michael@0 61 static uint32_t
michael@0 62 write32(FileStream *out, uint32_t byte, uint32_t column);
michael@0 63
michael@0 64 #if U_PLATFORM == U_PF_OS400
michael@0 65 static uint32_t
michael@0 66 write8str(FileStream *out, uint8_t byte, uint32_t column);
michael@0 67 #endif
michael@0 68 /* -------------------------------------------------------------------------- */
michael@0 69
michael@0 70 /*
michael@0 71 Creating Template Files for New Platforms
michael@0 72
michael@0 73 Let the cc compiler help you get started.
michael@0 74 Compile this program
michael@0 75 const unsigned int x[5] = {1, 2, 0xdeadbeef, 0xffffffff, 16};
michael@0 76 with the -S option to produce assembly output.
michael@0 77
michael@0 78 For example, this will generate array.s:
michael@0 79 gcc -S array.c
michael@0 80
michael@0 81 This will produce a .s file that may look like this:
michael@0 82
michael@0 83 .file "array.c"
michael@0 84 .version "01.01"
michael@0 85 gcc2_compiled.:
michael@0 86 .globl x
michael@0 87 .section .rodata
michael@0 88 .align 4
michael@0 89 .type x,@object
michael@0 90 .size x,20
michael@0 91 x:
michael@0 92 .long 1
michael@0 93 .long 2
michael@0 94 .long -559038737
michael@0 95 .long -1
michael@0 96 .long 16
michael@0 97 .ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.1 2.96-85)"
michael@0 98
michael@0 99 which gives a starting point that will compile, and can be transformed
michael@0 100 to become the template, generally with some consulting of as docs and
michael@0 101 some experimentation.
michael@0 102
michael@0 103 If you want ICU to automatically use this assembly, you should
michael@0 104 specify "GENCCODE_ASSEMBLY=-a name" in the specific config/mh-* file,
michael@0 105 where the name is the compiler or platform that you used in this
michael@0 106 assemblyHeader data structure.
michael@0 107 */
michael@0 108 static const struct AssemblyType {
michael@0 109 const char *name;
michael@0 110 const char *header;
michael@0 111 const char *beginLine;
michael@0 112 const char *footer;
michael@0 113 int8_t hexType; /* HEX_0X or HEX_0h */
michael@0 114 } assemblyHeader[] = {
michael@0 115 // For gcc assemblers, the meaning of .align changes depending on the
michael@0 116 // hardware, so we use .balign 16 which always means 16 bytes.
michael@0 117 // https://sourceware.org/binutils/docs/as/Pseudo-Ops.html
michael@0 118 {"gcc",
michael@0 119 ".globl %s\n"
michael@0 120 "\t.section .note.GNU-stack,\"\",%%progbits\n"
michael@0 121 "\t.section .rodata\n"
michael@0 122 "\t.balign 16\n"
michael@0 123 "\t.type %s,%%object\n"
michael@0 124 "%s:\n\n",
michael@0 125
michael@0 126 ".long ","",HEX_0X
michael@0 127 },
michael@0 128 {"gcc-darwin",
michael@0 129 /*"\t.section __TEXT,__text,regular,pure_instructions\n"
michael@0 130 "\t.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"*/
michael@0 131 ".globl _%s\n"
michael@0 132 "\t.data\n"
michael@0 133 "\t.const\n"
michael@0 134 "\t.balign 16\n"
michael@0 135 "_%s:\n\n",
michael@0 136
michael@0 137 ".long ","",HEX_0X
michael@0 138 },
michael@0 139 {"gcc-cygwin",
michael@0 140 ".globl _%s\n"
michael@0 141 "\t.section .rodata\n"
michael@0 142 "\t.balign 16\n"
michael@0 143 "_%s:\n\n",
michael@0 144
michael@0 145 ".long ","",HEX_0X
michael@0 146 },
michael@0 147 {"gcc-mingw64",
michael@0 148 ".globl %s\n"
michael@0 149 "\t.section .rodata\n"
michael@0 150 "\t.balign 16\n"
michael@0 151 "%s:\n\n",
michael@0 152
michael@0 153 ".long ","",HEX_0X
michael@0 154 },
michael@0 155 // 16 bytes alignment.
michael@0 156 // http://docs.oracle.com/cd/E19641-01/802-1947/802-1947.pdf
michael@0 157 {"sun",
michael@0 158 "\t.section \".rodata\"\n"
michael@0 159 "\t.align 16\n"
michael@0 160 ".globl %s\n"
michael@0 161 "%s:\n",
michael@0 162
michael@0 163 ".word ","",HEX_0X
michael@0 164 },
michael@0 165 // 16 bytes alignment for sun-x86.
michael@0 166 // http://docs.oracle.com/cd/E19963-01/html/821-1608/eoiyg.html
michael@0 167 {"sun-x86",
michael@0 168 "Drodata.rodata:\n"
michael@0 169 "\t.type Drodata.rodata,@object\n"
michael@0 170 "\t.size Drodata.rodata,0\n"
michael@0 171 "\t.globl %s\n"
michael@0 172 "\t.align 16\n"
michael@0 173 "%s:\n",
michael@0 174
michael@0 175 ".4byte ","",HEX_0X
michael@0 176 },
michael@0 177 // 1<<4 bit alignment for aix.
michael@0 178 // http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.aixassem%2Fdoc%2Falangref%2Fidalangref_csect_pseudoop.htm
michael@0 179 {"xlc",
michael@0 180 ".globl %s{RO}\n"
michael@0 181 "\t.toc\n"
michael@0 182 "%s:\n"
michael@0 183 "\t.csect %s{RO}, 4\n",
michael@0 184
michael@0 185 ".long ","",HEX_0X
michael@0 186 },
michael@0 187 {"aCC-ia64",
michael@0 188 "\t.file \"%s.s\"\n"
michael@0 189 "\t.type %s,@object\n"
michael@0 190 "\t.global %s\n"
michael@0 191 "\t.secalias .abe$0.rodata, \".rodata\"\n"
michael@0 192 "\t.section .abe$0.rodata = \"a\", \"progbits\"\n"
michael@0 193 "\t.align 16\n"
michael@0 194 "%s::\t",
michael@0 195
michael@0 196 "data4 ","",HEX_0X
michael@0 197 },
michael@0 198 {"aCC-parisc",
michael@0 199 "\t.SPACE $TEXT$\n"
michael@0 200 "\t.SUBSPA $LIT$\n"
michael@0 201 "%s\n"
michael@0 202 "\t.EXPORT %s\n"
michael@0 203 "\t.ALIGN 16\n",
michael@0 204
michael@0 205 ".WORD ","",HEX_0X
michael@0 206 },
michael@0 207 // align 16 bytes
michael@0 208 // http://msdn.microsoft.com/en-us/library/dwa9fwef.aspx
michael@0 209 { "masm",
michael@0 210 "\tTITLE %s\n"
michael@0 211 "; generated by genccode\n"
michael@0 212 ".386\n"
michael@0 213 ".model flat\n"
michael@0 214 "\tPUBLIC _%s\n"
michael@0 215 "ICUDATA_%s\tSEGMENT READONLY PARA PUBLIC FLAT 'DATA'\n"
michael@0 216 "\tALIGN 16\n"
michael@0 217 "_%s\tLABEL DWORD\n",
michael@0 218 "\tDWORD ","\nICUDATA_%s\tENDS\n\tEND\n",HEX_0H
michael@0 219 }
michael@0 220 };
michael@0 221
michael@0 222 static int32_t assemblyHeaderIndex = -1;
michael@0 223 static int32_t hexType = HEX_0X;
michael@0 224
michael@0 225 U_CAPI UBool U_EXPORT2
michael@0 226 checkAssemblyHeaderName(const char* optAssembly) {
michael@0 227 int32_t idx;
michael@0 228 assemblyHeaderIndex = -1;
michael@0 229 for (idx = 0; idx < (int32_t)(sizeof(assemblyHeader)/sizeof(assemblyHeader[0])); idx++) {
michael@0 230 if (uprv_strcmp(optAssembly, assemblyHeader[idx].name) == 0) {
michael@0 231 assemblyHeaderIndex = idx;
michael@0 232 hexType = assemblyHeader[idx].hexType; /* set the hex type */
michael@0 233 return TRUE;
michael@0 234 }
michael@0 235 }
michael@0 236
michael@0 237 return FALSE;
michael@0 238 }
michael@0 239
michael@0 240
michael@0 241 U_CAPI void U_EXPORT2
michael@0 242 printAssemblyHeadersToStdErr(void) {
michael@0 243 int32_t idx;
michael@0 244 fprintf(stderr, "%s", assemblyHeader[0].name);
michael@0 245 for (idx = 1; idx < (int32_t)(sizeof(assemblyHeader)/sizeof(assemblyHeader[0])); idx++) {
michael@0 246 fprintf(stderr, ", %s", assemblyHeader[idx].name);
michael@0 247 }
michael@0 248 fprintf(stderr,
michael@0 249 ")\n");
michael@0 250 }
michael@0 251
michael@0 252 U_CAPI void U_EXPORT2
michael@0 253 writeAssemblyCode(const char *filename, const char *destdir, const char *optEntryPoint, const char *optFilename, char *outFilePath) {
michael@0 254 uint32_t column = MAX_COLUMN;
michael@0 255 char entry[64];
michael@0 256 uint32_t buffer[1024];
michael@0 257 char *bufferStr = (char *)buffer;
michael@0 258 FileStream *in, *out;
michael@0 259 size_t i, length;
michael@0 260
michael@0 261 in=T_FileStream_open(filename, "rb");
michael@0 262 if(in==NULL) {
michael@0 263 fprintf(stderr, "genccode: unable to open input file %s\n", filename);
michael@0 264 exit(U_FILE_ACCESS_ERROR);
michael@0 265 }
michael@0 266
michael@0 267 getOutFilename(filename, destdir, bufferStr, entry, ".s", optFilename);
michael@0 268 out=T_FileStream_open(bufferStr, "w");
michael@0 269 if(out==NULL) {
michael@0 270 fprintf(stderr, "genccode: unable to open output file %s\n", bufferStr);
michael@0 271 exit(U_FILE_ACCESS_ERROR);
michael@0 272 }
michael@0 273
michael@0 274 if (outFilePath != NULL) {
michael@0 275 uprv_strcpy(outFilePath, bufferStr);
michael@0 276 }
michael@0 277
michael@0 278 #ifdef WINDOWS_WITH_GNUC
michael@0 279 /* Need to fix the file seperator character when using MinGW. */
michael@0 280 swapFileSepChar(outFilePath, U_FILE_SEP_CHAR, '/');
michael@0 281 #endif
michael@0 282
michael@0 283 if(optEntryPoint != NULL) {
michael@0 284 uprv_strcpy(entry, optEntryPoint);
michael@0 285 uprv_strcat(entry, "_dat");
michael@0 286 }
michael@0 287
michael@0 288 /* turn dashes or dots in the entry name into underscores */
michael@0 289 length=uprv_strlen(entry);
michael@0 290 for(i=0; i<length; ++i) {
michael@0 291 if(entry[i]=='-' || entry[i]=='.') {
michael@0 292 entry[i]='_';
michael@0 293 }
michael@0 294 }
michael@0 295
michael@0 296 sprintf(bufferStr, assemblyHeader[assemblyHeaderIndex].header,
michael@0 297 entry, entry, entry, entry,
michael@0 298 entry, entry, entry, entry);
michael@0 299 T_FileStream_writeLine(out, bufferStr);
michael@0 300 T_FileStream_writeLine(out, assemblyHeader[assemblyHeaderIndex].beginLine);
michael@0 301
michael@0 302 for(;;) {
michael@0 303 length=T_FileStream_read(in, buffer, sizeof(buffer));
michael@0 304 if(length==0) {
michael@0 305 break;
michael@0 306 }
michael@0 307 if (length != sizeof(buffer)) {
michael@0 308 /* pad with extra 0's when at the end of the file */
michael@0 309 for(i=0; i < (length % sizeof(uint32_t)); ++i) {
michael@0 310 buffer[length+i] = 0;
michael@0 311 }
michael@0 312 }
michael@0 313 for(i=0; i<(length/sizeof(buffer[0])); i++) {
michael@0 314 column = write32(out, buffer[i], column);
michael@0 315 }
michael@0 316 }
michael@0 317
michael@0 318 T_FileStream_writeLine(out, "\n");
michael@0 319
michael@0 320 sprintf(bufferStr, assemblyHeader[assemblyHeaderIndex].footer,
michael@0 321 entry, entry, entry, entry,
michael@0 322 entry, entry, entry, entry);
michael@0 323 T_FileStream_writeLine(out, bufferStr);
michael@0 324
michael@0 325 if(T_FileStream_error(in)) {
michael@0 326 fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
michael@0 327 exit(U_FILE_ACCESS_ERROR);
michael@0 328 }
michael@0 329
michael@0 330 if(T_FileStream_error(out)) {
michael@0 331 fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
michael@0 332 exit(U_FILE_ACCESS_ERROR);
michael@0 333 }
michael@0 334
michael@0 335 T_FileStream_close(out);
michael@0 336 T_FileStream_close(in);
michael@0 337 }
michael@0 338
michael@0 339 U_CAPI void U_EXPORT2
michael@0 340 writeCCode(const char *filename, const char *destdir, const char *optName, const char *optFilename, char *outFilePath) {
michael@0 341 uint32_t column = MAX_COLUMN;
michael@0 342 char buffer[4096], entry[64];
michael@0 343 FileStream *in, *out;
michael@0 344 size_t i, length;
michael@0 345
michael@0 346 in=T_FileStream_open(filename, "rb");
michael@0 347 if(in==NULL) {
michael@0 348 fprintf(stderr, "genccode: unable to open input file %s\n", filename);
michael@0 349 exit(U_FILE_ACCESS_ERROR);
michael@0 350 }
michael@0 351
michael@0 352 if(optName != NULL) { /* prepend 'icudt28_' */
michael@0 353 strcpy(entry, optName);
michael@0 354 strcat(entry, "_");
michael@0 355 } else {
michael@0 356 entry[0] = 0;
michael@0 357 }
michael@0 358
michael@0 359 getOutFilename(filename, destdir, buffer, entry+uprv_strlen(entry), ".c", optFilename);
michael@0 360 if (outFilePath != NULL) {
michael@0 361 uprv_strcpy(outFilePath, buffer);
michael@0 362 }
michael@0 363 out=T_FileStream_open(buffer, "w");
michael@0 364 if(out==NULL) {
michael@0 365 fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
michael@0 366 exit(U_FILE_ACCESS_ERROR);
michael@0 367 }
michael@0 368
michael@0 369 /* turn dashes or dots in the entry name into underscores */
michael@0 370 length=uprv_strlen(entry);
michael@0 371 for(i=0; i<length; ++i) {
michael@0 372 if(entry[i]=='-' || entry[i]=='.') {
michael@0 373 entry[i]='_';
michael@0 374 }
michael@0 375 }
michael@0 376
michael@0 377 #if U_PLATFORM == U_PF_OS400
michael@0 378 /*
michael@0 379 TODO: Fix this once the compiler implements this feature. Keep in sync with udatamem.c
michael@0 380
michael@0 381 This is here because this platform can't currently put
michael@0 382 const data into the read-only pages of an object or
michael@0 383 shared library (service program). Only strings are allowed in read-only
michael@0 384 pages, so we use char * strings to store the data.
michael@0 385
michael@0 386 In order to prevent the beginning of the data from ever matching the
michael@0 387 magic numbers we must still use the initial double.
michael@0 388 [grhoten 4/24/2003]
michael@0 389 */
michael@0 390 sprintf(buffer,
michael@0 391 "#ifndef IN_GENERATED_CCODE\n"
michael@0 392 "#define IN_GENERATED_CCODE\n"
michael@0 393 "#define U_DISABLE_RENAMING 1\n"
michael@0 394 "#include \"unicode/umachine.h\"\n"
michael@0 395 "#endif\n"
michael@0 396 "U_CDECL_BEGIN\n"
michael@0 397 "const struct {\n"
michael@0 398 " double bogus;\n"
michael@0 399 " const char *bytes; \n"
michael@0 400 "} %s={ 0.0, \n",
michael@0 401 entry);
michael@0 402 T_FileStream_writeLine(out, buffer);
michael@0 403
michael@0 404 for(;;) {
michael@0 405 length=T_FileStream_read(in, buffer, sizeof(buffer));
michael@0 406 if(length==0) {
michael@0 407 break;
michael@0 408 }
michael@0 409 for(i=0; i<length; ++i) {
michael@0 410 column = write8str(out, (uint8_t)buffer[i], column);
michael@0 411 }
michael@0 412 }
michael@0 413
michael@0 414 T_FileStream_writeLine(out, "\"\n};\nU_CDECL_END\n");
michael@0 415 #else
michael@0 416 /* Function renaming shouldn't be done in data */
michael@0 417 sprintf(buffer,
michael@0 418 "#ifndef IN_GENERATED_CCODE\n"
michael@0 419 "#define IN_GENERATED_CCODE\n"
michael@0 420 "#define U_DISABLE_RENAMING 1\n"
michael@0 421 "#include \"unicode/umachine.h\"\n"
michael@0 422 "#endif\n"
michael@0 423 "U_CDECL_BEGIN\n"
michael@0 424 "const struct {\n"
michael@0 425 " double bogus;\n"
michael@0 426 " uint8_t bytes[%ld]; \n"
michael@0 427 "} %s={ 0.0, {\n",
michael@0 428 (long)T_FileStream_size(in), entry);
michael@0 429 T_FileStream_writeLine(out, buffer);
michael@0 430
michael@0 431 for(;;) {
michael@0 432 length=T_FileStream_read(in, buffer, sizeof(buffer));
michael@0 433 if(length==0) {
michael@0 434 break;
michael@0 435 }
michael@0 436 for(i=0; i<length; ++i) {
michael@0 437 column = write8(out, (uint8_t)buffer[i], column);
michael@0 438 }
michael@0 439 }
michael@0 440
michael@0 441 T_FileStream_writeLine(out, "\n}\n};\nU_CDECL_END\n");
michael@0 442 #endif
michael@0 443
michael@0 444 if(T_FileStream_error(in)) {
michael@0 445 fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
michael@0 446 exit(U_FILE_ACCESS_ERROR);
michael@0 447 }
michael@0 448
michael@0 449 if(T_FileStream_error(out)) {
michael@0 450 fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
michael@0 451 exit(U_FILE_ACCESS_ERROR);
michael@0 452 }
michael@0 453
michael@0 454 T_FileStream_close(out);
michael@0 455 T_FileStream_close(in);
michael@0 456 }
michael@0 457
michael@0 458 static uint32_t
michael@0 459 write32(FileStream *out, uint32_t bitField, uint32_t column) {
michael@0 460 int32_t i;
michael@0 461 char bitFieldStr[64]; /* This is more bits than needed for a 32-bit number */
michael@0 462 char *s = bitFieldStr;
michael@0 463 uint8_t *ptrIdx = (uint8_t *)&bitField;
michael@0 464 static const char hexToStr[16] = {
michael@0 465 '0','1','2','3',
michael@0 466 '4','5','6','7',
michael@0 467 '8','9','A','B',
michael@0 468 'C','D','E','F'
michael@0 469 };
michael@0 470
michael@0 471 /* write the value, possibly with comma and newline */
michael@0 472 if(column==MAX_COLUMN) {
michael@0 473 /* first byte */
michael@0 474 column=1;
michael@0 475 } else if(column<32) {
michael@0 476 *(s++)=',';
michael@0 477 ++column;
michael@0 478 } else {
michael@0 479 *(s++)='\n';
michael@0 480 uprv_strcpy(s, assemblyHeader[assemblyHeaderIndex].beginLine);
michael@0 481 s+=uprv_strlen(s);
michael@0 482 column=1;
michael@0 483 }
michael@0 484
michael@0 485 if (bitField < 10) {
michael@0 486 /* It's a small number. Don't waste the space for 0x */
michael@0 487 *(s++)=hexToStr[bitField];
michael@0 488 }
michael@0 489 else {
michael@0 490 int seenNonZero = 0; /* This is used to remove leading zeros */
michael@0 491
michael@0 492 if(hexType==HEX_0X) {
michael@0 493 *(s++)='0';
michael@0 494 *(s++)='x';
michael@0 495 } else if(hexType==HEX_0H) {
michael@0 496 *(s++)='0';
michael@0 497 }
michael@0 498
michael@0 499 /* This creates a 32-bit field */
michael@0 500 #if U_IS_BIG_ENDIAN
michael@0 501 for (i = 0; i < sizeof(uint32_t); i++)
michael@0 502 #else
michael@0 503 for (i = sizeof(uint32_t)-1; i >= 0 ; i--)
michael@0 504 #endif
michael@0 505 {
michael@0 506 uint8_t value = ptrIdx[i];
michael@0 507 if (value || seenNonZero) {
michael@0 508 *(s++)=hexToStr[value>>4];
michael@0 509 *(s++)=hexToStr[value&0xF];
michael@0 510 seenNonZero = 1;
michael@0 511 }
michael@0 512 }
michael@0 513 if(hexType==HEX_0H) {
michael@0 514 *(s++)='h';
michael@0 515 }
michael@0 516 }
michael@0 517
michael@0 518 *(s++)=0;
michael@0 519 T_FileStream_writeLine(out, bitFieldStr);
michael@0 520 return column;
michael@0 521 }
michael@0 522
michael@0 523 static uint32_t
michael@0 524 write8(FileStream *out, uint8_t byte, uint32_t column) {
michael@0 525 char s[4];
michael@0 526 int i=0;
michael@0 527
michael@0 528 /* convert the byte value to a string */
michael@0 529 if(byte>=100) {
michael@0 530 s[i++]=(char)('0'+byte/100);
michael@0 531 byte%=100;
michael@0 532 }
michael@0 533 if(i>0 || byte>=10) {
michael@0 534 s[i++]=(char)('0'+byte/10);
michael@0 535 byte%=10;
michael@0 536 }
michael@0 537 s[i++]=(char)('0'+byte);
michael@0 538 s[i]=0;
michael@0 539
michael@0 540 /* write the value, possibly with comma and newline */
michael@0 541 if(column==MAX_COLUMN) {
michael@0 542 /* first byte */
michael@0 543 column=1;
michael@0 544 } else if(column<16) {
michael@0 545 T_FileStream_writeLine(out, ",");
michael@0 546 ++column;
michael@0 547 } else {
michael@0 548 T_FileStream_writeLine(out, ",\n");
michael@0 549 column=1;
michael@0 550 }
michael@0 551 T_FileStream_writeLine(out, s);
michael@0 552 return column;
michael@0 553 }
michael@0 554
michael@0 555 #if U_PLATFORM == U_PF_OS400
michael@0 556 static uint32_t
michael@0 557 write8str(FileStream *out, uint8_t byte, uint32_t column) {
michael@0 558 char s[8];
michael@0 559
michael@0 560 if (byte > 7)
michael@0 561 sprintf(s, "\\x%X", byte);
michael@0 562 else
michael@0 563 sprintf(s, "\\%X", byte);
michael@0 564
michael@0 565 /* write the value, possibly with comma and newline */
michael@0 566 if(column==MAX_COLUMN) {
michael@0 567 /* first byte */
michael@0 568 column=1;
michael@0 569 T_FileStream_writeLine(out, "\"");
michael@0 570 } else if(column<24) {
michael@0 571 ++column;
michael@0 572 } else {
michael@0 573 T_FileStream_writeLine(out, "\"\n\"");
michael@0 574 column=1;
michael@0 575 }
michael@0 576 T_FileStream_writeLine(out, s);
michael@0 577 return column;
michael@0 578 }
michael@0 579 #endif
michael@0 580
michael@0 581 static void
michael@0 582 getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix, const char *optFilename) {
michael@0 583 const char *basename=findBasename(inFilename), *suffix=uprv_strrchr(basename, '.');
michael@0 584
michael@0 585 /* copy path */
michael@0 586 if(destdir!=NULL && *destdir!=0) {
michael@0 587 do {
michael@0 588 *outFilename++=*destdir++;
michael@0 589 } while(*destdir!=0);
michael@0 590 if(*(outFilename-1)!=U_FILE_SEP_CHAR) {
michael@0 591 *outFilename++=U_FILE_SEP_CHAR;
michael@0 592 }
michael@0 593 inFilename=basename;
michael@0 594 } else {
michael@0 595 while(inFilename<basename) {
michael@0 596 *outFilename++=*inFilename++;
michael@0 597 }
michael@0 598 }
michael@0 599
michael@0 600 if(suffix==NULL) {
michael@0 601 /* the filename does not have a suffix */
michael@0 602 uprv_strcpy(entryName, inFilename);
michael@0 603 if(optFilename != NULL) {
michael@0 604 uprv_strcpy(outFilename, optFilename);
michael@0 605 } else {
michael@0 606 uprv_strcpy(outFilename, inFilename);
michael@0 607 }
michael@0 608 uprv_strcat(outFilename, newSuffix);
michael@0 609 } else {
michael@0 610 char *saveOutFilename = outFilename;
michael@0 611 /* copy basename */
michael@0 612 while(inFilename<suffix) {
michael@0 613 if(*inFilename=='-') {
michael@0 614 /* iSeries cannot have '-' in the .o objects. */
michael@0 615 *outFilename++=*entryName++='_';
michael@0 616 inFilename++;
michael@0 617 }
michael@0 618 else {
michael@0 619 *outFilename++=*entryName++=*inFilename++;
michael@0 620 }
michael@0 621 }
michael@0 622
michael@0 623 /* replace '.' by '_' */
michael@0 624 *outFilename++=*entryName++='_';
michael@0 625 ++inFilename;
michael@0 626
michael@0 627 /* copy suffix */
michael@0 628 while(*inFilename!=0) {
michael@0 629 *outFilename++=*entryName++=*inFilename++;
michael@0 630 }
michael@0 631
michael@0 632 *entryName=0;
michael@0 633
michael@0 634 if(optFilename != NULL) {
michael@0 635 uprv_strcpy(saveOutFilename, optFilename);
michael@0 636 uprv_strcat(saveOutFilename, newSuffix);
michael@0 637 } else {
michael@0 638 /* add ".c" */
michael@0 639 uprv_strcpy(outFilename, newSuffix);
michael@0 640 }
michael@0 641 }
michael@0 642 }
michael@0 643
michael@0 644 #ifdef CAN_GENERATE_OBJECTS
michael@0 645 static void
michael@0 646 getArchitecture(uint16_t *pCPU, uint16_t *pBits, UBool *pIsBigEndian, const char *optMatchArch) {
michael@0 647 union {
michael@0 648 char bytes[2048];
michael@0 649 #ifdef U_ELF
michael@0 650 Elf32_Ehdr header32;
michael@0 651 /* Elf32_Ehdr and ELF64_Ehdr are identical for the necessary fields. */
michael@0 652 #elif U_PLATFORM_HAS_WIN32_API
michael@0 653 IMAGE_FILE_HEADER header;
michael@0 654 #endif
michael@0 655 } buffer;
michael@0 656
michael@0 657 const char *filename;
michael@0 658 FileStream *in;
michael@0 659 int32_t length;
michael@0 660
michael@0 661 #ifdef U_ELF
michael@0 662
michael@0 663 #elif U_PLATFORM_HAS_WIN32_API
michael@0 664 const IMAGE_FILE_HEADER *pHeader;
michael@0 665 #else
michael@0 666 # error "Unknown platform for CAN_GENERATE_OBJECTS."
michael@0 667 #endif
michael@0 668
michael@0 669 if(optMatchArch != NULL) {
michael@0 670 filename=optMatchArch;
michael@0 671 } else {
michael@0 672 /* set defaults */
michael@0 673 #ifdef U_ELF
michael@0 674 /* set EM_386 because elf.h does not provide better defaults */
michael@0 675 *pCPU=EM_386;
michael@0 676 *pBits=32;
michael@0 677 *pIsBigEndian=(UBool)(U_IS_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB);
michael@0 678 #elif U_PLATFORM_HAS_WIN32_API
michael@0 679 /* _M_IA64 should be defined in windows.h */
michael@0 680 # if defined(_M_IA64)
michael@0 681 *pCPU=IMAGE_FILE_MACHINE_IA64;
michael@0 682 # elif defined(_M_AMD64)
michael@0 683 *pCPU=IMAGE_FILE_MACHINE_AMD64;
michael@0 684 # else
michael@0 685 *pCPU=IMAGE_FILE_MACHINE_I386;
michael@0 686 # endif
michael@0 687 *pBits= *pCPU==IMAGE_FILE_MACHINE_I386 ? 32 : 64;
michael@0 688 *pIsBigEndian=FALSE;
michael@0 689 #else
michael@0 690 # error "Unknown platform for CAN_GENERATE_OBJECTS."
michael@0 691 #endif
michael@0 692 return;
michael@0 693 }
michael@0 694
michael@0 695 in=T_FileStream_open(filename, "rb");
michael@0 696 if(in==NULL) {
michael@0 697 fprintf(stderr, "genccode: unable to open match-arch file %s\n", filename);
michael@0 698 exit(U_FILE_ACCESS_ERROR);
michael@0 699 }
michael@0 700 length=T_FileStream_read(in, buffer.bytes, sizeof(buffer.bytes));
michael@0 701
michael@0 702 #ifdef U_ELF
michael@0 703 if(length<sizeof(Elf32_Ehdr)) {
michael@0 704 fprintf(stderr, "genccode: match-arch file %s is too short\n", filename);
michael@0 705 exit(U_UNSUPPORTED_ERROR);
michael@0 706 }
michael@0 707 if(
michael@0 708 buffer.header32.e_ident[0]!=ELFMAG0 ||
michael@0 709 buffer.header32.e_ident[1]!=ELFMAG1 ||
michael@0 710 buffer.header32.e_ident[2]!=ELFMAG2 ||
michael@0 711 buffer.header32.e_ident[3]!=ELFMAG3 ||
michael@0 712 buffer.header32.e_ident[EI_CLASS]<ELFCLASS32 || buffer.header32.e_ident[EI_CLASS]>ELFCLASS64
michael@0 713 ) {
michael@0 714 fprintf(stderr, "genccode: match-arch file %s is not an ELF object file, or not supported\n", filename);
michael@0 715 exit(U_UNSUPPORTED_ERROR);
michael@0 716 }
michael@0 717
michael@0 718 *pBits= buffer.header32.e_ident[EI_CLASS]==ELFCLASS32 ? 32 : 64; /* only 32 or 64: see check above */
michael@0 719 #ifdef U_ELF64
michael@0 720 if(*pBits!=32 && *pBits!=64) {
michael@0 721 fprintf(stderr, "genccode: currently only supports 32-bit and 64-bit ELF format\n");
michael@0 722 exit(U_UNSUPPORTED_ERROR);
michael@0 723 }
michael@0 724 #else
michael@0 725 if(*pBits!=32) {
michael@0 726 fprintf(stderr, "genccode: built with elf.h missing 64-bit definitions\n");
michael@0 727 exit(U_UNSUPPORTED_ERROR);
michael@0 728 }
michael@0 729 #endif
michael@0 730
michael@0 731 *pIsBigEndian=(UBool)(buffer.header32.e_ident[EI_DATA]==ELFDATA2MSB);
michael@0 732 if(*pIsBigEndian!=U_IS_BIG_ENDIAN) {
michael@0 733 fprintf(stderr, "genccode: currently only same-endianness ELF formats are supported\n");
michael@0 734 exit(U_UNSUPPORTED_ERROR);
michael@0 735 }
michael@0 736 /* TODO: Support byte swapping */
michael@0 737
michael@0 738 *pCPU=buffer.header32.e_machine;
michael@0 739 #elif U_PLATFORM_HAS_WIN32_API
michael@0 740 if(length<sizeof(IMAGE_FILE_HEADER)) {
michael@0 741 fprintf(stderr, "genccode: match-arch file %s is too short\n", filename);
michael@0 742 exit(U_UNSUPPORTED_ERROR);
michael@0 743 }
michael@0 744 /* TODO: Use buffer.header. Keep aliasing legal. */
michael@0 745 pHeader=(const IMAGE_FILE_HEADER *)buffer.bytes;
michael@0 746 *pCPU=pHeader->Machine;
michael@0 747 /*
michael@0 748 * The number of bits is implicit with the Machine value.
michael@0 749 * *pBits is ignored in the calling code, so this need not be precise.
michael@0 750 */
michael@0 751 *pBits= *pCPU==IMAGE_FILE_MACHINE_I386 ? 32 : 64;
michael@0 752 /* Windows always runs on little-endian CPUs. */
michael@0 753 *pIsBigEndian=FALSE;
michael@0 754 #else
michael@0 755 # error "Unknown platform for CAN_GENERATE_OBJECTS."
michael@0 756 #endif
michael@0 757
michael@0 758 T_FileStream_close(in);
michael@0 759 }
michael@0 760
michael@0 761 U_CAPI void U_EXPORT2
michael@0 762 writeObjectCode(const char *filename, const char *destdir, const char *optEntryPoint, const char *optMatchArch, const char *optFilename, char *outFilePath) {
michael@0 763 /* common variables */
michael@0 764 char buffer[4096], entry[40]={ 0 };
michael@0 765 FileStream *in, *out;
michael@0 766 const char *newSuffix;
michael@0 767 int32_t i, entryLength, length, size, entryOffset=0, entryLengthOffset=0;
michael@0 768
michael@0 769 uint16_t cpu, bits;
michael@0 770 UBool makeBigEndian;
michael@0 771
michael@0 772 /* platform-specific variables and initialization code */
michael@0 773 #ifdef U_ELF
michael@0 774 /* 32-bit Elf file header */
michael@0 775 static Elf32_Ehdr header32={
michael@0 776 {
michael@0 777 /* e_ident[] */
michael@0 778 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
michael@0 779 ELFCLASS32,
michael@0 780 U_IS_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB,
michael@0 781 EV_CURRENT /* EI_VERSION */
michael@0 782 },
michael@0 783 ET_REL,
michael@0 784 EM_386,
michael@0 785 EV_CURRENT, /* e_version */
michael@0 786 0, /* e_entry */
michael@0 787 0, /* e_phoff */
michael@0 788 (Elf32_Off)sizeof(Elf32_Ehdr), /* e_shoff */
michael@0 789 0, /* e_flags */
michael@0 790 (Elf32_Half)sizeof(Elf32_Ehdr), /* eh_size */
michael@0 791 0, /* e_phentsize */
michael@0 792 0, /* e_phnum */
michael@0 793 (Elf32_Half)sizeof(Elf32_Shdr), /* e_shentsize */
michael@0 794 5, /* e_shnum */
michael@0 795 2 /* e_shstrndx */
michael@0 796 };
michael@0 797
michael@0 798 /* 32-bit Elf section header table */
michael@0 799 static Elf32_Shdr sectionHeaders32[5]={
michael@0 800 { /* SHN_UNDEF */
michael@0 801 0
michael@0 802 },
michael@0 803 { /* .symtab */
michael@0 804 1, /* sh_name */
michael@0 805 SHT_SYMTAB,
michael@0 806 0, /* sh_flags */
michael@0 807 0, /* sh_addr */
michael@0 808 (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)), /* sh_offset */
michael@0 809 (Elf32_Word)(2*sizeof(Elf32_Sym)), /* sh_size */
michael@0 810 3, /* sh_link=sect hdr index of .strtab */
michael@0 811 1, /* sh_info=One greater than the symbol table index of the last
michael@0 812 * local symbol (with STB_LOCAL). */
michael@0 813 4, /* sh_addralign */
michael@0 814 (Elf32_Word)(sizeof(Elf32_Sym)) /* sh_entsize */
michael@0 815 },
michael@0 816 { /* .shstrtab */
michael@0 817 9, /* sh_name */
michael@0 818 SHT_STRTAB,
michael@0 819 0, /* sh_flags */
michael@0 820 0, /* sh_addr */
michael@0 821 (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)+2*sizeof(Elf32_Sym)), /* sh_offset */
michael@0 822 40, /* sh_size */
michael@0 823 0, /* sh_link */
michael@0 824 0, /* sh_info */
michael@0 825 1, /* sh_addralign */
michael@0 826 0 /* sh_entsize */
michael@0 827 },
michael@0 828 { /* .strtab */
michael@0 829 19, /* sh_name */
michael@0 830 SHT_STRTAB,
michael@0 831 0, /* sh_flags */
michael@0 832 0, /* sh_addr */
michael@0 833 (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)+2*sizeof(Elf32_Sym)+40), /* sh_offset */
michael@0 834 (Elf32_Word)sizeof(entry), /* sh_size */
michael@0 835 0, /* sh_link */
michael@0 836 0, /* sh_info */
michael@0 837 1, /* sh_addralign */
michael@0 838 0 /* sh_entsize */
michael@0 839 },
michael@0 840 { /* .rodata */
michael@0 841 27, /* sh_name */
michael@0 842 SHT_PROGBITS,
michael@0 843 SHF_ALLOC, /* sh_flags */
michael@0 844 0, /* sh_addr */
michael@0 845 (Elf32_Off)(sizeof(header32)+sizeof(sectionHeaders32)+2*sizeof(Elf32_Sym)+40+sizeof(entry)), /* sh_offset */
michael@0 846 0, /* sh_size */
michael@0 847 0, /* sh_link */
michael@0 848 0, /* sh_info */
michael@0 849 16, /* sh_addralign */
michael@0 850 0 /* sh_entsize */
michael@0 851 }
michael@0 852 };
michael@0 853
michael@0 854 /* symbol table */
michael@0 855 static Elf32_Sym symbols32[2]={
michael@0 856 { /* STN_UNDEF */
michael@0 857 0
michael@0 858 },
michael@0 859 { /* data entry point */
michael@0 860 1, /* st_name */
michael@0 861 0, /* st_value */
michael@0 862 0, /* st_size */
michael@0 863 ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT),
michael@0 864 0, /* st_other */
michael@0 865 4 /* st_shndx=index of related section table entry */
michael@0 866 }
michael@0 867 };
michael@0 868
michael@0 869 /* section header string table, with decimal string offsets */
michael@0 870 static const char sectionStrings[40]=
michael@0 871 /* 0 */ "\0"
michael@0 872 /* 1 */ ".symtab\0"
michael@0 873 /* 9 */ ".shstrtab\0"
michael@0 874 /* 19 */ ".strtab\0"
michael@0 875 /* 27 */ ".rodata\0"
michael@0 876 /* 35 */ "\0\0\0\0"; /* contains terminating NUL */
michael@0 877 /* 40: padded to multiple of 8 bytes */
michael@0 878
michael@0 879 /*
michael@0 880 * Use entry[] for the string table which will contain only the
michael@0 881 * entry point name.
michael@0 882 * entry[0] must be 0 (NUL)
michael@0 883 * The entry point name can be up to 38 characters long (sizeof(entry)-2).
michael@0 884 */
michael@0 885
michael@0 886 /* 16-align .rodata in the .o file, just in case */
michael@0 887 static const char padding[16]={ 0 };
michael@0 888 int32_t paddingSize;
michael@0 889
michael@0 890 #ifdef U_ELF64
michael@0 891 /* 64-bit Elf file header */
michael@0 892 static Elf64_Ehdr header64={
michael@0 893 {
michael@0 894 /* e_ident[] */
michael@0 895 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
michael@0 896 ELFCLASS64,
michael@0 897 U_IS_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB,
michael@0 898 EV_CURRENT /* EI_VERSION */
michael@0 899 },
michael@0 900 ET_REL,
michael@0 901 EM_X86_64,
michael@0 902 EV_CURRENT, /* e_version */
michael@0 903 0, /* e_entry */
michael@0 904 0, /* e_phoff */
michael@0 905 (Elf64_Off)sizeof(Elf64_Ehdr), /* e_shoff */
michael@0 906 0, /* e_flags */
michael@0 907 (Elf64_Half)sizeof(Elf64_Ehdr), /* eh_size */
michael@0 908 0, /* e_phentsize */
michael@0 909 0, /* e_phnum */
michael@0 910 (Elf64_Half)sizeof(Elf64_Shdr), /* e_shentsize */
michael@0 911 5, /* e_shnum */
michael@0 912 2 /* e_shstrndx */
michael@0 913 };
michael@0 914
michael@0 915 /* 64-bit Elf section header table */
michael@0 916 static Elf64_Shdr sectionHeaders64[5]={
michael@0 917 { /* SHN_UNDEF */
michael@0 918 0
michael@0 919 },
michael@0 920 { /* .symtab */
michael@0 921 1, /* sh_name */
michael@0 922 SHT_SYMTAB,
michael@0 923 0, /* sh_flags */
michael@0 924 0, /* sh_addr */
michael@0 925 (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)), /* sh_offset */
michael@0 926 (Elf64_Xword)(2*sizeof(Elf64_Sym)), /* sh_size */
michael@0 927 3, /* sh_link=sect hdr index of .strtab */
michael@0 928 1, /* sh_info=One greater than the symbol table index of the last
michael@0 929 * local symbol (with STB_LOCAL). */
michael@0 930 4, /* sh_addralign */
michael@0 931 (Elf64_Xword)(sizeof(Elf64_Sym)) /* sh_entsize */
michael@0 932 },
michael@0 933 { /* .shstrtab */
michael@0 934 9, /* sh_name */
michael@0 935 SHT_STRTAB,
michael@0 936 0, /* sh_flags */
michael@0 937 0, /* sh_addr */
michael@0 938 (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)+2*sizeof(Elf64_Sym)), /* sh_offset */
michael@0 939 40, /* sh_size */
michael@0 940 0, /* sh_link */
michael@0 941 0, /* sh_info */
michael@0 942 1, /* sh_addralign */
michael@0 943 0 /* sh_entsize */
michael@0 944 },
michael@0 945 { /* .strtab */
michael@0 946 19, /* sh_name */
michael@0 947 SHT_STRTAB,
michael@0 948 0, /* sh_flags */
michael@0 949 0, /* sh_addr */
michael@0 950 (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)+2*sizeof(Elf64_Sym)+40), /* sh_offset */
michael@0 951 (Elf64_Xword)sizeof(entry), /* sh_size */
michael@0 952 0, /* sh_link */
michael@0 953 0, /* sh_info */
michael@0 954 1, /* sh_addralign */
michael@0 955 0 /* sh_entsize */
michael@0 956 },
michael@0 957 { /* .rodata */
michael@0 958 27, /* sh_name */
michael@0 959 SHT_PROGBITS,
michael@0 960 SHF_ALLOC, /* sh_flags */
michael@0 961 0, /* sh_addr */
michael@0 962 (Elf64_Off)(sizeof(header64)+sizeof(sectionHeaders64)+2*sizeof(Elf64_Sym)+40+sizeof(entry)), /* sh_offset */
michael@0 963 0, /* sh_size */
michael@0 964 0, /* sh_link */
michael@0 965 0, /* sh_info */
michael@0 966 16, /* sh_addralign */
michael@0 967 0 /* sh_entsize */
michael@0 968 }
michael@0 969 };
michael@0 970
michael@0 971 /*
michael@0 972 * 64-bit symbol table
michael@0 973 * careful: different order of items compared with Elf32_sym!
michael@0 974 */
michael@0 975 static Elf64_Sym symbols64[2]={
michael@0 976 { /* STN_UNDEF */
michael@0 977 0
michael@0 978 },
michael@0 979 { /* data entry point */
michael@0 980 1, /* st_name */
michael@0 981 ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT),
michael@0 982 0, /* st_other */
michael@0 983 4, /* st_shndx=index of related section table entry */
michael@0 984 0, /* st_value */
michael@0 985 0 /* st_size */
michael@0 986 }
michael@0 987 };
michael@0 988
michael@0 989 #endif /* U_ELF64 */
michael@0 990
michael@0 991 /* entry[] have a leading NUL */
michael@0 992 entryOffset=1;
michael@0 993
michael@0 994 /* in the common code, count entryLength from after the NUL */
michael@0 995 entryLengthOffset=1;
michael@0 996
michael@0 997 newSuffix=".o";
michael@0 998
michael@0 999 #elif U_PLATFORM_HAS_WIN32_API
michael@0 1000 struct {
michael@0 1001 IMAGE_FILE_HEADER fileHeader;
michael@0 1002 IMAGE_SECTION_HEADER sections[2];
michael@0 1003 char linkerOptions[100];
michael@0 1004 } objHeader;
michael@0 1005 IMAGE_SYMBOL symbols[1];
michael@0 1006 struct {
michael@0 1007 DWORD sizeofLongNames;
michael@0 1008 char longNames[100];
michael@0 1009 } symbolNames;
michael@0 1010
michael@0 1011 /*
michael@0 1012 * entry sometimes have a leading '_'
michael@0 1013 * overwritten if entryOffset==0 depending on the target platform
michael@0 1014 * see check for cpu below
michael@0 1015 */
michael@0 1016 entry[0]='_';
michael@0 1017
michael@0 1018 newSuffix=".obj";
michael@0 1019 #else
michael@0 1020 # error "Unknown platform for CAN_GENERATE_OBJECTS."
michael@0 1021 #endif
michael@0 1022
michael@0 1023 /* deal with options, files and the entry point name */
michael@0 1024 getArchitecture(&cpu, &bits, &makeBigEndian, optMatchArch);
michael@0 1025 printf("genccode: --match-arch cpu=%hu bits=%hu big-endian=%d\n", cpu, bits, makeBigEndian);
michael@0 1026 #if U_PLATFORM_HAS_WIN32_API
michael@0 1027 if(cpu==IMAGE_FILE_MACHINE_I386) {
michael@0 1028 entryOffset=1;
michael@0 1029 }
michael@0 1030 #endif
michael@0 1031
michael@0 1032 in=T_FileStream_open(filename, "rb");
michael@0 1033 if(in==NULL) {
michael@0 1034 fprintf(stderr, "genccode: unable to open input file %s\n", filename);
michael@0 1035 exit(U_FILE_ACCESS_ERROR);
michael@0 1036 }
michael@0 1037 size=T_FileStream_size(in);
michael@0 1038
michael@0 1039 getOutFilename(filename, destdir, buffer, entry+entryOffset, newSuffix, optFilename);
michael@0 1040 if (outFilePath != NULL) {
michael@0 1041 uprv_strcpy(outFilePath, buffer);
michael@0 1042 }
michael@0 1043
michael@0 1044 if(optEntryPoint != NULL) {
michael@0 1045 uprv_strcpy(entry+entryOffset, optEntryPoint);
michael@0 1046 uprv_strcat(entry+entryOffset, "_dat");
michael@0 1047 }
michael@0 1048 /* turn dashes in the entry name into underscores */
michael@0 1049 entryLength=(int32_t)uprv_strlen(entry+entryLengthOffset);
michael@0 1050 for(i=0; i<entryLength; ++i) {
michael@0 1051 if(entry[entryLengthOffset+i]=='-') {
michael@0 1052 entry[entryLengthOffset+i]='_';
michael@0 1053 }
michael@0 1054 }
michael@0 1055
michael@0 1056 /* open the output file */
michael@0 1057 out=T_FileStream_open(buffer, "wb");
michael@0 1058 if(out==NULL) {
michael@0 1059 fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
michael@0 1060 exit(U_FILE_ACCESS_ERROR);
michael@0 1061 }
michael@0 1062
michael@0 1063 #ifdef U_ELF
michael@0 1064 if(bits==32) {
michael@0 1065 header32.e_ident[EI_DATA]= makeBigEndian ? ELFDATA2MSB : ELFDATA2LSB;
michael@0 1066 header32.e_machine=cpu;
michael@0 1067
michael@0 1068 /* 16-align .rodata in the .o file, just in case */
michael@0 1069 paddingSize=sectionHeaders32[4].sh_offset & 0xf;
michael@0 1070 if(paddingSize!=0) {
michael@0 1071 paddingSize=0x10-paddingSize;
michael@0 1072 sectionHeaders32[4].sh_offset+=paddingSize;
michael@0 1073 }
michael@0 1074
michael@0 1075 sectionHeaders32[4].sh_size=(Elf32_Word)size;
michael@0 1076
michael@0 1077 symbols32[1].st_size=(Elf32_Word)size;
michael@0 1078
michael@0 1079 /* write .o headers */
michael@0 1080 T_FileStream_write(out, &header32, (int32_t)sizeof(header32));
michael@0 1081 T_FileStream_write(out, sectionHeaders32, (int32_t)sizeof(sectionHeaders32));
michael@0 1082 T_FileStream_write(out, symbols32, (int32_t)sizeof(symbols32));
michael@0 1083 } else /* bits==64 */ {
michael@0 1084 #ifdef U_ELF64
michael@0 1085 header64.e_ident[EI_DATA]= makeBigEndian ? ELFDATA2MSB : ELFDATA2LSB;
michael@0 1086 header64.e_machine=cpu;
michael@0 1087
michael@0 1088 /* 16-align .rodata in the .o file, just in case */
michael@0 1089 paddingSize=sectionHeaders64[4].sh_offset & 0xf;
michael@0 1090 if(paddingSize!=0) {
michael@0 1091 paddingSize=0x10-paddingSize;
michael@0 1092 sectionHeaders64[4].sh_offset+=paddingSize;
michael@0 1093 }
michael@0 1094
michael@0 1095 sectionHeaders64[4].sh_size=(Elf64_Xword)size;
michael@0 1096
michael@0 1097 symbols64[1].st_size=(Elf64_Xword)size;
michael@0 1098
michael@0 1099 /* write .o headers */
michael@0 1100 T_FileStream_write(out, &header64, (int32_t)sizeof(header64));
michael@0 1101 T_FileStream_write(out, sectionHeaders64, (int32_t)sizeof(sectionHeaders64));
michael@0 1102 T_FileStream_write(out, symbols64, (int32_t)sizeof(symbols64));
michael@0 1103 #endif
michael@0 1104 }
michael@0 1105
michael@0 1106 T_FileStream_write(out, sectionStrings, (int32_t)sizeof(sectionStrings));
michael@0 1107 T_FileStream_write(out, entry, (int32_t)sizeof(entry));
michael@0 1108 if(paddingSize!=0) {
michael@0 1109 T_FileStream_write(out, padding, paddingSize);
michael@0 1110 }
michael@0 1111 #elif U_PLATFORM_HAS_WIN32_API
michael@0 1112 /* populate the .obj headers */
michael@0 1113 uprv_memset(&objHeader, 0, sizeof(objHeader));
michael@0 1114 uprv_memset(&symbols, 0, sizeof(symbols));
michael@0 1115 uprv_memset(&symbolNames, 0, sizeof(symbolNames));
michael@0 1116
michael@0 1117 /* write the linker export directive */
michael@0 1118 uprv_strcpy(objHeader.linkerOptions, "-export:");
michael@0 1119 length=8;
michael@0 1120 uprv_strcpy(objHeader.linkerOptions+length, entry);
michael@0 1121 length+=entryLength;
michael@0 1122 uprv_strcpy(objHeader.linkerOptions+length, ",data ");
michael@0 1123 length+=6;
michael@0 1124
michael@0 1125 /* set the file header */
michael@0 1126 objHeader.fileHeader.Machine=cpu;
michael@0 1127 objHeader.fileHeader.NumberOfSections=2;
michael@0 1128 objHeader.fileHeader.TimeDateStamp=(DWORD)time(NULL);
michael@0 1129 objHeader.fileHeader.PointerToSymbolTable=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER+length+size; /* start of symbol table */
michael@0 1130 objHeader.fileHeader.NumberOfSymbols=1;
michael@0 1131
michael@0 1132 /* set the section for the linker options */
michael@0 1133 uprv_strncpy((char *)objHeader.sections[0].Name, ".drectve", 8);
michael@0 1134 objHeader.sections[0].SizeOfRawData=length;
michael@0 1135 objHeader.sections[0].PointerToRawData=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER;
michael@0 1136 objHeader.sections[0].Characteristics=IMAGE_SCN_LNK_INFO|IMAGE_SCN_LNK_REMOVE|IMAGE_SCN_ALIGN_1BYTES;
michael@0 1137
michael@0 1138 /* set the data section */
michael@0 1139 uprv_strncpy((char *)objHeader.sections[1].Name, ".rdata", 6);
michael@0 1140 objHeader.sections[1].SizeOfRawData=size;
michael@0 1141 objHeader.sections[1].PointerToRawData=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER+length;
michael@0 1142 objHeader.sections[1].Characteristics=IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_ALIGN_16BYTES|IMAGE_SCN_MEM_READ;
michael@0 1143
michael@0 1144 /* set the symbol table */
michael@0 1145 if(entryLength<=8) {
michael@0 1146 uprv_strncpy((char *)symbols[0].N.ShortName, entry, entryLength);
michael@0 1147 symbolNames.sizeofLongNames=4;
michael@0 1148 } else {
michael@0 1149 symbols[0].N.Name.Short=0;
michael@0 1150 symbols[0].N.Name.Long=4;
michael@0 1151 symbolNames.sizeofLongNames=4+entryLength+1;
michael@0 1152 uprv_strcpy(symbolNames.longNames, entry);
michael@0 1153 }
michael@0 1154 symbols[0].SectionNumber=2;
michael@0 1155 symbols[0].StorageClass=IMAGE_SYM_CLASS_EXTERNAL;
michael@0 1156
michael@0 1157 /* write the file header and the linker options section */
michael@0 1158 T_FileStream_write(out, &objHeader, objHeader.sections[1].PointerToRawData);
michael@0 1159 #else
michael@0 1160 # error "Unknown platform for CAN_GENERATE_OBJECTS."
michael@0 1161 #endif
michael@0 1162
michael@0 1163 /* copy the data file into section 2 */
michael@0 1164 for(;;) {
michael@0 1165 length=T_FileStream_read(in, buffer, sizeof(buffer));
michael@0 1166 if(length==0) {
michael@0 1167 break;
michael@0 1168 }
michael@0 1169 T_FileStream_write(out, buffer, (int32_t)length);
michael@0 1170 }
michael@0 1171
michael@0 1172 #if U_PLATFORM_HAS_WIN32_API
michael@0 1173 /* write the symbol table */
michael@0 1174 T_FileStream_write(out, symbols, IMAGE_SIZEOF_SYMBOL);
michael@0 1175 T_FileStream_write(out, &symbolNames, symbolNames.sizeofLongNames);
michael@0 1176 #endif
michael@0 1177
michael@0 1178 if(T_FileStream_error(in)) {
michael@0 1179 fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
michael@0 1180 exit(U_FILE_ACCESS_ERROR);
michael@0 1181 }
michael@0 1182
michael@0 1183 if(T_FileStream_error(out)) {
michael@0 1184 fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
michael@0 1185 exit(U_FILE_ACCESS_ERROR);
michael@0 1186 }
michael@0 1187
michael@0 1188 T_FileStream_close(out);
michael@0 1189 T_FileStream_close(in);
michael@0 1190 }
michael@0 1191 #endif

mercurial