Skip to content

newlib: fix definition of time_t and off_t#5132

Open
dybucc wants to merge 5 commits into
rust-lang:mainfrom
dybucc:newlib-fix-time_t
Open

newlib: fix definition of time_t and off_t#5132
dybucc wants to merge 5 commits into
rust-lang:mainfrom
dybucc:newlib-fix-time_t

Conversation

@dybucc

@dybucc dybucc commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Description

This PR addresses bit width mismatches for both time_t and file offset types. This applies to the linux/newlib module.

Upstream and forks for each supported target provide conditional definitions. time_t can be configured in the newlib build script. File offset types require manual intervention in a specific header file. The file containing the default definition will check for a macro that is defined in that specific "override" file. The default definition is a C long.

A number of supported targets were using either one of the above overrides. This was not being taken into account in the libc crate. This patch fixes that by further expanding conditionally compiled type definitions.

Sources

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget); especially relevant for platforms that may not be checked in CI

@dybucc

dybucc commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

CI seems to be failing for reasons unrelated to the changes introduced in the patch. A rerun should
do it.

@dybucc dybucc changed the title refactor: fix definition of time_t in Newlib refactor: fix definition of time_t and off_t in Newlib Jun 3, 2026
@dybucc dybucc force-pushed the newlib-fix-time_t branch 4 times, most recently from 7152f77 to e821035 Compare June 7, 2026 16:34
@dybucc dybucc marked this pull request as ready for review June 7, 2026 16:35
@dybucc dybucc force-pushed the newlib-fix-time_t branch from e821035 to de697e2 Compare June 9, 2026 07:11
@rustbot

This comment has been minimized.

@dybucc

dybucc commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

CI actually passes. There seems to be an issue with a glob import that is not used, but this has not
been changed in the patch (it's not even part of it, for that matter.) For some reason, rebasing
onto main with dependabot updates has ended up with a warning across all of my open PRs due to
that one (now apparently unused) import.

@dybucc dybucc force-pushed the newlib-fix-time_t branch from de697e2 to 0e027c7 Compare June 15, 2026 15:11
@rustbot

This comment has been minimized.

@dybucc dybucc force-pushed the newlib-fix-time_t branch from 0e027c7 to 6abed2e Compare June 18, 2026 16:20
@rustbot

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Contributor

I couldn't remember what the possible env+os combinations were for these platforms, here's a quick list.

> $cfg | find 'target_env="newlib"' | get tgt | each {|tgt| $cfg | find $tgt -n | find -nr '(target_env|target_os)' } | flatten
  #               tgt                        cfg
─────────────────────────────────────────────────────────
  0   armv6k-nintendo-3ds            target_env="newlib"
  1   armv6k-nintendo-3ds            target_os="horizon"
  2   armv7-rtems-eabihf             target_env="newlib"
  3   armv7-rtems-eabihf             target_os="rtems"
  4   armv7-sony-vita-newlibeabihf   target_env="newlib"
  5   armv7-sony-vita-newlibeabihf   target_os="vita"
  6   riscv32imac-esp-espidf         target_env="newlib"
  7   riscv32imac-esp-espidf         target_os="espidf"
  8   riscv32imafc-esp-espidf        target_env="newlib"
  9   riscv32imafc-esp-espidf        target_os="espidf"
 10   riscv32imc-esp-espidf          target_env="newlib"
 11   riscv32imc-esp-espidf          target_os="espidf"
 12   xtensa-esp32-espidf            target_env="newlib"
 13   xtensa-esp32-espidf            target_os="espidf"
 14   xtensa-esp32s2-espidf          target_env="newlib"
 15   xtensa-esp32s2-espidf          target_os="espidf"
 16   xtensa-esp32s3-espidf          target_env="newlib"
 17   xtensa-esp32s3-espidf          target_os="espidf"

@tgross35 tgross35 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having some difficulty extracting a simplified goal from the top post. Is this the net delta on targets we currently support?

  • On armv7-rtems, time_t changes from i32 to i64
  • On armv7-rtems, off_t changes from i64 to c_long, which should be i64->i32
  • On armv7-vita, off_t changes from c_int to c_long. I think there is no change here?

If this sounds accurate, ping the target maintainer (see https://doc.rust-lang.org/rustc/platform-support/armv7-rtems-eabihf.html) for confirmation. This is generally a good idea when tricky things like this change on less popular targets, there may be config or other nuance that doesn't show up in the source code.

Also please don't call this a refactor, that term usually means cleaning up code without changing behavior - if the user experience changes somehow then it's kind of misleading. (Applies to some of your other PRs as well).

Note this option is "documented" as being meant for end users of the libc crate to enable, but no information on it is provided anywhere in all of the libc crate.

It's mostly intended to be set by espidf build tooling, but it would be good to at least mention it. A PR would be welcome in the crate-level docs, after #5179 adds some structure.

It would also be great to know whether the libc crate considers horizon to be the OS for both the Nintendo Switch and the Nintendo 3DS targets, or only for the Nintendo 3DS target.

aarch64-nintendo-switch-freestanding does not appear to set a target_env, so I would assume the latter.

View changes since this review

@tgross35

Copy link
Copy Markdown
Contributor

@rustbot author for clarification

@rustbot

rustbot commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@dybucc dybucc changed the title refactor: fix definition of time_t and off_t in Newlib newlib: fix definition of time_t and off_t Jun 19, 2026
@dybucc

dybucc commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

The changes here are required to fit the definitions upstream of time_t, which was generally mismatched.

The changes for file offset types are concerned with either

  1. having a mirrored definition, rather an an equivalent one, and
  2. with the same bit width mismatches as with time_t.

I'll open a PR soon with the documentation for ESP-IDF.

@rustbot ready

@dybucc dybucc force-pushed the newlib-fix-time_t branch from 6abed2e to c2b0970 Compare June 19, 2026 10:56
@rustbot

This comment has been minimized.

@dybucc

dybucc commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

Hey @thesummer!

I was wondering if I could get confirmation on these changes on the RTEMS target you maintain?

@dybucc

dybucc commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

Hey @nikarh @pheki @zetanumbers!

I was wondering if I could get confirmation on these changes on the Sony PS Vita target you maintain?

@pheki pheki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for asking!

Just need a change as the Vita uses 32-bit time_t.

Keeping it on the top-level comment for easy reference, here are the sources I could find (somewhat redundant with the finds in the root PR comment but in vitasdk's fork):

off_t

time_t

View changes since this review

Comment thread src/unix/newlib/mod.rs Outdated
Comment on lines +9 to +14
if #[cfg(any(
target_os = "espidf",
target_os = "vita",
target_os = "rtems",
target_arch = "aarch64"
))] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For vita, this one seems correct, as by looking at the definitions it seems to be defined as a long. There should also be no change in behavior as both c_int and c_long are aliases to i32 in this platform.

Sources in the top level review comment.

Comment thread src/unix/newlib/mod.rs
Comment on lines +60 to +61
} else {
pub type time_t = i64;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks wrong for the vita, which uses 32-bit time, as --enable-newlib-long-time_t is set when building newlib in vitasdk (currently the only supported SDK).

The correct definition would be c_long (effectively i32).

Sources in the top-level comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. That's fixed now.

@dybucc dybucc requested a review from tgross35 June 21, 2026 17:04
@pheki

pheki commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

I may be misunderstanding or missing something here, but I have some questions. First regarding this:

having a mirrored definition, rather an an equivalent one, and

You kept the vita with pub type time_t = i32;. If the idea is to have a mirrored definition, shouldn't it be c_long?

horizon has a gone from 32-bit to 64-bit time_t.

Wasn't it already 64-bit, as c_longlong is an alias to i64 on all supported platforms?

RTEMS is unaffected.
AArch64 had changes to its file offset types.

It seems like time_t has changed from i32 to i64 for those, no?

@thesummer

Copy link
Copy Markdown
Contributor

Hey @thesummer!

I was wondering if I could get confirmation on these changes on the RTEMS target you maintain?

I just checked on the hardware with a compiled binary and both off_t and time_t are 8 bytes long.
So, to me the current state of the PR looks correct. Both times the RTEMS configuration should fall to the else block which sets the type to i64, right?

@tgross35

Copy link
Copy Markdown
Contributor

AArch64 had changes to its file offset types.

Which targets does this hit? I didn't see any from my quick search.

And then, espidf targets should also be unchanged right?

@pheki has a few good questions about the rest.

@dybucc dybucc force-pushed the newlib-fix-time_t branch from 1573775 to aa9f0af Compare June 23, 2026 13:09
@rustbot

This comment has been minimized.

@dybucc

dybucc commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

I may be misunderstanding or missing something here, but I have some questions. First regarding this:

having a mirrored definition, rather an an equivalent one, and

You kept the vita with pub type time_t = i32;. If the idea is to have a mirrored definition, shouldn't it be c_long?

horizon has a gone from 32-bit to 64-bit time_t.

Wasn't it already 64-bit, as c_longlong is an alias to i64 on all supported platforms?

RTEMS is unaffected.
AArch64 had changes to its file offset types.

It seems like time_t has changed from i32 to i64 for those, no?

Each of your queries is an item.

  1. You're right. I missed that. I also missed it in ESP-IDF.
  2. You're right. Changes were logged wrongly. They referred to the prior state of the branch. They did not refer to main.
  3. Ibid.

@dybucc

dybucc commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

AArch64 had changes to its file offset types.

Which targets does this hit? I didn't see any from my quick search.

And then, espidf targets should also be unchanged right?

@pheki has a few good questions about the rest.

AArch64 has changed. The changes are as follows:

  • dev_t: u32 -> c_short.
  • ino_t: u32 -> c_ushort.
  • off_t: i64 -> c_long. Not quite a change.

ESP-IDF is unchanged.

@dybucc

dybucc commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Hey @thesummer!
I was wondering if I could get confirmation on these changes on the RTEMS target you maintain?

I just checked on the hardware with a compiled binary and both off_t and time_t are 8 bytes long. So, to me the current state of the PR looks correct. Both times the RTEMS configuration should fall to the else block which sets the type to i64, right?

Thanks. That's right.

@dybucc dybucc force-pushed the newlib-fix-time_t branch 2 times, most recently from 284784b to 77fd7b4 Compare June 23, 2026 13:26
@tgross35

Copy link
Copy Markdown
Contributor

AArch64 has changed. The changes are as follows:

Target, not arch - we don't seem to have any newlib aarch64 targets, unless I'm missing something?

@dybucc

dybucc commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

You're right. We don't. I confused the devkitA64 SDK. Rust support on the aarch64-nintendo-switch-freestanding target is not implemented in terms of that.

No AArch64 targets are affected.

@dybucc dybucc force-pushed the newlib-fix-time_t branch from 77fd7b4 to 2fdd4f7 Compare June 25, 2026 07:49
@rustbot

This comment has been minimized.

@dybucc dybucc force-pushed the newlib-fix-time_t branch from 2fdd4f7 to d00f11f Compare June 25, 2026 10:14
@rustbot

This comment has been minimized.

@tgross35 tgross35 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are fine but have a question about the rtems source. This has gone through a few changes, could you post an updated summary of what changed on horizon/vita/rtems/espidf?

View changes since this review

Comment thread src/unix/newlib/mod.rs Outdated
if #[cfg(any(
target_os = "espidf",
target_os = "vita",
target_arch = "aarch64"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you drop aarch64 if not needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I also removed the aarch64 module.

Comment thread src/unix/newlib/mod.rs
Comment on lines -13 to 21
} else if #[cfg(any(target_os = "vita"))] {
pub type dev_t = c_short;
pub type ino_t = c_ushort;
pub type off_t = c_int;
} else {
pub type dev_t = u32;
pub type ino_t = u32;
pub type off_t = i64;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dybucc dybucc Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, thanks for clarifying.

Since these specific types are RTEMS-specific, I think it would be good to restrict this to target_os = "rtems" and not have a fallback. Which means any new OS using newlib won't build until they add these definitions, but that's a good thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except they're not. There's other targets that also use those. An example is horizon. See arm-non-eabi/include/machine/_types.h.

Do you mean to have multiple cfg_if blocks with the same definitions but a different predicate?

I can't link to sources. They're part of the downloadable SDK.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RTEMS and Horizon in that case - I just mean to exhaustively list the targets we know about, and let everything else error out or even compile_error!.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's done. I also noticed builds were failing. The newlib/generic module items were not being reexported.

@dybucc dybucc force-pushed the newlib-fix-time_t branch from d00f11f to a485281 Compare June 26, 2026 06:53
@rustbot

This comment has been minimized.

@dybucc dybucc force-pushed the newlib-fix-time_t branch 2 times, most recently from 4909667 to 8e6548a Compare June 26, 2026 06:57
@dybucc

dybucc commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

An updated summary of the changes follows.

  • vita went from i32 for time_t to c_long.
  • vita went from off_t as a c_int to off_t as a c_long.
  • AArch64 bindings got removed. There's no officially supported Rust target with that triple combination.

dybucc added 5 commits June 26, 2026 17:17
The `newlib` module uses a faulty definition for `time_t`. That type
falls back to a 32-bit signed integer definition except in `horizon` and
`espidf`.

The option to opt out of a 64-bit `time_t` is configurable. There were
other supported targets that had it enabled in their build scripts.
These were still having `time_t` exposed as 64-bits wide. This patch
fixes that.
Use mirrored definition of file offset types in `vita`.
Remove AArch64 bindigns in newlib. Rust has no supported target with
this triple combination.
@dybucc dybucc force-pushed the newlib-fix-time_t branch from 8e6548a to 3a97ab3 Compare June 26, 2026 15:26
@rustbot

rustbot commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants