13 label . : sizeof.name
\r
30 SCNHSZ = sizeof SCNHDR
\r
38 RELSZ = sizeof RELOC
\r
53 SYMESZ = sizeof SYMENT
\r
133 f_magic dw I386MAGIC
\r
134 f_nscns dw NUMBER_OF_SECTIONS
\r
135 f_timdat dd __TIME__
\r
136 f_symptr dd SYMBOL_TABLE_OFFSET
\r
137 f_nsyms dd NUMBER_OF_SYMBOLS
\r
139 f_flags dw F_AR32WR + F_LNNO
\r
141 Sections: db NUMBER_OF_SECTIONS * SCNHSZ dup 0
\r
144 symbol_table:: rb NUMBER_OF_SYMBOLS * SYMESZ
\r
148 string_table:: dd STRING_TABLE_SIZE
\r
149 STRING_POSITION = $
\r
150 rb STRING_TABLE_SIZE - $
\r
154 relocations:: rb NUMBER_OF_RELOCATIONS * RELSZ
\r
157 element relocatable?
\r
159 macro section_start
\r
161 element sym : relocatable * (1+SECTION_INDEX) + SYMBOL_INDEX
\r
164 if DEFINED_SECTION | DEFAULT_SECTION_PRESENT
\r
165 store SECTION_NAME : 8 at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_name
\r
166 store C_STAT at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_sclass
\r
167 store 1+SECTION_INDEX at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_scnum
\r
168 SYMBOL_INDEX = SYMBOL_INDEX + 1
\r
172 RELOCATION_INDEX = 0
\r
174 SECTION_RELOCATION_INDEX = RELOCATION_INDEX
\r
177 SECTION_OFFSET = $%
\r
179 SECTION_NAME = '.flat'
\r
180 SECTION_FLAGS = STYP_TEXT + STYP_DATA
\r
181 DEFINED_SECTION = 0
\r
182 DEFAULT_SECTION_PRESENT = 0
\r
190 SECTION_SIZE = $% - SECTION_OFFSET
\r
192 if DEFINED_SECTION | SECTION_SIZE > 0
\r
194 if ~ DEFINED_SECTION
\r
195 DEFAULT_SECTION_PRESENT = 1
\r
198 if $%% = SECTION_OFFSET
\r
199 SECTION_FLAGS = SECTION_FLAGS or STYP_BSS
\r
203 UNINITIALIZED_LENGTH = $% - $%%
\r
205 db UNINITIALIZED_LENGTH dup 0
\r
208 store SECTION_NAME : 8 at COFF:Sections + SECTION_INDEX * SCNHSZ + SCNHDR.s_name
\r
209 store SECTION_OFFSET at COFF:Sections + SECTION_INDEX * SCNHSZ + SCNHDR.s_scnptr
\r
210 store SECTION_SIZE at COFF:Sections + SECTION_INDEX * SCNHSZ + SCNHDR.s_size
\r
211 store SECTION_FLAGS at COFF:Sections + SECTION_INDEX * SCNHSZ + SCNHDR.s_flags
\r
213 if RELOCATION_INDEX > SECTION_RELOCATION_INDEX
\r
214 store RELOCATION_INDEX - SECTION_RELOCATION_INDEX at COFF:Sections + SECTION_INDEX * SCNHSZ + SCNHDR.s_nreloc
\r
215 store RELOCATIONS_OFFSET + SECTION_RELOCATION_INDEX * RELSZ at COFF:Sections + SECTION_INDEX * SCNHSZ + SCNHDR.s_relptr
\r
218 SECTION_INDEX = SECTION_INDEX + 1
\r
225 macro section? declaration*
\r
230 DEFINED_SECTION = 1
\r
232 SECTION_OFFSET = $%
\r
235 match name attributes, declaration
\r
237 SECTION_NAME = name
\r
240 define seq attributes
\r
252 irpv attribute, list
\r
253 match =code?, attribute
\r
254 SECTION_FLAGS = SECTION_FLAGS or STYP_TEXT
\r
255 else match =data?, attribute
\r
256 SECTION_FLAGS = SECTION_FLAGS or STYP_DATA
\r
258 err 'unknown attribute "',`attribute,'"'
\r
264 SECTION_NAME = declaration
\r
270 SECTION_RELOCATION_INDEX = RELOCATION_INDEX
\r
275 calminstruction align? boundary,value:?
\r
276 check COFF.SECTION_ALIGN mod (boundary) = 0
\r
278 arrange value, =err 'section not aligned enough'
\r
282 compute boundary, (boundary-1)-($-COFF.SECTION_BASE+boundary-1) mod boundary
\r
283 arrange value, =db boundary =dup value
\r
285 end calminstruction
\r
287 macro public? declaration*
\r
289 match =static? value =as? str, declaration
\r
290 SYMBOL_VALUE = value
\r
291 SYMBOL_NAME = string str
\r
292 SYMBOL_CLASS = C_STAT
\r
293 else match value =as? str, declaration
\r
294 SYMBOL_VALUE = value
\r
295 SYMBOL_NAME = string str
\r
296 SYMBOL_CLASS = C_EXT
\r
297 else match =static? value, declaration
\r
298 SYMBOL_VALUE = value
\r
299 SYMBOL_NAME = `value
\r
300 SYMBOL_CLASS = C_STAT
\r
302 SYMBOL_VALUE = declaration
\r
303 SYMBOL_NAME = `declaration
\r
304 SYMBOL_CLASS = C_EXT
\r
306 if SYMBOL_VALUE relativeto 1 elementof SYMBOL_VALUE & 1 elementof (1 metadataof SYMBOL_VALUE) relativeto relocatable & 1 scaleof (1 metadataof SYMBOL_VALUE) > 0
\r
307 SYMBOL_SECTION_INDEX = 1 scaleof (1 metadataof SYMBOL_VALUE)
\r
308 SYMBOL_VALUE = SYMBOL_VALUE - 1 elementof SYMBOL_VALUE
\r
310 SYMBOL_SECTION_INDEX = N_ABS
\r
312 if lengthof SYMBOL_NAME > 8
\r
313 store 0 at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_zeroes
\r
314 store STRING_POSITION at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_offset
\r
315 store SYMBOL_NAME : lengthof SYMBOL_NAME at string_table:STRING_POSITION
\r
316 STRING_POSITION = STRING_POSITION + lengthof SYMBOL_NAME + 1
\r
318 store SYMBOL_NAME : 8 at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_name
\r
320 store SYMBOL_VALUE at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_value
\r
321 store SYMBOL_SECTION_INDEX at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_scnum
\r
322 store SYMBOL_CLASS at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_sclass
\r
323 SYMBOL_INDEX = SYMBOL_INDEX + 1
\r
327 macro extrn? declaration*
\r
330 element sym : relocatable * (-1) + SYMBOL_INDEX
\r
331 match str =as? name:size, declaration
\r
332 label name:size at sym
\r
333 SYMBOL_NAME = string str
\r
334 else match name:size, declaration
\r
335 label name:size at sym
\r
336 SYMBOL_NAME = `name
\r
337 else match str =as? name, declaration
\r
339 SYMBOL_NAME = string str
\r
341 label declaration at sym
\r
342 SYMBOL_NAME = `declaration
\r
344 if lengthof SYMBOL_NAME > 8
\r
345 store 0 at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_zeroes
\r
346 store STRING_POSITION at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_offset
\r
347 store SYMBOL_NAME : lengthof SYMBOL_NAME at string_table:STRING_POSITION
\r
348 STRING_POSITION = STRING_POSITION + lengthof SYMBOL_NAME + 1
\r
350 store SYMBOL_NAME : 8 at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_name
\r
352 store C_EXT at symbol_table : SYMBOL_INDEX * SYMESZ + SYMENT.e_sclass
\r
353 SYMBOL_INDEX = SYMBOL_INDEX + 1
\r
357 calminstruction calminstruction?.init? var*, val:0
\r
360 end calminstruction
\r
362 calminstruction calminstruction?.initsym? var*, val&
\r
364 end calminstruction
\r
366 calminstruction calminstruction?.unique? name
\r
367 local counter, buffer
\r
369 compute counter, counter + 1
\r
370 arrange buffer, name#counter
\r
371 publish name, buffer
\r
372 end calminstruction
\r
374 calminstruction calminstruction?.asm? line&
\r
375 local tmp, ln, buffer
\r
376 initsym tmp, unique ln
\r
379 arrange buffer, =assemble ln
\r
381 end calminstruction
\r
383 calminstruction dword? value
\r
384 compute value, value
\r
385 check ~ value relativeto 0 & value relativeto 1 elementof value & 1 elementof (1 metadataof value) relativeto COFF.relocatable
\r
387 check ~ value relativeto 0 & (value + COFF.SECTION_BASE) relativeto 1 elementof (value + COFF.SECTION_BASE)
\r
389 check 1 elementof (1 metadataof (value + COFF.SECTION_BASE)) relativeto COFF.relocatable
\r
394 local offset, symndx, type
\r
396 compute symndx, 0 scaleof (1 metadataof value)
\r
397 compute type, RELOC_ADDR32
\r
398 jump add_relocation
\r
400 compute value, value + COFF.SECTION_BASE
\r
401 compute symndx, 0 scaleof (1 metadataof value)
\r
402 compute type, RELOC_REL32
\r
403 jump add_relocation
\r
406 asm emit 4: 0 scaleof value
\r
409 compute offset, offset - COFF.SECTION_OFFSET
\r
411 compute reloc, COFF.RELOCATION_INDEX * RELSZ
\r
412 asm store offset at COFF.relocations : reloc + RELOC.r_vaddr
\r
413 asm store symndx at COFF.relocations : reloc + RELOC.r_symndx
\r
414 asm store type at COFF.relocations : reloc + RELOC.r_type
\r
415 compute COFF.RELOCATION_INDEX, COFF.RELOCATION_INDEX + 1
\r
417 end calminstruction
\r
419 calminstruction dd? definitions&
\r
422 match value=,definitions, definitions, ()
\r
424 match value, definitions
\r
425 arrange definitions,
\r
427 match n =dup? value, value, ()
\r
431 arrange value, =dword value
\r
434 match , definitions
\r
437 take definitions, definitions
\r
441 arrange value, =dd ?
\r
445 match (value), value
\r
449 take definitions, value
\r
450 arrange value, definitions
\r
453 end calminstruction
\r
455 calminstruction (label) dd? definitions&
\r
457 arrange cmd, =label label : =dword
\r
459 arrange cmd, =dd definitions
\r
461 end calminstruction
\r
468 NUMBER_OF_SECTIONS := SECTION_INDEX
\r
469 STRING_TABLE_SIZE := STRING_POSITION
\r
470 NUMBER_OF_SYMBOLS := SYMBOL_INDEX
\r
471 NUMBER_OF_RELOCATIONS := RELOCATION_INDEX
\r
473 RELOCATIONS_OFFSET = $%
\r
474 load byte_sequence : NUMBER_OF_RELOCATIONS * RELSZ from relocations:0
\r
477 SYMBOL_TABLE_OFFSET = $%
\r
478 load byte_sequence : NUMBER_OF_SYMBOLS * SYMESZ from symbol_table:0
\r
481 load byte_sequence : STRING_TABLE_SIZE from string_table:0
\r