Skip to content

Commit 5524212

Browse files
committed
add bss init in executable
1 parent f26f3ae commit 5524212

File tree

7 files changed

+60
-7
lines changed

7 files changed

+60
-7
lines changed

.clangd

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ CompileFlags:
1313
- -Wno-gnu-zero-variadic-macro-arguments
1414
- -Wno-gnu-statement-expression-from-macro-expansion
1515
- -Wvla
16+
- -Wno-format-pedantic

.vscode/tasks.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
{
1010
"label": "build_unit_test",
1111
"type": "shell",
12-
"command": "make clean && CFLAGS='-DVERBOSE' make programs/linux/unit_test"
12+
"dependsOn": ["build"],
13+
"command": "programs/linux/unit_test"
14+
},
15+
{
16+
"label": "run",
17+
"type": "shell",
18+
"dependsOn": ["build"],
19+
"command": "./winloader ./windynamic.exe"
1320
}
1421
]
1522
}

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ WARNINGS = \
1212
-Werror=return-type \
1313
-Werror=incompatible-pointer-types \
1414
-Wno-gnu-empty-initializer \
15-
-Wvla
15+
-Wvla \
16+
-Wno-format-pedantic
1617

1718
all: \
1819
libtinyc.a \

src/dlls/msvcrt.c

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ static void fprintf_internal(
180180
print(file_handle, data);
181181
break;
182182
}
183+
case 'p':
183184
case 'x': {
184185
size_t data = va_arg(var_args, size_t);
185186
print_number_hex(file_handle, data);

src/loader/win_loader_main.c

+32-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
CREATE_LIST_STRUCT(WinRuntimeObject)
1414

1515
// @todo: hard-coding this may cause random program failures due to ASLR etc.
16+
// @todo: need smarter way of setting up IAT regions that don't conflict b/w
17+
// exes & libs
1618
#define IAT_BASE_START 0x7d7e0000
19+
#define IAT_INCREMENT 0x10000
1720

1821
struct WinRuntimeObject runtime_exe;
1922
struct RuntimeObject *lib_ntdll;
@@ -403,7 +406,7 @@ static bool initialize_lib_ntdll(struct RuntimeObject *lib_ntdll_object) {
403406

404407
tiny_c_close(ntdll_file);
405408

406-
// @todo: computed not initialized
409+
// @todo: bss, computed not initialized?
407410
uint8_t *bss = 0;
408411
size_t bss_len = 0;
409412
const struct SectionHeader *bss_section_header = find_section_header(
@@ -579,8 +582,21 @@ static bool initialize_dynamic_data(
579582
&iat_runtime_base
580583
);
581584
size_t iat_runtime_offset = current_iat_offset;
582-
current_iat_base += 0x1000;
583-
current_iat_offset += 0x1000;
585+
current_iat_base += IAT_INCREMENT;
586+
current_iat_offset += IAT_INCREMENT;
587+
588+
/* Init .bss */
589+
590+
// @todo: lib bss
591+
592+
// const struct WinSectionHeader *bss_header = find_win_section_header(
593+
// pe_exe.section_headers, pe_exe.section_headers_len, ".bss"
594+
// );
595+
// if (bss_header != NULL) {
596+
// uint8_t *bss_region =
597+
// (uint8_t *)(image_base + bss_header->base_address);
598+
// memset(bss_region, 0, bss_header->virtual_size);
599+
// }
584600

585601
struct WinRuntimeObject shared_lib = {
586602
.name = dir_entry->lib_name,
@@ -669,8 +685,8 @@ int main(int argc, char **argv) {
669685
(size_t)dynamic_callback_windows,
670686
&iat_runtime_base
671687
);
672-
current_iat_base += 0x1000;
673-
current_iat_offset += 0x1000;
688+
current_iat_base += IAT_INCREMENT;
689+
current_iat_offset += IAT_INCREMENT;
674690

675691
/* Load libntdll.so */
676692

@@ -679,6 +695,17 @@ int main(int argc, char **argv) {
679695
EXIT("initialize_lib_ntdll failed\n");
680696
}
681697

698+
/* Init .bss */
699+
700+
const struct WinSectionHeader *bss_header = find_win_section_header(
701+
pe_exe.section_headers, pe_exe.section_headers_len, ".bss"
702+
);
703+
if (bss_header != NULL) {
704+
uint8_t *bss_region =
705+
(uint8_t *)(image_base + bss_header->base_address);
706+
memset(bss_region, 0, bss_header->virtual_size);
707+
}
708+
682709
/* Load dlls */
683710

684711
shared_libraries = (WinRuntimeObjectList){

src/programs/windows/win_dynamic/win_dynamic_main.c

+12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
#include "../../../dlls/msvcrt.h"
22
#include "../../../dlls/tinyc_stdio.h"
3+
#include <stdint.h>
34
#include <stdlib.h>
45

6+
int32_t exe_global_var_bss = 0;
7+
int32_t exe_global_var_data = 42;
8+
59
int start_inferior() {
610
int32_t num1 = (int32_t)pow(2, 4);
711
int32_t num2 = (int32_t)pow(2, 4);
812
printf("%d + %d = %d\n", num1, num2, num1 + num2);
913

14+
printf("exe_global_var_bss: %d\n", exe_global_var_bss);
15+
exe_global_var_bss = 1;
16+
printf("exe_global_var_bss: %d\n", exe_global_var_bss);
17+
18+
printf("exe_global_var_data: %d\n", exe_global_var_data);
19+
exe_global_var_data = 24;
20+
printf("exe_global_var_data: %d\n", exe_global_var_data);
21+
1022
return 0;
1123
}
1224

test_winloader.sh

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ assert() {
1111
# Dynamic Test
1212
expected=`cat <<EOF
1313
16 + 16 = 32
14+
exe_global_var_bss: 0
15+
exe_global_var_bss: 1
16+
exe_global_var_data: 42
17+
exe_global_var_data: 24
1418
EOF
1519
`
1620
assert [ "`./winloader ./windynamic.exe`" == "$expected" ]

0 commit comments

Comments
 (0)