Commit 53a4742d authored by Moritz Angermann's avatar Moritz Angermann Committed by Austin Seipp

Allow -dead_strip linking on platforms with .subsections_via_symbols

Summary:
This allows to link objects produced with the llvm code generator to be linked with -dead_strip. This applies to at least the iOS cross compiler and OS X compiler.
Signed-off-by: Moritz Angermann's avatarMoritz Angermann <moritz@lichtzwerge.de>

Test Plan: Create a ffi library and link it with -dead_strip. If the resulting binary does not crash, the patch works as advertised.

Reviewers: rwbarton, simonmar, hvr, dterei, mzero, ezyang, austin

Reviewed By: dterei, ezyang, austin

Subscribers: thomie, mzero, simonmar, ezyang, carter

Differential Revision: https://phabricator.haskell.org/D206
parent 146dd138
Note [Subsections Via Symbols]
If we are using the .subsections_via_symbols directive
(available on recent versions of Darwin),
we have to make sure that there is some kind of reference
from the entry code to a label on the _top_ of of the info table,
so that the linker will not think it is unreferenced and dead-strip
it. That's why the label is called a DeadStripPreventer (_dsp).
The LLVM code gen already creates `iTableSuf` symbols, where
the X86 would generate the DeadStripPreventer (_dsp) symbol.
Therefore all that is left for llvm code gen, is to ensure
that all the `iTableSuf` symbols are marked as used.
As of this writing the documentation regarding the
.subsections_via_symbols and -dead_stip can be found at
<https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html#//apple_ref/doc/uid/TP30000823-TPXREF101>
\ No newline at end of file
......@@ -114,12 +114,18 @@ pprInfoTable count info_lbl stat
= do (ldata, ltypes) <- genLlvmData (Text, stat)
dflags <- getDynFlags
platform <- getLlvmPlatform
let setSection (LMGlobal (LMGlobalVar _ ty l _ _ c) d) = do
lbl <- strCLabel_llvm info_lbl
let sec = mkLayoutSection count
ilabel = lbl `appendFS` fsLit iTableSuf
gv = LMGlobalVar ilabel ty l sec (llvmInfAlign dflags) c
v = if l == Internal then [gv] else []
-- See Note [Subsections Via Symbols]
v = if (platformHasSubsectionsViaSymbols platform
&& l == ExternallyVisible)
|| l == Internal
then [gv]
else []
funInsert ilabel ty
return (LMGlobal gv d, v)
setSection v = return (v,[])
......
......@@ -73,12 +73,7 @@ pprNatCmmDecl proc@(CmmProc top_info lbl _ (ListGraph blocks)) =
-- elimination, it might be the target of a goto.
(if platformHasSubsectionsViaSymbols platform
then
-- If we are using the .subsections_via_symbols directive
-- (available on recent versions of Darwin),
-- we have to make sure that there is some kind of reference
-- from the entry code to a label on the _top_ of of the info table,
-- so that the linker will not think it is unreferenced and dead-strip
-- it. That's why the label is called a DeadStripPreventer (_dsp).
-- See Note [Subsections Via Symbols]
text "\t.long "
<+> ppr info_lbl
<+> char '-'
......
......@@ -77,12 +77,7 @@ pprNatCmmDecl proc@(CmmProc top_info lbl _ (ListGraph blocks)) =
-- elimination, it might be the target of a goto.
(if platformHasSubsectionsViaSymbols platform
then
-- If we are using the .subsections_via_symbols directive
-- (available on recent versions of Darwin),
-- we have to make sure that there is some kind of reference
-- from the entry code to a label on the _top_ of of the info table,
-- so that the linker will not think it is unreferenced and dead-strip
-- it. That's why the label is called a DeadStripPreventer (_dsp).
-- See Note [Subsections Via Symbols]
text "\t.long "
<+> ppr info_lbl
<+> char '-'
......
......@@ -78,12 +78,7 @@ pprNatCmmDecl proc@(CmmProc top_info lbl _ (ListGraph blocks)) =
-- elimination, it might be the target of a goto.
(if platformHasSubsectionsViaSymbols platform
then
-- If we are using the .subsections_via_symbols directive
-- (available on recent versions of Darwin),
-- we have to make sure that there is some kind of reference
-- from the entry code to a label on the _top_ of of the info table,
-- so that the linker will not think it is unreferenced and dead-strip
-- it. That's why the label is called a DeadStripPreventer (_dsp).
-- See Note [Subsections Via Symbols]
text "\t.long "
<+> ppr info_lbl
<+> char '-'
......
TOP=../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/test.mk
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/test.mk
# pass -dead_strip to the linker.
HCFLAGS = -O2 -fllvm
HCINC = $(TOP)/../includes
.PHONY: subsections_via_symbols_test
subsections_via_symbols_test:
'$(TEST_HC)' -o SubsectionsViaSymbols.o SubsectionsViaSymbols.hs $(HCFLAGS) -staticlib
'$(TEST_HC)' -o subsections_via_symbols SubsectionsViaSymbols subsections_via_symbols.m $(HCFLAGS) -optl -dead_strip -no-hs-main
./subsections_via_symbols
{-# LANGUAGE ForeignFunctionInterface #-}
module SymbolsViaSections where
foreign export ccall test :: Int -> IO ()
test :: Int -> IO ()
test i = putStrLn (replicate i '.')
if config.os == 'darwin':
only_darwin = normal
else:
only_darwin = skip
# Note [_ffi_call_unix64]
#
# Please refer to https://ghc.haskell.org/trac/ghc/ticket/5019
# for the subsections_via_symbols.stderr
test('subsections_via_symbols',
[extra_clean(['SubsectionsViaSymbols.hi', 'SubsectionsViaSymbols.o', 'SymbolsViaSections_stub.h', 'subsections_via_symbols', 'SubsectionsViaSymbols.a', 'SymbolsViaSections.hi', 'SymbolsViaSections.o', 'subsections_via_symbols.o']),
only_darwin],
run_command, ['$MAKE -s --no-print-directory subsections_via_symbols_test'])
#!/bin/sh
# Script to suppress annoying warnings from libtool (without suppressing any other interesting output)
libtool $@ 2>&1 | sed -e "/has no symbols/D" | sed -e "/due to use of basename, truncation and blank padding/D"
\ No newline at end of file
#import <Foundation/Foundation.h>
#import "HsFFI.h"
#import "SymbolsViaSections_stub.h"
int
main(int argc, char * argv[]) {
hs_init(&argc, &argv);
atexit(&hs_exit);
test(10);
return EXIT_SUCCESS;
}
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