From c998985c744549292ba4abe077aee3950548b083 Mon Sep 17 00:00:00 2001 From: grischka Date: Sat, 18 Jul 2009 22:07:42 +0200 Subject: [PATCH] cleanup: constify some global data --- arm-gen.c | 2 +- c67-gen.c | 2 +- i386-gen.c | 2 +- il-gen.c | 2 +- tccelf.c | 10 +-- tccpe.c | 220 +++++++++++++++++++++++++++++------------------------------ tccpp.c | 7 +- x86_64-gen.c | 6 +- 8 files changed, 126 insertions(+), 125 deletions(-) diff --git a/arm-gen.c b/arm-gen.c index 42feecf7..9370db54 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -75,7 +75,7 @@ enum { #endif }; -int reg_classes[NB_REGS] = { +const int reg_classes[NB_REGS] = { /* r0 */ RC_INT | RC_R0, /* r1 */ RC_INT | RC_R1, /* r2 */ RC_INT | RC_R2, diff --git a/c67-gen.c b/c67-gen.c index 04f8a12b..a60b6bff 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -85,7 +85,7 @@ enum { TREG_C67_B13, }; -int reg_classes[NB_REGS] = { +const int reg_classes[NB_REGS] = { /* eax */ RC_INT | RC_FLOAT | RC_EAX, // only allow even regs for floats (allow for doubles) /* ecx */ RC_INT | RC_ECX, diff --git a/i386-gen.c b/i386-gen.c index 2edbe2fd..61f9e78f 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -42,7 +42,7 @@ enum { TREG_ST0, }; -int reg_classes[NB_REGS] = { +const int reg_classes[NB_REGS] = { /* eax */ RC_INT | RC_EAX, /* ecx */ RC_INT | RC_ECX, /* edx */ RC_INT | RC_EDX, diff --git a/il-gen.c b/il-gen.c index 29f05265..61bc98d2 100644 --- a/il-gen.c +++ b/il-gen.c @@ -41,7 +41,7 @@ enum { REG_ST2, }; -int reg_classes[NB_REGS] = { +const int reg_classes[NB_REGS] = { /* ST0 */ RC_ST | RC_ST0, /* ST1 */ RC_ST | RC_ST1, /* ST2 */ RC_ST, diff --git a/tccelf.c b/tccelf.c index 826e8e0e..e114f425 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1283,15 +1283,15 @@ static void tcc_add_linker_symbols(TCCState *s1) /* name of ELF interpreter */ #if defined __FreeBSD__ -static char elf_interp[] = "/usr/libexec/ld-elf.so.1"; +static const char elf_interp[] = "/usr/libexec/ld-elf.so.1"; #elif defined TCC_ARM_EABI -static char elf_interp[] = "/lib/ld-linux.so.3"; +static const char elf_interp[] = "/lib/ld-linux.so.3"; #elif defined(TCC_TARGET_X86_64) -static char elf_interp[] = "/lib/ld-linux-x86-64.so.2"; +static const char elf_interp[] = "/lib/ld-linux-x86-64.so.2"; #elif defined(TCC_UCLIBC) -static char elf_interp[] = "/lib/ld-uClibc.so.0"; +static const char elf_interp[] = "/lib/ld-uClibc.so.0"; #else -static char elf_interp[] = "/lib/ld-linux.so.2"; +static const char elf_interp[] = "/lib/ld-linux.so.2"; #endif static void tcc_output_binary(TCCState *s1, FILE *f, diff --git a/tccpe.c b/tccpe.c index 652ab069..285d6891 100644 --- a/tccpe.c +++ b/tccpe.c @@ -274,106 +274,6 @@ struct pe_rsrc_reloc { #pragma pack(pop) -/* ----------------------------------------------------------- */ -ST_DATA struct pe_header pe_header = { -{ - /* IMAGE_DOS_HEADER doshdr */ - 0x5A4D, /*WORD e_magic; Magic number */ - 0x0090, /*WORD e_cblp; Bytes on last page of file */ - 0x0003, /*WORD e_cp; Pages in file */ - 0x0000, /*WORD e_crlc; Relocations */ - - 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */ - 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */ - 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */ - 0x0000, /*WORD e_ss; Initial (relative) SS value */ - - 0x00B8, /*WORD e_sp; Initial SP value */ - 0x0000, /*WORD e_csum; Checksum */ - 0x0000, /*WORD e_ip; Initial IP value */ - 0x0000, /*WORD e_cs; Initial (relative) CS value */ - 0x0040, /*WORD e_lfarlc; File address of relocation table */ - 0x0000, /*WORD e_ovno; Overlay number */ - {0,0,0,0}, /*WORD e_res[4]; Reserved words */ - 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */ - 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */ - {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */ - 0x00000080 /*DWORD e_lfanew; File address of new exe header */ -},{ - /* BYTE dosstub[0x40] */ - /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */ - 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68, - 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f, - 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20, - 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00, -}, - 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */ -{ - /* IMAGE_FILE_HEADER filehdr */ -#ifdef TCC_TARGET_X86_64 - 0x8664, /*WORD Machine; */ -#else - 0x014C, /*WORD Machine; */ -#endif - 0x0003, /*WORD NumberOfSections; */ - 0x00000000, /*DWORD TimeDateStamp; */ - 0x00000000, /*DWORD PointerToSymbolTable; */ - 0x00000000, /*DWORD NumberOfSymbols; */ -#ifdef TCC_TARGET_X86_64 - 0x00F0, /*WORD SizeOfOptionalHeader; */ - 0x022F /*WORD Characteristics; */ -#define CHARACTERISTICS_DLL 0x222E -#else - 0x00E0, /*WORD SizeOfOptionalHeader; */ - 0x030F /*WORD Characteristics; */ -#define CHARACTERISTICS_DLL 0x230E -#endif -},{ - /* IMAGE_OPTIONAL_HEADER opthdr */ - /* Standard fields. */ -#ifdef TCC_TARGET_X86_64 - 0x020B, /*WORD Magic; */ -#else - 0x010B, /*WORD Magic; */ -#endif - 0x06, /*BYTE MajorLinkerVersion; */ - 0x00, /*BYTE MinorLinkerVersion; */ - 0x00000000, /*DWORD SizeOfCode; */ - 0x00000000, /*DWORD SizeOfInitializedData; */ - 0x00000000, /*DWORD SizeOfUninitializedData; */ - 0x00000000, /*DWORD AddressOfEntryPoint; */ - 0x00000000, /*DWORD BaseOfCode; */ -#ifndef TCC_TARGET_X86_64 - 0x00000000, /*DWORD BaseOfData; */ -#endif - /* NT additional fields. */ - 0x00400000, /*DWORD ImageBase; */ - 0x00001000, /*DWORD SectionAlignment; */ - 0x00000200, /*DWORD FileAlignment; */ - 0x0004, /*WORD MajorOperatingSystemVersion; */ - 0x0000, /*WORD MinorOperatingSystemVersion; */ - 0x0000, /*WORD MajorImageVersion; */ - 0x0000, /*WORD MinorImageVersion; */ - 0x0004, /*WORD MajorSubsystemVersion; */ - 0x0000, /*WORD MinorSubsystemVersion; */ - 0x00000000, /*DWORD Win32VersionValue; */ - 0x00000000, /*DWORD SizeOfImage; */ - 0x00000200, /*DWORD SizeOfHeaders; */ - 0x00000000, /*DWORD CheckSum; */ - 0x0002, /*WORD Subsystem; */ - 0x0000, /*WORD DllCharacteristics; */ - 0x00100000, /*DWORD SizeOfStackReserve; */ - 0x00001000, /*DWORD SizeOfStackCommit; */ - 0x00100000, /*DWORD SizeOfHeapReserve; */ - 0x00001000, /*DWORD SizeOfHeapCommit; */ - 0x00000000, /*DWORD LoaderFlags; */ - 0x00000010, /*DWORD NumberOfRvaAndSizes; */ - - /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */ - {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}} -}}; - /* ------------------------------------------------------------- */ /* internal temporary structures */ @@ -400,7 +300,7 @@ enum { sec_last }; -ST_DATA DWORD pe_sec_flags[] = { +ST_DATA const DWORD pe_sec_flags[] = { 0x60000020, /* ".text" , */ 0xC0000040, /* ".data" , */ 0xC0000080, /* ".bss" , */ @@ -541,18 +441,118 @@ ST_FN void pe_align_section(Section *s, int a) section_ptr_add(s, a - i); } -ST_FN void pe_set_datadir(int dir, DWORD addr, DWORD size) +ST_FN void pe_set_datadir(struct pe_header *hdr, int dir, DWORD addr, DWORD size) { - pe_header.opthdr.DataDirectory[dir].VirtualAddress = addr; - pe_header.opthdr.DataDirectory[dir].Size = size; + hdr->opthdr.DataDirectory[dir].VirtualAddress = addr; + hdr->opthdr.DataDirectory[dir].Size = size; } /*----------------------------------------------------------------------------*/ ST_FN int pe_write(struct pe_info *pe) { + static const struct pe_header pe_template = { + { + /* IMAGE_DOS_HEADER doshdr */ + 0x5A4D, /*WORD e_magic; Magic number */ + 0x0090, /*WORD e_cblp; Bytes on last page of file */ + 0x0003, /*WORD e_cp; Pages in file */ + 0x0000, /*WORD e_crlc; Relocations */ + + 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */ + 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */ + 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */ + 0x0000, /*WORD e_ss; Initial (relative) SS value */ + + 0x00B8, /*WORD e_sp; Initial SP value */ + 0x0000, /*WORD e_csum; Checksum */ + 0x0000, /*WORD e_ip; Initial IP value */ + 0x0000, /*WORD e_cs; Initial (relative) CS value */ + 0x0040, /*WORD e_lfarlc; File address of relocation table */ + 0x0000, /*WORD e_ovno; Overlay number */ + {0,0,0,0}, /*WORD e_res[4]; Reserved words */ + 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */ + 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */ + {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */ + 0x00000080 /*DWORD e_lfanew; File address of new exe header */ + },{ + /* BYTE dosstub[0x40] */ + /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */ + 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68, + 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f, + 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20, + 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + }, + 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */ + { + /* IMAGE_FILE_HEADER filehdr */ +#ifdef TCC_TARGET_X86_64 + 0x8664, /*WORD Machine; */ +#else + 0x014C, /*WORD Machine; */ +#endif + 0x0003, /*WORD NumberOfSections; */ + 0x00000000, /*DWORD TimeDateStamp; */ + 0x00000000, /*DWORD PointerToSymbolTable; */ + 0x00000000, /*DWORD NumberOfSymbols; */ +#ifdef TCC_TARGET_X86_64 + 0x00F0, /*WORD SizeOfOptionalHeader; */ + 0x022F /*WORD Characteristics; */ +#define CHARACTERISTICS_DLL 0x222E +#else + 0x00E0, /*WORD SizeOfOptionalHeader; */ + 0x030F /*WORD Characteristics; */ +#define CHARACTERISTICS_DLL 0x230E +#endif +},{ + /* IMAGE_OPTIONAL_HEADER opthdr */ + /* Standard fields. */ +#ifdef TCC_TARGET_X86_64 + 0x020B, /*WORD Magic; */ +#else + 0x010B, /*WORD Magic; */ +#endif + 0x06, /*BYTE MajorLinkerVersion; */ + 0x00, /*BYTE MinorLinkerVersion; */ + 0x00000000, /*DWORD SizeOfCode; */ + 0x00000000, /*DWORD SizeOfInitializedData; */ + 0x00000000, /*DWORD SizeOfUninitializedData; */ + 0x00000000, /*DWORD AddressOfEntryPoint; */ + 0x00000000, /*DWORD BaseOfCode; */ +#ifndef TCC_TARGET_X86_64 + 0x00000000, /*DWORD BaseOfData; */ +#endif + /* NT additional fields. */ + 0x00400000, /*DWORD ImageBase; */ + 0x00001000, /*DWORD SectionAlignment; */ + 0x00000200, /*DWORD FileAlignment; */ + 0x0004, /*WORD MajorOperatingSystemVersion; */ + 0x0000, /*WORD MinorOperatingSystemVersion; */ + 0x0000, /*WORD MajorImageVersion; */ + 0x0000, /*WORD MinorImageVersion; */ + 0x0004, /*WORD MajorSubsystemVersion; */ + 0x0000, /*WORD MinorSubsystemVersion; */ + 0x00000000, /*DWORD Win32VersionValue; */ + 0x00000000, /*DWORD SizeOfImage; */ + 0x00000200, /*DWORD SizeOfHeaders; */ + 0x00000000, /*DWORD CheckSum; */ + 0x0002, /*WORD Subsystem; */ + 0x0000, /*WORD DllCharacteristics; */ + 0x00100000, /*DWORD SizeOfStackReserve; */ + 0x00001000, /*DWORD SizeOfStackCommit; */ + 0x00100000, /*DWORD SizeOfHeapReserve; */ + 0x00001000, /*DWORD SizeOfHeapCommit; */ + 0x00000000, /*DWORD LoaderFlags; */ + 0x00000010, /*DWORD NumberOfRvaAndSizes; */ + + /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */ + {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, + {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}} + }}; + int i; FILE *op; DWORD file_offset, r; + struct pe_header pe_header = pe_template; op = fopen(pe->filename, "wb"); if (NULL == op) { @@ -561,7 +561,7 @@ ST_FN int pe_write(struct pe_info *pe) } pe->sizeofheaders = pe_file_align( - sizeof pe_header + sizeof (struct pe_header) + pe->sec_count * sizeof (IMAGE_SECTION_HEADER) ); @@ -599,11 +599,11 @@ ST_FN int pe_write(struct pe_info *pe) break; case sec_reloc: - pe_set_datadir(IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size); + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size); break; case sec_rsrc: - pe_set_datadir(IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size); + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size); break; case sec_stab: @@ -612,13 +612,13 @@ ST_FN int pe_write(struct pe_info *pe) if (pe->thunk == pe->s1->sections[si->ord]) { if (pe->imp_size) { - pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IMPORT, + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IMPORT, pe->imp_offs + addr, pe->imp_size); - pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IAT, + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IAT, pe->iat_offs + addr, pe->iat_size); } if (pe->exp_size) { - pe_set_datadir(IMAGE_DIRECTORY_ENTRY_EXPORT, + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXPORT, pe->exp_offs + addr, pe->exp_size); } } diff --git a/tccpp.c b/tccpp.c index 25b6cbbf..0d00cbca 100644 --- a/tccpp.c +++ b/tccpp.c @@ -26,7 +26,9 @@ static const char tcc_keywords[] = ; /* WARNING: the content of this string encodes token numbers */ -static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266"; +static const unsigned char tok_two_chars[] = + "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253" + "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266"; /* true if isid(c) || isnum(c) */ static unsigned char isidnum_table[256-CH_EOF]; @@ -110,7 +112,6 @@ char *get_tok_str(int v, CValue *cv) static char buf[STRING_MAX_SIZE + 1]; static CString cstr_buf; CString *cstr; - unsigned char *q; char *p; int i, len; @@ -182,7 +183,7 @@ char *get_tok_str(int v, CValue *cv) default: if (v < TOK_IDENT) { /* search in two bytes table */ - q = tok_two_chars; + const unsigned char *q = tok_two_chars; while (*q) { if (q[2] == v) { *p++ = q[0]; diff --git a/x86_64-gen.c b/x86_64-gen.c index 593cd614..1739e957 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -60,7 +60,7 @@ enum { #define REX_BASE(reg) (((reg) >> 3) & 1) #define REG_VALUE(reg) ((reg) & 7) -int reg_classes[NB_REGS] = { +const int reg_classes[NB_REGS] = { /* eax */ RC_INT | RC_RAX, /* ecx */ RC_INT | RC_RCX, /* edx */ RC_INT | RC_RDX, @@ -535,12 +535,12 @@ static void gcall_or_jmp(int is_jmp) #ifdef TCC_TARGET_PE #define REGN 4 -static uint8_t arg_regs[] = { +static const uint8_t arg_regs[] = { TREG_RCX, TREG_RDX, TREG_R8, TREG_R9 }; #else #define REGN 6 -static uint8_t arg_regs[REGN] = { +static const uint8_t arg_regs[REGN] = { TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9 }; #endif -- 2.11.4.GIT