Commit Graph

10373 Commits

Author SHA1 Message Date
Motiejus Jakštys
c841f52fcb Elf: switch link order of libcompiler_rt and libc
Given `main.go`:

    package main
    import  _ "os/user"
    func main() {}

Compiling it to linux/arm64:

    $ CGO_CFLAGS='-O0' GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC="zig cc -target aarch64-linux-gnu.2.28" go build main.go

Results in this error:

    runtime/cgo(.text): unknown symbol memset in callarm64
    runtime/cgo(.text): unknown symbol memset in callarm64
    runtime/cgo(.text): relocation target memset not defined

In the midst of intermediate compilations files we can see this commmand:

    ld.lld -o _cgo_.o <...> /tmp/go-build206961058/b043/_x009.o <...> ~/.cache/zig/.../libcompiler_rt.a <...> ~/.cache/.../libc.so.6

`_x009.o` needs memset:

    $ readelf -Ws ./b043/_x009.o | grep memset
        22: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND memset

Both `libcompiler_rt.a` and `libc.so.6` provide it:

    $ readelf -Ws ~/.cache/zig/.../libcompiler_rt.a | grep memset
       870: 0000000000000000   318 FUNC    WEAK   DEFAULT  519 memset

    $ readelf -Ws ~/.cache/zig/.../libc.so.6 | grep -w memset
       476: 000000000001d34c     0 FUNC    GLOBAL DEFAULT    7 memset@@GLIBC_2.2.5

Since `libcompiler_rt.a` comes before libc in the linker line, the
resulting `_cgo_.o` still links to a weak, unversioned memset:

    $ readelf -Ws ./b043/_cgo_.o | grep -w memset
        40: 000000000022c07c   160 FUNC    WEAK   DEFAULT   14 memset
       719: 000000000022c07c   160 FUNC    WEAK   DEFAULT   14 memset

Since the final linking step is done by Golang's linker, it does not
know of `libcompiler_rt.a`, and fails to link with the error message
above. However, Go linker does recognize memset from glibc. If we
specify an `-lc` equivalent before the `libcompiler_rt.a`, it will link
to memset from libc:

    $ readelf -Wa ./b043/_x009.o |grep memset
        14: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND memset@GLIBC_2.17 (2)
       157: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND memset@GLIBC_2.17

... and then `main.go` will compile+link successfully.

Why doesn't Go linker take memset from glibc? An educated guess: Go
determines whether to link with glibc from what the program asks (I
presume `.dynsym`). Since `memset` is no longer attributed to glibc, Go
skips linking to glibc altogether.

Bonus question: curious why `-O0` is necessary? Because when
optimizations are enabled (the default), the C compiler replaces
`memset` function call with plain `stp` instructions (on aarch64).
2023-01-09 15:06:07 -07:00
IntegratedQuantum
d6faa4e868 Fix tautological big_int tests. 2023-01-09 15:04:55 -07:00
Andrew Kelley
07f6fc3000 Compilation: revert asking for exclusive locks on cache hits
We definitely want a shared lock on a cache hit. Without this, we get a
deadlock when Zig is asked to compile the same C source file multiple
times as part of the same compilation.

This is a partial revert of 8ccb9a6ad3.

cc @kcbanner
2023-01-09 15:02:38 -07:00
Casey Banner
a4418a8fd6 cache: Fix LockViolation during C compilation paths (#13591)
- C compilation flows didn't hold an exclusive lock on the cache manifest file when writing to it in all cases
- On windows, explicitly unlock the file lock before closing it
2023-01-09 15:00:03 -07:00
Andrew Kelley
bb957a976d Sema: resolve elem type for non-default aligned ptrs
Before this commit, I observe a crash from this abiSize call because the
element type is a struct that is not yet resolved. This is triggered by
running the behavior tests with -ofmt=c -target x86_64-windows.
2023-01-09 14:59:28 -07:00
Andrew Kelley
f8f65aebe6 zig cc: fix wrong flag name from earlier commit
In dfe9420244, I made a typo. This commit
corrects it. I tested this commit via a patch to zig-bootstrap.
2023-01-09 14:59:19 -07:00
Andrew Kelley
9067daeb94 zig cc: support -stack in addition to --stack for linker arg 2023-01-09 14:58:22 -07:00
Andrew Kelley
af958e95cc Merge pull request #13744 from Vexu/stage2-fixes
Improve error messages, fix dependency loops
2023-01-09 14:55:12 -07:00
Andrew Kelley
5fc7424a8d zig cc: detect -z stack-size arguments 2023-01-09 14:53:42 -07:00
Andrew Kelley
fb7ca1ad48 Sema: fix merge conflict regarding identifiers matching primitives 2023-01-09 14:51:03 -07:00
Veikka Tuominen
aa7e65d5ba Merge pull request #13719 from Vexu/debug
Improve debuggability of programs built by the self hosted compiler
2023-01-09 14:48:00 -07:00
Andrew Kelley
9d214f6f9d LLVM: fix canElideLoad behavior with loops
closes #13546
2023-01-09 14:46:40 -07:00
Veikka Tuominen
3357425b87 llvm: improve emitted debug info
* ensure parameter debug info is in the subroutine di scope
* slit sub file path into basename and dirname

Closes #12257
Closes #12665
2023-01-09 14:44:53 -07:00
Veikka Tuominen
b75197ef88 Merge pull request #13637 from Vexu/stage2-fixes
Stage2 bug fixes
2023-01-09 14:42:52 -07:00
Andrew Kelley
05afb3f358 Merge pull request #13632 from ziglang/cbe 2023-01-09 14:40:44 -07:00
Veikka Tuominen
7d812dc1c7 llvm: fix x86_64 sysV ABI of big vectors on avx512 enabled CPUs
Closes #13629
2023-01-09 14:38:35 -07:00
Jakub Konka
58d4d083ea value: account for undef value for ptr in slice in hashUncoerced 2023-01-09 14:38:04 -07:00
Andrew Kelley
8bbb4a2f81 disable failing arm-linux non-llvm backend tests
See #13623
2023-01-09 14:37:30 -07:00
Andrew Kelley
d27007cd7e CI: switch to GitHub Actions
This is a merge commit.
2023-01-09 14:37:20 -07:00
Veikka Tuominen
a3232c6764 Merge pull request #13585 from Vexu/stage2-fixes
Stage2 bug fixes
2023-01-09 14:35:42 -07:00
joachimschmidt557
10a660ebea stage2 ARM: misc fixes
- remove redundant `new` from `binOpRegisterNew` name
- fix mul_with_overflow
2023-01-09 14:35:32 -07:00
David Gonzalez Martin
9498c95faf uefi: Delete unneeded alignment and use default 4K
Closes #7484. Right now for UEFI targets an alignment
of 32 is being used for no reason other than support
a rare bytecode. As this is far from the standard case,
removing this alignment and using the default one,
as most toolchains do, should be the desired behavior.
2023-01-09 14:35:21 -07:00
Veikka Tuominen
da418ee0af translate-c: cast unsuffixed floats to f64 2023-01-09 14:34:05 -07:00
Guillaume Wenzek
58ac0082d2 compute LLVMTypes in ParamTypeIterator (#13592)
follow up on #13376 - fixes a bug in the x86_64 C ABI. 

Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-01-09 14:33:43 -07:00
Jimmi Holst Christensen
dd175e2928 c backend: Output string literals for array init of bytes 2023-01-09 14:29:59 -07:00
Casey Banner
90fedf24fc linker: fail the compilation if there were linker errors
There was no check for linker errors after flushing,
which meant that if the link failed the build would
continue and try to copy the non-existant exe, and
also write the manifest as if it had succeeded.

Also adds parsing of lld output, which is surfaced at the
end of the compilation with the other errors instead
of via stderr
2023-01-09 14:29:59 -07:00
kcbanner
c5900224fd windows: fix _tls_index not being defined if libc wasn't linked, and fix x86 name mangling 2023-01-09 14:18:09 -07:00
Guillaume Wenzek
91b8d9e370 fix Nvptx backend outputing files at the top level of zig-cache 2023-01-09 14:17:29 -07:00
Andrew Kelley
6064477928 Merge pull request #13561 from jacobly0/gcc-warnings 2023-01-09 14:17:06 -07:00
Veikka Tuominen
4ed0cd51c0 Merge pull request #13558 from Vexu/stage2-fixes
Stage2 bug fixes to get third party projects building
2023-01-09 14:06:50 -07:00
mike
9e50cb294b zig-cache: support windows drive + fwd-slash paths
closes #13539
2023-01-09 14:06:29 -07:00
mparadinha
35c13c262c Fix error reporting the wrong line for struct field inits (#13502)
* point to init part of field delc when that's where the error occurs

* update test to reflect fixed error message

* only lookup source location in case of error
2023-01-09 14:05:29 -07:00
50fb7a4144 musl.zig: remove unused enum (#13545) 2023-01-09 14:03:04 -07:00
Andrew Kelley
013d615077 Merge pull request #13536 from ziglang/cbe-zig-h
C backend: improve ergonomics of zig.h a little bit
2023-01-09 14:02:44 -07:00
Veikka Tuominen
33fe7f2881 Sema: remove block and src parameters from getBuiltin
These parameters are only ever needed when `std.builtin` is out of sync
with the compiler in which case panicking is the only valid operation
anyways. Removing them causes a domino effect of functions no longer
needing a `src` and/or a `block` parameter resulting in handling
compilation errors where they are actually meaningful becoming simpler.
2023-01-09 14:02:34 -07:00
Veikka Tuominen
b494f6a9dc Merge pull request #13497 from Vexu/stage2-fixes
Stage2 bug fixes
2023-01-09 14:01:01 -07:00
Cody Tapscott
bc33243650 stage2: Support modifiers in inline asm
These are supported using %[ident:mod] syntax. This allows requesting,
e.g., the "w" (32-bit) vs. "x" (64-bit) views of AArch64 registers.

See https://llvm.org/docs/LangRef.html#asm-template-argument-modifiers
2023-01-09 14:00:11 -07:00
Andrew Kelley
48798da29b Merge pull request #13074 from topolarity/stage2-opt
stage2: Miscellaneous fixes to vector arithmetic and copy elision
2023-01-09 13:59:04 -07:00
Jacob Young
b931889c65 Sema: avoid breaking hash contract when instantiating generic functions
* Add tagName to Value which behaves like @tagName.
 * Add hashUncoerced to Value as an alternative to hash when we want to
   produce the same hash for value that can coerce to each other.
 * Hash owner_decl instead of module_fn in Sema.instantiateGenericCall
   since Module.Decl.Index is not affected by ASLR like *Module.Fn was,
   and also because GenericCallAdapter.eql was already doing this.
 * Use Value.hashUncoerced in Sema.instantiateGenericCall because
   GenericCallAdapter.eql uses Value.eqlAdvanced to compare args, which
   ignores coersions.
 * Add revealed missing cases to Value.eqlAdvanced.

Without these changes, we were breaking the hash contract for
monomorphed_funcs, and were generating different hashes for values that
compared equal.  This resulted in a 0.2% chance when compiling
self-hosted of producing a different output, which depended on
fingerprint collisions of hashes that were affected by ASLR.  Normally,
the different hashes would have resulted in equal checks being skipped,
but in the case of a fingerprint collision, the truth would be revealed
and the compiler's behavior would diverge.
2023-01-09 13:58:52 -07:00
Jakub Konka
52f8de0194 Merge branch 'jcmoyer-lld-explicit-pdb' 2023-01-09 13:58:02 -07:00
Jakub Konka
84f5843178 Merge pull request #13495 from ziglang/macho-dsym
stage2: misc DWARF debug info fixes and additions for x86_64 and aarch64
2023-01-09 13:57:34 -07:00
Veikka Tuominen
c1fc15f913 llvm: implement packed unions
Closes #13340
2023-01-09 13:56:14 -07:00
Jakub Konka
eb21b20949 Merge pull request #13487 from ziglang/zld-dwarf-string
macho: misc DWARF parser fixes
2023-01-09 13:55:25 -07:00
Veikka Tuominen
6ea3d3e19f Merge pull request #13446 from Vexu/stage2-fixes
Stage2 bug fixes
2023-01-09 13:52:37 -07:00
Jakub Konka
8d488246da Merge pull request #13463 from ziglang/fix-13056
macho: parse weak imports in tbd descriptors
2023-01-09 13:51:02 -07:00
Jakub Konka
2593d4d7d6 Merge pull request #13459 from ziglang/issue-13457
macho: do not zero-out file if there are no nonzerofill sects
2023-01-09 13:50:08 -07:00
Veikka Tuominen
b511231f95 Merge pull request #13338 from Vexu/stage2-compile-errors
Improve some error messages
2023-01-09 13:48:34 -07:00
Micah Switzer
8d95b713c5 Sema: resolve lazy align in zirReify for union fields
Closes #13435
2023-01-09 13:48:15 -07:00
Jacob Young
6165de9a61 Sema: make InferredErrorSet deterministic
Empirically, this `AutoHashMapUnmanaged` -> `AutoArrayHashMapUnmanaged`
change fixes all non-determinism in `ReleaseFast` build artifacts.

Closes #12183
2023-01-09 13:47:41 -07:00
Andrew Kelley
0848d33d78 Merge pull request #13430 from ziglang/stack-probe-msvc
Miscellaneous arm64 windows fixes
2023-01-09 13:47:09 -07:00