Skip to content

Commit e72d378

Browse files
committed
malloc working in loader w/ init workaround
1 parent 514d7a9 commit e72d378

File tree

10 files changed

+72
-31
lines changed

10 files changed

+72
-31
lines changed

.clang-format

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ BinPackParameters: false
88
BinPackArguments: false
99
AllowAllParametersOfDeclarationOnNextLine: true
1010
PenaltyReturnTypeOnItsOwnLine: 1000000
11+
# ternary expressions
12+
AlignOperands: false

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ loader: src/loader/loader_main.c src/tiny_c/tiny_c.c
4949
-DARM32 \
5050
-o loader \
5151
src/loader/loader_main.c \
52+
src/loader/loader_lib.c \
5253
src/tiny_c/tinyc_sys.c \
5354
src/tiny_c/tiny_c.c \
54-
src/elf_tools.c
55+
src/loader/elf_tools.c
5556

5657
programs/linux/env:
5758
@$(CC) $(CFLAGS) -g \

src/elf_tools.c renamed to src/loader/elf_tools.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "elf_tools.h"
2-
#include "tiny_c/tiny_c.h"
2+
#include "../tiny_c/tiny_c.h"
3+
#include "loader_lib.h"
34

45
#define ELF_HEADER_LEN sizeof(ELF_HEADER)
56

@@ -12,7 +13,7 @@ const uint8_t ELF_MAGIC[] = {0x7f, 'E', 'L', 'F'};
1213
#define EXAMPLE2(fmt, ...) tiny_c_fprintf(STDERR, fmt, ##__VA_ARGS__)
1314

1415
bool get_elf_data(int fd, struct ElfData *elf_data) {
15-
ELF_HEADER *elf_header = tinyc_malloc(ELF_HEADER_LEN);
16+
ELF_HEADER *elf_header = loader_malloc_arena(ELF_HEADER_LEN);
1617
ssize_t header_read_len = tiny_c_read(fd, elf_header, ELF_HEADER_LEN);
1718
if (header_read_len != ELF_HEADER_LEN) {
1819
tiny_c_fprintf(STDERR, "read failed\n");
@@ -29,14 +30,14 @@ bool get_elf_data(int fd, struct ElfData *elf_data) {
2930
}
3031

3132
size_t program_headers_len = elf_header->e_phnum * elf_header->e_phentsize;
32-
PROGRAM_HEADER *program_headers = tinyc_malloc(program_headers_len);
33+
PROGRAM_HEADER *program_headers = loader_malloc_arena(program_headers_len);
3334
ssize_t ph_read_len = tiny_c_read(fd, program_headers, program_headers_len);
3435
if ((size_t)ph_read_len != program_headers_len) {
3536
BAIL("read failed\n")
3637
}
3738

3839
struct MemoryRegion *memory_regions =
39-
tinyc_malloc(sizeof(struct MemoryRegion) * elf_header->e_phnum);
40+
loader_malloc_arena(sizeof(struct MemoryRegion) * elf_header->e_phnum);
4041

4142
size_t j = 0;
4243
for (size_t i = 0; i < elf_header->e_phnum; i++) {
@@ -46,14 +47,13 @@ bool get_elf_data(int fd, struct ElfData *elf_data) {
4647
}
4748

4849
uint32_t file_offset = program_header->p_offset /
49-
program_header->p_align *
50-
program_header->p_align;
50+
program_header->p_align * program_header->p_align;
5151
uint32_t start = program_header->p_vaddr / program_header->p_align *
52-
program_header->p_align;
52+
program_header->p_align;
5353
uint32_t end = start +
54-
program_header->p_memsz / program_header->p_align *
55-
program_header->p_align +
56-
program_header->p_align;
54+
program_header->p_memsz / program_header->p_align *
55+
program_header->p_align +
56+
program_header->p_align;
5757

5858
memory_regions[j++] = (struct MemoryRegion){
5959
.start = start,
File renamed without changes.

src/loader/loader_lib.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "loader_lib.h"
2+
3+
#define LOADER_BUFFER_SIZE 1000
4+
5+
static uint8_t loader_buffer[LOADER_BUFFER_SIZE] = {0};
6+
static size_t loader_heap_end = LOADER_BUFFER_SIZE;
7+
static size_t loader_heap_index = 0;
8+
9+
void *loader_malloc_arena(size_t n) {
10+
if (loader_heap_index + n > loader_heap_end) {
11+
return NULL;
12+
}
13+
14+
void *address = (void *)(loader_buffer + loader_heap_index);
15+
loader_heap_index += n;
16+
17+
return address;
18+
}
19+
20+
void loader_free_arena(void) {
21+
loader_heap_index = 0;
22+
}

src/loader/loader_lib.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stdlib.h>
5+
6+
void *loader_malloc_arena(size_t n);
7+
8+
void loader_free_arena(void);

src/loader/loader_main.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "../elf_tools.h"
21
#include "../tiny_c/tiny_c.h"
2+
#include "elf_tools.h"
33
#include <fcntl.h>
44
#include <stdint.h>
55
#include <sys/mman.h>
@@ -117,8 +117,6 @@ int main(int32_t argc, char **argv) {
117117
return -1;
118118
}
119119

120-
// @todo: map data section
121-
122120
struct ElfData elf_data;
123121
if (!get_elf_data(fd, &elf_data)) {
124122
tiny_c_fprintf(STDERR, "error parsing elf data\n");
@@ -133,7 +131,6 @@ int main(int32_t argc, char **argv) {
133131
for (size_t i = 0; i < elf_data.memory_regions_len; i++) {
134132
struct MemoryRegion *memory_region = &elf_data.memory_regions[i];
135133
size_t memory_region_len = memory_region->end - memory_region->start;
136-
// @todo: mem region test?
137134
size_t prot_read = (memory_region->permissions & 4) >> 2;
138135
size_t prot_write = memory_region->permissions & 2;
139136
size_t prot_execute = (memory_region->permissions & 1) << 2;
@@ -167,6 +164,7 @@ int main(int32_t argc, char **argv) {
167164
tiny_c_fprintf(log_handle, "frame_pointer: %x\n", frame_pointer);
168165
tiny_c_fprintf(log_handle, "stack_start: %x\n", stack_start);
169166
tiny_c_fprintf(log_handle, "running program...\n");
167+
170168
run_asm(
171169
(size_t)inferior_frame_pointer,
172170
(size_t)stack_start,
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "../../../tiny_c/tiny_c.h"
2+
#include <stddef.h>
23

34
ARM32_START_FUNCTION
45

6+
extern size_t tinyc_heap_start;
7+
extern size_t tinyc_heap_end;
8+
extern size_t tinyc_heap_index;
9+
510
int main(void) {
611
int32_t pid = tiny_c_get_pid();
712
tiny_c_printf("pid: %x\n", pid);
@@ -10,18 +15,19 @@ int main(void) {
1015
const char *cwd = tiny_c_get_cwd(cwd_buffer, 100);
1116
tiny_c_printf("cwd: '%s'\n", cwd);
1217

13-
uint8_t *buffer = tinyc_malloc(128);
18+
const int MALLOC_SIZE = 0x1001;
19+
uint8_t *buffer = tinyc_malloc_arena(MALLOC_SIZE);
1420
tiny_c_printf("p: %x\n", buffer);
15-
tiny_c_printf("n: %x\n", *buffer);
16-
tinyc_free(buffer);
21+
tiny_c_printf("f: %x\n", *buffer);
22+
tiny_c_printf("l: %x\n", *(buffer + MALLOC_SIZE - 1));
1723

18-
buffer = tinyc_malloc(0x800);
24+
buffer = tinyc_malloc_arena(0x800);
1925
tiny_c_printf("p: %x\n", buffer);
2026
tiny_c_printf("n: %x\n", *buffer);
21-
tinyc_free(buffer);
2227

23-
buffer = tinyc_malloc(0x800);
28+
buffer = tinyc_malloc_arena(0x800);
2429
tiny_c_printf("p: %x\n", buffer);
2530
tiny_c_printf("n: %x\n", *buffer);
26-
tinyc_free(buffer);
31+
32+
tinyc_free_arena();
2733
}

src/tiny_c/tiny_c.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#include <sys/types.h>
1212

1313
int32_t tinyc_errno = 0;
14-
size_t tinyc_heap_start = 0;
14+
15+
// @todo: 0 initial value doesn't work
16+
size_t tinyc_heap_start = 42;
1517
size_t tinyc_heap_end = 0;
1618
size_t tinyc_heap_index = 0;
1719

@@ -384,25 +386,27 @@ const char *tinyc_strerror(int32_t err_number) {
384386
}
385387
}
386388

387-
void *tinyc_malloc(size_t n) {
388-
const int HEAP_CHUNK_LEN = 0x1000;
389+
void *tinyc_malloc_arena(size_t n) {
390+
const int PAGE_SIZE = 0x1000;
389391

390-
if (tinyc_heap_start == 0) {
392+
if (tinyc_heap_start == 42) {
391393
tinyc_heap_start = tinyc_sys_brk(0);
392394
tinyc_heap_end = tinyc_heap_start;
393395
tinyc_heap_index = tinyc_heap_start;
394396
}
395397
if (tinyc_heap_index + n > tinyc_heap_end) {
396-
tinyc_heap_end = tinyc_sys_brk((size_t)tinyc_heap_end + HEAP_CHUNK_LEN);
398+
size_t extend_size = PAGE_SIZE * (n / PAGE_SIZE) + PAGE_SIZE;
399+
tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + extend_size);
397400
}
398401

399402
void *address = (void *)tinyc_heap_index;
400403
tinyc_heap_index += n;
404+
401405
return address;
402406
}
403407

404-
void tinyc_free(void *_) {
405-
// @todo: free
408+
void tinyc_free_arena(void) {
409+
tinyc_heap_index = tinyc_heap_start;
406410
}
407411

408412
#ifdef ARM32

src/tiny_c/tiny_c.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ int32_t tiny_c_strcmp(const void *__s1, const void *__s2);
3333
int32_t tiny_c_get_pid(void);
3434
char *tiny_c_get_cwd(char *buffer, size_t size);
3535
const char *tinyc_strerror(int32_t err_number);
36-
void *tinyc_malloc(size_t n);
37-
void tinyc_free(void *_);
36+
void *tinyc_malloc_arena(size_t n);
37+
void tinyc_free_arena(void);
3838

3939
#ifdef ARM32
4040

0 commit comments

Comments
 (0)