tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] tcc --emit-relocs


From: Barath Aron
Subject: [Tinycc-devel] tcc --emit-relocs
Date: Tue, 16 May 2017 09:24:04 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1

Hi,

The OS that I use loads all executable to random addresses and relocates them. There are a few reasons behind this, and it isn't important. Executables built with gcc works fine if the "-Wl,--emit-relocs" is passed. TCC compiles a good relocatable ELF, except it removes the relocation entries related to the data section.
For instance, if the program contains some stuff like this:

char* my_str = "Hello!";

will crash at runtime due to the totally different addresses stored in the data section and in the reality. For example, the ELF could be loaded at 0x10001AA0000 or at 0x10003F40000. The actual load address is unknown at compile time for sure.

I have a hack that allows to keep the data relocations I want, but I'm pretty sure the patch won't work for all the cases. I'm was in a rush to make tcc work on the OS I use, so here it is [1] (in svn diff format).

Can you help me with? :)

Áron



 [1]:


Index: libtcc.c
===================================================================
--- libtcc.c    (revision 22252)
+++ libtcc.c    (working copy)
@@ -1439,6 +1439,8 @@
             ignoring = 1;
         } else if (link_option(option, "O", &p)) {
             ignoring = 1;
+ } else if (link_option(option, "q", &p) || link_option(option, "emit-relocs", &p)) {
+            s->emit_relocs = 1;
         } else if (link_option(option, "rpath=", &p)) {
             s->rpath = copy_linker_arg(p);
         } else if (link_option(option, "section-alignment=", &p)) {
Index: tcc.h
===================================================================
--- tcc.h    (revision 22252)
+++ tcc.h    (working copy)
@@ -600,6 +600,7 @@
     int rdynamic; /* if true, all symbols are exported */
int symbolic; /* if true, resolve symbols in the current module first */ int alacarte_link; /* if true, only link in referenced objects from archive */ + int emit_relocs; /* if true, leave relocation sections and contets in fully linked executables (ELF only) */

     char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
     char *soname; /* as specified on the command line (-soname) */
Index: tccelf.c
===================================================================
--- tccelf.c    (revision 22252)
+++ tccelf.c    (working copy)
@@ -48,6 +48,8 @@

 ST_FUNC void tccelf_new(TCCState *s)
 {
+    int symtab_sh_flags = 0;
+
     /* no section zero */
     dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);

@@ -57,7 +59,9 @@
bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);

     /* symbols are always generated for linking stage */
-    symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
+    if(s->emit_relocs)
+        symtab_sh_flags |= SHF_ALLOC;
+    symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, symtab_sh_flags,
                                 ".strtab",
                                 ".hashtab", SHF_PRIVATE);
     strtab_section = symtab_section->link;
@@ -787,10 +791,10 @@
         tgt += rel->r_addend;
 #endif
         addr = s->sh_addr + rel->r_offset;
-    relocate(s1, rel, type, ptr, addr, tgt);
+        relocate(s1, rel, type, ptr, addr, tgt);
     }
     /* if the relocation is allocated, we change its symbol table */
-    if (sr->sh_flags & SHF_ALLOC)
+    if (!s->emit_relocs && (sr->sh_flags & SHF_ALLOC))
         sr->link = s1->dynsym;
 }






reply via email to

[Prev in Thread] Current Thread [Next in Thread]