Clean tccelf.c

- remove debug printf and commented out code
- remove C++-like comments
- remove whitespace at end of lines
- replace tabs by spaces
This commit is contained in:
Thomas Preud'homme 2013-12-17 20:59:14 +08:00
parent 6f3569e4e2
commit 3d4b57ffe3

176
tccelf.c
View file

@ -588,13 +588,13 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
case R_386_16:
if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
output_file:
tcc_error("can only produce 16-bit binary files");
tcc_error("can only produce 16-bit binary files");
}
*(short *)ptr += val;
break;
case R_386_PC16:
if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
goto output_file;
goto output_file;
*(short *)ptr += val - addr;
break;
#elif defined(TCC_TARGET_ARM)
@ -642,7 +642,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
guard its use */
case R_ARM_THM_PC22:
case R_ARM_THM_JUMP24:
{
{
int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
Section *plt;
@ -720,7 +720,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
*(int *)ptr |= x;
else
*(int *)ptr += x;
}
}
break;
case R_ARM_THM_MOVT_ABS:
case R_ARM_THM_MOVW_ABS_NC:
@ -737,7 +737,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
*(int *)ptr |= x;
else
*(int *)ptr += x;
}
}
break;
case R_ARM_PREL31:
{
@ -785,13 +785,13 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
{
uint32_t orig;
/* put the low 16 bits of the absolute address */
// add to what is already there
/* put the low 16 bits of the absolute address
add to what is already there */
orig = ((*(int *)(ptr )) >> 7) & 0xffff;
orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16;
//patch both at once - assumes always in pairs Low - High
/* patch both at once - assumes always in pairs Low - High */
*(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | (((val+orig) & 0xffff) << 7);
*(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | ((((val+orig)>>16) & 0xffff) << 7);
@ -1110,14 +1110,14 @@ static void put_got_entry(TCCState *s1,
if (s1->sym_attrs[sym_index].plt_thumb_stub) {
p = section_ptr_add(plt, 20);
put32(p , 0x4778); // bx pc
put32(p+2, 0x46c0); // nop
put32(p , 0x4778); /* bx pc */
put32(p+2, 0x46c0); /* nop */
p += 4;
} else
p = section_ptr_add(plt, 16);
put32(p , 0xe59fc004); // ldr ip, [pc, #4] // offset in GOT
put32(p+4, 0xe08fc00c); // add ip, pc, ip // absolute address or offset
put32(p+8, 0xe59cf000); // ldr pc, [ip] // load absolute address or load offset
put32(p , 0xe59fc004); /* ldr ip, [pc, #4] ; offset in GOT */
put32(p+4, 0xe08fc00c); /* add ip, pc, ip ; absolute address or offset */
put32(p+8, 0xe59cf000); /* ldr pc, [ip] ; load absolute address or load offset */
put32(p+12, s1->got->data_offset);
/* the symbol is modified so that it will be relocated to
@ -1459,8 +1459,8 @@ static void tcc_output_binary(TCCState *s1, FILE *f,
}
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#define HAVE_PHDR 1
#define EXTRA_RELITEMS 14
#define HAVE_PHDR 1
#define EXTRA_RELITEMS 14
/* move the relocation value from .dynsym to .got */
void patch_dynsym_undef(TCCState *s1, Section *s)
@ -1468,19 +1468,19 @@ void patch_dynsym_undef(TCCState *s1, Section *s)
uint32_t *gotd = (void *)s1->got->data;
ElfW(Sym) *sym, *sym_end;
gotd += 3; // dummy entries in .got
gotd += 3; /* dummy entries in .got */
/* relocate symbols in .dynsym */
sym_end = (ElfW(Sym) *)(s->data + s->data_offset);
for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) {
if (sym->st_shndx == SHN_UNDEF) {
*gotd++ = sym->st_value + 6; // XXX 6 is magic ?
sym->st_value = 0;
}
if (sym->st_shndx == SHN_UNDEF) {
*gotd++ = sym->st_value + 6; /* XXX 6 is magic ? */
sym->st_value = 0;
}
}
}
#else
#define HAVE_PHDR 0
#define EXTRA_RELITEMS 9
#define HAVE_PHDR 0
#define EXTRA_RELITEMS 9
/* zero plt offsets of weak symbols in .dynsym */
void patch_dynsym_undef(TCCState *s1, Section *s)
@ -1496,45 +1496,45 @@ void patch_dynsym_undef(TCCState *s1, Section *s)
ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
{
int sym_index = ELFW(R_SYM) (rel->r_info);
ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
unsigned long offset;
int sym_index = ELFW(R_SYM) (rel->r_info);
ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
unsigned long offset;
if (sym_index >= s1->nb_sym_attrs)
return;
offset = s1->sym_attrs[sym_index].got_offset;
section_reserve(s1->got, offset + PTR_SIZE);
if (sym_index >= s1->nb_sym_attrs)
return;
offset = s1->sym_attrs[sym_index].got_offset;
section_reserve(s1->got, offset + PTR_SIZE);
#ifdef TCC_TARGET_X86_64
/* only works for x86-64 */
put32(s1->got->data + offset + 4, sym->st_value >> 32);
/* only works for x86-64 */
put32(s1->got->data + offset + 4, sym->st_value >> 32);
#endif
put32(s1->got->data + offset, sym->st_value & 0xffffffff);
put32(s1->got->data + offset, sym->st_value & 0xffffffff);
}
ST_FUNC void fill_got(TCCState *s1)
{
Section *s;
ElfW_Rel *rel, *rel_end;
int i;
Section *s;
ElfW_Rel *rel, *rel_end;
int i;
for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i];
if (s->sh_type != SHT_RELX)
continue;
/* no need to handle got relocations */
if (s->link != symtab_section)
continue;
rel_end = (ElfW_Rel *) (s->data + s->data_offset);
for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) {
switch (ELFW(R_TYPE) (rel->r_info)) {
case R_X86_64_GOT32:
case R_X86_64_GOTPCREL:
case R_X86_64_PLT32:
fill_got_entry(s1, rel);
break;
}
}
}
for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i];
if (s->sh_type != SHT_RELX)
continue;
/* no need to handle got relocations */
if (s->link != symtab_section)
continue;
rel_end = (ElfW_Rel *) (s->data + s->data_offset);
for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) {
switch (ELFW(R_TYPE) (rel->r_info)) {
case R_X86_64_GOT32:
case R_X86_64_GOTPCREL:
case R_X86_64_PLT32:
fill_got_entry(s1, rel);
break;
}
}
}
}
@ -1587,10 +1587,10 @@ static int elf_output_file(TCCState *s1, const char *filename)
if (file_type == TCC_OUTPUT_EXE) {
char *ptr;
/* allow override the dynamic loader */
const char *elfint = getenv("LD_SO");
if (elfint == NULL)
elfint = DEFAULT_ELFINTERP(s1);
/* allow override the dynamic loader */
const char *elfint = getenv("LD_SO");
if (elfint == NULL)
elfint = DEFAULT_ELFINTERP(s1);
/* add interpreter section only if executable */
interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
interp->sh_addralign = 1;
@ -1655,7 +1655,7 @@ static int elf_output_file(TCCState *s1, const char *filename)
index = put_elf_sym(s1->dynsym, offset, esym->st_size,
esym->st_info, 0,
bss_section->sh_num, name);
// Ensure R_COPY works for weak symbol aliases
/* Ensure R_COPY works for weak symbol aliases */
if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
dynsym_end = (ElfW(Sym) *)
(s1->dynsymtab_section->data +
@ -1839,21 +1839,12 @@ static int elf_output_file(TCCState *s1, const char *filename)
for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i];
s->sh_name = put_elf_str(strsec, s->name);
#if 0 /* gr */
printf("section: f=%08x t=%08x i=%08x %s %s\n",
s->sh_flags,
s->sh_type,
s->sh_info,
s->name,
s->reloc ? s->reloc->name : "n"
);
#endif
/* 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 */
/* gr: avoid bogus relocs for empty (debug) sections */
if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
prepare_dynamic_rel(s1, s);
else if (s1->do_debug)
@ -1969,14 +1960,14 @@ static int elf_output_file(TCCState *s1, const char *filename)
/* update dynamic relocation infos */
if (s->sh_type == SHT_RELX) {
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
if (!strcmp(strsec->data + s->sh_name, ".rel.got")) { // rel_size == 0) {
if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
rel_addr = addr;
rel_size += s->sh_size; // XXX only first rel.
}
if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) { // rel_size == 0) {
rel_size += s->sh_size; /* XXX only first rel. */
}
if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
bss_addr = addr;
bss_size = s->sh_size; // XXX only first rel.
}
bss_size = s->sh_size; /* XXX only first rel. */
}
#else
if (rel_size == 0)
rel_addr = addr;
@ -2010,18 +2001,18 @@ static int elf_output_file(TCCState *s1, const char *filename)
ph = &phdr[0];
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
{
int len = phnum * sizeof(ElfW(Phdr));
{
int len = phnum * sizeof(ElfW(Phdr));
ph->p_type = PT_PHDR;
ph->p_offset = sizeof(ElfW(Ehdr));
ph->p_vaddr = interp->sh_addr - len;
ph->p_paddr = ph->p_vaddr;
ph->p_filesz = ph->p_memsz = len;
ph->p_flags = PF_R | PF_X;
ph->p_align = 4; // interp->sh_addralign;
ph++;
}
ph->p_type = PT_PHDR;
ph->p_offset = sizeof(ElfW(Ehdr));
ph->p_vaddr = interp->sh_addr - len;
ph->p_paddr = ph->p_vaddr;
ph->p_filesz = ph->p_memsz = len;
ph->p_flags = PF_R | PF_X;
ph->p_align = 4; /* interp->sh_addralign; */
ph++;
}
#endif
ph->p_type = PT_INTERP;
@ -2518,7 +2509,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1,
next: ;
}
/* //gr relocate stab strings */
/* gr relocate stab strings */
if (stab_index && stabstr_index) {
Stab_Sym *a, *b;
unsigned o;
@ -2690,9 +2681,6 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size)
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
if(sym->st_shndx == SHN_UNDEF) {
off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
#if 0
printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);
#endif
++bound;
lseek(fd, off, SEEK_SET);
if(tcc_load_object_file(s1, fd, off) < 0) {
@ -2740,7 +2728,6 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
break;
}
ar_name[i + 1] = '\0';
// printf("name='%s' size=%d %s\n", ar_name, size, ar_size);
file_offset = lseek(fd, 0, SEEK_CUR);
/* align to even */
size = (size + 1) & ~1;
@ -2833,8 +2820,6 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
}
}
// printf("loading dll '%s'\n", soname);
/* add the dll and its level */
dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
dllref->level = level;
@ -2993,11 +2978,6 @@ static int ld_next(TCCState *s1, char *name, int name_size)
inp();
break;
}
#if 0
printf("tok=%c %d\n", c, c);
if (c == LD_TOK_NAME)
printf(" name=%s\n", name);
#endif
return c;
}
@ -3136,4 +3116,4 @@ ST_FUNC int tcc_load_ldscript(TCCState *s1)
}
return 0;
}
#endif /* ndef TCC_TARGET_PE */
#endif /* !TCC_TARGET_PE */