# Symbol aliases

**URL:** <https://internals.rust-lang.org/t/symbol-aliases/13297>\
**Category:** language design\
**Created:** [October 28, 2020, 8:19pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297 "2020-10-28T20:19:51Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlewycky](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nlewycky/32/8366_2.png) [@nlewycky](https://internals.rust-lang.org/u/nlewycky)\
**Post date:** [October 28, 2020, 8:19pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/1 "2020-10-28T20:19:51Z")

</div>

This is a continuation of the closed topic at [Pre-RFC: Defining function aliases](https://internals.rust-lang.org/t/pre-rfc-defining-function-aliases/11424) .

An alias is a second name for a function or global variable that is defined in the same object file. Because it's a second name for the same memory, it does not have its own address, rather a pointer to it and to the other name must compare equal.

These are useful in certain low-level systems programming circumstances when you have to define or use an interface like IRQ tables or similar (for example, a toolchain for an OS-less machine that requires a function named "irq\_sio4" to be the handler for that interrupt, and is space constrained, you can use aliases to emit the empty function once and alias all the other IRQ handlers to that one).

In my case, I'm defining an interface to be used by non-Rust code, which my rust program will load via `dlopen`. Part of my interface includes the stack probing symbol, which may go by different names on different platforms. I'd like to expose it in my interface under a consistent name. One apparent option is to write a wrapper function that calls `__rust_probestack` but this is incorrect because stack prober does not follow the ABI of a function, and wrapping it in a function breaks the expected ABI and causes real bugs.

Aliases are an important feature for low-level systems programming, despite the fact that they aren't standardized in C or C++. In practice, ELF, Mach-O and COFF all support symbol aliases, and all production C and C++ toolchains support it either in the compiler by language extensions or in the linker with linker scripts. LLVM [supports it](https://llvm.org/docs/LangRef.html#aliases) with the `@newname = alias %type, %type* @origname` syntax.

There's a related feature that I'm explicitly not asking for. In some object file formats it's possible for the alias name to have different properties (linkage, visibility, ...) than the original. Although useful I'd like to be conservative now, focus on what I know I need as a rust user and what we know all platforms support, and leave other that feature to a future enhancement request.

One possible implementation would be to place an `alias("")` attribute on a declaration to indicate that this declaration is an alias of the other symbol, and require that the other symbol be defined and have matching types.

```rust
#[alias("sum")]
#[no_mangle]
pub extern "C" fn alias_of_sum(a: i32, b: i32) -> i32;

```

---

<div class="post-metadata">

**Author:** ![Mart-Bogdan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mart-bogdan/32/5104_2.png) [@Mart-Bogdan](https://internals.rust-lang.org/u/Mart-Bogdan)\
**Post date:** [October 28, 2020, 9:49pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/2 "2020-10-28T21:49:11Z")

</div>

If I understand correctly link annotation is what You need:

```rust
#[link(name = "sum")]
pub extern "C" fn alias_of_sum(a: i32, b: i32) -> i32;

```

---

<div class="post-metadata">

**Author:** ![nlewycky](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nlewycky/32/8366_2.png) [@nlewycky](https://internals.rust-lang.org/u/nlewycky)\
**Post date:** [October 28, 2020, 10:08pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/3 "2020-10-28T22:08:04Z")

</div>

Thanks for the suggestion! I tried this:

```rust
#[no_mangle]
pub extern "C" fn sum(a: i32, b: i32) -> i32 {
  a + b
}

extern "C" {
  #[link(name = "sum")]
  fn alias_of_sum(a: i32, b: i32) -> i32;
}

fn main() {
    println!("{}", unsafe { alias_of_sum(1, 2) });
}

```

but it fails to link with the error "undefined symbol: alias\_of\_sum".

Does the link attribute have any effect on declarations? If not, this would be a better syntax than a new alias attribute.

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [October 28, 2020, 10:34pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/4 "2020-10-28T22:34:36Z")

</div>

`#[link(name = ...)]` is for specifying the library name. You want `#[link_name = "sum"]`. See also: [https://doc.rust-lang.org/reference/attributes.html](https://doc.rust-lang.org/reference/attributes.html)

---

<div class="post-metadata">

**Author:** ![nlewycky](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nlewycky/32/8366_2.png) [@nlewycky](https://internals.rust-lang.org/u/nlewycky)\
**Post date:** [October 28, 2020, 11:39pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/5 "2020-10-28T23:39:07Z")

</div>

> [@mjbshaw](#):
>
> `#[link(name = ...)]` is for specifying the library name. You want `#[link_name = "sum"]` . See also: [Attributes - The Rust Reference](https://doc.rust-lang.org/reference/attributes.html)

That does make the example code I pasted work, but doesn't solve the motivating problem. `#[link(name = ...)]` is a way of renaming the function so it has one name in the rust parser and a second name in the object file. The tool I'm asking for is a way to associate two names with a function in the object file. Your suggestion would work if I only needed to call `alias_to_sum` from other Rust code, but in the motivating problem I need a symbol with that name to be present in final binary to resolve symbols in a shared object. Given:

```rust
extern "Rust" {
  #[link_name = "object_file_name"]
  fn rust_friendly_name(a: i32, b: i32) -> i32;
}

```

the rust\_friendly\_name never makes it into LLVM. I need something close to:

```rust
#[no_mangle]
pub static wasmer_probestack: unsafe extern "C" fn() = __rust_probestack;

```

except that this defines a pointer-sized variable named wasmer\_probestack that contains the address of \_\_rust\_probestack. Using an alias would remove that one layer of indirection.

The object file doesn't have a high-level concept of functions, it stores a stream of bytes in one of more sections (different object files are different, I'm describing ELF here). The symbol table is a list of _\<symbol name, symbol address\>_ mappings, and we call that symbol address the "start" or "address" of the function. The idea of an alias is to add a second symbol name that shares the same symbol address. The only way to do that in LLVM is to emit an `llvm::GlobalAlias` to the IR.

---

<div class="post-metadata">

**Author:** ![nlewycky](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nlewycky/32/8366_2.png) [@nlewycky](https://internals.rust-lang.org/u/nlewycky)\
**Post date:** [October 28, 2020, 11:54pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/6 "2020-10-28T23:54:11Z")

</div>

For an example in C with GCC extensions:

```rust
int sum(int a, int b) { return a + b; }
__attribute__ ((alias("sum"))) int alias_of_sum(int a, int b);
int main(void) {}

```

compiled to an executable on linux becomes:

```rust
$ nm alias | grep sum
0000000000001125 T alias_of_sum
0000000000001125 T sum

```

This is _\<symbol address, symbol type, symbol name\>_ showing the two functions sharing the same address. I think there's no way to get this from rustc today.

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [October 29, 2020, 5:07am UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/7 "2020-10-29T05:07:32Z")

</div>

Perhaps the compiler should allow multiple `link_name` attributes on the same function.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [November 3, 2020, 8:43am UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/9 "2020-11-03T08:43:42Z")

</div>

Would an existential type work? (Using nightly features)

```rust
const wasmer_probestack: impl Fn() = __rust_probestack;

```

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [November 3, 2020, 9:53am UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/10 "2020-11-03T09:53:49Z")

</div>

No, function definition types (`ty::FnDef`) are zero sized. Also `const` doesn't result in any symbols in the resulting program, instead the value is copy pasted at every usage. Only `static` results in a symbol.

---

<div class="post-metadata">

**Author:** ![197g](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/197g/32/7276_2.png) [@197g](https://internals.rust-lang.org/u/197g)\
**Post date:** [November 3, 2020, 12:04pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/11 "2020-11-03T12:04:53Z")

</div>

The [`link_name`](https://doc.rust-lang.org/reference/items/external-blocks.html#the-link_name-attribute) attribute sets the name of an **imported** symbol and has no effect for exported symbols (that I know of).

> [@nlewycky](#):
>
> One possible implementation would be to place an `alias("")` attribute on a declaration to indicate that this declaration is an alias of the other symbol, and require that the other symbol be defined and have matching types.

That sounds a lot like defining a static which I believe should work? Maybe it needs a `#[used]` annotation although it shouldn't.

```rust
#[no_mangle]
pub static alias_of_sum: extern "C" fn(a: i32, b: i32) -> i32 = sum;

```

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [November 3, 2020, 12:08pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/12 "2020-11-03T12:08:03Z")

</div>

That will create a static containing a pointer. You will need to dereference it before being able to call it. Symbol aliases on the other hand point directly to the respective function/static.

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [November 3, 2020, 4:42pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/14 "2020-11-03T16:42:10Z")

</div>

> [@197g](#):
>
> The [`link_name`](https://doc.rust-lang.org/reference/items/external-blocks.html#the-link_name-attribute) attribute sets the name of an **imported** symbol and has no effect for exported symbols (that I know of).

Correct. `#[export_name = "foo"]` can be used to set the name of an exported symbol.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [February 1, 2021, 4:42pm UTC](https://internals.rust-lang.org/t/symbol-aliases/13297/15 "2021-02-01T16:42:23Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
