Skip to content

Commit 4ceacde

Browse files
committed
feat: embed wrappers into ELF binaries
1 parent 50f7135 commit 4ceacde

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+6536
-313
lines changed

packages/std/Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/std/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pedantic = { level = "warn", priority = -1 }
2626
result_large_err = "allow"
2727

2828
[workspace.dependencies]
29+
bytes = { version = "1", features = ["serde"] }
2930
clap = { version = "4", features = ["derive"] }
3031
fnv = "1"
3132
futures = "0.3"
@@ -35,6 +36,7 @@ libc = "0.2"
3536
serde = { version = "1", features = ["derive"] }
3637
serde_json = "1"
3738
tangram_client = { default-features = false, git = "https://github.com/tangramdotdev/tangram", rev = "58527c57de3217c82d0c54e3dacaba6394245fec" }
39+
tangram_serialize = { default-features = false, git = "https://github.com/tangramdotdev/tangram", rev = "58527c57de3217c82d0c54e3dacaba6394245fec" }
3840
tempfile = "3"
3941
tokio = { version = "1", default-features = false, features = [
4042
"rt",
@@ -77,12 +79,16 @@ path = "packages/std/lib.rs"
7779
workspace = true
7880

7981
[dependencies]
82+
bytes = { workspace = true }
8083
futures = { workspace = true }
8184
serde = { workspace = true }
8285
serde_json = { workspace = true }
8386
tangram_client = { workspace = true }
87+
tangram_serialize = { workspace = true }
88+
tempfile = { workspace = true }
8489
tracing = { workspace = true, optional = true }
8590
tracing-subscriber = { workspace = true, optional = true }
8691

8792
[features]
8893
tracing = ["dep:tracing", "dep:tracing-subscriber"]
94+
default = ["tracing"]

packages/std/a.out

69 KB
Binary file not shown.

packages/std/bootstrap/make.tg.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ export const source = () => {
2323

2424
export type Arg = {
2525
host?: string;
26+
embedWrapper?: boolean | undefined;
2627
};
2728

2829
export const build = async (arg?: Arg) => {
2930
const host = arg?.host ?? (await std.triple.host());
31+
const embedWrapper = arg?.embedWrapper ?? true;
3032

3133
const configure = {
3234
args: ["--disable-dependency-tracking"],
@@ -48,7 +50,14 @@ export const build = async (arg?: Arg) => {
4850
install,
4951
};
5052

51-
const env = std.env.arg(sdk(host), { utils: false });
53+
let envArgs: Array<tg.Unresolved<std.env.Arg>> = [
54+
sdk(host),
55+
{ utils: false },
56+
];
57+
if (embedWrapper) {
58+
envArgs.push({ TANGRAM_LINKER_EMBED_WRAPPER: true });
59+
}
60+
const env = std.env.arg(...envArgs);
5261

5362
const output = await autotoolsInternal({
5463
bootstrap: true,

packages/std/cctest/a.out

65.5 KB
Binary file not shown.

packages/std/cctest/main.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <elf.h>
2+
#include <stdio.h>
3+
#include <stdbool.h>
4+
#include <sys/mman.h>
5+
6+
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
7+
8+
int main(int argc, const char** argv) {
9+
if (argc != 3) {
10+
return 111;
11+
}
12+
const char* input = argv[0];
13+
const char* output = argv[1];
14+
int status = 0;
15+
16+
int in = open(input, O_RDONLY);
17+
int out = open(output, O_RDWR | O_CREAT, 0o755);
18+
if (in < 0 || out < 0) {
19+
perror("failed to open in/out/files");
20+
return 1;
21+
}
22+
off_t size = lseek(in, 0, SEEK_END);
23+
if (size < 0) {
24+
perror("failed to seek to the end of the input");
25+
status = 1;
26+
goto cleanup;
27+
}
28+
29+
uint8_t* elf = (uint8_t*)mmap(
30+
NULL,
31+
(size_t)size,
32+
PROT_READ,
33+
MAP_ANONYMOUS | MAP_PRIVATE,
34+
in,
35+
0
36+
);
37+
38+
Elf64_Ehdr* ehdr = (Elf64_Ehdr*)elf;
39+
Elf64_Phdr* phdr = (Elf64_Phr*)(elf + ehdr->e_phoff);
40+
41+
bool copied = false
42+
for (int i = 0; i < ehdr->e_phnum) {
43+
if (phdr[i].p_type != PT_LOAD) {
44+
continue;
45+
}
46+
if (copied) {
47+
status = 1;
48+
eprintf("expected a single loadable segment");
49+
goto cleanup;
50+
}
51+
52+
}
53+
cleanup:
54+
munmap(elf, size);
55+
close(in);
56+
close(out);
57+
return status;
58+
}

packages/std/cctest/output.o

832 Bytes
Binary file not shown.

packages/std/cctest/script.ld

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SECTIONS
2+
{
3+
. = 0x0;
4+
.text : {
5+
*(.text.start)
6+
}
7+
}

packages/std/cctest/start.bin

16 Bytes
Binary file not shown.

packages/std/cctest/start.elf

64.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)