1 ;; Native x86 GNU/Linux Forth System, Direct Threaded Code
3 ;; Copyright (C) 2020 Ketmar Dark // Invisible Vector
5 ;; This program is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, version 3 of the License ONLY.
9 ;; This program is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;; GNU General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;; save current image to ELF executable file
20 ;; we're using the fact that our ELF header is loaded into memory, and is writeable
21 ;; metacompiler will prolly need to rebuild it from scratch, but for now... meh
22 urword_forth
"(SAVE)",par_save
24 ;; ( fd -- successflag )
25 ;; just in case, check fd
30 ;; move fd to return stack
32 ;; fix code segment size
33 ;; write code from code start to real HERE
34 UF real_here par_code_base_addr
- dup par_code_base_addr
0x84 + !
35 UF drop
;; do not fix virtual size
36 ;; par_code_base_addr 0x88 + !
37 ;; everything in our header is ok now, including entry point (it isn't changed)
38 ;; write everything up until import table
39 UF par_code_base_addr elfhead_size rpeek par_fwrite
44 ;; write zero bytes for imports: this is where import addresses will be put by ld.so
46 UF real_here par_code_imports_size erase
47 UF real_here par_code_imports_size rpeek par_fwrite
48 UF par_code_imports_size equal
52 ;; setup some variables first, so the new image will process CLI args
53 ;; don't bother moving old values to "safe place", data stack is good enough
59 ;; write code from imports start to real here
60 UF par_code_imports_end_addr real_here par_code_imports_end_addr
- rpeek par_fwrite
61 ;; restore variables, just in case (TOS is write result)
62 UF rpush
;; move write result to the safe place
65 UF rpop
;; restore write result
69 ;; you may not believe me, but we're done!
70 UF rdrop
;; got rid of fd
71 UF
1 ;; exit with success
75 urword_forth
"SAVE",save
76 ;; ( addr count -- successflag )
78 UF o_wronly o_creat
or o_trunc
or ;; flags
79 UF s_irwxu s_irgrp
or s_ixgrp
or s_iroth
or s_ixoth
or ;; mode
88 UF swap par_fclose
0equal