Skip to content

Compiler bug #13976

Description

@james-elliot

Zig Version

0.11.0-dev.815+88b49ed00

Steps to Reproduce and Observed Behavior

In the following code, the program compiles and runs properly when NB_BITS is 27 or less, but the compilation fails if it is 28 with a relocation problem:

zig build-exe p4.zig
error: ld.lld: p4.o:(function start.posixCallMainAndExit: .text+0x64d): relocation R_X86_64_32S out of range: 2149950032 is not in [-2147483648, 2147483647]
const NB_BITS: u8 = 28;

const std = @import("std");
const stdout = std.io.getStdOut().writer();

const HASH_MASK: u64 = (1 << NB_BITS) - 1;
const HASH_SIZE: usize = 1 << NB_BITS;
var hashes: [HASH_SIZE]u64 = undefined;

fn retrieve(hv: u64) bool {
    const ind: usize = (hv & HASH_MASK);
    if (hashes[ind] == hv) return true else return false;
}

pub fn main() !void {
    for (hashes) |*a| a.* = 0;
    if (retrieve(0)) try stdout.print("{d}\n", .{0});
}

Expected Behavior

Correct compilation...

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    on Dec 16, 2022
  2. added
    backend-llvmThe LLVM backend outputs an LLVM IR Module.
    upstreamAn issue with a third party project that Zig uses.
    on Dec 16, 2022
  3. added this to the 0.12.0 milestone on Dec 16, 2022
  4. FanShupei commented on Dec 20, 2022

    @FanShupei
    Contributor

    I'd like to say this is not zig's bug, this is the intended behavior. The related concept is [code model][https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html]. In gcc, it's controlled by -mcmodel=small|medium|large. The default model is small model, all code and data must live in 2GB. When NB_BITS is set to 28 or higher, hashes will exceed 2GB. That's the problem.

    However, currently zig's support for code model is very limited, only supporting default and tiny. If zig adds support for medium and large code model, the code should run in proper code model.

  5. removed
    upstreamAn issue with a third party project that Zig uses.
    backend-llvmThe LLVM backend outputs an LLVM IR Module.
    on Dec 20, 2022
  6. Vexu commented on Dec 20, 2022

    @Vexu
    Member

    I'd like to say this is not zig's bug, this is the intended behavior. The related concept is [code model][https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html]. In gcc, it's controlled by -mcmodel=small|medium|large. The default model is small model, all code and data must live in 2GB. When NB_BITS is set to 28 or higher, hashes will exceed 2GB. That's the problem.

    It's still a bug in that there should be a compiler error before it reaches the linker.

    However, currently zig's support for code model is very limited, only supporting default and tiny. If zig adds support for medium and large code model, the code should run in proper code model.

    Medium and large are supported in addition to small and kernel.

      -mcmodel=[default|tiny|   Limit range of code and data virtual addresses
                small|kernel|
                medium|large]
    
  7. FanShupei commented on Dec 20, 2022

    @FanShupei
    Contributor

    It's still a bug in that there should be a compiler error before it reaches the linker.

    I agree with you. gcc and clang do nothing and just defer the error to linking. If zig could detect this then give a nicer message instead of a cryptic linking error, this is definitely an improvement. But I'm afraid that it's hard to solve perfectly. Thinking an extreme case, two object files each contains a 1GB array, it will only explode when they are linking together.

    Medium and large are supported in addition to small and kernel.

    You are right. I was just using zig build-exe -h | grep mcmodel, so I didn't see the last two lines, Sorry.

  8. Vexu commented on Dec 20, 2022

    @Vexu
    Member

    I agree with you. gcc and clang do nothing and just defer the error to linking. If zig could detect this then give a nicer message instead of a cryptic linking error, this is definitely an improvement. But I'm afraid that it's hard to solve perfectly. Thinking an extreme case, two object files each contains a 1GB array, it will only explode when they are linking together.

    Sure but a lot of the times a compilation will only contain Zig code which is all in one compilation unit in which this should be possible to detect.

  9. modified the milestones: 0.12.0, 1.0.0 on Dec 20, 2022
  10. added
    enhancementSolving this issue will likely involve adding new logic or components to the codebase.
    error messageThis issue points out an error message that is unhelpful and should be improved.
    on Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorenhancementSolving this issue will likely involve adding new logic or components to the codebase.error messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions