Skip to content

Commit

Permalink
- tcc -vv: show cross-libtcc1.a correctly (and more)
Browse files Browse the repository at this point in the history
(As long as it is in the default install location and was not
moved elsewhere into the library search path manually)

Also:
- libtcc.c:
  - error1(): show correct line with "In file included from ..."
  - support "tcc -Bxxx -vv"
  - tcc_new()/tcc_compile(): Don't create elf sections for tcc -E
- tccdbg.c:
  - tcc -E -g : revert 1de025c
    Let's keep things simple, everybody understands 'do_debug'
    and dState is set by tcov too (but no debug sections).
- tccgen.c:
  - avoid the extra parameter for gind()
    (from c3e3a07)
  - vla func params: use skip_or_save_block() and enable
    VT_LVAL (see 313855c)
  - cleanup nocode_wanted a bit
- tccelf.c:
  - tccelf_end_file(): don't try to translate zero-sym relocs
    (seems to happen with asm "jmp 0x1000")
  - version_add(): do not make "ld-linux.so" DT_NEEDED
  • Loading branch information
grischka committed Sep 24, 2022
1 parent 414c22c commit e41730f
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 111 deletions.
40 changes: 21 additions & 19 deletions libtcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ static void error1(int mode, const char *fmt, va_list ap)
if (f) {
for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
cstr_printf(&cs, "In file included from %s:%d:\n",
(*pf)->filename, (*pf)->line_num);
(*pf)->filename, (*pf)->line_num - 1);
cstr_printf(&cs, "%s:%d: ",
f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
} else if (s1->current_filename) {
Expand Down Expand Up @@ -733,24 +733,25 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
file->fd = fd;
}

tccelf_begin_file(s1);
preprocess_start(s1, filetype);
tccgen_init(s1);

if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
tcc_preprocess(s1);
} else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {
tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
} else {
tccgen_compile(s1);
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);
}
}
tccgen_finish(s1);
preprocess_end(s1);

s1->error_set_jmp_enabled = 0;
tcc_exit_state(s1);

tccelf_end_file(s1);
return s1->nb_errors != 0 ? -1 : 0;
}

Expand Down Expand Up @@ -819,8 +820,6 @@ LIBTCCAPI TCCState *tcc_new(void)
/* might be used in error() before preprocess_start() */
s->include_stack_ptr = s->include_stack;

tccelf_new(s);

tcc_set_lib_path(s, CONFIG_TCCDIR);
return s;
}
Expand Down Expand Up @@ -879,19 +878,23 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
}

if (output_type == TCC_OUTPUT_PREPROCESS)
if (output_type == TCC_OUTPUT_PREPROCESS) {
s->do_debug = 0;
return 0;
}

tccelf_new(s);
if (s->do_debug) {
/* add debug sections */
tcc_debug_new(s);
}
#ifdef CONFIG_TCC_BCHECK
if (s->do_bounds_check) {
/* if bound checking, then add corresponding sections */
tccelf_bounds_new(s);
}
#endif
if (s->do_debug) {
/* add debug sections */
tcc_debug_new(s);
}

if (output_type == TCC_OUTPUT_OBJ) {
/* always elf for objects */
s->output_format = TCC_OUTPUT_FORMAT_ELF;
Expand Down Expand Up @@ -1195,11 +1198,9 @@ ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
/* find [cross-]libtcc1.a and tcc helper objects in library path */
ST_FUNC void tcc_add_support(TCCState *s1, const char *filename)
{
#ifdef CONFIG_TCC_CROSSPREFIX
char buf[100];
snprintf(buf, sizeof buf, "%s%s", CONFIG_TCC_CROSSPREFIX, filename);
filename = buf;
#endif
if (CONFIG_TCC_CROSSPREFIX[0])
filename = strcat(strcpy(buf, CONFIG_TCC_CROSSPREFIX), filename);
if (tcc_add_dll(s1, filename, 0) < 0)
tcc_error_noabort("%s not found", filename);
}
Expand Down Expand Up @@ -1838,6 +1839,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
case TCC_OPTION_B:
/* set tcc utilities path (mainly for tcc development) */
tcc_set_lib_path(s, optarg);
++noaction;
break;
case TCC_OPTION_l:
args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
Expand Down
8 changes: 3 additions & 5 deletions tcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,8 @@ static void print_search_dirs(TCCState *s)
/* print_dirs("programs", NULL, 0); */
print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
print_dirs("libraries", s->library_paths, s->nb_library_paths);
#ifdef TCC_TARGET_PE
printf("libtcc1:\n %s/lib/"TCC_LIBTCC1"\n", s->tcc_lib_path);
#else
printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
printf("libtcc1:\n %s/%s\n", s->library_paths[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1);
#ifndef TCC_TARGET_PE
print_dirs("crt", s->crt_paths, s->nb_crt_paths);
printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
#endif
Expand Down Expand Up @@ -281,7 +279,7 @@ int main(int argc0, char **argv0)
redo:
argc = argc0, argv = argv0;
s = s1 = tcc_new();
#ifdef CONFIG_TCC_SWITCHES
#ifdef CONFIG_TCC_SWITCHES /* predefined options */
tcc_set_options(s, CONFIG_TCC_SWITCHES);
#endif
opt = tcc_parse_args(s, &argc, &argv, 1);
Expand Down
4 changes: 4 additions & 0 deletions tcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ extern long double strtold (const char *__nptr, char **__endptr);
# define TCC_LIBTCC1 "libtcc1.a"
#endif

#ifndef CONFIG_TCC_CROSSPREFIX
# define CONFIG_TCC_CROSSPREFIX ""
#endif

/* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
#if defined CONFIG_USE_LIBGCC && !defined TCC_LIBGCC
#define TCC_LIBGCC USE_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
Expand Down
26 changes: 13 additions & 13 deletions tccdbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ ST_FUNC void tcc_debug_start(TCCState *s1)
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
SHN_ABS, filename);

if (s1->dState) {
if (s1->do_debug) {

new_file = last_line_num = 0;
debug_next_type = N_DEFAULT_DEBUG;
Expand Down Expand Up @@ -835,7 +835,7 @@ ST_FUNC void tcc_debug_start(TCCState *s1)
/* put end of translation unit info */
ST_FUNC void tcc_debug_end(TCCState *s1)
{
if (!s1->dState)
if (!s1->do_debug)
return;
if (s1->dwarf) {
int i, j;
Expand Down Expand Up @@ -992,7 +992,7 @@ ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename)
if (0 == strcmp(file->filename, filename))
return;
pstrcpy(file->filename, sizeof(file->filename), filename);
if (!s1->dState)
if (!s1->do_debug)
return;
if (s1->dwarf)
dwarf_file(s1);
Expand All @@ -1002,7 +1002,7 @@ ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename)
/* begin of #include */
ST_FUNC void tcc_debug_bincl(TCCState *s1)
{
if (!s1->dState)
if (!s1->do_debug)
return;
if (s1->dwarf) {
int i, j;
Expand Down Expand Up @@ -1059,7 +1059,7 @@ ST_FUNC void tcc_debug_bincl(TCCState *s1)
/* end of #include */
ST_FUNC void tcc_debug_eincl(TCCState *s1)
{
if (!s1->dState)
if (!s1->do_debug)
return;
if (s1->dwarf)
dwarf_file(s1);
Expand All @@ -1073,7 +1073,7 @@ ST_FUNC void tcc_debug_line(TCCState *s1)
{
BufferedFile *f;

if (!s1->dState)
if (!s1->do_debug)
return;
if (cur_text_section != text_section)
return;
Expand Down Expand Up @@ -1161,7 +1161,7 @@ static void tcc_debug_stabs (TCCState *s1, const char *str, int type, unsigned l

ST_FUNC void tcc_debug_stabn(TCCState *s1, int type, int value)
{
if (!s1->dState)
if (!s1->do_debug)
return;
if (type == N_LBRAC) {
struct _debug_info *info =
Expand Down Expand Up @@ -1234,7 +1234,7 @@ ST_FUNC void tcc_debug_fix_anon(TCCState *s1, CType *t)
{
int i, j, debug_type;

if (!s1->dState || !s1->dwarf || debug_info)
if (!s1->do_debug || !s1->dwarf || debug_info)
return;
if ((t->t & VT_BTYPE) == VT_STRUCT && t->ref->c != -1)
for (i = 0; i < n_debug_anon_hash; i++)
Expand Down Expand Up @@ -1747,7 +1747,7 @@ static void tcc_debug_finish (TCCState *s1, struct _debug_info *cur)
ST_FUNC void tcc_add_debug_info(TCCState *s1, int param, Sym *s, Sym *e)
{
CString debug_str;
if (!s1->dState)
if (!s1->do_debug)
return;
cstr_new (&debug_str);
for (; s != e; s = s->prev) {
Expand Down Expand Up @@ -1777,7 +1777,7 @@ ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
CString debug_str;
BufferedFile *f;

if (!s1->dState)
if (!s1->do_debug)
return;
debug_info_root = NULL;
debug_info = NULL;
Expand Down Expand Up @@ -1816,7 +1816,7 @@ ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
/* put function size */
ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
{
if (!s1->dState)
if (!s1->do_debug)
return;
tcc_debug_line(s1);
tcc_debug_stabn(s1, N_RBRAC, size);
Expand Down Expand Up @@ -1873,7 +1873,7 @@ ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)

ST_FUNC void tcc_debug_extern_sym(TCCState *s1, Sym *sym, int sh_num, int sym_bind, int sym_type)
{
if (!s1->dState)
if (!s1->do_debug)
return;
if (sym_type == STT_FUNC || sym->v >= SYM_FIRST_ANOM)
return;
Expand Down Expand Up @@ -1925,7 +1925,7 @@ ST_FUNC void tcc_debug_extern_sym(TCCState *s1, Sym *sym, int sh_num, int sym_bi

ST_FUNC void tcc_debug_typedef(TCCState *s1, Sym *sym)
{
if (!s1->dState)
if (!s1->do_debug)
return;
if (s1->dwarf) {
int debug_type;
Expand Down
11 changes: 9 additions & 2 deletions tccelf.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ ST_FUNC void tccelf_end_file(TCCState *s1)
ElfW_Rel *rel_end = (ElfW_Rel*)(sr->data + sr->data_offset);
for (; rel < rel_end; ++rel) {
int n = ELFW(R_SYM)(rel->r_info) - first_sym;
//if (n < 0) tcc_error("internal: invalid symbol index in relocation");
if (n < 0) /* zero sym_index in reloc (can happen with asm) */
continue;
rel->r_info = ELFW(R_INFO)(tr[n], ELFW(R_TYPE)(rel->r_info));
}
}
Expand Down Expand Up @@ -596,8 +597,14 @@ version_add (TCCState *s1)
ElfW(Vernaux) *vna = 0;
if (sv->out_index < 1)
continue;

/* make sure that a DT_NEEDED tag is put */
tcc_add_dllref(s1, sv->lib, 0);
/* abitest-tcc fails on older i386-linux with "ld-linux.so.2" DT_NEEDED
ret_int_test... Inconsistency detected by ld.so: dl-minimal.c: 148:
realloc: Assertion `ptr == alloc_last_block' failed! */
if (strcmp(sv->lib, "ld-linux.so.2"))
tcc_add_dllref(s1, sv->lib, 0);

vnofs = section_add(verneed_section, sizeof(*vn), 1);
vn = (ElfW(Verneed)*)(verneed_section->data + vnofs);
vn->vn_version = 1;
Expand Down
Loading

0 comments on commit e41730f

Please sign in to comment.