<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "http://bugs.gentoo.org/bugzilla.dtd">

<bugzilla version="2.22.7"
          urlbase="http://bugs.gentoo.org/"
          maintainer="bugzilla@gentoo.org"
>

    <bug>
          <bug_id>123698</bug_id>
          
          <creation_ts>2006-02-22 03:39 0000</creation_ts>
          <short_desc>all binaries built by ghc have executable stacks</short_desc>
          <delta_ts>2006-03-15 03:55:24 0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Gentoo Linux</product>
          <component>Ebuilds</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>LATER</resolution>
          
          
          
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>dcoutts@gentoo.org</reporter>
          <assigned_to>haskell@gentoo.org</assigned_to>
          <cc>solar@gentoo.org</cc>
    
    <cc>vapier@gentoo.org</cc>

      

      
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-02-22 03:39:36 0000</bug_when>
            <thetext>This affects ghc and all other ebuilds that use ghc.

We have not yet figured out why this is happening. I suspect it may be due to ghc&apos;s &quot;split objs&quot; feature. We should see if it still happens with that turned off.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>solar@gentoo.org</who>
            <bug_when>2006-02-22 07:17:34 0000</bug_when>
            <thetext>Do all ghc compiled executables require an executable stack/heap? 
Or os this just a matter of being mismarked?
Do ghc compiled execitables contain a PT_GNU_STACK section at all?</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-02-22 08:09:33 0000</bug_when>
            <thetext>*** Bug 123711 has been marked as a duplicate of this bug. ***</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-02-22 08:38:31 0000</bug_when>
            <thetext>re comment #1; No ghc does not actually need an executable stack. The problem is that not all the things linked into a ghc-built binary have the PT_GNU_STACK section. So far I havn&apos;t been able to find which ones are missing it.

I rad through the instructions:
http://www.gentoo.org/proj/en/hardened/gnu-stack.xml

After a bit more experimentation I have discovered some more useful info:
ghc has two ways of compiling .hs files to .o files. One goes via C and one goes directly to assembly.

For the -fasm method:
ghc simply does not emit the .section .note.GNU-stack stuff into the assembly output.

For the -fvia-C method:
ghc emits C which is then compiled by gcc. The resulting .raw_s file does contain the .section .note.GNU-stack. However ghc then runs the generated assembly through the &quot;evil mangler&quot; which doesn&apos;t grok the .section .note.GNU-stack and does not emit it to the final assembly file.

So the solution is to get ghc to emit the .note.GNU-stack in it&apos;s native code geneerator and to modify the evil mangler to pass the .note.GNU-stack through to the output.

We may still have a problem with the &quot;split objs&quot; feature (which ghc uses for its own libraries). Hopefully it&apos;d just be a matter of adding .note.GNU-stack to each .s file that is spat out by ghc -split-objs.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>solar@gentoo.org</who>
            <bug_when>2006-02-22 08:50:22 0000</bug_when>
            <thetext>Duncan,
Pretty good summary of the problem. 
Are you in direct contact with anybody upstream who might be able to 
work on a solution? Is it something you yourself might be able to work on?

Also does the final linking happen using gnu-ld ? 
If so can -Wl,-z,noexecstack be forced?
Can -Wa,--noexecstack be used?</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-02-22 09:02:22 0000</bug_when>
            <thetext>This has been reported upstream:
http://hackage.haskell.org/trac/ghc/ticket/703</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-02-22 09:05:29 0000</bug_when>
            <thetext>I might be able to experiment with some patches to ghc. It may take some time however.

Yes ghc does use gnu ld. We could try forcing a noexecstack option. We&apos;ll look into it.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-02-22 09:46:34 0000</bug_when>
            <thetext>So adding -optl-Wl,-z,noexecstack to the ghc driver script makes it work. This is a bit on the dangerious side however. It means we&apos;ll use a non-executable stack even if we&apos;re linking with a C lib that does need an executable stack (right?).

For a simple hello world .hs file it&apos;s enough to add 
-opta-Wa,--noexecstack but this doesn&apos;t seem to work when building a larger program like cpphs. Presumably that&apos;s because in that case we&apos;re linking in lots of ghc libs that were not compiled with -opta-Wa,--noexecstack. So perhaps if this were added to the flags ghc uses to compile itself and it&apos;s libs then it&apos;d be sufficient to just add -opta-Wa,--noexecstack to the ghc driver script. I&apos;ve not tried this yet.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vapier@gentoo.org</who>
            <bug_when>2006-02-22 20:36:53 0000</bug_when>
            <thetext>&gt; So adding -optl-Wl,-z,noexecstack to the ghc driver script makes it work. This
&gt; is a bit on the dangerious side however.

it is on the dangerous side in terms of silently breaking stuff

&gt; It means we&apos;ll use a non-executable stack even if we&apos;re linking with a C lib
&gt; that does need an executable stack (right?).

no ... every ELF gets their own stack markings, so if some libfoo.so requires executable stack, it&apos;ll get it ... you&apos;re simply forcing executable stack policy on the final ELF ghc produces, that&apos;s it</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>solar@gentoo.org</who>
            <bug_when>2006-02-22 21:08:18 0000</bug_when>
            <thetext>Does ghc have a spec file? Anything like gcc&apos;s own?</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-03-01 04:31:39 0000</bug_when>
            <thetext>So it looks like adding -opta-Wa,--noexecstack to the ghc driver script (and the flags we build ghc itself with) fixes everything. I&apos;ll add this to the ghc-6.4.1-r2 ebuild shortly.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-03-10 16:29:04 0000</bug_when>
            <thetext>This has now been done. This seems to be working but I think it needs a little more testing before I&apos;m confident to call this fixed.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>dcoutts@gentoo.org</who>
            <bug_when>2006-03-15 03:55:24 0000</bug_when>
            <thetext>With the latest fix in the ghc ebuild I think this is now working ok.

Note that ghc-bin still sufferes from this problem since they were built using eariler versions of the ghc ebuild. At the moment we don&apos;t think it&apos;s worth rebuilding the ghc-bin .tbz2 files on all arches to fix this QA bug.

So it&apos;ll get fixed for ghc-bin next time we have to rebuild the .tbz2 files which will probably be with the next release ghc-6.4.2.

I&apos;ll leave this bug open until next time ghc-bin gets rebuilt.</thetext>
          </long_desc>
      
    </bug>

</bugzilla>