Commit 620fc6f9 authored by Tamar Christina's avatar Tamar Christina Committed by thomie

Make Windows linker more robust to unknown sections

The Windows Linker has 3 main parts that this patch changes.

1) Identification and classification of sections
2) Adding of symbols to the symbols tables
3) Reallocation of sections

1.
Previously section identification used to be done on a whitelisted
basis. It was also exclusively being done based on the names of the
sections. This meant that there was a bit of a cat and mouse game
between `GCC` and `GHC`. Every time `GCC` added new sections there was a
good chance `GHC` would break. Luckily this hasn't happened much in the
past because the `GCC` versions `GHC` used were largely unchanged.

The new code instead treats all new section as `CODE` or `DATA`
sections, and changes the classifications based on the `Characteristics`
flag in the PE header. By doing so we no longer have the fragility of
changing section names. The one exception to this is the `.ctors`
section, which has no differentiating flag in the PE header, but we know
we need to treat it as initialization data.

The check to see if the sections are aligned by `4` has been removed.
The reason is that debug sections often time are `1 aligned` but do have
relocation symbols. In order to support relocations of `.debug` sections
this check needs to be gone. Crucially this assumption doesn't seem to
be in the rest of the code. We only check if there are at least 4 bytes
to realign further down the road.

2.
The second loop is iterating of all the symbols in the file and trying
to add them to the symbols table. Because the classification of the
sections we did previously are (currently) not available in this phase
we still have to exclude the sections by hand. If they don't we will
load in symbols from sections we've explicitly ignored the in # 1. This
whole part should rewritten to avoid this. But didn't want to do it in
this commit.

3.
Finally the sections are relocated. But for some reason the PE files
contain a Linux relocation constant in them `0x0011` This constant as
far as I can tell does not come from GHC (or I couldn't find where it's
being set). I believe this is probably a bug in GAS. But because the
constant is in the output we have to handle it. I am thus mapping it to
the constant I think it should be `0x0003`.

Finally, static linking *should* work, but won't. At least not if you
want to statically link `libgcc` with exceptions support. Doing so would
require you to link `libgcc` and `libstd++` but also `libmingwex`. The
problem is that `libmingwex` also defines a lot of symbols that the RTS
automatically injects into the symbol table. Presumably because they're
symbols that it needs. like `coshf`. The these symbols are not in a
section that is declared with weak symbols support. So if we ever want
to get this working, we should either a) Ask mingw to declare the
section as such, or b) treat all a imported symbols as being weak.
Though this doesn't seem like it's a good idea..

Test Plan:
Running ./validate for both x86 and x86_64

Also running the specific test case for #10672

make TESTS="T10672_x86 T10672_x64"

Reviewed By: ezyang, thomie, austin

Differential Revision: https://phabricator.haskell.org/D1244

GHC Trac Issues: #9907, #10672, #10563
parent d2fb5328
This diff is collapsed.
......@@ -210,6 +210,9 @@ from testlib import *
if windows or darwin:
pkginfo = getStdout([config.ghc_pkg, 'dump'])
topdir = config.libdir
if windows:
mingw = os.path.join(topdir, '../mingw/bin')
os.environ['PATH'] = os.pathsep.join([os.environ.get("PATH", ""), mingw])
for line in pkginfo.split('\n'):
if line.startswith('library-dirs:'):
path = line.rstrip()
......
-- Copyright (C) 2015, Luke Iannini
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Printf ( pr )
foreign import ccall "talkToCxx" talkToCxx :: IO ()
main :: IO ()
main = do
putStrLn ( $(pr "Hello From Template Haskell!") )
talkToCxx
TOP=../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/test.mk
T10672_x64:
'$(TEST_HC)' $(TEST_HC_OPTS) -v0 -rtsopts=none -fforce-recomp -lgcc_s_seh-1 -lstdc++-6 \
Main.hs Printf.hs cxxy.cpp
T10672_x86:
'$(TEST_HC)' $(TEST_HC_OPTS) -v0 -rtsopts=none -fforce-recomp -lgcc_s_dw2-1 -lstdc++-6 \
Main.hs Printf.hs cxxy.cpp
-- Copyright (C) 2015, Luke Iannini
{-# LANGUAGE TemplateHaskell #-}
module Printf where
-- Skeletal printf from the paper:
-- http://research.microsoft.com/pubs/67015/meta-haskell.pdf
-- It needs to be in a separate module to the one where
-- you intend to use it.
-- Import some Template Haskell syntax
import Language.Haskell.TH
-- Describe a format string
data Format = D | S | L String
-- Parse a format string. This is left largely to you
-- as we are here interested in building our first ever
-- Template Haskell program and not in building printf.
parse :: String -> [Format]
parse s = [ L s ]
-- Generate Haskell source code from a parsed representation
-- of the format string. This code will be spliced into
-- the module which calls "pr", at compile time.
gen :: [Format] -> Q Exp
gen [D] = [| \n -> show n |]
gen [S] = [| \s -> s |]
gen [L s] = stringE s
-- Here we generate the Haskell code for the splice
-- from an input format string.
pr :: String -> Q Exp
pr s = gen (parse s)
test('T10672_x64', [extra_clean(['cxxy.o',
'Main.exe', 'Main.hi', 'Main.o',
'Printf.o', 'Printf.hi']),
[unless(opsys('mingw32'),skip) , unless(arch('x86_64'), skip)]],
run_command, ['$MAKE -s --no-print-directory T10672_x64'])
test('T10672_x86', [extra_clean(['cxxy.o',
'Main.exe', 'Main.hi', 'Main.o',
'Printf.o', 'Printf.hi']),
[unless(opsys('mingw32'),skip) , unless(arch('i386'), skip)]],
run_command, ['$MAKE -s --no-print-directory T10672_x86'])
// Copyright (C) 2015, Luke Iannini
#include <iostream>
#include <exception>
#include <string.h>
// Make sure can call unmangled names from Haskell's FFI
extern "C" {
int talkToCxx() {
try {
throw 20;
}
catch (int e) {
std::cout << "An exception occurred. Exception Nr. " << e << '\n';
}
std::cout << "Hello From C++!";
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment