-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.c
65 lines (49 loc) · 1.51 KB
/
link.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "link.h"
#include "alloc.h"
#include "log.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
enum link_result link_objects(const struct link_args *args) {
struct arena_allocator *arena;
arena_allocator_create(&arena);
// FIXME: support non `ld_classic` - requires arch and platform_version
// -arch arm64 -platform_version macos 14.0.0 14.4"
const char *template =
"ld -lSystem -lc -dynamic -syslibroot $(xcrun -sdk macosx "
"--show-sdk-path) -ld_classic";
// super inefficient here
size_t template_size = strlen(template);
size_t total_size = template_size;
total_size++;
for (size_t i = 0; i < args->num_objects; i++) {
// seperator
total_size++;
total_size += strlen(args->objects[i]);
}
total_size += /* "-o " */ 3 + strlen(args->output);
total_size++; // null terminator
char *buff = arena_alloc(arena, total_size);
size_t head = 0;
strcpy(&buff[head], template);
head += template_size;
buff[head++] = ' ';
for (size_t i = 0; i < args->num_objects; i++) {
strcpy(&buff[head], args->objects[i]);
head += strlen(args->objects[i]);
buff[head++] = ' ';
}
strcpy(&buff[head], "-o ");
head += 3;
strcpy(&buff[head], args->output);
head += strlen(args->output);
buff[head++] = '\0';
DEBUG_ASSERT(head == total_size, "string buffer calculations went wrong!");
int ret_code = system(buff);
arena_allocator_free(&arena);
if (!ret_code) {
return LINK_RESULT_SUCCESS;
} else {
return LINK_RESULT_FAILURE;
}
}