Skip to content

tccelf: fix int(0x8000_0000) overflow in shf_private constant#7

Open
quaesitor-scientiam wants to merge 2 commits intofelipensp:stablefrom
quaesitor-scientiam:fix-shf-private-overflow
Open

tccelf: fix int(0x8000_0000) overflow in shf_private constant#7
quaesitor-scientiam wants to merge 2 commits intofelipensp:stablefrom
quaesitor-scientiam:fix-shf-private-overflow

Conversation

@quaesitor-scientiam
Copy link
Copy Markdown

@quaesitor-scientiam quaesitor-scientiam commented Apr 12, 2026

Problem

src/tccelf.v has two constants using explicit int() casts on hex literals:

const shf_private = int(0x8000_0000)  // overflows int (2147483648 > 2147483647)
const shf_dynsym  = int(0x4000_0000)  // doesn't overflow, but cast is redundant

shf_private overflows V's int. V currently emits a warning and will make this a hard error soon. The overflow causes broken generated C that TCC rejects:

src/tccelf.v:15:21: warning: value `0x80000000` overflows `int`, this will be considered hard error soon
cc: error: invalid operand types for binary operation

This makes vtcc fail to build in the vlang/v CI with V panic: result not set (failed). Tracked at vlang/v#26853.

Fix

Two commits:

  1. shf_private: replace int(0x8000_0000) with -2147483648 (INT_MIN — same bit pattern in two's complement, type stays int, no cascading changes needed in new_section/new_symtab/struct Section.sh_flags)

  2. shf_dynsym: remove the redundant int() cast — the value 0x4000_0000 = 1073741824 fits int fine, the cast is just noise

-const shf_private = int(0x8000_0000)
-const shf_dynsym  = int(0x4000_0000)
+const shf_private = -2147483648 // = 0x8000_0000 as two's complement int (INT_MIN)
+const shf_dynsym  = 0x4000_0000 // remove redundant int() cast; value fits int

Fixes #6

`const shf_private = int(0x8000_0000)` overflows V's `int` type
(max 2147483647). V currently emits a warning for this and will make
it a hard error in a future release. When the overflow occurs, it
produces broken C that the TCC backend rejects with:

    cc: error: invalid operand types for binary operation

causing the vtcc build to fail with `V panic: result not set (failed)`.

Fix: replace with `-2147483648`, which is INT_MIN — the same bit
pattern as `0x8000_0000` in two's complement signed representation.
All existing bitwise operations on this constant are unaffected, and
the type stays `int` so no downstream type changes are required.

Fixes: vlang/v#26853
quaesitor-scientiam pushed a commit to quaesitor-scientiam/v that referenced this pull request Apr 12, 2026
The previous patch (in vlang#26858) changed `int(0x8000_0000)` → `u32(0x8000_0000)`,
which fixed the overflow warning but introduced a new type error:

    failed src/tccelf.v:49:65: error: `(shf_private | shf_dynsym)` (no value)
    used as value in argument 4 to `new_symtab`

Root cause: `shf_dynsym` is still `int` and `new_symtab` takes `sh_flags int`;
V cannot OR `u32 | int` and implicitly pass the result as `int`. Changing to
`u32` throughout cascades into 20+ sites across tccelf.v and struct Section.

Correct workaround: use `-2147483648` (INT_MIN) instead — the identical bit
pattern as `0x8000_0000` in two's complement, with type `int`. No downstream
type changes needed. A proper fix has been filed upstream at felipensp/vtcc#7.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Follow-up to the shf_private fix: `int(0x4000_0000)` does not overflow
(1073741824 < 2147483647) but the explicit `int()` cast is redundant and
misleading alongside the now-fixed shf_private. Remove it so both flag
constants read consistently.

Addresses the second half of felipensp#6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants