Skip to content

Commit

Permalink
FIX: changed completly the way __libc_start_main is called, now it sh…
Browse files Browse the repository at this point in the history
…ould conform much more to Linux ABI (only Linux supported so far).
  • Loading branch information
dockimbel committed Feb 25, 2012
1 parent dd4b7b7 commit c6edd0e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 79 deletions.
17 changes: 3 additions & 14 deletions red-system/compiler.r
Original file line number Diff line number Diff line change
Expand Up @@ -2095,19 +2095,6 @@ 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 @@ -2189,7 +2176,9 @@ system-dialect: context [
clean-up
loader/init

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

set-verbose-level opts/verbosity
Expand Down
13 changes: 0 additions & 13 deletions red-system/emitter.r
Original file line number Diff line number Diff line change
Expand Up @@ -561,19 +561,6 @@ 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: 0 additions & 52 deletions red-system/runtime/start.reds

This file was deleted.

57 changes: 57 additions & 0 deletions red-system/targets/IA-32.r
Original file line number Diff line number Diff line change
Expand Up @@ -1561,4 +1561,61 @@ 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 c6edd0e

Please sign in to comment.