rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.57k stars 12.74k forks source link

inline assmbly load symbol address will cause `relocation R_X86_64_32S out of range` #113984

Closed leelingrui closed 1 year ago

leelingrui commented 1 year ago

Code

#![no_main]
#![no_std]
use core::arch::asm;

use core::panic::PanicInfo;

const IDT_SIZE : usize = 0x100;
type HandlerFn = *const extern fn();

#[no_mangle]
pub static mut HANDLER_TABLE : [HandlerFn; IDT_SIZE] = [core::ptr::null(); IDT_SIZE];
#[no_mangle]
pub fn _start()
{
    unsafe { asm!(
            "call [HANDLER_TABLE + rax * 8]"
        ) };
}

#[panic_handler]
pub fn panic(_info: &PanicInfo) -> !
{
    loop { }
}

.cargo/config.toml

[build]
target = "x86_64-unknown-none"
target-dir = "./build"

rustflags = [
    "-Clink-arg=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes"
]

src/linker.ld

OUTPUT_ARCH(x86_64)
ENTRY(_start)
BASE_ADDRESS = 0xffff800000100000;

SECTIONS
{
    . = BASE_ADDRESS;
    skernel = .;

    stext = .;
    .text : {
        *(.text .text.*)
    }

    . = ALIGN(4K);
    .dynamic : {
        *(.dynamic)
    }

    . = ALIGN(4K);
    .got : {
        *(.got)
    }

    . = ALIGN(4K);
    erodata = .;
    sdata = .;
    .data : {
        *(.data .data.*)
        *(.sdata .sdata.*)
    }

    . = ALIGN(4K);
    etext = .;
    srodata = .;
    .rodata : {
        *(.rodata .rodata.*)
        *(.srodata .srodata.*)
    }

    . = ALIGN(4K);
    edata = .;
    .bss : {
        *(.bss.stack)
        sbss = .;
        *(.bss .bss.*)
        *(.sbss .sbss.*)
    }
    . = ALIGN(4K);
    dyn = .;
    .dynsym :
    {
        *(.dynsym)
    }
    .gnu.hash : {
        *(.gnu.hash)
    }
    .hash :
    {
        *(.hash)
    }
    . = ALIGN(4K);
    ebss = .;
    ekernel = .;

    /DISCARD/ : {
        *(.eh_frame)
    }
}

Meta

rustc --version --verbose:

rustc 1.73.0-nightly (399b06823 2023-07-20)
binary: rustc
commit-hash: 399b068235ceea440540539b3bfd1aeb82214a28
commit-date: 2023-07-20
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Error output

= note: rust-lld: error: /root/test_rust/./build/x86_64-unknown-none/debug/deps/test_rust-f16dd41437df1e78.1q363ytq95j1e9xg.rcgu.o:(function _start: .text._start+0x7): relocation R_X86_64_32S out of range: -140737487298560 is not in [-2147483648, 2147483647]; references HANDLER_TABLE
          >>> referenced by main.rs:15 (src/main.rs:15)
          >>> defined in /root/test_rust/./build/x86_64-unknown-none/debug/deps/test_rust-f16dd41437df1e78.1q363ytq95j1e9xg.rcgu.o
Backtrace

``` root@DESKTOP-77O23BF:~/test_rust# RUST_BACKTRACE=1 cargo build Compiling test_rust v0.1.0 (/root/test_rust) error: linking with `rust-lld` failed: exit status: 1 | = note: LC_ALL="C" PATH="/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/root/.cargo/bin:/root/.vscode-server/bin/74f6148eb9ea00507ec113ec51c489d6ffb4b771/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.1/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.1/libnvvp:/mnt/c/Python311/Scripts/:/mnt/c/Python311/:/mnt/c/Program Files (x86)/Common Files/MVS/Runtime/Win32_i86:/mnt/c/Program Files (x86)/Common Files/MVS/Runtime/Win64_x64:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Microsoft SQL Server/150/Tools/Binn/:/mnt/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/170/Tools/Binn/:/mnt/c/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/Scripts:/mnt/c/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin:/mnt/d/opencv/build/x64/vc15/lib:/mnt/d/opencv/build/x64/vc15/bin:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/d/MinGW/bin:/mnt/c/Qt/Tools/CMake_64/bin:/mnt/c/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer/Git/mingw64/bin:/mnt/d/IDEA/IntelliJ IDEA Community Edition 2020.3.3/jbr/bin:/mnt/c/Program Files/dotnet:/mnt/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64:/mnt/c/Program Files/nodejs/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/NVIDIA Corporation/Nsight Compute 2023.1.0/:/mnt/f/llvm-project-llvmorg-15.0.7/build:/mnt/c/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja:/mnt/d/Graphviz/bin:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Users/lingruilee/.cargo/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/extras/CUPTI/lib64:/mnt/c/Program Files (x86)/Common Files/MVS/Runtime/Win32_i86:/mnt/c/Program Files (x86)/Common Files/MVS/Runtime/Win64_x64:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/libnvvp:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Microsoft SQL Server/150/Tools/Binn/:/mnt/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/170/Tools/Binn/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/Scripts:/mnt/c/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64:/mnt/c/Program Files/NVIDIA Corporation/Nsight Compute 2022.1.1/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v:/mnt/c/Users/lingruilee/AppData/Roaming/npm:/mnt/c/Users/lingruilee/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/lingruilee/.dotnet/tools:/root/qemu-8.0.0/build" VSLANG="1033" "rust-lld" "-flavor" "gnu" "/tmp/rustcQTz282/symbols.o" "/root/test_rust/./build/x86_64-unknown-none/debug/deps/test_rust-f16dd41437df1e78.1q363ytq95j1e9xg.rcgu.o" "--as-needed" "-L" "/root/test_rust/./build/x86_64-unknown-none/debug/deps" "-L" "/root/test_rust/./build/debug/deps" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-none/lib" "-Bstatic" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-none/lib/librustc_std_workspace_core-2d0ea67b4f17a921.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-none/lib/libcore-8d771b431f12fe2c.rlib" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-none/lib/libcompiler_builtins-53d2f51eb0e48bb6.rlib" "-Bdynamic" "--eh-frame-hdr" "-z" "noexecstack" "-L" "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-none/lib" "-o" "/root/test_rust/./build/x86_64-unknown-none/debug/deps/test_rust-f16dd41437df1e78" "--gc-sections" "-pie" "-z" "relro" "-z" "now" "-Tsrc/linker.ld" = note: rust-lld: error: /root/test_rust/./build/x86_64-unknown-none/debug/deps/test_rust-f16dd41437df1e78.1q363ytq95j1e9xg.rcgu.o:(function _start: .text._start+0x7): relocation R_X86_64_32S out of range: -140737487298560 is not in [-2147483648, 2147483647]; references HANDLER_TABLE >>> referenced by main.rs:15 (src/main.rs:15) >>> defined in /root/test_rust/./build/x86_64-unknown-none/debug/deps/test_rust-f16dd41437df1e78.1q363ytq95j1e9xg.rcgu.o error: could not compile `test_rust` (bin "test_rust") due to previous error ```

riking commented 1 year ago

Your linker script has some problems. For one, blatantly wrong erodata, etext, edata, and ebss.

More relevantly, that offset is 0x0000_7fff_ffef_e000, which tells me that either the code or data isn't getting properly shifted up to high addresses.

leelingrui commented 1 year ago

@riking maybe this linker script has problem because I copy it from a tutorial. but I'm sure the code and date shifted up to correct address. I changed my BASE_ADDRESS to 0x100000 it works properly image if BASE_ADDRESS is 0x500000 all section has been move to correct position image I checked symbol table HANDLER_TABLE correctly been placed at 0x50000 image by the way address 0x0000_7fff_ffef_e000 also occur same error

leelingrui commented 1 year ago

error only occur if I use inline assmbly directly access symbol address. image this situation won't occur same error if my code and data section has some problem I guess it will occur again