89 Commits

Author SHA1 Message Date
Jakub Konka
ace9b3de64 macho: fix parsing target string when linking against tbds 2021-08-10 13:41:07 +02:00
Jakub Konka
bf25650974 macho: refactor management of section ordinals
Instead of storing a two-way relation (seg,sect) <=> ordinal
we get the latter with `getIndex((seg, sect))`.
2021-08-02 19:49:32 +02:00
Jakub Konka
0b15ba8334 macho: don't allocate Dylib on the heap
instead, immediately transfer ownership to MachO struct. Also, revert
back to try-ok-fail parsing approach of objects, archives, and dylibs.
It seems easier to try and fail than check if the file *is* of a
certain type given that a dylib may be a stub and parsing yaml
twice in a row seems very wasteful.

Hint for the future: if we optimise yaml/TAPI parsing, this approach
may be rethought!
2021-08-01 09:06:56 +02:00
Jakub Konka
f023cdad7c macho: don't allocate Archives on the heap
instead, transfer ownership directly to MachO struct.
2021-08-01 09:06:56 +02:00
Jakub Konka
06396ddd7d macho: don't allocate Objects on the heap
instead, ownership is transferred to MachO. This makes Object
management align closer with data-oriented design.
2021-08-01 09:06:56 +02:00
Jakub Konka
c30cc4dbbf macho: don't store allocator in Object
instead, pass it in functions that require it. Also, when parsing
relocs, make Object part of the context struct where we pass in
additional goodies such as `*MachO` or `*Allocator`.
2021-08-01 09:06:56 +02:00
Jakub Konka
1beda818e1 macho: re-enable parsing sections into atoms
However, make it default only when building in release modes since
it's a prelude to advanced dead code stripping not very useful in
debug.
2021-07-23 16:55:19 +02:00
Jakub Konka
a4feb97cdf macho: assign and cache section ordinals upon creation
then, when sorting sections within segments, clear and redo the
ordinals since we re-apply them to symbols anyway. It is vital
to have the ordinals consistent with parsing and resolving relocs
however.
2021-07-22 23:13:13 +02:00
Jakub Konka
4fd0cb7618 macho: sort nlists within object before filtering by type
Previously, we'd filter the nlists assuming they were correctly
ordered by type: local < extern defined < undefined within the
object's symbol table but this doesn't seem to be guaranteed,
therefore, we sort by type and address in one go, and filter
defined from undefined afterwards.
2021-07-22 16:02:31 +02:00
Jakub Konka
ca90efe88e macho: fix memory leaks when emptying TextBlocks
This happens on every call to `TextBlock.empty` by the `Module`.
2021-07-22 14:05:12 +02:00
Jakub Konka
3bfde76cff macho: fix text block management
For the time being, until we rewrite how atoms are handled across
linkers, store two tables in the MachO linker: one for TextBlocks
directly created and managed by the linker, and one for TextBlocks
that were spawned by Module.Decl. This allows for correct memory
clean up after linking is done.
2021-07-21 15:46:57 +02:00
Jakub Konka
a442b165f1 macho: add stub relocs when adding extern fn
in self-hosted.
2021-07-20 20:33:07 +02:00
Jakub Konka
f6d13e9d6f zld: move contents of Zld into MachO module 2021-07-18 17:48:00 +02:00
Jakub Konka
9f20a51555 zld: demote logging back to debug from warn 2021-07-17 18:33:47 +02:00
Jakub Konka
71384a383e zld: correctly set n_sect for sections as symbols 2021-07-17 11:29:40 +02:00
Jakub Konka
407745a5e9 zld: simplify and move Relocations into TextBlock
It makes sense to have them as a dependent type since they only ever
deal with TextBlocks. Simplify Relocations to rely on symbol indices
and symbol resolver rather than pointers.
2021-07-17 01:03:40 +02:00
Jakub Konka
54a403d4ff zld: replace parsed reloc with a simple wrapper around macho.relocation_info 2021-07-16 17:18:53 +02:00
Jakub Konka
5a2bea2931 zld: draft symbol resolver on macho.nlist_64 only 2021-07-16 13:02:02 +02:00
Jakub Konka
f519e781c6 zld: move TextBlock into standalone file
which should make managing the logic of parsing and resolving relocs
that much simpler to parse.
2021-07-15 18:49:48 +02:00
Jakub Konka
f8678c48ff zld: reuse string table for symbol names
rather than manage allocs separately per symbol.
2021-07-15 18:49:47 +02:00
Jakub Konka
398672eb30 zld: add temp basic handling of debugging stabs 2021-07-15 18:49:47 +02:00
Jakub Konka
e17f12dd64 zld: fix incorrectly worked out section size
Also, add a solution to a degenerate case where on x86_64 a relocation
refers to a cell in a section via section start address even though
a symbol exists. In such case, make the section spawned symbol an alias
of the actual symbol.
2021-07-15 18:49:47 +02:00
Jakub Konka
de30a704b1 zld: map [section addr, first symbol) to a tracked TextBlock
which applies exclusively to x86_64-macos.
2021-07-15 18:49:47 +02:00
Jakub Konka
496903c6a8 zld: add DICE support mainly for x86_64-macos 2021-07-15 18:49:47 +02:00
Jakub Konka
b8fce705ad zld: refactor nlist and reloc filtering logic 2021-07-15 18:49:47 +02:00
Jakub Konka
570660bb46 zld: ___dso_handle is regular at 0x100000000
which points at the start of the __TEXT segment.

Also, ensure C++ initializers and terminators are rebased.
2021-07-15 18:49:47 +02:00
Jakub Konka
9e051e365b zld: correctly estimate TextBlock's alignment with
section's alignment serving as the maximum alignment that
can be seen in this particular section. However, TextBlocks are
still allowed to have at most that alignment.
2021-07-15 18:49:47 +02:00
Jakub Konka
95aeb09b9b zld: populate sections from the top rather than from bottom 2021-07-15 18:49:47 +02:00
Jakub Konka
3bdb3b574e zld: turn logging off 2021-07-15 18:49:47 +02:00
Jakub Konka
12187586d1 zld: fix alloc alignment and resolve relocs 2021-07-15 18:49:47 +02:00
Jakub Konka
961b463fad zld: track symbols defined within TextBlock
in case TextBlock represents an entire section with symbols
defined within.
2021-07-15 18:49:47 +02:00
Jakub Konka
7aeedc0912 zld: allocate TextBlocks
temporarily by iterating over all defined TextBlocks. However,
once we merge this with MachO incremental, updates will be done
at the point of creation and/or update.

Also, fix mining TLV knowledge for working out TLV pointers.
2021-07-15 18:49:47 +02:00
Jakub Konka
e524f43a6f zld: save rebase and TLV offset as part of TextBlock
instead of as part of the Symbol. This seems to be more
optimal way of handling dyld ops in presence of no splittable
input sections in object files.
2021-07-15 18:49:47 +02:00
Jakub Konka
7c662db8d9 zld: keep text blocks per segment,section pair 2021-07-15 18:49:47 +02:00
Jakub Konka
a04bc1ed14 zld: update relocs and start prepping for segment allocs 2021-07-15 18:49:47 +02:00
Jakub Konka
dfa69e3c30 zld: dealloc TextBlock if omitted 2021-07-15 18:49:47 +02:00
Jakub Konka
555b66c255 zld: move should_rebase logic into Symbol 2021-07-15 18:49:47 +02:00
Jakub Konka
dbd2eb7c7f zld: simplify relocation parsing 2021-07-15 18:49:47 +02:00
Jakub Konka
15b85df3dd zld: parse relocs per generated TextBlock 2021-07-15 18:49:47 +02:00
Jakub Konka
54888c6f46 zld: create TextBlocks for tentative definitions
and fix the links in the `TextBlock`s linked list!
2021-07-15 18:49:47 +02:00
Jakub Konka
51e334af44 zld: refactor section into TextBlocks conversion 2021-07-15 18:49:47 +02:00
Jakub Konka
7b4063d558 zld: convert section in linked list of TextBlocks 2021-07-15 18:49:47 +02:00
Jakub Konka
5649242025 zld: draft up final format of TextBlock 2021-07-15 18:49:47 +02:00
Jakub Konka
5b3c4691e6 zld: put relocs in a TextBlock 2021-07-15 18:49:47 +02:00
Jakub Konka
453c16d8ac zld: draft out splitting sections into blocks 2021-07-15 18:49:47 +02:00
Jakub Konka
989639efba zld: coalesce symbols on creation 2021-07-15 18:49:47 +02:00
Jakub Konka
980f2915fa zld: use index to symbol in reloc
instead of pointer to the Symbol struct in the hope that we
can overwrite the Symbol in the object's symbol table with the
resolved Symbol later down the line.
2021-07-15 18:49:46 +02:00
Jakub Konka
ee6e25bc13 zld: add Symbol.Stab and move nlist creation logic there 2021-07-15 18:49:46 +02:00
Jakub Konka
2b3bda43e3 zld: abstract Symbol creation logic 2021-07-15 18:49:46 +02:00
Jakub Konka
49b3986417 zld: fix section mapping for Go specific sections
which include:
* `__TEXT,__rodata` => `__DATA_CONST,__const`
* `__TEXT,__typelink` => `__DATA_CONST,__const`
* `__TEXT,__itablink` => `__DATA_CONST,__const`
* `__TEXT,__gosymtab` => `__DATA_CONST,__const`
* `__TEXT,__gopclntab` => `__DATA_CONST,__const`

Also, we treat section as containing machine code and mapping
it to `__TEXT,__text` if it is `S_REGULAR` and contains either
`S_ATTR_PURE_INSTRUCTIONS` or `S_ATTR_SOME_INSTRUCTIONS` or both.
2021-06-25 20:32:58 +02:00