2 *******************************************************************************
3 * Copyright (C) 1999-2007, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: gennames.c
8 * tab size: 8 (not used)
11 * created on: 1999nov01
12 * created by: Markus W. Scherer
14 * This program reads a binary file and creates a C source code file
15 * with a byte array that contains the data of the binary file.
17 * 12/09/1999 weiv Added multiple file handling
20 #include "unicode/utypes.h"
24 # define WIN32_LEAN_AND_MEAN
39 # if defined(ELFCLASS64)
42 /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */
46 # define ICU_ENTRY_OFFSET 0
51 #include "unicode/putil.h"
56 #include "unicode/uclean.h"
59 #define MAX_COLUMN ((uint32_t)(0xFFFFFFFFU))
61 static uint32_t column
=MAX_COLUMN
;
63 #if defined(U_WINDOWS) || defined(U_ELF)
64 #define CAN_GENERATE_OBJECTS
67 /* prototypes --------------------------------------------------------------- */
70 writeCCode(const char *filename
, const char *destdir
);
73 writeAssemblyCode(const char *filename
, const char *destdir
);
75 #ifdef CAN_GENERATE_OBJECTS
77 writeObjectCode(const char *filename
, const char *destdir
);
81 getOutFilename(const char *inFilename
, const char *destdir
, char *outFilename
, char *entryName
, const char *newSuffix
);
84 write8(FileStream
*out
, uint8_t byte
);
87 write32(FileStream
*out
, uint32_t byte
);
91 write8str(FileStream
*out
, uint8_t byte
);
93 /* -------------------------------------------------------------------------- */
101 #ifdef CAN_GENERATE_OBJECTS
110 Creating Template Files for New Platforms
112 Let the cc compiler help you get started.
114 const unsigned int x[5] = {1, 2, 0xdeadbeef, 0xffffffff, 16};
115 with the -S option to produce assembly output.
117 For example, this will generate array.s:
120 This will produce a .s file that may look like this:
136 .ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.1 2.96-85)"
138 which gives a starting point that will compile, and can be transformed
139 to become the template, generally with some consulting of as docs and
140 some experimentation.
142 If you want ICU to automatically use this assembly, you should
143 specify "GENCCODE_ASSEMBLY=-a name" in the specific config/mh-* file,
144 where the name is the compiler or platform that you used in this
145 assemblyHeader data structure.
147 static const struct AssemblyType
{
150 const char *beginLine
;
151 } assemblyHeader
[] = {
154 "\t.section .note.GNU-stack,\"\",@progbits\n"
155 "\t.section .rodata\n"
156 "\t.align 8\n" /* Either align 8 bytes or 2^8 (256) bytes. 8 bytes is needed. */
157 "\t.type %s,@object\n"
163 /*"\t.section __TEXT,__text,regular,pure_instructions\n"
164 "\t.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"*/
168 "\t.align 4\n" /* 1<<4 = 16 */
175 "\t.section .rodata\n"
176 "\t.align 8\n" /* Either align 8 bytes or 2^8 (256) bytes. 8 bytes is needed. */
182 "\t.section \".rodata\"\n"
191 "\t.type Drodata.rodata,@object\n"
192 "\t.size Drodata.rodata,0\n"
203 "\t.csect %s{RO}, 4\n",
209 "\t.type %s,@object\n"
211 "\t.secalias .abe$0.rodata, \".rodata\"\n"
212 "\t.section .abe$0.rodata = \"a\", \"progbits\"\n"
229 static int32_t assemblyHeaderIndex
= -1;
231 static UOption options
[]={
233 UOPTION_HELP_QUESTION_MARK
,
235 UOPTION_DEF("name", 'n', UOPT_REQUIRES_ARG
),
236 UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG
),
237 #ifdef CAN_GENERATE_OBJECTS
238 /*5*/UOPTION_DEF("object", 'o', UOPT_NO_ARG
),
239 UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG
),
241 UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG
),
242 UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG
)
246 main(int argc
, char* argv
[]) {
247 UBool verbose
= TRUE
;
250 U_MAIN_INIT_ARGS(argc
, argv
);
252 options
[kOptDestDir
].value
= ".";
254 /* read command line options */
255 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
257 /* error handling, printing usage message */
260 "error in command line argument \"%s\"\n",
263 if(argc
<0 || options
[kOptHelpH
].doesOccur
|| options
[kOptHelpQuestionMark
].doesOccur
) {
265 "usage: %s [-options] filename1 filename2 ...\n"
266 "\tread each binary input file and \n"
267 "\tcreate a .c file with a byte array that contains the input file's data\n"
269 "\t-h or -? or --help this usage text\n"
270 "\t-d or --destdir destination directory, followed by the path\n"
271 "\t-n or --name symbol prefix, followed by the prefix\n"
272 "\t-e or --entrypoint entry point name, followed by the name (_dat will be appended)\n"
273 "\t-r or --revision Specify a version\n"
275 #ifdef CAN_GENERATE_OBJECTS
277 "\t-o or --object write a .obj file instead of .c\n"
278 "\t-m or --match-arch file.o match the architecture (CPU, 32/64 bits) of the specified .o\n"
279 "\t ELF format defaults to i386. Windows defaults to the native platform.\n");
282 "\t-f or --filename Specify an alternate base filename. (default: symbolname_typ)\n"
283 "\t-a or --assembly Create assembly file. (possible values are: ");
285 fprintf(stderr
, "%s", assemblyHeader
[0].name
);
286 for (idx
= 1; idx
< (int32_t)(sizeof(assemblyHeader
)/sizeof(assemblyHeader
[0])); idx
++) {
287 fprintf(stderr
, ", %s", assemblyHeader
[idx
].name
);
292 const char *message
, *filename
;
293 void (*writeCode
)(const char *, const char *);
295 if(options
[kOptAssembly
].doesOccur
) {
296 message
="generating assembly code for %s\n";
297 writeCode
=&writeAssemblyCode
;
298 for (idx
= 0; idx
< (int32_t)(sizeof(assemblyHeader
)/sizeof(assemblyHeader
[0])); idx
++) {
299 if (uprv_strcmp(options
[kOptAssembly
].value
, assemblyHeader
[idx
].name
) == 0) {
300 assemblyHeaderIndex
= idx
;
304 if (assemblyHeaderIndex
< 0) {
306 "Assembly type \"%s\" is unknown.\n", options
[kOptAssembly
].value
);
310 #ifdef CAN_GENERATE_OBJECTS
311 else if(options
[kOptObject
].doesOccur
) {
312 message
="generating object code for %s\n";
313 writeCode
=&writeObjectCode
;
318 message
="generating C code for %s\n";
319 writeCode
=&writeCCode
;
322 filename
=getLongPathname(argv
[argc
]);
324 fprintf(stdout
, message
, filename
);
327 writeCode(filename
, options
[kOptDestDir
].value
);
335 writeAssemblyCode(const char *filename
, const char *destdir
) {
337 uint32_t buffer
[1024];
338 char *bufferStr
= (char *)buffer
;
339 FileStream
*in
, *out
;
342 in
=T_FileStream_open(filename
, "rb");
344 fprintf(stderr
, "genccode: unable to open input file %s\n", filename
);
345 exit(U_FILE_ACCESS_ERROR
);
348 getOutFilename(filename
, destdir
, bufferStr
, entry
, ".s");
349 out
=T_FileStream_open(bufferStr
, "w");
351 fprintf(stderr
, "genccode: unable to open output file %s\n", bufferStr
);
352 exit(U_FILE_ACCESS_ERROR
);
355 if(options
[kOptEntryPoint
].doesOccur
) {
356 uprv_strcpy(entry
, options
[kOptEntryPoint
].value
);
357 uprv_strcat(entry
, "_dat");
360 /* turn dashes or dots in the entry name into underscores */
361 length
=uprv_strlen(entry
);
362 for(i
=0; i
<length
; ++i
) {
363 if(entry
[i
]=='-' || entry
[i
]=='.') {
368 sprintf(bufferStr
, assemblyHeader
[assemblyHeaderIndex
].header
,
369 entry
, entry
, entry
, entry
,
370 entry
, entry
, entry
, entry
);
371 T_FileStream_writeLine(out
, bufferStr
);
372 T_FileStream_writeLine(out
, assemblyHeader
[assemblyHeaderIndex
].beginLine
);
375 length
=T_FileStream_read(in
, buffer
, sizeof(buffer
));
379 if (length
!= sizeof(buffer
)) {
380 /* pad with extra 0's when at the end of the file */
381 for(i
=0; i
< (length
% sizeof(uint32_t)); ++i
) {
382 buffer
[length
+i
] = 0;
385 for(i
=0; i
<(length
/sizeof(buffer
[0])); i
++) {
386 write32(out
, buffer
[i
]);
390 T_FileStream_writeLine(out
, "\n");
392 if(T_FileStream_error(in
)) {
393 fprintf(stderr
, "genccode: file read error while generating from file %s\n", filename
);
394 exit(U_FILE_ACCESS_ERROR
);
397 if(T_FileStream_error(out
)) {
398 fprintf(stderr
, "genccode: file write error while generating from file %s\n", filename
);
399 exit(U_FILE_ACCESS_ERROR
);
402 T_FileStream_close(out
);
403 T_FileStream_close(in
);
407 writeCCode(const char *filename
, const char *destdir
) {
408 char buffer
[4096], entry
[64];
409 FileStream
*in
, *out
;
412 in
=T_FileStream_open(filename
, "rb");
414 fprintf(stderr
, "genccode: unable to open input file %s\n", filename
);
415 exit(U_FILE_ACCESS_ERROR
);
418 if(options
[kOptName
].doesOccur
) { /* prepend 'icudt28_' */
419 strcpy(entry
, options
[kOptName
].value
);
425 getOutFilename(filename
, destdir
, buffer
, entry
+uprv_strlen(entry
), ".c");
426 out
=T_FileStream_open(buffer
, "w");
428 fprintf(stderr
, "genccode: unable to open output file %s\n", buffer
);
429 exit(U_FILE_ACCESS_ERROR
);
432 /* turn dashes or dots in the entry name into underscores */
433 length
=uprv_strlen(entry
);
434 for(i
=0; i
<length
; ++i
) {
435 if(entry
[i
]=='-' || entry
[i
]=='.') {
442 TODO: Fix this once the compiler implements this feature. Keep in sync with udatamem.c
444 This is here because this platform can't currently put
445 const data into the read-only pages of an object or
446 shared library (service program). Only strings are allowed in read-only
447 pages, so we use char * strings to store the data.
449 In order to prevent the beginning of the data from ever matching the
450 magic numbers we must still use the initial double.
454 "#define U_DISABLE_RENAMING 1\n"
455 "#include \"unicode/umachine.h\"\n"
459 " const char *bytes; \n"
462 T_FileStream_writeLine(out
, buffer
);
465 length
=T_FileStream_read(in
, buffer
, sizeof(buffer
));
469 for(i
=0; i
<length
; ++i
) {
470 write8str(out
, (uint8_t)buffer
[i
]);
474 T_FileStream_writeLine(out
, "\"\n};\nU_CDECL_END\n");
476 /* Function renaming shouldn't be done in data */
478 "#define U_DISABLE_RENAMING 1\n"
479 "#include \"unicode/umachine.h\"\n"
483 " uint8_t bytes[%ld]; \n"
485 (long)T_FileStream_size(in
), entry
);
486 T_FileStream_writeLine(out
, buffer
);
489 length
=T_FileStream_read(in
, buffer
, sizeof(buffer
));
493 for(i
=0; i
<length
; ++i
) {
494 write8(out
, (uint8_t)buffer
[i
]);
498 T_FileStream_writeLine(out
, "\n}\n};\nU_CDECL_END\n");
501 if(T_FileStream_error(in
)) {
502 fprintf(stderr
, "genccode: file read error while generating from file %s\n", filename
);
503 exit(U_FILE_ACCESS_ERROR
);
506 if(T_FileStream_error(out
)) {
507 fprintf(stderr
, "genccode: file write error while generating from file %s\n", filename
);
508 exit(U_FILE_ACCESS_ERROR
);
511 T_FileStream_close(out
);
512 T_FileStream_close(in
);
515 #ifdef CAN_GENERATE_OBJECTS
517 getArchitecture(uint16_t *pCPU
, uint16_t *pBits
, UBool
*pIsBigEndian
) {
519 const char *filename
;
524 /* Pointer to ELF header. Elf32_Ehdr and ELF64_Ehdr are identical for the necessary fields. */
525 const Elf32_Ehdr
*pHeader32
;
526 #elif defined(U_WINDOWS)
527 const IMAGE_FILE_HEADER
*pHeader
;
529 # error "Unknown platform for CAN_GENERATE_OBJECTS."
532 if(options
[kOptMatchArch
].doesOccur
) {
533 filename
=options
[kOptMatchArch
].value
;
537 /* set EM_386 because elf.h does not provide better defaults */
540 *pIsBigEndian
=(UBool
)(U_IS_BIG_ENDIAN
? ELFDATA2MSB
: ELFDATA2LSB
);
541 #elif defined(U_WINDOWS)
542 /* _M_IA64 should be defined in windows.h */
543 # if defined(_M_IA64)
544 *pCPU
=IMAGE_FILE_MACHINE_IA64
;
545 # elif defined(_M_AMD64)
546 *pCPU
=IMAGE_FILE_MACHINE_AMD64
;
548 *pCPU
=IMAGE_FILE_MACHINE_I386
;
550 *pBits
= *pCPU
==IMAGE_FILE_MACHINE_I386
? 32 : 64;
553 # error "Unknown platform for CAN_GENERATE_OBJECTS."
558 in
=T_FileStream_open(filename
, "rb");
560 fprintf(stderr
, "genccode: unable to open match-arch file %s\n", filename
);
561 exit(U_FILE_ACCESS_ERROR
);
563 length
=T_FileStream_read(in
, buffer
, sizeof(buffer
));
566 if(length
<sizeof(Elf32_Ehdr
)) {
567 fprintf(stderr
, "genccode: match-arch file %s is too short\n", filename
);
568 exit(U_UNSUPPORTED_ERROR
);
570 pHeader32
=(const Elf32_Ehdr
*)buffer
;
572 pHeader32
->e_ident
[0]!=ELFMAG0
||
573 pHeader32
->e_ident
[1]!=ELFMAG1
||
574 pHeader32
->e_ident
[2]!=ELFMAG2
||
575 pHeader32
->e_ident
[3]!=ELFMAG3
||
576 pHeader32
->e_ident
[EI_CLASS
]<ELFCLASS32
|| pHeader32
->e_ident
[EI_CLASS
]>ELFCLASS64
578 fprintf(stderr
, "genccode: match-arch file %s is not an ELF object file, or not supported\n", filename
);
579 exit(U_UNSUPPORTED_ERROR
);
582 *pBits
= pHeader32
->e_ident
[EI_CLASS
]==ELFCLASS32
? 32 : 64; /* only 32 or 64: see check above */
584 if(*pBits
!=32 && *pBits
!=64) {
585 fprintf(stderr
, "genccode: currently only supports 32-bit and 64-bit ELF format\n");
586 exit(U_UNSUPPORTED_ERROR
);
590 fprintf(stderr
, "genccode: built with elf.h missing 64-bit definitions\n");
591 exit(U_UNSUPPORTED_ERROR
);
595 *pIsBigEndian
=(UBool
)(pHeader32
->e_ident
[EI_DATA
]==ELFDATA2MSB
);
596 if(*pIsBigEndian
!=U_IS_BIG_ENDIAN
) {
597 fprintf(stderr
, "genccode: currently only same-endianness ELF formats are supported\n");
598 exit(U_UNSUPPORTED_ERROR
);
600 /* TODO: Support byte swapping */
602 *pCPU
=pHeader32
->e_machine
;
603 #elif defined(U_WINDOWS)
604 if(length
<sizeof(IMAGE_FILE_HEADER
)) {
605 fprintf(stderr
, "genccode: match-arch file %s is too short\n", filename
);
606 exit(U_UNSUPPORTED_ERROR
);
608 pHeader
=(const IMAGE_FILE_HEADER
*)buffer
;
609 *pCPU
=pHeader
->Machine
;
611 * The number of bits is implicit with the Machine value.
612 * *pBits is ignored in the calling code, so this need not be precise.
614 *pBits
= *pCPU
==IMAGE_FILE_MACHINE_I386
? 32 : 64;
615 /* Windows always runs on little-endian CPUs. */
618 # error "Unknown platform for CAN_GENERATE_OBJECTS."
621 T_FileStream_close(in
);
625 writeObjectCode(const char *filename
, const char *destdir
) {
626 /* common variables */
627 char buffer
[4096], entry
[40]={ 0 };
628 FileStream
*in
, *out
;
629 const char *newSuffix
;
630 int32_t i
, entryLength
, length
, size
, entryOffset
=0, entryLengthOffset
=0;
635 /* platform-specific variables and initialization code */
637 /* 32-bit Elf file header */
638 static Elf32_Ehdr header32
={
641 ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
,
643 U_IS_BIG_ENDIAN
? ELFDATA2MSB
: ELFDATA2LSB
,
644 EV_CURRENT
/* EI_VERSION */
648 EV_CURRENT
, /* e_version */
651 (Elf32_Off
)sizeof(Elf32_Ehdr
), /* e_shoff */
653 (Elf32_Half
)sizeof(Elf32_Ehdr
), /* eh_size */
656 (Elf32_Half
)sizeof(Elf32_Shdr
), /* e_shentsize */
661 /* 32-bit Elf section header table */
662 static Elf32_Shdr sectionHeaders32
[5]={
671 (Elf32_Off
)(sizeof(header32
)+sizeof(sectionHeaders32
)), /* sh_offset */
672 (Elf32_Word
)(2*sizeof(Elf32_Sym
)), /* sh_size */
673 3, /* sh_link=sect hdr index of .strtab */
674 1, /* sh_info=One greater than the symbol table index of the last
675 * local symbol (with STB_LOCAL). */
676 4, /* sh_addralign */
677 (Elf32_Word
)(sizeof(Elf32_Sym
)) /* sh_entsize */
684 (Elf32_Off
)(sizeof(header32
)+sizeof(sectionHeaders32
)+2*sizeof(Elf32_Sym
)), /* sh_offset */
688 1, /* sh_addralign */
696 (Elf32_Off
)(sizeof(header32
)+sizeof(sectionHeaders32
)+2*sizeof(Elf32_Sym
)+40), /* sh_offset */
697 (Elf32_Word
)sizeof(entry
), /* sh_size */
700 1, /* sh_addralign */
706 SHF_ALLOC
, /* sh_flags */
708 (Elf32_Off
)(sizeof(header32
)+sizeof(sectionHeaders32
)+2*sizeof(Elf32_Sym
)+40+sizeof(entry
)), /* sh_offset */
712 16, /* sh_addralign */
718 static Elf32_Sym symbols32
[2]={
722 { /* data entry point */
726 ELF64_ST_INFO(STB_GLOBAL
, STT_OBJECT
),
728 4 /* st_shndx=index of related section table entry */
732 /* section header string table, with decimal string offsets */
733 static const char sectionStrings
[40]=
736 /* 9 */ ".shstrtab\0"
739 /* 35 */ "\0\0\0\0"; /* contains terminating NUL */
740 /* 40: padded to multiple of 8 bytes */
743 * Use entry[] for the string table which will contain only the
745 * entry[0] must be 0 (NUL)
746 * The entry point name can be up to 38 characters long (sizeof(entry)-2).
749 /* 16-align .rodata in the .o file, just in case */
750 static const char padding
[16]={ 0 };
754 /* 64-bit Elf file header */
755 static Elf64_Ehdr header64
={
758 ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
,
760 U_IS_BIG_ENDIAN
? ELFDATA2MSB
: ELFDATA2LSB
,
761 EV_CURRENT
/* EI_VERSION */
765 EV_CURRENT
, /* e_version */
768 (Elf64_Off
)sizeof(Elf64_Ehdr
), /* e_shoff */
770 (Elf64_Half
)sizeof(Elf64_Ehdr
), /* eh_size */
773 (Elf64_Half
)sizeof(Elf64_Shdr
), /* e_shentsize */
778 /* 64-bit Elf section header table */
779 static Elf64_Shdr sectionHeaders64
[5]={
788 (Elf64_Off
)(sizeof(header64
)+sizeof(sectionHeaders64
)), /* sh_offset */
789 (Elf64_Xword
)(2*sizeof(Elf64_Sym
)), /* sh_size */
790 3, /* sh_link=sect hdr index of .strtab */
791 1, /* sh_info=One greater than the symbol table index of the last
792 * local symbol (with STB_LOCAL). */
793 4, /* sh_addralign */
794 (Elf64_Xword
)(sizeof(Elf64_Sym
)) /* sh_entsize */
801 (Elf64_Off
)(sizeof(header64
)+sizeof(sectionHeaders64
)+2*sizeof(Elf64_Sym
)), /* sh_offset */
805 1, /* sh_addralign */
813 (Elf64_Off
)(sizeof(header64
)+sizeof(sectionHeaders64
)+2*sizeof(Elf64_Sym
)+40), /* sh_offset */
814 (Elf64_Xword
)sizeof(entry
), /* sh_size */
817 1, /* sh_addralign */
823 SHF_ALLOC
, /* sh_flags */
825 (Elf64_Off
)(sizeof(header64
)+sizeof(sectionHeaders64
)+2*sizeof(Elf64_Sym
)+40+sizeof(entry
)), /* sh_offset */
829 16, /* sh_addralign */
835 * 64-bit symbol table
836 * careful: different order of items compared with Elf32_sym!
838 static Elf64_Sym symbols64
[2]={
842 { /* data entry point */
844 ELF64_ST_INFO(STB_GLOBAL
, STT_OBJECT
),
846 4, /* st_shndx=index of related section table entry */
854 /* entry[] have a leading NUL */
857 /* in the common code, count entryLength from after the NUL */
862 #elif defined(U_WINDOWS)
864 IMAGE_FILE_HEADER fileHeader
;
865 IMAGE_SECTION_HEADER sections
[2];
866 char linkerOptions
[100];
868 IMAGE_SYMBOL symbols
[1];
870 DWORD sizeofLongNames
;
875 * entry sometimes have a leading '_'
876 * overwritten if entryOffset==0 depending on the target platform
877 * see check for cpu below
883 # error "Unknown platform for CAN_GENERATE_OBJECTS."
886 /* deal with options, files and the entry point name */
887 getArchitecture(&cpu
, &bits
, &makeBigEndian
);
888 printf("genccode: --match-arch cpu=%hu bits=%hu big-endian=%hu\n", cpu
, bits
, makeBigEndian
);
890 if(cpu
==IMAGE_FILE_MACHINE_I386
) {
895 in
=T_FileStream_open(filename
, "rb");
897 fprintf(stderr
, "genccode: unable to open input file %s\n", filename
);
898 exit(U_FILE_ACCESS_ERROR
);
900 size
=T_FileStream_size(in
);
902 getOutFilename(filename
, destdir
, buffer
, entry
+entryOffset
, newSuffix
);
904 if(options
[kOptEntryPoint
].doesOccur
) {
905 uprv_strcpy(entry
+entryOffset
, options
[kOptEntryPoint
].value
);
906 uprv_strcat(entry
+entryOffset
, "_dat");
908 /* turn dashes in the entry name into underscores */
909 entryLength
=(int32_t)uprv_strlen(entry
+entryLengthOffset
);
910 for(i
=0; i
<entryLength
; ++i
) {
911 if(entry
[entryLengthOffset
+i
]=='-') {
912 entry
[entryLengthOffset
+i
]='_';
916 /* open the output file */
917 out
=T_FileStream_open(buffer
, "wb");
919 fprintf(stderr
, "genccode: unable to open output file %s\n", buffer
);
920 exit(U_FILE_ACCESS_ERROR
);
925 header32
.e_ident
[EI_DATA
]= makeBigEndian
? ELFDATA2MSB
: ELFDATA2LSB
;
926 header32
.e_machine
=cpu
;
928 /* 16-align .rodata in the .o file, just in case */
929 paddingSize
=sectionHeaders32
[4].sh_offset
& 0xf;
931 paddingSize
=0x10-paddingSize
;
932 sectionHeaders32
[4].sh_offset
+=paddingSize
;
935 sectionHeaders32
[4].sh_size
=(Elf32_Word
)size
;
937 symbols32
[1].st_size
=(Elf32_Word
)size
;
939 /* write .o headers */
940 T_FileStream_write(out
, &header32
, (int32_t)sizeof(header32
));
941 T_FileStream_write(out
, sectionHeaders32
, (int32_t)sizeof(sectionHeaders32
));
942 T_FileStream_write(out
, symbols32
, (int32_t)sizeof(symbols32
));
943 } else /* bits==64 */ {
945 header64
.e_ident
[EI_DATA
]= makeBigEndian
? ELFDATA2MSB
: ELFDATA2LSB
;
946 header64
.e_machine
=cpu
;
948 /* 16-align .rodata in the .o file, just in case */
949 paddingSize
=sectionHeaders64
[4].sh_offset
& 0xf;
951 paddingSize
=0x10-paddingSize
;
952 sectionHeaders64
[4].sh_offset
+=paddingSize
;
955 sectionHeaders64
[4].sh_size
=(Elf64_Xword
)size
;
957 symbols64
[1].st_size
=(Elf64_Xword
)size
;
959 /* write .o headers */
960 T_FileStream_write(out
, &header64
, (int32_t)sizeof(header64
));
961 T_FileStream_write(out
, sectionHeaders64
, (int32_t)sizeof(sectionHeaders64
));
962 T_FileStream_write(out
, symbols64
, (int32_t)sizeof(symbols64
));
966 T_FileStream_write(out
, sectionStrings
, (int32_t)sizeof(sectionStrings
));
967 T_FileStream_write(out
, entry
, (int32_t)sizeof(entry
));
969 T_FileStream_write(out
, padding
, paddingSize
);
971 #elif defined(U_WINDOWS)
972 /* populate the .obj headers */
973 uprv_memset(&objHeader
, 0, sizeof(objHeader
));
974 uprv_memset(&symbols
, 0, sizeof(symbols
));
975 uprv_memset(&symbolNames
, 0, sizeof(symbolNames
));
977 /* write the linker export directive */
978 uprv_strcpy(objHeader
.linkerOptions
, "-export:");
980 uprv_strcpy(objHeader
.linkerOptions
+length
, entry
);
982 uprv_strcpy(objHeader
.linkerOptions
+length
, ",data ");
985 /* set the file header */
986 objHeader
.fileHeader
.Machine
=cpu
;
987 objHeader
.fileHeader
.NumberOfSections
=2;
988 objHeader
.fileHeader
.TimeDateStamp
=(DWORD
)time(NULL
);
989 objHeader
.fileHeader
.PointerToSymbolTable
=IMAGE_SIZEOF_FILE_HEADER
+2*IMAGE_SIZEOF_SECTION_HEADER
+length
+size
; /* start of symbol table */
990 objHeader
.fileHeader
.NumberOfSymbols
=1;
992 /* set the section for the linker options */
993 uprv_strncpy((char *)objHeader
.sections
[0].Name
, ".drectve", 8);
994 objHeader
.sections
[0].SizeOfRawData
=length
;
995 objHeader
.sections
[0].PointerToRawData
=IMAGE_SIZEOF_FILE_HEADER
+2*IMAGE_SIZEOF_SECTION_HEADER
;
996 objHeader
.sections
[0].Characteristics
=IMAGE_SCN_LNK_INFO
|IMAGE_SCN_LNK_REMOVE
|IMAGE_SCN_ALIGN_1BYTES
;
998 /* set the data section */
999 uprv_strncpy((char *)objHeader
.sections
[1].Name
, ".rdata", 6);
1000 objHeader
.sections
[1].SizeOfRawData
=size
;
1001 objHeader
.sections
[1].PointerToRawData
=IMAGE_SIZEOF_FILE_HEADER
+2*IMAGE_SIZEOF_SECTION_HEADER
+length
;
1002 objHeader
.sections
[1].Characteristics
=IMAGE_SCN_CNT_INITIALIZED_DATA
|IMAGE_SCN_ALIGN_16BYTES
|IMAGE_SCN_MEM_READ
;
1004 /* set the symbol table */
1005 if(entryLength
<=8) {
1006 uprv_strncpy((char *)symbols
[0].N
.ShortName
, entry
, entryLength
);
1007 symbolNames
.sizeofLongNames
=4;
1009 symbols
[0].N
.Name
.Short
=0;
1010 symbols
[0].N
.Name
.Long
=4;
1011 symbolNames
.sizeofLongNames
=4+entryLength
+1;
1012 uprv_strcpy(symbolNames
.longNames
, entry
);
1014 symbols
[0].SectionNumber
=2;
1015 symbols
[0].StorageClass
=IMAGE_SYM_CLASS_EXTERNAL
;
1017 /* write the file header and the linker options section */
1018 T_FileStream_write(out
, &objHeader
, objHeader
.sections
[1].PointerToRawData
);
1020 # error "Unknown platform for CAN_GENERATE_OBJECTS."
1023 /* copy the data file into section 2 */
1025 length
=T_FileStream_read(in
, buffer
, sizeof(buffer
));
1029 T_FileStream_write(out
, buffer
, (int32_t)length
);
1033 /* write the symbol table */
1034 T_FileStream_write(out
, symbols
, IMAGE_SIZEOF_SYMBOL
);
1035 T_FileStream_write(out
, &symbolNames
, symbolNames
.sizeofLongNames
);
1038 if(T_FileStream_error(in
)) {
1039 fprintf(stderr
, "genccode: file read error while generating from file %s\n", filename
);
1040 exit(U_FILE_ACCESS_ERROR
);
1043 if(T_FileStream_error(out
)) {
1044 fprintf(stderr
, "genccode: file write error while generating from file %s\n", filename
);
1045 exit(U_FILE_ACCESS_ERROR
);
1048 T_FileStream_close(out
);
1049 T_FileStream_close(in
);
1054 getOutFilename(const char *inFilename
, const char *destdir
, char *outFilename
, char *entryName
, const char *newSuffix
) {
1055 const char *basename
=findBasename(inFilename
), *suffix
=uprv_strrchr(basename
, '.');
1058 if(destdir
!=NULL
&& *destdir
!=0) {
1060 *outFilename
++=*destdir
++;
1061 } while(*destdir
!=0);
1062 if(*(outFilename
-1)!=U_FILE_SEP_CHAR
) {
1063 *outFilename
++=U_FILE_SEP_CHAR
;
1065 inFilename
=basename
;
1067 while(inFilename
<basename
) {
1068 *outFilename
++=*inFilename
++;
1073 /* the filename does not have a suffix */
1074 uprv_strcpy(entryName
, inFilename
);
1075 if(options
[kOptFilename
].doesOccur
) {
1076 uprv_strcpy(outFilename
, options
[kOptFilename
].value
);
1078 uprv_strcpy(outFilename
, inFilename
);
1080 uprv_strcat(outFilename
, newSuffix
);
1082 char *saveOutFilename
= outFilename
;
1084 while(inFilename
<suffix
) {
1085 if(*inFilename
=='-') {
1086 /* iSeries cannot have '-' in the .o objects. */
1087 *outFilename
++=*entryName
++='_';
1091 *outFilename
++=*entryName
++=*inFilename
++;
1095 /* replace '.' by '_' */
1096 *outFilename
++=*entryName
++='_';
1100 while(*inFilename
!=0) {
1101 *outFilename
++=*entryName
++=*inFilename
++;
1106 if(options
[kOptFilename
].doesOccur
) {
1107 uprv_strcpy(saveOutFilename
, options
[kOptFilename
].value
);
1108 uprv_strcat(saveOutFilename
, newSuffix
);
1111 uprv_strcpy(outFilename
, newSuffix
);
1117 write32(FileStream
*out
, uint32_t bitField
) {
1119 char bitFieldStr
[64]; /* This is more bits than needed for a 32-bit number */
1120 char *s
= bitFieldStr
;
1121 uint8_t *ptrIdx
= (uint8_t *)&bitField
;
1122 static const char hexToStr
[16] = {
1129 /* write the value, possibly with comma and newline */
1130 if(column
==MAX_COLUMN
) {
1133 } else if(column
<32) {
1138 uprv_strcpy(s
, assemblyHeader
[assemblyHeaderIndex
].beginLine
);
1143 if (bitField
< 10) {
1144 /* It's a small number. Don't waste the space for 0x */
1145 *(s
++)=hexToStr
[bitField
];
1148 int seenNonZero
= 0; /* This is used to remove leading zeros */
1153 /* This creates a 32-bit field */
1155 for (i
= 0; i
< sizeof(uint32_t); i
++)
1157 for (i
= sizeof(uint32_t)-1; i
>= 0 ; i
--)
1160 uint8_t value
= ptrIdx
[i
];
1161 if (value
|| seenNonZero
) {
1162 *(s
++)=hexToStr
[value
>>4];
1163 *(s
++)=hexToStr
[value
&0xF];
1170 T_FileStream_writeLine(out
, bitFieldStr
);
1174 write8(FileStream
*out
, uint8_t byte
) {
1178 /* convert the byte value to a string */
1180 s
[i
++]=(char)('0'+byte
/100);
1183 if(i
>0 || byte
>=10) {
1184 s
[i
++]=(char)('0'+byte
/10);
1187 s
[i
++]=(char)('0'+byte
);
1190 /* write the value, possibly with comma and newline */
1191 if(column
==MAX_COLUMN
) {
1194 } else if(column
<16) {
1195 T_FileStream_writeLine(out
, ",");
1198 T_FileStream_writeLine(out
, ",\n");
1201 T_FileStream_writeLine(out
, s
);
1206 write8str(FileStream
*out
, uint8_t byte
) {
1210 sprintf(s
, "\\x%X", byte
);
1212 sprintf(s
, "\\%X", byte
);
1214 /* write the value, possibly with comma and newline */
1215 if(column
==MAX_COLUMN
) {
1218 T_FileStream_writeLine(out
, "\"");
1219 } else if(column
<24) {
1222 T_FileStream_writeLine(out
, "\"\n\"");
1225 T_FileStream_writeLine(out
, s
);