2009-05-06 02:17:11 +08:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
2013-11-09 03:24:15 +08:00
|
|
|
*
|
2009-05-06 02:17:11 +08: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
|
|
|
|
*/
|
|
|
|
|
2019-12-11 07:37:18 +08:00
|
|
|
#if !defined ONE_SOURCE || ONE_SOURCE
|
2009-12-20 08:53:49 +08:00
|
|
|
#include "tccpp.c"
|
|
|
|
#include "tccgen.c"
|
2022-05-09 23:02:09 +08:00
|
|
|
#include "tccdbg.c"
|
2021-08-02 02:04:46 +08:00
|
|
|
#include "tccasm.c"
|
2009-12-20 08:53:49 +08:00
|
|
|
#include "tccelf.c"
|
|
|
|
#include "tccrun.c"
|
2009-05-06 02:18:53 +08:00
|
|
|
#ifdef TCC_TARGET_I386
|
2015-07-30 04:53:57 +08:00
|
|
|
#include "i386-gen.c"
|
2016-11-12 23:16:06 +08:00
|
|
|
#include "i386-link.c"
|
2017-05-13 14:59:06 +08:00
|
|
|
#include "i386-asm.c"
|
2019-06-22 12:13:10 +08:00
|
|
|
#elif defined(TCC_TARGET_ARM)
|
2015-07-30 04:53:57 +08:00
|
|
|
#include "arm-gen.c"
|
2016-11-12 23:16:06 +08:00
|
|
|
#include "arm-link.c"
|
2017-05-13 14:59:06 +08:00
|
|
|
#include "arm-asm.c"
|
2019-06-22 12:13:10 +08:00
|
|
|
#elif defined(TCC_TARGET_ARM64)
|
2015-07-30 04:53:57 +08:00
|
|
|
#include "arm64-gen.c"
|
2016-11-12 23:16:06 +08:00
|
|
|
#include "arm64-link.c"
|
2020-06-16 13:39:48 +08:00
|
|
|
#include "arm-asm.c"
|
2019-06-22 12:13:10 +08:00
|
|
|
#elif defined(TCC_TARGET_C67)
|
2015-07-30 04:53:57 +08:00
|
|
|
#include "c67-gen.c"
|
2016-11-12 23:16:06 +08:00
|
|
|
#include "c67-link.c"
|
2017-07-24 03:24:11 +08:00
|
|
|
#include "tcccoff.c"
|
2019-06-22 12:13:10 +08:00
|
|
|
#elif defined(TCC_TARGET_X86_64)
|
2015-07-30 04:53:57 +08:00
|
|
|
#include "x86_64-gen.c"
|
2016-11-12 23:16:06 +08:00
|
|
|
#include "x86_64-link.c"
|
2017-05-13 14:59:06 +08:00
|
|
|
#include "i386-asm.c"
|
2019-06-22 12:13:10 +08:00
|
|
|
#elif defined(TCC_TARGET_RISCV64)
|
|
|
|
#include "riscv64-gen.c"
|
|
|
|
#include "riscv64-link.c"
|
2020-06-16 13:39:48 +08:00
|
|
|
#include "riscv64-asm.c"
|
2019-06-22 12:13:10 +08:00
|
|
|
#else
|
|
|
|
#error unknown target
|
2009-05-06 02:18:53 +08:00
|
|
|
#endif
|
2009-07-07 03:13:20 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
#include "tccpe.c"
|
|
|
|
#endif
|
2020-05-22 12:29:18 +08:00
|
|
|
#ifdef TCC_TARGET_MACHO
|
|
|
|
#include "tccmacho.c"
|
|
|
|
#endif
|
2011-07-22 17:12:49 +08:00
|
|
|
#endif /* ONE_SOURCE */
|
2009-07-07 03:13:20 +08:00
|
|
|
|
2019-12-11 07:37:18 +08:00
|
|
|
#include "tcc.h"
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/********************************************************/
|
2019-12-11 07:37:18 +08:00
|
|
|
/* global variables */
|
|
|
|
|
|
|
|
/* XXX: get rid of this ASAP (or maybe not) */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_DATA struct TCCState *tcc_state;
|
2021-08-02 02:04:46 +08:00
|
|
|
TCC_SEM(static tcc_compile_sem);
|
2023-04-25 03:58:50 +08:00
|
|
|
/* an array of pointers to memory to be free'd after errors */
|
|
|
|
ST_DATA void** stk_data;
|
|
|
|
ST_DATA int nb_stk_data;
|
2019-12-11 07:37:18 +08:00
|
|
|
|
2009-07-19 04:07:25 +08:00
|
|
|
/********************************************************/
|
|
|
|
#ifdef _WIN32
|
2016-10-15 21:55:31 +08:00
|
|
|
ST_FUNC char *normalize_slashes(char *path)
|
2009-07-19 04:07:25 +08:00
|
|
|
{
|
|
|
|
char *p;
|
2013-08-29 04:55:05 +08:00
|
|
|
for (p = path; *p; ++p)
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '/';
|
2009-07-19 04:07:25 +08:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:25:58 +08:00
|
|
|
#if defined LIBTCC_AS_DLL && !defined CONFIG_TCCDIR
|
2009-12-20 08:53:49 +08:00
|
|
|
static HMODULE tcc_module;
|
2021-03-30 17:25:58 +08:00
|
|
|
BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
|
|
|
|
{
|
|
|
|
if (DLL_PROCESS_ATTACH == dwReason)
|
|
|
|
tcc_module = hDll;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define tcc_module NULL /* NULL means executable itself */
|
|
|
|
#endif
|
2009-07-19 04:07:25 +08:00
|
|
|
|
2021-08-02 02:04:46 +08:00
|
|
|
#ifndef CONFIG_TCCDIR
|
2009-07-19 04:07:25 +08:00
|
|
|
/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
|
2021-08-02 02:04:46 +08:00
|
|
|
static inline char *config_tccdir_w32(char *path)
|
2009-07-19 04:07:25 +08:00
|
|
|
{
|
2021-08-02 02:04:46 +08:00
|
|
|
char *p;
|
|
|
|
GetModuleFileName(tcc_module, path, MAX_PATH);
|
2009-07-19 04:07:25 +08:00
|
|
|
p = tcc_basename(normalize_slashes(strlwr(path)));
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
if (p > path)
|
|
|
|
--p;
|
2009-07-19 04:07:25 +08:00
|
|
|
*p = 0;
|
2021-08-02 02:04:46 +08:00
|
|
|
return path;
|
2009-07-19 04:07:25 +08:00
|
|
|
}
|
2021-08-02 02:04:46 +08:00
|
|
|
#define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
|
|
|
|
#endif
|
2009-07-19 04:07:25 +08:00
|
|
|
|
2013-02-07 02:01:07 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2021-10-22 13:39:54 +08:00
|
|
|
static void tcc_add_systemdir(TCCState *s)
|
2011-08-01 07:10:36 +08:00
|
|
|
{
|
|
|
|
char buf[1000];
|
|
|
|
GetSystemDirectory(buf, sizeof buf);
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_library_path(s, normalize_slashes(buf));
|
2011-08-01 07:10:36 +08:00
|
|
|
}
|
2013-02-07 02:01:07 +08:00
|
|
|
#endif
|
2009-07-19 04:07:25 +08:00
|
|
|
#endif
|
|
|
|
|
2019-12-11 07:37:18 +08:00
|
|
|
/********************************************************/
|
2021-08-02 02:04:46 +08:00
|
|
|
#if CONFIG_TCC_SEMLOCK
|
|
|
|
#if defined _WIN32
|
|
|
|
ST_FUNC void wait_sem(TCCSem *p)
|
|
|
|
{
|
|
|
|
if (!p->init)
|
|
|
|
InitializeCriticalSection(&p->cr), p->init = 1;
|
|
|
|
EnterCriticalSection(&p->cr);
|
|
|
|
}
|
|
|
|
ST_FUNC void post_sem(TCCSem *p)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&p->cr);
|
|
|
|
}
|
2020-05-22 11:07:19 +08:00
|
|
|
#elif defined __APPLE__
|
|
|
|
/* Half-compatible MacOS doesn't have non-shared (process local)
|
|
|
|
semaphores. Use the dispatch framework for lightweight locks. */
|
2021-08-02 02:04:46 +08:00
|
|
|
ST_FUNC void wait_sem(TCCSem *p)
|
2020-05-22 11:07:19 +08:00
|
|
|
{
|
2021-08-02 02:04:46 +08:00
|
|
|
if (!p->init)
|
|
|
|
p->sem = dispatch_semaphore_create(1), p->init = 1;
|
|
|
|
dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
|
|
|
|
}
|
|
|
|
ST_FUNC void post_sem(TCCSem *p)
|
|
|
|
{
|
|
|
|
dispatch_semaphore_signal(p->sem);
|
2020-05-22 11:07:19 +08:00
|
|
|
}
|
2019-12-11 07:37:18 +08:00
|
|
|
#else
|
2021-08-02 02:04:46 +08:00
|
|
|
ST_FUNC void wait_sem(TCCSem *p)
|
|
|
|
{
|
|
|
|
if (!p->init)
|
|
|
|
sem_init(&p->sem, 0, 1), p->init = 1;
|
|
|
|
while (sem_wait(&p->sem) < 0 && errno == EINTR);
|
|
|
|
}
|
|
|
|
ST_FUNC void post_sem(TCCSem *p)
|
2019-12-11 07:37:18 +08:00
|
|
|
{
|
2021-08-02 02:04:46 +08:00
|
|
|
sem_post(&p->sem);
|
2019-12-11 07:37:18 +08:00
|
|
|
}
|
|
|
|
#endif
|
2021-08-02 02:04:46 +08:00
|
|
|
#endif
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void tcc_enter_state(TCCState *s1)
|
2021-08-02 02:04:46 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->error_set_jmp_enabled)
|
2021-08-02 02:04:46 +08:00
|
|
|
return;
|
|
|
|
WAIT_SEM(&tcc_compile_sem);
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_state = s1;
|
2021-08-02 02:04:46 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void tcc_exit_state(TCCState *s1)
|
2021-08-02 02:04:46 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->error_set_jmp_enabled)
|
2021-08-02 02:04:46 +08:00
|
|
|
return;
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_state = NULL;
|
2021-08-02 02:04:46 +08:00
|
|
|
POST_SEM(&tcc_compile_sem);
|
|
|
|
}
|
2019-12-11 07:37:18 +08:00
|
|
|
|
2009-05-06 02:18:53 +08:00
|
|
|
/********************************************************/
|
|
|
|
/* copy a string and truncate it. */
|
2019-12-02 03:45:42 +08:00
|
|
|
ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
|
2009-05-06 02:18:53 +08: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. */
|
2019-12-02 03:45:42 +08:00
|
|
|
ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2019-12-02 03:45:42 +08:00
|
|
|
size_t len;
|
2009-05-06 02:18:53 +08:00
|
|
|
len = strlen(buf);
|
2013-11-09 03:24:15 +08:00
|
|
|
if (len < buf_size)
|
2009-05-06 02:18:53 +08:00
|
|
|
pstrcpy(buf + len, buf_size - len, s);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-02-09 02:49:28 +08:00
|
|
|
ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
|
2011-08-01 07:10:36 +08:00
|
|
|
{
|
|
|
|
memcpy(out, in, num);
|
|
|
|
out[num] = '\0';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:53 +08:00
|
|
|
/* extract the basename of a file */
|
2009-12-20 08:53:49 +08:00
|
|
|
PUB_FUNC char *tcc_basename(const char *name)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
char *p = strchr(name, 0);
|
2011-08-01 07:10:36 +08:00
|
|
|
while (p > name && !IS_DIRSEP(p[-1]))
|
2009-05-06 02:18:53 +08:00
|
|
|
--p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-06-20 23:17:42 +08:00
|
|
|
/* extract extension part of a file
|
|
|
|
*
|
|
|
|
* (if no extension, return pointer to end-of-string)
|
|
|
|
*/
|
2009-12-20 08:53:49 +08:00
|
|
|
PUB_FUNC char *tcc_fileextension (const char *name)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
char *b = tcc_basename(name);
|
|
|
|
char *e = strrchr(b, '.');
|
|
|
|
return e ? e : strchr(b, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC char *tcc_load_text(int fd)
|
2021-03-30 17:25:58 +08:00
|
|
|
{
|
|
|
|
int len = lseek(fd, 0, SEEK_END);
|
2021-10-22 13:39:54 +08:00
|
|
|
char *buf = load_data(fd, 0, len + 1);
|
2021-03-30 17:25:58 +08:00
|
|
|
buf[len] = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/********************************************************/
|
2009-05-06 02:18:53 +08:00
|
|
|
/* memory management */
|
2009-12-20 08:53:49 +08:00
|
|
|
|
2020-10-24 03:38:53 +08:00
|
|
|
#undef free
|
|
|
|
#undef malloc
|
|
|
|
#undef realloc
|
|
|
|
|
2023-04-15 15:54:23 +08:00
|
|
|
void mem_error(const char *msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:56:39 +08:00
|
|
|
#ifndef MEM_DEBUG
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void tcc_free(void *ptr)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void *tcc_malloc(unsigned long size)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ptr = malloc(size);
|
|
|
|
if (!ptr && size)
|
2023-04-15 15:54:23 +08:00
|
|
|
mem_error("memory full (malloc)");
|
2009-05-06 02:18:53 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void *tcc_mallocz(unsigned long size)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
void *ptr;
|
2021-10-22 13:39:54 +08:00
|
|
|
ptr = tcc_malloc(size);
|
2020-10-17 09:20:33 +08:00
|
|
|
if (size)
|
|
|
|
memset(ptr, 0, size);
|
2009-05-06 02:18:53 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
void *ptr1;
|
|
|
|
ptr1 = realloc(ptr, size);
|
2012-04-19 00:43:55 +08:00
|
|
|
if (!ptr1 && size)
|
2023-04-15 15:54:23 +08:00
|
|
|
mem_error("memory full (realloc)");
|
2009-05-06 02:18:53 +08:00
|
|
|
return ptr1;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC char *tcc_strdup(const char *str)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
char *ptr;
|
2021-10-22 13:39:54 +08:00
|
|
|
ptr = tcc_malloc(strlen(str) + 1);
|
2009-05-06 02:18:53 +08:00
|
|
|
strcpy(ptr, str);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:56:39 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define MEM_DEBUG_MAGIC1 0xFEEDDEB1
|
|
|
|
#define MEM_DEBUG_MAGIC2 0xFEEDDEB2
|
2016-12-19 05:57:03 +08:00
|
|
|
#define MEM_DEBUG_MAGIC3 0xFEEDDEB3
|
|
|
|
#define MEM_DEBUG_FILE_LEN 40
|
|
|
|
#define MEM_DEBUG_CHECK3(header) \
|
2021-01-24 23:20:48 +08:00
|
|
|
((mem_debug_header_t*)((char*)header + header->size))->magic3
|
2016-12-19 05:57:03 +08:00
|
|
|
#define MEM_USER_PTR(header) \
|
2021-01-24 23:20:48 +08:00
|
|
|
((char *)header + offsetof(mem_debug_header_t, magic3))
|
2016-12-19 05:57:03 +08:00
|
|
|
#define MEM_HEADER_PTR(ptr) \
|
2021-01-24 23:20:48 +08:00
|
|
|
(mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
|
2015-05-12 16:56:39 +08:00
|
|
|
|
|
|
|
struct mem_debug_header {
|
2016-12-19 05:05:42 +08:00
|
|
|
unsigned magic1;
|
|
|
|
unsigned size;
|
2015-05-12 16:56:39 +08:00
|
|
|
struct mem_debug_header *prev;
|
|
|
|
struct mem_debug_header *next;
|
2016-12-19 05:05:42 +08:00
|
|
|
int line_num;
|
|
|
|
char file_name[MEM_DEBUG_FILE_LEN + 1];
|
|
|
|
unsigned magic2;
|
2021-01-24 23:20:48 +08:00
|
|
|
ALIGNED(16) unsigned char magic3[4];
|
2015-05-12 16:56:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct mem_debug_header mem_debug_header_t;
|
|
|
|
|
2023-04-15 22:57:02 +08:00
|
|
|
TCC_SEM(static mem_sem);
|
2015-05-12 16:56:39 +08:00
|
|
|
static mem_debug_header_t *mem_debug_chain;
|
2016-12-19 05:05:42 +08:00
|
|
|
static unsigned mem_cur_size;
|
|
|
|
static unsigned mem_max_size;
|
2023-04-15 22:57:02 +08:00
|
|
|
static int nb_states;
|
2015-05-12 16:56:39 +08:00
|
|
|
|
2016-12-19 05:57:03 +08:00
|
|
|
static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
|
|
|
|
{
|
|
|
|
mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
|
|
|
|
if (header->magic1 != MEM_DEBUG_MAGIC1 ||
|
|
|
|
header->magic2 != MEM_DEBUG_MAGIC2 ||
|
2021-01-24 23:20:48 +08:00
|
|
|
read32le(MEM_DEBUG_CHECK3(header)) != MEM_DEBUG_MAGIC3 ||
|
2016-12-19 05:57:03 +08:00
|
|
|
header->size == (unsigned)-1) {
|
|
|
|
fprintf(stderr, "%s check failed\n", msg);
|
|
|
|
if (header->magic1 == MEM_DEBUG_MAGIC1)
|
|
|
|
fprintf(stderr, "%s:%u: block allocated here.\n",
|
|
|
|
header->file_name, header->line_num);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
|
2015-05-12 16:56:39 +08:00
|
|
|
{
|
2016-04-17 21:37:23 +08:00
|
|
|
int ofs;
|
2015-05-12 16:56:39 +08:00
|
|
|
mem_debug_header_t *header;
|
|
|
|
|
2016-12-19 05:57:03 +08:00
|
|
|
header = malloc(sizeof(mem_debug_header_t) + size);
|
|
|
|
if (!header)
|
2023-04-15 15:54:23 +08:00
|
|
|
mem_error("memory full (malloc)");
|
2015-05-12 16:56:39 +08:00
|
|
|
|
|
|
|
header->magic1 = MEM_DEBUG_MAGIC1;
|
|
|
|
header->magic2 = MEM_DEBUG_MAGIC2;
|
|
|
|
header->size = size;
|
2021-01-24 23:20:48 +08:00
|
|
|
write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3);
|
2015-05-12 16:56:39 +08:00
|
|
|
header->line_num = line;
|
2016-04-17 21:37:23 +08:00
|
|
|
ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
|
|
|
|
strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
|
2015-05-12 16:56:39 +08:00
|
|
|
header->file_name[MEM_DEBUG_FILE_LEN] = 0;
|
|
|
|
|
2023-04-15 22:57:02 +08:00
|
|
|
WAIT_SEM(&mem_sem);
|
2015-05-12 16:56:39 +08:00
|
|
|
header->next = mem_debug_chain;
|
|
|
|
header->prev = NULL;
|
|
|
|
if (header->next)
|
|
|
|
header->next->prev = header;
|
|
|
|
mem_debug_chain = header;
|
2016-12-19 05:57:03 +08:00
|
|
|
mem_cur_size += size;
|
|
|
|
if (mem_cur_size > mem_max_size)
|
|
|
|
mem_max_size = mem_cur_size;
|
2023-04-15 22:57:02 +08:00
|
|
|
POST_SEM(&mem_sem);
|
2016-12-19 05:57:03 +08:00
|
|
|
|
|
|
|
return MEM_USER_PTR(header);
|
2015-05-12 16:56:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void tcc_free_debug(void *ptr)
|
2015-05-12 16:56:39 +08:00
|
|
|
{
|
|
|
|
mem_debug_header_t *header;
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
2016-12-19 05:57:03 +08:00
|
|
|
header = malloc_check(ptr, "tcc_free");
|
2023-04-15 22:57:02 +08:00
|
|
|
|
|
|
|
WAIT_SEM(&mem_sem);
|
2015-05-12 16:56:39 +08:00
|
|
|
mem_cur_size -= header->size;
|
2016-12-19 05:05:42 +08:00
|
|
|
header->size = (unsigned)-1;
|
2015-05-12 16:56:39 +08: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;
|
2023-04-15 22:57:02 +08:00
|
|
|
POST_SEM(&mem_sem);
|
2016-12-19 05:57:03 +08:00
|
|
|
free(header);
|
2015-05-12 16:56:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
|
2015-05-12 16:56:39 +08:00
|
|
|
{
|
|
|
|
void *ptr;
|
2021-10-22 13:39:54 +08:00
|
|
|
ptr = tcc_malloc_debug(size,file,line);
|
2015-05-12 16:56:39 +08:00
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
|
2015-05-12 16:56:39 +08:00
|
|
|
{
|
|
|
|
mem_debug_header_t *header;
|
|
|
|
int mem_debug_chain_update = 0;
|
|
|
|
if (!ptr)
|
2021-10-22 13:39:54 +08:00
|
|
|
return tcc_malloc_debug(size, file, line);
|
2016-12-19 05:57:03 +08:00
|
|
|
header = malloc_check(ptr, "tcc_realloc");
|
2023-04-15 22:57:02 +08:00
|
|
|
|
|
|
|
WAIT_SEM(&mem_sem);
|
2016-12-19 05:57:03 +08:00
|
|
|
mem_cur_size -= header->size;
|
|
|
|
mem_debug_chain_update = (header == mem_debug_chain);
|
|
|
|
header = realloc(header, sizeof(mem_debug_header_t) + size);
|
|
|
|
if (!header)
|
2023-04-15 15:54:23 +08:00
|
|
|
mem_error("memory full (realloc)");
|
2015-05-12 16:56:39 +08:00
|
|
|
header->size = size;
|
2021-01-24 23:20:48 +08:00
|
|
|
write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3);
|
2015-05-12 16:56:39 +08:00
|
|
|
if (header->next)
|
|
|
|
header->next->prev = header;
|
|
|
|
if (header->prev)
|
|
|
|
header->prev->next = header;
|
|
|
|
if (mem_debug_chain_update)
|
|
|
|
mem_debug_chain = header;
|
2016-12-19 05:57:03 +08:00
|
|
|
mem_cur_size += size;
|
|
|
|
if (mem_cur_size > mem_max_size)
|
|
|
|
mem_max_size = mem_cur_size;
|
2023-04-15 22:57:02 +08:00
|
|
|
POST_SEM(&mem_sem);
|
|
|
|
|
2016-12-19 05:57:03 +08:00
|
|
|
return MEM_USER_PTR(header);
|
2015-05-12 16:56:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
|
2015-05-12 16:56:39 +08:00
|
|
|
{
|
|
|
|
char *ptr;
|
2021-10-22 13:39:54 +08:00
|
|
|
ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
|
2015-05-12 16:56:39 +08:00
|
|
|
strcpy(ptr, str);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2023-04-15 22:57:02 +08:00
|
|
|
PUB_FUNC void tcc_memcheck(int d)
|
2015-05-12 16:56:39 +08:00
|
|
|
{
|
2023-04-15 22:57:02 +08:00
|
|
|
WAIT_SEM(&mem_sem);
|
|
|
|
nb_states += d;
|
|
|
|
if (0 == nb_states && mem_cur_size) {
|
2015-05-12 16:56:39 +08:00
|
|
|
mem_debug_header_t *header = mem_debug_chain;
|
2023-04-15 22:57:02 +08:00
|
|
|
fflush(stdout);
|
2015-05-12 16:56:39 +08:00
|
|
|
fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
|
|
|
|
mem_cur_size, mem_max_size);
|
|
|
|
while (header) {
|
2016-10-06 00:34:17 +08:00
|
|
|
fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
|
2015-05-12 16:56:39 +08:00
|
|
|
header->file_name, header->line_num, header->size);
|
|
|
|
header = header->next;
|
|
|
|
}
|
2023-04-15 22:57:02 +08:00
|
|
|
fflush(stderr);
|
|
|
|
mem_cur_size = 0;
|
|
|
|
mem_debug_chain = NULL;
|
2016-12-19 05:05:42 +08:00
|
|
|
#if MEM_DEBUG-0 == 2
|
|
|
|
exit(2);
|
|
|
|
#endif
|
2017-02-18 16:55:34 +08:00
|
|
|
}
|
2023-04-15 22:57:02 +08:00
|
|
|
POST_SEM(&mem_sem);
|
2015-05-12 16:56:39 +08:00
|
|
|
}
|
2016-12-19 05:57:03 +08:00
|
|
|
#endif /* MEM_DEBUG */
|
2015-05-12 16:56:39 +08:00
|
|
|
|
2020-10-24 03:38:53 +08: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 08:53:49 +08:00
|
|
|
/********************************************************/
|
|
|
|
/* dynarrays */
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
int nb, nb_alloc;
|
|
|
|
void **pp;
|
2013-11-09 03:24:15 +08:00
|
|
|
|
2009-05-06 02:18:53 +08:00
|
|
|
nb = *nb_ptr;
|
2017-02-14 01:23:43 +08:00
|
|
|
pp = *(void ***)ptab;
|
2009-05-06 02:18:53 +08:00
|
|
|
/* every power of two we double array size */
|
|
|
|
if ((nb & (nb - 1)) == 0) {
|
|
|
|
if (!nb)
|
|
|
|
nb_alloc = 1;
|
|
|
|
else
|
|
|
|
nb_alloc = nb * 2;
|
2021-10-22 13:39:54 +08:00
|
|
|
pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
|
2017-02-14 01:23:43 +08:00
|
|
|
*(void***)ptab = pp;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
pp[nb++] = data;
|
|
|
|
*nb_ptr = nb;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void dynarray_reset(void *pp, int *n)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
void **p;
|
|
|
|
for (p = *(void***)pp; *n; ++p, --*n)
|
|
|
|
if (*p)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(*p);
|
|
|
|
tcc_free(*(void**)pp);
|
2009-05-06 02:18:53 +08:00
|
|
|
*(void**)pp = NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
|
2011-07-08 16:51:26 +08:00
|
|
|
{
|
2011-08-01 07:10:36 +08:00
|
|
|
const char *p;
|
2011-07-08 16:51:26 +08:00
|
|
|
do {
|
2011-08-01 07:10:36 +08:00
|
|
|
int c;
|
|
|
|
CString str;
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_new(&str);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
|
2011-08-11 22:55:30 +08:00
|
|
|
if (c == '{' && p[1] && p[2] == '}') {
|
|
|
|
c = p[1], p += 2;
|
|
|
|
if (c == 'B')
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_cat(&str, s->tcc_lib_path, -1);
|
2022-05-18 04:28:32 +08:00
|
|
|
if (c == 'R')
|
|
|
|
cstr_cat(&str, CONFIG_SYSROOT, -1);
|
2021-10-22 13:39:54 +08:00
|
|
|
if (c == 'f' && file) {
|
2019-12-17 05:34:47 +08:00
|
|
|
/* substitute current file's dir */
|
2021-10-22 13:39:54 +08:00
|
|
|
const char *f = file->true_filename;
|
2019-12-17 05:34:47 +08:00
|
|
|
const char *b = tcc_basename(f);
|
|
|
|
if (b > f)
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_cat(&str, f, b - f - 1);
|
2019-12-17 05:34:47 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_cat(&str, ".", 1);
|
2019-12-17 05:34:47 +08:00
|
|
|
}
|
2011-08-06 22:11:12 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_ccat(&str, c);
|
2011-08-01 07:10:36 +08:00
|
|
|
}
|
2011-07-08 16:51:26 +08:00
|
|
|
}
|
2016-10-03 18:29:38 +08:00
|
|
|
if (str.size) {
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_ccat(&str, '\0');
|
|
|
|
dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
|
2016-10-03 18:29:38 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_free(&str);
|
2011-08-06 22:11:12 +08:00
|
|
|
in = p+1;
|
|
|
|
} while (*p);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/********************************************************/
|
2021-08-02 02:04:46 +08:00
|
|
|
/* warning / error */
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2021-08-02 02:04:46 +08:00
|
|
|
/* warn_... option bits */
|
|
|
|
#define WARN_ON 1 /* warning is on (-Woption) */
|
|
|
|
#define WARN_ERR 2 /* warning is an error (-Werror=option) */
|
|
|
|
#define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2021-08-02 02:04:46 +08:00
|
|
|
/* error1() modes */
|
|
|
|
enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR };
|
2021-08-01 02:44:51 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static void error1(int mode, const char *fmt, va_list ap)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2013-02-14 13:53:07 +08:00
|
|
|
BufferedFile **pf, *f;
|
2021-10-22 13:39:54 +08:00
|
|
|
TCCState *s1 = tcc_state;
|
2021-08-02 02:04:46 +08:00
|
|
|
CString cs;
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_exit_state(s1);
|
2019-12-11 07:37:18 +08:00
|
|
|
|
|
|
|
if (mode == ERROR_WARN) {
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->warn_error)
|
2019-12-11 07:37:18 +08:00
|
|
|
mode = ERROR_ERROR;
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->warn_num) {
|
2021-08-01 02:44:51 +08:00
|
|
|
/* handle tcc_warning_c(warn_option)(fmt, ...) */
|
2021-10-22 13:39:54 +08:00
|
|
|
int wopt = *(&s1->warn_none + s1->warn_num);
|
|
|
|
s1->warn_num = 0;
|
2021-08-01 02:44:51 +08:00
|
|
|
if (0 == (wopt & WARN_ON))
|
|
|
|
return;
|
|
|
|
if (wopt & WARN_ERR)
|
|
|
|
mode = ERROR_ERROR;
|
|
|
|
if (wopt & WARN_NOE)
|
|
|
|
mode = ERROR_WARN;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->warn_none)
|
2021-08-01 02:44:51 +08:00
|
|
|
return;
|
2019-12-11 07:37:18 +08:00
|
|
|
}
|
2013-11-09 03:24:15 +08:00
|
|
|
|
2023-04-15 15:54:23 +08:00
|
|
|
cstr_new(&cs);
|
2020-06-17 14:21:37 +08:00
|
|
|
f = NULL;
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->error_set_jmp_enabled) { /* we're called while parsing a file */
|
2020-06-17 14:21:37 +08:00
|
|
|
/* use upper file if inline ":asm:" or token ":paste:" */
|
2021-10-22 13:39:54 +08:00
|
|
|
for (f = file; f && f->filename[0] == ':'; f = f->prev)
|
2020-06-17 14:21:37 +08:00
|
|
|
;
|
|
|
|
}
|
2013-02-14 13:53:07 +08:00
|
|
|
if (f) {
|
2021-10-22 13:39:54 +08:00
|
|
|
for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
|
|
|
|
cstr_printf(&cs, "In file included from %s:%d:\n",
|
2022-08-20 18:58:56 +08:00
|
|
|
(*pf)->filename, (*pf)->line_num - 1);
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_printf(&cs, "%s:%d: ",
|
|
|
|
f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
|
|
|
|
} else if (s1->current_filename) {
|
|
|
|
cstr_printf(&cs, "%s: ", s1->current_filename);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
2021-08-02 02:04:46 +08:00
|
|
|
if (0 == cs.size)
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_printf(&cs, "tcc: ");
|
|
|
|
cstr_printf(&cs, mode == ERROR_WARN ? "warning: " : "error: ");
|
|
|
|
cstr_vprintf(&cs, fmt, ap);
|
|
|
|
if (!s1 || !s1->error_func) {
|
2009-05-06 02:18:53 +08:00
|
|
|
/* default case: stderr */
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
|
2021-08-02 02:04:46 +08:00
|
|
|
printf("\n"); /* print a newline during tcc -E */
|
2017-02-14 01:23:55 +08:00
|
|
|
fflush(stdout); /* flush -v output */
|
2021-08-02 02:04:46 +08:00
|
|
|
fprintf(stderr, "%s\n", (char*)cs.data);
|
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 20:29:39 +08:00
|
|
|
fflush(stderr); /* print error/warning now (win32) */
|
2009-05-06 02:18:53 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
s1->error_func(s1->error_opaque, (char*)cs.data);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_free(&cs);
|
2023-04-15 15:54:23 +08:00
|
|
|
if (mode != ERROR_WARN)
|
|
|
|
s1->nb_errors++;
|
2023-04-25 03:58:50 +08:00
|
|
|
if (mode == ERROR_ERROR && s1->error_set_jmp_enabled) {
|
|
|
|
while (nb_stk_data)
|
|
|
|
tcc_free(*(void**)stk_data[--nb_stk_data]);
|
2023-04-15 15:54:23 +08:00
|
|
|
longjmp(s1->error_jmp_buf, 1);
|
2023-04-25 03:58:50 +08:00
|
|
|
}
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
s->error_opaque = error_opaque;
|
|
|
|
s->error_func = error_func;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s)
|
2019-10-14 15:36:14 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
return s->error_func;
|
2019-10-14 15:36:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI void *tcc_get_error_opaque(TCCState *s)
|
2019-10-14 15:36:14 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
return s->error_opaque;
|
2019-10-14 15:36:14 +08:00
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:53 +08:00
|
|
|
/* error without aborting current compilation */
|
2023-04-15 15:54:23 +08:00
|
|
|
PUB_FUNC int _tcc_error_noabort(const char *fmt, ...)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2021-10-22 13:39:54 +08:00
|
|
|
error1(ERROR_NOABORT, fmt, ap);
|
2009-05-06 02:18:53 +08:00
|
|
|
va_end(ap);
|
2023-04-15 15:54:23 +08:00
|
|
|
return -1;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2023-04-15 15:54:23 +08:00
|
|
|
#undef _tcc_error
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void _tcc_error(const char *fmt, ...)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2023-04-15 15:54:23 +08:00
|
|
|
error1(ERROR_ERROR, fmt, ap);
|
|
|
|
exit(1);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
2023-04-15 15:54:23 +08:00
|
|
|
#define _tcc_error use_tcc_error_noabort
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void _tcc_warning(const char *fmt, ...)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2021-10-22 13:39:54 +08:00
|
|
|
error1(ERROR_WARN, fmt, ap);
|
2009-05-06 02:18:53 +08:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2023-04-15 15:54:23 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/********************************************************/
|
2009-05-06 02:18:53 +08:00
|
|
|
/* I/O layer */
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
BufferedFile *bf;
|
2010-11-25 20:29:15 +08:00
|
|
|
int buflen = initlen ? initlen : IO_BUF_SIZE;
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
|
2009-05-06 02:18:53 +08:00
|
|
|
bf->buf_ptr = bf->buffer;
|
2010-11-25 20:29:15 +08:00
|
|
|
bf->buf_end = bf->buffer + initlen;
|
|
|
|
bf->buf_end[0] = CH_EOB; /* put eob symbol */
|
2009-05-06 02:18:53 +08:00
|
|
|
pstrcpy(bf->filename, sizeof(bf->filename), filename);
|
2019-12-11 07:37:18 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
normalize_slashes(bf->filename);
|
|
|
|
#endif
|
2017-07-09 18:07:40 +08:00
|
|
|
bf->true_filename = bf->filename;
|
2009-05-06 02:18:53 +08:00
|
|
|
bf->line_num = 1;
|
2021-10-22 13:39:54 +08:00
|
|
|
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
2013-01-15 00:34:07 +08:00
|
|
|
bf->fd = -1;
|
2021-10-22 13:39:54 +08:00
|
|
|
bf->prev = file;
|
|
|
|
file = bf;
|
|
|
|
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void tcc_close(void)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
BufferedFile *bf = file;
|
2013-01-15 00:34:07 +08:00
|
|
|
if (bf->fd > 0) {
|
|
|
|
close(bf->fd);
|
2023-04-01 00:20:49 +08:00
|
|
|
total_lines += bf->line_num - 1;
|
2010-11-25 20:29:15 +08:00
|
|
|
}
|
2017-07-09 18:07:40 +08:00
|
|
|
if (bf->true_filename != bf->filename)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(bf->true_filename);
|
|
|
|
file = bf->prev;
|
|
|
|
tcc_free(bf);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static int _tcc_open(TCCState *s1, const char *filename)
|
2010-11-25 20:29:15 +08:00
|
|
|
{
|
2013-01-15 00:34:07 +08: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 20:29:39 +08:00
|
|
|
fd = 0, filename = "<stdin>";
|
2010-11-25 20:29:15 +08:00
|
|
|
else
|
2013-01-15 00:34:07 +08:00
|
|
|
fd = open(filename, O_RDONLY | O_BINARY);
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
|
2013-01-15 00:34:07 +08:00
|
|
|
printf("%s %*s%s\n", fd < 0 ? "nf":"->",
|
2021-10-22 13:39:54 +08:00
|
|
|
(int)(s1->include_stack_ptr - s1->include_stack), "", filename);
|
2019-12-11 07:37:18 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int tcc_open(TCCState *s1, const char *filename)
|
2019-12-11 07:37:18 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
int fd = _tcc_open(s1, filename);
|
2013-01-15 00:34:07 +08:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_open_bf(s1, filename, 0);
|
|
|
|
file->fd = fd;
|
2019-12-11 07:37:18 +08:00
|
|
|
return 0;
|
2010-11-25 20:29:15 +08:00
|
|
|
}
|
|
|
|
|
2017-07-21 04:21:27 +08:00
|
|
|
/* compile the file opened in 'file'. Return non zero if errors. */
|
2021-10-22 13:39:54 +08:00
|
|
|
static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2019-12-11 07:37:18 +08:00
|
|
|
/* Here we enter the code section where we use the global variables for
|
|
|
|
parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
|
|
|
|
Other threads need to wait until we're done.
|
|
|
|
|
|
|
|
Alternatively we could use thread local storage for those global
|
|
|
|
variables, which may or may not have advantages */
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_enter_state(s1);
|
|
|
|
s1->error_set_jmp_enabled = 1;
|
2019-12-11 07:37:18 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
if (setjmp(s1->error_jmp_buf) == 0) {
|
|
|
|
s1->nb_errors = 0;
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2019-12-11 07:37:18 +08:00
|
|
|
if (fd == -1) {
|
|
|
|
int len = strlen(str);
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_open_bf(s1, "<string>", len);
|
|
|
|
memcpy(file->buffer, str, len);
|
2019-12-11 07:37:18 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_open_bf(s1, str, 0);
|
|
|
|
file->fd = fd;
|
2019-12-11 07:37:18 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
preprocess_start(s1, filetype);
|
|
|
|
tccgen_init(s1);
|
2022-08-20 18:58:56 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
|
|
|
|
tcc_preprocess(s1);
|
2017-07-21 04:21:27 +08:00
|
|
|
} else {
|
2022-08-20 18:58:56 +08:00
|
|
|
tccelf_begin_file(s1);
|
|
|
|
if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {
|
|
|
|
tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
|
|
|
|
} else {
|
|
|
|
tccgen_compile(s1);
|
|
|
|
}
|
|
|
|
tccelf_end_file(s1);
|
2017-07-21 04:21:27 +08:00
|
|
|
}
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
tccgen_finish(s1);
|
|
|
|
preprocess_end(s1);
|
|
|
|
s1->error_set_jmp_enabled = 0;
|
|
|
|
tcc_exit_state(s1);
|
|
|
|
return s1->nb_errors != 0 ? -1 : 0;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
return tcc_compile(s, s->filetype, str, -1);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 23:02:12 +08:00
|
|
|
/* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2020-06-27 23:02:12 +08:00
|
|
|
const char *eq;
|
|
|
|
if (NULL == (eq = strchr(sym, '=')))
|
|
|
|
eq = strchr(sym, 0);
|
|
|
|
if (NULL == value)
|
|
|
|
value = *eq ? eq + 1 : "1";
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_printf(&s1->cmdline_defs, "#define %.*s %s\n", (int)(eq-sym), sym, value);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* undefine a preprocessor symbol */
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
LIBTCCAPI TCCState *tcc_new(void)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
TCCState *s;
|
2016-10-18 05:24:01 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
s = tcc_mallocz(sizeof(TCCState));
|
2009-05-06 02:18:53 +08:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2019-12-14 19:31:03 +08:00
|
|
|
#ifdef MEM_DEBUG
|
2023-04-15 22:57:02 +08:00
|
|
|
tcc_memcheck(1);
|
2019-12-14 19:31:03 +08:00
|
|
|
#endif
|
2016-10-15 21:55:31 +08:00
|
|
|
|
2019-12-11 07:37:18 +08:00
|
|
|
#undef gnu_ext
|
|
|
|
|
|
|
|
s->gnu_ext = 1;
|
|
|
|
s->tcc_ext = 1;
|
2016-10-15 21:55:31 +08:00
|
|
|
s->nocommon = 1;
|
2019-06-11 19:45:22 +08:00
|
|
|
s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
|
2019-01-10 21:41:54 +08:00
|
|
|
s->cversion = 199901; /* default unless -std=c11 is supplied */
|
2021-08-01 02:44:51 +08:00
|
|
|
s->warn_implicit_function_declaration = 1;
|
|
|
|
s->warn_discarded_qualifiers = 1;
|
2017-07-09 18:07:40 +08:00
|
|
|
s->ms_extensions = 1;
|
2016-10-15 21:55:31 +08:00
|
|
|
|
|
|
|
#ifdef CHAR_IS_UNSIGNED
|
|
|
|
s->char_is_unsigned = 1;
|
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
s->seg_size = 32;
|
|
|
|
#endif
|
|
|
|
/* enable this if you want symbols with leading underscore on windows: */
|
2020-05-18 11:15:08 +08:00
|
|
|
#if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
|
2016-10-15 21:55:31 +08:00
|
|
|
s->leading_underscore = 1;
|
2019-12-11 07:37:18 +08:00
|
|
|
#endif
|
2020-12-18 07:33:44 +08:00
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
s->float_abi = ARM_FLOAT_ABI;
|
|
|
|
#endif
|
2022-05-18 04:28:32 +08:00
|
|
|
#ifdef CONFIG_NEW_DTAGS
|
|
|
|
s->enable_new_dtags = 1;
|
|
|
|
#endif
|
2019-12-11 07:37:18 +08:00
|
|
|
s->ppfp = stdout;
|
|
|
|
/* might be used in error() before preprocess_start() */
|
|
|
|
s->include_stack_ptr = s->include_stack;
|
|
|
|
|
2009-07-19 04:07:33 +08:00
|
|
|
tcc_set_lib_path(s, CONFIG_TCCDIR);
|
2009-05-06 02:18:53 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI void tcc_delete(TCCState *s1)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2016-10-15 21:55:31 +08:00
|
|
|
/* free sections */
|
2021-10-22 13:39:54 +08:00
|
|
|
tccelf_delete(s1);
|
2009-05-06 02:18:53 +08:00
|
|
|
|
|
|
|
/* free library paths */
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
|
|
|
|
dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
|
2009-05-06 02:18:53 +08:00
|
|
|
|
|
|
|
/* free include paths */
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
|
|
|
|
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
|
|
|
|
|
|
|
|
tcc_free(s1->tcc_lib_path);
|
|
|
|
tcc_free(s1->soname);
|
|
|
|
tcc_free(s1->rpath);
|
2022-05-08 05:16:13 +08:00
|
|
|
tcc_free(s1->elf_entryname);
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(s1->init_symbol);
|
|
|
|
tcc_free(s1->fini_symbol);
|
2022-07-28 23:33:51 +08:00
|
|
|
tcc_free(s1->mapfile);
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(s1->outfile);
|
|
|
|
tcc_free(s1->deps_outfile);
|
2022-12-20 14:24:48 +08:00
|
|
|
#if defined TCC_TARGET_MACHO
|
|
|
|
tcc_free(s1->install_name);
|
|
|
|
#endif
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_reset(&s1->files, &s1->nb_files);
|
|
|
|
dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
|
|
|
|
dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
|
|
|
|
dynarray_reset(&s1->argv, &s1->argc);
|
|
|
|
cstr_free(&s1->cmdline_defs);
|
|
|
|
cstr_free(&s1->cmdline_incl);
|
2023-04-15 15:54:23 +08:00
|
|
|
cstr_free(&s1->linker_arg);
|
2013-02-13 02:13:28 +08:00
|
|
|
#ifdef TCC_IS_NATIVE
|
2016-10-20 01:21:27 +08:00
|
|
|
/* free runtime memory */
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_run_free(s1);
|
2010-04-24 18:28:54 +08:00
|
|
|
#endif
|
2022-05-09 23:02:09 +08:00
|
|
|
tcc_free(s1->dState);
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(s1);
|
2019-12-14 19:31:03 +08:00
|
|
|
#ifdef MEM_DEBUG
|
2023-04-15 22:57:02 +08:00
|
|
|
tcc_memcheck(-1);
|
2019-12-14 19:31:03 +08:00
|
|
|
#endif
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
|
2016-10-15 21:55:31 +08:00
|
|
|
{
|
2022-05-29 03:00:40 +08:00
|
|
|
#ifdef CONFIG_TCC_PIE
|
|
|
|
if (output_type == TCC_OUTPUT_EXE)
|
|
|
|
output_type |= TCC_OUTPUT_DYN;
|
|
|
|
#endif
|
2021-10-22 13:39:54 +08:00
|
|
|
s->output_type = output_type;
|
2016-10-15 21:55:31 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
if (!s->nostdinc) {
|
2016-10-15 21:55:31 +08:00
|
|
|
/* default include paths */
|
|
|
|
/* -isystem paths have already been handled */
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
2022-05-29 03:00:40 +08:00
|
|
|
|
2022-08-20 18:58:56 +08:00
|
|
|
if (output_type == TCC_OUTPUT_PREPROCESS) {
|
|
|
|
s->do_debug = 0;
|
2022-05-29 03:00:40 +08:00
|
|
|
return 0;
|
2022-08-20 18:58:56 +08:00
|
|
|
}
|
2022-05-29 03:00:40 +08:00
|
|
|
|
2022-08-20 18:58:56 +08:00
|
|
|
tccelf_new(s);
|
|
|
|
if (s->do_debug) {
|
|
|
|
/* add debug sections */
|
|
|
|
tcc_debug_new(s);
|
|
|
|
}
|
2016-10-15 21:55:31 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s->do_bounds_check) {
|
2016-10-18 05:24:01 +08:00
|
|
|
/* if bound checking, then add corresponding sections */
|
2021-10-22 13:39:54 +08:00
|
|
|
tccelf_bounds_new(s);
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
|
|
|
#endif
|
2022-08-20 18:58:56 +08:00
|
|
|
|
2021-07-26 02:39:11 +08:00
|
|
|
if (output_type == TCC_OUTPUT_OBJ) {
|
|
|
|
/* always elf for objects */
|
2021-10-22 13:39:54 +08:00
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_ELF;
|
2021-07-26 02:39:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-10-15 21:55:31 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
|
2021-07-26 02:39:11 +08:00
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
# ifdef _WIN32
|
2021-07-26 02:39:11 +08:00
|
|
|
/* allow linking with system dll's directly */
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_systemdir(s);
|
2021-07-26 02:39:11 +08:00
|
|
|
# endif
|
|
|
|
/* target PE has its own startup code in libtcc1.a */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#elif defined TCC_TARGET_MACHO
|
|
|
|
# ifdef TCC_IS_NATIVE
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_macos_sdkpath(s);
|
2016-10-15 21:55:31 +08:00
|
|
|
# endif
|
2021-07-26 02:39:11 +08:00
|
|
|
/* Mach-O with LC_MAIN doesn't need any crt startup code. */
|
|
|
|
return 0;
|
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
#else
|
|
|
|
/* paths for crt objects */
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
|
2022-05-29 03:00:40 +08:00
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
/* add libc crt1/crti objects */
|
2022-05-29 03:00:40 +08:00
|
|
|
if (output_type != TCC_OUTPUT_MEMORY && !s->nostdlib) {
|
2020-12-17 03:08:43 +08:00
|
|
|
#if TARGETOS_OpenBSD
|
2020-11-29 15:47:27 +08:00
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crt0.o");
|
2022-08-22 00:37:09 +08:00
|
|
|
if (output_type == TCC_OUTPUT_DLL)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crtbeginS.o");
|
2021-01-10 00:36:13 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crtbegin.o");
|
2020-12-17 03:08:43 +08:00
|
|
|
#elif TARGETOS_FreeBSD
|
2020-12-08 16:13:29 +08:00
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crt1.o");
|
|
|
|
tcc_add_crt(s, "crti.o");
|
|
|
|
if (s->static_link)
|
|
|
|
tcc_add_crt(s, "crtbeginT.o");
|
2022-05-29 03:00:40 +08:00
|
|
|
else if (output_type & TCC_OUTPUT_DYN)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crtbeginS.o");
|
2021-01-10 00:36:13 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crtbegin.o");
|
2020-12-17 03:08:43 +08:00
|
|
|
#elif TARGETOS_NetBSD
|
2020-12-11 21:26:26 +08:00
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crt0.o");
|
|
|
|
tcc_add_crt(s, "crti.o");
|
|
|
|
if (s->static_link)
|
|
|
|
tcc_add_crt(s, "crtbeginT.o");
|
2022-05-29 03:00:40 +08:00
|
|
|
else if (output_type & TCC_OUTPUT_DYN)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crtbeginS.o");
|
2020-12-20 03:55:52 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crtbegin.o");
|
2022-05-18 04:28:32 +08:00
|
|
|
#elif defined TARGETOS_ANDROID
|
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
|
|
|
tcc_add_crt(s, "crtbegin_dynamic.o");
|
|
|
|
else
|
|
|
|
tcc_add_crt(s, "crtbegin_so.o");
|
2021-01-24 23:20:48 +08:00
|
|
|
#else
|
2016-10-15 21:55:31 +08:00
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_crt(s, "crt1.o");
|
|
|
|
tcc_add_crt(s, "crti.o");
|
2020-05-18 11:20:31 +08:00
|
|
|
#endif
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
|
|
|
return 0;
|
2021-07-26 02:39:11 +08:00
|
|
|
#endif
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
|
2009-05-06 02:18:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
|
2009-05-06 02:18:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-29 02:47:48 +08:00
|
|
|
/* add/update a 'DLLReference', Just find if level == -1 */
|
|
|
|
ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level)
|
2020-12-17 03:08:43 +08:00
|
|
|
{
|
2022-05-29 02:47:48 +08:00
|
|
|
DLLReference *ref = NULL;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s1->nb_loaded_dlls; i++)
|
|
|
|
if (0 == strcmp(s1->loaded_dlls[i]->name, dllname)) {
|
|
|
|
ref = s1->loaded_dlls[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (level == -1)
|
|
|
|
return ref;
|
|
|
|
if (ref) {
|
|
|
|
if (level < ref->level)
|
|
|
|
ref->level = level;
|
|
|
|
ref->found = 1;
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));
|
2020-12-17 03:08:43 +08:00
|
|
|
strcpy(ref->name, dllname);
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref);
|
2022-05-29 02:47:48 +08:00
|
|
|
ref->level = level;
|
|
|
|
ref->index = s1->nb_loaded_dlls;
|
2020-12-17 03:08:43 +08:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2021-01-24 23:20:48 +08:00
|
|
|
/* OpenBSD: choose latest from libxxx.so.x.y versions */
|
|
|
|
#if defined TARGETOS_OpenBSD && !defined _WIN32
|
|
|
|
#include <glob.h>
|
2021-10-22 13:39:54 +08:00
|
|
|
static int tcc_glob_so(TCCState *s1, const char *pattern, char *buf, int size)
|
2021-01-24 23:20:48 +08:00
|
|
|
{
|
|
|
|
const char *star;
|
|
|
|
glob_t g;
|
|
|
|
char *p;
|
|
|
|
int i, v, v1, v2, v3;
|
|
|
|
|
|
|
|
star = strchr(pattern, '*');
|
|
|
|
if (!star || glob(pattern, 0, NULL, &g))
|
|
|
|
return -1;
|
|
|
|
for (v = -1, i = 0; i < g.gl_pathc; ++i) {
|
|
|
|
p = g.gl_pathv[i];
|
|
|
|
if (2 != sscanf(p + (star - pattern), "%d.%d.%d", &v1, &v2, &v3))
|
|
|
|
continue;
|
|
|
|
if ((v1 = v1 * 1000 + v2) > v)
|
|
|
|
v = v1, pstrcpy(buf, size, p);
|
|
|
|
}
|
|
|
|
globfree(&g);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2020-12-17 03:08:43 +08:00
|
|
|
int fd, ret = -1;
|
2009-07-19 04:07:25 +08:00
|
|
|
|
2021-01-24 23:20:48 +08:00
|
|
|
#if defined TARGETOS_OpenBSD && !defined _WIN32
|
|
|
|
char buf[1024];
|
2021-10-22 13:39:54 +08:00
|
|
|
if (tcc_glob_so(s1, filename, buf, sizeof buf) >= 0)
|
2021-01-24 23:20:48 +08:00
|
|
|
filename = buf;
|
|
|
|
#endif
|
|
|
|
|
2022-10-09 07:40:18 +08:00
|
|
|
/* ignore binary files with -E */
|
|
|
|
if (s1->output_type == TCC_OUTPUT_PREPROCESS
|
|
|
|
&& (flags & AFF_TYPE_BIN))
|
|
|
|
return 0;
|
|
|
|
|
2009-05-06 02:18:53 +08:00
|
|
|
/* open the file */
|
2021-10-22 13:39:54 +08:00
|
|
|
fd = _tcc_open(s1, filename);
|
2019-12-11 07:37:18 +08:00
|
|
|
if (fd < 0) {
|
2009-07-19 04:07:25 +08:00
|
|
|
if (flags & AFF_PRINT_ERROR)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_error_noabort("file '%s' not found", filename);
|
2020-12-17 03:08:43 +08:00
|
|
|
return ret;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
s1->current_filename = filename;
|
2017-07-21 04:21:27 +08:00
|
|
|
if (flags & AFF_TYPE_BIN) {
|
2016-10-02 02:54:45 +08:00
|
|
|
ElfW(Ehdr) ehdr;
|
2019-12-11 07:37:18 +08:00
|
|
|
int obj_type;
|
2009-05-06 02:18:53 +08:00
|
|
|
|
2016-10-02 02:54:45 +08:00
|
|
|
obj_type = tcc_object_type(fd, &ehdr);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2009-07-19 04:07:25 +08:00
|
|
|
|
2016-10-02 02:54:45 +08:00
|
|
|
switch (obj_type) {
|
2020-12-17 03:08:43 +08:00
|
|
|
|
2016-10-02 02:54:45 +08:00
|
|
|
case AFF_BINTYPE_REL:
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = tcc_load_object_file(s1, fd, 0);
|
2016-10-02 02:54:45 +08:00
|
|
|
break;
|
2020-12-17 03:08:43 +08:00
|
|
|
|
|
|
|
case AFF_BINTYPE_AR:
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
|
2020-12-17 03:08:43 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
default:
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = pe_load_file(s1, fd, filename);
|
2021-07-26 02:39:11 +08:00
|
|
|
goto check_success;
|
|
|
|
|
|
|
|
#elif defined TCC_TARGET_MACHO
|
2016-10-02 02:54:45 +08:00
|
|
|
case AFF_BINTYPE_DYN:
|
2021-07-26 02:39:11 +08:00
|
|
|
case_dyn_or_tbd:
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->output_type == TCC_OUTPUT_MEMORY) {
|
2013-02-07 02:01:07 +08:00
|
|
|
#ifdef TCC_IS_NATIVE
|
2021-07-06 21:59:12 +08:00
|
|
|
void* dl;
|
|
|
|
const char* soname = filename;
|
2021-07-26 02:39:11 +08:00
|
|
|
if (obj_type != AFF_BINTYPE_DYN)
|
2021-07-04 17:07:48 +08:00
|
|
|
soname = macho_tbd_soname(filename);
|
2021-07-06 21:59:12 +08:00
|
|
|
dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY);
|
2021-07-26 02:39:11 +08:00
|
|
|
if (dl)
|
2022-05-29 02:47:48 +08:00
|
|
|
tcc_add_dllref(s1, soname, 0)->handle = dl, ret = 0;
|
2021-07-26 02:39:11 +08:00
|
|
|
if (filename != soname)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free((void *)soname);
|
2021-07-07 16:05:23 +08:00
|
|
|
#endif
|
2021-07-26 02:39:11 +08:00
|
|
|
} else if (obj_type == AFF_BINTYPE_DYN) {
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = macho_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
|
2021-07-26 02:39:11 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
2016-10-02 02:54:45 +08:00
|
|
|
break;
|
2021-07-26 02:39:11 +08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
const char *ext = tcc_fileextension(filename);
|
|
|
|
if (!strcmp(ext, ".tbd"))
|
|
|
|
goto case_dyn_or_tbd;
|
|
|
|
if (!strcmp(ext, ".dylib")) {
|
|
|
|
obj_type = AFF_BINTYPE_DYN;
|
|
|
|
goto case_dyn_or_tbd;
|
|
|
|
}
|
|
|
|
goto check_success;
|
|
|
|
}
|
2020-12-17 03:08:43 +08:00
|
|
|
|
2021-07-26 02:39:11 +08:00
|
|
|
#else /* unix */
|
|
|
|
case AFF_BINTYPE_DYN:
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->output_type == TCC_OUTPUT_MEMORY) {
|
2021-07-26 02:39:11 +08:00
|
|
|
#ifdef TCC_IS_NATIVE
|
|
|
|
void* dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
|
|
|
|
if (dl)
|
2022-05-29 02:47:48 +08:00
|
|
|
tcc_add_dllref(s1, filename, 0)->handle = dl, ret = 0;
|
2009-05-06 02:18:53 +08:00
|
|
|
#endif
|
2021-07-26 02:39:11 +08:00
|
|
|
} else
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = tcc_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);
|
2021-07-26 02:39:11 +08:00
|
|
|
break;
|
|
|
|
|
2016-10-02 02:54:45 +08:00
|
|
|
default:
|
|
|
|
/* as GNU ld, consider it is an ld script if not recognized */
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = tcc_load_ldscript(s1, fd);
|
2021-07-26 02:39:11 +08:00
|
|
|
goto check_success;
|
2020-12-17 03:08:43 +08:00
|
|
|
|
2021-07-26 02:39:11 +08:00
|
|
|
#endif /* pe / macos / unix */
|
|
|
|
|
|
|
|
check_success:
|
2016-10-02 02:54:45 +08:00
|
|
|
if (ret < 0)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_error_noabort("%s: unrecognized file type", filename);
|
2016-10-02 02:54:45 +08:00
|
|
|
break;
|
2021-07-26 02:39:11 +08:00
|
|
|
|
|
|
|
#ifdef TCC_TARGET_COFF
|
|
|
|
case AFF_BINTYPE_C67:
|
2021-10-22 13:39:54 +08:00
|
|
|
ret = tcc_load_coff(s1, fd);
|
2021-07-26 02:39:11 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2016-10-02 02:54:45 +08:00
|
|
|
}
|
2019-12-11 07:37:18 +08:00
|
|
|
close(fd);
|
2017-07-21 04:21:27 +08:00
|
|
|
} else {
|
2020-06-17 14:21:37 +08:00
|
|
|
/* update target deps */
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename));
|
|
|
|
ret = tcc_compile(s1, flags, filename, fd);
|
2016-10-02 02:54:45 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
s1->current_filename = NULL;
|
2009-05-06 02:18:53 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
int filetype = s->filetype;
|
2018-06-01 05:51:59 +08:00
|
|
|
if (0 == (filetype & AFF_TYPE_MASK)) {
|
2017-07-21 04:21:27 +08:00
|
|
|
/* use a file extension to detect a filetype */
|
|
|
|
const char *ext = tcc_fileextension(filename);
|
|
|
|
if (ext[0]) {
|
|
|
|
ext++;
|
|
|
|
if (!strcmp(ext, "S"))
|
|
|
|
filetype = AFF_TYPE_ASMPP;
|
|
|
|
else if (!strcmp(ext, "s"))
|
|
|
|
filetype = AFF_TYPE_ASM;
|
2021-06-28 19:07:29 +08:00
|
|
|
else if (!PATHCMP(ext, "c")
|
|
|
|
|| !PATHCMP(ext, "h")
|
|
|
|
|| !PATHCMP(ext, "i"))
|
2017-07-21 04:21:27 +08:00
|
|
|
filetype = AFF_TYPE_C;
|
|
|
|
else
|
2018-06-01 05:51:59 +08:00
|
|
|
filetype |= AFF_TYPE_BIN;
|
2017-07-21 04:21:27 +08:00
|
|
|
} else {
|
|
|
|
filetype = AFF_TYPE_C;
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
|
2009-05-06 02:18:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static int tcc_add_library_internal(TCCState *s, const char *fmt,
|
2021-12-16 00:45:16 +08:00
|
|
|
const char *filename, int flags, char **paths, int nb_paths)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int i;
|
|
|
|
|
2011-08-06 22:49:30 +08:00
|
|
|
for(i = 0; i < nb_paths; i++) {
|
|
|
|
snprintf(buf, sizeof(buf), fmt, paths[i], filename);
|
2021-10-22 13:39:54 +08:00
|
|
|
if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
|
2009-05-06 02:18:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-06 22:49:30 +08:00
|
|
|
/* find and load a dll. Return non zero if not found */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
|
2011-08-06 22:49:30 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
return tcc_add_library_internal(s, "%s/%s", filename, flags,
|
2021-12-16 00:45:16 +08:00
|
|
|
s->library_paths, s->nb_library_paths);
|
2011-08-06 22:49:30 +08:00
|
|
|
}
|
2022-07-23 23:19:56 +08:00
|
|
|
|
|
|
|
/* find [cross-]libtcc1.a and tcc helper objects in library path */
|
|
|
|
ST_FUNC void tcc_add_support(TCCState *s1, const char *filename)
|
|
|
|
{
|
|
|
|
char buf[100];
|
2022-08-20 18:58:56 +08:00
|
|
|
if (CONFIG_TCC_CROSSPREFIX[0])
|
|
|
|
filename = strcat(strcpy(buf, CONFIG_TCC_CROSSPREFIX), filename);
|
2022-07-23 23:19:56 +08:00
|
|
|
if (tcc_add_dll(s1, filename, 0) < 0)
|
|
|
|
tcc_error_noabort("%s not found", filename);
|
|
|
|
}
|
2011-08-06 22:49:30 +08:00
|
|
|
|
2020-06-27 23:15:06 +08:00
|
|
|
#if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename)
|
2011-08-06 22:49:30 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
if (-1 == tcc_add_library_internal(s1, "%s/%s",
|
2021-12-16 00:45:16 +08:00
|
|
|
filename, 0, s1->crt_paths, s1->nb_crt_paths))
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("file '%s' not found", filename);
|
2011-08-06 22:49:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-08 21:31:40 +08:00
|
|
|
#endif
|
2011-08-06 22:49:30 +08:00
|
|
|
|
2009-05-06 02:18:53 +08:00
|
|
|
/* the library name is the same as the argument of the '-l' option */
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2017-04-26 03:01:54 +08:00
|
|
|
#if defined TCC_TARGET_PE
|
2021-01-24 23:20:48 +08:00
|
|
|
static const char * const libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
|
2021-10-22 13:39:54 +08:00
|
|
|
const char * const *pp = s->static_link ? libs + 4 : libs;
|
2017-04-26 03:01:54 +08:00
|
|
|
#elif defined TCC_TARGET_MACHO
|
2021-07-04 17:06:55 +08:00
|
|
|
static const char * const libs[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL };
|
2021-10-22 13:39:54 +08:00
|
|
|
const char * const *pp = s->static_link ? libs + 2 : libs;
|
2021-01-24 23:20:48 +08:00
|
|
|
#elif defined TARGETOS_OpenBSD
|
|
|
|
static const char * const libs[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL };
|
2021-10-22 13:39:54 +08:00
|
|
|
const char * const *pp = s->static_link ? libs + 1 : libs;
|
2009-05-06 02:18:53 +08:00
|
|
|
#else
|
2021-01-24 23:20:48 +08:00
|
|
|
static const char * const libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
|
2021-10-22 13:39:54 +08:00
|
|
|
const char * const *pp = s->static_link ? libs + 1 : libs;
|
2009-07-19 04:07:25 +08:00
|
|
|
#endif
|
2021-10-22 13:39:54 +08:00
|
|
|
int flags = s->filetype & AFF_WHOLE_ARCHIVE;
|
2011-08-06 22:49:30 +08:00
|
|
|
while (*pp) {
|
2021-10-22 13:39:54 +08:00
|
|
|
if (0 == tcc_add_library_internal(s, *pp,
|
2021-12-16 00:45:16 +08:00
|
|
|
libraryname, flags, s->library_paths, s->nb_library_paths))
|
2009-05-06 02:18:53 +08:00
|
|
|
return 0;
|
2011-08-06 22:49:30 +08:00
|
|
|
++pp;
|
2009-05-06 02:18:53 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
|
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 20:29:39 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
int ret = tcc_add_library(s1, libname);
|
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 20:29:39 +08:00
|
|
|
if (ret < 0)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_error_noabort("library '%s' not found", libname);
|
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 20:29:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-08 12:38:09 +08:00
|
|
|
/* handle #pragma comment(lib,) */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
|
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 20:29:39 +08:00
|
|
|
{
|
|
|
|
int i;
|
2021-10-22 13:39:54 +08:00
|
|
|
for (i = 0; i < s1->nb_pragma_libs; i++)
|
|
|
|
tcc_add_library_err(s1, s1->pragma_libs[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 20:29:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
|
2009-05-06 02:18:53 +08:00
|
|
|
{
|
2010-01-15 03:55:51 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2013-02-13 02:13:28 +08: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. */
|
2021-10-22 13:39:54 +08:00
|
|
|
pe_putimport(s1, 0, name, (uintptr_t)val);
|
2010-01-15 03:55:51 +08:00
|
|
|
#else
|
2020-06-27 23:15:06 +08:00
|
|
|
char buf[256];
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s1->leading_underscore) {
|
2020-06-27 23:15:06 +08:00
|
|
|
buf[0] = '_';
|
|
|
|
pstrcpy(buf + 1, sizeof(buf) - 1, name);
|
|
|
|
name = buf;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
set_global_sym(s1, name, NULL, (addr_t)(uintptr_t)val); /* NULL: SHN_ABS */
|
2010-01-15 03:55:51 +08:00
|
|
|
#endif
|
2009-05-06 02:18:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
|
2013-02-13 02:13:28 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(s->tcc_lib_path);
|
|
|
|
s->tcc_lib_path = tcc_strdup(path);
|
2013-02-13 02:13:28 +08:00
|
|
|
}
|
|
|
|
|
2021-08-01 02:44:51 +08:00
|
|
|
/********************************************************/
|
|
|
|
/* options parser */
|
2021-07-27 06:36:45 +08:00
|
|
|
|
2013-02-13 02:13:28 +08:00
|
|
|
static int strstart(const char *val, const char **str)
|
2010-04-06 03:21:58 +08:00
|
|
|
{
|
|
|
|
const char *p, *q;
|
2013-02-13 02:13:28 +08:00
|
|
|
p = *str;
|
2010-04-06 03:21:58 +08:00
|
|
|
q = val;
|
2013-02-13 02:13:28 +08:00
|
|
|
while (*q) {
|
2010-04-06 03:21:58 +08:00
|
|
|
if (*p != *q)
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
2013-02-13 02:13:28 +08:00
|
|
|
*str = p;
|
2010-04-06 03:21:58 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 03:30:21 +08: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-13 02:13:28 +08:00
|
|
|
static int link_option(const char *str, const char *val, const char **ptr)
|
2010-08-13 03:30:21 +08:00
|
|
|
{
|
|
|
|
const char *p, *q;
|
2016-10-02 02:54:45 +08:00
|
|
|
int ret;
|
2010-08-13 03:30:21 +08:00
|
|
|
|
|
|
|
/* there should be 1 or 2 dashes */
|
|
|
|
if (*str++ != '-')
|
|
|
|
return 0;
|
|
|
|
if (*str == '-')
|
|
|
|
str++;
|
|
|
|
|
2017-05-08 12:38:09 +08:00
|
|
|
/* then str & val should match (potentially up to '=') */
|
2010-08-13 03:30:21 +08:00
|
|
|
p = str;
|
|
|
|
q = val;
|
|
|
|
|
2016-10-02 02:54:45 +08:00
|
|
|
ret = 1;
|
|
|
|
if (q[0] == '?') {
|
|
|
|
++q;
|
2021-08-01 02:44:51 +08:00
|
|
|
if (strstart("no-", &p))
|
2016-10-02 02:54:45 +08:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 03:30:21 +08:00
|
|
|
while (*q != '\0' && *q != '=') {
|
|
|
|
if (*p != *q)
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* '=' near eos means ',' or '=' is ok */
|
|
|
|
if (*q == '=') {
|
2016-10-02 02:49:38 +08:00
|
|
|
if (*p == 0)
|
|
|
|
*ptr = p;
|
2010-08-13 03:30:21 +08:00
|
|
|
if (*p != ',' && *p != '=')
|
|
|
|
return 0;
|
|
|
|
p++;
|
2017-04-04 14:34:52 +08:00
|
|
|
} else if (*p) {
|
|
|
|
return 0;
|
2010-08-13 03:30:21 +08:00
|
|
|
}
|
2016-10-02 02:49:38 +08:00
|
|
|
*ptr = p;
|
2016-10-02 02:54:45 +08:00
|
|
|
return ret;
|
2010-08-13 03:30:21 +08:00
|
|
|
}
|
|
|
|
|
2013-02-13 02:13:28 +08: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-13 03:30:21 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static void copy_linker_arg(char **pp, const char *s, int sep)
|
2010-04-06 03:21:58 +08:00
|
|
|
{
|
2017-02-11 16:54:01 +08:00
|
|
|
const char *q = s;
|
|
|
|
char *p = *pp;
|
|
|
|
int l = 0;
|
|
|
|
if (p && sep)
|
|
|
|
p[l = strlen(p)] = sep, ++l;
|
2013-02-13 02:13:28 +08:00
|
|
|
skip_linker_arg(&q);
|
2021-10-22 13:39:54 +08:00
|
|
|
pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
|
2013-02-13 02:13:28 +08:00
|
|
|
}
|
2010-04-06 03:21:58 +08:00
|
|
|
|
2022-12-20 14:24:48 +08:00
|
|
|
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
|
|
|
|
{
|
|
|
|
struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
|
|
|
|
f->type = filetype;
|
|
|
|
strcpy(f->name, filename);
|
|
|
|
dynarray_add(&s->files, &s->nb_files, f);
|
|
|
|
}
|
|
|
|
|
2013-02-13 02:13:28 +08:00
|
|
|
/* set linker options */
|
2021-10-22 13:39:54 +08:00
|
|
|
static int tcc_set_linker(TCCState *s, const char *option)
|
2013-02-13 02:13:28 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
TCCState *s1 = s;
|
2016-10-02 02:49:38 +08:00
|
|
|
while (*option) {
|
2013-02-13 02:13:28 +08:00
|
|
|
|
2016-10-02 02:49:38 +08:00
|
|
|
const char *p = NULL;
|
2013-02-13 02:13:28 +08:00
|
|
|
char *end = NULL;
|
|
|
|
int ignoring = 0;
|
2016-10-02 02:54:45 +08:00
|
|
|
int ret;
|
2013-02-13 02:13:28 +08:00
|
|
|
|
2010-08-13 03:30:21 +08:00
|
|
|
if (link_option(option, "Bsymbolic", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->symbolic = 1;
|
2012-04-18 15:48:57 +08:00
|
|
|
} else if (link_option(option, "nostdlib", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->nostdlib = 1;
|
2022-07-08 06:18:14 +08:00
|
|
|
} else if (link_option(option, "e=", &p)
|
|
|
|
|| link_option(option, "entry=", &p)) {
|
2022-04-18 14:26:15 +08:00
|
|
|
copy_linker_arg(&s->elf_entryname, p, 0);
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "fini=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
copy_linker_arg(&s->fini_symbol, p, 0);
|
2013-02-13 02:13:28 +08:00
|
|
|
ignoring = 1;
|
|
|
|
} else if (link_option(option, "image-base=", &p)
|
|
|
|
|| link_option(option, "Ttext=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->text_addr = strtoull(p, &end, 16);
|
|
|
|
s->has_text_addr = 1;
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "init=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
copy_linker_arg(&s->init_symbol, p, 0);
|
2013-02-13 02:13:28 +08:00
|
|
|
ignoring = 1;
|
2022-07-28 23:33:51 +08:00
|
|
|
} else if (link_option(option, "Map=", &p)) {
|
|
|
|
copy_linker_arg(&s->mapfile, p, 0);
|
|
|
|
ignoring = 1;
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "oformat=", &p)) {
|
2010-04-06 03:21:58 +08:00
|
|
|
#if defined(TCC_TARGET_PE)
|
2013-02-13 02:13:28 +08:00
|
|
|
if (strstart("pe-", &p)) {
|
2017-05-13 14:59:06 +08:00
|
|
|
#elif PTR_SIZE == 8
|
2013-02-13 02:13:28 +08:00
|
|
|
if (strstart("elf64-", &p)) {
|
2010-04-06 03:21:58 +08:00
|
|
|
#else
|
2013-02-13 02:13:28 +08:00
|
|
|
if (strstart("elf32-", &p)) {
|
2010-04-06 03:21:58 +08:00
|
|
|
#endif
|
2021-10-22 13:39:54 +08:00
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_ELF;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "binary")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_BINARY;
|
2010-04-06 03:21:58 +08:00
|
|
|
#ifdef TCC_TARGET_COFF
|
2013-02-13 02:13:28 +08:00
|
|
|
} else if (!strcmp(p, "coff")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_COFF;
|
2010-04-06 03:21:58 +08:00
|
|
|
#endif
|
2013-02-13 02:13:28 +08:00
|
|
|
} else
|
|
|
|
goto err;
|
2010-04-06 03:21:58 +08:00
|
|
|
|
2014-03-07 04:29:19 +08:00
|
|
|
} else if (link_option(option, "as-needed", &p)) {
|
|
|
|
ignoring = 1;
|
|
|
|
} else if (link_option(option, "O", &p)) {
|
|
|
|
ignoring = 1;
|
2017-04-04 14:34:52 +08:00
|
|
|
} else if (link_option(option, "export-all-symbols", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->rdynamic = 1;
|
2018-06-12 00:15:24 +08:00
|
|
|
} else if (link_option(option, "export-dynamic", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->rdynamic = 1;
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "rpath=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
copy_linker_arg(&s->rpath, p, ':');
|
-Wl, --enable-new-dtags for DT_RUNPATH instead of DT_RPATH
Today by accident i had to deal with linker problems of some
software and found an issue that mentioned DT_RUNPATH, which
mentioned that DT_RPATH is legacy and searched for
$LD_LIBRARY_PATH, whereas the newer DT_RUNPATH is searched
thereafter. Completely unencrypted! Well. For what's it worth,
i for one am astonished because of course i want to override
$LD_LIBRARY_PATH, but it surely has its merites, smart people came
to the conclusion, did they.
The attached diff below seems to be sufficient to support
DT_RUNPATH instead of DT_RPATH with tcc(1). But i have no insight
in what --enable-new-dtags is supposed to change in addition, so
i wonder.
Ciao!
--steffen
libtcc.c | 2 ++
tcc-doc.texi | 4 ++++
tcc.h | 1 +
tccelf.c | 3 ++-
4 files changed, 9 insertions(+), 1 deletion(-)
2017-02-16 22:49:02 +08:00
|
|
|
} else if (link_option(option, "enable-new-dtags", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->enable_new_dtags = 1;
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "section-alignment=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->section_align = strtoul(p, &end, 16);
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "soname=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
copy_linker_arg(&s->soname, p, 0);
|
2022-12-24 16:30:00 +08:00
|
|
|
} else if (link_option(option, "install_name=", &p)) {
|
|
|
|
copy_linker_arg(&s->soname, p, 0);
|
2010-04-06 03:21:58 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2016-12-12 22:52:45 +08:00
|
|
|
} else if (link_option(option, "large-address-aware", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_characteristics |= 0x20;
|
2011-07-12 00:47:16 +08:00
|
|
|
} else if (link_option(option, "file-alignment=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_file_align = strtoul(p, &end, 16);
|
2011-07-12 00:47:16 +08:00
|
|
|
} else if (link_option(option, "stack=", &p)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_stack_size = strtoul(p, &end, 10);
|
2010-08-13 03:30:21 +08:00
|
|
|
} else if (link_option(option, "subsystem=", &p)) {
|
2010-04-06 03:21:58 +08:00
|
|
|
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
|
|
|
if (!strcmp(p, "native")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 1;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "console")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 3;
|
2017-04-04 14:34:52 +08:00
|
|
|
} else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 2;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "posix")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 7;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "efiapp")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 10;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "efiboot")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 11;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "efiruntime")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 12;
|
2010-04-06 03:21:58 +08:00
|
|
|
} else if (!strcmp(p, "efirom")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 13;
|
2010-04-06 03:21:58 +08:00
|
|
|
#elif defined(TCC_TARGET_ARM)
|
|
|
|
if (!strcmp(p, "wince")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->pe_subsystem = 9;
|
2010-04-06 03:21:58 +08:00
|
|
|
#endif
|
2013-02-13 02:13:28 +08:00
|
|
|
} else
|
|
|
|
goto err;
|
2022-12-20 14:24:48 +08:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_MACHO
|
|
|
|
} else if (link_option(option, "all_load", &p)) {
|
|
|
|
s->filetype |= AFF_WHOLE_ARCHIVE;
|
|
|
|
} else if (link_option(option, "force_load", &p)) {
|
|
|
|
s->filetype |= AFF_WHOLE_ARCHIVE;
|
|
|
|
args_parser_add_file(s, p, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
|
|
|
|
s->nb_libraries++;
|
|
|
|
} else if (link_option(option, "single_module", &p)) {
|
|
|
|
ignoring = 1;
|
2010-04-06 03:21:58 +08:00
|
|
|
#endif
|
2016-10-02 02:54:45 +08:00
|
|
|
} else if (ret = link_option(option, "?whole-archive", &p), ret) {
|
2018-06-01 05:51:59 +08:00
|
|
|
if (ret > 0)
|
2021-10-22 13:39:54 +08:00
|
|
|
s->filetype |= AFF_WHOLE_ARCHIVE;
|
2018-06-01 05:51:59 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
s->filetype &= ~AFF_WHOLE_ARCHIVE;
|
2021-01-12 22:13:30 +08:00
|
|
|
} else if (link_option(option, "z=", &p)) {
|
|
|
|
ignoring = 1;
|
2016-10-02 02:49:38 +08:00
|
|
|
} else if (p) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
err:
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("unsupported linker option '%s'", option);
|
2013-02-13 02:13:28 +08:00
|
|
|
}
|
2021-08-01 02:44:51 +08:00
|
|
|
if (ignoring)
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_warning_c(warn_unsupported)("unsupported linker option '%s'", option);
|
2013-02-13 02:13:28 +08:00
|
|
|
option = skip_linker_arg(&p);
|
2010-04-06 03:21:58 +08:00
|
|
|
}
|
2016-10-02 02:49:38 +08:00
|
|
|
return 1;
|
2010-04-06 03:21:58 +08:00
|
|
|
}
|
|
|
|
|
2013-02-13 02:13:28 +08:00
|
|
|
typedef struct TCCOption {
|
|
|
|
const char *name;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t flags;
|
|
|
|
} TCCOption;
|
|
|
|
|
|
|
|
enum {
|
2021-03-30 17:25:58 +08:00
|
|
|
TCC_OPTION_ignored = 0,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_HELP,
|
2017-02-18 16:55:46 +08:00
|
|
|
TCC_OPTION_HELP2,
|
2017-02-18 16:55:34 +08:00
|
|
|
TCC_OPTION_v,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_I,
|
|
|
|
TCC_OPTION_D,
|
|
|
|
TCC_OPTION_U,
|
2015-03-03 19:19:14 +08:00
|
|
|
TCC_OPTION_P,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_L,
|
|
|
|
TCC_OPTION_B,
|
|
|
|
TCC_OPTION_l,
|
|
|
|
TCC_OPTION_bench,
|
|
|
|
TCC_OPTION_bt,
|
|
|
|
TCC_OPTION_b,
|
2019-12-12 20:29:45 +08:00
|
|
|
TCC_OPTION_ba,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_g,
|
|
|
|
TCC_OPTION_c,
|
2015-03-24 01:58:27 +08:00
|
|
|
TCC_OPTION_dumpversion,
|
2015-05-13 17:16:00 +08:00
|
|
|
TCC_OPTION_d,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_static,
|
2015-03-03 21:25:02 +08:00
|
|
|
TCC_OPTION_std,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_shared,
|
|
|
|
TCC_OPTION_soname,
|
|
|
|
TCC_OPTION_o,
|
|
|
|
TCC_OPTION_r,
|
|
|
|
TCC_OPTION_Wl,
|
2016-06-28 02:09:40 +08:00
|
|
|
TCC_OPTION_Wp,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_W,
|
|
|
|
TCC_OPTION_O,
|
2016-12-19 05:57:03 +08:00
|
|
|
TCC_OPTION_mfloat_abi,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_m,
|
|
|
|
TCC_OPTION_f,
|
|
|
|
TCC_OPTION_isystem,
|
2015-05-04 01:10:57 +08:00
|
|
|
TCC_OPTION_iwithprefix,
|
2016-06-27 22:40:00 +08:00
|
|
|
TCC_OPTION_include,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_nostdinc,
|
|
|
|
TCC_OPTION_nostdlib,
|
|
|
|
TCC_OPTION_print_search_dirs,
|
|
|
|
TCC_OPTION_rdynamic,
|
|
|
|
TCC_OPTION_pthread,
|
|
|
|
TCC_OPTION_run,
|
|
|
|
TCC_OPTION_w,
|
|
|
|
TCC_OPTION_E,
|
2021-06-28 13:27:39 +08:00
|
|
|
TCC_OPTION_M,
|
2013-02-13 02:13:28 +08:00
|
|
|
TCC_OPTION_MD,
|
|
|
|
TCC_OPTION_MF,
|
2021-06-28 13:27:39 +08:00
|
|
|
TCC_OPTION_MM,
|
|
|
|
TCC_OPTION_MMD,
|
2017-02-18 16:55:34 +08:00
|
|
|
TCC_OPTION_x,
|
|
|
|
TCC_OPTION_ar,
|
2020-07-11 12:41:44 +08:00
|
|
|
TCC_OPTION_impdef,
|
2022-12-20 14:24:48 +08:00
|
|
|
TCC_OPTION_dynamiclib,
|
|
|
|
TCC_OPTION_flat_namespace,
|
|
|
|
TCC_OPTION_two_levelnamespace,
|
|
|
|
TCC_OPTION_undefined,
|
|
|
|
TCC_OPTION_install_name,
|
|
|
|
TCC_OPTION_compatibility_version ,
|
|
|
|
TCC_OPTION_current_version,
|
2013-02-13 02:13:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#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 },
|
2017-02-18 16:55:46 +08:00
|
|
|
{ "hh", TCC_OPTION_HELP2, 0 },
|
2017-02-18 16:55:34 +08:00
|
|
|
{ "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2020-05-11 04:47:53 +08:00
|
|
|
{ "-version", TCC_OPTION_v, 0 }, /* handle as verbose, also prints version*/
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "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 19:19:14 +08:00
|
|
|
{ "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
|
2018-11-13 00:50:20 +08:00
|
|
|
{ "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "bench", TCC_OPTION_bench, 0 },
|
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2020-01-18 05:58:39 +08:00
|
|
|
{ "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-13 02:13:28 +08:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
{ "b", TCC_OPTION_b, 0 },
|
|
|
|
#endif
|
|
|
|
{ "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2022-12-20 14:24:48 +08:00
|
|
|
#ifdef TCC_TARGET_MACHO
|
|
|
|
{ "compatibility_version", TCC_OPTION_compatibility_version, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "current_version", TCC_OPTION_current_version, TCC_OPTION_HAS_ARG },
|
|
|
|
#endif
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "c", TCC_OPTION_c, 0 },
|
2022-12-20 14:24:48 +08:00
|
|
|
#ifdef TCC_TARGET_MACHO
|
|
|
|
{ "dynamiclib", TCC_OPTION_dynamiclib, 0 },
|
|
|
|
#endif
|
2015-03-24 01:58:27 +08:00
|
|
|
{ "dumpversion", TCC_OPTION_dumpversion, 0},
|
2015-05-13 17:16:00 +08:00
|
|
|
{ "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "static", TCC_OPTION_static, 0 },
|
2015-03-03 21:25:02 +08:00
|
|
|
{ "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "shared", TCC_OPTION_shared, 0 },
|
|
|
|
{ "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "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 },
|
|
|
|
{ "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2016-06-28 02:09:40 +08:00
|
|
|
{ "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2016-12-19 05:57:03 +08:00
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
{ "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
|
|
|
|
#endif
|
|
|
|
{ "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2022-12-20 14:24:48 +08:00
|
|
|
#ifdef TCC_TARGET_MACHO
|
|
|
|
{ "flat_namespace", TCC_OPTION_flat_namespace, 0 },
|
|
|
|
#endif
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
|
2016-06-27 22:40:00 +08:00
|
|
|
{ "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "nostdinc", TCC_OPTION_nostdinc, 0 },
|
|
|
|
{ "nostdlib", TCC_OPTION_nostdlib, 0 },
|
2013-11-09 03:24:15 +08:00
|
|
|
{ "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "w", TCC_OPTION_w, 0 },
|
|
|
|
{ "E", TCC_OPTION_E, 0},
|
2021-06-28 13:27:39 +08:00
|
|
|
{ "M", TCC_OPTION_M, 0},
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "MD", TCC_OPTION_MD, 0},
|
|
|
|
{ "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
|
2021-06-28 13:27:39 +08:00
|
|
|
{ "MM", TCC_OPTION_MM, 0},
|
|
|
|
{ "MMD", TCC_OPTION_MMD, 0},
|
2013-02-13 02:13:28 +08:00
|
|
|
{ "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
|
2017-02-18 16:55:34 +08:00
|
|
|
{ "ar", TCC_OPTION_ar, 0},
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
{ "impdef", TCC_OPTION_impdef, 0},
|
2022-12-20 14:24:48 +08:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_MACHO
|
|
|
|
{ "install_name", TCC_OPTION_install_name, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "two_levelnamespace", TCC_OPTION_two_levelnamespace, 0 },
|
|
|
|
{ "undefined", TCC_OPTION_undefined, TCC_OPTION_HAS_ARG },
|
2017-02-18 16:55:34 +08:00
|
|
|
#endif
|
2021-03-30 17:25:58 +08:00
|
|
|
/* ignored (silently, except after -Wunsupported) */
|
|
|
|
{ "arch", 0, TCC_OPTION_HAS_ARG},
|
|
|
|
{ "C", 0, 0 },
|
|
|
|
{ "-param", 0, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "pedantic", 0, 0 },
|
|
|
|
{ "pipe", 0, 0 },
|
|
|
|
{ "s", 0, 0 },
|
|
|
|
{ "traditional", 0, 0 },
|
2013-02-13 02:13:28 +08:00
|
|
|
{ NULL, 0, 0 },
|
|
|
|
};
|
|
|
|
|
2021-08-01 02:44:51 +08:00
|
|
|
typedef struct FlagDef {
|
|
|
|
uint16_t offset;
|
|
|
|
uint16_t flags;
|
|
|
|
const char *name;
|
|
|
|
} FlagDef;
|
|
|
|
|
|
|
|
#define WD_ALL 0x0001 /* warning is activated when using -Wall */
|
|
|
|
#define FD_INVERT 0x0002 /* invert value before storing */
|
|
|
|
|
|
|
|
static const FlagDef options_W[] = {
|
|
|
|
{ offsetof(TCCState, warn_all), WD_ALL, "all" },
|
|
|
|
{ offsetof(TCCState, warn_error), 0, "error" },
|
|
|
|
{ offsetof(TCCState, warn_write_strings), 0, "write-strings" },
|
|
|
|
{ offsetof(TCCState, warn_unsupported), 0, "unsupported" },
|
|
|
|
{ offsetof(TCCState, warn_implicit_function_declaration), WD_ALL, "implicit-function-declaration" },
|
|
|
|
{ offsetof(TCCState, warn_discarded_qualifiers), WD_ALL, "discarded-qualifiers" },
|
|
|
|
{ 0, 0, NULL }
|
|
|
|
};
|
|
|
|
|
2016-12-19 05:57:03 +08:00
|
|
|
static const FlagDef options_f[] = {
|
|
|
|
{ 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" },
|
|
|
|
{ offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
|
|
|
|
{ offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
|
2021-01-24 01:17:38 +08:00
|
|
|
{ offsetof(TCCState, test_coverage), 0, "test-coverage" },
|
2016-12-19 05:57:03 +08:00
|
|
|
{ 0, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const FlagDef options_m[] = {
|
|
|
|
{ offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
{ offsetof(TCCState, nosse), FD_INVERT, "sse" },
|
|
|
|
#endif
|
|
|
|
{ 0, 0, NULL }
|
|
|
|
};
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static int set_flag(TCCState *s, const FlagDef *flags, const char *name)
|
2021-08-01 02:44:51 +08:00
|
|
|
{
|
|
|
|
int value, mask, ret;
|
|
|
|
const FlagDef *p;
|
|
|
|
const char *r;
|
|
|
|
unsigned char *f;
|
|
|
|
|
|
|
|
r = name, value = !strstart("no-", &r), mask = 0;
|
|
|
|
|
|
|
|
/* when called with options_W, look for -W[no-]error=<option> */
|
|
|
|
if ((flags->flags & WD_ALL) && strstart("error=", &r))
|
|
|
|
value = value ? WARN_ON|WARN_ERR : WARN_NOE, mask = WARN_ON;
|
|
|
|
|
|
|
|
for (ret = -1, p = flags; p->name; ++p) {
|
|
|
|
if (ret) {
|
|
|
|
if (strcmp(r, p->name))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (0 == (p->flags & WD_ALL))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
f = (unsigned char *)s + p->offset;
|
2021-08-01 02:44:51 +08:00
|
|
|
*f = (*f & mask) | (value ^ !!(p->flags & FD_INVERT));
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
ret = 0;
|
|
|
|
if (strcmp(r, "all"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static int args_parser_make_argv(const char *r, int *argc, char ***argv)
|
2017-02-18 16:55:34 +08:00
|
|
|
{
|
|
|
|
int ret = 0, q, c;
|
|
|
|
CString str;
|
|
|
|
for(;;) {
|
|
|
|
while (c = (unsigned char)*r, c && c <= ' ')
|
2019-01-11 21:26:17 +08:00
|
|
|
++r;
|
2017-02-18 16:55:34 +08:00
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
q = 0;
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_new(&str);
|
2017-02-18 16:55:34 +08:00
|
|
|
while (c = (unsigned char)*r, c) {
|
|
|
|
++r;
|
|
|
|
if (c == '\\' && (*r == '"' || *r == '\\')) {
|
|
|
|
c = *r++;
|
|
|
|
} else if (c == '"') {
|
|
|
|
q = !q;
|
|
|
|
continue;
|
|
|
|
} else if (q == 0 && c <= ' ') {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_ccat(&str, c);
|
2017-02-18 16:55:34 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_ccat(&str, 0);
|
2017-02-18 16:55:34 +08:00
|
|
|
//printf("<%s>\n", str.data), fflush(stdout);
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_add(argv, argc, tcc_strdup(str.data));
|
|
|
|
cstr_free(&str);
|
2017-02-18 16:55:34 +08:00
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-02 02:19:37 +08:00
|
|
|
/* read list file */
|
2023-04-15 15:54:23 +08:00
|
|
|
static int args_parser_listfile(TCCState *s,
|
2017-02-18 16:55:34 +08:00
|
|
|
const char *filename, int optind, int *pargc, char ***pargv)
|
2016-10-02 02:19:37 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
TCCState *s1 = s;
|
2017-02-18 16:55:34 +08:00
|
|
|
int fd, i;
|
2016-10-02 02:19:37 +08:00
|
|
|
char *p;
|
2017-02-18 16:55:34 +08:00
|
|
|
int argc = 0;
|
|
|
|
char **argv = NULL;
|
2016-10-02 02:19:37 +08:00
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY | O_BINARY);
|
|
|
|
if (fd < 0)
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("listfile '%s' not found", filename);
|
2016-10-02 02:19:37 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
p = tcc_load_text(fd);
|
2017-02-18 16:55:34 +08:00
|
|
|
for (i = 0; i < *pargc; ++i)
|
|
|
|
if (i == optind)
|
2021-10-22 13:39:54 +08:00
|
|
|
args_parser_make_argv(p, &argc, &argv);
|
2017-02-18 16:55:34 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
|
2017-02-18 16:55:34 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_free(p);
|
|
|
|
dynarray_reset(&s->argv, &s->argc);
|
|
|
|
*pargc = s->argc = argc, *pargv = s->argv = argv;
|
2023-04-15 15:54:23 +08:00
|
|
|
return 0;
|
2016-10-02 02:19:37 +08:00
|
|
|
}
|
|
|
|
|
2022-12-20 14:24:48 +08:00
|
|
|
#if defined TCC_TARGET_MACHO
|
|
|
|
static uint32_t parse_version(TCCState *s1, const char *version)
|
|
|
|
{
|
|
|
|
uint32_t a = 0;
|
|
|
|
uint32_t b = 0;
|
|
|
|
uint32_t c = 0;
|
|
|
|
char* last;
|
|
|
|
|
|
|
|
a = strtoul(version, &last, 10);
|
|
|
|
if (*last == '.') {
|
|
|
|
b = strtoul(&last[1], &last, 10);
|
|
|
|
if (*last == '.')
|
|
|
|
c = strtoul(&last[1], &last, 10);
|
|
|
|
}
|
|
|
|
if (*last || a > 0xffff || b > 0xff || c > 0xff)
|
2023-04-15 15:54:23 +08:00
|
|
|
tcc_error_noabort("version a.b.c not correct: %s", version);
|
2022-12-20 14:24:48 +08:00
|
|
|
return (a << 16) | (b << 8) | c;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
|
2010-06-16 20:54:24 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
TCCState *s1 = s;
|
2013-02-13 02:13:28 +08:00
|
|
|
const TCCOption *popt;
|
|
|
|
const char *optarg, *r;
|
2017-02-14 01:23:43 +08:00
|
|
|
const char *run = NULL;
|
2017-02-18 16:55:34 +08:00
|
|
|
int x;
|
|
|
|
int tool = 0, arg_start = 0, noaction = optind;
|
|
|
|
char **argv = *pargv;
|
|
|
|
int argc = *pargc;
|
2016-10-02 02:19:37 +08:00
|
|
|
|
2023-04-15 15:54:23 +08:00
|
|
|
cstr_reset(&s->linker_arg);
|
2010-06-16 20:54:24 +08:00
|
|
|
|
2013-02-13 02:13:28 +08:00
|
|
|
while (optind < argc) {
|
2017-02-18 16:55:34 +08:00
|
|
|
r = argv[optind];
|
2016-10-02 02:19:37 +08:00
|
|
|
if (r[0] == '@' && r[1] != '\0') {
|
2023-04-15 15:54:23 +08:00
|
|
|
if (args_parser_listfile(s, r + 1, optind, &argc, &argv))
|
|
|
|
return -1;
|
2019-01-11 21:26:17 +08:00
|
|
|
continue;
|
2016-10-02 02:19:37 +08:00
|
|
|
}
|
2017-02-18 16:55:34 +08:00
|
|
|
optind++;
|
|
|
|
if (tool) {
|
|
|
|
if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
|
2021-10-22 13:39:54 +08:00
|
|
|
++s->verbose;
|
2017-02-18 16:55:34 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reparse:
|
2013-02-13 02:13:28 +08:00
|
|
|
if (r[0] != '-' || r[1] == '\0') {
|
2023-04-01 00:20:49 +08:00
|
|
|
args_parser_add_file(s, r, s->filetype);
|
2016-10-02 02:19:37 +08:00
|
|
|
if (run) {
|
2023-04-01 00:20:49 +08:00
|
|
|
dorun:
|
2023-04-15 15:54:23 +08:00
|
|
|
if (tcc_set_options(s, run))
|
|
|
|
return -1;
|
2017-02-18 16:55:34 +08:00
|
|
|
arg_start = optind - 1;
|
2016-10-02 02:19:37 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
}
|
|
|
|
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-21 00:08:12 +08:00
|
|
|
|
2023-04-01 00:20:49 +08:00
|
|
|
/* allow "tcc files... -run -- args ..." */
|
|
|
|
if (r[1] == '-' && r[2] == '\0' && run)
|
|
|
|
goto dorun;
|
|
|
|
|
2013-02-13 02:13:28 +08:00
|
|
|
/* find option in table */
|
|
|
|
for(popt = tcc_options; ; ++popt) {
|
|
|
|
const char *p1 = popt->name;
|
|
|
|
const char *r1 = r + 1;
|
|
|
|
if (p1 == NULL)
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("invalid option -- '%s'", r);
|
2013-02-13 02:13:28 +08:00
|
|
|
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)
|
2016-10-02 02:49:38 +08:00
|
|
|
arg_err:
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("argument to '%s' is missing", r);
|
2013-02-13 02:13:28 +08:00
|
|
|
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-21 00:08:12 +08:00
|
|
|
|
2013-02-13 02:13:28 +08:00
|
|
|
switch(popt->index) {
|
|
|
|
case TCC_OPTION_HELP:
|
2020-06-02 00:10:58 +08:00
|
|
|
x = OPT_HELP;
|
|
|
|
goto extra_action;
|
2017-02-18 16:55:46 +08:00
|
|
|
case TCC_OPTION_HELP2:
|
2020-06-02 00:10:58 +08:00
|
|
|
x = OPT_HELP2;
|
|
|
|
goto extra_action;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_I:
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_include_path(s, optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_D:
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_define_symbol(s, optarg, NULL);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_U:
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_undefine_symbol(s, optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_L:
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_library_path(s, optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_B:
|
|
|
|
/* set tcc utilities path (mainly for tcc development) */
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_set_lib_path(s, optarg);
|
2022-08-20 18:58:56 +08:00
|
|
|
++noaction;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_l:
|
2021-10-22 13:39:54 +08:00
|
|
|
args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
|
|
|
|
s->nb_libraries++;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_pthread:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->option_pthread = 1;
|
2017-12-23 21:46:27 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_bench:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->do_bench = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
case TCC_OPTION_bt:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->rt_num_callers = atoi(optarg);
|
|
|
|
s->do_backtrace = 1;
|
|
|
|
s->do_debug = 1;
|
Add dwarf support
The new gcc12 release does not support stabs any more.
This was a good reason to add support for dwarf.
The stabs code still works and is used if configure option --dwarf
is not used.
Tested on x86_64, i386, arm, arm64, riscv64 with dwarf-5.
Some debuggers may not support dwarf-5. Try using older dwarf versions
i that case.
The tccmacho.c code probably need some support for dwarf.
arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.
- fix get_sym_ref symbol size
arm-link.c, arm64-link.c, i386-link.c, riscv64-link.c, x86_64-link.c
- add R_DATA_32U
libtcc.c:
- parse -gdwarf option
tcc.c:
- add dwarf option
tcc.h:
- add dwarf option and sections
tccelf.c:
- init dwarf sections
- avoid adding sh_addr for dwarf sections
- remove dwarf relocs for output dll
- add dwarf sections for tccrun
tccgen.c:
- add dwarf defines + global data
- add dwarf_* functions
- mix dwarf code with stabs code
- a trick is used to emit function name in .debug_line section so
only this section has to be parsed instead of .debug_info and
.debug_abbrev.
- fix init debug_modes
tccrun.c:
- add dwarf sections in rt_context
- init them in tcc_run
- add new dwarf code rt_printline_dwarf to find file/function
dwarf.h:
- New file
tcc-doc.texi:
- document dwarf
configure:
- add dwarf option
lib/Makefile
- change -gstabs into -gdwarf
lib/bt-exe.c, tests/tests2/Makefile, tests/tests2/126_bound_global:
- Add __bound_init call
- Add new testcase to test it
2022-05-05 15:10:37 +08:00
|
|
|
s->dwarf = DWARF_VERSION;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
case TCC_OPTION_b:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->do_bounds_check = 1;
|
|
|
|
s->do_backtrace = 1;
|
|
|
|
s->do_debug = 1;
|
Add dwarf support
The new gcc12 release does not support stabs any more.
This was a good reason to add support for dwarf.
The stabs code still works and is used if configure option --dwarf
is not used.
Tested on x86_64, i386, arm, arm64, riscv64 with dwarf-5.
Some debuggers may not support dwarf-5. Try using older dwarf versions
i that case.
The tccmacho.c code probably need some support for dwarf.
arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.
- fix get_sym_ref symbol size
arm-link.c, arm64-link.c, i386-link.c, riscv64-link.c, x86_64-link.c
- add R_DATA_32U
libtcc.c:
- parse -gdwarf option
tcc.c:
- add dwarf option
tcc.h:
- add dwarf option and sections
tccelf.c:
- init dwarf sections
- avoid adding sh_addr for dwarf sections
- remove dwarf relocs for output dll
- add dwarf sections for tccrun
tccgen.c:
- add dwarf defines + global data
- add dwarf_* functions
- mix dwarf code with stabs code
- a trick is used to emit function name in .debug_line section so
only this section has to be parsed instead of .debug_info and
.debug_abbrev.
- fix init debug_modes
tccrun.c:
- add dwarf sections in rt_context
- init them in tcc_run
- add new dwarf code rt_printline_dwarf to find file/function
dwarf.h:
- New file
tcc-doc.texi:
- document dwarf
configure:
- add dwarf option
lib/Makefile
- change -gstabs into -gdwarf
lib/bt-exe.c, tests/tests2/Makefile, tests/tests2/126_bound_global:
- Add __bound_init call
- Add new testcase to test it
2022-05-05 15:10:37 +08:00
|
|
|
s->dwarf = DWARF_VERSION;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TCC_OPTION_g:
|
Add dwarf support
The new gcc12 release does not support stabs any more.
This was a good reason to add support for dwarf.
The stabs code still works and is used if configure option --dwarf
is not used.
Tested on x86_64, i386, arm, arm64, riscv64 with dwarf-5.
Some debuggers may not support dwarf-5. Try using older dwarf versions
i that case.
The tccmacho.c code probably need some support for dwarf.
arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.
- fix get_sym_ref symbol size
arm-link.c, arm64-link.c, i386-link.c, riscv64-link.c, x86_64-link.c
- add R_DATA_32U
libtcc.c:
- parse -gdwarf option
tcc.c:
- add dwarf option
tcc.h:
- add dwarf option and sections
tccelf.c:
- init dwarf sections
- avoid adding sh_addr for dwarf sections
- remove dwarf relocs for output dll
- add dwarf sections for tccrun
tccgen.c:
- add dwarf defines + global data
- add dwarf_* functions
- mix dwarf code with stabs code
- a trick is used to emit function name in .debug_line section so
only this section has to be parsed instead of .debug_info and
.debug_abbrev.
- fix init debug_modes
tccrun.c:
- add dwarf sections in rt_context
- init them in tcc_run
- add new dwarf code rt_printline_dwarf to find file/function
dwarf.h:
- New file
tcc-doc.texi:
- document dwarf
configure:
- add dwarf option
lib/Makefile
- change -gstabs into -gdwarf
lib/bt-exe.c, tests/tests2/Makefile, tests/tests2/126_bound_global:
- Add __bound_init call
- Add new testcase to test it
2022-05-05 15:10:37 +08:00
|
|
|
s->do_debug = 1;
|
2022-09-01 21:55:56 +08:00
|
|
|
s->dwarf = DWARF_VERSION;
|
|
|
|
|
|
|
|
if (strstart("dwarf", &optarg))
|
|
|
|
s->dwarf = (*optarg) ? (0 - atoi(optarg)) : DEFAULT_DWARF_VERSION;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_c:
|
2016-10-02 02:19:37 +08:00
|
|
|
x = TCC_OUTPUT_OBJ;
|
|
|
|
set_output_type:
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s->output_type)
|
|
|
|
tcc_warning("-%s: overriding compiler action already specified", popt->name);
|
|
|
|
s->output_type = x;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
2015-05-13 17:16:00 +08:00
|
|
|
case TCC_OPTION_d:
|
2016-05-05 20:12:53 +08:00
|
|
|
if (*optarg == 'D')
|
2021-10-22 13:39:54 +08:00
|
|
|
s->dflag = 3;
|
2016-05-05 20:12:53 +08:00
|
|
|
else if (*optarg == 'M')
|
2021-10-22 13:39:54 +08:00
|
|
|
s->dflag = 7;
|
2017-07-21 04:21:27 +08:00
|
|
|
else if (*optarg == 't')
|
2021-10-22 13:39:54 +08:00
|
|
|
s->dflag = 16;
|
2017-07-15 01:26:01 +08:00
|
|
|
else if (isnum(*optarg))
|
2021-10-22 13:39:54 +08:00
|
|
|
s->g_debug |= atoi(optarg);
|
2016-05-05 20:12:53 +08:00
|
|
|
else
|
|
|
|
goto unsupported_option;
|
2015-05-13 17:16:00 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_static:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->static_link = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
2015-03-03 21:25:02 +08:00
|
|
|
case TCC_OPTION_std:
|
2022-05-08 05:16:13 +08:00
|
|
|
if (strcmp(optarg, "=c11") == 0)
|
|
|
|
s->cversion = 201112;
|
2015-03-03 21:25:02 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_shared:
|
2016-10-02 02:19:37 +08:00
|
|
|
x = TCC_OUTPUT_DLL;
|
|
|
|
goto set_output_type;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_soname:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->soname = tcc_strdup(optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_o:
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s->outfile) {
|
|
|
|
tcc_warning("multiple -o option");
|
|
|
|
tcc_free(s->outfile);
|
2015-05-10 15:13:35 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
s->outfile = tcc_strdup(optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_r:
|
|
|
|
/* generate a .o merging several output files */
|
2021-10-22 13:39:54 +08:00
|
|
|
s->option_r = 1;
|
2016-10-02 02:19:37 +08:00
|
|
|
x = TCC_OUTPUT_OBJ;
|
|
|
|
goto set_output_type;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_isystem:
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_add_sysinclude_path(s, optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
2019-01-11 21:26:17 +08:00
|
|
|
case TCC_OPTION_include:
|
2021-10-22 13:39:54 +08:00
|
|
|
cstr_printf(&s->cmdline_incl, "#include \"%s\"\n", optarg);
|
2019-01-11 21:26:17 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_nostdinc:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->nostdinc = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_nostdlib:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->nostdlib = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_run:
|
2016-10-02 02:04:33 +08:00
|
|
|
#ifndef TCC_IS_NATIVE
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("-run is not available in a cross compiler");
|
|
|
|
#else
|
2017-02-14 01:23:43 +08:00
|
|
|
run = optarg;
|
2016-10-02 02:19:37 +08:00
|
|
|
x = TCC_OUTPUT_MEMORY;
|
|
|
|
goto set_output_type;
|
2023-04-15 15:54:23 +08:00
|
|
|
#endif
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_v:
|
2021-10-22 13:39:54 +08:00
|
|
|
do ++s->verbose; while (*optarg++ == 'v');
|
2017-02-18 16:55:34 +08:00
|
|
|
++noaction;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_f:
|
2021-10-22 13:39:54 +08:00
|
|
|
if (set_flag(s, options_f, optarg) < 0)
|
2016-12-19 05:57:03 +08:00
|
|
|
goto unsupported_option;
|
|
|
|
break;
|
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
case TCC_OPTION_mfloat_abi:
|
|
|
|
/* tcc doesn't support soft float yet */
|
|
|
|
if (!strcmp(optarg, "softfp")) {
|
2021-10-22 13:39:54 +08:00
|
|
|
s->float_abi = ARM_SOFTFP_FLOAT;
|
2016-12-19 05:57:03 +08:00
|
|
|
} else if (!strcmp(optarg, "hard"))
|
2021-10-22 13:39:54 +08:00
|
|
|
s->float_abi = ARM_HARD_FLOAT;
|
2016-12-19 05:57:03 +08:00
|
|
|
else
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("unsupported float abi '%s'", optarg);
|
2016-12-19 05:57:03 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TCC_OPTION_m:
|
2021-10-22 13:39:54 +08:00
|
|
|
if (set_flag(s, options_m, optarg) < 0) {
|
2017-02-18 16:55:34 +08:00
|
|
|
if (x = atoi(optarg), x != 32 && x != 64)
|
|
|
|
goto unsupported_option;
|
|
|
|
if (PTR_SIZE != x/8)
|
|
|
|
return x;
|
|
|
|
++noaction;
|
|
|
|
}
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_W:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->warn_none = 0;
|
|
|
|
if (optarg[0] && set_flag(s, options_W, optarg) < 0)
|
2013-02-13 02:13:28 +08:00
|
|
|
goto unsupported_option;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_w:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->warn_none = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_rdynamic:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->rdynamic = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_Wl:
|
2023-04-15 15:54:23 +08:00
|
|
|
if (s->linker_arg.size)
|
|
|
|
((char*)s->linker_arg.data)[s->linker_arg.size - 1] = ',';
|
|
|
|
cstr_cat(&s->linker_arg, optarg, 0);
|
|
|
|
x = tcc_set_linker(s, s->linker_arg.data);
|
|
|
|
if (x)
|
|
|
|
cstr_reset(&s->linker_arg);
|
|
|
|
if (x < 0)
|
|
|
|
return -1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
2019-01-11 21:26:17 +08:00
|
|
|
case TCC_OPTION_Wp:
|
|
|
|
r = optarg;
|
|
|
|
goto reparse;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_E:
|
2016-10-02 02:19:37 +08:00
|
|
|
x = TCC_OUTPUT_PREPROCESS;
|
|
|
|
goto set_output_type;
|
2015-03-03 19:19:14 +08:00
|
|
|
case TCC_OPTION_P:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->Pflag = atoi(optarg) + 1;
|
2015-03-03 19:19:14 +08:00
|
|
|
break;
|
2021-06-28 13:27:39 +08:00
|
|
|
case TCC_OPTION_M:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->include_sys_deps = 1;
|
2021-06-28 13:27:39 +08:00
|
|
|
// fall through
|
|
|
|
case TCC_OPTION_MM:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->just_deps = 1;
|
|
|
|
if(!s->deps_outfile)
|
|
|
|
s->deps_outfile = tcc_strdup("-");
|
2021-06-28 13:27:39 +08:00
|
|
|
// fall through
|
|
|
|
case TCC_OPTION_MMD:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->gen_deps = 1;
|
2021-06-28 13:27:39 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_MD:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->gen_deps = 1;
|
|
|
|
s->include_sys_deps = 1;
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_MF:
|
2021-10-22 13:39:54 +08:00
|
|
|
s->deps_outfile = tcc_strdup(optarg);
|
2013-02-13 02:13:28 +08:00
|
|
|
break;
|
2013-02-14 15:24:51 +08:00
|
|
|
case TCC_OPTION_dumpversion:
|
|
|
|
printf ("%s\n", TCC_VERSION);
|
|
|
|
exit(0);
|
2015-04-10 11:53:48 +08:00
|
|
|
break;
|
2015-04-12 20:35:37 +08:00
|
|
|
case TCC_OPTION_x:
|
2018-06-01 05:51:59 +08:00
|
|
|
x = 0;
|
2015-04-12 20:35:37 +08:00
|
|
|
if (*optarg == 'c')
|
2018-06-01 05:51:59 +08:00
|
|
|
x = AFF_TYPE_C;
|
2016-10-02 02:46:16 +08:00
|
|
|
else if (*optarg == 'a')
|
2018-06-01 05:51:59 +08:00
|
|
|
x = AFF_TYPE_ASMPP;
|
|
|
|
else if (*optarg == 'b')
|
|
|
|
x = AFF_TYPE_BIN;
|
2016-10-02 02:46:16 +08:00
|
|
|
else if (*optarg == 'n')
|
2018-06-01 05:51:59 +08:00
|
|
|
x = AFF_TYPE_NONE;
|
2015-04-12 20:35:37 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_warning("unsupported language '%s'", optarg);
|
|
|
|
s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
|
2015-04-12 20:35:37 +08:00
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
case TCC_OPTION_O:
|
2022-05-08 05:16:13 +08:00
|
|
|
s->optimize = atoi(optarg);
|
2015-05-10 15:21:37 +08:00
|
|
|
break;
|
2017-02-18 16:55:34 +08:00
|
|
|
case TCC_OPTION_print_search_dirs:
|
|
|
|
x = OPT_PRINT_DIRS;
|
|
|
|
goto extra_action;
|
|
|
|
case TCC_OPTION_impdef:
|
|
|
|
x = OPT_IMPDEF;
|
|
|
|
goto extra_action;
|
2022-12-20 14:24:48 +08:00
|
|
|
#if defined TCC_TARGET_MACHO
|
|
|
|
case TCC_OPTION_dynamiclib:
|
|
|
|
x = TCC_OUTPUT_DLL;
|
|
|
|
goto set_output_type;
|
|
|
|
case TCC_OPTION_flat_namespace:
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_two_levelnamespace:
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_undefined:
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_install_name:
|
|
|
|
s->install_name = tcc_strdup(optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_compatibility_version:
|
|
|
|
s->compatibility_version = parse_version(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_current_version:
|
|
|
|
s->current_version = parse_version(s, optarg);;
|
|
|
|
break;
|
|
|
|
#endif
|
2017-02-18 16:55:34 +08:00
|
|
|
case TCC_OPTION_ar:
|
|
|
|
x = OPT_AR;
|
|
|
|
extra_action:
|
|
|
|
arg_start = optind - 1;
|
|
|
|
if (arg_start != noaction)
|
2023-04-15 15:54:23 +08:00
|
|
|
return tcc_error_noabort("cannot parse %s here", r);
|
2017-02-18 16:55:34 +08:00
|
|
|
tool = x;
|
|
|
|
break;
|
2013-02-13 02:13:28 +08:00
|
|
|
default:
|
2016-05-05 20:12:53 +08:00
|
|
|
unsupported_option:
|
2021-10-22 13:39:54 +08:00
|
|
|
tcc_warning_c(warn_unsupported)("unsupported option '%s'", r);
|
2013-02-13 02:13:28 +08:00
|
|
|
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-21 00:08:12 +08:00
|
|
|
}
|
2023-04-15 15:54:23 +08:00
|
|
|
if (s->linker_arg.size) {
|
|
|
|
r = s->linker_arg.data;
|
2016-10-02 02:49:38 +08:00
|
|
|
goto arg_err;
|
2016-09-30 04:23:11 +08:00
|
|
|
}
|
2017-02-18 16:55:34 +08:00
|
|
|
*pargc = argc - arg_start;
|
|
|
|
*pargv = argv + arg_start;
|
|
|
|
if (tool)
|
|
|
|
return tool;
|
|
|
|
if (optind != noaction)
|
|
|
|
return 0;
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s->verbose == 2)
|
2017-02-18 16:55:34 +08:00
|
|
|
return OPT_PRINT_DIRS;
|
2021-10-22 13:39:54 +08:00
|
|
|
if (s->verbose)
|
2017-02-18 16:55:34 +08:00
|
|
|
return OPT_V;
|
|
|
|
return OPT_HELP;
|
2013-02-13 02:13:28 +08:00
|
|
|
}
|
|
|
|
|
2023-04-15 15:54:23 +08:00
|
|
|
LIBTCCAPI int tcc_set_options(TCCState *s, const char *r)
|
2013-02-13 02:13:28 +08:00
|
|
|
{
|
2017-02-18 16:55:34 +08:00
|
|
|
char **argv = NULL;
|
2023-04-15 15:54:23 +08:00
|
|
|
int argc = 0, ret;
|
2021-10-22 13:39:54 +08:00
|
|
|
args_parser_make_argv(r, &argc, &argv);
|
2023-04-15 15:54:23 +08:00
|
|
|
ret = tcc_parse_args(s, &argc, &argv, 0);
|
2021-10-22 13:39:54 +08:00
|
|
|
dynarray_reset(&argv, &argc);
|
2023-04-15 15:54:23 +08:00
|
|
|
return ret < 0 ? ret : 0;
|
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-21 00:08:12 +08:00
|
|
|
}
|
2013-02-10 07:38:40 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
|
2013-02-10 07:38:40 +08:00
|
|
|
{
|
2023-03-24 03:02:19 +08:00
|
|
|
if (!total_time)
|
2016-10-18 05:24:01 +08:00
|
|
|
total_time = 1;
|
2023-03-24 03:02:19 +08:00
|
|
|
fprintf(stderr, "# %d idents, %d lines, %u bytes\n"
|
|
|
|
"# %0.3f s, %u lines/s, %0.1f MB/s\n",
|
2019-12-14 19:31:03 +08:00
|
|
|
total_idents, total_lines, total_bytes,
|
2016-10-18 05:24:01 +08:00
|
|
|
(double)total_time/1000,
|
|
|
|
(unsigned)total_lines*1000/total_time,
|
|
|
|
(double)total_bytes/1000/total_time);
|
2023-03-24 03:02:19 +08:00
|
|
|
fprintf(stderr, "# text %u, data.rw %u, data.ro %u, bss %u bytes\n",
|
2021-10-22 13:39:54 +08:00
|
|
|
s1->total_output[0],
|
|
|
|
s1->total_output[1],
|
|
|
|
s1->total_output[2],
|
|
|
|
s1->total_output[3]
|
2021-02-01 22:10:58 +08:00
|
|
|
);
|
2017-02-18 16:55:34 +08:00
|
|
|
#ifdef MEM_DEBUG
|
2023-03-24 03:02:19 +08:00
|
|
|
fprintf(stderr, "# %d bytes memory used\n", mem_max_size);
|
2017-02-18 16:55:34 +08:00
|
|
|
#endif
|
2013-02-10 07:38:40 +08:00
|
|
|
}
|