Skip to content

Commit 08f267f

Browse files
authored
Merge pull request #62 from sysprog21/startup
Cut dynamic-linker startup syscalls
2 parents 0d0e6d1 + 9aa54a3 commit 08f267f

10 files changed

Lines changed: 724 additions & 15 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ SRCS := \
6666
debug/gdbstub.c \
6767
debug/gdbstub-reg.c \
6868
debug/gdbstub-rsp.c \
69-
debug/log.c
69+
debug/log.c \
70+
debug/syscall-hist.c
7071

7172
SRCS := $(addprefix src/,$(SRCS))
7273
OBJS := $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(SRCS))

src/core/elf.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,16 @@ int elf_map_segments(const elf_info_t *info,
348348
return -1;
349349
}
350350

351-
/* Zero the full page-aligned segment extent (zero_len computed above
352-
* with guest_size and infra_reserve checks). Linux guarantees
353-
* zero-filled tail bytes in the last mapped page, and some dynamic
354-
* linkers allocate from that page tail before they request more
355-
* memory. Leaving stale bytes there leaks state across execve and
356-
* corrupts the new image.
351+
/* Zero only the tail beyond filesz: the BSS portion [filesz, memsz)
352+
* plus the page-padding [memsz, zero_len) that Linux guarantees clean
353+
* for dynamic linkers allocating from the last mapped page's tail.
354+
* Skipping the file-data range avoids writing zeros that the fread
355+
* below would immediately overwrite; for typical shared libraries that
356+
* is a hundreds-of-KiB win per segment.
357357
*/
358-
memset((uint8_t *) guest_base + gpa, 0, zero_len);
358+
if (zero_len > filesz)
359+
memset((uint8_t *) guest_base + gpa + filesz, 0, zero_len - filesz);
359360

360-
/* Overlay initialized bytes after zeroing so BSS and page tail remain
361-
* zero-filled.
362-
*/
363361
if (filesz > 0) {
364362
if (fseek(f, (long) ph->p_offset, SEEK_SET) != 0) {
365363
log_error("%s: seek failed for segment at 0x%llx", path,

src/core/startup-trace.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
* static inline so each translation unit can use them without pulling in a
1010
* separate object; the getenv check resolves once per translation unit but
1111
* the resolution itself is idempotent.
12+
*
13+
* Accepted env values:
14+
* unset, "", "0" -> all tracing off
15+
* "1", "steps" -> per-step VM bring-up timings (this header)
16+
* "syscalls" -> per-syscall histogram (debug/syscall-hist.c)
17+
* "all" -> both, comma-separated tokens also accepted
18+
* "1" is preserved as a legacy alias for "steps" so old scripts keep
19+
* working. The histogram mode never enables the step tracer and vice
20+
* versa, so a user can ask for one without paying for the other.
1221
*/
1322

1423
#ifndef ELFUSE_STARTUP_TRACE_H
@@ -30,10 +39,37 @@
3039
static pthread_once_t startup_trace_once = PTHREAD_ONCE_INIT;
3140
static bool startup_trace_value;
3241

42+
static inline bool startup_trace_env_has(const char *env, const char *tok)
43+
{
44+
if (!env || !env[0])
45+
return false;
46+
size_t toklen = strlen(tok);
47+
const char *p = env;
48+
while (*p) {
49+
const char *comma = strchr(p, ',');
50+
size_t len = comma ? (size_t) (comma - p) : strlen(p);
51+
if (len == toklen && memcmp(p, tok, toklen) == 0)
52+
return true;
53+
if (!comma)
54+
break;
55+
p = comma + 1;
56+
}
57+
return false;
58+
}
59+
3360
static inline void startup_trace_resolve(void)
3461
{
3562
const char *v = getenv("ELFUSE_STARTUP_TRACE");
36-
startup_trace_value = v && v[0] && strcmp(v, "0") != 0;
63+
if (!v || !v[0] || !strcmp(v, "0"))
64+
return;
65+
/* The legacy "1" knob enables steps. Recognize it both as the whole
66+
* value and as a token so compound forms like "1,syscalls" still
67+
* keep the step trace on alongside the histogram, instead of
68+
* silently dropping it.
69+
*/
70+
if (!strcmp(v, "1") || startup_trace_env_has(v, "1") ||
71+
startup_trace_env_has(v, "steps") || startup_trace_env_has(v, "all"))
72+
startup_trace_value = true;
3773
}
3874

3975
static inline bool startup_trace_enabled(void)

0 commit comments

Comments
 (0)