From 3b27f053aee06fa46d558cda48ab0b7e95021cf3 Mon Sep 17 00:00:00 2001 From: "Sergey Semyonov (Serge)" Date: Sun, 7 Aug 2011 13:02:30 +0000 Subject: [PATCH] newlib: fix stack allocation git-svn-id: svn://kolibrios.org@2040 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/develop/libraries/newlib/Makefile | 2 + .../develop/libraries/newlib/crt/chkstk.S | 4 +- .../develop/libraries/newlib/crt/crt_amz.S | 10 ++- programs/develop/libraries/newlib/dll.lds | 2 - programs/develop/libraries/newlib/pe/loader.c | 81 ++++++++++++++++++- programs/develop/libraries/newlib/static.lds | 2 +- 6 files changed, 94 insertions(+), 7 deletions(-) diff --git a/programs/develop/libraries/newlib/Makefile b/programs/develop/libraries/newlib/Makefile index 99037a380d..566e50dcd5 100644 --- a/programs/develop/libraries/newlib/Makefile +++ b/programs/develop/libraries/newlib/Makefile @@ -18,6 +18,7 @@ AMZ_SRCS:= \ crt/chkstk.S \ crt/exit.S \ crt/pseudo-reloc.c \ + crt/dllstart.c \ crt/setjmp.S STATIC_SRCS:= \ @@ -204,6 +205,7 @@ STDIO_SRCS= \ fdopen.c \ fflush.c \ flags.c \ + fileno.c \ findfp.c \ fiprintf.c \ fiscanf.c \ diff --git a/programs/develop/libraries/newlib/crt/chkstk.S b/programs/develop/libraries/newlib/crt/chkstk.S index 1b9cc33878..405dfc6eb2 100644 --- a/programs/develop/libraries/newlib/crt/chkstk.S +++ b/programs/develop/libraries/newlib/crt/chkstk.S @@ -4,8 +4,8 @@ .section .text -.def ___chkstk; .scl 2; .type 32; .endef -.def __alloca; .scl 2; .type 32; .endef +#.def ___chkstk; .scl 2; .type 32; .endef +#.def __alloca; .scl 2; .type 32; .endef ___chkstk: __alloca: pushl %ecx /* save temp */ diff --git a/programs/develop/libraries/newlib/crt/crt_amz.S b/programs/develop/libraries/newlib/crt/crt_amz.S index 72bbfb79b0..2d317a3add 100644 --- a/programs/develop/libraries/newlib/crt/crt_amz.S +++ b/programs/develop/libraries/newlib/crt/crt_amz.S @@ -1,11 +1,19 @@ -.section .text .global __start .global ___main +.global _DllMainCRTStartup + + +.section .init + +.def __start; .scl 2; .type 32; .endef +.def _DllMainCRTStartup; .scl 2; .type 32; .endef .align 4 __start: +_DllMainCRTStartup: + call __pei386_runtime_relocator jmp _main diff --git a/programs/develop/libraries/newlib/dll.lds b/programs/develop/libraries/newlib/dll.lds index 1c44270fca..632d03f5ab 100644 --- a/programs/develop/libraries/newlib/dll.lds +++ b/programs/develop/libraries/newlib/dll.lds @@ -1,8 +1,6 @@ OUTPUT_FORMAT(pei-i386) -ENTRY("__start") - SECTIONS { . = SIZEOF_HEADERS; diff --git a/programs/develop/libraries/newlib/pe/loader.c b/programs/develop/libraries/newlib/pe/loader.c index cd30cd4484..23a0fa9a0d 100644 --- a/programs/develop/libraries/newlib/pe/loader.c +++ b/programs/develop/libraries/newlib/pe/loader.c @@ -561,7 +561,75 @@ void* get_entry_point(void *raw) nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); return MakePtr(void*, raw, nt->OptionalHeader.AddressOfEntryPoint); -} +}; + + +void *get_proc_address(module_t *module, char *proc_name) +{ + + PIMAGE_DOS_HEADER expdos; + PIMAGE_NT_HEADERS32 expnt; + PIMAGE_EXPORT_DIRECTORY exp; + + uint32_t *exp_functions; + uint16_t *exp_ordinals; + char **exp_names; + + int minn, maxn; + char *export_name; + uint16_t ordinal; + void *function=NULL; + + exp = module->img_exp; + + exp_functions = MakePtr(uint32_t*,exp->AddressOfFunctions,module->start); + exp_ordinals = MakePtr(uint16_t*, exp->AddressOfNameOrdinals,module->start); + exp_names = MakePtr(char**, exp->AddressOfNames,module->start); + + minn = 0; + maxn = exp->NumberOfNames - 1; + while (minn <= maxn) + { + int mid; + int res; + + mid = (minn + maxn) / 2; + + export_name = MakePtr(char*,exp_names[mid],module->start); + + res = strcmp(export_name, proc_name); + if (res == 0) + { + ordinal = exp_ordinals[mid]; + function = MakePtr(void*,exp_functions[ordinal], module->start); + + if((uint32_t)function >= (uint32_t)exp) + { + printf("forward %s\n", function); + } + else + { + DBG(" \t\tat %x\n", function); + }; + break; + } + else if (minn == maxn) + { + DBG(" unresolved %s\n",proc_name); + break; + } + else if (res > 0) + { + maxn = mid - 1; + } + else + { + minn = mid + 1; + } + }; + + return function; +}; module_t* load_module(const char *name) @@ -642,7 +710,18 @@ module_t* load_module(const char *name) list_add_tail(&module->list, &dll_list); if( link_image(img_base)) + { + int (*dll_startup)(module_t *mod, uint32_t reason); + + dll_startup = get_proc_address(module, "DllStartup"); + if( dll_startup ) + { + if( 0 == dll_startup(module, 1)) + return 0; + } return module; + }; + return NULL; }; diff --git a/programs/develop/libraries/newlib/static.lds b/programs/develop/libraries/newlib/static.lds index 60df13981a..b6d4a43e00 100644 --- a/programs/develop/libraries/newlib/static.lds +++ b/programs/develop/libraries/newlib/static.lds @@ -90,7 +90,7 @@ SECTIONS . = . + 256; ___pgmname = .; ___menuet__app_path_area = .; - . = . + 1024 + 16; + . = . + 1024 + __size_of_stack_reserve__; ___stacktop = .; ___memsize = . ; }