History

review some recent changes

tccrun.c: - simplify usage of VirtualAlloc on win32/64 from 6728a64f1b539386bde6458212b349851ff20cd1 tccpe.c: - revert changes to pe_create_pdb() (doesn't work and/or too much complication) from 37b72477962173806d6164ef1af96bc5c39898de - reduce #ifdef amount tccgen.c: - fix fix for cast to void from fad812360ba836b4ca6f52236d867476ff671633 tccelf.c: - put create_riscv_attribute_section() into right place from c77339ab415e537798d3231613f8d0a0bb285e9d tcc.h/libtcc.c: - simplify link-option parser for pe dll_characteristics from 1a54e47dda4722e849a83b2fb5108a6ee7bb8725 i386-gen.c: - fix tls from 8502540b4aad94a8c349b0a3b7309363617a2ff9 workflow/build.yml: - add i386-linux - disable riscv64-linux-native (seems not supported currently)

grischka committed 5 months ago

Commit 7f7845c1 parent a33825813 files · +178 −310Browse files

Unified · Split

13 files
  1. .github/workflows/build.yml +24 −18
  2. i386-gen.c +2 −2
  3. lib/Makefile +1 −1
  4. lib/armflush.c +6 −0
  5. lib/lib-riscv.c +0 −20
  6. libtcc.c +29 −44
  7. tcc-doc.texi +1 −3
  8. tcc.h +0 −10
  9. tccelf.c +4 −3
  10. tccgen.c +2 −0
  11. tccpe.c +82 −174
  12. tccrun.c +18 −24
  13. tests/tests2/Makefile +9 −11
.github/workflows/build.yml+24−18
@@ -34,16 +34,13 @@ jobs: · 33 unmodified lines
34 timeout-minutes: 634 timeout-minutes: 6
35 steps:35 steps:
36 - uses: actions/checkout@v436 - uses: actions/checkout@v4
37 - name: build tcc (x86_64-win32)37 - name: build & test (x86_64-win32)
38 shell: cmd38 shell: cmd
39 run: |39 run: |
40 cd win3240 cd win32
41 for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" amd6441 for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" amd64
42 call build-tcc.bat -c cl -t x86_6442 call build-tcc.bat -c cl -t x86_64
43 - name: test (x86_64-win32)43 cd ..\tests
44 shell: cmd
45 run: |
46 cd tests
47 call test-win32.bat all -k44 call test-win32.bat all -k
48 45
49 test-i386-win32:46 test-i386-win32:
@@ -51,16 +48,13 @@ jobs: · 1 unmodified line
51 timeout-minutes: 648 timeout-minutes: 6
52 steps:49 steps:
53 - uses: actions/checkout@v450 - uses: actions/checkout@v4
54 - name: build tcc (i386-win32)51 - name: build & test (i386-win32)
55 shell: cmd52 shell: cmd
56 run: |53 run: |
57 cd win3254 cd win32
58 for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" x8655 for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" x86
59 call build-tcc.bat -c cl -t i38656 call build-tcc.bat -c cl -t i386
60 - name: test (i386-win32)57 cd ..\tests
61 shell: cmd
62 run: |
63 cd tests
64 call test-win32.bat -p c:\mingw32\bin all -k58 call test-win32.bat -p c:\mingw32\bin all -k
65 59
66 test-arm64-win32:60 test-arm64-win32:
@@ -72,9 +66,8 @@ jobs: · 5 unmodified lines
72 shell: cmd66 shell: cmd
73 run: |67 run: |
74 cd win3268 cd win32
75 for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" amd64_arm6469 for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" arm64
76 call build-tcc.bat -c cl -t arm6470 call build-tcc.bat -c cl -t arm64
77 set "PATH=C:\Program Files\LLVM\bin;%CD%;%PATH%"
78 cd ..\tests71 cd ..\tests
79 call test-win32.bat -c clang all -k72 call test-win32.bat -c clang all -k
80 73
@@ -132,12 +125,25 @@ jobs: · 51 unmodified lines
132 echo "::endgroup::" # flatten 'run container'125 echo "::endgroup::" # flatten 'run container'
133 ./configure && make && make test -k126 ./configure && make && make test -k
134 127
135 test-riscv64-linux-native:128# test-riscv64-linux-native:
136 runs-on: ubuntu-24.04-riscv129# runs-on: ubuntu-24.04-riscv
137 timeout-minutes: 8130# timeout-minutes: 8
131# steps:
132# - uses: actions/checkout@v4
133# - name: make & test tcc (riscv64-linux native)
134# run: |
135# uname -m # riscv64
136# ./configure && make && make test -k
137
138 test-i386-linux:
139 runs-on: ubuntu-latest
140 timeout-minutes: 3
138 steps:141 steps:
139 - uses: actions/checkout@v4142 - uses: actions/checkout@v4
140 - name: make & test tcc (riscv64-linux native)143 - name: install i386-gcc
141 run: |144 run: |
142 uname -m # riscv64145 sudo dpkg --add-architecture i386
143 ./configure && make && make test -k146 sudo apt-get update -q -y
147 sudo apt-get install -q -y gcc-multilib libc6-dev:i386
148 - name: make & test tcc (i386-linux)
149 run: ./configure --cpu=i386 --cc="gcc -m32" && make && make test -k
i386-gen.c+2−2
@@ -439,7 +439,7 @@ ST_FUNC void store(int r, SValue *v) · 438 unmodified lines
439 } else439 } else
440#endif440#endif
441 441
442 if ((fr & VT_SYM) && v->sym->type.t & VT_TLS) {442 if ((v->r & VT_SYM) && v->sym->type.t & VT_TLS) {
443 o(0x65); /* gs segment prefix */443 o(0x65); /* gs segment prefix */
444 o(opc);444 o(opc);
445 o(0x04 | (REG_VALUE(r) << 3)); /* modrm: [sib] | srcreg */445 o(0x04 | (REG_VALUE(r) << 3)); /* modrm: [sib] | srcreg */
@@ -467,7 +467,7 @@ static void gadd_sp(int val) · 21 unmodified lines
467 }467 }
468}468}
469 469
470#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE || defined CONFIG_TCC_PIC470#if defined TCC_TARGET_PE || defined CONFIG_TCC_PIC
471static void gen_static_call(int v)471static void gen_static_call(int v)
472{472{
473 Sym *sym;473 Sym *sym;
lib/Makefile+1−1
@@ -73,7 +73,7 @@ OBJ-arm-vfp = $(OBJ-arm) · 72 unmodified lines
73OBJ-arm-eabi = $(OBJ-arm)73OBJ-arm-eabi = $(OBJ-arm)
74OBJ-arm-eabihf = $(OBJ-arm)74OBJ-arm-eabihf = $(OBJ-arm)
75OBJ-arm-wince = $(ARM_O) $(WIN_O)75OBJ-arm-wince = $(ARM_O) $(WIN_O)
76OBJ-riscv64 = $(RISCV64_O) lib-riscv.o $(LIN_O)76OBJ-riscv64 = $(RISCV64_O) armflush.o $(LIN_O)
77 77
78OBJ-extra = $(filter $(EXTRA_O),$(OBJ-$T))78OBJ-extra = $(filter $(EXTRA_O),$(OBJ-$T))
79OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))79OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))
lib/armflush.c+6−0
@@ -61,4 +61,10 @@ void __clear_cache(void *beg, void *end) · 60 unmodified lines
61}61}
62 62
63/* ------------------------------------------------------------- */63/* ------------------------------------------------------------- */
64#elif defined __riscv
65void __clear_cache(void *beg, void *end)
66{
67 __riscv64_clear_cache(beg, end);
68}
69
64#endif70#endif
lib/lib-riscv.c+0−20
@@ -1,20 +0,0 @@
1/*
2 * TCC runtime library for riscv64.
3 *
4 * Copyright (c) 2026
5 *
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved. This file is offered as-is,
9 * without any warranty.
10 */
11
12/* ------------------------------------------------------------- */
13/* __clear_cache is used in tccrun.c. It is a built-in
14 intrinsic with gcc. However tcc in order to compile
15 itself needs this function */
16
17void __clear_cache(void *beg, void *end)
18{
19 __riscv64_clear_cache(beg, end);
20}
libtcc.c+29−44
@@ -899,6 +899,9 @@ LIBTCCAPI TCCState *tcc_new(void) · 898 unmodified lines
899#ifdef TCC_ARM_HARDFLOAT899#ifdef TCC_ARM_HARDFLOAT
900 s->float_abi = ARM_HARD_FLOAT;900 s->float_abi = ARM_HARD_FLOAT;
901#endif901#endif
902#if defined TCC_TARGET_ARM64 && defined TCC_TARGET_PE
903 s->pe_dll_characteristics = 0x8160;
904#endif
902#ifdef CONFIG_NEW_DTAGS905#ifdef CONFIG_NEW_DTAGS
903 s->enable_new_dtags = 1;906 s->enable_new_dtags = 1;
904#endif907#endif
@@ -1365,7 +1368,7 @@ struct lopt { · 460 unmodified lines
1365static int link_option(struct lopt *o, const char *q)1368static int link_option(struct lopt *o, const char *q)
1366{1369{
1367 const char *p;1370 const char *p;
1368 int c;1371 int c, r;
1369redo:1372redo:
1370 /* there should be 1 or 2 dashes */1373 /* there should be 1 or 2 dashes */
1371 p = o->opt;1374 p = o->opt;
@@ -1373,6 +1376,16 @@ redo: · 1 unmodified line
1373 return 0;1376 return 0;
1374 if (*p == '-')1377 if (*p == '-')
1375 p++;1378 p++;
1379 r = 1;
1380 if (q[0] == '?') { /* check for no-/disable- prefix */
1381 ++q;
1382 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
1383 p += 3, r = -1;
1384#ifdef TCC_TARGET_PE
1385 else if (0 == memcmp(p, "disable-", 8))
1386 p += 8, r = -1;
1387#endif
1388 }
1376 while ((c = *q) == *p) {1389 while ((c = *q) == *p) {
1377 if (c == '\0')1390 if (c == '\0')
1378 goto succ; /* -Wl,-opt */1391 goto succ; /* -Wl,-opt */
@@ -1401,24 +1414,13 @@ redo: · 22 unmodified lines
1401succ:1414succ:
1402 o->arg = p;1415 o->arg = p;
1403 //printf("set %s '%s'\n", o->opt, o->arg);1416 //printf("set %s '%s'\n", o->opt, o->arg);
1404 return 1;1417 return r;
1405}1418}
1406 1419
1407static void args_parser_add_file(TCCState *s, const char* filename, int filetype);1420static void args_parser_add_file(TCCState *s, const char* filename, int filetype);
1408 1421
1409#ifdef TCC_TARGET_PE1422#define SET_OR_CLEAR(v,f) (v = r > 0 ? v | f : v & ~f)
1410static void tcc_pe_set_dll_characteristics(TCCState *s, unsigned flags)1423#define SET_OR_CLEAR_2(v,f1,f2) (v = r > 0 ? v | f1 : v & ~f2)
1411{
1412 s->pe_dll_characteristics |= flags;
1413 s->pe_dll_characteristics_clear &= ~flags;
1414}
1415
1416static void tcc_pe_clear_dll_characteristics(TCCState *s, unsigned flags)
1417{
1418 s->pe_dll_characteristics &= ~flags;
1419 s->pe_dll_characteristics_clear |= flags;
1420}
1421#endif
1422 1424
1423/* set linker options */1425/* set linker options */
1424static int tcc_set_linker(TCCState *s, const char *optarg)1426static int tcc_set_linker(TCCState *s, const char *optarg)
@@ -1431,6 +1433,7 @@ static int tcc_set_linker(TCCState *s, const char *optarg) · 6 unmodified lines
1431 char *end = NULL;1433 char *end = NULL;
1432 int ignoring = 0;1434 int ignoring = 0;
1433 struct lopt o = {0};1435 struct lopt o = {0};
1436 int r;
1434 o.s = s;1437 o.s = s;
1435 o.opt = s->link_argv[s->link_optind];1438 o.opt = s->link_argv[s->link_optind];
1436 1439
@@ -1481,39 +1484,21 @@ static int tcc_set_linker(TCCState *s, const char *optarg) · 44 unmodified lines
1481 s->section_align = strtoul(o.arg, &end, 16);1484 s->section_align = strtoul(o.arg, &end, 16);
1482 } else if (link_option(&o, "soname=|install_name=")) {1485 } else if (link_option(&o, "soname=|install_name=")) {
1483 tcc_set_str(&s->soname, o.arg);1486 tcc_set_str(&s->soname, o.arg);
1484 } else if (link_option(&o, "whole-archive")) {1487 } else if (!!(r = link_option(&o, "?whole-archive"))) {
1485 s->filetype |= AFF_WHOLE_ARCHIVE;1488 SET_OR_CLEAR(s->filetype, AFF_WHOLE_ARCHIVE);
1486 } else if (link_option(&o, "no-whole-archive")) {
1487 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1488 } else if (link_option(&o, "znodelete")) {1489 } else if (link_option(&o, "znodelete")) {
1489 s->znodelete = 1;1490 s->znodelete = 1;
1490#ifdef TCC_TARGET_PE1491#ifdef TCC_TARGET_PE
1491 } else if (link_option(&o, "large-address-aware")) {1492 } else if (link_option(&o, "large-address-aware")) {
1492 s->pe_characteristics |= PE_IMAGE_FILE_LARGE_ADDRESS_AWARE;1493 s->pe_characteristics |= 0x20;
1493 } else if (link_option(&o, "dynamicbase")) {1494 } else if (!!(r = link_option(&o, "?dynamicbase"))) {
1494 tcc_pe_set_dll_characteristics(s, PE_DLLCHARACTERISTICS_DYNAMIC_BASE);1495 SET_OR_CLEAR_2(s1->pe_dll_characteristics, 0x40, 0x60);
1495 } else if (link_option(&o, "disable-dynamicbase|no-dynamicbase")) {1496 } else if (!!(r = link_option(&o, "?high-entropy-va"))) {
1496 tcc_pe_clear_dll_characteristics(s,1497 SET_OR_CLEAR_2(s1->pe_dll_characteristics, 0x60, 0x20);
1497 PE_DLLCHARACTERISTICS_DYNAMIC_BASE |1498 } else if (!!(r = link_option(&o, "?nxcompat"))) {
1498 PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA);1499 SET_OR_CLEAR(s1->pe_dll_characteristics, 0x100);
1499 } else if (link_option(&o, "nxcompat")) {1500 } else if (!!(r = link_option(&o, "?tsaware"))) {
1500 tcc_pe_set_dll_characteristics(s, PE_DLLCHARACTERISTICS_NX_COMPAT);1501 SET_OR_CLEAR(s1->pe_dll_characteristics, 0x8000);
1501 } else if (link_option(&o, "disable-nxcompat|no-nxcompat")) {
1502 tcc_pe_clear_dll_characteristics(s, PE_DLLCHARACTERISTICS_NX_COMPAT);
1503 } else if (link_option(&o, "high-entropy-va")) {
1504# if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_ARM64)
1505 tcc_pe_set_dll_characteristics(s,
1506 PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA |
1507 PE_DLLCHARACTERISTICS_DYNAMIC_BASE);
1508# else
1509 goto err;
1510# endif
1511 } else if (link_option(&o, "disable-high-entropy-va|no-high-entropy-va")) {
1512 tcc_pe_clear_dll_characteristics(s, PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA);
1513 } else if (link_option(&o, "tsaware")) {
1514 tcc_pe_set_dll_characteristics(s, PE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE);
1515 } else if (link_option(&o, "disable-tsaware|no-tsaware")) {
1516 tcc_pe_clear_dll_characteristics(s, PE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE);
1517 } else if (link_option(&o, "file-alignment=")) {1502 } else if (link_option(&o, "file-alignment=")) {
1518 s->pe_file_align = strtoul(o.arg, &end, 16);1503 s->pe_file_align = strtoul(o.arg, &end, 16);
1519 } else if (link_option(&o, "stack=")) {1504 } else if (link_option(&o, "stack=")) {
tcc-doc.texi+1−3
@@ -386,9 +386,7 @@ Set type for PE (Windows) executables. · 385 unmodified lines
386@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]386@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]
387Modify executable layout.387Modify executable layout.
388 388
389@item -Wl,-[dynamicbase | nxcompat | high-entropy-va | tsaware]389@item -Wl,-(no-|disable-)[dynamicbase | nxcompat | high-entropy-va | tsaware]
390@item -Wl,-[no-dynamicbase | no-nxcompat | no-high-entropy-va | no-tsaware]
391@item -Wl,-[disable-dynamicbase | disable-nxcompat | disable-high-entropy-va | disable-tsaware]
392Set or clear PE (Windows) executable header hardening flags. The390Set or clear PE (Windows) executable header hardening flags. The
393@option{-Wl,-high-entropy-va} option is supported on x86-64 and ARM64 PE391@option{-Wl,-high-entropy-va} option is supported on x86-64 and ARM64 PE
394targets and implies @option{-Wl,-dynamicbase}. Clearing dynamicbase also392targets and implies @option{-Wl,-dynamicbase}. Clearing dynamicbase also
tcc.h+0−10
@@ -727,15 +727,6 @@ struct sym_attr { · 726 unmodified lines
727#endif727#endif
728};728};
729 729
730#ifdef TCC_TARGET_PE
731#define PE_IMAGE_FILE_RELOCS_STRIPPED 0x0001
732#define PE_IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020
733#define PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA 0x0020
734#define PE_DLLCHARACTERISTICS_DYNAMIC_BASE 0x0040
735#define PE_DLLCHARACTERISTICS_NX_COMPAT 0x0100
736#define PE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE 0x8000
737#endif
738
739struct TCCState {730struct TCCState {
740 unsigned char verbose; /* if true, display some information during compilation */731 unsigned char verbose; /* if true, display some information during compilation */
741 unsigned char nostdinc; /* if true, no standard headers are added */732 unsigned char nostdinc; /* if true, no standard headers are added */
@@ -949,7 +940,6 @@ struct TCCState { · 207 unmodified lines
949 int pe_subsystem;940 int pe_subsystem;
950 unsigned pe_characteristics;941 unsigned pe_characteristics;
951 unsigned pe_dll_characteristics;942 unsigned pe_dll_characteristics;
952 unsigned pe_dll_characteristics_clear;
953 unsigned pe_file_align;943 unsigned pe_file_align;
954 unsigned pe_stack_size;944 unsigned pe_stack_size;
955 addr_t pe_imagebase;945 addr_t pe_imagebase;
tccelf.c+4−3
@@ -2992,6 +2992,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2991 unmodified lines
2992 create_arm_attribute_section (s1);2992 create_arm_attribute_section (s1);
2993#endif2993#endif
2994 2994
2995#ifdef TCC_TARGET_RISCV64
2996 create_riscv_attribute_section(s1);
2997#endif
2998
2995#if TARGETOS_OpenBSD2999#if TARGETOS_OpenBSD
2996 dyninf.note = create_bsd_note_section (s1, ".note.openbsd.ident", "OpenBSD");3000 dyninf.note = create_bsd_note_section (s1, ".note.openbsd.ident", "OpenBSD");
2997#endif3001#endif
@@ -3172,9 +3176,6 @@ static int elf_output_obj(TCCState *s1, const char *filename) · 174 unmodified lines
3172{3176{
3173 Section *s;3177 Section *s;
3174 int i, ret, file_offset;3178 int i, ret, file_offset;
3175#ifdef TCC_TARGET_RISCV64
3176 create_riscv_attribute_section(s1);
3177#endif
3178 /* Allocate strings for section names */3179 /* Allocate strings for section names */
3179 alloc_sec_names(s1, 1);3180 alloc_sec_names(s1, 1);
3180 file_offset = (sizeof (ElfW(Ehdr)) + 3) & -4;3181 file_offset = (sizeof (ElfW(Ehdr)) + 3) & -4;
tccgen.c+2−0
@@ -3280,6 +3280,8 @@ again: · 3279 unmodified lines
3280 dbt_bt = dbt & VT_BTYPE;3280 dbt_bt = dbt & VT_BTYPE;
3281 sbt_bt = sbt & VT_BTYPE;3281 sbt_bt = sbt & VT_BTYPE;
3282 if (dbt_bt == VT_VOID) {3282 if (dbt_bt == VT_VOID) {
3283 /* do not confuse backends with VT_VOID in registers */
3284 vpop(), vpushi(0);
3283 goto done;3285 goto done;
3284 }3286 }
3285 if (sbt_bt == VT_VOID) {3287 if (sbt_bt == VT_VOID) {
tccpe.c+82−174
@@ -27,54 +27,78 @@ · 26 unmodified lines
27#define stricmp strcasecmp27#define stricmp strcasecmp
28#define strnicmp strncasecmp28#define strnicmp strncasecmp
29#include <sys/stat.h> /* chmod() */29#include <sys/stat.h> /* chmod() */
30#else
31#include <process.h>
32#endif30#endif
33 31
34#ifdef TCC_TARGET_X86_6432#if defined TCC_TARGET_X86_64
35# define ADDR3264 ULONGLONG
36# define PE_IMAGE_REL IMAGE_REL_BASED_DIR64
37# define REL_TYPE_DIRECT R_X86_64_6433# define REL_TYPE_DIRECT R_X86_64_64
38# define R_XXX_THUNKFIX R_X86_64_PC3234# define R_XXX_THUNKFIX R_X86_64_PC32
39# define R_XXX_RELATIVE R_X86_64_RELATIVE35# define R_XXX_RELATIVE R_X86_64_RELATIVE
40# define R_XXX_FUNCCALL R_X86_64_PLT3236# define R_XXX_FUNCCALL R_X86_64_PLT32
41# define IMAGE_FILE_MACHINE 0x8664
42# define RSRC_RELTYPE 337# define RSRC_RELTYPE 3
38# define IMAGE_FILE_MACHINE 0x8664
39# define CHARACTERISTICS_EXE 0x022F
40# define CHARACTERISTICS_DLL 0x222E
41# define IMAGE_BASE_EXE 0x00400000
42# define IMAGE_BASE_DLL 0x10000000
43# define DLLCHARACTERISTICS 0
44# define OS_VER 0x0400
43 45
44#elif defined TCC_TARGET_ARM46#elif defined TCC_TARGET_ARM
45# define ADDR3264 DWORD
46# define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW
47# define REL_TYPE_DIRECT R_ARM_ABS3247# define REL_TYPE_DIRECT R_ARM_ABS32
48# define R_XXX_THUNKFIX R_ARM_ABS3248# define R_XXX_THUNKFIX R_ARM_ABS32
49# define R_XXX_RELATIVE R_ARM_RELATIVE49# define R_XXX_RELATIVE R_ARM_RELATIVE
50# define R_XXX_FUNCCALL R_ARM_PC2450# define R_XXX_FUNCCALL R_ARM_PC24
51# define R_XXX_FUNCCALL2 R_ARM_ABS3251# define R_XXX_FUNCCALL2 R_ARM_ABS32
52# define IMAGE_FILE_MACHINE 0x01C0
53# define RSRC_RELTYPE 7 /* ??? (not tested) */52# define RSRC_RELTYPE 7 /* ??? (not tested) */
53# define IMAGE_FILE_MACHINE 0x01C0
54# define CHARACTERISTICS_EXE 0x010F
55# define CHARACTERISTICS_DLL 0x230F
56# define IMAGE_BASE_EXE 0x00100000
57# define IMAGE_BASE_DLL 0x10000000
58# define DLLCHARACTERISTICS 0
59# define OS_VER 0x0400
54 60
55#elif defined TCC_TARGET_ARM6461#elif defined TCC_TARGET_ARM64
56# define ADDR3264 ULONGLONG
57# define PE_IMAGE_REL IMAGE_REL_BASED_DIR64
58# define REL_TYPE_DIRECT R_AARCH64_ABS6462# define REL_TYPE_DIRECT R_AARCH64_ABS64
59# define R_XXX_THUNKFIX R_AARCH64_ABS6463# define R_XXX_THUNKFIX R_AARCH64_ABS64
60# define R_XXX_RELATIVE R_AARCH64_RELATIVE64# define R_XXX_RELATIVE R_AARCH64_RELATIVE
61# define R_XXX_FUNCCALL R_AARCH64_CALL2665# define R_XXX_FUNCCALL R_AARCH64_CALL26
62# define IMAGE_FILE_MACHINE 0xAA64
63# define RSRC_RELTYPE 366# define RSRC_RELTYPE 3
67# define IMAGE_FILE_MACHINE 0xAA64
68# define CHARACTERISTICS_EXE 0x0022
69# define CHARACTERISTICS_DLL 0x2022
70# define IMAGE_BASE_EXE 0x140000000ULL
71# define IMAGE_BASE_DLL 0x180000000ULL
72# define OS_VER 0x0602
73# define DLLCHARACTERISTICS 0x8160
64 74
65#elif defined TCC_TARGET_I38675#elif defined TCC_TARGET_I386
66# define ADDR3264 DWORD
67# define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW
68# define REL_TYPE_DIRECT R_386_3276# define REL_TYPE_DIRECT R_386_32
69# define R_XXX_THUNKFIX R_386_3277# define R_XXX_THUNKFIX R_386_32
70# define R_XXX_RELATIVE R_386_RELATIVE78# define R_XXX_RELATIVE R_386_RELATIVE
71# define R_XXX_FUNCCALL R_386_PC3279# define R_XXX_FUNCCALL R_386_PC32
72# define IMAGE_FILE_MACHINE 0x014C
73# define RSRC_RELTYPE 7 /* DIR32NB */80# define RSRC_RELTYPE 7 /* DIR32NB */
81# define IMAGE_FILE_MACHINE 0x014C
82# define CHARACTERISTICS_EXE 0x030F
83# define CHARACTERISTICS_DLL 0x230E
84# define IMAGE_BASE_EXE 0x00400000
85# define IMAGE_BASE_DLL 0x10000000
86# define OS_VER 0x0400
87# define DLLCHARACTERISTICS 0
88#endif
74 89
90#if PTR_SIZE == 8
91# define ADDR3264 ULONGLONG
92# define PE_MAGIC 0x020B
93# define PE_IMAGE_REL IMAGE_REL_BASED_DIR64
94#else
95# define ADDR3264 DWORD
96# define PE_MAGIC 0x010B
97# define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW
75#endif98#endif
76 99
77#ifndef IMAGE_NT_SIGNATURE100#ifndef IMAGE_NT_SIGNATURE
101/* cross compiler: windows.h was not included */
78/* ----------------------------------------------------------- */102/* ----------------------------------------------------------- */
79/* definitions below are from winnt.h */103/* definitions below are from winnt.h */
80 104
@@ -107,7 +131,6 @@ typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */ · 26 unmodified lines
107} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;131} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
108 132
109#define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */133#define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
110#define SIZE_OF_NT_SIGNATURE 4
111 134
112typedef struct _IMAGE_FILE_HEADER {135typedef struct _IMAGE_FILE_HEADER {
113 WORD Machine;136 WORD Machine;
@@ -138,7 +161,7 @@ typedef struct _IMAGE_OPTIONAL_HEADER { · 24 unmodified lines
138 DWORD SizeOfUninitializedData;161 DWORD SizeOfUninitializedData;
139 DWORD AddressOfEntryPoint;162 DWORD AddressOfEntryPoint;
140 DWORD BaseOfCode;163 DWORD BaseOfCode;
141#if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)164#if PTR_SIZE == 4
142 DWORD BaseOfData;165 DWORD BaseOfData;
143#endif166#endif
144 /* NT additional fields. */167 /* NT additional fields. */
@@ -237,19 +260,6 @@ typedef struct _IMAGE_BASE_RELOCATION { · 92 unmodified lines
237 260
238#define IMAGE_SIZEOF_BASE_RELOCATION 8261#define IMAGE_SIZEOF_BASE_RELOCATION 8
239 262
240#ifndef IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA
241#define IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA
242#endif
243#ifndef IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
244#define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE PE_DLLCHARACTERISTICS_DYNAMIC_BASE
245#endif
246#ifndef IMAGE_DLLCHARACTERISTICS_NX_COMPAT
247#define IMAGE_DLLCHARACTERISTICS_NX_COMPAT PE_DLLCHARACTERISTICS_NX_COMPAT
248#endif
249#ifndef IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE
250#define IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE PE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE
251#endif
252
253#define IMAGE_REL_BASED_ABSOLUTE 0263#define IMAGE_REL_BASED_ABSOLUTE 0
254#define IMAGE_REL_BASED_HIGH 1264#define IMAGE_REL_BASED_HIGH 1
255#define IMAGE_REL_BASED_LOW 2265#define IMAGE_REL_BASED_LOW 2
@@ -269,34 +279,19 @@ typedef struct _IMAGE_BASE_RELOCATION { · 13 unmodified lines
269#define IMAGE_SCN_MEM_READ 0x40000000279#define IMAGE_SCN_MEM_READ 0x40000000
270#define IMAGE_SCN_MEM_WRITE 0x80000000280#define IMAGE_SCN_MEM_WRITE 0x80000000
271 281
282#define IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA 0x0020
283#define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE 0x0040
284#define IMAGE_DLLCHARACTERISTICS_NX_COMPAT 0x0100
285#define IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE 0x8000
286
287#define IMAGE_FILE_RELOCS_STRIPPED 0x0001
288
272#pragma pack(pop)289#pragma pack(pop)
273 290
274/* ----------------------------------------------------------- */291/* ----------------------------------------------------------- */
275#endif /* ndef IMAGE_NT_SIGNATURE */292#endif /* ndef IMAGE_NT_SIGNATURE */
276/* ----------------------------------------------------------- */293/* ----------------------------------------------------------- */
277 294
278static WORD pe_get_dll_characteristics(TCCState *s1)
279{
280 unsigned v = 0;
281
282#ifdef TCC_TARGET_ARM64
283 v = PE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA |
284 PE_DLLCHARACTERISTICS_DYNAMIC_BASE |
285 PE_DLLCHARACTERISTICS_NX_COMPAT |
286 PE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
287#endif
288 v |= s1->pe_dll_characteristics;
289 v &= ~s1->pe_dll_characteristics_clear;
290 return v;
291}
292
293#ifndef IMAGE_FILE_MACHINE_ARM64
294#define IMAGE_FILE_MACHINE_ARM64 0xAA64
295#endif
296#ifndef IMAGE_REL_BASED_DIR64
297# define IMAGE_REL_BASED_DIR64 10
298#endif
299
300#pragma pack(push, 1)295#pragma pack(push, 1)
301struct pe_header296struct pe_header
302{297{
@@ -304,7 +299,7 @@ struct pe_header · 1 unmodified line
304 BYTE dosstub[0x40];299 BYTE dosstub[0x40];
305 DWORD nt_sig;300 DWORD nt_sig;
306 IMAGE_FILE_HEADER filehdr;301 IMAGE_FILE_HEADER filehdr;
307#if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_ARM64)302#if PTR_SIZE == 8
308 IMAGE_OPTIONAL_HEADER64 opthdr;303 IMAGE_OPTIONAL_HEADER64 opthdr;
309#else304#else
310#ifdef _WIN64305#ifdef _WIN64
@@ -580,55 +575,17 @@ static void pe_add_coffsym(struct pe_info *pe) · 269 unmodified lines
580 575
581/* Run cv2pdb, available at https://github.com/rainers/cv2pdb. It reads576/* Run cv2pdb, available at https://github.com/rainers/cv2pdb. It reads
582 and strips the dwarf info and creates a <exename>.pdb file instead */577 and strips the dwarf info and creates a <exename>.pdb file instead */
583#ifndef _WIN32
584static void pe_shell_quote(CString *cmd, const char *arg)
585{
586 cstr_cat(cmd, "'", 1);
587 while (*arg) {
588 if (*arg == '\'')
589 cstr_cat(cmd, "'\\''", 4);
590 else
591 cstr_cat(cmd, arg, 1);
592 ++arg;
593 }
594 cstr_cat(cmd, "'", 1);
595}
596#endif
597
598static intptr_t pe_run_cv2pdb(const char *exename)
599{
600#ifdef _WIN32
601 const char *argv[] = { "cv2pdb.exe", exename, NULL };
602 return _spawnvp(_P_WAIT, "cv2pdb.exe", argv);
603#else
604 CString cmd;
605 intptr_t ret;
606
607 cstr_new(&cmd);
608 cstr_cat(&cmd, "cv2pdb.exe ", -1);
609 pe_shell_quote(&cmd, exename);
610 cstr_ccat(&cmd, 0);
611 ret = system(cmd.data);
612 cstr_free(&cmd);
613 return ret;
614#endif
615}
616
617static void pe_create_pdb(TCCState *s1, const char *exename)578static void pe_create_pdb(TCCState *s1, const char *exename)
618{579{
619 size_t len = strlen(exename);580 char buf[300]; int r;
620 char *pdbfile = tcc_malloc(len + sizeof(".pdb"));581 snprintf(buf, sizeof buf, "cv2pdb.exe \"%s\"", exename);
621 intptr_t r;582 r = system(buf);
622 583 strcpy(tcc_fileextension(strcpy(buf, exename)), ".pdb");
623 strcpy(pdbfile, exename);
624 strcpy(tcc_fileextension(pdbfile), ".pdb");
625 r = pe_run_cv2pdb(exename);
626 if (r) {584 if (r) {
627 tcc_error_noabort("could not create '%s'\n(need working cv2pdb from https://github.com/rainers/cv2pdb)", pdbfile);585 tcc_error_noabort("could not create '%s'\n(need working cv2pdb from https://github.com/rainers/cv2pdb)", buf);
628 } else if (s1->verbose) {586 } else if (s1->verbose) {
629 printf("<- %s\n", pdbfile);587 printf("<- %s\n", buf);
630 }588 }
631 tcc_free(pdbfile);
632}589}
633 590
634/*----------------------------------------------------------------------------*/591/*----------------------------------------------------------------------------*/
@@ -674,31 +631,12 @@ static int pe_write(struct pe_info *pe) · 39 unmodified lines
674 0x00000000, /*DWORD TimeDateStamp; */631 0x00000000, /*DWORD TimeDateStamp; */
675 0x00000000, /*DWORD PointerToSymbolTable; */632 0x00000000, /*DWORD PointerToSymbolTable; */
676 0x00000000, /*DWORD NumberOfSymbols; */633 0x00000000, /*DWORD NumberOfSymbols; */
677#if defined(TCC_TARGET_X86_64)634 0x00E0 + (PTR_SIZE-4)*4, /*WORD SizeOfOptionalHeader; */
678 0x00F0, /*WORD SizeOfOptionalHeader; */635 CHARACTERISTICS_EXE, /*WORD Characteristics; */
679 0x022F /*WORD Characteristics; */636 },{
680#define CHARACTERISTICS_DLL 0x222E
681#elif defined(TCC_TARGET_I386)
682 0x00E0, /*WORD SizeOfOptionalHeader; */
683 0x030F /*WORD Characteristics; */
684#define CHARACTERISTICS_DLL 0x230E
685#elif defined(TCC_TARGET_ARM)
686 0x00E0, /*WORD SizeOfOptionalHeader; */
687 0x010F, /*WORD Characteristics; */
688#define CHARACTERISTICS_DLL 0x230F
689#elif defined(TCC_TARGET_ARM64)
690 0x00F0, /*WORD SizeOfOptionalHeader; */
691 0x0022 /*WORD Characteristics; */
692#define CHARACTERISTICS_DLL 0x2022
693#endif
694},{
695 /* IMAGE_OPTIONAL_HEADER opthdr */637 /* IMAGE_OPTIONAL_HEADER opthdr */
696 /* Standard fields. */638 /* Standard fields. */
697#if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_ARM64)639 PE_MAGIC, /*WORD Magic; */
698 0x020B, /*WORD Magic; */
699#else
700 0x010B, /*WORD Magic; */
701#endif
702 0x06, /*BYTE MajorLinkerVersion; */640 0x06, /*BYTE MajorLinkerVersion; */
703 0x00, /*BYTE MinorLinkerVersion; */641 0x00, /*BYTE MinorLinkerVersion; */
704 0x00000000, /*DWORD SizeOfCode; */642 0x00000000, /*DWORD SizeOfCode; */
@@ -706,45 +644,29 @@ static int pe_write(struct pe_info *pe) · 1 unmodified line
706 0x00000000, /*DWORD SizeOfUninitializedData; */644 0x00000000, /*DWORD SizeOfUninitializedData; */
707 0x00000000, /*DWORD AddressOfEntryPoint; */645 0x00000000, /*DWORD AddressOfEntryPoint; */
708 0x00000000, /*DWORD BaseOfCode; */646 0x00000000, /*DWORD BaseOfCode; */
709#if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)647#if PTR_SIZE == 4
710 0x00000000, /*DWORD BaseOfData; */648 0x00000000, /*DWORD BaseOfData; */
711#endif649#endif
712 /* NT additional fields. */650 /* NT additional fields. */
713#if defined(TCC_TARGET_ARM)651 0x00000000, /*ADDR3264 ImageBase; */
714 0x00100000, /*DWORD ImageBase; */
715#elif defined(TCC_TARGET_ARM64)
716 0x140000000ULL, /*ULONGLONG ImageBase; */
717#else
718 0x00400000, /*DWORD ImageBase; */
719#endif
720 0x00001000, /*DWORD SectionAlignment; */652 0x00001000, /*DWORD SectionAlignment; */
721 0x00000200, /*DWORD FileAlignment; */653 0x00000200, /*DWORD FileAlignment; */
722#if defined(TCC_TARGET_ARM64)654 OS_VER >> 8, /*WORD MajorOperatingSystemVersion; */
723 0x0006, /*WORD MajorOperatingSystemVersion; */655 OS_VER & 255, /*WORD MinorOperatingSystemVersion; */
724 0x0002, /*WORD MinorOperatingSystemVersion; */
725#else
726 0x0004, /*WORD MajorOperatingSystemVersion; */
727 0x0000, /*WORD MinorOperatingSystemVersion; */
728#endif
729 0x0000, /*WORD MajorImageVersion; */656 0x0000, /*WORD MajorImageVersion; */
730 0x0000, /*WORD MinorImageVersion; */657 0x0000, /*WORD MinorImageVersion; */
731#if defined(TCC_TARGET_ARM64)658 OS_VER >> 8, /*WORD MajorSubsystemVersion; */
732 0x0006, /*WORD MajorSubsystemVersion; */659 OS_VER & 255, /*WORD MinorSubsystemVersion; */
733 0x0002, /*WORD MinorSubsystemVersion; */
734#else
735 0x0004, /*WORD MajorSubsystemVersion; */
736 0x0000, /*WORD MinorSubsystemVersion; */
737#endif
738 0x00000000, /*DWORD Win32VersionValue; */660 0x00000000, /*DWORD Win32VersionValue; */
739 0x00000000, /*DWORD SizeOfImage; */661 0x00000000, /*DWORD SizeOfImage; */
740 0x00000200, /*DWORD SizeOfHeaders; */662 0x00000200, /*DWORD SizeOfHeaders; */
741 0x00000000, /*DWORD CheckSum; */663 0x00000000, /*DWORD CheckSum; */
742 0x0002, /*WORD Subsystem; */664 0x0002, /*WORD Subsystem; */
743 0x0000, /*WORD DllCharacteristics; */665 DLLCHARACTERISTICS, /*WORD DllCharacteristics; */
744 0x00100000, /*DWORD SizeOfStackReserve; */666 0x00100000, /*ADDR3264 SizeOfStackReserve; */
745 0x00001000, /*DWORD SizeOfStackCommit; */667 0x00001000, /*ADDR3264 SizeOfStackCommit; */
746 0x00100000, /*DWORD SizeOfHeapReserve; */668 0x00100000, /*ADDR3264 SizeOfHeapReserve; */
747 0x00001000, /*DWORD SizeOfHeapCommit; */669 0x00001000, /*ADDR3264 SizeOfHeapCommit; */
748 0x00000000, /*DWORD LoaderFlags; */670 0x00000000, /*DWORD LoaderFlags; */
749 0x00000010, /*DWORD NumberOfRvaAndSizes; */671 0x00000010, /*DWORD NumberOfRvaAndSizes; */
750 672
@@ -799,7 +721,7 @@ static int pe_write(struct pe_info *pe) · 48 unmodified lines
799 break;721 break;
800 722
801 case sec_data:723 case sec_data:
802#if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)724#if PTR_SIZE == 4
803 if (!pe_header.opthdr.BaseOfData)725 if (!pe_header.opthdr.BaseOfData)
804 pe_header.opthdr.BaseOfData = addr;726 pe_header.opthdr.BaseOfData = addr;
805#endif727#endif
@@ -861,14 +783,14 @@ static int pe_write(struct pe_info *pe) · 55 unmodified lines
861 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;783 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;
862 pe_header.opthdr.ImageBase = pe->imagebase;784 pe_header.opthdr.ImageBase = pe->imagebase;
863 pe_header.opthdr.Subsystem = pe->subsystem;785 pe_header.opthdr.Subsystem = pe->subsystem;
864 pe_header.opthdr.DllCharacteristics = pe_get_dll_characteristics(s1);786 pe_header.opthdr.DllCharacteristics = s1->pe_dll_characteristics;
865 if (s1->pe_stack_size)787 if (s1->pe_stack_size)
866 pe_header.opthdr.SizeOfStackReserve = s1->pe_stack_size;788 pe_header.opthdr.SizeOfStackReserve = s1->pe_stack_size;
867 if (PE_DLL == pe->type)789 if (PE_DLL == pe->type)
868 pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL;790 pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL;
869 pe_header.filehdr.Characteristics |= s1->pe_characteristics;791 pe_header.filehdr.Characteristics |= s1->pe_characteristics;
870 if (pe->reloc)792 if (pe->reloc)
871 pe_header.filehdr.Characteristics &= ~PE_IMAGE_FILE_RELOCS_STRIPPED;793 pe_header.filehdr.Characteristics &= ~IMAGE_FILE_RELOCS_STRIPPED;
872 794
873 if (pe->coffsym) {795 if (pe->coffsym) {
874 pe_add_coffsym(pe);796 pe_add_coffsym(pe);
@@ -1291,8 +1213,8 @@ static int pe_assign_addresses (struct pe_info *pe) · 416 unmodified lines
1291 Section *s;1213 Section *s;
1292 TCCState *s1 = pe->s1;1214 TCCState *s1 = pe->s1;
1293 1215
1294 if (PE_DLL == pe->type ||1216 if (PE_DLL == pe->type
1295 (pe_get_dll_characteristics(s1) & PE_DLLCHARACTERISTICS_DYNAMIC_BASE))1217 || (s1->pe_dll_characteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
1296 pe->reloc = new_section(s1, ".reloc", SHT_PROGBITS, 0);1218 pe->reloc = new_section(s1, ".reloc", SHT_PROGBITS, 0);
1297 //pe->thunk = new_section(s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);1219 //pe->thunk = new_section(s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);
1298 1220
@@ -1483,14 +1405,10 @@ static int pe_check_symbols(struct pe_info *pe) · 184 unmodified lines
1483 offset + 8, R_XXX_THUNKFIX, is->iat_index); // offset to IAT position1405 offset + 8, R_XXX_THUNKFIX, is->iat_index); // offset to IAT position
1484#elif defined(TCC_TARGET_ARM64)1406#elif defined(TCC_TARGET_ARM64)
1485 p = section_ptr_add(text_section, 24);1407 p = section_ptr_add(text_section, 24);
1486 /* ldr x16, [pc, #16] */1408 write32le(p + 0, 0x58000090); /* ldr x16, [pc, #16] */
1487 write32le(p + 0, 0x58000090);1409 write32le(p + 4, 0xf9400210); /* ldr x16, [x16] */
1488 /* ldr x16, [x16] */1410 write32le(p + 8, 0xd61f0200); /* br x16 */
1489 write32le(p + 4, 0xf9400210);1411 write32le(p + 12, 0xd503201f); /* nop for alignment */
1490 /* br x16 */
1491 write32le(p + 8, 0xd61f0200);
1492 /* nop for 8-byte literal alignment */
1493 write32le(p + 12, 0xd503201f);
1494 put_elf_reloc(symtab_section, text_section,1412 put_elf_reloc(symtab_section, text_section,
1495 offset + 16, R_XXX_THUNKFIX, is->iat_index);1413 offset + 16, R_XXX_THUNKFIX, is->iat_index);
1496#else1414#else
@@ -1716,7 +1634,7 @@ static int get_dllexports(int fd, char **pp) · 219 unmodified lines
1716 if (!read_mem(fd, pef_hdroffset, &ih, sizeof ih))1634 if (!read_mem(fd, pef_hdroffset, &ih, sizeof ih))
1717 goto the_end;1635 goto the_end;
1718 opt_hdroffset = pef_hdroffset + sizeof ih;1636 opt_hdroffset = pef_hdroffset + sizeof ih;
1719 if (ih.Machine == 0x014C) {1637 if (ih.Machine == 0x014C || ih.Machine == 0x01C0) {
1720 IMAGE_OPTIONAL_HEADER32 oh;1638 IMAGE_OPTIONAL_HEADER32 oh;
1721 sec_hdroffset = opt_hdroffset + sizeof oh;1639 sec_hdroffset = opt_hdroffset + sizeof oh;
1722 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))1640 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))
@@ -1724,7 +1642,7 @@ static int get_dllexports(int fd, char **pp) · 1 unmodified line
1724 if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)1642 if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
1725 goto the_end_0;1643 goto the_end_0;
1726 addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;1644 addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
1727 } else if (ih.Machine == 0x8664 || ih.Machine == IMAGE_FILE_MACHINE_ARM64) {1645 } else if (ih.Machine == 0x8664 || ih.Machine == 0xAA64) {
1728 IMAGE_OPTIONAL_HEADER64 oh;1646 IMAGE_OPTIONAL_HEADER64 oh;
1729 sec_hdroffset = opt_hdroffset + sizeof oh;1647 sec_hdroffset = opt_hdroffset + sizeof oh;
1730 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))1648 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))
@@ -2209,19 +2127,9 @@ static void pe_set_options(TCCState * s1, struct pe_info *pe) · 478 unmodified lines
2209{2127{
2210 if (PE_DLL == pe->type) {2128 if (PE_DLL == pe->type) {
2211 /* XXX: check if is correct for arm-pe target */2129 /* XXX: check if is correct for arm-pe target */
2212#if defined(TCC_TARGET_ARM64)2130 pe->imagebase = IMAGE_BASE_DLL;
2213 pe->imagebase = 0x180000000ULL;
2214#else
2215 pe->imagebase = 0x10000000;
2216#endif
2217 } else {2131 } else {
2218#if defined(TCC_TARGET_ARM)2132 pe->imagebase = IMAGE_BASE_EXE;
2219 pe->imagebase = 0x00010000;
2220#elif defined(TCC_TARGET_ARM64)
2221 pe->imagebase = 0x140000000ULL;
2222#else
2223 pe->imagebase = 0x00400000;
2224#endif
2225 }2133 }
2226 2134
2227#if defined(TCC_TARGET_ARM)2135#if defined(TCC_TARGET_ARM)
tccrun.c+18−24
@@ -108,6 +108,11 @@ static void win64_del_function_table(void *); · 107 unmodified lines
108//#define CONFIG_SELINUX 1108//#define CONFIG_SELINUX 1
109#endif109#endif
110 110
111/* use VirtualAlloc() instead of tcc_malloc() */
112#if defined _WIN32 && !defined CONFIG_RUNMEM_VIRTUALALLOC
113# define CONFIG_RUNMEM_VIRTUALALLOC 1
114#endif
115
111static int rt_mem(TCCState *s1, int size)116static int rt_mem(TCCState *s1, int size)
112{117{
113 void *ptr;118 void *ptr;
@@ -129,16 +134,14 @@ static int rt_mem(TCCState *s1, int size) · 15 unmodified lines
129 ptr_diff = (char*)prw - (char*)ptr; /* = size; */134 ptr_diff = (char*)prw - (char*)ptr; /* = size; */
130 //printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff);135 //printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff);
131 size *= 2;136 size *= 2;
132#else137#elif CONFIG_RUNMEM_VIRTUALALLOC
133# ifdef _WIN32138 /* always page-aligned */
134 /* Generated code is page-protected below; avoid changing CRT heap pages. */
135 ptr = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);139 ptr = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
136# else
137 ptr = tcc_malloc(size += PAGESIZE); /* one extra page to align malloc memory */
138# endif
139#endif
140 if (!ptr)140 if (!ptr)
141 return tcc_error_noabort("tccrun: could not allocate memory");141 return tcc_error_noabort("tccrun: could not allocate memory");
142#else
143 ptr = tcc_malloc(size += PAGESIZE); /* one extra page to align malloc memory */
144#endif
142 s1->run_ptr = ptr;145 s1->run_ptr = ptr;
143 s1->run_size = size;146 s1->run_size = size;
144 return ptr_diff;147 return ptr_diff;
@@ -191,24 +194,18 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 46 unmodified lines
191 if (NULL == ptr)194 if (NULL == ptr)
192 return;195 return;
193 st_unlink(s1);196 st_unlink(s1);
197#ifdef _WIN64
198 win64_del_function_table(s1->run_function_table);
199#endif
194 size = s1->run_size;200 size = s1->run_size;
195#ifdef CONFIG_SELINUX201#ifdef CONFIG_SELINUX
196 munmap(ptr, size);202 munmap(ptr, size);
203#elif CONFIG_RUNMEM_VIRTUALALLOC
204 VirtualFree(ptr, size, MEM_RELEASE);
197#else205#else
198# ifdef _WIN32
199 (void)size;
200# ifdef _WIN64
201 win64_del_function_table(s1->run_function_table);
202# endif
203 VirtualFree(ptr, 0, MEM_RELEASE);
204# else
205 /* unprotect memory to make it usable for malloc again */206 /* unprotect memory to make it usable for malloc again */
206 protect_pages((void*)PAGEALIGN(ptr), size - PAGESIZE, 2 /*rw*/);207 protect_pages((void*)PAGEALIGN(ptr), size - PAGESIZE, 2 /*rw*/);
207# ifdef _WIN64
208 win64_del_function_table(s1->run_function_table);
209# endif
210 tcc_free(ptr);208 tcc_free(ptr);
211# endif
212#endif209#endif
213}210}
214 211
@@ -312,7 +309,7 @@ static void cleanup_sections(TCCState *s1) · 97 unmodified lines
312}309}
313 310
314/* ------------------------------------------------------------- */311/* ------------------------------------------------------------- */
315/* 0 = .text rwx other rw (memory >= 2 pages a 4096 bytes) */312/* 0 = .text rwx other rwx (memory >= 2 pages a 4096 bytes) */
316/* 1 = .text rx other rw (memory >= 3 pages) */313/* 1 = .text rx other rw (memory >= 3 pages) */
317/* 2 = .text rx .rdata ro .data/.bss rw (memory >= 4 pages) */314/* 2 = .text rx .rdata ro .data/.bss rw (memory >= 4 pages) */
318 315
@@ -506,14 +503,11 @@ static void *win64_add_function_table(TCCState *s1) · 187 unmodified lines
506 void *p = NULL;503 void *p = NULL;
507 if (s1->uw_pdata) {504 if (s1->uw_pdata) {
508 p = (void*)s1->uw_pdata->sh_addr;505 p = (void*)s1->uw_pdata->sh_addr;
509 if (!RtlAddFunctionTable(506 RtlAddFunctionTable(
510 (RUNTIME_FUNCTION*)p,507 (RUNTIME_FUNCTION*)p,
511 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),508 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
512 s1->pe_imagebase509 s1->pe_imagebase
513 )) {510 );
514 tcc_error_noabort("RtlAddFunctionTable failed");
515 p = NULL;
516 }
517 s1->uw_pdata = NULL;511 s1->uw_pdata = NULL;
518 }512 }
519 return p;513 return p;
tests/tests2/Makefile+9−11
@@ -19,9 +19,6 @@ ifeq (,$(filter i386 x86_64,$(ARCH))) · 18 unmodified lines
19 SKIP += 85_asm-outside-function.test # x86 asm19 SKIP += 85_asm-outside-function.test # x86 asm
20 SKIP += 127_asm_goto.test # hardcodes x86 asm20 SKIP += 127_asm_goto.test # hardcodes x86 asm
21endif21endif
22ifeq (,$(filter x86_64 riscv64 arm64 i386,$(ARCH)))
23 SKIP += 144_tls.test # TLS only implemented on these architectures so far
24endif
25ifeq ($(CONFIG_backtrace),no)22ifeq ($(CONFIG_backtrace),no)
26 SKIP += 113_btdll.test23 SKIP += 113_btdll.test
27 CONFIG_bcheck = no24 CONFIG_bcheck = no
@@ -57,22 +54,23 @@ ifneq (,$(filter OpenBSD FreeBSD NetBSD,$(TARGETOS))) · 29 unmodified lines
57 SKIP += 114_bound_signal.test # libc problem signal/fork54 SKIP += 114_bound_signal.test # libc problem signal/fork
58 SKIP += 116_bound_setjmp2.test # No TLS_FUNC/TLS_VAR in bcheck.c55 SKIP += 116_bound_setjmp2.test # No TLS_FUNC/TLS_VAR in bcheck.c
59endif56endif
60ifdef CONFIG_OSX57ifeq ($(CONFIG_OSX),yes)
61 SKIP += 144_tls.test # TLS runtime not supported on Mach-O58 SKIP += 144_tls.test # TLS runtime not supported on Mach-O
62endif59endif
63ifeq (,$(filter arm64 aarch64,$(ARCH)))60ifeq (,$(filter i386 x86_64 riscv64 arm64,$(ARCH)))
61 SKIP += 144_tls.test # TLS only implemented on these architectures so far
62endif
63ifneq (-$(ARCH)-,-arm64-)
64 SKIP += 138_arm64_encoding.test64 SKIP += 138_arm64_encoding.test
65 SKIP += 139_arm64_errors.test65 SKIP += 139_arm64_errors.test
66 SKIP += 140_arm64_extasm.test66 SKIP += 140_arm64_extasm.test
67endif67endif
68ifneq ($(CONFIG_WIN32),yes)68ifneq (-$(ARCH)-,-riscv64-)
69 SKIP += 145_winarm64_interlocked.test
70else ifeq (,$(filter arm64 aarch64,$(ARCH)))
71 SKIP += 145_winarm64_interlocked.test
72endif
73ifeq (,$(filter riscv64,$(ARCH)))
74 SKIP += 141_riscv_asm.test # riscv64 asm69 SKIP += 141_riscv_asm.test # riscv64 asm
75endif70endif
71ifneq (-$(ARCH)-$(CONFIG_WIN32)-,-arm64-yes-)
72 SKIP += 145_winarm64_interlocked.test
73endif
76 74
77# Some tests might need arguments75# Some tests might need arguments
78ARGS =76ARGS =

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t