History

portability: fix void* <-> target address conversion confusion

- #define addr_t as ElfW(Addr) - replace uplong by addr_t - #define TCC_HAS_RUNTIME_PLTGOT and use it

grischka committed 13 years ago

Commit 82bcbd01 parent 31864556 files · +136 −130Browse files

Unified · Split

6 files
  1. libtcc.c +6 −6
  2. tcc.h +60 −55
  3. tcccoff.c +1 −1
  4. tccelf.c +40 −37
  5. tccpe.c +7 −7
  6. tccrun.c +22 −24
libtcc.c+6−6
@@ -423,7 +423,7 @@ ST_FUNC Section *find_section(TCCState *s1, const char *name) · 422 unmodified lines
423/* update sym->c so that it points to an external symbol in section423/* update sym->c so that it points to an external symbol in section
424 'section' with value 'value' */424 'section' with value 'value' */
425ST_FUNC void put_extern_sym2(Sym *sym, Section *section, 425ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
426 uplong value, unsigned long size,426 addr_t value, unsigned long size,
427 int can_add_underscore)427 int can_add_underscore)
428{428{
429 int sym_type, sym_bind, sh_num, info, other;429 int sym_type, sym_bind, sh_num, info, other;
@@ -527,7 +527,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, · 97 unmodified lines
527}527}
528 528
529ST_FUNC void put_extern_sym(Sym *sym, Section *section, 529ST_FUNC void put_extern_sym(Sym *sym, Section *section,
530 uplong value, unsigned long size)530 addr_t value, unsigned long size)
531{531{
532 put_extern_sym2(sym, section, value, size, 1);532 put_extern_sym2(sym, section, value, size, 1);
533}533}
@@ -1279,11 +1279,11 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 745 unmodified lines
1279LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)1279LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1280{1280{
1281#ifdef TCC_TARGET_PE1281#ifdef TCC_TARGET_PE
1282 pe_putimport(s, 0, name, val);1282pe_putimport(s, 0, name, (uintptr_t)val);
1283#else1283#else
1284 add_elf_sym(symtab_section, (uplong)val, 0,1284add_elf_sym(symtab_section, (uintptr_t)val, 0,
1285 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1285 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1286 SHN_ABS, name);1286 SHN_ABS, name);
1287#endif1287#endif
1288 return 0;1288 return 0;
1289}1289}
tcc.h+60−55
@@ -42,25 +42,28 @@ · 41 unmodified lines
42#include <setjmp.h>42#include <setjmp.h>
43#include <time.h>43#include <time.h>
44 44
45#ifdef _WIN32
46#include <windows.h>
47#include <sys/timeb.h>
48#include <io.h> /* open, close etc. */
49#include <direct.h> /* getcwd */
50#define inline __inline
51#define inp next_inp
52#ifdef LIBTCC_AS_DLL
53# define LIBTCCAPI __declspec(dllexport)
54# define PUB_FUNC LIBTCCAPI
55#endif
56#endif
57
58#ifndef _WIN3245#ifndef _WIN32
59#include <unistd.h>46# include <unistd.h>
60#include <sys/time.h>47# include <sys/time.h>
61#include <sys/ucontext.h>48# include <sys/ucontext.h>
62#include <sys/mman.h>49# include <sys/mman.h>
63#include <dlfcn.h>50# include <dlfcn.h>
51#else
52# include <windows.h>
53# include <sys/timeb.h>
54# include <io.h> /* open, close etc. */
55# include <direct.h> /* getcwd */
56# ifdef __GNUC__
57# include <stdint.h>
58# else
59 typedef UINT_PTR uintptr_t;
60# endif
61# define inline __inline
62# define inp next_inp
63# ifdef LIBTCC_AS_DLL
64# define LIBTCCAPI __declspec(dllexport)
65# define PUB_FUNC LIBTCCAPI
66# endif
64#endif67#endif
65 68
66#endif /* !CONFIG_TCCBOOT */69#endif /* !CONFIG_TCCBOOT */
@@ -78,6 +81,27 @@ · 11 unmodified lines
78#endif81#endif
79 82
80#include "elf.h"83#include "elf.h"
84#ifdef TCC_TARGET_X86_64
85# define ELFCLASSW ELFCLASS64
86# define ElfW(type) Elf##64##_##type
87# define ELFW(type) ELF##64##_##type
88# define ElfW_Rel ElfW(Rela)
89# define SHT_RELX SHT_RELA
90# define REL_SECTION_FMT ".rela%s"
91/* XXX: DLL with PLT would only work with x86-64 for now */
92# define TCC_OUTPUT_DLL_WITH_PLT
93#else
94# define ELFCLASSW ELFCLASS32
95# define ElfW(type) Elf##32##_##type
96# define ELFW(type) ELF##32##_##type
97# define ElfW_Rel ElfW(Rel)
98# define SHT_RELX SHT_REL
99# define REL_SECTION_FMT ".rel%s"
100#endif
101
102/* target address type */
103#define addr_t ElfW(Addr)
104
81#include "stab.h"105#include "stab.h"
82#include "libtcc.h"106#include "libtcc.h"
83 107
@@ -139,13 +163,6 @@ · 55 unmodified lines
139# define CONFIG_TCC_BACKTRACE163# define CONFIG_TCC_BACKTRACE
140#endif164#endif
141 165
142/* target address type */
143#if defined TCC_TARGET_X86_64 && (!defined __x86_64__ || defined _WIN32)
144# define uplong unsigned long long
145#else
146# define uplong unsigned long
147#endif
148
149/* ------------ path configuration ------------ */166/* ------------ path configuration ------------ */
150 167
151#ifndef CONFIG_SYSROOT168#ifndef CONFIG_SYSROOT
@@ -327,7 +344,7 @@ typedef struct Section { · 175 unmodified lines
327 int sh_addralign; /* elf section alignment */344 int sh_addralign; /* elf section alignment */
328 int sh_entsize; /* elf entry size */345 int sh_entsize; /* elf entry size */
329 unsigned long sh_size; /* section size (only used during output) */346 unsigned long sh_size; /* section size (only used during output) */
330 uplong sh_addr; /* address at which the section is relocated */347 addr_t sh_addr; /* address at which the section is relocated */
331 unsigned long sh_offset; /* file offset */348 unsigned long sh_offset; /* file offset */
332 int nb_hashed_syms; /* used to resize the hash table */349 int nb_hashed_syms; /* used to resize the hash table */
333 struct Section *link; /* link to another section */350 struct Section *link; /* link to another section */
@@ -552,7 +569,7 @@ struct TCCState { · 218 unmodified lines
552 int alacarte_link;569 int alacarte_link;
553 570
554 /* address of text section */571 /* address of text section */
555 uplong text_addr;572 addr_t text_addr;
556 int has_text_addr;573 int has_text_addr;
557 574
558 /* symbols to call at load-time / unload-time */575 /* symbols to call at load-time / unload-time */
@@ -644,12 +661,12 @@ struct TCCState { · 85 unmodified lines
644#endif661#endif
645#endif662#endif
646 663
647#ifndef TCC_TARGET_PE664#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE \
648#if defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM665 && (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM)
649 /* write PLT and GOT here */666 /* write PLT and GOT here */
650 char *runtime_plt_and_got;667 char *runtime_plt_and_got;
651 unsigned int runtime_plt_and_got_offset;668 unsigned int runtime_plt_and_got_offset;
652#endif669# define TCC_HAS_RUNTIME_PLTGOT
653#endif670#endif
654};671};
655 672
@@ -1007,8 +1024,8 @@ ST_FUNC void *section_ptr_add(Section *sec, unsigned long size); · 351 unmodified lines
1007ST_FUNC void section_reserve(Section *sec, unsigned long size);1024ST_FUNC void section_reserve(Section *sec, unsigned long size);
1008ST_FUNC Section *find_section(TCCState *s1, const char *name);1025ST_FUNC Section *find_section(TCCState *s1, const char *name);
1009 1026
1010ST_FUNC void put_extern_sym2(Sym *sym, Section *section, uplong value, unsigned long size, int can_add_underscore);1027ST_FUNC void put_extern_sym2(Sym *sym, Section *section, addr_t value, unsigned long size, int can_add_underscore);
1011ST_FUNC void put_extern_sym(Sym *sym, Section *section, uplong value, unsigned long size);1028ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
1012ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);1029ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1013 1030
1014ST_INLN void sym_free(Sym *sym);1031ST_INLN void sym_free(Sym *sym);
@@ -1187,8 +1204,8 @@ typedef struct { · 172 unmodified lines
1187ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, const char *strtab_name, const char *hash_name, int hash_sh_flags);1204ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, const char *strtab_name, const char *hash_name, int hash_sh_flags);
1188 1205
1189ST_FUNC int put_elf_str(Section *s, const char *sym);1206ST_FUNC int put_elf_str(Section *s, const char *sym);
1190ST_FUNC int put_elf_sym(Section *s, uplong value, unsigned long size, int info, int other, int shndx, const char *name);1207ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1191ST_FUNC int add_elf_sym(Section *s, uplong value, unsigned long size, int info, int other, int sh_num, const char *name);1208ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int sh_num, const char *name);
1192ST_FUNC int find_elf_sym(Section *s, const char *name);1209ST_FUNC int find_elf_sym(Section *s, const char *name);
1193ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);1210ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1194 1211
@@ -1204,12 +1221,16 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s); · 9 unmodified lines
1204ST_FUNC void tcc_add_linker_symbols(TCCState *s1);1221ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
1205ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);1222ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1206ST_FUNC int tcc_load_archive(TCCState *s1, int fd);1223ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
1207ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1208ST_FUNC void tcc_add_bcheck(TCCState *s1);1224ST_FUNC void tcc_add_bcheck(TCCState *s1);
1209 1225
1210ST_FUNC void build_got_entries(TCCState *s1);1226ST_FUNC void build_got_entries(TCCState *s1);
1211ST_FUNC void tcc_add_runtime(TCCState *s1);1227ST_FUNC void tcc_add_runtime(TCCState *s1);
1212 1228
1229ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
1230#ifdef TCC_IS_NATIVE
1231ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1232#endif
1233
1213#ifndef TCC_TARGET_PE1234#ifndef TCC_TARGET_PE
1214ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);1235ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1215ST_FUNC int tcc_load_ldscript(TCCState *s1);1236ST_FUNC int tcc_load_ldscript(TCCState *s1);
@@ -1219,24 +1240,6 @@ ST_INLN void inp(void); · 3 unmodified lines
1219ST_FUNC int handle_eob(void);1240ST_FUNC int handle_eob(void);
1220#endif1241#endif
1221 1242
1222#ifdef TCC_TARGET_X86_64
1223# define ELFCLASSW ELFCLASS64
1224# define ElfW(type) Elf##64##_##type
1225# define ELFW(type) ELF##64##_##type
1226# define ElfW_Rel ElfW(Rela)
1227# define SHT_RELX SHT_RELA
1228# define REL_SECTION_FMT ".rela%s"
1229/* XXX: DLL with PLT would only work with x86-64 for now */
1230# define TCC_OUTPUT_DLL_WITH_PLT
1231#else
1232# define ELFCLASSW ELFCLASS32
1233# define ElfW(type) Elf##32##_##type
1234# define ELFW(type) ELF##32##_##type
1235# define ElfW_Rel ElfW(Rel)
1236# define SHT_RELX SHT_REL
1237# define REL_SECTION_FMT ".rel%s"
1238#endif
1239
1240/* ------------ xxx-gen.c ------------ */1243/* ------------ xxx-gen.c ------------ */
1241 1244
1242ST_FUNC void gsym_addr(int t, int a);1245ST_FUNC void gsym_addr(int t, int a);
@@ -1319,7 +1322,7 @@ ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str); · 76 unmodified lines
1319#ifdef TCC_TARGET_PE1322#ifdef TCC_TARGET_PE
1320ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);1323ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
1321ST_FUNC int pe_output_file(TCCState * s1, const char *filename);1324ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1322ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, const void *value);1325ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
1323ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);1326ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1324/* tiny_impdef.c */1327/* tiny_impdef.c */
1325ST_FUNC char *get_export_names(FILE *fp);1328ST_FUNC char *get_export_names(FILE *fp);
@@ -1329,6 +1332,7 @@ ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack); · 3 unmodified lines
1329#endif1332#endif
1330 1333
1331/* ------------ tccrun.c ----------------- */1334/* ------------ tccrun.c ----------------- */
1335#ifdef TCC_IS_NATIVE
1332#ifdef CONFIG_TCC_STATIC1336#ifdef CONFIG_TCC_STATIC
1333#define RTLD_LAZY 0x0011337#define RTLD_LAZY 0x001
1334#define RTLD_NOW 0x0021338#define RTLD_NOW 0x002
@@ -1339,7 +1343,7 @@ ST_FUNC void *dlopen(const char *filename, int flag); · 4 unmodified lines
1339ST_FUNC void dlclose(void *p);1343ST_FUNC void dlclose(void *p);
1340//ST_FUNC const char *dlerror(void);1344//ST_FUNC const char *dlerror(void);
1341ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);1345ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1342#elif !defined TCC_TARGET_PE || !defined _WIN321346#elif !defined _WIN32
1343ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);1347ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1344#endif1348#endif
1345 1349
@@ -1349,6 +1353,7 @@ ST_DATA const char **rt_bound_error_msg; · 3 unmodified lines
1349ST_DATA void *rt_prog_main;1353ST_DATA void *rt_prog_main;
1350PUB_FUNC void tcc_set_num_callers(int n);1354PUB_FUNC void tcc_set_num_callers(int n);
1351#endif1355#endif
1356#endif
1352 1357
1353/********************************************************/1358/********************************************************/
1354/* include the target specific definitions */1359/* include the target specific definitions */
tcccoff.c+1−1
@@ -933,7 +933,7 @@ ST_FUNC int tcc_load_coff(TCCState * s1, int fd) · 932 unmodified lines
933 if (name[0] == '_' && strcmp(name, "_main") != 0)933 if (name[0] == '_' && strcmp(name, "_main") != 0)
934 name++;934 name++;
935 935
936 tcc_add_symbol(s1, name, (void*)(uplong)csym.n_value);936 tcc_add_symbol(s1, name, (void*)(uintptr_t)csym.n_value);
937 }937 }
938 // skip any aux records938 // skip any aux records
939 939
tccelf.c+40−37
@@ -84,7 +84,7 @@ static void rebuild_hash(Section *s, unsigned int nb_buckets) · 83 unmodified lines
84}84}
85 85
86/* return the symbol number */86/* return the symbol number */
87ST_FUNC int put_elf_sym(Section *s, uplong value, unsigned long size,87ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
88 int info, int other, int shndx, const char *name)88 int info, int other, int shndx, const char *name)
89{89{
90 int name_offset, sym_index;90 int name_offset, sym_index;
@@ -157,7 +157,7 @@ ST_FUNC int find_elf_sym(Section *s, const char *name) · 66 unmodified lines
157}157}
158 158
159/* return elf symbol value, signal error if 'err' is nonzero */159/* return elf symbol value, signal error if 'err' is nonzero */
160static void *get_elf_sym_addr(TCCState *s, const char *name, int err)160ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
161{161{
162 int sym_index;162 int sym_index;
163 ElfW(Sym) *sym;163 ElfW(Sym) *sym;
@@ -167,26 +167,28 @@ static void *get_elf_sym_addr(TCCState *s, const char *name, int err) · 3 unmodified lines
167 if (!sym_index || sym->st_shndx == SHN_UNDEF) {167 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
168 if (err)168 if (err)
169 tcc_error("%s not defined", name);169 tcc_error("%s not defined", name);
170 return NULL;170 return 0;
171 }171 }
172 return (void*)(uplong)sym->st_value;172 return sym->st_value;
173}173}
174 174
175#ifdef TCC_IS_NATIVE
175/* return elf symbol value */176/* return elf symbol value */
176LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)177LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
177{178{
178 return get_elf_sym_addr(s, name, 0);179 return (void*)get_elf_sym_addr(s, name, 0);
179}180}
180 181
181/* return elf symbol value or error */182/* return elf symbol value or error */
182ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name)183ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
183{184{
184 return get_elf_sym_addr(s, name, 1);185 return (void*)get_elf_sym_addr(s, name, 1);
185}186}
187#endif
186 188
187/* add an elf symbol : check if it is already defined and patch189/* add an elf symbol : check if it is already defined and patch
188 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */190 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
189ST_FUNC int add_elf_sym(Section *s, uplong value, unsigned long size,191ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size,
190 int info, int other, int sh_num, const char *name)192 int info, int other, int sh_num, const char *name)
191{193{
192 ElfW(Sym) *esym;194 ElfW(Sym) *esym;
@@ -431,12 +433,12 @@ ST_FUNC void relocate_syms(TCCState *s1, int do_resolve) · 238 unmodified lines
431 if (sh_num == SHN_UNDEF) {433 if (sh_num == SHN_UNDEF) {
432 name = strtab_section->data + sym->st_name;434 name = strtab_section->data + sym->st_name;
433 if (do_resolve) {435 if (do_resolve) {
434#if !defined TCC_TARGET_PE || !defined _WIN32436#if defined TCC_IS_NATIVE && !defined _WIN32
435 void *addr;437 void *addr;
436 name = symtab_section->link->data + sym->st_name;438 name = symtab_section->link->data + sym->st_name;
437 addr = resolve_sym(s1, name);439 addr = resolve_sym(s1, name);
438 if (addr) {440 if (addr) {
439 sym->st_value = (uplong)addr;441 sym->st_value = (addr_t)addr;
440 goto found;442 goto found;
441 }443 }
442#endif444#endif
@@ -469,10 +471,10 @@ ST_FUNC void relocate_syms(TCCState *s1, int do_resolve) · 26 unmodified lines
469 }471 }
470}472}
471 473
472#ifndef TCC_TARGET_PE474#ifdef TCC_HAS_RUNTIME_PLTGOT
473#ifdef TCC_TARGET_X86_64475#ifdef TCC_TARGET_X86_64
474#define JMP_TABLE_ENTRY_SIZE 14476#define JMP_TABLE_ENTRY_SIZE 14
475static uplong add_jmp_table(TCCState *s1, uplong val)477static addr_t add_jmp_table(TCCState *s1, addr_t val)
476{478{
477 char *p = s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset;479 char *p = s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset;
478 s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE;480 s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE;
@@ -480,30 +482,30 @@ static uplong add_jmp_table(TCCState *s1, uplong val) · 1 unmodified line
480 p[0] = 0xff;482 p[0] = 0xff;
481 p[1] = 0x25;483 p[1] = 0x25;
482 *(int *)(p + 2) = 0;484 *(int *)(p + 2) = 0;
483 *(uplong *)(p + 6) = val;485 *(addr_t *)(p + 6) = val;
484 return (uplong)p;486 return (addr_t)p;
485}487}
486 488
487static uplong add_got_table(TCCState *s1, uplong val)489static addr_t add_got_table(TCCState *s1, addr_t val)
488{490{
489 uplong *p = (uplong *)(s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset);491 addr_t *p = (addr_t *)(s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset);
490 s1->runtime_plt_and_got_offset += sizeof(uplong);492 s1->runtime_plt_and_got_offset += sizeof(addr_t);
491 *p = val;493 *p = val;
492 return (uplong)p;494 return (addr_t)p;
493}495}
494#elif defined TCC_TARGET_ARM496#elif defined TCC_TARGET_ARM
495#define JMP_TABLE_ENTRY_SIZE 8497#define JMP_TABLE_ENTRY_SIZE 8
496static uplong add_jmp_table(TCCState *s1, int val)498static addr_t add_jmp_table(TCCState *s1, int val)
497{499{
498 uint32_t *p = (uint32_t *)(s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset);500 uint32_t *p = (uint32_t *)(s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset);
499 s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE;501 s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE;
500 /* ldr pc, [pc, #-4] */502 /* ldr pc, [pc, #-4] */
501 p[0] = 0xE51FF004;503 p[0] = 0xE51FF004;
502 p[1] = val;504 p[1] = val;
503 return (uplong)p;505 return (addr_t)p;
504}506}
505#endif507#endif
506#endif508#endif /* def TCC_HAS_RUNTIME_PLTGOT */
507 509
508/* relocate a given section (CPU dependent) */510/* relocate a given section (CPU dependent) */
509ST_FUNC void relocate_section(TCCState *s1, Section *s)511ST_FUNC void relocate_section(TCCState *s1, Section *s)
@@ -513,7 +515,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 3 unmodified lines
513 ElfW(Sym) *sym;515 ElfW(Sym) *sym;
514 int type, sym_index;516 int type, sym_index;
515 unsigned char *ptr;517 unsigned char *ptr;
516 uplong val, addr;518 addr_t val, addr;
517#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64519#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
518 int esym_index;520 int esym_index;
519#endif521#endif
@@ -612,13 +614,14 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 92 unmodified lines
612 is_call = (type == R_ARM_CALL);614 is_call = (type == R_ARM_CALL);
613 x += val - addr;615 x += val - addr;
614 h = x & 2;616 h = x & 2;
615#ifndef TCC_TARGET_PE617#ifdef TCC_HAS_RUNTIME_PLTGOT
616 if ((x & 3) || x >= 0x2000000 || x < -0x2000000)618 if (s1->output_type == TCC_OUTPUT_MEMORY) {
617 if (!(x & 3) || !blx_avail || !is_call)619 if ((x & 3) || x >= 0x2000000 || x < -0x2000000)
618 if (s1->output_type == TCC_OUTPUT_MEMORY) {620 if (!(x & 3) || !blx_avail || !is_call) {
619 x += add_jmp_table(s1, val) - val; /* add veneer */621 x += add_jmp_table(s1, val) - val; /* add veneer */
620 is_thumb = 0; /* Veneer uses ARM instructions */622 is_thumb = 0; /* Veneer uses ARM instructions */
621 }623 }
624 }
622#endif625#endif
623 if ((x & 3) || x >= 0x2000000 || x < -0x2000000)626 if ((x & 3) || x >= 0x2000000 || x < -0x2000000)
624 if (!(x & 3) || !blx_avail || !is_call)627 if (!(x & 3) || !blx_avail || !is_call)
@@ -770,8 +773,8 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 145 unmodified lines
770 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */773 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
771 break;774 break;
772 default:775 default:
773 fprintf(stderr,"FIXME: handle reloc type %x at %x [%.8x] to %x\n",776 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
774 type, (unsigned)addr, (unsigned)(uplong)ptr, (unsigned)val);777 type, (unsigned)addr, ptr, (unsigned)val);
775 break;778 break;
776#elif defined(TCC_TARGET_C67)779#elif defined(TCC_TARGET_C67)
777 case R_C60_32:780 case R_C60_32:
@@ -796,8 +799,8 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 18 unmodified lines
796 case R_C60HI16:799 case R_C60HI16:
797 break;800 break;
798 default:801 default:
799 fprintf(stderr,"FIXME: handle reloc type %x at %x [%.8x] to %x\n",802 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
800 type, (unsigned)addr, (unsigned)(uplong)ptr, (unsigned)val);803 type, (unsigned)addr, ptr, (unsigned)val);
801 break;804 break;
802#elif defined(TCC_TARGET_X86_64)805#elif defined(TCC_TARGET_X86_64)
803 case R_X86_64_64:806 case R_X86_64_64:
@@ -837,7 +840,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 33 unmodified lines
837 long long diff;840 long long diff;
838 diff = (long long)val - addr;841 diff = (long long)val - addr;
839 if (diff <= -2147483647 || diff > 2147483647) {842 if (diff <= -2147483647 || diff > 2147483647) {
840#ifndef TCC_TARGET_PE843#ifdef TCC_HAS_RUNTIME_PLTGOT
841 /* XXX: naive support for over 32bit jump */844 /* XXX: naive support for over 32bit jump */
842 if (s1->output_type == TCC_OUTPUT_MEMORY) {845 if (s1->output_type == TCC_OUTPUT_MEMORY) {
843 val = (add_jmp_table(s1, val - rel->r_addend) +846 val = (add_jmp_table(s1, val - rel->r_addend) +
@@ -858,7 +861,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 14 unmodified lines
858 *(int *)ptr = val - rel->r_addend;861 *(int *)ptr = val - rel->r_addend;
859 break;862 break;
860 case R_X86_64_GOTPCREL:863 case R_X86_64_GOTPCREL:
861#ifndef TCC_TARGET_PE864#ifdef TCC_HAS_RUNTIME_PLTGOT
862 if (s1->output_type == TCC_OUTPUT_MEMORY) {865 if (s1->output_type == TCC_OUTPUT_MEMORY) {
863 val = add_got_table(s1, val - rel->r_addend) + rel->r_addend;866 val = add_got_table(s1, val - rel->r_addend) + rel->r_addend;
864 *(int *)ptr += val - addr;867 *(int *)ptr += val - addr;
@@ -1275,7 +1278,7 @@ ST_FUNC Section *new_symtab(TCCState *s1, · 410 unmodified lines
1275}1278}
1276 1279
1277/* put dynamic tag */1280/* put dynamic tag */
1278static void put_dt(Section *dynamic, int dt, uplong val)1281static void put_dt(Section *dynamic, int dt, addr_t val)
1279{1282{
1280 ElfW(Dyn) *dyn;1283 ElfW(Dyn) *dyn;
1281 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));1284 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
@@ -1545,7 +1548,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 263 unmodified lines
1545 int *section_order;1548 int *section_order;
1546 int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k;1549 int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k;
1547 long long tmp;1550 long long tmp;
1548 uplong addr;1551 addr_t addr;
1549 Section *strsec, *s;1552 Section *strsec, *s;
1550 ElfW(Shdr) shdr, *sh;1553 ElfW(Shdr) shdr, *sh;
1551 ElfW(Phdr) *phdr, *ph;1554 ElfW(Phdr) *phdr, *ph;
@@ -1553,9 +1556,9 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1 unmodified line
1553 unsigned long saved_dynamic_data_offset;1556 unsigned long saved_dynamic_data_offset;
1554 ElfW(Sym) *sym;1557 ElfW(Sym) *sym;
1555 int type, file_type;1558 int type, file_type;
1556 uplong rel_addr, rel_size;1559 addr_t rel_addr, rel_size;
1557#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)1560#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1558 uplong bss_addr, bss_size;1561 addr_t bss_addr, bss_size;
1559#endif1562#endif
1560 1563
1561 file_type = s1->output_type;1564 file_type = s1->output_type;
@@ -2191,7 +2194,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 629 unmodified lines
2191 2194
2192 /* get entry point address */2195 /* get entry point address */
2193 if (file_type == TCC_OUTPUT_EXE)2196 if (file_type == TCC_OUTPUT_EXE)
2194 ehdr.e_entry = (uplong)tcc_get_symbol_err(s1, "_start");2197 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
2195 else2198 else
2196 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */2199 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
2197 }2200 }
tccpe.c+7−7
@@ -1,4 +1,4 @@
1 /*1/*
2 * TCCPE.C - PE file output for the Tiny C Compiler2 * TCCPE.C - PE file output for the Tiny C Compiler
3 *3 *
4 * Copyright (c) 2005-2007 grischka4 * Copyright (c) 2005-2007 grischka
@@ -1480,11 +1480,11 @@ ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2) · 1475 unmodified lines
1480 return v2;1480 return v2;
1481}1481}
1482 1482
1483ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, const void *value)1483ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)
1484{1484{
1485 return add_elf_sym(1485 return add_elf_sym(
1486 s1->dynsymtab_section,1486 s1->dynsymtab_section,
1487 (uplong)value,1487 value,
1488 dllindex, /* st_size */1488 dllindex, /* st_size */
1489 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE),1489 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE),
1490 0,1490 0,
@@ -1613,7 +1613,7 @@ static int pe_load_def(TCCState *s1, FILE *fp) · 122 unmodified lines
1613 ++state;1613 ++state;
1614 1614
1615 default:1615 default:
1616 pe_putimport(s1, dllindex, p, NULL);1616 pe_putimport(s1, dllindex, p, 0);
1617 continue;1617 continue;
1618 }1618 }
1619 }1619 }
@@ -1635,7 +1635,7 @@ static int pe_load_dll(TCCState *s1, const char *dllname, FILE *fp) · 15 unmodified lines
1635 return -1;1635 return -1;
1636 index = add_dllref(s1, dllname);1636 index = add_dllref(s1, dllname);
1637 for (q = p; *q; q += 1 + strlen(q))1637 for (q = p; *q; q += 1 + strlen(q))
1638 pe_putimport(s1, index, q, NULL);1638 pe_putimport(s1, index, q, 0);
1639 tcc_free(p);1639 tcc_free(p);
1640 return 0;1640 return 0;
1641}1641}
@@ -1729,7 +1729,7 @@ ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack) · 87 unmodified lines
1729static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)1729static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
1730{1730{
1731 const char *start_symbol;1731 const char *start_symbol;
1732 unsigned long addr = 0;1732 ADDR3264 addr = 0;
1733 int pe_type = 0;1733 int pe_type = 0;
1734 1734
1735 if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16")))1735 if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16")))
@@ -1781,7 +1781,7 @@ static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 45 unmodified lines
1781 pe_type = PE_RUN;1781 pe_type = PE_RUN;
1782 1782
1783 if (start_symbol) {1783 if (start_symbol) {
1784 addr = (uplong)tcc_get_symbol_err(s1, start_symbol);1784 addr = get_elf_sym_addr(s1, start_symbol, 1);
1785 if (PE_RUN == pe_type && addr)1785 if (PE_RUN == pe_type && addr)
1786 /* for -run GUI's, put '_runwinmain' instead of 'main' */1786 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1787 add_elf_sym(symtab_section,1787 add_elf_sym(symtab_section,
tccrun.c+22−24
@@ -35,7 +35,7 @@ ST_DATA void *rt_prog_main; · 34 unmodified lines
35 35
36static void set_pages_executable(void *ptr, unsigned long length);36static void set_pages_executable(void *ptr, unsigned long length);
37static void set_exception_handler(void);37static void set_exception_handler(void);
38static int rt_get_caller_pc(uplong *paddr, ucontext_t *uc, int level);38static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level);
39static void rt_error(ucontext_t *uc, const char *fmt, ...);39static void rt_error(ucontext_t *uc, const char *fmt, ...);
40static int tcc_relocate_ex(TCCState *s1, void *ptr);40static int tcc_relocate_ex(TCCState *s1, void *ptr);
41 41
@@ -127,7 +127,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) · 85 unmodified lines
127{127{
128 Section *s;128 Section *s;
129 unsigned long offset, length;129 unsigned long offset, length;
130 uplong mem;130 addr_t mem;
131 int i;131 int i;
132 132
133 if (0 == s1->runtime_added) {133 if (0 == s1->runtime_added) {
@@ -145,7 +145,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) · 11 unmodified lines
145 return -1;145 return -1;
146 }146 }
147 147
148 offset = 0, mem = (uplong)ptr;148 offset = 0, mem = (addr_t)ptr;
149 for(i = 1; i < s1->nb_sections; i++) {149 for(i = 1; i < s1->nb_sections; i++) {
150 s = s1->sections[i];150 s = s1->sections[i];
151 if (0 == (s->sh_flags & SHF_ALLOC))151 if (0 == (s->sh_flags & SHF_ALLOC))
@@ -161,7 +161,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) · 9 unmodified lines
161 if (s1->nb_errors)161 if (s1->nb_errors)
162 return -1;162 return -1;
163 163
164#if (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM) && !defined TCC_TARGET_PE164#ifdef TCC_HAS_RUNTIME_PLTGOT
165 s1->runtime_plt_and_got_offset = 0;165 s1->runtime_plt_and_got_offset = 0;
166 s1->runtime_plt_and_got = (char *)(mem + offset);166 s1->runtime_plt_and_got = (char *)(mem + offset);
167 /* double the size of the buffer for got and plt entries167 /* double the size of the buffer for got and plt entries
@@ -185,7 +185,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) · 17 unmodified lines
185 continue;185 continue;
186 length = s->data_offset;186 length = s->data_offset;
187 // printf("%-12s %08x %04x\n", s->name, s->sh_addr, length);187 // printf("%-12s %08x %04x\n", s->name, s->sh_addr, length);
188 ptr = (void*)(uplong)s->sh_addr;188 ptr = (void*)s->sh_addr;
189 if (NULL == s->data || s->sh_type == SHT_NOBITS)189 if (NULL == s->data || s->sh_type == SHT_NOBITS)
190 memset(ptr, 0, length);190 memset(ptr, 0, length);
191 else191 else
@@ -195,7 +195,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) · 3 unmodified lines
195 set_pages_executable(ptr, length);195 set_pages_executable(ptr, length);
196 }196 }
197 197
198#if (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM) && !defined TCC_TARGET_PE198#ifdef TCC_HAS_RUNTIME_PLTGOT
199 set_pages_executable(s1->runtime_plt_and_got,199 set_pages_executable(s1->runtime_plt_and_got,
200 s1->runtime_plt_and_got_offset);200 s1->runtime_plt_and_got_offset);
201#endif201#endif
@@ -215,17 +215,15 @@ static void set_pages_executable(void *ptr, unsigned long length) · 13 unmodified lines
215 unsigned long old_protect;215 unsigned long old_protect;
216 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);216 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
217#else217#else
218 unsigned long start, end;218 addr_t start, end;
219 start = (uplong)ptr & ~(PAGESIZE - 1);219 start = (addr_t)ptr & ~(PAGESIZE - 1);
220 end = (uplong)ptr + length;220 end = (addr_t)ptr + length;
221 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);221 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
222 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);222 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
223#endif223#endif
224}224}
225 225
226/* ------------------------------------------------------------- */226/* ------------------------------------------------------------- */
227#endif /* TCC_IS_NATIVE */
228
229#ifdef CONFIG_TCC_BACKTRACE227#ifdef CONFIG_TCC_BACKTRACE
230 228
231PUB_FUNC void tcc_set_num_callers(int n)229PUB_FUNC void tcc_set_num_callers(int n)
@@ -235,10 +233,10 @@ PUB_FUNC void tcc_set_num_callers(int n) · 3 unmodified lines
235 233
236/* print the position in the source file of PC value 'pc' by reading234/* print the position in the source file of PC value 'pc' by reading
237 the stabs debug information */235 the stabs debug information */
238static uplong rt_printline(uplong wanted_pc, const char *msg)236static addr_t rt_printline(addr_t wanted_pc, const char *msg)
239{237{
240 char func_name[128], last_func_name[128];238 char func_name[128], last_func_name[128];
241 uplong func_addr, last_pc, pc;239 addr_t func_addr, last_pc, pc;
242 const char *incl_files[INCLUDE_STACK_SIZE];240 const char *incl_files[INCLUDE_STACK_SIZE];
243 int incl_index, len, last_line_num, i;241 int incl_index, len, last_line_num, i;
244 const char *str, *p;242 const char *str, *p;
@@ -257,7 +255,7 @@ static uplong rt_printline(uplong wanted_pc, const char *msg) · 12 unmodified lines
257 func_addr = 0;255 func_addr = 0;
258 incl_index = 0;256 incl_index = 0;
259 last_func_name[0] = '\0';257 last_func_name[0] = '\0';
260 last_pc = (uplong)-1;258 last_pc = (addr_t)-1;
261 last_line_num = 1;259 last_line_num = 1;
262 260
263 if (!stab_sym)261 if (!stab_sym)
@@ -380,7 +378,7 @@ no_stabs: · 116 unmodified lines
380static void rt_error(ucontext_t *uc, const char *fmt, ...)378static void rt_error(ucontext_t *uc, const char *fmt, ...)
381{379{
382 va_list ap;380 va_list ap;
383 uplong pc;381 addr_t pc;
384 int i;382 int i;
385 383
386 fprintf(stderr, "Runtime error: ");384 fprintf(stderr, "Runtime error: ");
@@ -393,7 +391,7 @@ static void rt_error(ucontext_t *uc, const char *fmt, ...) · 6 unmodified lines
393 if (rt_get_caller_pc(&pc, uc, i) < 0)391 if (rt_get_caller_pc(&pc, uc, i) < 0)
394 break;392 break;
395 pc = rt_printline(pc, i ? "by" : "at");393 pc = rt_printline(pc, i ? "by" : "at");
396 if (pc == (uplong)rt_prog_main && pc)394 if (pc == (addr_t)rt_prog_main && pc)
397 break;395 break;
398 }396 }
399}397}
@@ -464,7 +462,7 @@ static void set_exception_handler(void) · 64 unmodified lines
464#endif462#endif
465 463
466/* return the PC at frame level 'level'. Return non zero if not found */464/* return the PC at frame level 'level'. Return non zero if not found */
467static int rt_get_caller_pc(unsigned long *paddr, ucontext_t *uc, int level)465static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
468{466{
469 unsigned long fp;467 unsigned long fp;
470 int i;468 int i;
@@ -634,17 +632,17 @@ static void set_exception_handler(void) · 163 unmodified lines
634static void win64_add_function_table(TCCState *s1)632static void win64_add_function_table(TCCState *s1)
635{633{
636 RtlAddFunctionTable(634 RtlAddFunctionTable(
637 (RUNTIME_FUNCTION*)(uplong)s1->uw_pdata->sh_addr,635 (RUNTIME_FUNCTION*)s1->uw_pdata->sh_addr,
638 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),636 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
639 (uplong)text_section->sh_addr637 text_section->sh_addr
640 );638 );
641}639}
642#endif640#endif
643 641
644/* return the PC at frame level 'level'. Return non zero if not found */642/* return the PC at frame level 'level'. Return non zero if not found */
645static int rt_get_caller_pc(uplong *paddr, CONTEXT *uc, int level)643static int rt_get_caller_pc(addr_t *paddr, CONTEXT *uc, int level)
646{644{
647 uplong fp, pc;645 addr_t fp, pc;
648 int i;646 int i;
649#ifdef _WIN64647#ifdef _WIN64
650 pc = uc->Rip;648 pc = uc->Rip;
@@ -658,9 +656,9 @@ static int rt_get_caller_pc(uplong *paddr, CONTEXT *uc, int level) · 7 unmodified lines
658 /* XXX: check address validity with program info */656 /* XXX: check address validity with program info */
659 if (fp <= 0x1000 || fp >= 0xc0000000)657 if (fp <= 0x1000 || fp >= 0xc0000000)
660 return -1;658 return -1;
661 fp = ((uplong*)fp)[0];659 fp = ((addr_t*)fp)[0];
662 }660 }
663 pc = ((uplong*)fp)[1];661 pc = ((addr_t*)fp)[1];
664 }662 }
665 *paddr = pc;663 *paddr = pc;
666 return 0;664 return 0;
@@ -730,5 +728,5 @@ ST_FUNC void *resolve_sym(TCCState *s1, const char *sym) · 63 unmodified lines
730}728}
731 729
732#endif /* CONFIG_TCC_STATIC */730#endif /* CONFIG_TCC_STATIC */
733 731#endif /* TCC_IS_NATIVE */
734/* ------------------------------------------------------------- */732/* ------------------------------------------------------------- */

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