2009-05-05 22:17:11 +04:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
2013-11-08 23:24:15 +04:00
|
|
|
*
|
2009-05-05 22:17:11 +04:00
|
|
|
* Copyright (c) 2001-2004 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#include "tcc.h"
|
|
|
|
|
2009-05-11 20:45:56 +04:00
|
|
|
/********************************************************/
|
|
|
|
/* global variables */
|
|
|
|
|
2009-05-05 22:30:13 +04:00
|
|
|
/* use GNU C extensions */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_DATA int gnu_ext = 1;
|
2009-05-05 22:30:13 +04:00
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/* use TinyCC extensions */
|
|
|
|
ST_DATA int tcc_ext = 1;
|
2009-05-05 22:30:13 +04:00
|
|
|
|
|
|
|
/* XXX: get rid of this ASAP */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_DATA struct TCCState *tcc_state;
|
2009-05-11 20:45:56 +04:00
|
|
|
|
2009-07-06 23:13:20 +04:00
|
|
|
/********************************************************/
|
2009-05-11 20:45:56 +04:00
|
|
|
|
2011-07-14 20:45:37 +04:00
|
|
|
#ifdef ONE_SOURCE
|
2009-12-20 03:53:49 +03:00
|
|
|
#include "tccpp.c"
|
|
|
|
#include "tccgen.c"
|
|
|
|
#include "tccelf.c"
|
|
|
|
#include "tccrun.c"
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef TCC_TARGET_I386
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "i386-gen.c"
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_ARM
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "arm-gen.c"
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2015-02-13 21:58:31 +03:00
|
|
|
#ifdef TCC_TARGET_ARM64
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "arm64-gen.c"
|
2015-02-13 21:58:31 +03:00
|
|
|
#endif
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef TCC_TARGET_C67
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "c67-gen.c"
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "x86_64-gen.c"
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2009-07-06 23:13:20 +04:00
|
|
|
#ifdef CONFIG_TCC_ASM
|
2009-12-20 03:53:49 +03:00
|
|
|
#include "tccasm.c"
|
2009-12-20 00:08:37 +03:00
|
|
|
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "i386-asm.c"
|
2009-07-06 23:13:20 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_COFF
|
|
|
|
#include "tcccoff.c"
|
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
#include "tccpe.c"
|
|
|
|
#endif
|
2011-07-22 13:12:49 +04:00
|
|
|
#endif /* ONE_SOURCE */
|
2009-07-06 23:13:20 +04:00
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/********************************************************/
|
|
|
|
#ifndef CONFIG_TCC_ASM
|
|
|
|
ST_FUNC void asm_instr(void)
|
|
|
|
{
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("inline asm() not supported");
|
2009-12-20 03:53:49 +03:00
|
|
|
}
|
|
|
|
ST_FUNC void asm_global_instr(void)
|
|
|
|
{
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("inline asm() not supported");
|
2009-12-20 03:53:49 +03:00
|
|
|
}
|
|
|
|
#endif
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-07-19 00:07:25 +04:00
|
|
|
/********************************************************/
|
|
|
|
#ifdef _WIN32
|
2009-12-20 03:53:49 +03:00
|
|
|
static char *normalize_slashes(char *path)
|
2009-07-19 00:07:25 +04:00
|
|
|
{
|
|
|
|
char *p;
|
2013-08-29 00:55:05 +04:00
|
|
|
for (p = path; *p; ++p)
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '/';
|
2009-07-19 00:07:25 +04:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
static HMODULE tcc_module;
|
2009-07-19 00:07:25 +04:00
|
|
|
|
|
|
|
/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
|
2009-12-20 03:53:49 +03:00
|
|
|
static void tcc_set_lib_path_w32(TCCState *s)
|
2009-07-19 00:07:25 +04:00
|
|
|
{
|
|
|
|
char path[1024], *p;
|
|
|
|
GetModuleFileNameA(tcc_module, path, sizeof path);
|
|
|
|
p = tcc_basename(normalize_slashes(strlwr(path)));
|
|
|
|
if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
|
|
|
|
p -= 5;
|
|
|
|
else if (p > path)
|
|
|
|
p--;
|
|
|
|
*p = 0;
|
|
|
|
tcc_set_lib_path(s, path);
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:01:07 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
2011-08-01 03:10:36 +04:00
|
|
|
static void tcc_add_systemdir(TCCState *s)
|
|
|
|
{
|
|
|
|
char buf[1000];
|
|
|
|
GetSystemDirectory(buf, sizeof buf);
|
2011-08-11 18:55:30 +04:00
|
|
|
tcc_add_library_path(s, normalize_slashes(buf));
|
2011-08-01 03:10:36 +04:00
|
|
|
}
|
2013-02-06 22:01:07 +04:00
|
|
|
#endif
|
2011-08-01 03:10:36 +04:00
|
|
|
|
2009-12-20 22:33:41 +03:00
|
|
|
#ifndef CONFIG_TCC_STATIC
|
|
|
|
void dlclose(void *p)
|
|
|
|
{
|
|
|
|
FreeLibrary((HMODULE)p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-19 00:07:25 +04:00
|
|
|
#ifdef LIBTCC_AS_DLL
|
2014-04-12 07:49:53 +04:00
|
|
|
BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
|
2009-07-19 00:07:25 +04:00
|
|
|
{
|
|
|
|
if (DLL_PROCESS_ATTACH == dwReason)
|
|
|
|
tcc_module = hDll;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/********************************************************/
|
|
|
|
/* copy a string and truncate it. */
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
char *q, *q_end;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (buf_size > 0) {
|
|
|
|
q = buf;
|
|
|
|
q_end = buf + buf_size - 1;
|
|
|
|
while (q < q_end) {
|
|
|
|
c = *s++;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
*q++ = c;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* strcat and truncate. */
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
len = strlen(buf);
|
2013-11-08 23:24:15 +04:00
|
|
|
if (len < buf_size)
|
2009-05-05 22:18:53 +04:00
|
|
|
pstrcpy(buf + len, buf_size - len, s);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2011-08-01 03:10:36 +04:00
|
|
|
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num)
|
|
|
|
{
|
|
|
|
memcpy(out, in, num);
|
|
|
|
out[num] = '\0';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* extract the basename of a file */
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC char *tcc_basename(const char *name)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
char *p = strchr(name, 0);
|
2011-08-01 03:10:36 +04:00
|
|
|
while (p > name && !IS_DIRSEP(p[-1]))
|
2009-05-05 22:18:53 +04:00
|
|
|
--p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-06-20 19:17:42 +04:00
|
|
|
/* extract extension part of a file
|
|
|
|
*
|
|
|
|
* (if no extension, return pointer to end-of-string)
|
|
|
|
*/
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC char *tcc_fileextension (const char *name)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
char *b = tcc_basename(name);
|
|
|
|
char *e = strrchr(b, '.');
|
|
|
|
return e ? e : strchr(b, 0);
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/********************************************************/
|
2009-05-05 22:18:53 +04:00
|
|
|
/* memory management */
|
2009-12-20 03:53:49 +03:00
|
|
|
|
|
|
|
#undef free
|
|
|
|
#undef malloc
|
|
|
|
#undef realloc
|
|
|
|
|
2015-05-12 11:56:39 +03:00
|
|
|
#ifndef MEM_DEBUG
|
2009-05-05 22:18:53 +04:00
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC void tcc_free(void *ptr)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC void *tcc_malloc(unsigned long size)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ptr = malloc(size);
|
|
|
|
if (!ptr && size)
|
2014-01-06 22:56:26 +04:00
|
|
|
tcc_error("memory full (malloc)");
|
2009-05-05 22:18:53 +04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC void *tcc_mallocz(unsigned long size)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ptr = tcc_malloc(size);
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
void *ptr1;
|
|
|
|
ptr1 = realloc(ptr, size);
|
2012-04-18 20:43:55 +04:00
|
|
|
if (!ptr1 && size)
|
2014-01-06 22:56:26 +04:00
|
|
|
tcc_error("memory full (realloc)");
|
2009-05-05 22:18:53 +04:00
|
|
|
return ptr1;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
PUB_FUNC char *tcc_strdup(const char *str)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
ptr = tcc_malloc(strlen(str) + 1);
|
|
|
|
strcpy(ptr, str);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:07:09 +03:00
|
|
|
PUB_FUNC void tcc_memstats(int bench)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-05-12 11:56:39 +03:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define MEM_DEBUG_MAGIC1 0xFEEDDEB1
|
|
|
|
#define MEM_DEBUG_MAGIC2 0xFEEDDEB2
|
|
|
|
#define MEM_DEBUG_FILE_LEN 15
|
|
|
|
|
|
|
|
struct mem_debug_header {
|
|
|
|
size_t magic1;
|
|
|
|
size_t size;
|
|
|
|
struct mem_debug_header *prev;
|
|
|
|
struct mem_debug_header *next;
|
|
|
|
size_t line_num;
|
|
|
|
char file_name[MEM_DEBUG_FILE_LEN + 1];
|
|
|
|
size_t magic2;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct mem_debug_header mem_debug_header_t;
|
|
|
|
|
|
|
|
static mem_debug_header_t *mem_debug_chain;
|
|
|
|
static size_t mem_cur_size;
|
|
|
|
static size_t mem_max_size;
|
|
|
|
|
|
|
|
PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
mem_debug_header_t *header;
|
|
|
|
|
|
|
|
ptr = malloc(sizeof(mem_debug_header_t) + size);
|
|
|
|
if (!ptr)
|
|
|
|
tcc_error("memory full (malloc)");
|
|
|
|
|
|
|
|
mem_cur_size += size;
|
|
|
|
if (mem_cur_size > mem_max_size)
|
|
|
|
mem_max_size = mem_cur_size;
|
|
|
|
|
|
|
|
header = (mem_debug_header_t *)ptr;
|
|
|
|
|
|
|
|
header->magic1 = MEM_DEBUG_MAGIC1;
|
|
|
|
header->magic2 = MEM_DEBUG_MAGIC2;
|
|
|
|
header->size = size;
|
|
|
|
header->line_num = line;
|
|
|
|
|
|
|
|
strncpy(header->file_name, file, MEM_DEBUG_FILE_LEN);
|
|
|
|
header->file_name[MEM_DEBUG_FILE_LEN] = 0;
|
|
|
|
|
|
|
|
header->next = mem_debug_chain;
|
|
|
|
header->prev = NULL;
|
|
|
|
|
|
|
|
if (header->next)
|
|
|
|
header->next->prev = header;
|
|
|
|
|
|
|
|
mem_debug_chain = header;
|
|
|
|
|
|
|
|
ptr = (char *)ptr + sizeof(mem_debug_header_t);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUB_FUNC void tcc_free_debug(void *ptr)
|
|
|
|
{
|
|
|
|
mem_debug_header_t *header;
|
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ptr = (char *)ptr - sizeof(mem_debug_header_t);
|
|
|
|
header = (mem_debug_header_t *)ptr;
|
|
|
|
if (header->magic1 != MEM_DEBUG_MAGIC1 ||
|
|
|
|
header->magic2 != MEM_DEBUG_MAGIC2 ||
|
|
|
|
header->size == (size_t)-1 )
|
|
|
|
{
|
|
|
|
tcc_error("tcc_free check failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_cur_size -= header->size;
|
|
|
|
header->size = (size_t)-1;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2015-05-12 11:56:39 +03:00
|
|
|
if (header->next)
|
|
|
|
header->next->prev = header->prev;
|
|
|
|
|
|
|
|
if (header->prev)
|
|
|
|
header->prev->next = header->next;
|
|
|
|
|
|
|
|
if (header == mem_debug_chain)
|
|
|
|
mem_debug_chain = header->next;
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ptr = tcc_malloc_debug(size,file,line);
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
|
|
|
|
{
|
|
|
|
mem_debug_header_t *header;
|
|
|
|
int mem_debug_chain_update = 0;
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
ptr = tcc_malloc_debug(size, file, line);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = (char *)ptr - sizeof(mem_debug_header_t);
|
|
|
|
header = (mem_debug_header_t *)ptr;
|
|
|
|
if (header->magic1 != MEM_DEBUG_MAGIC1 ||
|
|
|
|
header->magic2 != MEM_DEBUG_MAGIC2 ||
|
|
|
|
header->size == (size_t)-1 )
|
|
|
|
{
|
|
|
|
check_error:
|
|
|
|
tcc_error("tcc_realloc check failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_debug_chain_update = (header == mem_debug_chain);
|
|
|
|
|
|
|
|
mem_cur_size -= header->size;
|
|
|
|
ptr = realloc(ptr, sizeof(mem_debug_header_t) + size);
|
|
|
|
if (!ptr)
|
|
|
|
tcc_error("memory full (realloc)");
|
|
|
|
|
|
|
|
header = (mem_debug_header_t *)ptr;
|
|
|
|
if (header->magic1 != MEM_DEBUG_MAGIC1 ||
|
|
|
|
header->magic2 != MEM_DEBUG_MAGIC2)
|
|
|
|
{
|
|
|
|
goto check_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_cur_size += size;
|
|
|
|
if (mem_cur_size > mem_max_size)
|
|
|
|
mem_max_size = mem_cur_size;
|
|
|
|
|
|
|
|
header->size = size;
|
|
|
|
if (header->next)
|
|
|
|
header->next->prev = header;
|
|
|
|
|
|
|
|
if (header->prev)
|
|
|
|
header->prev->next = header;
|
|
|
|
|
|
|
|
if (mem_debug_chain_update)
|
|
|
|
mem_debug_chain = header;
|
|
|
|
|
|
|
|
ptr = (char *)ptr + sizeof(mem_debug_header_t);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
|
|
|
|
strcpy(ptr, str);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:07:09 +03:00
|
|
|
PUB_FUNC void tcc_memstats(int bench)
|
2015-05-12 11:56:39 +03:00
|
|
|
{
|
|
|
|
if (mem_cur_size) {
|
|
|
|
mem_debug_header_t *header = mem_debug_chain;
|
|
|
|
|
|
|
|
fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
|
|
|
|
mem_cur_size, mem_max_size);
|
|
|
|
|
|
|
|
while (header) {
|
|
|
|
fprintf(stderr, " file %s, line %u: %u bytes\n",
|
|
|
|
header->file_name, header->line_num, header->size);
|
|
|
|
header = header->next;
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 16:07:09 +03:00
|
|
|
else if (bench)
|
|
|
|
fprintf(stderr, "mem_max_size= %d bytes\n", mem_max_size);
|
2015-05-12 11:56:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef MEM_DEBUG_MAGIC1
|
|
|
|
#undef MEM_DEBUG_MAGIC2
|
|
|
|
#undef MEM_DEBUG_FILE_LEN
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#define free(p) use_tcc_free(p)
|
|
|
|
#define malloc(s) use_tcc_malloc(s)
|
|
|
|
#define realloc(p, s) use_tcc_realloc(p, s)
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/********************************************************/
|
|
|
|
/* dynarrays */
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
int nb, nb_alloc;
|
|
|
|
void **pp;
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
nb = *nb_ptr;
|
|
|
|
pp = *ptab;
|
|
|
|
/* every power of two we double array size */
|
|
|
|
if ((nb & (nb - 1)) == 0) {
|
|
|
|
if (!nb)
|
|
|
|
nb_alloc = 1;
|
|
|
|
else
|
|
|
|
nb_alloc = nb * 2;
|
|
|
|
pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
|
|
|
|
*ptab = pp;
|
|
|
|
}
|
|
|
|
pp[nb++] = data;
|
|
|
|
*nb_ptr = nb;
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
ST_FUNC void dynarray_reset(void *pp, int *n)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
void **p;
|
|
|
|
for (p = *(void***)pp; *n; ++p, --*n)
|
|
|
|
if (*p)
|
|
|
|
tcc_free(*p);
|
|
|
|
tcc_free(*(void**)pp);
|
|
|
|
*(void**)pp = NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-01 03:10:36 +04:00
|
|
|
static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
|
2011-07-08 12:51:26 +04:00
|
|
|
{
|
2011-08-01 03:10:36 +04:00
|
|
|
const char *p;
|
2011-07-08 12:51:26 +04:00
|
|
|
do {
|
2011-08-01 03:10:36 +04:00
|
|
|
int c;
|
|
|
|
CString str;
|
|
|
|
|
|
|
|
cstr_new(&str);
|
2011-08-06 18:11:12 +04:00
|
|
|
for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
|
2011-08-11 18:55:30 +04:00
|
|
|
if (c == '{' && p[1] && p[2] == '}') {
|
|
|
|
c = p[1], p += 2;
|
|
|
|
if (c == 'B')
|
|
|
|
cstr_cat(&str, s->tcc_lib_path);
|
2011-08-06 18:11:12 +04:00
|
|
|
} else {
|
|
|
|
cstr_ccat(&str, c);
|
2011-08-01 03:10:36 +04:00
|
|
|
}
|
2011-07-08 12:51:26 +04:00
|
|
|
}
|
2011-08-06 18:11:12 +04:00
|
|
|
cstr_ccat(&str, '\0');
|
2011-08-01 03:10:36 +04:00
|
|
|
dynarray_add(p_ary, p_nb_ary, str.data);
|
2011-08-06 18:11:12 +04:00
|
|
|
in = p+1;
|
|
|
|
} while (*p);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/********************************************************/
|
2009-05-05 22:18:53 +04:00
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
Section *sec;
|
|
|
|
|
2014-03-29 00:25:39 +04:00
|
|
|
sec = tcc_mallocz(sizeof(Section) + strlen(name));
|
2009-05-05 22:18:53 +04:00
|
|
|
strcpy(sec->name, name);
|
|
|
|
sec->sh_type = sh_type;
|
|
|
|
sec->sh_flags = sh_flags;
|
|
|
|
switch(sh_type) {
|
|
|
|
case SHT_HASH:
|
|
|
|
case SHT_REL:
|
|
|
|
case SHT_RELA:
|
|
|
|
case SHT_DYNSYM:
|
|
|
|
case SHT_SYMTAB:
|
|
|
|
case SHT_DYNAMIC:
|
|
|
|
sec->sh_addralign = 4;
|
|
|
|
break;
|
|
|
|
case SHT_STRTAB:
|
|
|
|
sec->sh_addralign = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sec->sh_addralign = 32; /* default conservative alignment */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sh_flags & SHF_PRIVATE) {
|
|
|
|
dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
|
|
|
|
} else {
|
|
|
|
sec->sh_num = s1->nb_sections;
|
|
|
|
dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_section(Section *s)
|
|
|
|
{
|
|
|
|
tcc_free(s->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* realloc section and set its content to zero */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
unsigned long size;
|
|
|
|
unsigned char *data;
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
size = sec->data_allocated;
|
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
|
|
|
while (size < new_size)
|
|
|
|
size = size * 2;
|
|
|
|
data = tcc_realloc(sec->data, size);
|
|
|
|
memset(data + sec->data_allocated, 0, size - sec->data_allocated);
|
|
|
|
sec->data = data;
|
|
|
|
sec->data_allocated = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reserve at least 'size' bytes in section 'sec' from
|
|
|
|
sec->data_offset. */
|
2015-03-26 11:28:11 +03:00
|
|
|
ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2015-03-26 07:47:45 +03:00
|
|
|
size_t offset, offset1;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
offset = sec->data_offset;
|
|
|
|
offset1 = offset + size;
|
|
|
|
if (offset1 > sec->data_allocated)
|
|
|
|
section_realloc(sec, offset1);
|
|
|
|
sec->data_offset = offset1;
|
|
|
|
return sec->data + offset;
|
|
|
|
}
|
|
|
|
|
2010-02-04 22:38:01 +03:00
|
|
|
/* reserve at least 'size' bytes from section start */
|
|
|
|
ST_FUNC void section_reserve(Section *sec, unsigned long size)
|
|
|
|
{
|
|
|
|
if (size > sec->data_allocated)
|
|
|
|
section_realloc(sec, size);
|
|
|
|
if (size > sec->data_offset)
|
|
|
|
sec->data_offset = size;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* return a reference to a section, and create it if it does not
|
|
|
|
exists */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC Section *find_section(TCCState *s1, const char *name)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
Section *sec;
|
|
|
|
int i;
|
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
sec = s1->sections[i];
|
2013-11-08 23:24:15 +04:00
|
|
|
if (!strcmp(name, sec->name))
|
2009-05-05 22:18:53 +04:00
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
/* sections are created as PROGBITS */
|
|
|
|
return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update sym->c so that it points to an external symbol in section
|
|
|
|
'section' with value 'value' */
|
2013-11-08 23:24:15 +04:00
|
|
|
ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t value, unsigned long size,
|
2009-05-05 22:18:53 +04:00
|
|
|
int can_add_underscore)
|
|
|
|
{
|
2010-01-14 22:55:51 +03:00
|
|
|
int sym_type, sym_bind, sh_num, info, other;
|
2009-05-05 22:18:53 +04:00
|
|
|
ElfW(Sym) *esym;
|
|
|
|
const char *name;
|
|
|
|
char buf1[256];
|
2015-04-12 04:47:15 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2015-04-11 16:22:34 +03:00
|
|
|
char buf[32];
|
2015-04-12 04:47:15 +03:00
|
|
|
#endif
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
if (section == NULL)
|
|
|
|
sh_num = SHN_UNDEF;
|
2013-11-08 23:24:15 +04:00
|
|
|
else if (section == SECTION_ABS)
|
2009-05-05 22:18:53 +04:00
|
|
|
sh_num = SHN_ABS;
|
|
|
|
else
|
|
|
|
sh_num = section->sh_num;
|
|
|
|
|
|
|
|
if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
|
|
|
|
sym_type = STT_FUNC;
|
2009-12-19 21:12:33 +03:00
|
|
|
} else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
|
|
|
|
sym_type = STT_NOTYPE;
|
2009-05-05 22:18:53 +04:00
|
|
|
} else {
|
2013-11-03 14:55:54 +04:00
|
|
|
sym_type = STT_OBJECT;
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sym->type.t & VT_STATIC)
|
|
|
|
sym_bind = STB_LOCAL;
|
2010-02-27 19:37:59 +03:00
|
|
|
else {
|
2011-02-01 11:37:53 +03:00
|
|
|
if (sym->type.t & VT_WEAK)
|
2010-02-27 19:37:59 +03:00
|
|
|
sym_bind = STB_WEAK;
|
|
|
|
else
|
|
|
|
sym_bind = STB_GLOBAL;
|
|
|
|
}
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
if (!sym->c) {
|
2011-02-08 00:42:38 +03:00
|
|
|
name = get_tok_str(sym->v, NULL);
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-11 20:45:44 +04:00
|
|
|
if (tcc_state->do_bounds_check) {
|
2009-05-05 22:18:53 +04:00
|
|
|
/* XXX: avoid doing that for statics ? */
|
|
|
|
/* if bound checking is activated, we change some function
|
|
|
|
names by adding the "__bound" prefix */
|
|
|
|
switch(sym->v) {
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef TCC_TARGET_PE
|
2009-05-05 22:18:53 +04:00
|
|
|
/* XXX: we rely only on malloc hooks */
|
2013-11-08 23:24:15 +04:00
|
|
|
case TOK_malloc:
|
|
|
|
case TOK_free:
|
|
|
|
case TOK_realloc:
|
|
|
|
case TOK_memalign:
|
|
|
|
case TOK_calloc:
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2013-11-08 23:24:15 +04:00
|
|
|
case TOK_memcpy:
|
2009-05-05 22:18:53 +04:00
|
|
|
case TOK_memmove:
|
|
|
|
case TOK_memset:
|
|
|
|
case TOK_strlen:
|
|
|
|
case TOK_strcpy:
|
2009-05-17 00:30:13 +04:00
|
|
|
case TOK_alloca:
|
2009-05-05 22:18:53 +04:00
|
|
|
strcpy(buf, "__bound_");
|
|
|
|
strcat(buf, name);
|
|
|
|
name = buf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-14 22:55:51 +03:00
|
|
|
other = 0;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
#ifdef TCC_TARGET_PE
|
2010-01-14 22:55:51 +03:00
|
|
|
if (sym->type.t & VT_EXPORT)
|
2014-04-17 19:01:28 +04:00
|
|
|
other |= ST_PE_EXPORT;
|
2010-01-14 22:55:51 +03:00
|
|
|
if (sym_type == STT_FUNC && sym->type.ref) {
|
2014-01-07 17:57:07 +04:00
|
|
|
Sym *ref = sym->type.ref;
|
|
|
|
if (ref->a.func_export)
|
2014-04-17 19:01:28 +04:00
|
|
|
other |= ST_PE_EXPORT;
|
2014-01-07 17:57:07 +04:00
|
|
|
if (ref->a.func_call == FUNC_STDCALL && can_add_underscore) {
|
|
|
|
sprintf(buf1, "_%s@%d", name, ref->a.func_args * PTR_SIZE);
|
2010-01-14 22:55:51 +03:00
|
|
|
name = buf1;
|
2014-04-17 19:01:28 +04:00
|
|
|
other |= ST_PE_STDCALL;
|
2010-01-14 22:55:51 +03:00
|
|
|
can_add_underscore = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (find_elf_sym(tcc_state->dynsymtab_section, name))
|
2014-04-17 19:01:28 +04:00
|
|
|
other |= ST_PE_IMPORT;
|
2010-01-14 22:55:51 +03:00
|
|
|
if (sym->type.t & VT_IMPORT)
|
2014-04-17 19:01:28 +04:00
|
|
|
other |= ST_PE_IMPORT;
|
2010-01-14 22:55:51 +03:00
|
|
|
}
|
2014-04-14 05:33:50 +04:00
|
|
|
#else
|
|
|
|
if (! (sym->type.t & VT_STATIC))
|
2015-07-29 23:53:57 +03:00
|
|
|
other = (sym->type.t & VT_VIS_MASK) >> VT_VIS_SHIFT;
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
|
|
|
if (tcc_state->leading_underscore && can_add_underscore) {
|
|
|
|
buf1[0] = '_';
|
|
|
|
pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
|
|
|
|
name = buf1;
|
|
|
|
}
|
2011-02-08 00:42:38 +03:00
|
|
|
if (sym->asm_label) {
|
|
|
|
name = sym->asm_label;
|
|
|
|
}
|
2009-05-05 22:18:53 +04:00
|
|
|
info = ELFW(ST_INFO)(sym_bind, sym_type);
|
|
|
|
sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
|
|
|
|
} else {
|
|
|
|
esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
|
|
|
|
esym->st_value = value;
|
|
|
|
esym->st_size = size;
|
|
|
|
esym->st_shndx = sh_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 23:24:15 +04:00
|
|
|
ST_FUNC void put_extern_sym(Sym *sym, Section *section,
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t value, unsigned long size)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
put_extern_sym2(sym, section, value, size, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a new relocation entry to symbol 'sym' in section 's' */
|
2015-02-22 00:29:03 +03:00
|
|
|
ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
|
2015-03-23 19:24:55 +03:00
|
|
|
addr_t addend)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2009-12-20 00:08:37 +03:00
|
|
|
int c = 0;
|
|
|
|
if (sym) {
|
|
|
|
if (0 == sym->c)
|
|
|
|
put_extern_sym(sym, NULL, 0, 0);
|
|
|
|
c = sym->c;
|
|
|
|
}
|
2009-05-05 22:18:53 +04:00
|
|
|
/* now we can add ELF relocation info */
|
2015-02-22 00:29:03 +03:00
|
|
|
put_elf_reloca(symtab_section, s, offset, type, c, addend);
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
|
|
|
|
{
|
|
|
|
greloca(s, sym, offset, type, 0);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/********************************************************/
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
len = strlen(buf);
|
|
|
|
vsnprintf(buf + len, buf_size - len, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
strcat_vprintf(buf, buf_size, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
char buf[2048];
|
2013-02-14 09:53:07 +04:00
|
|
|
BufferedFile **pf, *f;
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
buf[0] = '\0';
|
2013-02-14 09:53:07 +04:00
|
|
|
/* use upper file if inline ":asm:" or token ":paste:" */
|
2013-02-18 18:44:18 +04:00
|
|
|
for (f = file; f && f->filename[0] == ':'; f = f->prev)
|
|
|
|
;
|
2013-02-14 09:53:07 +04:00
|
|
|
if (f) {
|
|
|
|
for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
|
|
|
|
strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
|
|
|
|
(*pf)->filename, (*pf)->line_num);
|
|
|
|
if (f->line_num > 0) {
|
|
|
|
strcat_printf(buf, sizeof(buf), "%s:%d: ",
|
|
|
|
f->filename, f->line_num);
|
2009-05-05 22:18:53 +04:00
|
|
|
} else {
|
2013-02-14 09:53:07 +04:00
|
|
|
strcat_printf(buf, sizeof(buf), "%s: ",
|
|
|
|
f->filename);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
} else {
|
2013-02-14 09:53:07 +04:00
|
|
|
strcat_printf(buf, sizeof(buf), "tcc: ");
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
if (is_warning)
|
|
|
|
strcat_printf(buf, sizeof(buf), "warning: ");
|
2009-06-17 04:10:56 +04:00
|
|
|
else
|
|
|
|
strcat_printf(buf, sizeof(buf), "error: ");
|
2009-05-05 22:18:53 +04:00
|
|
|
strcat_vprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
|
|
|
|
if (!s1->error_func) {
|
|
|
|
/* default case: stderr */
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
if (s1->ppfp) /* print a newline during tcc -E */
|
|
|
|
fprintf(s1->ppfp, "\n"), fflush(s1->ppfp);
|
2009-05-05 22:18:53 +04:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
fflush(stderr); /* print error/warning now (win32) */
|
2009-05-05 22:18:53 +04:00
|
|
|
} else {
|
|
|
|
s1->error_func(s1->error_opaque, buf);
|
|
|
|
}
|
|
|
|
if (!is_warning || s1->warn_error)
|
|
|
|
s1->nb_errors++;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
|
2009-05-05 22:18:53 +04:00
|
|
|
void (*error_func)(void *opaque, const char *msg))
|
|
|
|
{
|
|
|
|
s->error_opaque = error_opaque;
|
|
|
|
s->error_func = error_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* error without aborting current compilation */
|
2011-08-11 19:07:56 +04:00
|
|
|
PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error1(s1, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2011-08-11 19:07:56 +04:00
|
|
|
PUB_FUNC void tcc_error(const char *fmt, ...)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error1(s1, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
/* better than nothing: in some cases, we accept to handle errors */
|
|
|
|
if (s1->error_set_jmp_enabled) {
|
|
|
|
longjmp(s1->error_jmp_buf, 1);
|
|
|
|
} else {
|
|
|
|
/* XXX: eliminate this someday */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 19:07:56 +04:00
|
|
|
PUB_FUNC void tcc_warning(const char *fmt, ...)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (s1->warn_none)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error1(s1, 1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
/********************************************************/
|
2009-05-05 22:18:53 +04:00
|
|
|
/* I/O layer */
|
|
|
|
|
2010-11-25 15:29:15 +03:00
|
|
|
ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
BufferedFile *bf;
|
2010-11-25 15:29:15 +03:00
|
|
|
int buflen = initlen ? initlen : IO_BUF_SIZE;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
|
2009-05-05 22:18:53 +04:00
|
|
|
bf->buf_ptr = bf->buffer;
|
2010-11-25 15:29:15 +03:00
|
|
|
bf->buf_end = bf->buffer + initlen;
|
|
|
|
bf->buf_end[0] = CH_EOB; /* put eob symbol */
|
2009-05-05 22:18:53 +04:00
|
|
|
pstrcpy(bf->filename, sizeof(bf->filename), filename);
|
|
|
|
#ifdef _WIN32
|
|
|
|
normalize_slashes(bf->filename);
|
|
|
|
#endif
|
|
|
|
bf->line_num = 1;
|
|
|
|
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
2013-01-14 20:34:07 +04:00
|
|
|
bf->fd = -1;
|
2010-11-25 15:29:15 +03:00
|
|
|
bf->prev = file;
|
|
|
|
file = bf;
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
2010-11-25 15:29:15 +03:00
|
|
|
ST_FUNC void tcc_close(void)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2010-11-25 15:29:15 +03:00
|
|
|
BufferedFile *bf = file;
|
2013-01-14 20:34:07 +04:00
|
|
|
if (bf->fd > 0) {
|
|
|
|
close(bf->fd);
|
2010-11-25 15:29:15 +03:00
|
|
|
total_lines += bf->line_num;
|
|
|
|
}
|
|
|
|
file = bf->prev;
|
2009-05-05 22:18:53 +04:00
|
|
|
tcc_free(bf);
|
|
|
|
}
|
|
|
|
|
2013-01-14 20:34:07 +04:00
|
|
|
ST_FUNC int tcc_open(TCCState *s1, const char *filename)
|
2010-11-25 15:29:15 +03:00
|
|
|
{
|
2013-01-14 20:34:07 +04:00
|
|
|
int fd;
|
|
|
|
if (strcmp(filename, "-") == 0)
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
fd = 0, filename = "<stdin>";
|
2010-11-25 15:29:15 +03:00
|
|
|
else
|
2013-01-14 20:34:07 +04:00
|
|
|
fd = open(filename, O_RDONLY | O_BINARY);
|
|
|
|
if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
|
|
|
|
printf("%s %*s%s\n", fd < 0 ? "nf":"->",
|
2010-11-25 15:29:15 +03:00
|
|
|
(int)(s1->include_stack_ptr - s1->include_stack), "", filename);
|
2013-01-14 20:34:07 +04:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
2010-11-25 15:29:15 +03:00
|
|
|
|
|
|
|
tcc_open_bf(s1, filename, 0);
|
|
|
|
file->fd = fd;
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* compile the C file opened in 'file'. Return non zero if errors. */
|
|
|
|
static int tcc_compile(TCCState *s1)
|
|
|
|
{
|
|
|
|
Sym *define_start;
|
|
|
|
char buf[512];
|
|
|
|
volatile int section_sym;
|
|
|
|
|
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("%s: **** new file\n", file->filename);
|
|
|
|
#endif
|
|
|
|
preprocess_init(s1);
|
|
|
|
|
|
|
|
cur_text_section = NULL;
|
|
|
|
funcname = "";
|
2013-11-08 23:24:15 +04:00
|
|
|
anon_sym = SYM_FIRST_ANOM;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
/* file info: full path + filename */
|
|
|
|
section_sym = 0; /* avoid warning */
|
2009-05-11 20:45:44 +04:00
|
|
|
if (s1->do_debug) {
|
2013-11-08 23:24:15 +04:00
|
|
|
section_sym = put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
|
2009-05-05 22:18:53 +04:00
|
|
|
text_section->sh_num, NULL);
|
|
|
|
getcwd(buf, sizeof(buf));
|
|
|
|
#ifdef _WIN32
|
|
|
|
normalize_slashes(buf);
|
|
|
|
#endif
|
|
|
|
pstrcat(buf, sizeof(buf), "/");
|
2013-11-08 23:24:15 +04:00
|
|
|
put_stabs_r(buf, N_SO, 0, 0,
|
2009-05-05 22:18:53 +04:00
|
|
|
text_section->data_offset, text_section, section_sym);
|
2013-11-08 23:24:15 +04:00
|
|
|
put_stabs_r(file->filename, N_SO, 0, 0,
|
2009-05-05 22:18:53 +04:00
|
|
|
text_section->data_offset, text_section, section_sym);
|
|
|
|
}
|
|
|
|
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
|
|
|
|
symbols can be safely used */
|
2013-11-08 23:24:15 +04:00
|
|
|
put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
|
2009-05-05 22:18:53 +04:00
|
|
|
SHN_ABS, file->filename);
|
|
|
|
|
|
|
|
/* define some often used types */
|
|
|
|
int_type.t = VT_INT;
|
|
|
|
|
|
|
|
char_pointer_type.t = VT_BYTE;
|
|
|
|
mk_pointer(&char_pointer_type);
|
|
|
|
|
2012-04-16 03:13:25 +04:00
|
|
|
#if PTR_SIZE == 4
|
|
|
|
size_type.t = VT_INT;
|
|
|
|
#else
|
|
|
|
size_type.t = VT_LLONG;
|
|
|
|
#endif
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
func_old_type.t = VT_FUNC;
|
|
|
|
func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
|
2013-02-08 22:07:11 +04:00
|
|
|
#ifdef TCC_TARGET_ARM
|
2014-01-07 11:23:54 +04:00
|
|
|
arm_init(s1);
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* define 'void *alloca(unsigned int)' builtin function */
|
|
|
|
{
|
|
|
|
Sym *s1;
|
|
|
|
|
|
|
|
p = anon_sym++;
|
|
|
|
sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
|
|
|
|
s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
|
|
|
|
s1->next = NULL;
|
|
|
|
sym->next = s1;
|
|
|
|
sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
define_start = define_stack;
|
2015-03-03 15:44:29 +03:00
|
|
|
nocode_wanted = 1;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
if (setjmp(s1->error_jmp_buf) == 0) {
|
|
|
|
s1->nb_errors = 0;
|
|
|
|
s1->error_set_jmp_enabled = 1;
|
|
|
|
|
|
|
|
ch = file->buf_ptr[0];
|
|
|
|
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
|
2009-05-05 22:18:53 +04:00
|
|
|
next();
|
|
|
|
decl(VT_CONST);
|
|
|
|
if (tok != TOK_EOF)
|
|
|
|
expect("declaration");
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
check_vstack();
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
/* end of translation unit info */
|
2009-05-11 20:45:44 +04:00
|
|
|
if (s1->do_debug) {
|
2013-11-08 23:24:15 +04:00
|
|
|
put_stabs_r(NULL, N_SO, 0, 0,
|
2009-05-05 22:18:53 +04:00
|
|
|
text_section->data_offset, text_section, section_sym);
|
|
|
|
}
|
|
|
|
}
|
2011-04-14 12:02:42 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
s1->error_set_jmp_enabled = 0;
|
|
|
|
|
|
|
|
/* reset define stack, but leave -Dsymbols (may be incorrect if
|
|
|
|
they are undefined) */
|
2013-11-08 23:24:15 +04:00
|
|
|
free_defines(define_start);
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
gen_inline_functions();
|
|
|
|
|
|
|
|
sym_pop(&global_stack, NULL);
|
|
|
|
sym_pop(&local_stack, NULL);
|
|
|
|
|
|
|
|
return s1->nb_errors != 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-01-14 20:34:07 +04:00
|
|
|
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2010-11-25 15:29:15 +03:00
|
|
|
int len, ret;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
len = strlen(str);
|
2013-01-14 20:34:07 +04:00
|
|
|
tcc_open_bf(s, "<string>", len);
|
2010-11-25 15:29:15 +03:00
|
|
|
memcpy(file->buffer, str, len);
|
|
|
|
ret = tcc_compile(s);
|
|
|
|
tcc_close();
|
2009-05-05 22:18:53 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* define a preprocessor symbol. A value can also be provided with the '=' operator */
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2010-11-25 15:29:15 +03:00
|
|
|
int len1, len2;
|
2009-05-05 22:18:53 +04:00
|
|
|
/* default value */
|
2010-11-25 15:29:15 +03:00
|
|
|
if (!value)
|
2009-05-05 22:18:53 +04:00
|
|
|
value = "1";
|
2010-11-25 15:29:15 +03:00
|
|
|
len1 = strlen(sym);
|
|
|
|
len2 = strlen(value);
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* init file structure */
|
2010-11-25 15:29:15 +03:00
|
|
|
tcc_open_bf(s1, "<define>", len1 + len2 + 1);
|
|
|
|
memcpy(file->buffer, sym, len1);
|
|
|
|
file->buffer[len1] = ' ';
|
|
|
|
memcpy(file->buffer + len1 + 1, value, len2);
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
/* parse with define parser */
|
|
|
|
ch = file->buf_ptr[0];
|
|
|
|
next_nomacro();
|
|
|
|
parse_define();
|
2010-11-25 15:29:15 +03:00
|
|
|
|
|
|
|
tcc_close();
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* undefine a preprocessor symbol */
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
TokenSym *ts;
|
|
|
|
Sym *s;
|
|
|
|
ts = tok_alloc(sym, strlen(sym));
|
|
|
|
s = define_find(ts->tok);
|
|
|
|
/* undefine symbol by putting an invalid name */
|
|
|
|
if (s)
|
|
|
|
define_undef(s);
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* cleanup all static data used during compilation */
|
2009-05-05 22:18:53 +04:00
|
|
|
static void tcc_cleanup(void)
|
|
|
|
{
|
|
|
|
if (NULL == tcc_state)
|
|
|
|
return;
|
|
|
|
tcc_state = NULL;
|
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
preprocess_delete();
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
/* free sym_pools */
|
|
|
|
dynarray_reset(&sym_pools, &nb_sym_pools);
|
|
|
|
/* string buffer */
|
|
|
|
cstr_free(&tokcstr);
|
|
|
|
/* reset symbol stack */
|
|
|
|
sym_free_first = NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI TCCState *tcc_new(void)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
TCCState *s;
|
2009-05-25 04:07:30 +04:00
|
|
|
char buffer[100];
|
|
|
|
int a,b,c;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
tcc_cleanup();
|
|
|
|
|
|
|
|
s = tcc_mallocz(sizeof(TCCState));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
tcc_state = s;
|
2009-07-19 00:07:33 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
tcc_set_lib_path_w32(s);
|
|
|
|
#else
|
|
|
|
tcc_set_lib_path(s, CONFIG_TCCDIR);
|
|
|
|
#endif
|
2015-01-06 22:19:45 +03:00
|
|
|
s->output_type = 0;
|
2009-05-05 22:30:39 +04:00
|
|
|
preprocess_new();
|
2010-11-26 22:46:25 +03:00
|
|
|
s->include_stack_ptr = s->include_stack;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
/* we add dummy defines for some special macros to speed up tests
|
|
|
|
and to have working defined() */
|
|
|
|
define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
|
|
|
|
define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
|
|
|
|
define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
|
|
|
|
define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* define __TINYC__ 92X */
|
|
|
|
sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
|
|
|
|
sprintf(buffer, "%d", a*10000 + b*100 + c);
|
|
|
|
tcc_define_symbol(s, "__TINYC__", buffer);
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* standard defines */
|
|
|
|
tcc_define_symbol(s, "__STDC__", NULL);
|
|
|
|
tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
|
2013-02-18 18:44:18 +04:00
|
|
|
tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
|
|
|
|
/* target defines */
|
2009-05-05 22:18:53 +04:00
|
|
|
#if defined(TCC_TARGET_I386)
|
2010-01-14 22:56:35 +03:00
|
|
|
tcc_define_symbol(s, "__i386__", NULL);
|
|
|
|
tcc_define_symbol(s, "__i386", NULL);
|
|
|
|
tcc_define_symbol(s, "i386", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
#elif defined(TCC_TARGET_X86_64)
|
2009-05-05 22:18:53 +04:00
|
|
|
tcc_define_symbol(s, "__x86_64__", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
#elif defined(TCC_TARGET_ARM)
|
2009-05-05 22:18:53 +04:00
|
|
|
tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
|
|
|
|
tcc_define_symbol(s, "__arm_elf__", NULL);
|
|
|
|
tcc_define_symbol(s, "__arm_elf", NULL);
|
|
|
|
tcc_define_symbol(s, "arm_elf", NULL);
|
|
|
|
tcc_define_symbol(s, "__arm__", NULL);
|
|
|
|
tcc_define_symbol(s, "__arm", NULL);
|
|
|
|
tcc_define_symbol(s, "arm", NULL);
|
|
|
|
tcc_define_symbol(s, "__APCS_32__", NULL);
|
2013-11-26 08:06:21 +04:00
|
|
|
tcc_define_symbol(s, "__ARMEL__", NULL);
|
|
|
|
#if defined(TCC_ARM_EABI)
|
|
|
|
tcc_define_symbol(s, "__ARM_EABI__", NULL);
|
2014-01-07 11:23:54 +04:00
|
|
|
#endif
|
2013-06-11 20:13:04 +04:00
|
|
|
#if defined(TCC_ARM_HARDFLOAT)
|
2014-01-07 11:23:54 +04:00
|
|
|
s->float_abi = ARM_HARD_FLOAT;
|
2013-06-11 20:13:04 +04:00
|
|
|
tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
|
2014-01-07 11:23:54 +04:00
|
|
|
#else
|
|
|
|
s->float_abi = ARM_SOFTFP_FLOAT;
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2015-02-13 21:58:31 +03:00
|
|
|
#elif defined(TCC_TARGET_ARM64)
|
|
|
|
tcc_define_symbol(s, "__aarch64__", NULL);
|
2013-11-26 08:06:21 +04:00
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
tcc_define_symbol(s, "_WIN32", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
# ifdef TCC_TARGET_X86_64
|
2009-07-19 00:05:27 +04:00
|
|
|
tcc_define_symbol(s, "_WIN64", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
# endif
|
2009-05-05 22:18:53 +04:00
|
|
|
#else
|
2010-01-14 22:56:35 +03:00
|
|
|
tcc_define_symbol(s, "__unix__", NULL);
|
|
|
|
tcc_define_symbol(s, "__unix", NULL);
|
|
|
|
tcc_define_symbol(s, "unix", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
# if defined(__linux)
|
2009-05-05 22:18:53 +04:00
|
|
|
tcc_define_symbol(s, "__linux__", NULL);
|
|
|
|
tcc_define_symbol(s, "__linux", NULL);
|
2013-02-12 22:13:28 +04:00
|
|
|
# endif
|
|
|
|
# if defined(__FreeBSD__)
|
|
|
|
# define str(s) #s
|
|
|
|
tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
|
|
|
|
# undef str
|
|
|
|
# endif
|
|
|
|
# if defined(__FreeBSD_kernel__)
|
|
|
|
tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
|
|
|
|
# endif
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2015-10-11 04:12:35 +03:00
|
|
|
# if defined(__NetBSD__)
|
|
|
|
# define str(s) #s
|
|
|
|
tcc_define_symbol(s, "__NetBSD__", str( __NetBSD__));
|
|
|
|
# undef str
|
|
|
|
# endif
|
2009-05-05 22:18:53 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* TinyCC & gcc defines */
|
2010-01-14 22:56:35 +03:00
|
|
|
#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
|
|
|
|
tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
|
|
|
|
tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
|
|
|
|
#else
|
2009-07-18 23:26:19 +04:00
|
|
|
tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
|
|
|
|
tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
|
2010-01-14 22:56:35 +03:00
|
|
|
#endif
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
|
2014-04-07 02:26:36 +04:00
|
|
|
tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
|
2009-05-05 22:18:53 +04:00
|
|
|
#else
|
|
|
|
tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
|
2014-04-07 02:26:36 +04:00
|
|
|
/* wint_t is unsigned int by default, but (signed) int on BSDs
|
|
|
|
and unsigned short on windows. Other OSes might have still
|
|
|
|
other conventions, sigh. */
|
2015-10-11 12:58:04 +03:00
|
|
|
#if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__)
|
2014-04-07 02:26:36 +04:00
|
|
|
tcc_define_symbol(s, "__WINT_TYPE__", "int");
|
|
|
|
#else
|
|
|
|
tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
|
|
|
|
#endif
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2010-09-11 19:57:15 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef TCC_TARGET_PE
|
2010-09-11 19:57:15 +04:00
|
|
|
/* glibc defines */
|
2012-02-13 02:27:26 +04:00
|
|
|
tcc_define_symbol(s, "__REDIRECT(name, proto, alias)", "name proto __asm__ (#alias)");
|
2010-09-11 19:57:15 +04:00
|
|
|
tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", "name proto __asm__ (#alias) __THROW");
|
2011-08-06 18:49:30 +04:00
|
|
|
/* paths for crt objects */
|
|
|
|
tcc_split_path(s, (void ***)&s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* no section zero */
|
|
|
|
dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
|
|
|
|
|
|
|
|
/* create standard sections */
|
|
|
|
text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
|
|
|
|
data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
|
|
|
|
bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
|
|
|
|
|
|
|
|
/* symbols are always generated for linking stage */
|
|
|
|
symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
|
|
|
|
".strtab",
|
2013-11-08 23:24:15 +04:00
|
|
|
".hashtab", SHF_PRIVATE);
|
2009-05-05 22:18:53 +04:00
|
|
|
strtab_section = symtab_section->link;
|
2012-04-18 20:48:26 +04:00
|
|
|
s->symtab = symtab_section;
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* private symbol table for dynamic symbols */
|
|
|
|
s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
|
2013-11-08 23:24:15 +04:00
|
|
|
".dynstrtab",
|
2009-05-05 22:18:53 +04:00
|
|
|
".dynhashtab", SHF_PRIVATE);
|
|
|
|
s->alacarte_link = 1;
|
2009-12-20 03:53:49 +03:00
|
|
|
s->nocommon = 1;
|
2015-03-03 16:32:25 +03:00
|
|
|
s->warn_implicit_function_declaration = 1;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
#ifdef CHAR_IS_UNSIGNED
|
|
|
|
s->char_is_unsigned = 1;
|
|
|
|
#endif
|
2011-02-13 19:41:05 +03:00
|
|
|
/* enable this if you want symbols with leading underscore on windows: */
|
2013-02-17 03:48:51 +04:00
|
|
|
#if 0 /* def TCC_TARGET_PE */
|
2009-05-05 22:18:53 +04:00
|
|
|
s->leading_underscore = 1;
|
2009-08-27 11:34:35 +04:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
s->seg_size = 32;
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2013-11-08 23:24:15 +04:00
|
|
|
#ifdef TCC_IS_NATIVE
|
2013-08-29 00:55:05 +04:00
|
|
|
s->runtime_main = "main";
|
2013-11-08 23:24:15 +04:00
|
|
|
#endif
|
2009-05-05 22:18:53 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI void tcc_delete(TCCState *s1)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
int i;
|
2015-05-12 16:07:09 +03:00
|
|
|
int bench = s1->do_bench;
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
tcc_cleanup();
|
|
|
|
|
2015-05-12 21:32:32 +03:00
|
|
|
/* close a preprocessor output */
|
|
|
|
if (s1->ppfp && s1->ppfp != stdout)
|
|
|
|
fclose(s1->ppfp);
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* free all sections */
|
|
|
|
for(i = 1; i < s1->nb_sections; i++)
|
|
|
|
free_section(s1->sections[i]);
|
|
|
|
dynarray_reset(&s1->sections, &s1->nb_sections);
|
|
|
|
|
|
|
|
for(i = 0; i < s1->nb_priv_sections; i++)
|
|
|
|
free_section(s1->priv_sections[i]);
|
|
|
|
dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* free any loaded DLLs */
|
2013-04-26 01:30:53 +04:00
|
|
|
#ifdef TCC_IS_NATIVE
|
2009-05-05 22:18:53 +04:00
|
|
|
for ( i = 0; i < s1->nb_loaded_dlls; i++) {
|
|
|
|
DLLReference *ref = s1->loaded_dlls[i];
|
|
|
|
if ( ref->handle )
|
|
|
|
dlclose(ref->handle);
|
|
|
|
}
|
2013-04-26 01:30:53 +04:00
|
|
|
#endif
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* free loaded dlls array */
|
|
|
|
dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
|
|
|
|
|
|
|
|
/* free library paths */
|
|
|
|
dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
|
2011-08-06 18:49:30 +04:00
|
|
|
dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
|
2009-05-05 22:18:53 +04:00
|
|
|
|
|
|
|
/* free include paths */
|
|
|
|
dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
|
|
|
|
dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
|
|
|
|
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
|
|
|
|
|
2011-08-06 18:08:46 +04:00
|
|
|
tcc_free(s1->tcc_lib_path);
|
2013-02-12 22:13:28 +04:00
|
|
|
tcc_free(s1->soname);
|
|
|
|
tcc_free(s1->rpath);
|
|
|
|
tcc_free(s1->init_symbol);
|
|
|
|
tcc_free(s1->fini_symbol);
|
|
|
|
tcc_free(s1->outfile);
|
|
|
|
tcc_free(s1->deps_outfile);
|
|
|
|
dynarray_reset(&s1->files, &s1->nb_files);
|
|
|
|
dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
|
2011-08-06 18:08:46 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifdef TCC_IS_NATIVE
|
|
|
|
# ifdef HAVE_SELINUX
|
2010-04-24 14:28:54 +04:00
|
|
|
munmap (s1->write_mem, s1->mem_size);
|
2013-11-08 23:24:15 +04:00
|
|
|
munmap (s1->runtime_mem, s1->mem_size);
|
2013-02-12 22:13:28 +04:00
|
|
|
# else
|
2009-12-20 00:11:12 +03:00
|
|
|
tcc_free(s1->runtime_mem);
|
2013-02-12 22:13:28 +04:00
|
|
|
# endif
|
2010-04-24 14:28:54 +04:00
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
tcc_free(s1->sym_attrs);
|
2009-05-05 22:18:53 +04:00
|
|
|
tcc_free(s1);
|
2015-05-12 16:07:09 +03:00
|
|
|
tcc_memstats(bench);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
2011-08-01 03:10:36 +04:00
|
|
|
LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2011-08-01 03:10:36 +04:00
|
|
|
tcc_split_path(s, (void ***)&s->include_paths, &s->nb_include_paths, pathname);
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-01 03:10:36 +04:00
|
|
|
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2011-08-01 03:10:36 +04:00
|
|
|
tcc_split_path(s, (void ***)&s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-12 15:35:37 +03:00
|
|
|
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
ElfW(Ehdr) ehdr;
|
2013-01-14 20:34:07 +04:00
|
|
|
int fd, ret, size;
|
2009-07-19 00:07:25 +04:00
|
|
|
|
2015-04-10 16:53:29 +03:00
|
|
|
parse_flags = 0;
|
2011-02-02 02:26:21 +03:00
|
|
|
#ifdef CONFIG_TCC_ASM
|
|
|
|
/* if .S file, define __ASSEMBLER__ like gcc does */
|
2015-04-12 15:35:37 +03:00
|
|
|
if ((filetype == TCC_FILETYPE_ASM) || (filetype == TCC_FILETYPE_ASM_PP)) {
|
2011-02-02 02:26:21 +03:00
|
|
|
tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
|
2015-04-27 16:36:58 +03:00
|
|
|
parse_flags = PARSE_FLAG_ASM_FILE;
|
2015-04-10 16:53:29 +03:00
|
|
|
}
|
2011-02-02 02:26:21 +03:00
|
|
|
#endif
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* open the file */
|
2013-01-14 20:34:07 +04:00
|
|
|
ret = tcc_open(s1, filename);
|
|
|
|
if (ret < 0) {
|
2009-07-19 00:07:25 +04:00
|
|
|
if (flags & AFF_PRINT_ERROR)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error_noabort("file '%s' not found", filename);
|
2013-01-14 20:34:07 +04:00
|
|
|
return ret;
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
/* update target deps */
|
|
|
|
dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
|
|
|
|
tcc_strdup(filename));
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
if (flags & AFF_PREPROCESS) {
|
|
|
|
ret = tcc_preprocess(s1);
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2015-04-12 15:35:37 +03:00
|
|
|
if (filetype == TCC_FILETYPE_C) {
|
2009-05-05 22:18:53 +04:00
|
|
|
/* C file assumed */
|
|
|
|
ret = tcc_compile(s1);
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef CONFIG_TCC_ASM
|
2015-04-12 15:35:37 +03:00
|
|
|
if (filetype == TCC_FILETYPE_ASM_PP) {
|
2015-04-10 16:31:12 +03:00
|
|
|
/* non preprocessed assembler */
|
2009-05-05 22:18:53 +04:00
|
|
|
ret = tcc_assemble(s1, 1);
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2015-04-12 15:35:37 +03:00
|
|
|
if (filetype == TCC_FILETYPE_ASM) {
|
2015-04-10 16:31:12 +03:00
|
|
|
/* preprocessed assembler */
|
2009-05-05 22:18:53 +04:00
|
|
|
ret = tcc_assemble(s1, 0);
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
|
|
|
|
2009-07-19 00:07:25 +04:00
|
|
|
fd = file->fd;
|
|
|
|
/* assume executable format: auto guess file type */
|
2013-01-14 20:34:07 +04:00
|
|
|
size = read(fd, &ehdr, sizeof(ehdr));
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2009-07-19 00:07:25 +04:00
|
|
|
if (size <= 0) {
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error_noabort("could not read header");
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == sizeof(ehdr) &&
|
|
|
|
ehdr.e_ident[0] == ELFMAG0 &&
|
|
|
|
ehdr.e_ident[1] == ELFMAG1 &&
|
|
|
|
ehdr.e_ident[2] == ELFMAG2 &&
|
|
|
|
ehdr.e_ident[3] == ELFMAG3) {
|
|
|
|
|
|
|
|
/* do not display line number if error */
|
|
|
|
file->line_num = 0;
|
|
|
|
if (ehdr.e_type == ET_REL) {
|
|
|
|
ret = tcc_load_object_file(s1, fd, 0);
|
|
|
|
goto the_end;
|
|
|
|
|
|
|
|
}
|
|
|
|
#ifndef TCC_TARGET_PE
|
|
|
|
if (ehdr.e_type == ET_DYN) {
|
|
|
|
if (s1->output_type == TCC_OUTPUT_MEMORY) {
|
2013-02-06 22:01:07 +04:00
|
|
|
#ifdef TCC_IS_NATIVE
|
2009-07-19 00:07:25 +04:00
|
|
|
void *h;
|
|
|
|
h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
|
|
|
|
if (h)
|
2013-02-06 22:01:07 +04:00
|
|
|
#endif
|
2009-07-19 00:07:25 +04:00
|
|
|
ret = 0;
|
2009-05-05 22:18:53 +04:00
|
|
|
} else {
|
2013-11-08 23:24:15 +04:00
|
|
|
ret = tcc_load_dll(s1, fd, filename,
|
2009-07-19 00:07:25 +04:00
|
|
|
(flags & AFF_REFERENCED_DLL) != 0);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error_noabort("unrecognized ELF file");
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
|
|
|
|
file->line_num = 0; /* do not display line number if error */
|
|
|
|
ret = tcc_load_archive(s1, fd);
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef TCC_TARGET_COFF
|
2009-07-19 00:07:25 +04:00
|
|
|
if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
|
|
|
|
ret = tcc_load_coff(s1, fd);
|
|
|
|
goto the_end;
|
|
|
|
}
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2009-07-19 00:07:25 +04:00
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
2009-07-19 00:07:25 +04:00
|
|
|
ret = pe_load_file(s1, filename, fd);
|
|
|
|
#else
|
|
|
|
/* as GNU ld, consider it is an ld script if not recognized */
|
|
|
|
ret = tcc_load_ldscript(s1);
|
2009-05-05 22:18:53 +04:00
|
|
|
#endif
|
2009-07-19 00:07:25 +04:00
|
|
|
if (ret < 0)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error_noabort("unrecognized file type");
|
2009-07-19 00:07:25 +04:00
|
|
|
|
|
|
|
the_end:
|
2010-11-25 15:29:15 +03:00
|
|
|
tcc_close();
|
2009-05-05 22:18:53 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-12 15:35:37 +03:00
|
|
|
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2009-05-11 20:46:02 +04:00
|
|
|
if (s->output_type == TCC_OUTPUT_PREPROCESS)
|
2015-04-12 15:35:37 +03:00
|
|
|
return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS, filetype);
|
2009-05-11 20:46:02 +04:00
|
|
|
else
|
2015-04-12 15:35:37 +03:00
|
|
|
return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR, filetype);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2011-08-01 03:10:36 +04:00
|
|
|
tcc_split_path(s, (void ***)&s->library_paths, &s->nb_library_paths, pathname);
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-06 18:49:30 +04:00
|
|
|
static int tcc_add_library_internal(TCCState *s, const char *fmt,
|
|
|
|
const char *filename, int flags, char **paths, int nb_paths)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int i;
|
|
|
|
|
2011-08-06 18:49:30 +04:00
|
|
|
for(i = 0; i < nb_paths; i++) {
|
|
|
|
snprintf(buf, sizeof(buf), fmt, paths[i], filename);
|
2015-04-12 15:35:37 +03:00
|
|
|
if (tcc_add_file_internal(s, buf, flags, TCC_FILETYPE_BINARY) == 0)
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-10 11:37:36 +03:00
|
|
|
#ifndef TCC_TARGET_PE
|
2011-08-06 18:49:30 +04:00
|
|
|
/* find and load a dll. Return non zero if not found */
|
|
|
|
/* XXX: add '-rpath' option support ? */
|
|
|
|
ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
|
|
|
|
{
|
|
|
|
return tcc_add_library_internal(s, "%s/%s", filename, flags,
|
|
|
|
s->library_paths, s->nb_library_paths);
|
|
|
|
}
|
2015-05-10 11:37:36 +03:00
|
|
|
#endif
|
2011-08-06 18:49:30 +04:00
|
|
|
|
|
|
|
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
|
|
|
|
{
|
|
|
|
if (-1 == tcc_add_library_internal(s, "%s/%s",
|
|
|
|
filename, 0, s->crt_paths, s->nb_crt_paths))
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error_noabort("file '%s' not found", filename);
|
2011-08-06 18:49:30 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
/* the library name is the same as the argument of the '-l' option */
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
#ifdef TCC_TARGET_PE
|
2011-08-06 18:49:30 +04:00
|
|
|
const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
|
|
|
|
const char **pp = s->static_link ? libs + 4 : libs;
|
2009-05-05 22:18:53 +04:00
|
|
|
#else
|
2011-08-06 18:49:30 +04:00
|
|
|
const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
|
|
|
|
const char **pp = s->static_link ? libs + 1 : libs;
|
2009-07-19 00:07:25 +04:00
|
|
|
#endif
|
2011-08-06 18:49:30 +04:00
|
|
|
while (*pp) {
|
|
|
|
if (0 == tcc_add_library_internal(s, *pp,
|
|
|
|
libraryname, 0, s->library_paths, s->nb_library_paths))
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
2011-08-06 18:49:30 +04:00
|
|
|
++pp;
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
|
|
|
|
{
|
|
|
|
int ret = tcc_add_library(s, libname);
|
|
|
|
if (ret < 0)
|
|
|
|
tcc_error_noabort("cannot find library 'lib%s'", libname);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* habdle #pragma comment(lib,) */
|
|
|
|
ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s1->nb_pragma_libs; i++)
|
|
|
|
tcc_add_library_err(s1, s1->pragma_libs[i]);
|
|
|
|
}
|
|
|
|
|
2010-01-14 22:56:04 +03:00
|
|
|
LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
2010-01-14 22:55:51 +03:00
|
|
|
#ifdef TCC_TARGET_PE
|
2013-02-12 22:13:28 +04:00
|
|
|
/* On x86_64 'val' might not be reachable with a 32bit offset.
|
|
|
|
So it is handled here as if it were in a DLL. */
|
|
|
|
pe_putimport(s, 0, name, (uintptr_t)val);
|
2010-01-14 22:55:51 +03:00
|
|
|
#else
|
2013-02-12 22:13:28 +04:00
|
|
|
add_elf_sym(symtab_section, (uintptr_t)val, 0,
|
|
|
|
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
|
|
|
|
SHN_ABS, name);
|
2010-01-14 22:55:51 +03:00
|
|
|
#endif
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
s->output_type = output_type;
|
|
|
|
|
|
|
|
if (!s->nostdinc) {
|
|
|
|
/* default include paths */
|
2011-02-02 10:32:53 +03:00
|
|
|
/* -isystem paths have already been handled */
|
2011-08-06 18:11:12 +04:00
|
|
|
tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if bound checking, then add corresponding sections */
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-11 20:45:44 +04:00
|
|
|
if (s->do_bounds_check) {
|
2009-05-05 22:18:53 +04:00
|
|
|
/* define symbol */
|
|
|
|
tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
|
|
|
|
/* create bounds sections */
|
2013-11-08 23:24:15 +04:00
|
|
|
bounds_section = new_section(s, ".bounds",
|
2009-05-05 22:18:53 +04:00
|
|
|
SHT_PROGBITS, SHF_ALLOC);
|
2013-11-08 23:24:15 +04:00
|
|
|
lbounds_section = new_section(s, ".lbounds",
|
2009-05-05 22:18:53 +04:00
|
|
|
SHT_PROGBITS, SHF_ALLOC);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (s->char_is_unsigned) {
|
|
|
|
tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add debug sections */
|
2009-05-11 20:45:44 +04:00
|
|
|
if (s->do_debug) {
|
2009-05-05 22:18:53 +04:00
|
|
|
/* stab symbols */
|
|
|
|
stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
|
|
|
|
stab_section->sh_entsize = sizeof(Stab_Sym);
|
|
|
|
stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
|
|
|
|
put_elf_str(stabstr_section, "");
|
|
|
|
stab_section->link = stabstr_section;
|
|
|
|
/* put first entry */
|
|
|
|
put_stabs("", 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2011-08-06 18:11:12 +04:00
|
|
|
tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
|
2013-09-23 11:40:06 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
2011-08-01 03:10:36 +04:00
|
|
|
# ifdef _WIN32
|
|
|
|
tcc_add_systemdir(s);
|
|
|
|
# endif
|
|
|
|
#else
|
2011-08-06 18:11:12 +04:00
|
|
|
/* add libc crt1/crti objects */
|
2009-05-05 22:18:53 +04:00
|
|
|
if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
|
|
|
|
!s->nostdlib) {
|
2011-08-04 00:26:39 +04:00
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
2011-08-06 18:49:30 +04:00
|
|
|
tcc_add_crt(s, "crt1.o");
|
|
|
|
tcc_add_crt(s, "crti.o");
|
2009-05-05 22:18:53 +04:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-10 07:37:31 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (s->do_bounds_check && (output_type == TCC_OUTPUT_EXE))
|
|
|
|
{
|
2015-04-12 04:47:15 +03:00
|
|
|
/* force a bcheck.o linking */
|
|
|
|
addr_t func = TOK___bound_init;
|
|
|
|
Sym *sym = external_global_sym(func, &func_old_type, 0);
|
|
|
|
if (!sym->c)
|
|
|
|
put_extern_sym(sym, NULL, 0, 0);
|
2015-04-10 07:37:31 +03:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-13 12:16:00 +03:00
|
|
|
|
|
|
|
if (s->output_type == TCC_OUTPUT_PREPROCESS)
|
|
|
|
print_defines();
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
|
|
|
|
{
|
|
|
|
tcc_free(s->tcc_lib_path);
|
|
|
|
s->tcc_lib_path = tcc_strdup(path);
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:18:53 +04:00
|
|
|
#define WD_ALL 0x0001 /* warning is activated when using -Wall */
|
|
|
|
#define FD_INVERT 0x0002 /* invert value before storing */
|
|
|
|
|
|
|
|
typedef struct FlagDef {
|
|
|
|
uint16_t offset;
|
|
|
|
uint16_t flags;
|
|
|
|
const char *name;
|
|
|
|
} FlagDef;
|
|
|
|
|
|
|
|
static const FlagDef warning_defs[] = {
|
|
|
|
{ offsetof(TCCState, warn_unsupported), 0, "unsupported" },
|
|
|
|
{ offsetof(TCCState, warn_write_strings), 0, "write-strings" },
|
|
|
|
{ offsetof(TCCState, warn_error), 0, "error" },
|
|
|
|
{ offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
|
|
|
|
"implicit-function-declaration" },
|
|
|
|
};
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
|
2009-05-05 22:18:53 +04:00
|
|
|
const char *name, int value)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const FlagDef *p;
|
|
|
|
const char *r;
|
|
|
|
|
|
|
|
r = name;
|
|
|
|
if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
|
|
|
|
r += 3;
|
|
|
|
value = !value;
|
|
|
|
}
|
|
|
|
for(i = 0, p = flags; i < nb_flags; i++, p++) {
|
|
|
|
if (!strcmp(r, p->name))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
found:
|
|
|
|
if (p->flags & FD_INVERT)
|
|
|
|
value = !value;
|
|
|
|
*(int *)((uint8_t *)s + p->offset) = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set/reset a warning */
|
2013-02-12 22:13:28 +04:00
|
|
|
static int tcc_set_warning(TCCState *s, const char *warning_name, int value)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const FlagDef *p;
|
|
|
|
|
|
|
|
if (!strcmp(warning_name, "all")) {
|
|
|
|
for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
|
|
|
|
if (p->flags & WD_ALL)
|
|
|
|
*(int *)((uint8_t *)s + p->offset) = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return set_flag(s, warning_defs, countof(warning_defs),
|
|
|
|
warning_name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const FlagDef flag_defs[] = {
|
|
|
|
{ offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
|
|
|
|
{ offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
|
|
|
|
{ offsetof(TCCState, nocommon), FD_INVERT, "common" },
|
|
|
|
{ offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
|
2015-04-10 06:31:58 +03:00
|
|
|
{ offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
|
2015-04-10 23:44:10 +03:00
|
|
|
{ offsetof(TCCState, old_struct_init_code), 0, "old-struct-init-code" },
|
2015-04-12 15:32:03 +03:00
|
|
|
{ offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
|
2009-05-05 22:18:53 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* set/reset a flag */
|
2013-02-12 22:13:28 +04:00
|
|
|
static int tcc_set_flag(TCCState *s, const char *flag_name, int value)
|
2009-05-05 22:18:53 +04:00
|
|
|
{
|
|
|
|
return set_flag(s, flag_defs, countof(flag_defs),
|
|
|
|
flag_name, value);
|
|
|
|
}
|
|
|
|
|
2010-04-05 23:21:58 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static int strstart(const char *val, const char **str)
|
2010-04-05 23:21:58 +04:00
|
|
|
{
|
|
|
|
const char *p, *q;
|
2013-02-12 22:13:28 +04:00
|
|
|
p = *str;
|
2010-04-05 23:21:58 +04:00
|
|
|
q = val;
|
2013-02-12 22:13:28 +04:00
|
|
|
while (*q) {
|
2010-04-05 23:21:58 +04:00
|
|
|
if (*p != *q)
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
*str = p;
|
2010-04-05 23:21:58 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-12 23:30:21 +04:00
|
|
|
/* Like strstart, but automatically takes into account that ld options can
|
|
|
|
*
|
|
|
|
* - start with double or single dash (e.g. '--soname' or '-soname')
|
|
|
|
* - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
|
|
|
|
* or '-Wl,-soname=x.so')
|
|
|
|
*
|
|
|
|
* you provide `val` always in 'option[=]' form (no leading -)
|
|
|
|
*/
|
2013-02-12 22:13:28 +04:00
|
|
|
static int link_option(const char *str, const char *val, const char **ptr)
|
2010-08-12 23:30:21 +04:00
|
|
|
{
|
|
|
|
const char *p, *q;
|
|
|
|
|
|
|
|
/* there should be 1 or 2 dashes */
|
|
|
|
if (*str++ != '-')
|
|
|
|
return 0;
|
|
|
|
if (*str == '-')
|
|
|
|
str++;
|
|
|
|
|
|
|
|
/* then str & val should match (potentialy up to '=') */
|
|
|
|
p = str;
|
|
|
|
q = val;
|
|
|
|
|
|
|
|
while (*q != '\0' && *q != '=') {
|
|
|
|
if (*p != *q)
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* '=' near eos means ',' or '=' is ok */
|
|
|
|
if (*q == '=') {
|
|
|
|
if (*p != ',' && *p != '=')
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr)
|
2013-02-12 22:13:28 +04:00
|
|
|
*ptr = p;
|
2010-08-12 23:30:21 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static const char *skip_linker_arg(const char **str)
|
|
|
|
{
|
|
|
|
const char *s1 = *str;
|
|
|
|
const char *s2 = strchr(s1, ',');
|
|
|
|
*str = s2 ? s2++ : (s2 = s1 + strlen(s1));
|
|
|
|
return s2;
|
|
|
|
}
|
2010-08-12 23:30:21 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static char *copy_linker_arg(const char *p)
|
2010-04-05 23:21:58 +04:00
|
|
|
{
|
2013-02-12 22:13:28 +04:00
|
|
|
const char *q = p;
|
|
|
|
skip_linker_arg(&q);
|
|
|
|
return pstrncpy(tcc_malloc(q - p + 1), p, q - p);
|
|
|
|
}
|
2010-04-05 23:21:58 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* set linker options */
|
|
|
|
static int tcc_set_linker(TCCState *s, const char *option)
|
|
|
|
{
|
2010-04-05 23:21:58 +04:00
|
|
|
while (option && *option) {
|
2013-02-12 22:13:28 +04:00
|
|
|
|
|
|
|
const char *p = option;
|
|
|
|
char *end = NULL;
|
|
|
|
int ignoring = 0;
|
|
|
|
|
2010-08-12 23:30:21 +04:00
|
|
|
if (link_option(option, "Bsymbolic", &p)) {
|
2013-02-04 18:10:08 +04:00
|
|
|
s->symbolic = 1;
|
2012-04-18 11:48:57 +04:00
|
|
|
} else if (link_option(option, "nostdlib", &p)) {
|
2013-02-04 18:10:08 +04:00
|
|
|
s->nostdlib = 1;
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "fini=", &p)) {
|
2013-02-12 22:13:28 +04:00
|
|
|
s->fini_symbol = copy_linker_arg(p);
|
|
|
|
ignoring = 1;
|
|
|
|
} else if (link_option(option, "image-base=", &p)
|
|
|
|
|| link_option(option, "Ttext=", &p)) {
|
2012-03-03 21:10:15 +04:00
|
|
|
s->text_addr = strtoull(p, &end, 16);
|
2010-04-05 23:21:58 +04:00
|
|
|
s->has_text_addr = 1;
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "init=", &p)) {
|
2013-02-12 22:13:28 +04:00
|
|
|
s->init_symbol = copy_linker_arg(p);
|
|
|
|
ignoring = 1;
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "oformat=", &p)) {
|
2010-04-05 23:21:58 +04:00
|
|
|
#if defined(TCC_TARGET_PE)
|
2013-02-12 22:13:28 +04:00
|
|
|
if (strstart("pe-", &p)) {
|
2015-02-13 21:58:31 +03:00
|
|
|
#elif defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
|
2013-02-12 22:13:28 +04:00
|
|
|
if (strstart("elf64-", &p)) {
|
2010-04-05 23:21:58 +04:00
|
|
|
#else
|
2013-02-12 22:13:28 +04:00
|
|
|
if (strstart("elf32-", &p)) {
|
2010-04-05 23:21:58 +04:00
|
|
|
#endif
|
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_ELF;
|
|
|
|
} else if (!strcmp(p, "binary")) {
|
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_BINARY;
|
|
|
|
#ifdef TCC_TARGET_COFF
|
2013-02-12 22:13:28 +04:00
|
|
|
} else if (!strcmp(p, "coff")) {
|
2010-04-05 23:21:58 +04:00
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_COFF;
|
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
} else
|
|
|
|
goto err;
|
2010-04-05 23:21:58 +04:00
|
|
|
|
2014-03-07 00:29:19 +04:00
|
|
|
} else if (link_option(option, "as-needed", &p)) {
|
|
|
|
ignoring = 1;
|
|
|
|
} else if (link_option(option, "O", &p)) {
|
|
|
|
ignoring = 1;
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "rpath=", &p)) {
|
2013-02-12 22:13:28 +04:00
|
|
|
s->rpath = copy_linker_arg(p);
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "section-alignment=", &p)) {
|
2010-04-05 23:21:58 +04:00
|
|
|
s->section_align = strtoul(p, &end, 16);
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "soname=", &p)) {
|
2013-02-12 22:13:28 +04:00
|
|
|
s->soname = copy_linker_arg(p);
|
2010-04-05 23:21:58 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
2011-07-11 20:47:16 +04:00
|
|
|
} else if (link_option(option, "file-alignment=", &p)) {
|
|
|
|
s->pe_file_align = strtoul(p, &end, 16);
|
|
|
|
} else if (link_option(option, "stack=", &p)) {
|
|
|
|
s->pe_stack_size = strtoul(p, &end, 10);
|
2010-08-12 23:30:21 +04:00
|
|
|
} else if (link_option(option, "subsystem=", &p)) {
|
2010-04-05 23:21:58 +04:00
|
|
|
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
|
|
|
if (!strcmp(p, "native")) {
|
|
|
|
s->pe_subsystem = 1;
|
|
|
|
} else if (!strcmp(p, "console")) {
|
|
|
|
s->pe_subsystem = 3;
|
|
|
|
} else if (!strcmp(p, "gui")) {
|
|
|
|
s->pe_subsystem = 2;
|
|
|
|
} else if (!strcmp(p, "posix")) {
|
|
|
|
s->pe_subsystem = 7;
|
|
|
|
} else if (!strcmp(p, "efiapp")) {
|
|
|
|
s->pe_subsystem = 10;
|
|
|
|
} else if (!strcmp(p, "efiboot")) {
|
|
|
|
s->pe_subsystem = 11;
|
|
|
|
} else if (!strcmp(p, "efiruntime")) {
|
|
|
|
s->pe_subsystem = 12;
|
|
|
|
} else if (!strcmp(p, "efirom")) {
|
|
|
|
s->pe_subsystem = 13;
|
|
|
|
#elif defined(TCC_TARGET_ARM)
|
|
|
|
if (!strcmp(p, "wince")) {
|
|
|
|
s->pe_subsystem = 9;
|
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
} else
|
|
|
|
goto err;
|
2010-04-05 23:21:58 +04:00
|
|
|
#endif
|
|
|
|
} else
|
2013-02-12 22:13:28 +04:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (ignoring && s->warn_unsupported) err: {
|
|
|
|
char buf[100], *e;
|
|
|
|
pstrcpy(buf, sizeof buf, e = copy_linker_arg(option)), tcc_free(e);
|
|
|
|
if (ignoring)
|
|
|
|
tcc_warning("unsupported linker option '%s'", buf);
|
|
|
|
else
|
|
|
|
tcc_error("unsupported linker option '%s'", buf);
|
|
|
|
}
|
|
|
|
option = skip_linker_arg(&p);
|
2010-04-05 23:21:58 +04:00
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
return 0;
|
2010-04-05 23:21:58 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
typedef struct TCCOption {
|
|
|
|
const char *name;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t flags;
|
|
|
|
} TCCOption;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TCC_OPTION_HELP,
|
|
|
|
TCC_OPTION_I,
|
|
|
|
TCC_OPTION_D,
|
|
|
|
TCC_OPTION_U,
|
2015-03-03 14:19:14 +03:00
|
|
|
TCC_OPTION_P,
|
2013-02-12 22:13:28 +04:00
|
|
|
TCC_OPTION_L,
|
|
|
|
TCC_OPTION_B,
|
|
|
|
TCC_OPTION_l,
|
|
|
|
TCC_OPTION_bench,
|
|
|
|
TCC_OPTION_bt,
|
|
|
|
TCC_OPTION_b,
|
|
|
|
TCC_OPTION_g,
|
|
|
|
TCC_OPTION_c,
|
2015-03-23 20:58:27 +03:00
|
|
|
TCC_OPTION_dumpversion,
|
2015-05-13 12:16:00 +03:00
|
|
|
TCC_OPTION_d,
|
2014-01-07 11:23:54 +04:00
|
|
|
TCC_OPTION_float_abi,
|
2013-02-12 22:13:28 +04:00
|
|
|
TCC_OPTION_static,
|
2015-03-03 16:25:02 +03:00
|
|
|
TCC_OPTION_std,
|
2013-02-12 22:13:28 +04:00
|
|
|
TCC_OPTION_shared,
|
|
|
|
TCC_OPTION_soname,
|
|
|
|
TCC_OPTION_o,
|
|
|
|
TCC_OPTION_r,
|
|
|
|
TCC_OPTION_s,
|
2015-05-03 20:10:57 +03:00
|
|
|
TCC_OPTION_traditional,
|
2013-02-12 22:13:28 +04:00
|
|
|
TCC_OPTION_Wl,
|
|
|
|
TCC_OPTION_W,
|
|
|
|
TCC_OPTION_O,
|
|
|
|
TCC_OPTION_m,
|
|
|
|
TCC_OPTION_f,
|
|
|
|
TCC_OPTION_isystem,
|
2015-05-03 20:10:57 +03:00
|
|
|
TCC_OPTION_iwithprefix,
|
2013-02-12 22:13:28 +04:00
|
|
|
TCC_OPTION_nostdinc,
|
|
|
|
TCC_OPTION_nostdlib,
|
|
|
|
TCC_OPTION_print_search_dirs,
|
|
|
|
TCC_OPTION_rdynamic,
|
|
|
|
TCC_OPTION_pedantic,
|
|
|
|
TCC_OPTION_pthread,
|
|
|
|
TCC_OPTION_run,
|
|
|
|
TCC_OPTION_v,
|
|
|
|
TCC_OPTION_w,
|
|
|
|
TCC_OPTION_pipe,
|
|
|
|
TCC_OPTION_E,
|
|
|
|
TCC_OPTION_MD,
|
|
|
|
TCC_OPTION_MF,
|
|
|
|
TCC_OPTION_x,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TCC_OPTION_HAS_ARG 0x0001
|
|
|
|
#define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
|
|
|
|
|
|
|
|
static const TCCOption tcc_options[] = {
|
|
|
|
{ "h", TCC_OPTION_HELP, 0 },
|
|
|
|
{ "-help", TCC_OPTION_HELP, 0 },
|
|
|
|
{ "?", TCC_OPTION_HELP, 0 },
|
|
|
|
{ "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
|
2015-03-03 14:19:14 +03:00
|
|
|
{ "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-12 22:13:28 +04:00
|
|
|
{ "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "bench", TCC_OPTION_bench, 0 },
|
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
{ "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
{ "b", TCC_OPTION_b, 0 },
|
|
|
|
#endif
|
|
|
|
{ "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "c", TCC_OPTION_c, 0 },
|
2015-03-23 20:58:27 +03:00
|
|
|
{ "dumpversion", TCC_OPTION_dumpversion, 0},
|
2015-05-13 12:16:00 +03:00
|
|
|
{ "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2014-01-07 11:23:54 +04:00
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
{ "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG },
|
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
{ "static", TCC_OPTION_static, 0 },
|
2015-03-03 16:25:02 +03:00
|
|
|
{ "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-12 22:13:28 +04:00
|
|
|
{ "shared", TCC_OPTION_shared, 0 },
|
|
|
|
{ "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "pedantic", TCC_OPTION_pedantic, 0},
|
|
|
|
{ "pthread", TCC_OPTION_pthread, 0},
|
|
|
|
{ "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "rdynamic", TCC_OPTION_rdynamic, 0 },
|
|
|
|
{ "r", TCC_OPTION_r, 0 },
|
|
|
|
{ "s", TCC_OPTION_s, 0 },
|
2015-05-03 20:10:57 +03:00
|
|
|
{ "traditional", TCC_OPTION_traditional, 0 },
|
2013-02-12 22:13:28 +04:00
|
|
|
{ "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
|
2015-05-03 20:10:57 +03:00
|
|
|
{ "iwithprefix", TCC_OPTION_iwithprefix, TCC_OPTION_HAS_ARG },
|
2013-02-12 22:13:28 +04:00
|
|
|
{ "nostdinc", TCC_OPTION_nostdinc, 0 },
|
|
|
|
{ "nostdlib", TCC_OPTION_nostdlib, 0 },
|
2013-11-08 23:24:15 +04:00
|
|
|
{ "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
|
2013-02-12 22:13:28 +04:00
|
|
|
{ "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "w", TCC_OPTION_w, 0 },
|
|
|
|
{ "pipe", TCC_OPTION_pipe, 0},
|
|
|
|
{ "E", TCC_OPTION_E, 0},
|
|
|
|
{ "MD", TCC_OPTION_MD, 0},
|
|
|
|
{ "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
|
|
|
|
{ NULL, 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void parse_option_D(TCCState *s1, const char *optarg)
|
2009-12-20 03:53:49 +03:00
|
|
|
{
|
2013-02-12 22:13:28 +04:00
|
|
|
char *sym = tcc_strdup(optarg);
|
|
|
|
char *value = strchr(sym, '=');
|
|
|
|
if (value)
|
|
|
|
*value++ = '\0';
|
|
|
|
tcc_define_symbol(s1, sym, value);
|
|
|
|
tcc_free(sym);
|
2009-12-20 03:53:49 +03:00
|
|
|
}
|
|
|
|
|
2015-04-12 15:35:37 +03:00
|
|
|
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
|
|
|
|
{
|
|
|
|
int len = strlen(filename);
|
|
|
|
char *p = tcc_malloc(len + 2);
|
|
|
|
if (filetype) {
|
|
|
|
*p = filetype;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* use a file extension to detect a filetype */
|
|
|
|
const char *ext = tcc_fileextension(filename);
|
|
|
|
if (ext[0]) {
|
|
|
|
ext++;
|
|
|
|
if (!strcmp(ext, "S"))
|
|
|
|
*p = TCC_FILETYPE_ASM_PP;
|
|
|
|
else
|
|
|
|
if (!strcmp(ext, "s"))
|
|
|
|
*p = TCC_FILETYPE_ASM;
|
|
|
|
else
|
|
|
|
if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
|
|
|
|
*p = TCC_FILETYPE_C;
|
|
|
|
else
|
|
|
|
*p = TCC_FILETYPE_BINARY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = TCC_FILETYPE_C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strcpy(p+1, filename);
|
|
|
|
dynarray_add((void ***)&s->files, &s->nb_files, p);
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
|
2010-06-16 16:54:24 +04:00
|
|
|
{
|
2013-02-12 22:13:28 +04:00
|
|
|
const TCCOption *popt;
|
|
|
|
const char *optarg, *r;
|
|
|
|
int run = 0;
|
|
|
|
int pthread = 0;
|
|
|
|
int optind = 0;
|
2015-04-12 15:35:37 +03:00
|
|
|
int filetype = 0;
|
2011-08-06 18:08:46 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
|
|
|
|
CString linker_arg;
|
|
|
|
cstr_new(&linker_arg);
|
2010-06-16 16:54:24 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
while (optind < argc) {
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
r = argv[optind++];
|
|
|
|
if (r[0] != '-' || r[1] == '\0') {
|
2015-04-12 15:35:37 +03:00
|
|
|
args_parser_add_file(s, r, filetype);
|
2013-02-12 22:13:28 +04:00
|
|
|
if (run) {
|
|
|
|
optind--;
|
|
|
|
/* argv[0] will be this file */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* find option in table */
|
|
|
|
for(popt = tcc_options; ; ++popt) {
|
|
|
|
const char *p1 = popt->name;
|
|
|
|
const char *r1 = r + 1;
|
|
|
|
if (p1 == NULL)
|
|
|
|
tcc_error("invalid option -- '%s'", r);
|
|
|
|
if (!strstart(p1, &r1))
|
|
|
|
continue;
|
|
|
|
optarg = r1;
|
|
|
|
if (popt->flags & TCC_OPTION_HAS_ARG) {
|
|
|
|
if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
|
|
|
|
if (optind >= argc)
|
|
|
|
tcc_error("argument to '%s' is missing", r);
|
|
|
|
optarg = argv[optind++];
|
|
|
|
}
|
|
|
|
} else if (*r1 != '\0')
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
switch(popt->index) {
|
|
|
|
case TCC_OPTION_HELP:
|
|
|
|
return 0;
|
|
|
|
case TCC_OPTION_I:
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +03:00
|
|
|
tcc_add_include_path(s, optarg);
|
2013-02-12 22:13:28 +04:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_D:
|
|
|
|
parse_option_D(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_U:
|
|
|
|
tcc_undefine_symbol(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_L:
|
|
|
|
tcc_add_library_path(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_B:
|
|
|
|
/* set tcc utilities path (mainly for tcc development) */
|
|
|
|
tcc_set_lib_path(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_l:
|
2015-04-12 15:35:37 +03:00
|
|
|
args_parser_add_file(s, r, TCC_FILETYPE_BINARY);
|
2013-02-12 22:13:28 +04:00
|
|
|
s->nb_libraries++;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_pthread:
|
|
|
|
parse_option_D(s, "_REENTRANT");
|
|
|
|
pthread = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_bench:
|
|
|
|
s->do_bench = 1;
|
|
|
|
break;
|
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
case TCC_OPTION_bt:
|
|
|
|
tcc_set_num_callers(atoi(optarg));
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
case TCC_OPTION_b:
|
|
|
|
s->do_bounds_check = 1;
|
|
|
|
s->do_debug = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TCC_OPTION_g:
|
|
|
|
s->do_debug = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_c:
|
2015-05-13 12:16:00 +03:00
|
|
|
if (s->output_type)
|
2015-01-06 22:19:45 +03:00
|
|
|
tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
|
2013-02-12 22:13:28 +04:00
|
|
|
s->output_type = TCC_OUTPUT_OBJ;
|
|
|
|
break;
|
2015-05-13 12:16:00 +03:00
|
|
|
case TCC_OPTION_d:
|
|
|
|
if (*optarg == 'D')
|
|
|
|
s->dflag = 1;
|
|
|
|
else {
|
|
|
|
if (s->warn_unsupported)
|
|
|
|
goto unsupported_option;
|
|
|
|
tcc_error("invalid option -- '%s'", r);
|
|
|
|
}
|
|
|
|
break;
|
2014-01-07 11:23:54 +04:00
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
case TCC_OPTION_float_abi:
|
|
|
|
/* tcc doesn't support soft float yet */
|
|
|
|
if (!strcmp(optarg, "softfp")) {
|
|
|
|
s->float_abi = ARM_SOFTFP_FLOAT;
|
|
|
|
tcc_undefine_symbol(s, "__ARM_PCS_VFP");
|
|
|
|
} else if (!strcmp(optarg, "hard"))
|
|
|
|
s->float_abi = ARM_HARD_FLOAT;
|
|
|
|
else
|
|
|
|
tcc_error("unsupported float abi '%s'", optarg);
|
|
|
|
break;
|
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
case TCC_OPTION_static:
|
|
|
|
s->static_link = 1;
|
|
|
|
break;
|
2015-03-03 16:25:02 +03:00
|
|
|
case TCC_OPTION_std:
|
2015-07-29 23:53:57 +03:00
|
|
|
/* silently ignore, a current purpose:
|
|
|
|
allow to use a tcc as a reference compiler for "make test" */
|
2015-03-03 16:25:02 +03:00
|
|
|
break;
|
2013-02-12 22:13:28 +04:00
|
|
|
case TCC_OPTION_shared:
|
2015-07-29 23:53:57 +03:00
|
|
|
if (s->output_type)
|
2015-01-06 22:19:45 +03:00
|
|
|
tcc_warning("-shared: some compiler action already specified (%d)", s->output_type);
|
2013-02-12 22:13:28 +04:00
|
|
|
s->output_type = TCC_OUTPUT_DLL;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_soname:
|
|
|
|
s->soname = tcc_strdup(optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_m:
|
|
|
|
s->option_m = tcc_strdup(optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_o:
|
2015-05-10 10:13:35 +03:00
|
|
|
if (s->outfile) {
|
|
|
|
tcc_warning("multiple -o option");
|
|
|
|
tcc_free(s->outfile);
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
s->outfile = tcc_strdup(optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_r:
|
|
|
|
/* generate a .o merging several output files */
|
2015-07-29 23:53:57 +03:00
|
|
|
if (s->output_type)
|
2015-01-06 22:19:45 +03:00
|
|
|
tcc_warning("-r: some compiler action already specified (%d)", s->output_type);
|
2013-02-12 22:13:28 +04:00
|
|
|
s->option_r = 1;
|
|
|
|
s->output_type = TCC_OUTPUT_OBJ;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_isystem:
|
|
|
|
tcc_add_sysinclude_path(s, optarg);
|
|
|
|
break;
|
2015-05-03 20:10:57 +03:00
|
|
|
case TCC_OPTION_iwithprefix:
|
|
|
|
if (1) {
|
|
|
|
char buf[1024];
|
|
|
|
int buf_size = sizeof(buf)-1;
|
|
|
|
char *p = &buf[0];
|
|
|
|
|
|
|
|
char *sysroot = "{B}/";
|
|
|
|
int len = strlen(sysroot);
|
|
|
|
if (len > buf_size)
|
|
|
|
len = buf_size;
|
|
|
|
strncpy(p, sysroot, len);
|
|
|
|
p += len;
|
|
|
|
buf_size -= len;
|
|
|
|
|
|
|
|
len = strlen(optarg);
|
|
|
|
if (len > buf_size)
|
|
|
|
len = buf_size;
|
|
|
|
strncpy(p, optarg, len+1);
|
|
|
|
tcc_add_sysinclude_path(s, buf);
|
|
|
|
}
|
|
|
|
break;
|
2013-02-12 22:13:28 +04:00
|
|
|
case TCC_OPTION_nostdinc:
|
|
|
|
s->nostdinc = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_nostdlib:
|
|
|
|
s->nostdlib = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_print_search_dirs:
|
|
|
|
s->print_search_dirs = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_run:
|
2015-07-29 23:53:57 +03:00
|
|
|
if (s->output_type)
|
2015-01-06 22:19:45 +03:00
|
|
|
tcc_warning("-run: some compiler action already specified (%d)", s->output_type);
|
2013-02-12 22:13:28 +04:00
|
|
|
s->output_type = TCC_OUTPUT_MEMORY;
|
|
|
|
tcc_set_options(s, optarg);
|
|
|
|
run = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_v:
|
|
|
|
do ++s->verbose; while (*optarg++ == 'v');
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_f:
|
|
|
|
if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
|
|
|
|
goto unsupported_option;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_W:
|
2013-11-08 23:24:15 +04:00
|
|
|
if (tcc_set_warning(s, optarg, 1) < 0 &&
|
2013-02-12 22:13:28 +04:00
|
|
|
s->warn_unsupported)
|
|
|
|
goto unsupported_option;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_w:
|
|
|
|
s->warn_none = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_rdynamic:
|
|
|
|
s->rdynamic = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_Wl:
|
|
|
|
if (linker_arg.size)
|
|
|
|
--linker_arg.size, cstr_ccat(&linker_arg, ',');
|
|
|
|
cstr_cat(&linker_arg, optarg);
|
|
|
|
cstr_ccat(&linker_arg, '\0');
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_E:
|
2015-07-29 23:53:57 +03:00
|
|
|
if (s->output_type)
|
2015-01-06 22:19:45 +03:00
|
|
|
tcc_warning("-E: some compiler action already specified (%d)", s->output_type);
|
2013-02-12 22:13:28 +04:00
|
|
|
s->output_type = TCC_OUTPUT_PREPROCESS;
|
|
|
|
break;
|
2015-03-03 14:19:14 +03:00
|
|
|
case TCC_OPTION_P:
|
|
|
|
s->Pflag = atoi(optarg) + 1;
|
|
|
|
break;
|
2013-02-12 22:13:28 +04:00
|
|
|
case TCC_OPTION_MD:
|
|
|
|
s->gen_deps = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_MF:
|
|
|
|
s->deps_outfile = tcc_strdup(optarg);
|
|
|
|
break;
|
2013-02-14 11:24:51 +04:00
|
|
|
case TCC_OPTION_dumpversion:
|
|
|
|
printf ("%s\n", TCC_VERSION);
|
|
|
|
exit(0);
|
2015-04-10 06:53:48 +03:00
|
|
|
case TCC_OPTION_s:
|
|
|
|
s->do_strip = 1;
|
|
|
|
break;
|
2015-05-03 20:10:57 +03:00
|
|
|
case TCC_OPTION_traditional:
|
|
|
|
break;
|
2015-04-12 15:35:37 +03:00
|
|
|
case TCC_OPTION_x:
|
|
|
|
if (*optarg == 'c')
|
|
|
|
filetype = TCC_FILETYPE_C;
|
|
|
|
else
|
|
|
|
if (*optarg == 'a')
|
|
|
|
filetype = TCC_FILETYPE_ASM_PP;
|
|
|
|
else
|
|
|
|
if (*optarg == 'n')
|
|
|
|
filetype = 0;
|
|
|
|
else
|
|
|
|
tcc_warning("unsupported language '%s'", optarg);
|
|
|
|
break;
|
2013-02-12 22:13:28 +04:00
|
|
|
case TCC_OPTION_O:
|
2015-05-10 10:21:37 +03:00
|
|
|
if (1) {
|
|
|
|
int opt = atoi(optarg);
|
|
|
|
char *sym = "__OPTIMIZE__";
|
|
|
|
if (opt)
|
|
|
|
tcc_define_symbol(s, sym, 0);
|
|
|
|
else
|
|
|
|
tcc_undefine_symbol(s, sym);
|
|
|
|
}
|
|
|
|
break;
|
2013-02-12 22:13:28 +04:00
|
|
|
case TCC_OPTION_pedantic:
|
|
|
|
case TCC_OPTION_pipe:
|
|
|
|
/* ignored */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (s->warn_unsupported) {
|
|
|
|
unsupported_option:
|
|
|
|
tcc_warning("unsupported option '%s'", r);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
}
|
|
|
|
|
2015-05-03 14:17:18 +03:00
|
|
|
if (s->output_type == 0)
|
2015-07-29 23:53:57 +03:00
|
|
|
s->output_type = TCC_OUTPUT_EXE;
|
2015-05-03 14:17:18 +03:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (pthread && s->output_type != TCC_OUTPUT_OBJ)
|
|
|
|
tcc_set_options(s, "-lpthread");
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
|
2015-05-03 14:17:18 +03:00
|
|
|
if (s->output_type == TCC_OUTPUT_EXE)
|
2015-07-29 23:53:57 +03:00
|
|
|
tcc_set_linker(s, (const char *)linker_arg.data);
|
2013-02-12 22:13:28 +04:00
|
|
|
cstr_free(&linker_arg);
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
return optind;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBTCCAPI int tcc_set_options(TCCState *s, const char *str)
|
|
|
|
{
|
|
|
|
const char *s1;
|
|
|
|
char **argv, *arg;
|
|
|
|
int argc, len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
argc = 0, argv = NULL;
|
|
|
|
for(;;) {
|
|
|
|
while (is_space(*str))
|
|
|
|
str++;
|
|
|
|
if (*str == '\0')
|
|
|
|
break;
|
|
|
|
s1 = str;
|
|
|
|
while (*str != '\0' && !is_space(*str))
|
|
|
|
str++;
|
|
|
|
len = str - s1;
|
|
|
|
arg = tcc_malloc(len + 1);
|
|
|
|
pstrncpy(arg, s1, len);
|
|
|
|
dynarray_add((void ***)&argv, &argc, arg);
|
|
|
|
}
|
|
|
|
ret = tcc_parse_args(s, argc, argv);
|
|
|
|
dynarray_reset(&argv, &argc);
|
|
|
|
return ret;
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
}
|
2013-02-10 03:38:40 +04:00
|
|
|
|
|
|
|
PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time)
|
|
|
|
{
|
|
|
|
double tt;
|
|
|
|
tt = (double)total_time / 1000000.0;
|
|
|
|
if (tt < 0.001)
|
|
|
|
tt = 0.001;
|
|
|
|
if (total_bytes < 1)
|
|
|
|
total_bytes = 1;
|
2015-05-12 22:02:51 +03:00
|
|
|
fprintf(stderr, "%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
|
2013-02-10 03:38:40 +04:00
|
|
|
tok_ident - TOK_IDENT, total_lines, total_bytes,
|
|
|
|
tt, (int)(total_lines / tt),
|
|
|
|
total_bytes / tt / 1000000.0);
|
|
|
|
}
|
2013-02-19 15:47:36 +04:00
|
|
|
|
|
|
|
PUB_FUNC void tcc_set_environment(TCCState *s)
|
|
|
|
{
|
|
|
|
char * path;
|
2013-11-08 23:24:15 +04:00
|
|
|
|
2013-02-19 15:47:36 +04:00
|
|
|
path = getenv("C_INCLUDE_PATH");
|
|
|
|
if(path != NULL) {
|
|
|
|
tcc_add_include_path(s, path);
|
|
|
|
}
|
|
|
|
path = getenv("CPATH");
|
|
|
|
if(path != NULL) {
|
|
|
|
tcc_add_include_path(s, path);
|
|
|
|
}
|
2013-02-19 17:41:58 +04:00
|
|
|
path = getenv("LIBRARY_PATH");
|
2013-02-19 15:47:36 +04:00
|
|
|
if(path != NULL) {
|
|
|
|
tcc_add_library_path(s, path);
|
|
|
|
}
|
2013-02-19 17:41:58 +04:00
|
|
|
}
|