Commit Graph

20130 Commits

Author SHA1 Message Date
Andrew Kelley
9501ea688f stage2: --autofix proof-of-concept
* Introduce the `--autofix` CLI flag when building an executable,
   object, or library.
 * Refactor std.zig.render to use a struct parameter to make it easier
   to add/remove fields from the struct.
 * Introduce a "Fixups" concept to std.zig.render which can perform
   edits while rendering code, while leaving the Ast read-only.
 * Add a fixup for adding a discard after a variable declaration.
 * Update the Module code to check for fixable errors after AstGen
   lowering.

Improvements that need to be made before this can be merged:

 * Introduce an error for "pointless discard" and a fixup for it so that
   --autofix can undo the effects of itself when a variable becomes
   used.
 * Support local variables as well as local constants.
 * Support captures in addition to local variables and constants.
 * Integrate properly with incremental compilation.
 * Integrate with the Zig build system.
 * Distinguish between AstGen errors that can be autofixed and those
   that cannot.
 * Remove std.debug.print statements.
 * Only perform fixups when all errors are autofixable errors. However,
   suppress all autofixable errors when reporting errors with --autofix.

The purpose of this feature is to satisfy two use cases that
traditionally have been at odds:

 * Some people want the guarantee that all Zig code they read has the
   property that there are no unused locals.
 * Some people find no value from such errors and want to not have to
   deal with them.

The problem with an `--allow-unused` flag is that people from the second
group will use it, and then some projects will fail to compile without
the flag enabled.

I like to think of Zig as having "inline warnings". The warnings are
there, inside your source code, next to the relevant lines, ready to be
noticed by diffs, code reviews, and when refactoring.

`--autofix` is a way for Zig to automatically insert inline warnings for
those who wish to iterate quickly on a messy codebase.
2022-09-09 18:49:05 -07:00
Andrew Kelley
5739099989 std.fs.File.setEndPos: add the word "truncate" to doc comments
To aid people searching autodocs.
2022-09-09 18:48:39 -07:00
Andrew Kelley
9e070b653c
Merge pull request #12788 from ziglang/detect-native-glibc
std.zig.system.NativeTargetInfo: improve glibc version and dynamic linker detection
2022-09-09 12:28:25 -04:00
Andrew Kelley
68e61bbc0c std.zig.system.NativeTargetInfo: more headroom for libc.so.6 .dynstr 2022-09-09 09:27:02 -07:00
Yusuf Bham
c9f145a50b std.os.uefi: mark BlockIoProtocol and EfiBlockMedia as public 2022-09-09 16:48:54 +03:00
Jakub Konka
56b96cd61b
Merge pull request #12772 from ziglang/coff-basic-imports
coff: implement enough of the incremental linker to pass behavior and incremental tests on Windows
2022-09-09 13:08:58 +02:00
Andrew Kelley
c7d6048081 std.zig.system.NativeTargetInfo: add fallback check
After failing to find RUNPATH in the ELF of /usr/bin/env, not finding
the answer in a symlink of the dynamic interpreter, and not finding
libc.so.6 in the same directory as the dynamic interpreter, Zig will
check `/lib/$triple`.

This fixes incorrect native glibc version detected on Debian bookworm.
2022-09-09 00:07:02 -07:00
Jakub Konka
5006fb6846 macho: fix compilation for 32bit targets 2022-09-09 08:30:27 +02:00
Andrew Kelley
9f40f34501 std.zig.system.NativeTargetInfo: restore symlink logic
This is a partial revert of the previous commit, fixing a regression on
Debian. However, the commit additionally improves the
detectAbiAndDynamicLinker function to read more than 1 byte at a time
when detecting a shebang line.
2022-09-08 20:52:49 -07:00
Andrew Kelley
c668396941 std.zig.system.NativeTargetInfo: handle missing DT_RUNPATH
This commit removes the check that takes advantage of when the dynamic
linker is a symlink. Instead, it falls back on the same directory as the
dynamic linker as a de facto runpath. Empirically, this gives correct
results on Gentoo and NixOS.

Unfortunately it is still falling short for Debian, which has libc.so.6
in a different directory as the dynamic linker.
2022-09-08 20:52:49 -07:00
Andrew Kelley
1b6fa1965a stage2: fix building for 32-bit targets 2022-09-08 20:52:49 -07:00
Andrew Kelley
3ee01c14ee std.zig.system.NativeTargetInfo: detection ignores self exe
Before, native glibc and dynamic linker detection attempted to use the
executable's own binary if it was dynamically linked to answer both the
C ABI question and the dynamic linker question. However, this could be
problematic on a system that uses a RUNPATH for the compiler binary,
locking it to an older glibc version, while system binaries such as
/usr/bin/env use a newer glibc version. The problem is that libc.so.6
glibc version will match that of the system while the dynamic linker
will match that of the compiler binary. Executables with these versions
mismatching will fail to run.

Therefore, this commit changes the logic to be the same regardless of
whether the compiler binary is dynamically or statically linked. It
inspects `/usr/bin/env` as an ELF file to find the answer to these
questions, or if there is a shebang line, then it chases the referenced
file recursively. If that does not provide the answer, then the function
falls back to defaults.

This commit also solves a TODO to remove an Allocator parameter to the
detect() function.
2022-09-08 20:52:49 -07:00
Andrew Kelley
fa940bafa2 std.zig.system.NativeTargetInfo: improve glibc version detection
Previously, this code would fail to detect glibc version because it
relied on libc.so.6 being a symlink which revealed the answer. On modern
distros, this is no longer the case.

This new strategy finds the path to libc.so.6 from /usr/bin/env, then
inspects the .dynstr section of libc.so.6, looking for symbols that
start with "GLIBC_2.". It then parses those as semantic versions and
takes the maximum value as the system-native glibc version.

closes #6469
   see #11137
closes #12567
2022-09-08 20:52:49 -07:00
Motiejus Jakštys
a833bdcd7e [ld] add --print-* for diagnostics
This adds the following for passthrough to lld:
- `--print-gc-sections`
- `--print-icf-sections`
- `--print-map`

I am not adding these to the cache manifest, since it does not change
the produced artifacts.

Tested with an example from #11398: it successfully prints the resulting
map and the GC'd sections.
2022-09-08 22:30:32 -04:00
Jakub Konka
8378cde743 macho: prefill any space between __DATA and __LINKEDIT with 0s if required
If there are zerofill sections, the loader may copy the contents of
the physical space in file directly into memory and attach that to
the zerofill section. This is a performance optimisation in the loader
but requires us, the linker, to properly zero-out any space between
__DATA and __LINKEDIT segments in file. This is of course completely
skipped if there are no zerofill sections present.
2022-09-09 00:01:22 +02:00
Luuk de Gram
ab4b26d8a6
Merge pull request #12770 from Luukdegram/wasm-dwarf
wasm-linker: implement linking debug-information
2022-09-08 16:57:23 +02:00
Veikka Tuominen
6a62a15ecd
Merge pull request #12773 from Vexu/stage2-fixes
Sema: fix UAF in zirClosureGet
2022-09-08 16:43:54 +03:00
Jakub Konka
0ae2ea671b wasm: temporarily save curr file pointer before pwriting on Win
This is a temporary workaround to an unclear platform-dependence
behavior we have in libstd for `std.fs.File` abstraction. See
https://github.com/ziglang/zig/issues/12783 for more information.
2022-09-08 14:29:54 +02:00
Veikka Tuominen
c7e45aebaf llvm: handle pointers in packed structs in more places
Closes #12776
2022-09-08 12:51:40 +03:00
Martin Stuurwold
a7661f115d NativeTargetInfo.zig: fix typo 2022-09-08 01:53:32 -04:00
Veikka Tuominen
44b9a1d031 translate-c: use correct name for failDecl
Closes #12737
2022-09-08 01:00:19 +03:00
Veikka Tuominen
99826a2ba8 Sema: fix UAF in zirClosureGet
Previously if a decl failed its capture scope would be deallocated and
set to undefined which would then lead to invalid dereference in
`zirClosureGet`. To avoid this set the capture scope to a special
failed state and fail the current decl with dependency failure if
the failed state is encountered in `zirClosureGet`.

Closes #12433
Closes #12530
Closes #12593
2022-09-08 00:37:11 +03:00
Veikka Tuominen
37afab2add Sema: preserve alignment of const decl pointers
Closes #12769
2022-09-08 00:37:11 +03:00
Veikka Tuominen
0fa80e66b7 Sema: correct types in @memset and @memcpy
Closes #12750
2022-09-08 00:37:11 +03:00
Jakub Konka
b98b3252be revert part of 945111ae92 2022-09-07 23:18:57 +02:00
Jakub Konka
0e152b76ac tests: force LLD off for stage2 backends until auto-select deems worthy 2022-09-07 23:16:42 +02:00
Jakub Konka
c4d297b1af test-cases: add missing incremental x86_64-windows test cases 2022-09-07 22:42:59 +02:00
Jakub Konka
a226aef36c test-cases: enable stage2 tests on Windows 2022-09-07 22:42:59 +02:00
Jakub Konka
678e07b924 macho+wasm: unify and clean up closing file handles 2022-09-07 22:42:59 +02:00
Jakub Konka
639237c7b4 macho: set file instance in linkOneShot only if not already set 2022-09-07 22:42:59 +02:00
Jakub Konka
8ef1c62f2e macho: properly close file handles owned by the linker in deinit() 2022-09-07 22:42:59 +02:00
Jakub Konka
215fce8c51 coff: fix tracking of got and import entries; free relocs in update* fns 2022-09-07 22:42:59 +02:00
Jakub Konka
99c2cb72e8 coff: track globals in contiguous array to allow for tombstones 2022-09-07 22:42:59 +02:00
Jakub Konka
f3e4e44a2b coff: fix memory leak in incorrectly disposing of globals table 2022-09-07 22:42:58 +02:00
Jakub Konka
945111ae92 enable testing of x86_64-windows-gnu using self-hosted backend and linker 2022-09-07 22:42:58 +02:00
Jakub Konka
7b8cc599d9 coff: use more generous initial memory sizes for sections 2022-09-07 22:42:58 +02:00
Jakub Konka
16ca47b9b8 coff: remove redundant bits and clean up 2022-09-07 22:42:58 +02:00
Jakub Konka
2b373b0579 coff: grow section in virtual address space when required 2022-09-07 22:42:58 +02:00
Jakub Konka
9116e0f746 coff: find new file space for a section (file offsets) 2022-09-07 22:42:58 +02:00
Jakub Konka
79e51c5e4b coff: differentiate between file space and VM space for alloc 2022-09-07 22:42:58 +02:00
Jakub Konka
08f6546c84 coff: create a helper for allocating sections 2022-09-07 22:42:58 +02:00
Jakub Konka
f1bdf3f62f coff: fix writing strtab to PE image file 2022-09-07 22:42:57 +02:00
Jakub Konka
467d69c68a x86_64: fix SystemV calling convention 2022-09-07 22:42:57 +02:00
Jakub Konka
56a131d27a test-cases: fix compiler error locations for hello world with updates 2022-09-07 22:42:57 +02:00
Jakub Konka
66bad3eaaf coff: mark relocations dirty when target atoms change 2022-09-07 22:42:57 +02:00
Jakub Konka
1e2a2d6fad coff: fix bug in lowerUnnamedConst 2022-09-07 22:42:57 +02:00
Jakub Konka
423f424c27 libstd: use windows.GetStdHandle() with stage2_x86_64 backend for now 2022-09-07 22:42:57 +02:00
Jakub Konka
e0167ae0e3 x86_64: allow for any index register in complex SIB encodings
This relieves register pressure, and reduce generated code size
(since now we can use the same index register for both `mov_scale_src`
and `mov_scale_dst` MIR instructions).

Fix lowering of ModRM + SIB encodings where index register is extended
- previously, we would carelessly ignore the fact generating incorrect
encodings.
2022-09-07 22:42:57 +02:00
Jakub Konka
619d82234e x86_64: clean up return registers for unspecified cc 2022-09-07 22:42:57 +02:00
Jakub Konka
28f525baa4 x86_64: handle ptr_stack_offset for blocks 2022-09-07 22:42:56 +02:00