1 /* outrdf.c output routines for the Netwide Assembler to produce
2 * RDOFF format object files (which are intended mainly
3 * for use in proprietary projects, as the code to load and
4 * execute them is very simple). They will also be used
5 * for device drivers and possibly some executable files
6 * in the MOSCOW operating system. See Rdoff.txt for
9 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
10 * Julian Hall. All rights reserved. The software is
11 * redistributable under the licence given in the file "Licence"
12 * distributed in the NASM archive.
27 typedef short int16
; /* not sure if this will be required to be altered
28 at all... best to typedef it just in case */
30 static const char *RDOFFId
= "RDOFF1"; /* written to start of RDOFF files */
32 /* the records that can be found in the RDOFF header */
34 /* Note that whenever a segment is referred to in the RDOFF file, its number
35 * is always half of the segment number that NASM uses to refer to it; this
36 * is because NASM only allocates even numbered segments, so as to not
37 * waste any of the 16 bits of segment number written to the file - this
38 * allows up to 65533 external labels to be defined; otherwise it would be
42 char type
; /* must be 1 */
43 char segment
; /* only 0 for code, or 1 for data supported,
44 * but add 64 for relative refs (ie do not require
45 * reloc @ loadtime, only linkage) */
46 long offset
; /* from start of segment in which reference is loc'd */
47 char length
; /* 1 2 or 4 bytes */
48 int16 refseg
; /* segment to which reference refers to */
52 char type
; /* must be 2 */
53 int16 segment
; /* segment number allocated to the label for reloc
54 * records - label is assumed to be at offset zero
55 * in this segment, so linker must fix up with offset
56 * of segment and of offset within segment */
57 char label
[33]; /* zero terminated... should be written to file until
58 * the zero, but not after it - max len = 32 chars */
62 char type
; /* must be 3 */
63 char segment
; /* segment referred to (0/1) */
64 long offset
; /* offset within segment */
65 char label
[33]; /* zero terminated as above. max len = 32 chars */
69 char type
; /* must be 4 */
70 char libname
[128]; /* name of library to link with at load time */
74 char type
; /* must be 5 */
75 long amount
; /* number of bytes BSS to reserve */
78 /* code for managing buffers needed to seperate code and data into individual
79 * sections until they are ready to be written to the file.
80 * We'd better hope that it all fits in memory else we're buggered... */
82 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
83 * on 80x86 machines for efficiency */
85 typedef struct memorybuffer
{
87 char buffer
[BUF_BLOCK_LEN
];
88 struct memorybuffer
*next
;
91 static memorybuffer
* newmembuf(){
94 t
= nasm_malloc(sizeof(memorybuffer
));
101 static void membufwrite(memorybuffer
*b
, void *data
, int bytes
) {
105 if (b
->next
) { /* memory buffer full - use next buffer */
106 membufwrite(b
->next
,data
,bytes
);
109 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
110 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
112 /* buffer full and no next allocated... allocate and initialise next
115 b
->next
= newmembuf();
116 membufwrite(b
->next
,data
,bytes
);
121 case -4: /* convert to little-endian */
122 l
= * (long *) data
;
123 b
->buffer
[b
->length
++] = l
& 0xFF;
125 b
->buffer
[b
->length
++] = l
& 0xFF;
127 b
->buffer
[b
->length
++] = l
& 0xFF;
129 b
->buffer
[b
->length
++] = l
& 0xFF;
133 w
= * (int16
*) data
;
134 b
->buffer
[b
->length
++] = w
& 0xFF;
136 b
->buffer
[b
->length
++] = w
& 0xFF;
141 b
->buffer
[b
->length
++] = *(* (unsigned char **) &data
);
143 (* (unsigned char **) &data
)++ ;
149 static void membufdump(memorybuffer
*b
,FILE *fp
)
153 fwrite (b
->buffer
, 1, b
->length
, fp
);
155 membufdump(b
->next
,fp
);
158 static int membuflength(memorybuffer
*b
)
161 return b
->length
+ membuflength(b
->next
);
164 static void freemembuf(memorybuffer
*b
)
171 /***********************************************************************
172 * Actual code to deal with RDOFF ouput format begins here...
175 /* global variables set during the initialisation phase */
177 static memorybuffer
*seg
[2]; /* seg 0 = code, seg 1 = data */
178 static memorybuffer
*header
; /* relocation/import/export records */
184 static int segtext
,segdata
,segbss
;
185 static long bsslength
;
187 static void rdf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
)
191 seg
[0] = newmembuf();
192 seg
[1] = newmembuf();
193 header
= newmembuf();
194 segtext
= seg_alloc();
195 segdata
= seg_alloc();
196 segbss
= seg_alloc();
197 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
198 error(ERR_PANIC
,"rdf segment numbers not allocated as expected (%d,%d,%d)",
199 segtext
,segdata
,segbss
);
203 static long rdf_section_names(char *name
, int pass
, int *bits
)
206 * Default is 32 bits.
212 if (!strcmp(name
, ".text")) return 0;
213 else if (!strcmp(name
, ".data")) return 2;
214 else if (!strcmp(name
, ".bss")) return 4;
219 static void write_reloc_rec(struct RelocRec
*r
)
221 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
223 membufwrite(header
,&r
->type
,1);
224 membufwrite(header
,&r
->segment
,1);
225 membufwrite(header
,&r
->offset
,-4);
226 membufwrite(header
,&r
->length
,1);
227 membufwrite(header
,&r
->refseg
,-2); /* 9 bytes written */
230 static void write_export_rec(struct ExportRec
*r
)
234 membufwrite(header
,&r
->type
,1);
235 membufwrite(header
,&r
->segment
,1);
236 membufwrite(header
,&r
->offset
,-4);
237 membufwrite(header
,r
->label
,strlen(r
->label
) + 1);
240 static void write_import_rec(struct ImportRec
*r
)
244 membufwrite(header
,&r
->type
,1);
245 membufwrite(header
,&r
->segment
,-2);
246 membufwrite(header
,r
->label
,strlen(r
->label
) + 1);
249 static void write_bss_rec(struct BSSRec
*r
)
251 membufwrite(header
,&r
->type
,1);
252 membufwrite(header
,&r
->amount
,-4);
255 static void rdf_deflabel(char *name
, long segment
, long offset
, int is_global
)
260 if (is_global
&& segment
> 4) {
261 error(ERR_WARNING
,"common declarations not supported... using extern");
269 strncpy(r
.label
,name
,32);
271 write_export_rec(&r
);
274 if (segment
> 4) { /* EXTERN declaration */
276 ri
.segment
= segment
;
277 strncpy(ri
.label
,name
,32);
279 write_import_rec(&ri
);
283 static void rdf_out (long segto
, void *data
, unsigned long type
,
284 long segment
, long wrt
)
286 long bytes
= type
& OUT_SIZMASK
;
288 unsigned char databuf
[4],*pd
;
290 segto
>>= 1; /* convert NASM segment no to RDF number */
292 if (segto
!= 0 && segto
!= 1 && segto
!= 2) {
293 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
298 wrt
= NO_SEG
; /* continue to do _something_ */
299 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
304 if (segto
== 2 && type
!= OUT_RESERVE
)
306 error(ERR_NONFATAL
, "BSS segments may not be initialised");
308 /* just reserve the space for now... */
310 if (type
== OUT_REL2ADR
)
317 if (type
== OUT_RESERVE
) {
318 if (segto
== 2) /* BSS segment space reserverd */
322 membufwrite(seg
[segto
],databuf
,1);
324 else if (type
== OUT_RAWDATA
) {
325 if (segment
!= NO_SEG
)
326 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
327 membufwrite(seg
[segto
],data
,bytes
);
329 else if (type
== OUT_ADDRESS
) {
331 /* if segment == NO_SEG then we are writing an address of an
332 object within the same segment - do not produce reloc rec. */
334 if (segment
!= NO_SEG
)
337 /* it's an address, so we must write a relocation record */
339 rr
.type
= 1; /* type signature */
340 rr
.segment
= segto
; /* segment we're currently in */
341 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
342 rr
.length
= bytes
; /* length of reference */
343 rr
.refseg
= segment
; /* segment referred to */
344 write_reloc_rec(&rr
);
347 pd
= databuf
; /* convert address to little-endian */
349 WRITESHORT (pd
, *(long *)data
);
351 WRITELONG (pd
, *(long *)data
);
353 membufwrite(seg
[segto
],databuf
,bytes
);
356 else if (type
== OUT_REL2ADR
)
358 if (segment
== segto
)
359 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
360 if (segment
!= NO_SEG
&& segment
% 2) {
361 error(ERR_NONFATAL
, "rdf format does not support segment base refs");
364 rr
.type
= 1; /* type signature */
365 rr
.segment
= segto
+64; /* segment we're currently in + rel flag */
366 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
367 rr
.length
= 2; /* length of reference */
368 rr
.refseg
= segment
; /* segment referred to */
369 write_reloc_rec(&rr
);
371 /* work out what to put in the code: offset of the end of this operand,
372 * subtracted from any data specified, so that loader can just add
373 * address of imported symbol onto it to get address relative to end of
374 * instruction: import_address + data(offset) - end_of_instrn */
376 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
378 membufwrite(seg
[segto
],&rr
.offset
,-2);
380 else if (type
== OUT_REL4ADR
)
382 if (segment
== segto
)
383 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
384 if (segment
!= NO_SEG
&& segment
% 2) {
385 error(ERR_NONFATAL
, "rdf format does not support segment base refs");
388 rr
.type
= 1; /* type signature */
389 rr
.segment
= segto
+64; /* segment we're currently in + rel tag */
390 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
391 rr
.length
= 4; /* length of reference */
392 rr
.refseg
= segment
; /* segment referred to */
393 write_reloc_rec(&rr
);
395 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
396 membufwrite(seg
[segto
],&rr
.offset
,-4);
400 static void rdf_cleanup (void) {
402 unsigned char b
[4],*d
;
406 /* should write imported & exported symbol declarations to header here */
408 /* generate the output file... */
409 fwrite(RDOFFId
,6,1,ofile
); /* file type magic number */
411 if (bsslength
!= 0) /* reserve BSS */
414 bs
.amount
= bsslength
;
418 l
= membuflength(header
);d
=b
;
421 fwrite(b
,4,1,ofile
); /* write length of header */
422 membufdump(header
,ofile
); /* dump header */
424 l
= membuflength(seg
[0]);d
=b
; /* code segment */
428 membufdump(seg
[0],ofile
);
430 l
= membuflength(seg
[1]);d
=b
; /* data segment */
434 membufdump(seg
[1],ofile
);
442 static long rdf_segbase (long segment
) {
446 static int rdf_directive (char *directive
, char *value
, int pass
) {
450 static void rdf_filename (char *inname
, char *outname
, efunc error
) {
451 standard_extension(inname
,outname
,".rdf",error
);
454 struct ofmt of_rdf
= {
455 "Relocatable Dynamic Object File Format v1.1",