Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / os / sun / saveelf.fth
CommitLineData
920dae64
AT
1\ ========== Copyright Header Begin ==========================================
2\
3\ Hypervisor Software File: saveelf.fth
4\
5\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6\
7\ - Do no alter or remove copyright notices
8\
9\ - Redistribution and use of this software in source and binary forms, with
10\ or without modification, are permitted provided that the following
11\ conditions are met:
12\
13\ - Redistribution of source code must retain the above copyright notice,
14\ this list of conditions and the following disclaimer.
15\
16\ - Redistribution in binary form must reproduce the above copyright notice,
17\ this list of conditions and the following disclaimer in the
18\ documentation and/or other materials provided with the distribution.
19\
20\ Neither the name of Sun Microsystems, Inc. or the names of contributors
21\ may be used to endorse or promote products derived from this software
22\ without specific prior written permission.
23\
24\ This software is provided "AS IS," without a warranty of any kind.
25\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
26\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
27\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
28\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
29\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
30\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
31\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
32\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
33\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
34\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
35\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36\
37\ You acknowledge that this software is not designed, licensed or
38\ intended for use in the design, construction, operation or maintenance of
39\ any nuclear facility.
40\
41\ ========== Copyright Header End ============================================
42\ saveelf.fth 1.4 95/04/19
43\ Copyright 1985-1991 Bradley Forthware
44
45only forth also definitions
46headerless
47
48only forth also hidden also forth definitions
49
50\ Save an image of the target system in the Unix file whose name
51\ is the argument on the stack.
52
53: text-base ( -- ) origin ;
54: text-size ( -- ) here text-base - 7 + 7 invert and ;
55
56\ \ Adds "text-size" to all the symbols in the data segment, because the
57\ \ linker expects data segment addresses to start at the end of the text
58\ \ segment.
59\ : adjust-data-symbols ( -- )
60\ symbol# @ 1 ?do
61\ i /sym* symbol-table + dup sym_type c@ ( adr symbol-type )
62\ h# 1e and 6 = if
63\ sym_value text-size swap +!
64\ else
65\ drop
66\ then
67\ loop
68\ ;
69
70\ This is a hack way to create a "common" symbol to cause BSS space to
71\ be allocated at the end of the image. This saves PROM space by not
72\ requiring unitialized parts of the user area image to be stored in the
73\ data segment. The following line creates a very simple symbol table
74\ entry with just one entry "_foo", of type "common", whose size will be
75\ set later when it is determined (the size field is at offset 8).
76\ The array "symbol" will later be copied into a file called "userbss.o"
77\ The bytes are as follows (see /usr/include/nlist.h):
78\ 0 - offset into string table for this symbol's name entry
79\ 4 - symbol type (1000000 == "common")
80\ 8 - symbol value (set later to the bss size)
81\ (String table begins at offset c)
82\ c - string table size (including padding)
83\ 10 - The string "_foo", null terminated
84\ 15 - padding to align to a longword boundary
85
86: ,cstring ( adr len -- )
87 here over allot swap move 0 c,
88;
89
90hex
91
920 value default-elf-header
93
940 value section-headers
950 value /section-headers
960 value section-names
970 value /section-names
98
99: >section ( sec# -- adr ) /elf32-sheader * section-headers + ;
100: !name ( sec# -- ) here section-names - swap >section st32_name l! ;
101
102: lalign ( -- ) here /l round-up here - allot ; \ longword align
103
104lalign
105here is default-elf-header
106
107 7f c, ascii E c, ascii L c, ascii F c, \ 0-3 Magic number
108 1 c, 2 c, 1 c, \ 4,5,6 32-bit, big-endian, V1
109 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, \ 7-15 Reserved
110 1 w, 2 w, 1 l, \ 16,18,20 relocatable,SPARC,V1
111 0 l, 0 l, \ 24,28 no entry, no phdr
112 /elf32-header l, 0 l, \ 32,36 shdr offset, no flags
113 /elf32-header w, \ 40 ehdr size
114 0 w, 0 w, \ 42,44 no phdr, no phdr
115 /elf32-sheader w, \ 46 shdr size
116 9 w, \ 48 #sections
117 1 w, \ 50 section name table sec#
118
119here is section-headers
120\ nam type flags addr offs size link info align entsize
121
122 0 l, 0 l, 0 l, 0 l, 0 l, 0 l, 0 l, 0 l, 0 l, 0 l, \ 0 Null
123
124 0 l, 3 l, 0 l, 0 l, 0 l, 0 l, 0 l, 0 l, 1 l, 0 l, \ 1 shnames
125
126 0 l, 1 l, 6 l, 0 l, 0 l, 0 l, 0 l, 0 l, 4 l, 0 l, \ 2 text
127
128 0 l, 1 l, 3 l, 0 l, 0 l, 0 l, 0 l, 0 l, 4 l, 0 l, \ 3 data
129
130 0 l, 8 l, 3 l, 0 l, 0 l, 0 l, 8 l, 0 l, 4 l, 0 l, \ 4 bss
131
132 0 l, 4 l, 0 l, 0 l, 0 l, 0 l, 7 l, 2 l, 4 l, c l, \ 5 rela.text
133
134 0 l, 4 l, 0 l, 0 l, 0 l, 0 l, 7 l, 3 l, 4 l, c l, \ 6 rela.data
135
136 0 l, 2 l, 2 l, 0 l, 0 l, 0 l, 8 l, 3 l, 4 l, 10 l, \ 7 syms
137
138 0 l, 3 l, 2 l, 0 l, 0 l, 0 l, 0 l, 0 l, 1 l, 0 l, \ 8 strtab
139
140here section-headers - is /section-headers
141
142here is section-names
143
1440 !name " " ,cstring \ Required null entry
1451 !name " .shstrtab" ,cstring
1462 !name " .text" ,cstring
1473 !name " .data" ,cstring
1484 !name " .bss" ,cstring
1495 !name " .rela.text" ,cstring
1506 !name " .rela.data" ,cstring
1517 !name " .symtab" ,cstring
1528 !name " .strtab" ,cstring
153here section-names - is /section-names
154 " " ,cstring \ Terminating entry
155
156\ : /headers ( -- n ) /elf32-header /section-headers + /section-names + ;
157
158: !loc ( offset size sec# -- offset' )
159 2dup >section >sh32_size l!
160
161 \ SHT_NOBITS occupies no space in the file
162 >r r@ >section >sh32_type l@ 8 = if drop 0 then ( offset size )
163
164 bounds r> >section >sh32_offset l! ( offset' )
165;
166
167headers
168: save-obj ( str -- )
169 terminate-string-table
170
171\ #align p" _userbss" external-common user-size #user @ -
172\ add-sized-symbol
173
174 new-file
175
176 /elf32-header /section-headers + ( first-offset )
177
178 /section-names 1 !loc \ Section name table size
179 text-size 2 !loc \ Text size
180 #user @ 3 !loc \ Data size
181 user-size #user @ - 4 !loc \ BSS size
182 /relocation-table 5 !loc \ Text reloc. table size
183 0 6 !loc \ Data reloc. table size
184 /symbol-table 7 !loc \ Symbol table size
185 /string-table 8 !loc \ String table size
186 drop
187
188 " stand-init-io" $find-name is init-io
189 " stand-init" init-save
190
191 default-elf-header /elf32-header ofd @ fputs \ ELF header
192 section-headers /section-headers ofd @ fputs \ Section headers
193 section-names /section-names ofd @ fputs \ Section name table
194
195 text-base text-size ofd @ fputs
196 up@ #user @ ofd @ fputs
197
198 relocation-table /relocation-table ofd @ fputs
199 symbol-table /symbol-table ofd @ fputs
200 string-table /string-table ofd @ fputs
201
202 ofd @ fclose
203
204;
205
206only forth also definitions