Skip to content

Commit

Permalink
Revert "FIX: changed completly the way __libc_start_main is called, n…
Browse files Browse the repository at this point in the history
…ow it should conform much more to Linux ABI (only Linux supported so far)."

This reverts commit c4edd0e7a2f927ab08c9a812b5ef7bb43376b675.
  • Loading branch information
dockimbel committed Feb 25, 2012
1 parent c6edd0e commit cdaf058
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 60 deletions.
17 changes: 14 additions & 3 deletions red-system/compiler.r
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,19 @@ system-dialect: context [
]
]

comp-start: has [script][
emitter/start-prolog
script: secure-clean-path runtime-path/start.reds
compiler/run/no-events job loader/process script script
emitter/start-epilog

;-- selective clean-up of compiler's internals
remove/part find compiler/globals 'system 2 ;-- avoid 'system redefinition clash
remove/part find emitter/symbols 'system 4
clear compiler/definitions
clear compiler/aliased-types
]

comp-runtime-prolog: has [script][
script: secure-clean-path runtime-path/common.reds
compiler/run/runtime job loader/process script script
Expand Down Expand Up @@ -2176,9 +2189,7 @@ system-dialect: context [
clean-up
loader/init

unless opts/use-natives? [
emitter/target/emit-libc-init job ;-- init libC properly
]
unless opts/use-natives? [comp-start] ;-- init libC properly
if opts/runtime? [comp-runtime-prolog]

set-verbose-level opts/verbosity
Expand Down
13 changes: 13 additions & 0 deletions red-system/emitter.r
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,19 @@ emitter: context [
]
]

start-prolog: does [ ;-- libc init prolog
append compiler/functions [ ;-- create a fake function to
***-start [0 native cdecl []] ;-- let the linker write the entry point
]
append symbols [
***-start [native 0 []]
]
]

start-epilog: does [ ;-- libc init epilog
poke second find/last symbols '***-start 2 tail-ptr - 1 ;-- save the "main" entry point
]

init: func [link? [logic!] job [object!]][
if link? [
clear code-buf
Expand Down
52 changes: 52 additions & 0 deletions red-system/runtime/start.reds
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Red/System [
Title: "Red/System OS-independent runtime"
Author: "Nenad Rakocevic"
File: %start.reds
Rights: "Copyright (C) 2011 Nenad Rakocevic. All rights reserved."
License: {
Distributed under the Boost Software License, Version 1.0.
See https://github.com/dockimbel/Red/blob/master/red-system/runtime/BSL-License.txt
}
]

#include %lib-names.reds

__stack!: alias struct! [
top [pointer! [integer!]]
]

system: declare struct! [ ;-- trimmed down temporary system definition
stack [__stack!] ;-- stack virtual access
]

#switch OS [
Linux [
#import [LIBC-file cdecl [
libc-start: "__libc_start_main" [
main [function! []]
argc [integer!]
argv [pointer! [integer!]]
init [function! []]
finish [function! []]
loader-finish [function! []]
stack-end [pointer! [integer!]]
]
; _init: "__libc_csu_init" []
; _finish: "__libc_csu_fini" []
]]

***__ptr: system/stack/top
***__argc: ***__ptr/value
***__argv: ***__ptr + 1
until [ ;-- pass all arguments
***__ptr: ***__ptr + 1
***__ptr/value = 0
]
until [ ;-- pass all environment variables
***__ptr: ***__ptr + 1
***__ptr/value = 0
] ;-- ptr should point to stack top now

libc-start :***-start ***__argc ***__argv null null null ***__ptr
]
]
57 changes: 0 additions & 57 deletions red-system/targets/IA-32.r
Original file line number Diff line number Diff line change
Expand Up @@ -1561,61 +1561,4 @@ make target-class [
]
]
]

emit-libc-init: func [job [object!] /local base refs main-call][
if verbose >= 3 [print "^/>>>emitting LIBC-INIT"]

base: emitter/tail-ptr

switch job/OS [
Linux [
compiler/process-import [
"libc.so.6" cdecl [
__libc_start_main: "__libc_start_main" []
;__libc_csu_init: "__libc_csu_init" []
;__libc_csu_fini: "__libc_csu_fini" []
]
]
foreach opcode [
#{31ED} ;-- XOR ebp,ebp
#{5E} ;-- POP esi
#{89E1} ;-- MOV ecx, esp
#{83E4F0} ;-- AND esp, FFFFFFF0h
#{50} ;-- PUSH eax
#{54} ;-- PUSH esp
#{52} ;-- PUSH edx
#{6A00} ;-- PUSH null ; <init>
#{6A00} ;-- PUSH null ; <fini>
#{51} ;-- PUSH ecx
#{56} ;-- PUSH esi
#{68} main ;-- PUSH <main>
#{FF15} start ;-- CALL FAR <libc-start>
#{F4} ;-- HLT ; dragons!
][
either binary? opcode [
emit opcode
][
refs: switch opcode [
;init [emitter/symbols/__libc_csu_init]
;fini [emitter/symbols/__libc_csu_fini]
start [emitter/symbols/__libc_start_main]
main [main-call: emitter/tail-ptr none]
]
if refs [append refs/3 emitter/tail-ptr]
emit void-ptr
]
]
append emitter/symbols compose/deep [
***-start [native-ref (emitter/tail-ptr - 1) [(main-call)]]
]
]
Syllable [

]
MacOSX [

]

]
]
]

0 comments on commit cdaf058

Please sign in to comment.