aboutsummaryrefslogtreecommitdiff
path: root/src/link/Elf.zig
AgeCommit message (Collapse)Author
2022-09-08[ld] add --print-* for diagnosticsMotiejus Jakštys
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-08-29link: add force_undefined_symbols to cache hashAndrew Kelley
Follow-up for d5233ee85ce13cba3dd03e4c0c938cee193b9b19.
2022-08-25add ability to pass force undefined symbols to the linkerJakub Konka
This commit enables `-u <symbol>` for ELF and `-include:<symbol>` for COFF linkers for use internally. This means we do not expose these flags to the users just yet, however, we make use of them internally whenever required. One such use case is forcing inclusion of `_tls_index` when linking for Windows with mingw and LTO and dead code stripping enabled. This ensures we add `_tls_index` to the symbol resolver as an undefined symbol and force the linker to include an atom that provides it marking it a dead-code-stripping root - meaning it will not be garbage collected by the linker no matter what.
2022-08-19make self-hosted the default compilerAndrew Kelley
stage1 is available behind the -fstage1 flag. closes #89
2022-08-19stage2: implement stack protectorsAndrew Kelley
This is one of the final remaining TODOs for the LLVM backend.
2022-08-18std.Target gains ObjectFormat fieldAndrew Kelley
2022-07-29ELF: Scan for dylibs-as-objects when adding rpathsCody Tapscott
Shared libraries can be provided on the command line as if they were objects, as a path to the ".so" file. The "each-lib-rpath" functionality was ignoring these shared libraries accidentally, causing missing rpaths in the output executable.
2022-07-11stage2: cleanups to --compress-debug-sectionsAndrew Kelley
* make the setting in the linker backend be non-optional; by this time all defaults are supposed to be resolved. * integrate with `zig cc` * change the CLI parsing to match C compiler parsing, allowing `--compress-debug-sections` alone to choose a default encoding of zlib.
2022-07-11ELF: understand -Wl,--compress-debug-sectionsMotiejus Jakštys
This argument is both a compiler and a linker flag. The linker flag was not understood; now it is. Go likes to use it as a linker flag. Tested with sqlite3. The size difference is significant, and I confirmed gdb understands both binaries. zlib: 3.66MB ------------ CC="zig cc" CFLAGS="-Wl,--compress-debug-sections=zlib -O2" ./configure --disable-tcl make FILE SIZE VM SIZE -------------- -------------- 39.1% 1.43Mi 88.4% 1.43Mi .text 19.6% 734Ki 0.0% 0 .debug_info 16.4% 613Ki 0.0% 0 .debug_loc 13.1% 492Ki 0.0% 0 .debug_line 4.2% 157Ki 9.5% 157Ki .rodata 2.3% 87.6Ki 0.0% 0 .debug_ranges 1.5% 56.2Ki 0.0% 0 .symtab 1.1% 40.2Ki 0.0% 0 .strtab 1.0% 38.2Ki 0.0% 0 .debug_str 0.7% 26.2Ki 0.0% 0 .debug_frame 0.4% 15.3Ki 0.9% 15.3Ki .data 0.1% 4.71Ki 0.3% 4.71Ki .dynsym 0.1% 3.65Ki 0.2% 3.26Ki [16 Others] 0.1% 2.55Ki 0.2% 2.55Ki .rela.plt 0.1% 2.12Ki 0.0% 0 [ELF Section Headers] 0.0% 0 0.1% 2.02Ki .bss 0.0% 1.84Ki 0.1% 1.84Ki .dynstr 0.0% 1.72Ki 0.1% 1.72Ki .plt 0.0% 1.58Ki 0.1% 1.58Ki .hash 0.0% 1.17Ki 0.0% 0 .debug_abbrev 0.0% 1.01Ki 0.1% 1.01Ki .rela.dyn 100.0% 3.66Mi 100.0% 1.62Mi TOTAL none: 8.56MB ------------ CC="zig cc" CFLAGS="-O2" ./configure --disable-tcl make FILE SIZE VM SIZE -------------- -------------- 41.1% 3.52Mi 0.0% 0 .debug_loc 18.5% 1.59Mi 0.0% 0 .debug_info 16.7% 1.43Mi 88.4% 1.43Mi .text 11.8% 1.01Mi 0.0% 0 .debug_line 5.9% 515Ki 0.0% 0 .debug_ranges 1.8% 157Ki 9.5% 157Ki .rodata 1.3% 118Ki 0.0% 0 .debug_frame 1.3% 110Ki 0.0% 0 .debug_str 0.6% 56.2Ki 0.0% 0 .symtab 0.5% 40.2Ki 0.0% 0 .strtab 0.2% 15.3Ki 0.9% 15.3Ki .data 0.1% 4.71Ki 0.3% 4.71Ki .dynsym 0.0% 3.64Ki 0.2% 3.26Ki [16 Others] 0.0% 2.98Ki 0.0% 0 .debug_abbrev 0.0% 2.55Ki 0.2% 2.55Ki .rela.plt 0.0% 2.12Ki 0.0% 0 [ELF Section Headers] 0.0% 0 0.1% 2.02Ki .bss 0.0% 1.84Ki 0.1% 1.84Ki .dynstr 0.0% 1.72Ki 0.1% 1.72Ki .plt 0.0% 1.58Ki 0.1% 1.58Ki .hash 0.0% 1.01Ki 0.1% 1.01Ki .rela.dyn 100.0% 8.56Mi 100.0% 1.62Mi TOTAL
2022-07-04stage2: ELF: improve error reporting when libc is missingAndrew Kelley
Future improvement: make plain error notes actually render as notes rather than errors, but keep them as errors for the case of sub-compilation errors, e.g. when compiler-rt has compilation errors.
2022-06-27macho: implement -dead_strip_dylibs linker flagJakub Konka
2022-06-25macho: implement -headerpad_max_install_namesJakub Konka
2022-06-25cache setting macho search strategy flagsJakub Konka
2022-06-20[MachO] add -pagezero_sizeMotiejus Jakštys
Pass `-pagezero_size` to the MachO linker. This is the final "unsupported linker arg" that I could chase that CGo uses. After this and #11874 we may be able to fail on an "unsupported linker arg" instead of emiting a warning. Test case: zig=/code/zig/build/zig CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC="$zig cc -target x86_64-macos" CXX="$zig c++ -target x86_64-macos" go build -a -ldflags "-s -w" cgo.go I compiled a trivial CGo program and executed it on an amd64 Darwin host. To be honest, I am not entirely sure what this is doing. This feels right after reading what this argument does in LLVM sources, but I am by no means qualified to make MachO pull requests. Will take feedback.
2022-06-17compiler_rt: use single cache for libcompiler_rt.a static libJakub Konka
2022-06-17compiler_rt: compile each unit separately for improved archivingJakub Konka
2022-06-09linker: remove `-z noexecstack` optionIsaac Freund
Note that the current documentation for the `-z noexecstack` is incorrect. This indicates that an object *does not* require an executable stack. This is actually the default of LLD, and there has never been a way to override this default by passing `-z execstack` to LLD. This commit removes the redundant `-z noexecstack` option from zig build-exe/build-lib/build-obj and ignores the option if passed to zig cc for compatibility. As far as I can tell, there is no reason for code to require an executable stack. This option only exists because the stack was originally executable by default and some programs came to depend on that behavior. Instead, mprotect(2) may be used to make memory pages executable.
2022-06-08linker: Enable full RELRO by defaultIsaac Freund
Full RELRO is a hardening feature that makes it impossible to perform certian attacks involving overwriting parts of the Global Offset Table to invoke arbitrary code. It requires all symbols to be resolved before execution of the program starts which may have an impact on startup time. However most if not all popular Linux distributions enable full RELRO by default for all binaries and this does not seem to make a noticeable difference in practice. "Partial RELRO" is equivalent to `-z relro -z lazy`. "Full RELRO" is equivalent to `-z relro -z now`. LLD defaults to `-z relro -z lazy`, which means Zig's current `-z relro` option has no effect on LLD's behavior. The changes made by this commit are as follows: - Document that `-z relro` is the default and add `-z norelro`. - Pass `-z now` to LLD by default to enable full RELRO by default. - Add `-z lazy` to disable passing `-z now`.
2022-06-07Merge pull request #11794 from ziglang/elf-macho-alignmentJakub Konka
elf+macho: use explicit alignment on Decl if specified
2022-06-06elf: refactor and enhance logging symtabJakub Konka
2022-06-05elf+macho: use explicit alignment on decl is specifiedJakub Konka
2022-05-31stage2: add support for -fbuild-id,-fno-build-idAndrew Kelley
closes #3047
2022-05-19[Elf] add -z nocopyrelocMotiejus Jakštys
Warnings about non-implemented `-z nocopyreloc` are common when compiling go code (including Go's tests themselves). Let's just make it stop complaining.
2022-05-13target: Rename sparcv9 -> sparc64Koakuma
Rename all references of sparcv9 to sparc64, to make Zig align more with other projects. Also, added new function to convert glibc arch name to Zig arch name, since it refers to the architecture as sparcv9. This is based on the suggestion by @kubkon in PR 11847. (https://github.com/ziglang/zig/pull/11487#pullrequestreview-963761757)
2022-04-29std: Do not allocate the result for ChildProcess.initJimmi Holst Christensen
Instead, just return ChildProcess directly. This structure does not require a stable address, so we can put it on the stack just fine. If someone wants it on the heap they should do. const proc = try allocator.create(ChildProcess); proc.* = ChildProcess.init(args, allocator);
2022-04-20stage2: use indexes for Decl objectsAndrew Kelley
Rather than allocating Decl objects with an Allocator, we instead allocate them with a SegmentedList. This provides four advantages: * Stable memory so that one thread can access a Decl object while another thread allocates additional Decl objects from this list. * It allows us to use u32 indexes to reference Decl objects rather than pointers, saving memory in Type, Value, and dependency sets. * Using integers to reference Decl objects rather than pointers makes serialization trivial. * It provides a unique integer to be used for anonymous symbol names, avoiding multi-threaded contention on an atomic counter.
2022-04-17stage2: expose progress bar API to linker backendsAndrew Kelley
This gives us insight as to what is happening when we are waiting for things such as LLVM emit object and LLD linking.
2022-04-14Merge pull request #11434 from koachan/sparc64-codegenJakub Konka
2022-04-14Merge pull request #11433 from ziglang/elf-fixesJakub Konka
elf: support `--strip` option and set symtab size when writing globals
2022-04-14stage2: progress towards stage3Andrew Kelley
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly doing element casting for slice element types. This required an enhancement both to stage1 and stage2. * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that abused this is fixed. * Make some parameters comptime to support functions in switch expressions (as opposed to making them function pointers). * Avoid relying on local temporaries being mutable. * Workarounds for when stage1 and stage2 disagree on function pointer types. * Workaround recursive formatting bug with a `@panic("TODO")`. * Remove unreachable `else` prongs for some inferred error sets. All in effort towards #89.
2022-04-14linker: ELF: Add page sizes for ppc64le and sparcv9Koakuma
2022-04-14elf: check symtab section headers size when writing globalsJakub Konka
Without this, it may happen we write the globals without extending the symtab section header's size. This can potentially lead to clobbering some data in the file, or simply omitting the globals from the symtab when displaying with support tooling such as `readelf`.
2022-04-14elf: support --strip optionJakub Konka
When the user passes `--strip` option on the command line, we do not emit any debug info by skipping initializing the internal `Dwarf` module.
2022-04-05zig fmt: remove trailing whitespace on doc commentsDamien Firmenich
Fixes #11353 The renderer treats comments and doc comments differently since doc comments are parsed into the Ast. This commit adds a check after getting the text for the doc comment and trims whitespace at the end before rendering. The `a = 0,` in the test is here to avoid a ParseError while parsing the test.
2022-04-04Pull elf magic string out to re-used constantTom Read Cutting
2022-03-30dwarf: pass DeclState around instead of storing a temp global in DwarfJakub Konka
Avoids many pitfalls connected with premature/early return in case there are errors with Decl, etc. This is effectively bringing back the old design however in a much nicer packaging, where every mechanism related to tracking Decl's debug info is now nicely wrapped in a single struct (aka the `DeclState`). This includes relocation table, type arena, etc. It is now the caller's responsibility to deinit the state (so that no memory is leaked) after `Decl` has been analysed (or errored out). The caller here is typically a linker such as `Elf` or `MachO`.
2022-03-29link: half-hearted bug fix for decl_state fieldAndrew Kelley
The init()/commit() API of this field leads to the type of bug that this commit fixes by defering an uncomfortably complex expression. I didn't bother doing the equivalent fix in link/MachO.zig because instead I think the `decl_state` field should be entirely removed from Dwarf.
2022-03-27dwarf: track type relocation state in Dwarf moduleJakub Konka
2022-03-27dwarf: move DbgInfoTypeRelocsTable into Dwarf moduleJakub Konka
2022-03-27dwarf: rename DebugInfoAtom into Atom; free all allocated memoryJakub Konka
2022-03-27dwarf: draft poc of deferred resolution of error sets debug infoJakub Konka
2022-03-17Revert "link.Elf: swap order of GOT and text program headers"Andrew Kelley
This reverts commit 8e7b1a74ac4568a2b806026f210ae59e52d2768d. Sorry, I should have put up a PR and ran that one by Jakub and done some more inspection. This causes problems with gdb: BFD: /home/andy/dev/zig/build-release/test: invalid string offset 3254779904 >= 153524 for section `.shstrtab'
2022-03-17link.Elf: swap order of GOT and text program headersAndrew Kelley
This makes Valgrind happier because it results in the program headers being sorted by VirtAddr in the ELF file.
2022-03-08dwarf: move all dwarf into standalone moduleJakub Konka
Hook up Elf and MachO linkers to the new solution.
2022-03-05elf: add debug info for non-ptr optionalsJakub Konka
2022-03-01codegen: lower field_ptr to memory across linking backendsJakub Konka
This requires generating an addend for the target relocation as the field pointer might point at a field inner to the container.
2022-02-24elf: use fully qualified decl names in the linkerJakub Konka
2022-02-22codegen: lower error_set and error_unionJakub Konka
2022-02-16link: avoid double close on openPath errorAndrew Kelley
2022-02-11elf: store pointer relocations indexed by containing atomJakub Konka
In `getDeclVAddr`, it may happen that the target `Decl` has not been allocated space in virtual memory. In this case, we store a relocation in the linker-global table which we will iterate over when flushing the module, and fill in any missing address in the final binary. Note that for optimisation, if the address was resolved at the time of a call to `getDeclVAddr`, we skip relocating this atom. This commit also adds the glue code for lowering const slices in the ARM backend.