[Aboriginal] PIE

David Halls dahalls at gmail.com
Fri May 29 13:38:29 PDT 2015


The patch below does the job but you may want to tweak it.
I also needed this:
https://sourceware.org/viewvc/src/bfd/elf64-x86-64.c?r1=1.178&r2=1.179

--- sources/toys/ccwrap.c.orig 2015-05-29 07:03:54.523059741 +0100
+++ sources/toys/ccwrap.c 2015-05-29 09:16:18.571311754 +0100
@@ -125,13 +125,14 @@
 // Some compiler versions don't provide separate T and S versions of begin/end,
 // so fall back to the base version if they're not there.

-char *find_TSpath(char *base, char *top, int use_shared, int
use_static_linking)
+char *find_TSpath(char *base, char *top, int use_shared, int
use_static_linking,
+                  int use_pie)
 {
   int i;
   char *temp;

   temp = xmprintf(base, top,
-    use_shared ? "S.o" : use_static_linking ? "T.o" : ".o");
+    use_shared || use_pie ? "S.o" : use_static_linking ? "T.o" : ".o");

   if (!is_file(temp, 0)) {
     free(temp);
@@ -144,7 +145,7 @@

 enum {
   Clibccso, Clink, Cprofile, Cshared, Cstart, Cstatic, Cstdinc, Cstdlib,
-  Cverbose, Cx, Cdashdash,
+  Cverbose, Cx, Cdashdash, Cpie,

   CPctordtor, CP, CPstdinc
 };
@@ -382,6 +383,10 @@

         return 0;
       } else if (!strcmp(c, "pg")) SET_FLAG(Cprofile);
+      else if (!strcmp(c, "pie")) {
+        keepc--;
+        SET_FLAG(Cpie);
+      }
     } else if (*c == 's') {
       keepc--;
       if (!strcmp(c, "shared")) {
@@ -440,6 +445,7 @@
     outv[outc++] = "-nostdlib";
     outv[outc++] = GET_FLAG(Cstatic) ? "-static" : dynlink;
     if (GET_FLAG(Cshared)) outv[outc++] = "-shared";
+    if (GET_FLAG(Cpie)) outv[outc++] = "-pie";

     // Copy libraries to output (first move fallback to end, break circle)
     libs = libs->next->next;
@@ -452,11 +458,12 @@
     if (GET_FLAG(CPctordtor)) {
       outv[outc++] = xmprintf("%s/lib/crti.o", topdir);
       outv[outc++] = find_TSpath("%s/cc/lib/crtbegin%s", topdir,
-                                 GET_FLAG(Cshared), GET_FLAG(Cstatic));
+                                 GET_FLAG(Cshared), GET_FLAG(Cstatic),
+                                 GET_FLAG(Cpie));
     }
     if (!GET_FLAG(Cprofile) && GET_FLAG(Cstart))
       outv[outc++] = xmprintf("%s/lib/%scrt1.o", topdir,
-                              GET_FLAG(Cshared) ? "S" : "");
+                              GET_FLAG(Cshared) || GET_FLAG(Cpie) ? "S" : "");
   }

   // Copy unclaimed arguments
@@ -482,7 +489,8 @@
     }
     if (GET_FLAG(CPctordtor)) {
       outv[outc++] = find_TSpath("%s/cc/lib/crtend%s", topdir,
-                                 GET_FLAG(Cshared), GET_FLAG(Cstatic));
+                                 GET_FLAG(Cshared), GET_FLAG(Cstatic),
+                                 GET_FLAG(Cpie));
       outv[outc++] = xmprintf("%s/lib/crtn.o", topdir);
     }
   }

On 19 May 2015 at 08:11, David Halls <dahalls at gmail.com> wrote:
> This is what QEMU does:
>
>
>   if compile_prog "-fPIE -DPIE" "-pie"; then
>     QEMU_CFLAGS="-fPIE -DPIE $QEMU_CFLAGS"
>     LDFLAGS="-pie $LDFLAGS"
>     pie="yes"
>     if compile_prog "" "-Wl,-z,relro -Wl,-z,now" ; then
>       LDFLAGS="-Wl,-z,relro -Wl,-z,now $LDFLAGS"
>     fi
>   else
>     if test "$pie" = "yes"; then
>       error_exit "PIE not available due to missing toolchain support"
>     else
>       echo "Disabling PIE due to missing toolchain support"
>       pie="no"
>     fi
>   fi
>
> On 19 May 2015 at 07:43, David Halls <dahalls at gmail.com> wrote:
>> -pie
>>            Produce a position independent executable on targets that support
>>            it.  For predictable results, you must also specify the same set of
>>            options used for compilation (-fpie, -fPIE, or model suboptions)
>>            when you specify this linker option.
>>
>> Also, it's what QEMU's configure step does to detect whether PIE is
>> available :-)
>>
>> On 19 May 2015 at 07:38, Rob Landley <rob at landley.net> wrote:
>>> On Mon, May 18, 2015 at 3:35 PM, David Halls <dahalls at gmail.com> wrote:
>>>> ~ # cat wup.c
>>>> int main(int argc, char **argv) { return 0; }
>>>> ~ # gcc -fPIE -pie wup.c
>>>
>>> Why does it need both -fPIE and -pie ?
>>>
>>> (Trying to figure out what changes I need to make to ccwrap parsing.)
>>>
>>> Rob



More information about the Aboriginal mailing list