summaryrefslogtreecommitdiff
path: root/tccpe.c
diff options
context:
space:
mode:
authorThomas Preud'homme <robotux@celest.fr>2016-12-17 11:04:34 +0000
committerThomas Preud'homme <robotux@celest.fr>2016-12-17 11:04:34 +0000
commit3f2e65a51523fbb98a44b71c29ae3a3fcc13854b (patch)
tree6e401550a4eddcc55a9db4b685e6a4968550a661 /tccpe.c
parent83bd60f5184818823d7be84af0930236d0d892f7 (diff)
New upstream version 0.9.27~git20161217.cd9514ab
Diffstat (limited to 'tccpe.c')
-rw-r--r--tccpe.c125
1 files changed, 68 insertions, 57 deletions
diff --git a/tccpe.c b/tccpe.c
index eb13d2d..e2101bb 100644
--- a/tccpe.c
+++ b/tccpe.c
@@ -20,6 +20,9 @@
#include "tcc.h"
+#define PE_MERGE_DATA
+/* #define PE_PRINT_SECTIONS */
+
#ifndef _WIN32
#define stricmp strcasecmp
#define strnicmp strncasecmp
@@ -29,9 +32,6 @@
#define MAX_PATH 260
#endif
-#define PE_MERGE_DATA
-/* #define PE_PRINT_SECTIONS */
-
#ifdef TCC_TARGET_X86_64
# define ADDR3264 ULONGLONG
# define REL_TYPE_DIRECT R_X86_64_64
@@ -58,6 +58,7 @@
#endif
+#if 0
#ifdef _WIN32
void dbg_printf (const char *fmt, ...)
{
@@ -70,6 +71,7 @@ void dbg_printf (const char *fmt, ...)
OutputDebugString(buffer);
}
#endif
+#endif
/* ----------------------------------------------------------- */
#ifndef IMAGE_NT_SIGNATURE
@@ -830,7 +832,7 @@ static void pe_build_imports(struct pe_info *pe)
if (dllref) {
if ( !dllref->handle )
dllref->handle = LoadLibrary(dllref->name);
- v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(LPCSTR)NULL+ordinal:name);
+ v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
}
if (!v)
tcc_error_noabort("can't build symbol '%s'", name);
@@ -1072,6 +1074,9 @@ static int pe_assign_addresses (struct pe_info *pe)
struct section_info *si;
Section *s;
+ if (PE_DLL == pe->type)
+ pe->reloc = new_section(pe->s1, ".reloc", SHT_PROGBITS, 0);
+
// pe->thunk = new_section(pe->s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);
section_order = tcc_malloc(pe->s1->nb_sections * sizeof (int));
@@ -1445,6 +1450,7 @@ static void pe_print_sections(TCCState *s1, const char *fname)
/* ------------------------------------------------------------- */
/* helper function for load/store to insert one more indirection */
+#ifndef TCC_TARGET_ARM
ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2)
{
Sym *sym;
@@ -1484,10 +1490,11 @@ ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2)
v2->r |= sv->r & VT_LVAL;
return v2;
}
+#endif
ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)
{
- return add_elf_sym(
+ return set_elf_sym(
s1->dynsymtab_section,
value,
dllindex, /* st_size */
@@ -1564,17 +1571,18 @@ quit:
}
/* ------------------------------------------------------------- */
+
static char *trimfront(char *p)
{
while (*p && (unsigned char)*p <= ' ')
- ++p;
+ ++p;
return p;
}
static char *trimback(char *a, char *e)
{
while (e > a && (unsigned char)e[-1] <= ' ')
- --e;
+ --e;
*e = 0;;
return a;
}
@@ -1661,7 +1669,7 @@ ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd)
ret = pe_load_def(s1, fd);
else if (pe_load_res(s1, fd) == 0)
ret = 0;
- else if (read_mem(fd, 0, buf, sizeof buf) && 0 == strncmp(buf, "MZ", 2))
+ else if (read_mem(fd, 0, buf, 4) && 0 == memcmp(buf, "MZ\220", 4))
ret = pe_load_dll(s1, tcc_basename(filename), fd);
return ret;
}
@@ -1762,7 +1770,7 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe)
/* grab the startup code from libtcc1 */
if (TCC_OUTPUT_MEMORY != s1->output_type || PE_GUI == pe_type)
- add_elf_sym(symtab_section,
+ set_elf_sym(symtab_section,
0, 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
SHN_UNDEF, start_symbol);
@@ -1796,7 +1804,54 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe)
pe->type = pe_type;
}
-ST_FUNC int pe_output_file(TCCState * s1, const char *filename)
+static void pe_set_options(TCCState * s1, struct pe_info *pe)
+{
+ if (PE_DLL == pe->type) {
+ /* XXX: check if is correct for arm-pe target */
+ pe->imagebase = 0x10000000;
+ } else {
+#if defined(TCC_TARGET_ARM)
+ pe->imagebase = 0x00010000;
+#else
+ pe->imagebase = 0x00400000;
+#endif
+ }
+
+#if defined(TCC_TARGET_ARM)
+ /* we use "console" subsystem by default */
+ pe->subsystem = 9;
+#else
+ if (PE_DLL == pe->type || PE_GUI == pe->type)
+ pe->subsystem = 2;
+ else
+ pe->subsystem = 3;
+#endif
+ /* Allow override via -Wl,-subsystem=... option */
+ if (s1->pe_subsystem != 0)
+ pe->subsystem = s1->pe_subsystem;
+
+ /* set default file/section alignment */
+ if (pe->subsystem == 1) {
+ pe->section_align = 0x20;
+ pe->file_align = 0x20;
+ } else {
+ pe->section_align = 0x1000;
+ pe->file_align = 0x200;
+ }
+
+ if (s1->section_align != 0)
+ pe->section_align = s1->section_align;
+ if (s1->pe_file_align != 0)
+ pe->file_align = s1->pe_file_align;
+
+ if ((pe->subsystem >= 10) && (pe->subsystem <= 12))
+ pe->imagebase = 0;
+
+ if (s1->has_text_addr)
+ pe->imagebase = s1->text_addr;
+}
+
+ST_FUNC int pe_output_file(TCCState *s1, const char *filename)
{
int ret;
struct pe_info pe;
@@ -1806,62 +1861,18 @@ ST_FUNC int pe_output_file(TCCState * s1, const char *filename)
pe.filename = filename;
pe.s1 = s1;
- pe_add_runtime(s1, &pe);
tcc_add_bcheck(s1);
+ pe_add_runtime(s1, &pe);
relocate_common_syms(); /* assign bss adresses */
tcc_add_linker_symbols(s1);
+ pe_set_options(s1, &pe);
ret = pe_check_symbols(&pe);
if (ret)
;
else if (filename) {
- if (PE_DLL == pe.type) {
- pe.reloc = new_section(pe.s1, ".reloc", SHT_PROGBITS, 0);
- /* XXX: check if is correct for arm-pe target */
- pe.imagebase = 0x10000000;
- } else {
-#if defined(TCC_TARGET_ARM)
- pe.imagebase = 0x00010000;
-#else
- pe.imagebase = 0x00400000;
-#endif
- }
-
-#if defined(TCC_TARGET_ARM)
- /* we use "console" subsystem by default */
- pe.subsystem = 9;
-#else
- if (PE_DLL == pe.type || PE_GUI == pe.type)
- pe.subsystem = 2;
- else
- pe.subsystem = 3;
-#endif
- /* Allow override via -Wl,-subsystem=... option */
- if (s1->pe_subsystem != 0)
- pe.subsystem = s1->pe_subsystem;
-
- /* set default file/section alignment */
- if (pe.subsystem == 1) {
- pe.section_align = 0x20;
- pe.file_align = 0x20;
- } else {
- pe.section_align = 0x1000;
- pe.file_align = 0x200;
- }
-
- if (s1->section_align != 0)
- pe.section_align = s1->section_align;
- if (s1->pe_file_align != 0)
- pe.file_align = s1->pe_file_align;
-
- if ((pe.subsystem >= 10) && (pe.subsystem <= 12))
- pe.imagebase = 0;
-
- if (s1->has_text_addr)
- pe.imagebase = s1->text_addr;
-
pe_assign_addresses(&pe);
- relocate_syms(s1, 0);
+ relocate_syms(s1, s1->symtab, 0);
for (i = 1; i < s1->nb_sections; ++i) {
Section *s = s1->sections[i];
if (s->reloc) {