tinycc-devel
[Top][All Lists]
Advanced

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

Re: [Tinycc-devel] Argh! No PIE on musl libC (i give up)


From: Michael Matz
Subject: Re: [Tinycc-devel] Argh! No PIE on musl libC (i give up)
Date: Mon, 9 Oct 2017 21:58:46 +0200 (CEST)
User-agent: Alpine 2.21 (LSU 202 2017-01-01)

Hi,

On Thu, 5 Oct 2017, Steffen Nurpmeso wrote:

> But so i tried a simple-minded approach which surprisingly simple
> ends up as

Yeah, that's not enough.  You need to adjust relocation processing to be 
more like TCC_OUTPUT_DLL mode (which then eventually results in unoptimal 
but working PIEs).  At the very least you need the hunks below in addition 
to what you had.  As is this wouldn't be ready for mob.

Instead of this it'd probably be nicer to just figure out why musls ldd 
doesn't like the executables we produce by default.


Ciao,
Michael.
diff --git a/libtcc.c b/libtcc.c
index 3adb793..5c4a76a 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -992,7 +992,7 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int 
output_type)
     if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
         !s->nostdlib) {
         if (output_type != TCC_OUTPUT_DLL)
-            tcc_add_crt(s, "crt1.o");
+            tcc_add_crt(s, "Scrt1.o");
         tcc_add_crt(s, "crti.o");
     }
 #endif
diff --git a/tccelf.c b/tccelf.c
index e2ba5a4..a539986 100644
--- a/tccelf.c
+++ b/tccelf.c
@@ -1015,8 +1015,10 @@ ST_FUNC void build_got_entries(TCCState *s1)
             attr = put_got_entry(s1, reloc_type, type, sym->st_size, 
sym->st_info,
                                  sym_index);
 
-            if (reloc_type == R_JMP_SLOT)
+            if (reloc_type == R_JMP_SLOT) {
                 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
+               get_sym_attr(s1, attr->plt_sym, 1)->dyn_index = 0;
+           }
         }
     }
 }
@@ -1402,7 +1404,7 @@ static void alloc_sec_names(TCCState *s1, int file_type, 
Section *strsec)
         s->sh_name = put_elf_str(strsec, s->name);
         /* when generating a DLL, we include relocations but we may
            patch them */
-        if (file_type == TCC_OUTPUT_DLL &&
+        if ((file_type == TCC_OUTPUT_DLL || file_type == TCC_OUTPUT_EXE) &&
             s->sh_type == SHT_RELX &&
             !(s->sh_flags & SHF_ALLOC)) {
             /* gr: avoid bogus relocs for empty (debug) sections */
@@ -1976,8 +1980,9 @@ static int elf_output_file(TCCState *s1, const char 
*filename)
             if (file_type == TCC_OUTPUT_DLL) {
                 if (s1->soname)
                     put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, 
s1->soname));
-                put_dt(dynamic, DT_TEXTREL, 0);
+                //put_dt(dynamic, DT_TEXTREL, 0);
             }
+                put_dt(dynamic, DT_TEXTREL, 0); //xxx
 
             if (s1->symbolic)
                 put_dt(dynamic, DT_SYMBOLIC, 0);
diff --git a/x86_64-link.c b/x86_64-link.c
index 27cad93..ee8bc58 100644
--- a/x86_64-link.c
+++ b/x86_64-link.c
@@ -165,7 +165,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, char 
*ptr, addr_t addr, add
 
     switch (type) {
         case R_X86_64_64:
-            if (s1->output_type == TCC_OUTPUT_DLL) {
+            if (s1->output_type == TCC_OUTPUT_DLL || s1->output_type == 
TCC_OUTPUT_EXE) {
                 esym_index = s1->sym_attrs[sym_index].dyn_index;
                 qrel->r_offset = rel->r_offset;
                 if (esym_index) {
@@ -183,7 +183,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, char 
*ptr, addr_t addr, add
             break;
         case R_X86_64_32:
         case R_X86_64_32S:
-            if (s1->output_type == TCC_OUTPUT_DLL) {
+            if (s1->output_type == TCC_OUTPUT_DLL || s1->output_type == 
TCC_OUTPUT_EXE) {
                 /* XXX: this logic may depend on TCC's codegen
                    now TCC uses R_X86_64_32 even for a 64bit pointer */
                 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
@@ -195,7 +195,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, char 
*ptr, addr_t addr, add
             break;
 
         case R_X86_64_PC32:
-            if (s1->output_type == TCC_OUTPUT_DLL) {
+            if (s1->output_type == TCC_OUTPUT_DLL || s1->output_type == 
TCC_OUTPUT_EXE) {
                 /* DLL relocation */
                 esym_index = s1->sym_attrs[sym_index].dyn_index;
                 if (esym_index) {



reply via email to

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