lotsa debug printing

This commit is contained in:
Jan Nieuwenhuizen 2017-11-16 07:48:49 +01:00
parent 3bafbef287
commit c8f509e8cd
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
9 changed files with 1642 additions and 70 deletions

View File

@ -225,12 +225,20 @@ void o(uint32_t i)
if (ind1 > cur_text_section->data_allocated)
section_realloc(cur_text_section, ind1);
cur_text_section->data[ind++] = i&255;
trace ("o "); eputs (itoa (i&255)); eputs ("\n");
trace ("o "); eputs (itoa (cur_text_section->data[ind-1])); eputs ("\n");
i>>=8;
cur_text_section->data[ind++] = i&255;
trace ("o "); eputs (itoa (i&255)); eputs ("\n");
trace ("o "); eputs (itoa (cur_text_section->data[ind-1])); eputs ("\n");
i>>=8;
cur_text_section->data[ind++] = i&255;
trace ("o "); eputs (itoa (i&255)); eputs ("\n");
trace ("o "); eputs (itoa (cur_text_section->data[ind-1])); eputs ("\n");
i>>=8;
cur_text_section->data[ind++] = i;
trace ("o "); eputs (itoa (i)); eputs ("\n");
trace ("o "); eputs (itoa (cur_text_section->data[ind-1])); eputs ("\n");
}
static uint32_t stuff_const(uint32_t op, uint32_t c)

View File

@ -1593,6 +1593,7 @@ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands,
ASMOperand *op;
int i, reg;
trace_enter ("asm_gen_code");
/* Strictly speaking %Xbp and %Xsp should be included in the
call-preserved registers, but currently it doesn't matter. */
#ifdef TCC_TARGET_X86_64
@ -1686,6 +1687,7 @@ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands,
}
}
}
trace_exit ("asm_gen_code");
}
ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str)

View File

@ -99,6 +99,8 @@ static unsigned long func_bound_ind;
/* XXX: make it faster ? */
ST_FUNC void g(int c)
{
trace_enter ("g");
trace ("gen o c="); eputs (itoa (c)); eputs ("\n");
int ind1;
if (nocode_wanted)
return;
@ -106,7 +108,9 @@ ST_FUNC void g(int c)
if (ind1 > cur_text_section->data_allocated)
section_realloc(cur_text_section, ind1);
cur_text_section->data[ind] = c;
trace ("gen o data="); eputs (itoa (cur_text_section->data[ind])); eputs ("\n");
ind = ind1;
trace_exit ("g");
}
ST_FUNC void o(unsigned int c)
@ -206,6 +210,7 @@ ST_FUNC void load(int r, SValue *sv)
int v, t, ft, fc, fr;
SValue v1;
trace_enter ("load");
#ifdef TCC_TARGET_PE
SValue v2;
sv = pe_getimport(sv, &v2);
@ -277,6 +282,7 @@ ST_FUNC void load(int r, SValue *sv)
o(0xc0 + r + v * 8); /* mov v, r */
}
}
trace_exit ("load");
}
/* store register 'r' in lvalue 'v' */
@ -284,6 +290,7 @@ ST_FUNC void store(int r, SValue *v)
{
int fr, bt, ft, fc;
trace_enter ("store");
#ifdef TCC_TARGET_PE
SValue v2;
v = pe_getimport(v, &v2);
@ -320,6 +327,7 @@ ST_FUNC void store(int r, SValue *v)
} else if (fr != r) {
o(0xc0 + fr + r * 8); /* mov r, fr */
}
trace_exit ("store");
}
static void gadd_sp(int val)
@ -430,6 +438,7 @@ ST_FUNC void gfunc_call(int nb_args)
int size, align, r, args_size, i, func_call;
Sym *func_sym;
trace_enter ("gfunc_call");
args_size = 0;
for(i = 0;i < nb_args; i++) {
if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
@ -509,6 +518,7 @@ ST_FUNC void gfunc_call(int nb_args)
if (args_size && func_call != FUNC_STDCALL)
gadd_sp(args_size);
vtop--;
trace_exit ("gfunc_call");
}
#ifdef TCC_TARGET_PE
@ -526,30 +536,40 @@ ST_FUNC void gfunc_prolog(CType *func_type)
Sym *sym;
CType *type;
trace_enter ("gfunc_prolog");
sym = func_type->ref;
func_call = sym->a.func_call;
addr = 8;
loc = 0;
func_vc = 0;
trace ("gfunc_prolog 10\n");
if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
trace ("gfunc_prolog 11\n");
fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
fastcall_regs_ptr = fastcall_regs;
} else if (func_call == FUNC_FASTCALLW) {
trace ("gfunc_prolog 15\n");
fastcall_nb_regs = 2;
fastcall_regs_ptr = fastcallw_regs;
} else {
trace ("gfunc_prolog 18\n");
fastcall_nb_regs = 0;
fastcall_regs_ptr = NULL;
}
param_index = 0;
trace ("gfunc_prolog 20\n");
ind += FUNC_PROLOG_SIZE;
trace ("gfunc_prolog 21\n");
func_sub_sp_offset = ind;
trace ("gfunc_prolog 22\n");
/* if the function returns a structure, then add an
implicit pointer parameter */
func_vt = sym->type;
trace ("gfunc_prolog 23\n");
func_var = (sym->c == FUNC_ELLIPSIS);
trace ("gfunc_prolog 24\n");
#ifdef TCC_TARGET_PE
size = type_size(&func_vt,&align);
if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
@ -557,29 +577,35 @@ ST_FUNC void gfunc_prolog(CType *func_type)
#else
if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
#endif
trace ("gfunc_prolog 25\n");
/* XXX: fastcall case ? */
func_vc = addr;
addr += 4;
param_index++;
}
trace ("gfunc_prolog 30\n");
/* define parameters */
while ((sym = sym->next) != NULL) {
trace ("gfunc_prolog 31 param_index="); eputs (itoa (param_index)); eputs ("\n");
type = &sym->type;
size = type_size(type, &align);
size = (size + 3) & ~3;
#ifdef FUNC_STRUCT_PARAM_AS_PTR
/* structs are passed as pointer */
if ((type->t & VT_BTYPE) == VT_STRUCT) {
trace ("gfunc_prolog 36\n");
size = 4;
}
#endif
if (param_index < fastcall_nb_regs) {
trace ("gfunc_prolog 38\n");
/* save FASTCALL register */
loc -= 4;
o(0x89); /* movl */
gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
param_addr = loc;
} else {
trace ("gfunc_prolog 39\n");
param_addr = addr;
addr += size;
}
@ -588,6 +614,7 @@ ST_FUNC void gfunc_prolog(CType *func_type)
param_index++;
}
func_ret_sub = 0;
trace ("gfunc_prolog 50\n");
/* pascal type call ? */
if (func_call == FUNC_STDCALL)
func_ret_sub = addr - 8;
@ -596,15 +623,18 @@ ST_FUNC void gfunc_prolog(CType *func_type)
func_ret_sub = 4;
#endif
trace ("gfunc_prolog 60\n");
#ifdef CONFIG_TCC_BCHECK
/* leave some room for bound checking code */
if (tcc_state->do_bounds_check) {
trace ("gfunc_prolog 61\n");
func_bound_offset = lbounds_section->data_offset;
func_bound_ind = ind;
oad(0xb8, 0); /* lbound section pointer */
oad(0xb8, 0); /* call to function */
}
#endif
trace_exit ("gfunc_prolog");
}
/* generate function epilog */
@ -612,9 +642,11 @@ ST_FUNC void gfunc_epilog(void)
{
addr_t v, saved_ind;
trace ("gfunc_epilog\n");
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check
&& func_bound_offset != lbounds_section->data_offset) {
trace ("gfunc_epilog 01\n");
addr_t saved_ind;
addr_t *bounds_ptr;
Sym *sym_data;
@ -643,18 +675,22 @@ ST_FUNC void gfunc_epilog(void)
}
#endif
trace ("gfunc_epilog 20\n");
/* align local size to word & save local variables */
v = (-loc + 3) & -4;
#if USE_EBX
trace ("gfunc_epilog 21\n");
o(0x8b);
gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
#endif
o(0xc9); /* leave */
if (func_ret_sub == 0) {
trace ("gfunc_epilog 23\n");
o(0xc3); /* ret */
} else {
trace ("gfunc_epilog 24\n");
o(0xc2); /* ret n */
g(func_ret_sub);
g(func_ret_sub >> 8);
@ -668,6 +704,7 @@ ST_FUNC void gfunc_epilog(void)
} else
#endif
{
trace ("gfunc_epilog 30\n");
o(0xe58955); /* push %ebp, mov %esp, %ebp */
o(0xec81); /* sub esp, stacksize */
gen_le32(v);
@ -760,6 +797,8 @@ ST_FUNC void gen_opi(int op)
{
int r, fr, opc, c;
trace_enter ("gen_opi");
trace ("gen_opi op="); eputs (itoa (op)); eputs ("\n");
switch(op) {
case '+':
case TOK_ADDC1: /* add with carry generation */
@ -771,6 +810,7 @@ ST_FUNC void gen_opi(int op)
r = gv(RC_INT);
vswap();
c = vtop->c.i;
trace ("gen_opi 14 c ="); eputs (itoa (c)); eputs ("\n");
#if __MESC__
char ch = c;
if (c == ch && ((ch >= 0 && ch < 128) || c < 0)) {
@ -789,6 +829,7 @@ ST_FUNC void gen_opi(int op)
g(c);
}
} else {
trace ("gen_opi 20\n");
o(0x81);
oad(0xc0 | (opc << 3) | r, c);
}

194
libtcc.c
View File

@ -233,10 +233,14 @@ PUB_FUNC void *tcc_mallocz(unsigned long size)
PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
{
trace_enter ("tcc_realloc");
trace ("tcc_realloc size="); eputs (itoa (size)); eputs ("\n");
void *ptr1;
ptr1 = realloc(ptr, size);
if (!ptr1 && size)
tcc_error("memory full (realloc)");
trace_exit ("tcc_realloc");
return ptr1;
}
@ -446,11 +450,17 @@ ST_FUNC void dynarray_reset(void *pp, int *n)
static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
{
const char *p;
//eputs ("sp in="); //eputs (in); //eputs ("\n");
do {
int c;
CString str;
//eputs ("sp in="); //eputs (in); //eputs ("\n");
//if (s->nb_files) {struct filespec *f = s->files[0]; //eputs ("sp 01 file[0]="); //eputs (f->name); //eputs ("\n");}
cstr_new(&str);
//if (s->nb_files) {struct filespec *f = s->files[0]; //eputs ("sp 02 file[0]="); //eputs (f->name); //eputs ("\n");}
for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
if (c == '{' && p[1] && p[2] == '}') {
c = p[1], p += 2;
@ -462,11 +472,15 @@ static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *
}
if (str.size) {
cstr_ccat(&str, '\0');
//if (s->nb_files) {struct filespec *f = s->files[0]; //eputs ("sp 14 file[0]="); //eputs (f->name); //eputs ("\n");}
dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
}
//if (s->nb_files) {struct filespec *f = s->files[0]; //eputs ("sp 15 file[0]="); //eputs (f->name); //eputs ("\n");}
cstr_free(&str);
//if (s->nb_files) {struct filespec *f = s->files[0]; //eputs ("sp 16 file[0]="); //eputs (f->name); //eputs ("\n");}
in = p+1;
} while (*p);
//eputs ("sp 99\n");
}
/********************************************************/
@ -583,51 +597,77 @@ PUB_FUNC void tcc_warning(const char *fmt, ...)
ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
{
trace_enter ("tcc_open_bf");
BufferedFile *bf;
int buflen = initlen ? initlen : IO_BUF_SIZE;
bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
trace ("tcc_open_bf buflen="); eputs (itoa (buflen)); eputs ("\n");
//eputs ("tcc_open_bf bf size="); //eputs (itoa (sizeof (BufferedFile) + buflen)); //eputs ("\n");
trace ("tcc_open_bf 10\n");
bf->buf_ptr = bf->buffer;
trace ("tcc_open_bf 11\n");
bf->buf_end = bf->buffer + initlen;
trace ("tcc_open_bf 12\n");
bf->buf_end[0] = CH_EOB; /* put eob symbol */
trace ("tcc_open_bf 13\n");
pstrcpy(bf->filename, sizeof(bf->filename), filename);
trace ("tcc_open_bf 14\n");
pstrcpy(bf->filename2, sizeof(bf->filename2), filename);
trace ("tcc_open_bf 15\n");
#ifdef _WIN32
normalize_slashes(bf->filename);
#endif
trace ("tcc_open_bf 20\n");
bf->line_num = 1;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
bf->fd = -1;
bf->prev = file;
file = bf;
trace_exit ("tcc_open_bf");
}
ST_FUNC void tcc_close(void)
{
trace_enter ("tcc_close");
BufferedFile *bf = file;
//eputs ("tcc_close file:"); //eputs (itoa (file)); //eputs ("\n");
//eputs ("tcc_close bf->fd:"); //eputs (itoa (bf->fd)); //eputs ("\n");
//eputs ("tcc_close bf->prev:"); //eputs (itoa (bf->prev)); //eputs ("\n");
if (bf->fd > 0) {
//eputs ("tcc_close bf->filename:"); //eputs (bf->filename); //eputs ("\n");
close(bf->fd);
total_lines += bf->line_num;
}
trace ("tcc_close file->buf_ptr="); eputs (file->buf_ptr); eputs ("\n");
file = bf->prev;
tcc_free(bf);
trace_exit ("tcc_close");
}
ST_FUNC int tcc_open(TCCState *s1, const char *filename)
{
int fd;
trace_enter ("tcc_open");
trace ("tcc_open file-name="); eputs (filename); eputs ("\n");
if (strcmp(filename, "-") == 0)
fd = 0, filename = "<stdin>";
else
fd = open(filename, O_RDONLY | O_BINARY);
trace ("tcc_open fd="); eputs (itoa (fd)); eputs ("\n");
if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
printf("%s %*s%s\n", fd < 0 ? "nf":"->",
(int)(s1->include_stack_ptr - s1->include_stack), "", filename);
if (fd < 0)
if (fd < 0) {
trace_exit ("tcc_open");
return -1;
}
eputs ("tcc_open 10\n");
tcc_open_bf(s1, filename, 0);
eputs ("tcc_open 11\n");
file->fd = fd;
trace_exit ("tcc_open");
return fd;
}
@ -636,30 +676,42 @@ static int tcc_compile(TCCState *s1)
{
Sym *define_start;
trace_enter ("tcc_compile");
define_start = define_stack;
if (setjmp(s1->error_jmp_buf) == 0) {
s1->nb_errors = 0;
s1->error_set_jmp_enabled = 1;
trace ("tcc_compile 05 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
preprocess_start(s1);
trace ("tcc_compile 06 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
tccgen_start(s1);
trace ("tcc_compile 07 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
#ifdef INC_DEBUG
printf("%s: **** new file\n", file->filename);
#endif
ch = file->buf_ptr[0];
trace ("tcc_compile 08 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
trace ("tcc_compile 10 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
next();
trace ("tcc_compile 11 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
decl(VT_CONST);
trace ("tcc_compile 12 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
if (tok != TOK_EOF)
expect("declaration");
/* free defines here already on behalf of of M.M.'s possibly existing
experimental preprocessor implementation. The normal call below
is still there to free after error-longjmp */
trace ("tcc_compile 13 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
free_defines(define_start);
trace ("tcc_compile 14 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
tccgen_end(s1);
trace ("tcc_compile 15 nb_errors="); eputs (itoa (s1->nb_errors)); eputs ("\n");
}
s1->error_set_jmp_enabled = 0;
@ -668,6 +720,7 @@ static int tcc_compile(TCCState *s1)
free_defines(define_start);
sym_pop(&global_stack, NULL, 0);
sym_pop(&local_stack, NULL, 0);
trace_exit ("tcc_compile");
return s1->nb_errors != 0 ? -1 : 0;
}
@ -686,25 +739,37 @@ LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
/* define a preprocessor symbol. A value can also be provided with the '=' operator */
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
{
trace_enter ("tcc_define_symbol");
trace ("tcc_define_symbol value="); eputs (itoa (value)); eputs ("\n");
int len1, len2;
/* default value */
if (!value)
value = "1";
trace ("tcc_define_symbol 01\n");
len1 = strlen(sym);
trace ("tcc_define_symbol 02\n");
len2 = strlen(value);
trace ("tcc_define_symbol 03\n");
/* init file structure */
tcc_open_bf(s1, "<define>", len1 + len2 + 1);
trace ("tcc_define_symbol 04\n");
memcpy(file->buffer, sym, len1);
file->buffer[len1] = ' ';
memcpy(file->buffer + len1 + 1, value, len2);
trace ("tcc_define_symbol X="); //eputs (file->buffer); //eputs ("\n");
/* parse with define parser */
ch = file->buf_ptr[0];
trace ("tcc_define_symbol 08\n");
next_nomacro();
trace ("tcc_define_symbol 09\n");
parse_define();
trace ("tcc_define_symbol 10\n");
tcc_close();
trace_exit ("tcc_define_symbol");
}
/* undefine a preprocessor symbol */
@ -722,12 +787,15 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
/* cleanup all static data used during compilation */
static void tcc_cleanup(void)
{
if (NULL == tcc_state)
return;
tccpp_delete(tcc_state);
//eputs ("tcc_cleanup01\n");
tcc_state = NULL;
/* free sym_pools */
dynarray_reset(&sym_pools, &nb_sym_pools);
//eputs ("tcc_cleanup02\n");
/* reset symbol stack */
sym_free_first = NULL;
}
@ -736,14 +804,25 @@ LIBTCCAPI TCCState *tcc_new(void)
{
TCCState *s;
//eputs ("tcc_new\n");
tcc_cleanup();
//eputs ("tcc_new01\n");
s = tcc_mallocz(sizeof(TCCState));
//eputs ("size:"); //eputs (itoa (sizeof(TCCState))); //eputs ("\n");
//eputs ("tcc_new02\n");
//eputs ("s="); //eputs (itoa (s)); //eputs ("\n");
if (!s)
return NULL;
tcc_state = s;
trace ("tccnew include_stack="); eputs (itoa (&s->include_stack[0])); eputs ("\n");
++nb_states;
//eputs ("tcc_new03\n");
s->alacarte_link = 1;
s->nocommon = 1;
s->warn_implicit_function_declaration = 1;
@ -898,6 +977,8 @@ LIBTCCAPI TCCState *tcc_new(void)
/* Some GCC builtins that are simple to express as macros. */
tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
#endif /* ndef TCC_TARGET_PE */
//eputs ("tcc_new99\n");
return s;
}
@ -1005,10 +1086,34 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
return 0;
}
#if !__x86_64__
#undef eputs
#endif
#if !BOOTSTRAP // MESC
#define for_each_elem(sec, startoff, elem, type) \
for (elem = (type *) sec->data + startoff; \
elem < (type *) (sec->data + sec->data_offset); elem++)
#else
#define for_each_elem(sec, startoff, elem, type) \
elem = sec->data + sizeof (type) * startoff; \
for (;elem < ((type *) (sec->data + sec->data_offset)); elem++)
#endif
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
{
int ret, filetype;
trace_enter ("tcc_add_file_internal");
char *name;
ElfW(Sym) *esym;
for_each_elem(symtab_section, 1, esym, ElfW(Sym)) {
trace ("tcc_add_file_internal num="); eputs (itoa (esym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + esym->st_name;
trace ("tcc_add_file_internal name="); eputs (name); eputs ("\n");
}
trace ("tcc_add_file_internal file-name="); eputs (filename); eputs ("\n");
filetype = flags & 0x0F;
if (filetype == 0) {
/* use a file extension to detect a filetype */
@ -1029,7 +1134,16 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
}
/* open the file */
trace ("tcc_add_file_internal filename="); eputs (filename); eputs ("\n");
ret = tcc_open(s1, filename);
trace ("tcc_add_file_internal 20 ret="); eputs (itoa (ret));; eputs ("\n");
for_each_elem(symtab_section, 1, esym, ElfW(Sym)) {
trace ("tcc_add_file_internal num="); eputs (itoa (esym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + esym->st_name;
trace ("tcc_add_file_internal name="); eputs (name); eputs ("\n");
}
if (ret < 0) {
if (flags & AFF_PRINT_ERROR)
tcc_error_noabort("file '%s' not found", filename);
@ -1040,6 +1154,13 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
dynarray_add(&s1->target_deps, &s1->nb_target_deps,
tcc_strdup(filename));
trace ("tcc_add_file_internal 21\n");
for_each_elem(symtab_section, 1, esym, ElfW(Sym)) {
trace ("tcc_add_file_internal num="); eputs (itoa (esym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + esym->st_name;
trace ("tcc_add_file_internal name="); eputs (name); eputs ("\n");
}
parse_flags = 0;
/* if .S file, define __ASSEMBLER__ like gcc does */
if (filetype == AFF_TYPE_ASM || filetype == AFF_TYPE_ASMPP) {
@ -1047,18 +1168,35 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
parse_flags = PARSE_FLAG_ASM_FILE;
}
trace ("tcc_add_file_internal 22\n");
for_each_elem(symtab_section, 1, esym, ElfW(Sym)) {
trace ("tcc_add_file_internal num="); eputs (itoa (esym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + esym->st_name;
trace ("tcc_add_file_internal name="); eputs (name); eputs ("\n");
}
if (flags & AFF_PREPROCESS) {
ret = tcc_preprocess(s1);
trace ("tcc_add_file_internal 31 ret="); eputs (itoa (ret));; eputs ("\n");
} else if (filetype == AFF_TYPE_C) {
//eputs ("tcc_add_file_internal: compile\n");
ret = tcc_compile(s1);
trace ("tcc_add_file_internal 32 ret="); eputs (itoa (ret));; eputs ("\n");
for_each_elem(symtab_section, 1, esym, ElfW(Sym)) {
trace ("tcc_add_file_internal num="); eputs (itoa (esym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + esym->st_name;
trace ("tcc_add_file_internal name="); eputs (name); eputs ("\n");
}
#ifdef CONFIG_TCC_ASM
} else if (filetype == AFF_TYPE_ASMPP) {
/* non preprocessed assembler */
ret = tcc_assemble(s1, 1);
trace ("tcc_add_file_internal 33 ret="); eputs (itoa (ret));; eputs ("\n");
} else if (filetype == AFF_TYPE_ASM) {
/* preprocessed assembler */
ret = tcc_assemble(s1, 0);
#endif
trace ("tcc_add_file_internal 34 ret="); eputs (itoa (ret));; eputs ("\n");
} else {
ElfW(Ehdr) ehdr;
int fd, obj_type;
@ -1077,43 +1215,60 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
switch (obj_type) {
case AFF_BINTYPE_REL:
{
ret = tcc_load_object_file(s1, fd, 0);
trace ("tcc_add_file_internal 41 ret="); eputs (itoa (ret));; eputs ("\n");
break;
}
#ifndef TCC_TARGET_PE
case AFF_BINTYPE_DYN:
{
if (s1->output_type == TCC_OUTPUT_MEMORY) {
ret = 0;
#ifdef TCC_IS_NATIVE
if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
ret = -1;
#endif
trace ("tcc_add_file_internal 42 ret="); eputs (itoa (ret));; eputs ("\n");
} else {
ret = tcc_load_dll(s1, fd, filename,
(flags & AFF_REFERENCED_DLL) != 0);
trace ("tcc_add_file_internal 43 ret="); eputs (itoa (ret));; eputs ("\n");
}
break;
#endif
}
case AFF_BINTYPE_AR:
{
ret = tcc_load_archive(s1, fd);
trace ("tcc_add_file_internal 51 ret="); eputs (itoa (ret));; eputs ("\n");
break;
}
#ifdef TCC_TARGET_COFF
case AFF_BINTYPE_C67:
ret = tcc_load_coff(s1, fd);
trace ("tcc_add_file_internal 52 ret="); eputs (itoa (ret));; eputs ("\n");
break;
#endif
default:
{
#ifdef TCC_TARGET_PE
ret = pe_load_file(s1, filename, fd);
#else
/* as GNU ld, consider it is an ld script if not recognized */
ret = tcc_load_ldscript(s1);
trace ("tcc_add_file_internal 61 ret="); eputs (itoa (ret));; eputs ("\n");
#endif
if (ret < 0)
tcc_error_noabort("unrecognized file type");
break;
}
}
}
trace ("tcc_add_file_internal 90\n");
tcc_close();
trace_exit ("tcc_add_file_internal");
return ret;
}
@ -1127,7 +1282,11 @@ LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
{
trace ("tcc_add_library_path path="); eputs (pathname); eputs ("\n");
tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
for(int i = 0; i < s->nb_library_paths; i++) {
trace ("tcc_add_library_path dir="); eputs (s->library_paths[i]); eputs ("\n");
}
return 0;
}
@ -1137,8 +1296,11 @@ static int tcc_add_library_internal(TCCState *s, const char *fmt,
char buf[1024];
int i;
trace ("tcc_add_library_internal file-name="); eputs (filename); eputs ("\n");
trace ("tcc_add_library_internal nb_paths="); eputs (itoa (nb_paths)); eputs ("\n");
for(i = 0; i < nb_paths; i++) {
snprintf(buf, sizeof(buf), fmt, paths[i], filename);
trace ("tcc_add_library_internal buf="); eputs (buf); eputs ("\n");
if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
return 0;
}
@ -1155,6 +1317,7 @@ ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
{
trace ("tcc_add_crt file name="); eputs (filename); eputs ("\n");
if (-1 == tcc_add_library_internal(s, "%s/%s",
filename, 0, s->crt_paths, s->nb_crt_paths))
tcc_error_noabort("file '%s' not found", filename);
@ -1195,6 +1358,7 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
{
trace ("tcc_add_library_err libname="); eputs (libname); eputs ("\n");
int ret = tcc_add_library(s, libname);
if (ret < 0)
tcc_error_noabort("library '%s' not found", libname);
@ -1216,6 +1380,7 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
So it is handled here as if it were in a DLL. */
pe_putimport(s, 0, name, (uintptr_t)val);
#else
trace ("tcc_add_symbol name="); eputs (name); eputs ("\n");
set_elf_sym(symtab_section, (uintptr_t)val, 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
SHN_ABS, name);
@ -1280,10 +1445,13 @@ ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
static int strstart(const char *val, const char **str)
{
//eputs ("strstart val:"); //eputs (val); //eputs ("\n");
//eputs ("strstart *str:"); //eputs (*str); //eputs ("\n");
const char *p, *q;
p = *str;
q = val;
while (*q) {
//eputs ("*q:"); //eputc (*q); //eputs ("\n");
if (*p != *q)
return 0;
p++;
@ -1643,10 +1811,16 @@ static void parse_option_D(TCCState *s1, const char *optarg)
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
{
struct filespec *f = tcc_malloc(sizeof (struct filespec) + strlen(filename));
trace ("arg_parser_add file name="); eputs (filename); eputs ("\n");
f->type = filetype;
f->alacarte = s->alacarte_link;
strcpy(f->name, filename);
trace ("arg_parser_add file f->name="); //eputs (f->name); //eputs ("\n");
trace ("arg_parser_add nb_files="); //eputs (itoa (s->nb_files)); //eputs ("\n");
dynarray_add(&s->files, &s->nb_files, f);
//struct filespec *= s->files[s->nb_files - 1];
struct filespec * fs = s->files[0];
//eputs ("arg_parser_add file fs->name="); //eputs (fs->name); //eputs ("\n");
}
static int args_parser_make_argv(const char *r, int *argc, char ***argv)
@ -1691,6 +1865,7 @@ static void args_parser_listfile(TCCState *s,
int argc = 0;
char **argv = NULL;
trace ("arg_parser_listfile filename="); eputs (filename); eputs ("\n");
fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0)
tcc_error("listfile '%s' not found", filename);
@ -1712,6 +1887,8 @@ static void args_parser_listfile(TCCState *s,
PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
{
trace ("tcc_parse_args\n");
const TCCOption *popt;
const char *optarg, *r;
const char *run = NULL;
@ -1726,7 +1903,11 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
cstr_new(&linker_arg);
while (optind < argc) {
trace ("tcc_parse_args 02 arg:"); eputs (itoa (optind)); eputs ("\n");
r = argv[optind];
trace ("tcc_parse_args 03 r:"); eputs (r); eputs ("\n");
if (r[0] == '@' && r[1] != '\0') {
args_parser_listfile(s, r + 1, optind, &argc, &argv);
continue;
@ -1749,10 +1930,17 @@ reparse:
continue;
}
trace ("tcc_parse_args 20\n");
trace ("tcc_parse_args 20 opt0="); eputs (tcc_options[0].name); eputs ("\n");
trace ("tcc_parse_args 20 opt1="); eputs (tcc_options[1].name); eputs ("\n");
/* find option in table */
for(popt = tcc_options; ; ++popt) {
trace ("tcc_parse_args 21\n");
trace ("tcc_parse_args 22 popt:"); eputs (popt->name); eputs ("\n");
const char *p1 = popt->name;
const char *r1 = r + 1;
trace ("tcc_parse_args 23 r1:"); eputs (r1); eputs ("\n");
if (p1 == NULL)
tcc_error("invalid option -- '%s'", r);
if (!strstart(p1, &r1))
@ -1770,6 +1958,8 @@ reparse:
break;
}
trace ("pars_arg 2\n");
switch(popt->index) {
case TCC_OPTION_HELP:
return OPT_HELP;

31
tcc.c
View File

@ -255,9 +255,21 @@ int main(int argc, char **argv)
vstack = &__vstack[1];
#endif
trace_enter ("main");
redo:
trace ("main 01\n");
s = tcc_new();
trace ("main 02\n");
opt = tcc_parse_args(s, &argc, &argv, 1);
trace ("main 03\n");
if (s->nb_files) {
struct filespec *f = s->files[0];
trace ("main 05 file[0]="); eputs (f->name); eputs ("\n");
}
trace ("main 06\n");
if (n == 0) {
if (opt == OPT_HELP)
@ -309,19 +321,28 @@ redo:
start_time = getclock_ms();
}
trace ("main 20\n");
set_environment(s);
trace ("main 21\n");
if (s->output_type == 0)
s->output_type = TCC_OUTPUT_EXE;
tcc_set_output_type(s, s->output_type);
trace ("main 23\n");
/* compile or add each files or library */
for (first_file = NULL, ret = 0;;) {
trace ("main 24 nb-files="); eputs (itoa (s->nb_files)); eputs ("\n");
trace ("main 24 n="); eputs (itoa (n)); eputs ("\n");
struct filespec *f = s->files[s->nb_files - n];
trace ("for f->name="); eputs (f->name); eputs ("\n");
s->filetype = f->type;
s->alacarte_link = f->alacarte;
if (f->type == AFF_TYPE_LIB) {
if (tcc_add_library_err(s, f->name) < 0)
ret = 1;
trace ("main 32 ret="); eputs (itoa (ret)); eputs ("\n");
} else {
if (1 == s->verbose)
printf("-> %s\n", f->name);
@ -329,7 +350,9 @@ redo:
first_file = f->name;
if (tcc_add_file(s, f->name) < 0)
ret = 1;
trace ("main 33 ret="); eputs (itoa (ret)); eputs ("\n");
}
trace ("ret="); eputs (itoa (ret)); eputs ("\n");
s->filetype = 0;
s->alacarte_link = 1;
if (ret || --n == 0
@ -337,17 +360,24 @@ redo:
break;
}
trace ("main 40\n");
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
trace ("main 41\n");
if (s->outfile)
fclose(s->ppfp);
} else if (0 == ret) {
trace ("main 42 ret == 0\n");
if (s->output_type == TCC_OUTPUT_MEMORY) {
trace ("main RUN\n");
#ifdef TCC_IS_NATIVE
ret = tcc_run(s, argc, argv);
#endif
} else {
trace ("main 44 gonna output_file\n");
if (!s->outfile)
s->outfile = default_outputfile(s, first_file);
trace ("main 45 output_file\n");
if (tcc_output_file(s, s->outfile))
ret = 1;
else if (s->gen_deps)
@ -360,5 +390,6 @@ redo:
tcc_delete(s);
if (ret == 0 && n)
goto redo; /* compile more files with -c */
trace_exit ("main");
return ret;
}

View File

@ -1155,9 +1155,12 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
next();
skip(']');
}
parse_mult_str(&astr, "string constant");
parse_mult_str(&astr, "parse_asm_operands string constant");
trace ("parse arm_operands tokc.str.data="); eputs (tokc.str.data); eputs ("\n");
op->constraint = tcc_malloc(astr.size);
trace ("parse arm_operands tokc.str.data="); eputs (tokc.str.data); eputs ("\n");
strcpy(op->constraint, astr.data);
trace ("parse arm_operands tokc.str.data="); eputs (tokc.str.data); eputs ("\n");
cstr_free(&astr);
skip('(');
gexpr();
@ -1196,13 +1199,17 @@ ST_FUNC void asm_instr(void)
int nb_outputs, nb_operands, i, must_subst, out_reg;
uint8_t clobber_regs[NB_ASM_REGS];
trace_enter ("asm_instr");
next();
trace ("asm_instr 01\n");
/* since we always generate the asm() instruction, we can ignore
volatile */
if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
next();
}
trace ("asm_instr 02\n");
parse_asm_str(&astr);
trace ("asm_instr astr.data="); eputs (astr.data); eputs ("\n");
nb_operands = 0;
nb_outputs = 0;
must_subst = 0;
@ -1255,6 +1262,7 @@ ST_FUNC void asm_instr(void)
#ifdef ASM_DEBUG
printf("asm: \"%s\"\n", (char *)astr.data);
#endif
trace ("asm_instr asm:"); eputs (astr.data); eputs ("\n");
if (must_subst) {
subst_asm_operands(operands, nb_operands, &astr1, &astr);
cstr_free(&astr);
@ -1264,6 +1272,7 @@ ST_FUNC void asm_instr(void)
#ifdef ASM_DEBUG
printf("subst_asm: \"%s\"\n", (char *)astr1.data);
#endif
trace ("asm_instr subst:"); eputs (astr1.data); eputs ("\n");
/* generate loads */
asm_gen_code(operands, nb_operands, nb_outputs, 0,
@ -1287,6 +1296,7 @@ ST_FUNC void asm_instr(void)
vpop();
}
cstr_free(&astr1);
trace_exit ("asm_instr");
}
ST_FUNC void asm_global_instr(void)

342
tccelf.c
View File

@ -51,28 +51,42 @@ static int new_undef_sym = 0; /* Is there a new undefined sym since last new_und
ST_FUNC void tccelf_new(TCCState *s)
{
//eputs ("tccelf_new00\n");
/* no section zero */
//eputs ("tccelf_new00 nb_sections="); //eputs (itoa (s->nb_sections)); //eputs ("\n");
dynarray_add(&s->sections, &s->nb_sections, NULL);
//eputs ("tccelf_new01\n");
/* create standard sections */
text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
//eputs ("tccelf_new02\n");
data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
//eputs ("tccelf_new3\n");
bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
//eputs ("tccelf_new04\n");
common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
//eputs ("tccelf_new05\n");
common_section->sh_num = SHN_COMMON;
//eputs ("tccelf_new06\n");
/* symbols are always generated for linking stage */
symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
".strtab",
".hashtab", SHF_PRIVATE);
//eputs ("tccelf_new07\n");
strtab_section = symtab_section->link;
//eputs ("tccelf_new08\n");
s->symtab = symtab_section;
//eputs ("tccelf_new09\n");
/* private symbol table for dynamic symbols */
s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
".dynstrtab",
".dynhashtab", SHF_PRIVATE);
//eputs ("tccelf_new10\n");
get_sym_attr(s, 0, 1);
//eputs ("tccelf_new99\n");
}
#ifdef CONFIG_TCC_BCHECK
@ -136,10 +150,16 @@ ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh
{
Section *sec;
trace_enter ("new_section");
sec = tcc_mallocz(sizeof(Section) + strlen(name));
trace ("size:"); eputs (itoa (sizeof(Section) + strlen (name))); eputs ("\n");
trace ("new_section 01\n");
trace ("name="); eputs (name); eputs ("\n");
strcpy(sec->name, name);
trace ("new_section 02\n");
sec->sh_type = sh_type;
sec->sh_flags = sh_flags;
trace ("new_section 05\n");
switch(sh_type) {
case SHT_HASH:
case SHT_REL:
@ -157,13 +177,29 @@ ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh
break;
}
trace ("new_section 10\n");
if (sh_flags & SHF_PRIVATE) {
trace ("new_section 11\n");
dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
} else {
trace ("new_section 12\n");
sec->sh_num = s1->nb_sections;
dynarray_add(&s1->sections, &s1->nb_sections, sec);
}
for (int i = 1; i < s1->nb_sections; i++) {
trace ("new_section i= "); eputs (itoa (i)); eputs ("\n");
Section *s;
#if !BOOTSTRAP
s = s1->sections[i];
#else
Section **ps = s1->sections;
s = ps[i];
#endif
trace ("new_section name="); eputs (s->name); eputs ("\n");
}
trace_exit ("new_section");
return sec;
}
@ -175,16 +211,26 @@ ST_FUNC Section *new_symtab(TCCState *s1,
Section *symtab, *strtab, *hash;
int *ptr, nb_buckets;
//eputs ("new_symtab00\n");
symtab = new_section(s1, symtab_name, sh_type, sh_flags);
//eputs ("new_symtab01\n");
//eputs ("s->hash="); //eputs (itoa (symtab->hash)); //eputs ("\n");
symtab->sh_entsize = sizeof(ElfW(Sym));
//eputs ("new_symtab02\n");
strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
//eputs ("new_symtab03\n");
put_elf_str(strtab, "");
//eputs ("new_symtab04\n");
symtab->link = strtab;
//eputs ("new_symtab05\n");
put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
//eputs ("new_symtab06\n");
nb_buckets = 1;
//eputs ("new_symtab10\n");
hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
//eputs ("new_symtab11\n");
hash->sh_entsize = sizeof(int);
symtab->hash = hash;
hash->link = symtab;
@ -219,13 +265,19 @@ ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
{
size_t offset, offset1;
trace_enter ("section_add");
offset = (sec->data_offset + align - 1) & -align;
trace ("section_add 01 offset="); eputs (itoa (offset)); eputs ("\n");
offset1 = offset + size;
if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated)
if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated) {
trace ("section_add 05\n");
section_realloc(sec, offset1);
}
sec->data_offset = offset1;
trace ("section_add 08 offset1="); eputs (itoa (offset1)); eputs ("\n");
if (align > sec->sh_addralign)
sec->sh_addralign = align;
trace_exit ("section_add");
return offset;
}
@ -233,7 +285,9 @@ ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
sec->data_offset. */
ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
{
trace_enter ("section_ptr_add");
size_t offset = section_add(sec, size, 1);
trace_exit ("section_ptr_add");
return sec->data + offset;
}
@ -268,6 +322,7 @@ ST_FUNC int put_elf_str(Section *s, const char *sym)
int offset, len;
char *ptr;
trace ("put_elf_str\n");
len = strlen(sym) + 1;
offset = s->data_offset;
ptr = section_ptr_add(s, len);
@ -333,6 +388,10 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
ElfW(Sym) *sym;
Section *hs;
trace_enter ("put_elf_sym");
if (name) {
trace ("put_elf_sym 00 name="); eputs (name); eputs ("\n");
}
sym = section_ptr_add(s, sizeof(ElfW(Sym)));
if (name)
name_offset = put_elf_str(s->link, name);
@ -347,12 +406,16 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
sym->st_shndx = shndx;
sym_index = sym - (ElfW(Sym) *)s->data;
hs = s->hash;
trace ("put_elf_sym20 size="); eputs (itoa (sizeof (ElfW(Sym)))); eputs ("\n");
trace ("put_elf_sym21 sym_index="); eputs (itoa (sym_index)); eputs ("\n");
if (hs) {
int *ptr, *base;
trace ("put_elf_sym 21\n");
ptr = section_ptr_add(hs, sizeof(int));
base = (int *)hs->data;
/* only add global or weak symbols */
if (ELFW(ST_BIND)(info) != STB_LOCAL) {
trace ("put_elf_sym 25\n");
/* add another hashing entry */
nbuckets = base[0];
h = elf_hash((unsigned char *) name) % nbuckets;
@ -365,10 +428,12 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
rebuild_hash(s, 2 * nbuckets);
}
} else {
trace ("put_elf_sym 40\n");
*ptr = 0;
base[1]++;
}
}
trace_exit ("put_elf_sym");
return sym_index;
}
@ -381,19 +446,37 @@ ST_FUNC int find_elf_sym(Section *s, const char *name)
int nbuckets, sym_index, h;
const char *name1;
trace_enter ("find_elf_sym");
trace ("find_elf_sym name="); eputs (name); eputs ("\n");
hs = s->hash;
if (!hs)
trace ("find_elf_sym 01\n");
if (!hs) {
trace_exit ("find_elf_sym");
return 0;
}
nbuckets = ((int *)hs->data)[0];
trace ("find_elf_sym 10 nbuckets="); eputs (itoa (nbuckets)); eputs ("\n");
h = elf_hash((unsigned char *) name) % nbuckets;
trace ("find_elf_sym 11 h="); eputs (itoa (h)); eputs ("\n");
sym_index = ((int *)hs->data)[2 + h];
trace ("find_elf_sym 12 sym_index="); eputs (itoa (sym_index)); eputs ("\n");
while (sym_index != 0) {
sym = &((ElfW(Sym) *)s->data)[sym_index];
name1 = (char *) s->link->data + sym->st_name;
if (!strcmp(name, name1))
trace ("find_elf_sym 13 sym->st_name="); eputs (itoa (sym->st_name)); eputs ("\n");
trace ("find_elf_sym 14 name1= "); eputs (itoa ((int)name1 - (int)s->link->data)); eputs ("\n");
//trace ("find_elf_sym 15 name= "); eputs (itoa (name)); eputs ("\n");
trace ("find_elf_sym 16 *name= "); eputs (itoa (*name)); eputs ("\n");
trace ("find_elf_sym 17 *name1="); eputs (itoa (name1 && sym->st_name >=0 && sym->st_name < 1000 ? *name1 : 0)); eputs ("\n");
//trace ("find_elf_sym 19 name= "); eputs (name); eputs ("\n");
//trace ("find_elf_sym 18 name1= "); eputs (name1); eputs ("\n");
if (!strcmp(name, name1)) {
trace_exit ("find_elf_sym 20");
return sym_index;
}
sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
}
trace_exit ("find_elf_sym");
return 0;
}
@ -427,31 +510,53 @@ ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
}
#endif
#if !BOOTSTRAP // MESC
#define for_each_elem(sec, startoff, elem, type) \
for (elem = (type *) sec->data + startoff; \
elem < (type *) (sec->data + sec->data_offset); elem++)
#else
#define for_each_elem(sec, startoff, elem, type) \
elem = sec->data + sizeof (type) * startoff; \
for (;elem < ((type *) (sec->data + sec->data_offset)); elem++)
#endif
/* add an elf symbol : check if it is already defined and patch
it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
int info, int other, int shndx, const char *name)
{
trace_enter ("set_elf_sym");
ElfW(Sym) *esym;
trace ("set_elf_sym 01 name="); eputs (name); eputs ("\n");
int sym_bind, sym_index, sym_type, esym_bind;
unsigned char sym_vis, esym_vis, new_vis;
sym_bind = ELFW(ST_BIND)(info);
trace ("set_elf_sym 02\n");
sym_type = ELFW(ST_TYPE)(info);
trace ("set_elf_sym 03\n");
sym_vis = ELFW(ST_VISIBILITY)(other);
trace ("set_elf_sym 05\n");
sym_index = find_elf_sym(s, name);
esym = &((ElfW(Sym) *)s->data)[sym_index];
trace ("set_elf_sym 10\n");
if (sym_index && esym->st_value == value && esym->st_size == size
&& esym->st_info == info && esym->st_other == other
&& esym->st_shndx == shndx)
{
trace_exit ("set_elf_sym 11");
return sym_index;
}
trace ("set_elf_sym 12\n");
if (sym_bind != STB_LOCAL) {
trace ("set_elf_sym 13\n");
/* we search global or weak symbols */
if (!sym_index)
goto do_def;
if (esym->st_shndx != SHN_UNDEF) {
trace ("set_elf_sym 16\n");
esym_bind = ELFW(ST_BIND)(esym->st_info);
/* propagate the most constraining visibility */
/* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
@ -497,6 +602,7 @@ ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
}
} else {
do_patch:
trace ("set_elf_sym 40\n");
esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
esym->st_shndx = shndx;
new_undef_sym = 1;
@ -506,10 +612,31 @@ ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
}
} else {
do_def:
trace ("set_elf_sym 50\n");
sym_index = put_elf_sym(s, value, size,
ELFW(ST_INFO)(sym_bind, sym_type), other,
shndx, name);
}
trace ("set_elf_sym 90 name="); eputs (name); eputs ("\n");
ElfW(Sym) *sym;
#if 1
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("set_elf_sym num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("set_elf_sym name="); eputs (name); eputs ("\n");
}
#else
for (sym = symtab_section->data; sym < (symtab_section->data + symtab_section->data_offset); sym++) {
trace ("set_elf_sym num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = strtab_section->data;
name += sym->st_name;
//name = ((char *) strtab_section->data) + sym->st_name;
trace ("set_elf_sym name="); eputs (name); eputs ("\n");
}
#endif
trace_exit ("set_elf_sym");
return sym_index;
}
@ -669,26 +796,39 @@ static void sort_syms(TCCState *s1, Section *s)
Section *sr;
int type, sym_index;
trace_enter ("sort_syms\n");
nb_syms = s->data_offset / sizeof(ElfW(Sym));
new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
trace ("sort_syms 05\n");
/* first pass for local symbols */
p = (ElfW(Sym) *)s->data;
q = new_syms;
trace ("sort_syms 10\n");
for(i = 0; i < nb_syms; i++) {
trace ("sort_syms 11 i="); eputs (itoa (i)); eputs ("\n");
unsigned char val = p->st_info;
trace ("val="); eputs (itoa (val)); eputs ("\n");
val >>= 4;
trace ("val="); eputs (itoa (val)); eputs ("\n");
if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
trace ("sort_syms 12\n");
old_to_new_syms[i] = q - new_syms;
*q++ = *p;
trace ("sort_syms 13\n");
}
p++;
}
trace ("sort_syms 20\n");
/* save the number of local symbols in section header */
s->sh_info = q - new_syms;
/* then second pass for non local symbols */
p = (ElfW(Sym) *)s->data;
trace ("sort_syms 25\n");
for(i = 0; i < nb_syms; i++) {
trace ("sort_syms 31 i="); eputs (itoa (i)); eputs ("\n");
if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
old_to_new_syms[i] = q - new_syms;
*q++ = *p;
@ -696,12 +836,15 @@ static void sort_syms(TCCState *s1, Section *s)
p++;
}
trace ("sort_syms40\n");
/* we copy the new symbols to the old */
memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
tcc_free(new_syms);
trace ("sort_syms43\n");
/* now we modify all the relocations */
for(i = 1; i < s1->nb_sections; i++) {
trace ("sort_syms 43 i="); eputs (itoa (i)); eputs ("\n");
sr = s1->sections[i];
if (sr->sh_type == SHT_RELX && sr->link == s) {
for_each_elem(sr, 0, rel, ElfW_Rel) {
@ -714,6 +857,7 @@ static void sort_syms(TCCState *s1, Section *s)
}
tcc_free(old_to_new_syms);
trace_exit ("sort_syms\n");
}
/* relocate common symbols in the .bss section */
@ -721,6 +865,7 @@ ST_FUNC void relocate_common_syms(void)
{
ElfW(Sym) *sym;
trace_enter ("relocate_common_syms");
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
if (sym->st_shndx == SHN_COMMON) {
/* symbol alignment is in st_value for SHN_COMMONs */
@ -729,6 +874,15 @@ ST_FUNC void relocate_common_syms(void)
sym->st_shndx = bss_section->sh_num;
}
}
trace ("relocate_common_syms 10\n");
char *name;
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("tcc_add_linker_symbols num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("tcc_add_linker_symbols name="); eputs (name); eputs ("\n");
}
trace_exit ("relocate_common_syms");
}
/* relocate symbol table, resolve undefined symbols if do_resolve is
@ -741,10 +895,13 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
for_each_elem(symtab, 1, sym, ElfW(Sym)) {
sh_num = sym->st_shndx;
trace ("relocate_syms 01 num="); eputs (itoa (sh_num)); eputs ("\n");
if (sh_num == SHN_UNDEF) {
name = (char *) strtab_section->data + sym->st_name;
trace ("relocate_syms 02 name="); eputs (name); eputs ("\n");
/* Use ld.so to resolve symbol for us (for tcc -run) */
if (do_resolve) {
trace ("relocate_syms 03\n");
#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
void *addr = dlsym(RTLD_DEFAULT, name);
if (addr) {
@ -756,25 +913,36 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
}
#endif
/* if dynamic symbol exist, it will be used in relocate_section */
} else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
} else if (s1->dynsym && find_elf_sym(s1->dynsym, name)) {
trace ("relocate_syms 04\n");
goto found;
}
/* XXX: _fp_hw seems to be part of the ABI, so we ignore
it */
if (!strcmp(name, "_fp_hw"))
trace ("relocate_syms 05\n");
if (!strcmp(name, "_fp_hw")) {
trace ("relocate_syms 06\n");
goto found;
}
/* only weak symbols are accepted to be undefined. Their
value is zero */
sym_bind = ELFW(ST_BIND)(sym->st_info);
trace ("relocate_syms 08\n");
if (sym_bind == STB_WEAK)
sym->st_value = 0;
else
tcc_error_noabort("undefined symbol '%s'", name);
} else if (sh_num < SHN_LORESERVE) {
trace ("relocate_syms 20\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("relocate_syms 02 name="); eputs (name); eputs ("\n");
/* add section base */
sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
trace ("relocate_syms 22\n");
}
found: ;
}
trace_exit ("relocate_syms");
}
/* relocate a given section (CPU dependent) by applying the relocations
@ -1173,18 +1341,37 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
int i;
Section *s;
trace_enter ("tcc_add_linker_symbols");
set_elf_sym(symtab_section,
text_section->data_offset, 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
text_section->sh_num, "_etext");
ElfW(Sym) *sym;
char *name;
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("tcc_add_linker_symbols num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("tcc_add_linker_symbols name="); eputs (name); eputs ("\n");
}
set_elf_sym(symtab_section,
data_section->data_offset, 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
data_section->sh_num, "_edata");
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("tcc_add_linker_symbols num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("tcc_add_linker_symbols name="); eputs (name); eputs ("\n");
}
set_elf_sym(symtab_section,
bss_section->data_offset, 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
bss_section->sh_num, "_end");
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("tcc_add_linker_symbols num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("tcc_add_linker_symbols name="); eputs (name); eputs ("\n");
}
#ifndef TCC_TARGET_PE
/* horrible new standard ldscript defines */
add_init_array_defines(s1, ".preinit_array");
@ -1224,6 +1411,12 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
}
next_sec: ;
}
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("set_elf_sym num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("set_elf_sym name="); eputs (name); eputs ("\n");
}
trace_exit ("tcc_add_linker_symbols");
}
static void tcc_output_binary(TCCState *s1, FILE *f,
@ -1280,6 +1473,7 @@ ST_FUNC void fill_got(TCCState *s1)
ElfW_Rel *rel;
int i;
trace_enter ("fill_got");
for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i];
if (s->sh_type != SHT_RELX)
@ -1299,6 +1493,7 @@ ST_FUNC void fill_got(TCCState *s1)
}
}
}
trace_exit ("fill_got");
}
/* See put_got_entry for a description. This is the second stage
@ -1307,6 +1502,7 @@ static void fill_local_got_entries(TCCState *s1)
{
ElfW_Rel *rel;
for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
trace_enter ("fill_local_got_entries");
if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
int sym_index = ELFW(R_SYM) (rel->r_info);
ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
@ -1323,6 +1519,7 @@ static void fill_local_got_entries(TCCState *s1)
#endif
}
}
trace_exit ("fill_local_got_entries");
}
/* Bind symbols of executable: resolve undefined symbols from exported symbols
@ -1468,30 +1665,43 @@ static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
int i;
Section *s;
trace_enter ("alloc_sec_names");
/* Allocate strings for section names */
for(i = 1; i < s1->nb_sections; i++) {
trace ("alloc_sec_names i="); eputs (itoa (i)); eputs ("\n");
s = s1->sections[i];
trace ("alloc_sec_names 02\n");
trace ("alloc_sec_names name="); eputs (s->name); eputs ("\n");
/* when generating a DLL, we include relocations but we may
patch them */
if (file_type == TCC_OUTPUT_DLL &&
s->sh_type == SHT_RELX &&
!(s->sh_flags & SHF_ALLOC)) {
/* gr: avoid bogus relocs for empty (debug) sections */
if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) {
trace ("alloc_sec_names 07\n");
prepare_dynamic_rel(s1, s);
}
else if (s1->do_debug)
s->sh_size = s->data_offset;
trace ("alloc_sec_names 09\n");
} else if (s1->do_debug ||
file_type == TCC_OUTPUT_OBJ ||
(s->sh_flags & SHF_ALLOC) ||
i == (s1->nb_sections - 1)) {
trace ("alloc_sec_names 10\n");
/* we output all sections if debug or object file */
s->sh_size = s->data_offset;
trace ("alloc_sec_names 11\n");
}
if (s->sh_size || (s->sh_flags & SHF_ALLOC))
trace ("alloc_sec_names 12\n");
if (s->sh_size || (s->sh_flags & SHF_ALLOC)) {
trace ("alloc_sec_names 13\n");
s->sh_name = put_elf_str(strsec, s->name);
}
}
strsec->sh_size = strsec->data_offset;
trace_exit ("alloc_sec_names");
}
/* Info to be copied in dynamic section */
@ -1520,6 +1730,7 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
ElfW(Phdr) *ph;
Section *s;
trace_enter ("layout_sections");
file_type = s1->output_type;
sh_order_index = 1;
file_offset = 0;
@ -1529,8 +1740,11 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
if (s1->section_align)
s_align = s1->section_align;
trace ("layout_sections 10\n");
if (phnum > 0) {
trace ("layout_sections 11\n");
if (s1->has_text_addr) {
trace ("layout_sections 12\n");
int a_offset, p_offset;
addr = s1->text_addr;
/* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
@ -1541,6 +1755,7 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
a_offset += s_align;
file_offset += (a_offset - p_offset);
} else {
trace ("layout_sections 20\n");
if (file_type == TCC_OUTPUT_DLL)
addr = 0;
else
@ -1549,6 +1764,7 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
addr += (file_offset & (s_align - 1));
}
trace ("layout_sections 30\n");
ph = &phdr[0];
/* Leave one program headers for the program interpreter and one for
the program header table itself if needed. These are done later as
@ -1563,6 +1779,7 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
#endif
for(j = 0; j < 2; j++) {
trace ("layout_sections j="); eputs (itoa (j)); eputs ("\n");
ph->p_type = PT_LOAD;
if (j == 0)
ph->p_flags = PF_R | PF_X;
@ -1576,8 +1793,10 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
symbol tables, relocations, progbits, nobits */
/* XXX: do faster and simpler sorting */
for(k = 0; k < 5; k++) {
trace ("layout_sections k="); eputs (itoa (k)); eputs ("\n");
for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i];
trace ("layout_sections i="); eputs (itoa (i)); eputs ("\n");
/* compute if section should be included */
if (j == 0) {
if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
@ -1670,8 +1889,10 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
}
}
trace ("layout_sections 80\n");
/* all other sections come after */
for(i = 1; i < s1->nb_sections; i++) {
trace ("layout_sections 80 i="); eputs (itoa (i)); eputs ("\n");
s = s1->sections[i];
if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
continue;
@ -1679,11 +1900,14 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
file_offset = (file_offset + s->sh_addralign - 1) &
~(s->sh_addralign - 1);
trace ("layout_sections file_offset="); eputs (itoa (file_offset)); eputs ("\n");
s->sh_offset = file_offset;
if (s->sh_type != SHT_NOBITS)
file_offset += s->sh_size;
}
trace_exit ("layout_sections");
return file_offset;
}
@ -1780,14 +2004,20 @@ static int final_sections_reloc(TCCState *s1)
int i;
Section *s;
trace_enter ("final_sections_reloc");
relocate_syms(s1, s1->symtab, 0);
trace ("final_sections_reloc 01\n");
if (s1->nb_errors != 0)
if (s1->nb_errors != 0) {
trace_exit ("final_sections_reloc");
return -1;
}
trace ("final_sections_reloc 02\n");
/* relocate sections */
/* XXX: ignore sections with allocated relocations ? */
for(i = 1; i < s1->nb_sections; i++) {
trace ("final_sections_reloc 21\n");
s = s1->sections[i];
#if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
@ -1800,15 +2030,19 @@ static int final_sections_reloc(TCCState *s1)
relocate_section(s1, s);
}
trace ("final_sections_reloc 80\n");
/* relocate relocation entries if the relocation tables are
allocated in the executable */
for(i = 1; i < s1->nb_sections; i++) {
trace ("final_sections_reloc 81\n");
s = s1->sections[i];
if ((s->sh_flags & SHF_ALLOC) &&
s->sh_type == SHT_RELX) {
relocate_rel(s1, s);
}
}
trace_exit ("final_sections_reloc");
return 0;
}
@ -1822,6 +2056,7 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
ElfW(Ehdr) ehdr;
ElfW(Shdr) shdr, *sh;
trace_enter ("tcc_output_elf");
file_type = s1->output_type;
shnum = s1->nb_sections;
@ -1833,6 +2068,8 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
ehdr.e_phoff = sizeof(ElfW(Ehdr));
}
trace ("tcc_output_elf 10\n");
/* align to 4 */
file_offset = (file_offset + 3) & -4;
@ -1861,6 +2098,7 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
#endif
#endif
trace ("tcc_output_elf 20\n");
switch(file_type) {
case TCC_OUTPUT_DLL:
ehdr.e_type = ET_DYN;
@ -1883,6 +2121,15 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
ehdr.e_shnum = shnum;
ehdr.e_shstrndx = shnum - 1;
trace ("tcc_output_elf 50\n");
trace ("tcc_output_elf ehdr.size="); eputs (itoa (sizeof(ElfW(Ehdr)))); eputs ("\n");
trace ("tcc_output_elf ehdr.e_machine="); eputs (itoa (ehdr.e_machine)); eputs ("\n");
trace ("tcc_output_elf ehdr.e_version="); eputs (itoa (ehdr.e_version)); eputs ("\n");
unsigned char *p = &ehdr;
for (int i = 0; i < sizeof(ElfW(Ehdr)); i++) {
if (i < 10) eputs (" ");
trace (itoa (i)); eputs (": "); eputs (itoa (p[i])); eputs ("\n");
}
fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
@ -1890,6 +2137,7 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
sort_syms(s1, symtab_section);
for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[sec_order[i]];
trace ("tcc_output_elf 50 name="); eputs (s->name); eputs ("\n");
if (s->sh_type != SHT_NOBITS) {
while (offset < s->sh_offset) {
fputc(0, f);
@ -1902,31 +2150,44 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
}
}
trace ("tcc_output_elf 60\n");
/* output section headers */
while (offset < ehdr.e_shoff) {
fputc(0, f);
offset++;
}
trace ("tcc_output_elf 70\n");
for(i = 0; i < s1->nb_sections; i++) {
trace ("tcc_output_elf 70 i="); eputs (itoa (i)); eputs ("\n");
sh = &shdr;
memset(sh, 0, sizeof(ElfW(Shdr)));
trace ("tcc_output_elf 72\n");
s = s1->sections[i];
if (s) {
trace ("tcc_output_elf 80\n");
trace ("tcc_output_elf 80 name="); eputs (s->name ? s->name : "<null>"); eputs ("\n");
sh->sh_name = s->sh_name;
sh->sh_type = s->sh_type;
sh->sh_flags = s->sh_flags;
sh->sh_entsize = s->sh_entsize;
sh->sh_info = s->sh_info;
if (s->link)
trace ("tcc_output_elf 85\n");
if (s->link) {
sh->sh_link = s->link->sh_num;
trace ("tcc_output_elf 86 sh_num="); eputs (itoa (s->link->sh_num)); eputs ("\n");
}
trace ("tcc_output_elf 87\n");
sh->sh_addralign = s->sh_addralign;
sh->sh_addr = s->sh_addr;
sh->sh_offset = s->sh_offset;
sh->sh_size = s->sh_size;
}
trace ("tcc_output_elf 90\n");
fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
trace ("tcc_output_elf 91\n");
}
trace_exit ("tcc_output_elf");
}
/* Write an elf, coff or "binary" file */
@ -1936,18 +2197,24 @@ static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
int fd, mode, file_type;
FILE *f;
trace_enter ("tcc_write_elf_file");
file_type = s1->output_type;
if (file_type == TCC_OUTPUT_OBJ)
mode = 0666;
else
mode = 0777;
unlink(filename);
trace ("tcc_write_elf_file 06\n");
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
trace ("tcc_write_elf_file 07\n");
if (fd < 0) {
trace ("tcc_write_elf_file fd < 0\n");
tcc_error_noabort("could not write '%s'", filename);
return -1;
}
trace ("tcc_write_elf_file 09\n");
f = fdopen(fd, "wb");
trace ("tcc_write_elf_file 10\n");
if (s1->verbose)
printf("<- %s\n", filename);
@ -1962,6 +2229,7 @@ static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
tcc_output_binary(s1, f, sec_order);
fclose(f);
trace_exit ("tcc_write_elf_file");
return 0;
}
@ -1973,6 +2241,7 @@ static void tidy_section_headers(TCCState *s1, int *sec_order)
Section **snew, *s;
ElfW(Sym) *sym;
trace_enter ("tidy_section_headers");
snew = tcc_malloc(s1->nb_sections * sizeof(struct Section*));
backmap = tcc_malloc(s1->nb_sections * sizeof(int));
for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
@ -2008,6 +2277,7 @@ static void tidy_section_headers(TCCState *s1, int *sec_order)
s1->sections = snew;
s1->nb_sections = nnew;
tcc_free(backmap);
trace_exit ("tidy_section_headers");
}
/* Output an elf, coff or binary file */
@ -2020,6 +2290,7 @@ static int elf_output_file(TCCState *s1, const char *filename)
ElfW(Sym) *sym;
Section *strsec, *interp, *dynamic, *dynstr;
trace_enter ("elf_output_file");
file_type = s1->output_type;
s1->nb_errors = 0;
@ -2033,12 +2304,43 @@ static int elf_output_file(TCCState *s1, const char *filename)
interp = dynamic = dynstr = NULL; /* avoid warning */
if (file_type != TCC_OUTPUT_OBJ) {
trace ("elf_output_file 10\n");
char *name;
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("elf_output_file num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("elf_output_file name="); eputs (name); eputs ("\n");
}
for (sym = symtab_section->data; sym < (symtab_section->data + symtab_section->data_offset); sym++) {
trace ("XXX num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = strtab_section->data;
name += sym->st_name;
//name = ((char *) strtab_section->data) + sym->st_name;
trace ("XXX name="); eputs (name); eputs ("\n");
}
relocate_common_syms();
trace ("elf_output_file 11\n");
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("elf_output_file num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("elf_output_file name="); eputs (name); eputs ("\n");
}
tcc_add_linker_symbols(s1);
trace ("elf_output_file 12\n");
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
trace ("elf_output_file num="); eputs (itoa (sym->st_shndx)); eputs ("\n");
name = (char *) strtab_section->data + sym->st_name;
trace ("elf_output_file name="); eputs (name); eputs ("\n");
}
if (!s1->static_link) {
trace ("elf_output_file 13\n");
if (file_type == TCC_OUTPUT_EXE) {
trace ("elf_output_file 14\n");
char *ptr;
/* allow override the dynamic loader */
const char *elfint = getenv("LD_SO");
@ -2050,6 +2352,7 @@ static int elf_output_file(TCCState *s1, const char *filename)
ptr = section_ptr_add(interp, 1 + strlen(elfint));
strcpy(ptr, elfint);
}
trace ("elf_output_file 20\n");
/* add dynamic symbol table */
s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
@ -2065,7 +2368,9 @@ static int elf_output_file(TCCState *s1, const char *filename)
build_got(s1);
trace ("elf_output_file 30\n");
if (file_type == TCC_OUTPUT_EXE) {
trace ("elf_output_file 31\n");
bind_exe_dynsyms(s1);
if (s1->nb_errors) {
@ -2105,14 +2410,18 @@ static int elf_output_file(TCCState *s1, const char *filename)
dyninf.dyn_rel_off = dynamic->data_offset;
dynamic->data_offset += sizeof(ElfW(Dyn)) * EXTRA_RELITEMS;
} else {
trace ("elf_output_file 49\n");
/* still need to build got entries in case of static link */
build_got_entries(s1);
}
}
trace ("elf_output_file 50\n");
/* we add a section for symbols */
strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
trace ("elf_output_file 51\n");
put_elf_str(strsec, "");
trace ("elf_output_file 52\n");
/* compute number of sections */
shnum = s1->nb_sections;
@ -2138,16 +2447,20 @@ static int elf_output_file(TCCState *s1, const char *filename)
break;
}
trace ("elf_output_file 60\n");
/* Allocate strings for section names */
alloc_sec_names(s1, file_type, strsec);
trace ("elf_output_file 61\n");
/* allocate program segment headers */
phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
trace ("elf_output_file 62\n");
/* compute section to program header mapping */
file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
sec_order);
trace ("elf_output_file 63\n");
/* Fill remaining program header and finalize relocation related to dynamic
linking. */
if (phnum > 0) {
@ -2174,14 +2487,20 @@ static int elf_output_file(TCCState *s1, const char *filename)
}
}
trace ("elf_output_file 80\n");
/* if building executable or DLL, then relocate each section
except the GOT which is already relocated */
if (file_type != TCC_OUTPUT_OBJ) {
trace ("elf_output_file 81\n");
ret = final_sections_reloc(s1);
trace ("elf_output_file 82\n");
if (ret)
goto the_end;
#if !BOOTSTRAP
trace ("elf_output_file 83\n");
tidy_section_headers(s1, sec_order);
trace ("elf_output_file 84\n");
#endif
}
@ -2193,12 +2512,17 @@ static int elf_output_file(TCCState *s1, const char *filename)
fill_local_got_entries(s1);
#endif
trace ("elf_output_file 90\n");
/* Create the ELF file with name 'filename' */
ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
trace ("elf_output_file 91\n");
s1->nb_sections = shnum;
the_end:
trace ("elf_output_file 92\n");
tcc_free(sec_order);
trace ("elf_output_file 93\n");
tcc_free(phdr);
trace_exit ("elf_output_file");
return ret;
}

609
tccgen.c

File diff suppressed because it is too large Load Diff

473
tccpp.c

File diff suppressed because it is too large Load Diff