2016-12-05 20:40:59 +00:00
|
|
|
#ifdef TARGET_DEFS_ONLY
|
|
|
|
|
|
|
|
#define EM_TCC_TARGET EM_386
|
|
|
|
|
|
|
|
/* relocation type for 32 bit data relocation */
|
|
|
|
#define R_DATA_32 R_386_32
|
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 07:10:37 +00:00
|
|
|
#define R_DATA_32U R_386_32
|
2016-12-05 20:40:59 +00:00
|
|
|
#define R_DATA_PTR R_386_32
|
|
|
|
#define R_JMP_SLOT R_386_JMP_SLOT
|
|
|
|
#define R_GLOB_DAT R_386_GLOB_DAT
|
|
|
|
#define R_COPY R_386_COPY
|
2017-05-07 02:41:40 +00:00
|
|
|
#define R_RELATIVE R_386_RELATIVE
|
2016-12-05 20:40:59 +00:00
|
|
|
|
|
|
|
#define R_NUM R_386_NUM
|
|
|
|
|
|
|
|
#define ELF_START_ADDR 0x08048000
|
|
|
|
#define ELF_PAGE_SIZE 0x1000
|
|
|
|
|
2016-12-10 09:14:29 +00:00
|
|
|
#define PCRELATIVE_DLLPLT 0
|
2020-12-18 14:24:32 +00:00
|
|
|
#define RELOCATE_DLLPLT 1
|
2016-11-12 15:16:06 +00:00
|
|
|
|
2016-12-05 20:40:59 +00:00
|
|
|
#else /* !TARGET_DEFS_ONLY */
|
|
|
|
|
|
|
|
#include "tcc.h"
|
|
|
|
|
2019-12-09 19:32:13 +00:00
|
|
|
#ifndef ELF_OBJ_ONLY
|
2016-12-10 17:22:08 +00:00
|
|
|
/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
|
|
|
|
relocations, returns -1. */
|
|
|
|
int code_reloc (int reloc_type)
|
|
|
|
{
|
|
|
|
switch (reloc_type) {
|
2016-12-15 16:01:22 +00:00
|
|
|
case R_386_RELATIVE:
|
2016-12-10 17:22:08 +00:00
|
|
|
case R_386_16:
|
|
|
|
case R_386_32:
|
|
|
|
case R_386_GOTPC:
|
|
|
|
case R_386_GOTOFF:
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_GOT32X:
|
|
|
|
case R_386_GLOB_DAT:
|
|
|
|
case R_386_COPY:
|
2020-09-08 12:31:58 +00:00
|
|
|
case R_386_TLS_GD:
|
|
|
|
case R_386_TLS_LDM:
|
|
|
|
case R_386_TLS_LDO_32:
|
|
|
|
case R_386_TLS_LE:
|
2016-12-10 17:22:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case R_386_PC16:
|
|
|
|
case R_386_PC32:
|
|
|
|
case R_386_PLT32:
|
|
|
|
case R_386_JMP_SLOT:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-08 04:38:09 +00:00
|
|
|
/* Returns an enumerator to describe whether and when the relocation needs a
|
2016-12-10 17:22:08 +00:00
|
|
|
GOT and/or PLT entry to be created. See tcc.h for a description of the
|
|
|
|
different values. */
|
|
|
|
int gotplt_entry_type (int reloc_type)
|
|
|
|
{
|
|
|
|
switch (reloc_type) {
|
2016-12-15 16:01:22 +00:00
|
|
|
case R_386_RELATIVE:
|
2016-12-10 17:22:08 +00:00
|
|
|
case R_386_16:
|
|
|
|
case R_386_GLOB_DAT:
|
|
|
|
case R_386_JMP_SLOT:
|
|
|
|
case R_386_COPY:
|
|
|
|
return NO_GOTPLT_ENTRY;
|
|
|
|
|
2016-12-18 04:18:19 +00:00
|
|
|
case R_386_32:
|
|
|
|
/* This relocations shouldn't normally need GOT or PLT
|
|
|
|
slots if it weren't for simplicity in the code generator.
|
|
|
|
See our caller for comments. */
|
|
|
|
return AUTO_GOTPLT_ENTRY;
|
|
|
|
|
2016-12-10 17:22:08 +00:00
|
|
|
case R_386_PC16:
|
|
|
|
case R_386_PC32:
|
|
|
|
return AUTO_GOTPLT_ENTRY;
|
|
|
|
|
|
|
|
case R_386_GOTPC:
|
|
|
|
case R_386_GOTOFF:
|
|
|
|
return BUILD_GOT_ONLY;
|
|
|
|
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_GOT32X:
|
|
|
|
case R_386_PLT32:
|
2020-09-08 12:31:58 +00:00
|
|
|
case R_386_TLS_GD:
|
|
|
|
case R_386_TLS_LDM:
|
|
|
|
case R_386_TLS_LDO_32:
|
|
|
|
case R_386_TLS_LE:
|
2016-12-10 17:22:08 +00:00
|
|
|
return ALWAYS_GOTPLT_ENTRY;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-10-22 05:39:54 +00:00
|
|
|
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
|
2016-12-15 16:01:22 +00:00
|
|
|
{
|
2021-10-22 05:39:54 +00:00
|
|
|
Section *plt = s1->plt;
|
2016-12-15 16:01:22 +00:00
|
|
|
uint8_t *p;
|
|
|
|
int modrm;
|
|
|
|
unsigned plt_offset, relofs;
|
|
|
|
|
|
|
|
/* on i386 if we build a DLL, we add a %ebx offset */
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->output_type == TCC_OUTPUT_DLL)
|
2016-12-15 16:01:22 +00:00
|
|
|
modrm = 0xa3;
|
|
|
|
else
|
|
|
|
modrm = 0x25;
|
|
|
|
|
2017-05-08 04:38:09 +00:00
|
|
|
/* empty PLT: create PLT0 entry that pushes the library identifier
|
2016-12-15 16:01:22 +00:00
|
|
|
(GOT + PTR_SIZE) and jumps to ld.so resolution routine
|
|
|
|
(GOT + 2 * PTR_SIZE) */
|
|
|
|
if (plt->data_offset == 0) {
|
2021-10-22 05:39:54 +00:00
|
|
|
p = section_ptr_add(plt, 16);
|
2016-12-15 16:01:22 +00:00
|
|
|
p[0] = 0xff; /* pushl got + PTR_SIZE */
|
|
|
|
p[1] = modrm + 0x10;
|
|
|
|
write32le(p + 2, PTR_SIZE);
|
|
|
|
p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
|
|
|
|
p[7] = modrm;
|
|
|
|
write32le(p + 8, PTR_SIZE * 2);
|
|
|
|
}
|
|
|
|
plt_offset = plt->data_offset;
|
|
|
|
|
|
|
|
/* The PLT slot refers to the relocation entry it needs via offset.
|
|
|
|
The reloc entry is created below, so its offset is the current
|
|
|
|
data_offset */
|
2021-10-22 05:39:54 +00:00
|
|
|
relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0;
|
2016-12-15 16:01:22 +00:00
|
|
|
|
|
|
|
/* Jump to GOT entry where ld.so initially put the address of ip + 4 */
|
2021-10-22 05:39:54 +00:00
|
|
|
p = section_ptr_add(plt, 16);
|
2016-12-15 16:01:22 +00:00
|
|
|
p[0] = 0xff; /* jmp *(got + x) */
|
|
|
|
p[1] = modrm;
|
|
|
|
write32le(p + 2, got_offset);
|
|
|
|
p[6] = 0x68; /* push $xxx */
|
2020-12-18 14:24:32 +00:00
|
|
|
write32le(p + 7, relofs - sizeof (ElfW_Rel));
|
2016-12-15 16:01:22 +00:00
|
|
|
p[11] = 0xe9; /* jmp plt_start */
|
|
|
|
write32le(p + 12, -(plt->data_offset));
|
|
|
|
return plt_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* relocate the PLT: compute addresses and offsets in the PLT now that final
|
|
|
|
address for PLT and GOT are known (see fill_program_header) */
|
2021-10-22 05:39:54 +00:00
|
|
|
ST_FUNC void relocate_plt(TCCState *s1)
|
2016-12-15 16:01:22 +00:00
|
|
|
{
|
|
|
|
uint8_t *p, *p_end;
|
|
|
|
|
2021-10-22 05:39:54 +00:00
|
|
|
if (!s1->plt)
|
2016-12-15 16:01:22 +00:00
|
|
|
return;
|
|
|
|
|
2021-10-22 05:39:54 +00:00
|
|
|
p = s1->plt->data;
|
|
|
|
p_end = p + s1->plt->data_offset;
|
2016-12-15 16:01:22 +00:00
|
|
|
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->output_type != TCC_OUTPUT_DLL && p < p_end) {
|
|
|
|
add32le(p + 2, s1->got->sh_addr);
|
|
|
|
add32le(p + 8, s1->got->sh_addr);
|
2016-12-15 16:01:22 +00:00
|
|
|
p += 16;
|
|
|
|
while (p < p_end) {
|
2021-10-22 05:39:54 +00:00
|
|
|
add32le(p + 2, s1->got->sh_addr);
|
2016-12-15 16:01:22 +00:00
|
|
|
p += 16;
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 14:24:32 +00:00
|
|
|
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->plt->reloc) {
|
2020-12-18 14:24:32 +00:00
|
|
|
ElfW_Rel *rel;
|
2021-10-22 05:39:54 +00:00
|
|
|
int x = s1->plt->sh_addr + 16 + 6;
|
|
|
|
p = s1->got->data;
|
|
|
|
for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
|
2021-01-28 13:20:05 +00:00
|
|
|
write32le(p + rel->r_offset, x);
|
2020-12-18 14:24:32 +00:00
|
|
|
x += 16;
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 16:01:22 +00:00
|
|
|
}
|
2019-12-09 19:32:13 +00:00
|
|
|
#endif
|
2019-06-23 00:10:10 +00:00
|
|
|
|
2021-10-22 05:39:54 +00:00
|
|
|
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
|
2016-11-12 15:16:06 +00:00
|
|
|
{
|
|
|
|
int sym_index, esym_index;
|
|
|
|
|
|
|
|
sym_index = ELFW(R_SYM)(rel->r_info);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case R_386_32:
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->output_type == TCC_OUTPUT_DLL) {
|
|
|
|
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
|
2016-11-12 15:16:06 +00:00
|
|
|
qrel->r_offset = rel->r_offset;
|
|
|
|
if (esym_index) {
|
|
|
|
qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
|
|
|
|
qrel++;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
|
|
|
|
qrel++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add32le(ptr, val);
|
|
|
|
return;
|
|
|
|
case R_386_PC32:
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->output_type == TCC_OUTPUT_DLL) {
|
2016-11-12 15:16:06 +00:00
|
|
|
/* DLL relocation */
|
2021-10-22 05:39:54 +00:00
|
|
|
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
|
2016-11-12 15:16:06 +00:00
|
|
|
if (esym_index) {
|
|
|
|
qrel->r_offset = rel->r_offset;
|
|
|
|
qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
|
|
|
|
qrel++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add32le(ptr, val - addr);
|
|
|
|
return;
|
|
|
|
case R_386_PLT32:
|
|
|
|
add32le(ptr, val - addr);
|
|
|
|
return;
|
|
|
|
case R_386_GLOB_DAT:
|
|
|
|
case R_386_JMP_SLOT:
|
|
|
|
write32le(ptr, val);
|
|
|
|
return;
|
|
|
|
case R_386_GOTPC:
|
2021-10-22 05:39:54 +00:00
|
|
|
add32le(ptr, s1->got->sh_addr - addr);
|
2016-11-12 15:16:06 +00:00
|
|
|
return;
|
|
|
|
case R_386_GOTOFF:
|
2021-10-22 05:39:54 +00:00
|
|
|
add32le(ptr, val - s1->got->sh_addr);
|
2016-11-12 15:16:06 +00:00
|
|
|
return;
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_GOT32X:
|
|
|
|
/* we load the got offset */
|
2021-10-22 05:39:54 +00:00
|
|
|
add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
|
2016-11-12 15:16:06 +00:00
|
|
|
return;
|
|
|
|
case R_386_16:
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
|
2016-11-12 15:16:06 +00:00
|
|
|
output_file:
|
2021-10-22 05:39:54 +00:00
|
|
|
tcc_error("can only produce 16-bit binary files");
|
2016-11-12 15:16:06 +00:00
|
|
|
}
|
|
|
|
write16le(ptr, read16le(ptr) + val);
|
|
|
|
return;
|
|
|
|
case R_386_PC16:
|
2021-10-22 05:39:54 +00:00
|
|
|
if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
|
2016-11-12 15:16:06 +00:00
|
|
|
goto output_file;
|
|
|
|
write16le(ptr, read16le(ptr) + val - addr);
|
|
|
|
return;
|
|
|
|
case R_386_RELATIVE:
|
2017-12-12 16:57:20 +00:00
|
|
|
#ifdef TCC_TARGET_PE
|
2021-10-22 05:39:54 +00:00
|
|
|
add32le(ptr, val - s1->pe_imagebase);
|
2017-12-12 16:57:20 +00:00
|
|
|
#endif
|
2016-11-12 15:16:06 +00:00
|
|
|
/* do nothing */
|
|
|
|
return;
|
|
|
|
case R_386_COPY:
|
2017-05-08 04:38:09 +00:00
|
|
|
/* This relocation must copy initialized data from the library
|
2016-11-12 15:16:06 +00:00
|
|
|
to the program .bss segment. Currently made like for ARM
|
2017-05-08 04:38:09 +00:00
|
|
|
(to remove noise of default case). Is this true?
|
2016-11-12 15:16:06 +00:00
|
|
|
*/
|
|
|
|
return;
|
2020-09-08 12:31:58 +00:00
|
|
|
case R_386_TLS_GD:
|
|
|
|
{
|
|
|
|
static const unsigned char expect[] = {
|
|
|
|
/* lea 0(,%ebx,1),%eax */
|
|
|
|
0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
/* call __tls_get_addr@PLT */
|
|
|
|
0xe8, 0xfc, 0xff, 0xff, 0xff };
|
|
|
|
static const unsigned char replace[] = {
|
|
|
|
/* mov %gs:0,%eax */
|
|
|
|
0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
/* sub 0,%eax */
|
|
|
|
0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
|
|
if (memcmp (ptr-3, expect, sizeof(expect)) == 0) {
|
|
|
|
ElfW(Sym) *sym;
|
|
|
|
Section *sec;
|
|
|
|
int32_t x;
|
|
|
|
|
|
|
|
memcpy(ptr-3, replace, sizeof(replace));
|
|
|
|
rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
|
|
|
|
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
|
2021-10-22 05:39:54 +00:00
|
|
|
sec = s1->sections[sym->st_shndx];
|
2020-09-08 12:31:58 +00:00
|
|
|
x = sym->st_value - sec->sh_addr - sec->data_offset;
|
|
|
|
add32le(ptr + 5, -x);
|
|
|
|
}
|
|
|
|
else
|
2021-10-22 05:39:54 +00:00
|
|
|
tcc_error("unexpected R_386_TLS_GD pattern");
|
2020-09-08 12:31:58 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case R_386_TLS_LDM:
|
|
|
|
{
|
|
|
|
static const unsigned char expect[] = {
|
|
|
|
/* lea 0(%ebx),%eax */
|
|
|
|
0x8d, 0x83, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
/* call __tls_get_addr@PLT */
|
|
|
|
0xe8, 0xfc, 0xff, 0xff, 0xff };
|
|
|
|
static const unsigned char replace[] = {
|
|
|
|
/* mov %gs:0,%eax */
|
|
|
|
0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
/* nop */
|
|
|
|
0x90,
|
|
|
|
/* lea 0(%esi,%eiz,1),%esi */
|
|
|
|
0x8d, 0x74, 0x26, 0x00 };
|
|
|
|
|
|
|
|
if (memcmp (ptr-2, expect, sizeof(expect)) == 0) {
|
|
|
|
memcpy(ptr-2, replace, sizeof(replace));
|
|
|
|
rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
|
|
|
|
}
|
|
|
|
else
|
2021-10-22 05:39:54 +00:00
|
|
|
tcc_error("unexpected R_386_TLS_LDM pattern");
|
2020-09-08 12:31:58 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case R_386_TLS_LDO_32:
|
|
|
|
case R_386_TLS_LE:
|
|
|
|
{
|
|
|
|
ElfW(Sym) *sym;
|
|
|
|
Section *sec;
|
|
|
|
int32_t x;
|
|
|
|
|
|
|
|
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
|
2021-10-22 05:39:54 +00:00
|
|
|
sec = s1->sections[sym->st_shndx];
|
2020-09-08 12:31:58 +00:00
|
|
|
x = val - sec->sh_addr - sec->data_offset;
|
|
|
|
add32le(ptr, x);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case R_386_NONE:
|
|
|
|
return;
|
2016-11-12 15:16:06 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
|
|
|
|
type, (unsigned)addr, ptr, (unsigned)val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 20:40:59 +00:00
|
|
|
|
|
|
|
#endif /* !TARGET_DEFS_ONLY */
|