[LLVMdev] [RFC] Alias should not point to declarations

Rafael Espíndola
Wed, 30 Oct 2013 19:35:09 -0000

A long time ago (before r97733) we used to model the weakref attribute
by outputting a new declaration and a weak alias to it. This was
fairly buggy and we now implement weakref directly in clang, with the
same logic an assembler uses to implement .weakref (which is what gcc
prints).

One thing that was left from that old implementation is that we still
have alias to declarations and they are a very different concept from
alias to definitions.

Alias to definitions are just another symbol pointing to the same
position in the object file. For example, given

@bar = alias void ()* @foo
define void @foo() {
  ret void
}

Both ELF end COFF just get two symbols (foo and bar or _foo and _bar)
pointing to the same section and offset.

Alias to declarations are a lot fuzzier. They are still considered
definitions according to GlobalValue::isDeclaration(), but the IR

@bar = alias void ()* @foo
declare void @foo()

produces an empty ELF file and COFF files with two undefined symbols,
but no connection between them. There is no good answer, since neither
COFF nor ELF have an alias directive to pass the connection down to
the linker.

The only possible use right now seems to be what we used to do for
weakref. Given

@bar = hidden alias void ()* @foo
declare void @foo()
define void @zed() {
       call void @foo()
       call void @bar()
       ret void
}

llc with the pic relocation model will produce a R_X86_64_PLT32 and a
R_X86_64_PC32, both pointing to foo. That is, the alias acts as an
alternative declaration.

The supported way of doing this is to have a proper declaration in the
file using the symbol:

declare hidden void @bar()
declare void @foo()
define void @zed() {
       call void @foo()
       call void @bar()
       ret void
}

and an alias on the file defining foo:

@bar = hidden alias void ()* @foo
define void @foo() {
       ret void
}

With all that in mind, the attached patch changes the verifier to
reject aliases to declarations and updates that language reference.
The patch also updates the testcases. They seem to be almost all from
the old weakref implementation.

Cheers,
Rafael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: t.patch
Type: text/x-patch
Size: 20378 bytes
Desc: not available
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20131030/28668dbd/attachment.bin>