6567 Commits

Author SHA1 Message Date
Jakub Konka
159e55dfd9 macho: fix alignment of atoms which begin the section 2021-09-07 20:37:58 +02:00
Jakub Konka
a2279e7e20 macho: fix commands.zig tests 2021-09-07 12:23:26 +02:00
Jakub Konka
c35f668932 macho: update max section alignment when inserting atoms 2021-09-07 10:07:09 +02:00
Jakub Konka
6836cc473c macho: make sure that parsed bss atoms are zero-filled 2021-09-06 18:30:40 +02:00
Jakub Konka
81e5320973 macho: set and reset file offset to zerofill sections in flush
This way, there's no need to special the set/reset behaviour at
every stage of updating sections/atoms.
2021-09-06 17:43:24 +02:00
Jakub Konka
29d2e19c3e macho: allocate sections one after the other and grow if needed 2021-09-06 17:31:20 +02:00
Jakub Konka
2914ea9e33 macho: require atom padding for machine code only 2021-09-06 11:17:20 +02:00
Jakub Konka
5e64d9745b macho: fix noninclusion of data-in-code
Also, calculate non-extern, section offset based addends for SIGNED
and UNSIGNED relocations on x86_64 upfront as an offset wrt to the
target symbol representing position of the section/atom within the
final artifact.
2021-09-06 10:38:51 +02:00
Jakub Konka
61dca19107 macho: encaps logic for extract sect offset into fn 2021-09-05 23:30:06 +02:00
Jakub Konka
ea8808f87b macho: add logic for segment expansion 2021-09-05 11:15:48 +02:00
Jakub Konka
d92b5416e8 macho: add a small routine to update section ordinals at the end
The actual ordinals may change when adding new sections to the segments.
2021-09-05 00:19:45 +02:00
Jakub Konka
7e87f93e06 macho: unfortunately, LINKEDIT commands NEED to be in order
Otherwise, Apple's tooling goes mental and reports that the executable
is malformed/fails strict validation. We absolutely have to get it
right to support tools such `codesign` which are required to
successfully launch an app on an iOS device for instance. When Zig
matures enough so that we can ditch any Apple tooling and still be
able to successfully codesign for iOS and other, we can revisit this
area. Until then however, we are stuck in having to rewrite the LINKEDIT
segment at every update run of the self-hosted.

FYI, the strict layout for the MachO binary apparently is (please,
read this with a pinch of salt as this is inferred by me):
* __TEXT segment
* __DATA_CONST segment
* __DATA segment
* __LINKEDIT segment
* dyld info (rebase, bind, weak bind, lazy bind, export)
* symbol table
* dynamic symbol table
* string table
* code signature (if expected)
2021-09-04 22:59:16 +02:00
Jakub Konka
80e1c244b6 macho: dyld info subsections need to follow in strict order
MachO, why are doing this to me?
2021-09-03 17:12:39 +02:00
Jakub Konka
1d2199b71c macho: dirty export trie when adding globals
Similarly, dirty rebase info when adding a GOT atom.
2021-09-03 13:41:28 +02:00
Jakub Konka
7536a2f8cf macho: minor fixes to allow the linker to output malformed stage1 2021-09-03 11:47:58 +02:00
Jakub Konka
a783f3a369 macho: remove obsolete mentions of stage1 2021-09-03 00:25:47 +02:00
Jakub Konka
4b07da7090 macho: remove all Zld codepaths 2021-09-03 00:21:45 +02:00
Jakub Konka
5af13f35f9 macho: implement basic section movement and reallocation 2021-09-02 18:19:07 +02:00
Jakub Konka
4741c04254 macho: better spec for sections which don't require padding 2021-09-02 00:44:33 +02:00
Jakub Konka
9dbad2d188 macho: fix calculating file offset for atom when allocating 2021-09-01 23:44:01 +02:00
Jakub Konka
17066cc12e macho: minor refactor of const namings 2021-09-01 23:26:24 +02:00
Jakub Konka
4eff0f4ea1 macho: fix condition for checking available size for an atom 2021-09-01 16:49:16 +02:00
Jakub Konka
7a99cd069a macho: clean up allocating atom logic
Instead of checking for stage1 at every callsite, move the logic
inside `allocateAtom`. This is fine since this logic will disappear
anyhow once I add expanding and shifting segments and sections.
2021-09-01 12:14:29 +02:00
Jakub Konka
d0dc622638 macho: do not reset section's size after allocating atom 2021-09-01 00:06:43 +02:00
Jakub Konka
50db993119 macho: fix allocating sections within segment when parsing objects 2021-08-31 23:05:01 +02:00
Jakub Konka
2831d6e9b8 macho: add first pass at allocating parsed atoms in objects
This commit makes it possible to combine self-hosted with a pre-compiled
C object file, e.g.:

```
zig-out/bin/zig build-exe hello.zig add.o
```

where `add.o` is a pre-compiled C object file.
2021-08-30 15:43:20 +02:00
Jakub Konka
a14e98fcac macho: remove sorting sections and refactor atom parsing in objects 2021-08-27 20:32:11 +02:00
Jakub Konka
ad4a8e7665 macho: keep actual file offset for zerofill sections separately
This way, we will conform to the standard practice of setting the
offset within the section header to the beginning of the file and
we will be able to track the location of the section in the file
for incremental updates.
2021-08-27 13:47:43 +02:00
Jakub Konka
1e65d41a65 macho: merge __common with __bss section 2021-08-27 11:30:02 +02:00
Jakub Konka
705cd64080 macho: preactively add zerofill sections in correct order
The required order on macOS nowadays is:
* __data
* __thread_vars
* __thread_data
* __thread_bss
* __bss
2021-08-27 10:45:56 +02:00
Jakub Konka
8bd0687713 macho: port mechanism for allocating symbols within atoms
such as entire sections represented as a single atom.
2021-08-26 16:16:34 +02:00
Jakub Konka
29df0ca39d Merge remote-tracking branch 'origin/master' into zld-incr 2021-08-26 14:35:34 +02:00
Jakub Konka
570c75cb74 macho: write all atoms in flush so that we can resolve relocs 2021-08-26 14:23:28 +02:00
Jakub Konka
432fb7054d macho: fix stub writing in self-hosted setting 2021-08-26 13:45:28 +02:00
Jakub Konka
d19d3342c2 macho: save lazy binding info as part of the atom 2021-08-26 11:43:11 +02:00
Jakub Konka
c12183b608 macho: fix text atom allocation 2021-08-25 23:00:58 +02:00
Jakub Konka
ee786e5c3c macho: add GOT entries as actual atoms 2021-08-25 17:23:30 +02:00
Jakub Konka
af57ccbe27 macho: generalise free list usage to all sections 2021-08-25 15:11:21 +02:00
Jakub Konka
ea4bd2b879 macho: add routine for creating Got atoms 2021-08-25 10:51:09 +02:00
Andrew Kelley
9c95f38a7c stage1: remove incorrect compile error for var redeclaration
Locals are not allowed to shadow declarations, but declarations are
allowed to shadow each other, as long as there are no ambiguous
references.

closes #678
2021-08-24 22:35:37 -07:00
Jakub Konka
aee6f14bce macho: use existing rebase mechanism to rebase lazy pointers 2021-08-24 20:16:34 +02:00
Jakub Konka
32ce8238a8 macho: rewrite populateLazyBindOffsetsInStubHelper to use atoms
Instead of referencing stub indices since these can now be obtained
in a more generic fashion from the actual linked-list of atoms in
the __stub_helper section.
2021-08-24 15:19:28 +02:00
Takeshi Yoneda
5c79370e9c
wasm: pass --export-dynamic to wasm-ld for WASI reactors. (#9605)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-08-24 14:03:50 +02:00
Jakub Konka
8d30092704 macho: port stub and lazy ptr atoms to stage2 2021-08-24 13:16:43 +02:00
Frank Denis
9e3ec98937
Don't define valgrind_support on macOS (#9612)
Unfortunately, Valgrind for macOS has been broken for years,
and the Homebrew formula is only for Linux.
2021-08-23 20:06:02 -04:00
Andrew Kelley
bb38931c71 stage1: @intToEnum implicitly does an @intCast
This is a backwards-compatible language change.

Previously, `@intToEnum` coerced its integer operand to the integer tag
type of the destination enum type, often requiring the callsite to
additionally wrap the operand in an `@intCast`. Now, the `@intCast` is
implicit, and any integer operand can be passed to `@intToEnum`.

The same as before, it is illegal behavior to pass any integer which does
not have a corresponding enum tag.
2021-08-23 15:30:57 -07:00
Jakub Konka
91c0552cfc macho: add routine for creating stubs in __stubs section 2021-08-24 00:03:51 +02:00
Jakub Konka
876071b50b macho: fix writing stubs (on x86_64 only) and lazy ptrs 2021-08-23 23:23:00 +02:00
Jakub Konka
9e7b2fb894 macho: add routine for creating lazy pointer for stub 2021-08-23 23:01:14 +02:00
Jakub Konka
799c5bb955 macho: add routine for creating a dynamic stub_helper atom
With this routine, we are now able to freely shift stub_helper
section in memory and in file since the VM addressing is now dynamically
dependent on the positioning of `__stub_helper` preamble and other
sections generated by the linker.
2021-08-23 22:12:04 +02:00