[PATCH] Weak symbols for COFF
Nico Rieck
nico.rieck at gmail.com
Tue Aug 6 18:52:06 PDT 2013
I was looking into emission of weak symbols for COFF, with the goal to be able to provide operator new/delete which can be overridden. This is usually accomplished by marking these operators with the weak attribute.
For COFF, weak symbols are implemented using weak externals and a second symbol. The code that sets up COFF weak externals is never hit when using the weak attribute or IR functions with weak linkage. Using asm with the .weak directive works, but produces wrong object files.
When feeding it the following snippet:
.text
.globl f1_default
f1_default: ret
.weak f1
f1 = f1_default
.weak f2
f2: ret
these symbols are produced:
002 00000000 SECT1 notype External | f1_default
003 00000000 SECT1 notype WeakExternal | f1
Default index 2 library search
005 00000000 SECT1 notype WeakExternal | f2
Default index 7 library search
007 00000000 ABS notype External | .weak.f2.default
Linking with this object file only works if nothing else defines f1 and f2 which defeats the purpose of them being weak. Also, with the weak external characteristic "library search" it seems that the weak symbol can only be referenced from the same TU. Using it from somewhere else results in unresolved external symbols.
For comparison, here is what GCC 4.8.0 gives me for the same snippet:
008 00000000 SECT1 notype External | f1_default
009 00000000 SECT1 notype External | .weak.f1.f1_default
00A 00000001 SECT1 notype External | .weak.f2.f1_default
00B 00000000 UNDEF notype WeakExternal | f1
Default index 9 No library search
00D 00000000 UNDEF notype WeakExternal | f2
Default index A No library search
But linking with this has the same problems with unresolved externals.
Using "alias" as weak external characteristic resolves this problem, and is the same characteristic used by Interix for weak symbols.
Fixing this issue proved a bit challenging. We want to emit instructions to the default symbol, but also look it up using the weak name. Renaming MCSymbols is not possible/safe. The same goes for swapping MCSymbolData values. Trying a few different ways this one seemed the least ugly.
I pushed the creation of the default symbols to a later point where it's possible to swap the COFFSymbols directly. Care must then be taken to ensure that fixups (which are applied earlier) are not wrongly resolved at compile-time. I did this similar to the ELF writer by overwriting IsSymbolRefDifferenceFully-ResolvedImpl, and then later not using the aliased symbol when recording a relocation.
http://llvm-reviews.chandlerc.com/D1309
Files:
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/MC/WinCOFFObjectWriter.cpp
test/MC/COFF/alias.s
test/MC/COFF/weak-symbol.ll
test/MC/COFF/weak.s
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1309.1.patch
Type: text/x-patch
Size: 11238 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130806/e1f4491a/attachment.bin>
More information about the llvm-commits
mailing list