Home > Net >  Why can't we declare uninitialized variables in .bss section using `?` in arbitrary order?
Why can't we declare uninitialized variables in .bss section using `?` in arbitrary order?

Time:11-18

This code works as intended:

section .bss
    var2:      DB     ?     
    X:         DW     ?     ; works

With the reservations in opposite order, the code doesn't assemble:

section .bss
    X:         DW     ?     
    var2:      DB     ?    ; error with lines in other order

I got this error, even though I don't use this label, var2 on another part of the program (in fact, this is reproducible assembling just that code block as a 3-line file).

error: label `var2' changed during code generation [-w error=label-redef-late]

I think the var2 variable is overwritten by the X variable because it's a word, on 2 bytes.

I am using NASM, version 2.15.04 to assemble this code (also reproducible with 2.15.05).

CodePudding user response:

After some debugging and poking around in the source code I can confirm my initial suspicion that this is a bug.

The size calculation for instructions of the form Dx ? (i.e. any Dx which includes a uninitialized storage token ?) where Dx is larger than DB internally returns the wrong size (assuming elements of 1 byte instead of the appropriate element size). This has the side effect of inconsistently altering the segment offset of any label following the instruction, causing a mismatch in the final code generation stage which is caught by a couple of checks and makes NASM error out.

Normally I would've simply reported the bug, but since NASM's GitHub repo does not have an "Issues" page active and their Bugzilla currently disallows registration I went ahead and submitted a pull request. The fix seems quite simple, unless there's something that I'm missing, in which case we'll find out (hopefully) soon.

  • Related