1 #include "../includes/ghcconfig.h"
3 module PositionIndependentCode (
4 cmmMakeDynamicReference,
12 This module handles generation of position independent code and
13 dynamic-linking related issues for the native code generator.
15 Things outside this module which are related to this:
18 - PIC base label (pretty printed as local label 1)
19 - DynamicLinkerLabels - several kinds:
20 CodeStub, SymbolPtr, GotSymbolPtr, GotSymbolOffset
21 - labelDynamic predicate
23 - The CmmExpr datatype has a CmmPicBaseReg constructor
24 - The CmmLit datatype has a CmmLabelDiffOff constructor
26 - When tablesNextToCode, no absolute addresses are stored in info tables
27 any more. Instead, offsets from the info label are used.
28 - For Win32 only, SRTs might contain addresses of __imp_ symbol pointers
29 because Win32 doesn't support external references in data sections.
30 TODO: make sure this still works, it might be bitrotted
32 - The cmmToCmm pass in AsmCodeGen calls cmmMakeDynamicReference for all
34 - nativeCodeGen calls pprImportedSymbol and pprGotDeclaration to output
35 all the necessary stuff for imported symbols.
36 - The NCG monad keeps track of a list of imported symbols.
37 - MachCodeGen invokes initializePicBase to generate code to initialize
38 the PIC base register when needed.
39 - MachCodeGen calls cmmMakeDynamicReference whenever it uses a CLabel
40 that wasn't in the original Cmm code (e.g. floating point literals).
42 - The mangler converts absolure refs to relative refs in info tables
43 - Symbol pointers, stub code and PIC calculations that are generated
44 by GCC are left intact by the mangler (so far only on ppc-darwin
48 #include "HsVersions.h"
49 #include "nativeGen/NCG.h"
52 import MachOp ( MachOp(MO_Add), wordRep )
53 import CLabel ( CLabel, pprCLabel,
54 mkDynamicLinkerLabel, DynamicLinkerLabelInfo(..),
55 dynamicLinkerLabelInfo, mkPicBaseLabel,
56 labelDynamic, externallyVisibleCLabel )
58 #if powerpc_TARGET_ARCH && linux_TARGET_OS
59 import CLabel ( mkForeignLabel )
64 import NCGMonad ( NatM, getNewRegNat, getNewLabelNat )
66 import CmdLineOpts ( opt_PIC, opt_Static )
69 import qualified Outputable
71 import Panic ( panic )
74 -- The most important function here is cmmMakeDynamicReference.
76 -- It gets called by the cmmToCmm pass for every CmmLabel in the Cmm
77 -- code. It does The Right Thing(tm) to convert the CmmLabel into a
78 -- position-independent, dynamic-linking-aware reference to the thing
80 -- Note that this also has to be called from MachCodeGen in order to
81 -- access static data like floating point literals (labels that were
82 -- created after the cmmToCmm pass).
83 -- The function must run in a monad that can keep track of imported symbols
84 -- A function for recording an imported symbol must be passed in:
85 -- - addImportCmmOpt for the CmmOptM monad
86 -- - addImportNat for the NatM monad.
88 cmmMakeDynamicReference
89 :: Monad m => (CLabel -> m ()) -- a monad & a function
90 -- used for recording imported symbols
91 -> Bool -- whether this is the target of a jump
92 -> CLabel -- the label
95 cmmMakeDynamicReference addImport isJumpTarget lbl
96 | Just _ <- dynamicLinkerLabelInfo lbl
97 = return $ CmmLit $ CmmLabel lbl -- already processed it, pass through
98 | otherwise = case howToAccessLabel isJumpTarget lbl of
100 let stub = mkDynamicLinkerLabel CodeStub lbl
102 return $ CmmLit $ CmmLabel stub
103 AccessViaSymbolPtr -> do
104 let symbolPtr = mkDynamicLinkerLabel SymbolPtr lbl
106 return $ CmmLoad (cmmMakePicReference symbolPtr) wordRep
108 -- all currently supported processors support
109 -- a PC-relative branch instruction, so just jump there
110 | isJumpTarget -> return $ CmmLit $ CmmLabel lbl
111 -- for data, we might have to make some calculations:
112 | otherwise -> return $ cmmMakePicReference lbl
114 -- -------------------------------------------------------------------
116 -- Create a position independent reference to a label.
117 -- (but do not bother with dynamic linking).
118 -- We calculate the label's address by adding some (platform-dependent)
119 -- offset to our base register; this offset is calculated by
120 -- the function picRelative in the platform-dependent part below.
122 cmmMakePicReference :: CLabel -> CmmExpr
124 #if !mingw32_TARGET_OS
125 -- Windows doesn't need PIC,
126 -- everything gets relocated at runtime
128 cmmMakePicReference lbl
129 | opt_PIC && absoluteLabel lbl = CmmMachOp (MO_Add wordRep) [
131 CmmLit $ picRelative lbl
134 absoluteLabel lbl = case dynamicLinkerLabelInfo lbl of
135 Just (GotSymbolPtr, _) -> False
136 Just (GotSymbolOffset, _) -> False
140 cmmMakePicReference lbl = CmmLit $ CmmLabel lbl
142 -- ===================================================================
143 -- Platform dependent stuff
144 -- ===================================================================
146 -- Knowledge about how special dynamic linker labels like symbol
147 -- pointers, code stubs and GOT offsets look like is located in the
150 -- -------------------------------------------------------------------
152 -- We have to decide which labels need to be accessed
153 -- indirectly or via a piece of stub code.
155 data LabelAccessStyle = AccessViaStub
159 howToAccessLabel :: Bool -> CLabel -> LabelAccessStyle
161 #if mingw32_TARGET_OS
164 -- We need to use access *exactly* those things that
165 -- are imported from a DLL via an __imp_* label.
166 -- There are no stubs for imported code.
168 howToAccessLabel _ lbl | labelDynamic lbl = AccessViaSymbolPtr
169 | otherwise = AccessDirectly
171 #elif darwin_TARGET_OS
172 -- Mach-O (Darwin, Mac OS X)
174 -- Indirect access is required in the following cases:
175 -- * things imported from a dynamic library
176 -- * things from a different module, if we're generating PIC code
177 -- It is always possible to access something indirectly,
178 -- even when it's not necessary.
180 howToAccessLabel True lbl
181 -- jumps to a dynamic library go via a symbol stub
182 | labelDynamic lbl = AccessViaStub
183 -- when generating PIC code, all cross-module references must
184 -- must go via a symbol pointer, too.
185 -- Unfortunately, we don't know whether it's cross-module,
186 -- so we do it for all externally visible labels.
187 -- This is a slight waste of time and space, but otherwise
188 -- we'd need to pass the current Module all the way in to
190 | opt_PIC && externallyVisibleCLabel lbl = AccessViaStub
191 howToAccessLabel False lbl
192 -- data access to a dynamic library goes via a symbol pointer
193 | labelDynamic lbl = AccessViaSymbolPtr
194 -- cross-module PIC references: same as above
195 | opt_PIC && externallyVisibleCLabel lbl = AccessViaSymbolPtr
196 howToAccessLabel _ _ = AccessDirectly
198 #elif linux_TARGET_OS && powerpc64_TARGET_ARCH
199 -- ELF PPC64 (powerpc64-linux), AIX, MacOS 9, BeOS/PPC
201 howToAccessLabel True lbl = AccessDirectly -- actually, .label instead of label
202 howToAccessLabel _ lbl = AccessViaSymbolPtr
204 #elif linux_TARGET_OS
207 -- ELF tries to pretend to the main application code that dynamic linking does
208 -- not exist. While this may sound convenient, it tends to mess things up in
209 -- very bad ways, so we have to be careful when we generate code for the main
210 -- program (-dynamic but no -fPIC).
212 -- Indirect access is required for references to imported symbols
213 -- from position independent code. It is also required from the main program
214 -- when dynamic libraries containing Haskell code are used.
216 howToAccessLabel isJump lbl
217 -- no PIC -> the dynamic linker does everything for us;
218 -- if we don't dynamically link to Haskell code,
219 -- it actually manages to do so without messing thins up.
220 | not opt_PIC && opt_Static = AccessDirectly
222 #if !i386_TARGET_ARCH
223 -- for Intel, we temporarily disable the use of the
224 -- Procedure Linkage Table, because PLTs on intel require the
225 -- address of the GOT to be loaded into register %ebx before
226 -- a jump through the PLT is made.
227 -- TODO: make the i386 NCG ensure this before jumping to a
228 -- CodeStub label, so we can remove this special case.
230 -- As long as we're in a shared library ourselves,
231 -- we can use the plt.
232 -- NOTE: We might want to disable this, because this
233 -- prevents -fPIC code from being linked statically.
234 | isJump && labelDynamic lbl && opt_PIC = AccessViaStub
236 -- TODO: it would be OK to access non-Haskell code via a stub
237 -- | isJump && labelDynamic lbl && not isHaskellCode lbl = AccessViaStub
239 -- Using code stubs for jumps from the main program to an entry
240 -- label in a dynamic library is deadly; this will cause the dynamic
241 -- linker to replace all references (even data references) to that
242 -- label by references to the stub, so we won't find our info tables
246 -- A dynamic label needs to be accessed via a symbol pointer.
247 -- NOTE: It would be OK to jump to foreign code via a PLT stub.
248 | labelDynamic lbl = AccessViaSymbolPtr
250 #if powerpc_ARGET_ARCH
251 -- For PowerPC32 -fPIC, we have to access even static data
252 -- via a symbol pointer (see below for an explanation why
253 -- PowerPC32 Linux is especially broken).
254 | opt_PIC && not isJump = AccessViaSymbolPtr
257 | otherwise = AccessDirectly
261 -- all other platforms
264 | not opt_PIC = AccessDirectly
265 | otherwise = panic "howToAccessLabel: PIC not defined for this platform"
268 -- -------------------------------------------------------------------
270 -- What do we have to add to our 'PIC base register' in order to
271 -- get the address of a label?
273 picRelative :: CLabel -> CmmLit
276 -- The PIC base register points to the PIC base label at the beginning
277 -- of the current CmmTop. We just have to use a label difference to
279 -- We have already made sure that all labels that are not from the current
280 -- module are accessed indirectly ('as' can't calculate differences between
281 -- undefined labels).
284 = CmmLabelDiffOff lbl mkPicBaseLabel 0
286 #elif powerpc_TARGET_ARCH && linux_TARGET_OS
288 -- The PIC base register points to our fake GOT. Use a label difference
289 -- to get the offset.
290 -- We have made sure that *everything* is accessed indirectly, so this
291 -- is only used for offsets from the GOT to symbol pointers inside the
294 = CmmLabelDiffOff lbl gotLabel 0
296 #elif linux_TARGET_OS
297 -- Other Linux versions:
298 -- The PIC base register points to the GOT. Use foo@got for symbol
299 -- pointers, and foo@gotoff for everything else.
302 | Just (SymbolPtr, lbl') <- dynamicLinkerLabelInfo lbl
303 = CmmLabel $ mkDynamicLinkerLabel GotSymbolPtr lbl'
305 = CmmLabel $ mkDynamicLinkerLabel GotSymbolOffset lbl
308 picRelative lbl = panic "PositionIndependentCode.picRelative"
311 -- -------------------------------------------------------------------
313 -- What do we have to add to every assembly file we generate?
315 -- utility function for pretty-printing asm-labels,
316 -- copied from PprMach
317 asmSDoc d = Outputable.withPprStyleDoc (
318 Outputable.mkCodeStyle Outputable.AsmStyle) d
319 pprCLabel_asm l = asmSDoc (pprCLabel l)
324 needImportedSymbols = True
326 -- We don't need to declare any offset tables
327 pprGotDeclaration = Pretty.empty
329 -- On Darwin, we have to generate our own stub code for lazy binding..
330 -- There are two versions, one for PIC and one for non-PIC.
331 pprImportedSymbol importedLbl
332 | Just (CodeStub, lbl) <- dynamicLinkerLabelInfo importedLbl
336 ptext SLIT(".symbol_stub"),
337 ptext SLIT("L") <> pprCLabel_asm lbl <> ptext SLIT("$stub:"),
338 ptext SLIT("\t.indirect_symbol") <+> pprCLabel_asm lbl,
339 ptext SLIT("\tlis r11,ha16(L") <> pprCLabel_asm lbl
340 <> ptext SLIT("$lazy_ptr)"),
341 ptext SLIT("\tlwz r12,lo16(L") <> pprCLabel_asm lbl
342 <> ptext SLIT("$lazy_ptr)(r11)"),
343 ptext SLIT("\tmtctr r12"),
344 ptext SLIT("\taddi r11,r11,lo16(L") <> pprCLabel_asm lbl
345 <> ptext SLIT("$lazy_ptr)"),
350 ptext SLIT(".section __TEXT,__picsymbolstub1,")
351 <> ptext SLIT("symbol_stubs,pure_instructions,32"),
352 ptext SLIT("\t.align 2"),
353 ptext SLIT("L") <> pprCLabel_asm lbl <> ptext SLIT("$stub:"),
354 ptext SLIT("\t.indirect_symbol") <+> pprCLabel_asm lbl,
355 ptext SLIT("\tmflr r0"),
356 ptext SLIT("\tbcl 20,31,L0$") <> pprCLabel_asm lbl,
357 ptext SLIT("L0$") <> pprCLabel_asm lbl <> char ':',
358 ptext SLIT("\tmflr r11"),
359 ptext SLIT("\taddis r11,r11,ha16(L") <> pprCLabel_asm lbl
360 <> ptext SLIT("$lazy_ptr-L0$") <> pprCLabel_asm lbl <> char ')',
361 ptext SLIT("\tmtlr r0"),
362 ptext SLIT("\tlwzu r12,lo16(L") <> pprCLabel_asm lbl
363 <> ptext SLIT("$lazy_ptr-L0$") <> pprCLabel_asm lbl
364 <> ptext SLIT(")(r11)"),
365 ptext SLIT("\tmtctr r12"),
369 ptext SLIT(".lazy_symbol_pointer"),
370 ptext SLIT("L") <> pprCLabel_asm lbl <> ptext SLIT("$lazy_ptr:"),
371 ptext SLIT("\t.indirect_symbol") <+> pprCLabel_asm lbl,
372 ptext SLIT("\t.long dyld_stub_binding_helper")
375 -- We also have to declare our symbol pointers ourselves:
376 | Just (SymbolPtr, lbl) <- dynamicLinkerLabelInfo importedLbl
378 ptext SLIT(".non_lazy_symbol_pointer"),
379 char 'L' <> pprCLabel_asm lbl <> ptext SLIT("$non_lazy_ptr:"),
380 ptext SLIT("\t.indirect_symbol") <+> pprCLabel_asm lbl,
381 ptext SLIT("\t.long\t0")
386 #elif powerpc_TARGET_ARCH && linux_TARGET_OS
390 -- PowerPC Linux is just plain broken.
391 -- While it's theoretically possible to use GOT offsets larger
392 -- than 16 bit, the standard crt*.o files don't, which leads to
393 -- linker errors as soon as the GOT size exceeds 16 bit.
394 -- Also, the assembler doesn't support @gotoff labels.
395 -- In order to be able to use a larger GOT, we circumvent the
396 -- entire GOT mechanism and do it ourselves (this is what GCC does).
398 -- In this scheme, we need to do _all data references_ (even refs
399 -- to static data) via a SymbolPtr when we are generating PIC.
401 -- We need to do this whenever we explicitly access something via
403 needImportedSymbols = opt_PIC || not opt_Static
405 -- If we're generating PIC, we need to create our own "fake GOT".
407 gotLabel = mkForeignLabel -- HACK: it's not really foreign
408 FSLIT(".LCTOC1") Nothing False
410 -- The .LCTOC1 label is defined to point 32768 bytes into the table,
411 -- to make the most of the PPC's 16-bit displacements.
413 pprGotDeclaration = vcat [
414 ptext SLIT(".section \".got2\",\"aw\""),
415 ptext SLIT(".LCTOC1 = .+32768")
418 -- We generate one .long literal for every symbol we import;
419 -- the dynamic linker will relocate those addresses.
421 pprImportedSymbol importedLbl
422 | Just (SymbolPtr, lbl) <- dynamicLinkerLabelInfo importedLbl
424 ptext SLIT(".section \".got2\", \"aw\""),
425 ptext SLIT(".LC_") <> pprCLabel_asm lbl <> char ':',
426 ptext SLIT("\t.long") <+> pprCLabel_asm lbl
429 -- PLT code stubs are generated automatically be the dynamic linker.
434 -- For all other currently supported platforms, we don't need to do
437 needImportedSymbols = False
438 pprGotDeclaration = Pretty.empty
439 pprImportedSymbol _ = empty
442 -- -------------------------------------------------------------------
444 -- Generate code to calculate the address that should be put in the
445 -- PIC base register.
446 -- This is called by MachCodeGen for every CmmProc that accessed the
447 -- PIC base register. It adds the appropriate instructions to the
448 -- top of the CmmProc.
450 -- It is assumed that the first NatCmmTop in the input list is a Proc
451 -- and the rest are CmmDatas.
453 initializePicBase :: Reg -> [NatCmmTop] -> NatM [NatCmmTop]
455 #if powerpc_TARGET_ARCH && darwin_TARGET_OS
457 -- Darwin is simple: just fetch the address of a local label.
458 initializePicBase picReg (CmmProc info lab params blocks : statics)
459 = return (CmmProc info lab params (b':tail blocks) : statics)
460 where BasicBlock bID insns = head blocks
461 b' = BasicBlock bID (FETCHPC picReg : insns)
463 #elif powerpc_TARGET_ARCH && linux_TARGET_OS
465 -- Get a pointer to our own fake GOT, which is defined on a per-module basis.
466 -- This is exactly how GCC does it, and it's quite horrible:
467 -- We first fetch the address of a local label (mkPicBaseLabel).
468 -- Then we add a 16-bit offset to that to get the address of a .long that we
469 -- define in .text space right next to the proc. This .long literal contains
470 -- the (32-bit) offset from our local label to our global offset table
471 -- (.LCTOC1 aka gotOffLabel).
472 initializePicBase picReg
473 (CmmProc info lab params blocks : statics)
475 gotOffLabel <- getNewLabelNat
476 tmp <- getNewRegNat wordRep
478 gotOffset = CmmData Text [
479 CmmDataLabel gotOffLabel,
480 CmmStaticLit (CmmLabelDiffOff gotLabel
484 offsetToOffset = ImmConstantDiff (ImmCLbl gotOffLabel)
485 (ImmCLbl mkPicBaseLabel)
486 BasicBlock bID insns = head blocks
487 b' = BasicBlock bID (FETCHPC picReg
489 (AddrRegImm picReg offsetToOffset)
490 : ADD picReg picReg (RIReg tmp)
492 return (CmmProc info lab params (b' : tail blocks) : gotOffset : statics)
494 initializePicBase picReg proc = panic "initializePicBase"
497 -- i386_TARGET_ARCH && linux_TARGET_OS:
498 -- generate something like:
501 -- addl __GLOBAL_OFFSET_TABLE__+.-1b, %picReg
502 -- It might be a good idea to use a FETCHPC pseudo-instruction (like for PowerPC)
503 -- in order to avoid having to create a new basic block.
504 -- ((FETCHPC reg) should pretty-print as call 1f; 1: popl reg)
506 -- mingw32_TARGET_OS: not needed, won't be called
508 -- i386_TARGET_ARCH && darwin_TARGET_OS:
509 -- (just for completeness ;-)