Skip to content

-Zbuild-std outputs a symbol just outside the containing section, causing a linker error #101107

Closed
@yvt

Description

@yvt

I tried this code (full code):

// target: riscv32imac-unknown-none-elf
// rustflags: -C link-arg=-Tmemory-c8.x -C link-arg=-Tlink-patched.x
// cargo build options: -Zbuild-std=core
// profile.release.debug = true
#![no_std]
#![no_main]

use panic_halt as _;
use riscv_rt::entry;

#[entry]
fn main() -> ! {
    char::try_from(u32::MAX).unwrap();
    unreachable!();
}

The above program causes a linker error starting from nightly-2022-08-13.

nightly-2022-08-13 (first known bad nightly):

$ cargo build --release -Zbuild-std=core
 ⋮
  = note: rust-lld: error: /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib(core-dd70e77b667e899d.core.1c09fd4f-cgu.9.rcgu.o):(.rodata.cst16): offset is outside the section

$ llvm-objdump -t /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib | rg 'cst16|cgu\.9'
 ⋮
/work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib(core-dd70e77b667e899d.core.1c09fd4f-cgu.9.rcgu.o): file format elf32-littleriscv
00000000 l    df *ABS*  00000000 core.1c09fd4f-cgu.9
00000000 l     O .rodata.cst16  00000010 .Lanon.95de604ed2fc4f21cfc77c2472e08c5d.40
00000010 l     O .rodata.cst16  00000010 .Lanon.95de604ed2fc4f21cfc77c2472e08c5d.44
00000020 l     O .rodata.cst16  00000010 _ZN4core7unicode12unicode_data11white_space17SHORT_OFFSET_RUNS17h84cb836af6ba6fedE
00000030 l       .rodata.cst16  00000000

$ llvm-objdump -h /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib -j .rodata.cst16
 ⋮
/work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib(core-dd70e77b667e899d.core.1c09fd4f-cgu.9.rcgu.o): file format elf32-littleriscv

Sections:
Idx Name          Size     VMA      Type
167 .rodata.cst16 00000030 00000000 DATA


$ llvm-objdump -s /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib -j .rodata.cst16
 ⋮
/work/target/riscv32imac-unknown-none-elf/release/deps/libcore-dd70e77b667e899d.rlib(core-dd70e77b667e899d.core.2b561e14-cgu.9.rcgu.o): file format elf32-littleriscv
Contents of section .rodata.cst16:
 0000 43686172 54727946 726f6d45 72726f72  CharTryFromError
 0010 4e6f744e 756c5465 726d696e 61746564  NotNulTerminated
 0020 80160000 00202001 00306001 01307102  .....  ..0`..0q.

Notice the unnamed symbol at offset 0x00000030, which is just outside the section .rodata.cst16.

nightly-2022-08-12 (last known good nightly):

$ cargo build --release -Zbuild-std=core
 ⋮
    Finished release [optimized + debuginfo] target(s) in 18.87s

$ llvm-objdump -t /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-18b8786d85f8f8c1.rlib | rg 'cst16|cgu\.9'
 ⋮
/work/target/riscv32imac-unknown-none-elf/release/deps/libcore-18b8786d85f8f8c1.rlib(core-18b8786d85f8f8c1.core.2b561e14-cgu.9.rcgu.o): file format elf32-littleriscv
00000000 l    df *ABS*  00000000 core.2b561e14-cgu.9
00000000 l     O .rodata.cst16  00000010 .Lanon.736cf878ba36b4be6f1568c62f41c419.40
00000010 l     O .rodata.cst16  00000010 .Lanon.736cf878ba36b4be6f1568c62f41c419.44
00000020 l     O .rodata.cst16  00000010 _ZN4core7unicode12unicode_data11white_space17SHORT_OFFSET_RUNS17hd6eb1961d178650dE

$ llvm-objdump -h /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-18b8786d85f8f8c1.rlib -j .rodata.cst16
 ⋮
/work/target/riscv32imac-unknown-none-elf/release/deps/libcore-18b8786d85f8f8c1.rlib(core-18b8786d85f8f8c1.core.2b561e14-cgu.9.rcgu.o): file format elf32-littleriscv

Sections:
Idx Name          Size     VMA      Type
167 .rodata.cst16 00000030 00000000 DATA

$ llvm-objdump -s /work/target/riscv32imac-unknown-none-elf/release/deps/libcore-18b8786d85f8f8c1.rlib -j .rodata.cst16
 ⋮
/work/target/riscv32imac-unknown-none-elf/release/deps/libcore-18b8786d85f8f8c1.rlib(core-18b8786d85f8f8c1.core.2b561e14-cgu.9.rcgu.o): file format elf32-littleriscv
Contents of section .rodata.cst16:
 0000 43686172 54727946 726f6d45 72726f72  CharTryFromError
 0010 4e6f744e 756c5465 726d696e 61746564  NotNulTerminated
 0020 80160000 00202001 00306001 01307102  .....  ..0`..0q.

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (f22819bcc 2022-08-12)
binary: rustc
commit-hash: f22819bcce4abaff7d1246a56eec493418f9f4ee
commit-date: 2022-08-12
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions