Commit af686a79 authored by mingodad's avatar mingodad
Browse files

Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64

parent ca11849e
This diff is collapsed.
This diff is collapsed.
......@@ -107,9 +107,9 @@ int gotplt_entry_type (int reloc_type)
}
#ifndef TCC_TARGET_PE
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
ST_FUNC unsigned create_plt_entry(TCCState *S, unsigned got_offset, struct sym_attr *attr)
{
Section *plt = s1->plt;
Section *plt = S->plt;
uint8_t *p;
unsigned plt_offset;
......@@ -119,7 +119,7 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
/* empty PLT: create PLT0 entry that push address of call site and
jump to ld.so resolution routine (GOT + 8) */
if (plt->data_offset == 0) {
p = section_ptr_add(plt, 20);
p = section_ptr_add(S, plt, 20);
write32le(p, 0xe52de004); /* push {lr} */
write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */
write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */
......@@ -129,11 +129,11 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
plt_offset = plt->data_offset;
if (attr->plt_thumb_stub) {
p = section_ptr_add(plt, 4);
p = section_ptr_add(S, plt, 4);
write32le(p, 0x4778); /* bx pc */
write32le(p+2, 0x46c0); /* nop */
}
p = section_ptr_add(plt, 16);
p = section_ptr_add(S, plt, 16);
/* save GOT offset for relocate_plt */
write32le(p + 4, got_offset);
return plt_offset;
......@@ -141,22 +141,22 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
/* relocate the PLT: compute addresses and offsets in the PLT now that final
address for PLT and GOT are known (see fill_program_header) */
ST_FUNC void relocate_plt(TCCState *s1)
ST_FUNC void relocate_plt(TCCState *S)
{
uint8_t *p, *p_end;
if (!s1->plt)
if (!S->plt)
return;
p = s1->plt->data;
p_end = p + s1->plt->data_offset;
p = S->plt->data;
p_end = p + S->plt->data_offset;
if (p < p_end) {
int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
write32le(s1->plt->data + 16, x - 4);
int x = S->got->sh_addr - S->plt->sh_addr - 12;
write32le(S->plt->data + 16, x - 4);
p += 20;
while (p < p_end) {
unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4;
unsigned off = x + read32le(p + 4) + (S->plt->data - p) + 4;
if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
p += 4;
write32le(p, 0xe28fc200 | ((off >> 28) & 0xf)); // add ip, pc, #0xN0000000
......@@ -167,17 +167,17 @@ ST_FUNC void relocate_plt(TCCState *s1)
}
}
if (s1->plt->reloc) {
if (S->plt->reloc) {
ElfW_Rel *rel;
p = s1->got->data;
for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
write32le(p + rel->r_offset, s1->plt->sh_addr);
p = S->got->data;
for_each_elem(S->plt->reloc, 0, rel, ElfW_Rel) {
write32le(p + rel->r_offset, S->plt->sh_addr);
}
}
}
#endif
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
void relocate(TCCState *S, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
{
ElfW(Sym) *sym;
int sym_index, esym_index;
......@@ -212,7 +212,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
h = x & 2;
th_ko = (x & 3) && (!blx_avail || !is_call);
if (th_ko || x >= 0x2000000 || x < -0x2000000)
tcc_error("can't relocate value at %x,%d",addr, type);
tcc_error(S, "can't relocate value at %x,%d",addr, type);
x >>= 2;
x &= 0xffffff;
/* Only reached if blx is avail and it is a call */
......@@ -255,7 +255,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
/* Relocation infos */
to_thumb = val & 1;
plt = s1->plt;
plt = S->plt;
to_plt = (val >= plt->sh_addr) &&
(val < plt->sh_addr + plt->data_offset);
is_call = (type == R_ARM_THM_PC22);
......@@ -267,10 +267,10 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
Section *text;
name = (char *) symtab_section->link->data + sym->st_name;
text = s1->sections[sym->st_shndx];
text = S->sections[sym->st_shndx];
/* Modify reloc to target a thumb stub to switch to ARM */
snprintf(buf, sizeof(buf), "%s_from_thumb", name);
index = put_elf_sym(symtab_section,
index = put_elf_sym(S, symtab_section,
text->data_offset + 1,
sym->st_size, sym->st_info, 0,
sym->st_shndx, buf);
......@@ -281,7 +281,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
put_elf_reloc(symtab_section, text,
text->data_offset + 4, R_ARM_JUMP24,
sym_index);
p = section_ptr_add(text, 8);
p = section_ptr_add(S, text, 8);
write32le(p, 0x4778); /* bx pc */
write32le(p+2, 0x46c0); /* nop */
write32le(p+4, 0xeafffffe); /* b $sym */
......@@ -301,7 +301,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
- instruction must be a call (bl) or a jump to PLT */
if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
if (to_thumb || (val & 2) || (!is_call && !to_plt))
tcc_error("can't relocate value at %x,%d",addr, type);
tcc_error(S, "can't relocate value at %x,%d",addr, type);
/* Compute and store final offset */
s = (x >> 24) & 1;
......@@ -372,14 +372,14 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
x = (x * 2) / 2;
x += val - addr;
if((x^(x>>1))&0x40000000)
tcc_error("can't relocate value at %x,%d",addr, type);
tcc_error(S, "can't relocate value at %x,%d",addr, type);
(*(int *)ptr) |= x & 0x7fffffff;
}
return;
case R_ARM_ABS32:
case R_ARM_TARGET1:
if (s1->output_type == TCC_OUTPUT_DLL) {
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
if (S->output_type == TCC_OUTPUT_DLL) {
esym_index = get_sym_attr(S, sym_index, 0)->dyn_index;
qrel->r_offset = rel->r_offset;
if (esym_index) {
qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
......@@ -396,19 +396,19 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
*(int *)ptr += val - addr;
return;
case R_ARM_GOTPC:
*(int *)ptr += s1->got->sh_addr - addr;
*(int *)ptr += S->got->sh_addr - addr;
return;
case R_ARM_GOTOFF:
*(int *)ptr += val - s1->got->sh_addr;
*(int *)ptr += val - S->got->sh_addr;
return;
case R_ARM_GOT32:
/* we load the got offset */
*(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
*(int *)ptr += get_sym_attr(S, sym_index, 0)->got_offset;
return;
case R_ARM_GOT_PREL:
/* we load the pc relative got offset */
*(int *)ptr += s1->got->sh_addr +
get_sym_attr(s1, sym_index, 0)->got_offset -
*(int *)ptr += S->got->sh_addr +
get_sym_attr(S, sym_index, 0)->got_offset -
addr;
return;
case R_ARM_COPY:
......@@ -428,7 +428,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
return;
case R_ARM_RELATIVE:
#ifdef TCC_TARGET_PE
add32le(ptr, val - s1->pe_imagebase);
add32le(ptr, val - S->pe_imagebase);
#endif
/* do nothing */
return;
......
......@@ -9,9 +9,9 @@
#define CONFIG_TCC_ASM
#define NB_ASM_REGS 16
ST_FUNC void g(int c);
ST_FUNC void gen_le16(int c);
ST_FUNC void gen_le32(int c);
ST_FUNC void g(TCCState* S, int c);
ST_FUNC void gen_le16(TCCState* S, int c);
ST_FUNC void gen_le32(TCCState* S, int c);
/*************************************************************/
#else
......@@ -25,7 +25,7 @@ static void asm_error(void)
}
/* XXX: make it faster ? */
ST_FUNC void g(int c)
ST_FUNC void g(TCCState* S, int c)
{
int ind1;
if (nocode_wanted)
......@@ -37,24 +37,24 @@ ST_FUNC void g(int c)
ind = ind1;
}
ST_FUNC void gen_le16 (int i)
ST_FUNC void gen_le16 (TCCState* S, int i)
{
g(i);
g(i>>8);
g(S, i);
g(S, i>>8);
}
ST_FUNC void gen_le32 (int i)
ST_FUNC void gen_le32 (TCCState* S, int i)
{
gen_le16(i);
gen_le16(i>>16);
gen_le16(S, i);
gen_le16(S, i>>16);
}
ST_FUNC void gen_expr32(ExprValue *pe)
ST_FUNC void gen_expr32(TCCState* S, ExprValue *pe)
{
gen_le32(pe->v);
gen_le32(S, pe->v);
}
ST_FUNC void asm_opcode(TCCState *s1, int opcode)
ST_FUNC void asm_opcode(TCCState *S, int opcode)
{
asm_error();
}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment