Add new copyright headers to the rdoff code
[nasm/avx512.git] / output / outbin.c
blobfdfbe8ebca2415bb90b7584a881a02a253308674
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 /*
35 * outbin.c output routines for the Netwide Assembler to produce
36 * flat-form binary files
39 /* This is the extended version of NASM's original binary output
40 * format. It is backward compatible with the original BIN format,
41 * and contains support for multiple sections and advanced section
42 * ordering.
44 * Feature summary:
46 * - Users can create an arbitrary number of sections; they are not
47 * limited to just ".text", ".data", and ".bss".
49 * - Sections can be either progbits or nobits type.
51 * - You can specify that they be aligned at a certian boundary
52 * following the previous section ("align="), or positioned at an
53 * arbitrary byte-granular location ("start=").
55 * - You can specify a "virtual" start address for a section, which
56 * will be used for the calculation for all address references
57 * with respect to that section ("vstart=").
59 * - The ORG directive, as well as the section/segment directive
60 * arguments ("align=", "start=", "vstart="), can take a critical
61 * expression as their value. For example: "align=(1 << 12)".
63 * - You can generate map files using the 'map' directive.
67 /* Uncomment the following define if you want sections to adapt
68 * their progbits/nobits state depending on what type of
69 * instructions are issued, rather than defaulting to progbits.
70 * Note that this behavior violates the specification.
72 #define ABIN_SMART_ADAPT
76 #include "compiler.h"
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <ctype.h>
82 #include <inttypes.h>
84 #include "nasm.h"
85 #include "nasmlib.h"
86 #include "saa.h"
87 #include "stdscan.h"
88 #include "labels.h"
89 #include "eval.h"
90 #include "output/outform.h"
91 #include "output/outlib.h"
93 #ifdef OF_BIN
95 struct ofmt *bin_get_ofmt(); /* Prototype goes here since no header file. */
97 static FILE *fp, *rf = NULL;
98 static efunc error;
100 /* Section flags keep track of which attributes the user has defined. */
101 #define START_DEFINED 0x001
102 #define ALIGN_DEFINED 0x002
103 #define FOLLOWS_DEFINED 0x004
104 #define VSTART_DEFINED 0x008
105 #define VALIGN_DEFINED 0x010
106 #define VFOLLOWS_DEFINED 0x020
107 #define TYPE_DEFINED 0x040
108 #define TYPE_PROGBITS 0x080
109 #define TYPE_NOBITS 0x100
111 /* This struct is used to keep track of symbols for map-file generation. */
112 static struct bin_label {
113 char *name;
114 struct bin_label *next;
115 } *no_seg_labels, **nsl_tail;
117 static struct Section {
118 char *name;
119 struct SAA *contents;
120 int64_t length; /* section length in bytes */
122 /* Section attributes */
123 int flags; /* see flag definitions above */
124 uint64_t align; /* section alignment */
125 uint64_t valign; /* notional section alignment */
126 uint64_t start; /* section start address */
127 uint64_t vstart; /* section virtual start address */
128 char *follows; /* the section that this one will follow */
129 char *vfollows; /* the section that this one will notionally follow */
130 int32_t start_index; /* NASM section id for non-relocated version */
131 int32_t vstart_index; /* the NASM section id */
133 struct bin_label *labels; /* linked-list of label handles for map output. */
134 struct bin_label **labels_end; /* Holds address of end of labels list. */
135 struct Section *ifollows; /* Points to previous section (implicit follows). */
136 struct Section *next; /* This links sections with a defined start address. */
138 /* The extended bin format allows for sections to have a "virtual"
139 * start address. This is accomplished by creating two sections:
140 * one beginning at the Load Memory Address and the other beginning
141 * at the Virtual Memory Address. The LMA section is only used to
142 * define the section.<section_name>.start label, but there isn't
143 * any other good way for us to handle that label.
146 } *sections, *last_section;
148 static struct Reloc {
149 struct Reloc *next;
150 int32_t posn;
151 int32_t bytes;
152 int32_t secref;
153 int32_t secrel;
154 struct Section *target;
155 } *relocs, **reloctail;
157 extern char *stdscan_bufptr;
159 static uint8_t format_mode; /* 0 = original bin, 1 = extended bin */
160 static int32_t current_section; /* only really needed if format_mode = 0 */
161 static uint64_t origin;
162 static int origin_defined;
164 /* Stuff we need for map-file generation. */
165 #define MAP_ORIGIN 1
166 #define MAP_SUMMARY 2
167 #define MAP_SECTIONS 4
168 #define MAP_SYMBOLS 8
169 static int map_control = 0;
170 static char *infile, *outfile;
172 extern macros_t bin_stdmac[];
174 static void add_reloc(struct Section *s, int32_t bytes, int32_t secref,
175 int32_t secrel)
177 struct Reloc *r;
179 r = *reloctail = nasm_malloc(sizeof(struct Reloc));
180 reloctail = &r->next;
181 r->next = NULL;
182 r->posn = s->length;
183 r->bytes = bytes;
184 r->secref = secref;
185 r->secrel = secrel;
186 r->target = s;
189 static struct Section *find_section_by_name(const char *name)
191 struct Section *s;
193 for (s = sections; s; s = s->next)
194 if (!strcmp(s->name, name))
195 break;
196 return s;
199 static struct Section *find_section_by_index(int32_t index)
201 struct Section *s;
203 for (s = sections; s; s = s->next)
204 if ((index == s->vstart_index) || (index == s->start_index))
205 break;
206 return s;
209 static struct Section *create_section(char *name)
210 { /* Create a new section. */
211 last_section->next = nasm_malloc(sizeof(struct Section));
212 last_section->next->ifollows = last_section;
213 last_section = last_section->next;
214 last_section->labels = NULL;
215 last_section->labels_end = &(last_section->labels);
217 /* Initialize section attributes. */
218 last_section->name = nasm_strdup(name);
219 last_section->contents = saa_init(1L);
220 last_section->follows = last_section->vfollows = 0;
221 last_section->length = 0;
222 last_section->flags = 0;
223 last_section->next = NULL;
225 /* Register our sections with NASM. */
226 last_section->vstart_index = seg_alloc();
227 last_section->start_index = seg_alloc();
228 return last_section;
231 static void bin_cleanup(int debuginfo)
233 struct Section *g, **gp;
234 struct Section *gs = NULL, **gsp;
235 struct Section *s, **sp;
236 struct Section *nobits = NULL, **nt;
237 struct Section *last_progbits;
238 struct bin_label *l;
239 struct Reloc *r;
240 uint64_t pend;
241 int h;
243 (void)debuginfo; /* placate optimizers */
245 #ifdef DEBUG
246 fprintf(stdout,
247 "bin_cleanup: Sections were initially referenced in this order:\n");
248 for (h = 0, s = sections; s; h++, s = s->next)
249 fprintf(stdout, "%i. %s\n", h, s->name);
250 #endif
252 /* Assembly has completed, so now we need to generate the output file.
253 * Step 1: Separate progbits and nobits sections into separate lists.
254 * Step 2: Sort the progbits sections into their output order.
255 * Step 3: Compute start addresses for all progbits sections.
256 * Step 4: Compute vstart addresses for all sections.
257 * Step 5: Apply relocations.
258 * Step 6: Write the sections' data to the output file.
259 * Step 7: Generate the map file.
260 * Step 8: Release all allocated memory.
263 /* To do: Smart section-type adaptation could leave some empty sections
264 * without a defined type (progbits/nobits). Won't fix now since this
265 * feature will be disabled. */
267 /* Step 1: Split progbits and nobits sections into separate lists. */
269 nt = &nobits;
270 /* Move nobits sections into a separate list. Also pre-process nobits
271 * sections' attributes. */
272 for (sp = &sections->next, s = sections->next; s; s = *sp) { /* Skip progbits sections. */
273 if (s->flags & TYPE_PROGBITS) {
274 sp = &s->next;
275 continue;
277 /* Do some special pre-processing on nobits sections' attributes. */
278 if (s->flags & (START_DEFINED | ALIGN_DEFINED | FOLLOWS_DEFINED)) { /* Check for a mixture of real and virtual section attributes. */
279 if (s->flags & (VSTART_DEFINED | VALIGN_DEFINED |
280 VFOLLOWS_DEFINED))
281 error(ERR_FATAL|ERR_NOFILE,
282 "cannot mix real and virtual attributes"
283 " in nobits section (%s)", s->name);
284 /* Real and virtual attributes mean the same thing for nobits sections. */
285 if (s->flags & START_DEFINED) {
286 s->vstart = s->start;
287 s->flags |= VSTART_DEFINED;
289 if (s->flags & ALIGN_DEFINED) {
290 s->valign = s->align;
291 s->flags |= VALIGN_DEFINED;
293 if (s->flags & FOLLOWS_DEFINED) {
294 s->vfollows = s->follows;
295 s->flags |= VFOLLOWS_DEFINED;
296 s->flags &= ~FOLLOWS_DEFINED;
299 /* Every section must have a start address. */
300 if (s->flags & VSTART_DEFINED) {
301 s->start = s->vstart;
302 s->flags |= START_DEFINED;
304 /* Move the section into the nobits list. */
305 *sp = s->next;
306 s->next = NULL;
307 *nt = s;
308 nt = &s->next;
311 /* Step 2: Sort the progbits sections into their output order. */
313 /* In Step 2 we move around sections in groups. A group
314 * begins with a section (group leader) that has a user-
315 * defined start address or follows section. The remainder
316 * of the group is made up of the sections that implicitly
317 * follow the group leader (i.e., they were defined after
318 * the group leader and were not given an explicit start
319 * address or follows section by the user). */
321 /* For anyone attempting to read this code:
322 * g (group) points to a group of sections, the first one of which has
323 * a user-defined start address or follows section.
324 * gp (g previous) holds the location of the pointer to g.
325 * gs (g scan) is a temp variable that we use to scan to the end of the group.
326 * gsp (gs previous) holds the location of the pointer to gs.
327 * nt (nobits tail) points to the nobits section-list tail.
330 /* Link all 'follows' groups to their proper position. To do
331 * this we need to know three things: the start of the group
332 * to relocate (g), the section it is following (s), and the
333 * end of the group we're relocating (gs). */
334 for (gp = &sections, g = sections; g; g = gs) { /* Find the next follows group that is out of place (g). */
335 if (!(g->flags & FOLLOWS_DEFINED)) {
336 while (g->next) {
337 if ((g->next->flags & FOLLOWS_DEFINED) &&
338 strcmp(g->name, g->next->follows))
339 break;
340 g = g->next;
342 if (!g->next)
343 break;
344 gp = &g->next;
345 g = g->next;
347 /* Find the section that this group follows (s). */
348 for (sp = &sections, s = sections;
349 s && strcmp(s->name, g->follows);
350 sp = &s->next, s = s->next) ;
351 if (!s)
352 error(ERR_FATAL|ERR_NOFILE, "section %s follows an invalid or"
353 " unknown section (%s)", g->name, g->follows);
354 if (s->next && (s->next->flags & FOLLOWS_DEFINED) &&
355 !strcmp(s->name, s->next->follows))
356 error(ERR_FATAL|ERR_NOFILE, "sections %s and %s can't both follow"
357 " section %s", g->name, s->next->name, s->name);
358 /* Find the end of the current follows group (gs). */
359 for (gsp = &g->next, gs = g->next;
360 gs && (gs != s) && !(gs->flags & START_DEFINED);
361 gsp = &gs->next, gs = gs->next) {
362 if (gs->next && (gs->next->flags & FOLLOWS_DEFINED) &&
363 strcmp(gs->name, gs->next->follows)) {
364 gsp = &gs->next;
365 gs = gs->next;
366 break;
369 /* Re-link the group after its follows section. */
370 *gsp = s->next;
371 s->next = g;
372 *gp = gs;
375 /* Link all 'start' groups to their proper position. Once
376 * again we need to know g, s, and gs (see above). The main
377 * difference is we already know g since we sort by moving
378 * groups from the 'unsorted' list into a 'sorted' list (g
379 * will always be the first section in the unsorted list). */
380 for (g = sections, sections = NULL; g; g = gs) { /* Find the section that we will insert this group before (s). */
381 for (sp = &sections, s = sections; s; sp = &s->next, s = s->next)
382 if ((s->flags & START_DEFINED) && (g->start < s->start))
383 break;
384 /* Find the end of the group (gs). */
385 for (gs = g->next, gsp = &g->next;
386 gs && !(gs->flags & START_DEFINED);
387 gsp = &gs->next, gs = gs->next) ;
388 /* Re-link the group before the target section. */
389 *sp = g;
390 *gsp = s;
393 /* Step 3: Compute start addresses for all progbits sections. */
395 /* Make sure we have an origin and a start address for the first section. */
396 if (origin_defined) {
397 if (sections->flags & START_DEFINED) {
398 /* Make sure this section doesn't begin before the origin. */
399 if (sections->start < origin)
400 error(ERR_FATAL|ERR_NOFILE, "section %s begins"
401 " before program origin", sections->name);
402 } else if (sections->flags & ALIGN_DEFINED) {
403 sections->start = ((origin + sections->align - 1) &
404 ~(sections->align - 1));
405 } else {
406 sections->start = origin;
408 } else {
409 if (!(sections->flags & START_DEFINED))
410 sections->start = 0;
411 origin = sections->start;
413 sections->flags |= START_DEFINED;
415 /* Make sure each section has an explicit start address. If it
416 * doesn't, then compute one based its alignment and the end of
417 * the previous section. */
418 for (pend = sections->start, g = s = sections; g; g = g->next) { /* Find the next section that could cause an overlap situation
419 * (has a defined start address, and is not zero length). */
420 if (g == s)
421 for (s = g->next;
422 s && ((s->length == 0) || !(s->flags & START_DEFINED));
423 s = s->next) ;
424 /* Compute the start address of this section, if necessary. */
425 if (!(g->flags & START_DEFINED)) { /* Default to an alignment of 4 if unspecified. */
426 if (!(g->flags & ALIGN_DEFINED)) {
427 g->align = 4;
428 g->flags |= ALIGN_DEFINED;
430 /* Set the section start address. */
431 g->start = (pend + g->align - 1) & ~(g->align - 1);
432 g->flags |= START_DEFINED;
434 /* Ugly special case for progbits sections' virtual attributes:
435 * If there is a defined valign, but no vstart and no vfollows, then
436 * we valign after the previous progbits section. This case doesn't
437 * really make much sense for progbits sections with a defined start
438 * address, but it is possible and we must do *something*.
439 * Not-so-ugly special case:
440 * If a progbits section has no virtual attributes, we set the
441 * vstart equal to the start address. */
442 if (!(g->flags & (VSTART_DEFINED | VFOLLOWS_DEFINED))) {
443 if (g->flags & VALIGN_DEFINED)
444 g->vstart = (pend + g->valign - 1) & ~(g->valign - 1);
445 else
446 g->vstart = g->start;
447 g->flags |= VSTART_DEFINED;
449 /* Ignore zero-length sections. */
450 if (g->start < pend)
451 continue;
452 /* Compute the span of this section. */
453 pend = g->start + g->length;
454 /* Check for section overlap. */
455 if (s) {
456 if (s->start < origin)
457 error(ERR_FATAL|ERR_NOFILE, "section %s beings before program origin",
458 s->name);
459 if (g->start > s->start)
460 error(ERR_FATAL|ERR_NOFILE, "sections %s ~ %s and %s overlap!",
461 gs->name, g->name, s->name);
462 if (pend > s->start)
463 error(ERR_FATAL|ERR_NOFILE, "sections %s and %s overlap!",
464 g->name, s->name);
466 /* Remember this section as the latest >0 length section. */
467 gs = g;
470 /* Step 4: Compute vstart addresses for all sections. */
472 /* Attach the nobits sections to the end of the progbits sections. */
473 for (s = sections; s->next; s = s->next) ;
474 s->next = nobits;
475 last_progbits = s;
477 * Scan for sections that don't have a vstart address. If we find
478 * one we'll attempt to compute its vstart. If we can't compute
479 * the vstart, we leave it alone and come back to it in a
480 * subsequent scan. We continue scanning and re-scanning until
481 * we've gone one full cycle without computing any vstarts.
483 do { /* Do one full scan of the sections list. */
484 for (h = 0, g = sections; g; g = g->next) {
485 if (g->flags & VSTART_DEFINED)
486 continue;
487 /* Find the section that this one virtually follows. */
488 if (g->flags & VFOLLOWS_DEFINED) {
489 for (s = sections; s && strcmp(g->vfollows, s->name);
490 s = s->next) ;
491 if (!s)
492 error(ERR_FATAL|ERR_NOFILE,
493 "section %s vfollows unknown section (%s)",
494 g->name, g->vfollows);
495 } else if (g->ifollows != NULL)
496 for (s = sections; s && (s != g->ifollows); s = s->next) ;
497 /* The .bss section is the only one with ifollows = NULL.
498 In this case we implicitly follow the last progbits
499 section. */
500 else
501 s = last_progbits;
503 /* If the section we're following has a vstart, we can proceed. */
504 if (s->flags & VSTART_DEFINED) { /* Default to virtual alignment of four. */
505 if (!(g->flags & VALIGN_DEFINED)) {
506 g->valign = 4;
507 g->flags |= VALIGN_DEFINED;
509 /* Compute the vstart address. */
510 g->vstart = (s->vstart + s->length + g->valign - 1)
511 & ~(g->valign - 1);
512 g->flags |= VSTART_DEFINED;
513 h++;
514 /* Start and vstart mean the same thing for nobits sections. */
515 if (g->flags & TYPE_NOBITS)
516 g->start = g->vstart;
519 } while (h);
521 /* Now check for any circular vfollows references, which will manifest
522 * themselves as sections without a defined vstart. */
523 for (h = 0, s = sections; s; s = s->next) {
524 if (!(s->flags & VSTART_DEFINED)) { /* Non-fatal errors after assembly has completed are generally a
525 * no-no, but we'll throw a fatal one eventually so it's ok. */
526 error(ERR_NONFATAL, "cannot compute vstart for section %s",
527 s->name);
528 h++;
531 if (h)
532 error(ERR_FATAL|ERR_NOFILE, "circular vfollows path detected");
534 #ifdef DEBUG
535 fprintf(stdout,
536 "bin_cleanup: Confirm final section order for output file:\n");
537 for (h = 0, s = sections; s && (s->flags & TYPE_PROGBITS);
538 h++, s = s->next)
539 fprintf(stdout, "%i. %s\n", h, s->name);
540 #endif
542 /* Step 5: Apply relocations. */
544 /* Prepare the sections for relocating. */
545 for (s = sections; s; s = s->next)
546 saa_rewind(s->contents);
547 /* Apply relocations. */
548 for (r = relocs; r; r = r->next) {
549 uint8_t *p, *q, mydata[8];
550 int64_t l;
552 saa_fread(r->target->contents, r->posn, mydata, r->bytes);
553 p = q = mydata;
554 l = *p++;
556 if (r->bytes > 1) {
557 l += ((int64_t)*p++) << 8;
558 if (r->bytes >= 4) {
559 l += ((int64_t)*p++) << 16;
560 l += ((int64_t)*p++) << 24;
562 if (r->bytes == 8) {
563 l += ((int64_t)*p++) << 32;
564 l += ((int64_t)*p++) << 40;
565 l += ((int64_t)*p++) << 48;
566 l += ((int64_t)*p++) << 56;
570 s = find_section_by_index(r->secref);
571 if (s) {
572 if (r->secref == s->start_index)
573 l += s->start;
574 else
575 l += s->vstart;
577 s = find_section_by_index(r->secrel);
578 if (s) {
579 if (r->secrel == s->start_index)
580 l -= s->start;
581 else
582 l -= s->vstart;
585 if (r->bytes >= 4)
586 WRITEDLONG(q, l);
587 else if (r->bytes == 2)
588 WRITESHORT(q, l);
589 else
590 *q++ = (uint8_t)(l & 0xFF);
591 saa_fwrite(r->target->contents, r->posn, mydata, r->bytes);
594 /* Step 6: Write the section data to the output file. */
596 /* Write the progbits sections to the output file. */
597 for (pend = origin, s = sections; s && (s->flags & TYPE_PROGBITS); s = s->next) { /* Skip zero-length sections. */
598 if (s->length == 0)
599 continue;
600 /* Pad the space between sections. */
601 for (h = s->start - pend; h; h--)
602 fputc('\0', fp);
603 /* Write the section to the output file. */
604 if (s->length > 0)
605 saa_fpwrite(s->contents, fp);
606 pend = s->start + s->length;
608 /* Done writing the file, so close it. */
609 fclose(fp);
611 /* Step 7: Generate the map file. */
613 if (map_control) {
614 static const char not_defined[] = "not defined";
616 /* Display input and output file names. */
617 fprintf(rf, "\n- NASM Map file ");
618 for (h = 63; h; h--)
619 fputc('-', rf);
620 fprintf(rf, "\n\nSource file: %s\nOutput file: %s\n\n",
621 infile, outfile);
623 if (map_control & MAP_ORIGIN) { /* Display program origin. */
624 fprintf(rf, "-- Program origin ");
625 for (h = 61; h; h--)
626 fputc('-', rf);
627 fprintf(rf, "\n\n%08"PRIX64"\n\n", origin);
629 /* Display sections summary. */
630 if (map_control & MAP_SUMMARY) {
631 fprintf(rf, "-- Sections (summary) ");
632 for (h = 57; h; h--)
633 fputc('-', rf);
634 fprintf(rf, "\n\nVstart Start Stop "
635 "Length Class Name\n");
636 for (s = sections; s; s = s->next) {
637 fprintf(rf, "%16"PRIX64" %16"PRIX64" %16"PRIX64" %08"PRIX64" ",
638 s->vstart, s->start, s->start + s->length,
639 s->length);
640 if (s->flags & TYPE_PROGBITS)
641 fprintf(rf, "progbits ");
642 else
643 fprintf(rf, "nobits ");
644 fprintf(rf, "%s\n", s->name);
646 fprintf(rf, "\n");
648 /* Display detailed section information. */
649 if (map_control & MAP_SECTIONS) {
650 fprintf(rf, "-- Sections (detailed) ");
651 for (h = 56; h; h--)
652 fputc('-', rf);
653 fprintf(rf, "\n\n");
654 for (s = sections; s; s = s->next) {
655 fprintf(rf, "---- Section %s ", s->name);
656 for (h = 65 - strlen(s->name); h; h--)
657 fputc('-', rf);
658 fprintf(rf, "\n\nclass: ");
659 if (s->flags & TYPE_PROGBITS)
660 fprintf(rf, "progbits");
661 else
662 fprintf(rf, "nobits");
663 fprintf(rf, "\nlength: %16"PRIX64"\nstart: %16"PRIX64""
664 "\nalign: ", s->length, s->start);
665 if (s->flags & ALIGN_DEFINED)
666 fprintf(rf, "%16"PRIX64"", s->align);
667 else
668 fputs(not_defined, rf);
669 fprintf(rf, "\nfollows: ");
670 if (s->flags & FOLLOWS_DEFINED)
671 fprintf(rf, "%s", s->follows);
672 else
673 fputs(not_defined, rf);
674 fprintf(rf, "\nvstart: %16"PRIX64"\nvalign: ", s->vstart);
675 if (s->flags & VALIGN_DEFINED)
676 fprintf(rf, "%16"PRIX64"", s->valign);
677 else
678 fputs(not_defined, rf);
679 fprintf(rf, "\nvfollows: ");
680 if (s->flags & VFOLLOWS_DEFINED)
681 fprintf(rf, "%s", s->vfollows);
682 else
683 fputs(not_defined, rf);
684 fprintf(rf, "\n\n");
687 /* Display symbols information. */
688 if (map_control & MAP_SYMBOLS) {
689 int32_t segment;
690 int64_t offset;
692 fprintf(rf, "-- Symbols ");
693 for (h = 68; h; h--)
694 fputc('-', rf);
695 fprintf(rf, "\n\n");
696 if (no_seg_labels) {
697 fprintf(rf, "---- No Section ");
698 for (h = 63; h; h--)
699 fputc('-', rf);
700 fprintf(rf, "\n\nValue Name\n");
701 for (l = no_seg_labels; l; l = l->next) {
702 lookup_label(l->name, &segment, &offset);
703 fprintf(rf, "%08"PRIX64" %s\n", offset, l->name);
705 fprintf(rf, "\n\n");
707 for (s = sections; s; s = s->next) {
708 if (s->labels) {
709 fprintf(rf, "---- Section %s ", s->name);
710 for (h = 65 - strlen(s->name); h; h--)
711 fputc('-', rf);
712 fprintf(rf, "\n\nReal Virtual Name\n");
713 for (l = s->labels; l; l = l->next) {
714 lookup_label(l->name, &segment, &offset);
715 fprintf(rf, "%16"PRIX64" %16"PRIX64" %s\n",
716 s->start + offset, s->vstart + offset,
717 l->name);
719 fprintf(rf, "\n");
725 /* Close the report file. */
726 if (map_control && (rf != stdout) && (rf != stderr))
727 fclose(rf);
729 /* Step 8: Release all allocated memory. */
731 /* Free sections, label pointer structs, etc.. */
732 while (sections) {
733 s = sections;
734 sections = s->next;
735 saa_free(s->contents);
736 nasm_free(s->name);
737 if (s->flags & FOLLOWS_DEFINED)
738 nasm_free(s->follows);
739 if (s->flags & VFOLLOWS_DEFINED)
740 nasm_free(s->vfollows);
741 while (s->labels) {
742 l = s->labels;
743 s->labels = l->next;
744 nasm_free(l);
746 nasm_free(s);
749 /* Free no-section labels. */
750 while (no_seg_labels) {
751 l = no_seg_labels;
752 no_seg_labels = l->next;
753 nasm_free(l);
756 /* Free relocation structures. */
757 while (relocs) {
758 r = relocs->next;
759 nasm_free(relocs);
760 relocs = r;
764 static void bin_out(int32_t segto, const void *data,
765 enum out_type type, uint64_t size,
766 int32_t segment, int32_t wrt)
768 uint8_t *p, mydata[8];
769 struct Section *s;
771 if (wrt != NO_SEG) {
772 wrt = NO_SEG; /* continue to do _something_ */
773 error(ERR_NONFATAL, "WRT not supported by binary output format");
776 /* Handle absolute-assembly (structure definitions). */
777 if (segto == NO_SEG) {
778 if (type != OUT_RESERVE)
779 error(ERR_NONFATAL, "attempt to assemble code in"
780 " [ABSOLUTE] space");
781 return;
784 /* Find the segment we are targeting. */
785 s = find_section_by_index(segto);
786 if (!s)
787 error(ERR_PANIC, "code directed to nonexistent segment?");
789 /* "Smart" section-type adaptation code. */
790 if (!(s->flags & TYPE_DEFINED)) {
791 if (type == OUT_RESERVE)
792 s->flags |= TYPE_DEFINED | TYPE_NOBITS;
793 else
794 s->flags |= TYPE_DEFINED | TYPE_PROGBITS;
797 if ((s->flags & TYPE_NOBITS) && (type != OUT_RESERVE))
798 error(ERR_WARNING, "attempt to initialize memory in a"
799 " nobits section: ignored");
801 if (type == OUT_ADDRESS) {
802 if (segment != NO_SEG && !find_section_by_index(segment)) {
803 if (segment % 2)
804 error(ERR_NONFATAL, "binary output format does not support"
805 " segment base references");
806 else
807 error(ERR_NONFATAL, "binary output format does not support"
808 " external references");
809 segment = NO_SEG;
811 if (s->flags & TYPE_PROGBITS) {
812 if (segment != NO_SEG)
813 add_reloc(s, size, segment, -1L);
814 p = mydata;
815 WRITEADDR(p, *(int64_t *)data, size);
816 saa_wbytes(s->contents, mydata, size);
818 s->length += size;
819 } else if (type == OUT_RAWDATA) {
820 if (s->flags & TYPE_PROGBITS)
821 saa_wbytes(s->contents, data, size);
822 s->length += size;
823 } else if (type == OUT_RESERVE) {
824 if (s->flags & TYPE_PROGBITS) {
825 error(ERR_WARNING, "uninitialized space declared in"
826 " %s section: zeroing", s->name);
827 saa_wbytes(s->contents, NULL, size);
829 s->length += size;
830 } else if (type == OUT_REL2ADR || type == OUT_REL4ADR ||
831 type == OUT_REL8ADR) {
832 int64_t addr = *(int64_t *)data - size;
833 size = realsize(type, size);
834 if (segment != NO_SEG && !find_section_by_index(segment)) {
835 if (segment % 2)
836 error(ERR_NONFATAL, "binary output format does not support"
837 " segment base references");
838 else
839 error(ERR_NONFATAL, "binary output format does not support"
840 " external references");
841 segment = NO_SEG;
843 if (s->flags & TYPE_PROGBITS) {
844 add_reloc(s, size, segment, segto);
845 p = mydata;
846 WRITEADDR(p, addr - s->length, size);
847 saa_wbytes(s->contents, mydata, size);
849 s->length += size;
853 static void bin_deflabel(char *name, int32_t segment, int64_t offset,
854 int is_global, char *special)
856 (void)segment; /* Don't warn that this parameter is unused */
857 (void)offset; /* Don't warn that this parameter is unused */
859 if (special)
860 error(ERR_NONFATAL, "binary format does not support any"
861 " special symbol types");
862 else if (name[0] == '.' && name[1] == '.' && name[2] != '@')
863 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
864 else if (is_global == 2)
865 error(ERR_NONFATAL, "binary output format does not support common"
866 " variables");
867 else {
868 struct Section *s;
869 struct bin_label ***ltp;
871 /* Remember label definition so we can look it up later when
872 * creating the map file. */
873 s = find_section_by_index(segment);
874 if (s)
875 ltp = &(s->labels_end);
876 else
877 ltp = &nsl_tail;
878 (**ltp) = nasm_malloc(sizeof(struct bin_label));
879 (**ltp)->name = name;
880 (**ltp)->next = NULL;
881 *ltp = &((**ltp)->next);
886 /* These constants and the following function are used
887 * by bin_secname() to parse attribute assignments. */
889 enum { ATTRIB_START, ATTRIB_ALIGN, ATTRIB_FOLLOWS,
890 ATTRIB_VSTART, ATTRIB_VALIGN, ATTRIB_VFOLLOWS,
891 ATTRIB_NOBITS, ATTRIB_PROGBITS
894 static int bin_read_attribute(char **line, int *attribute,
895 uint64_t *value)
897 expr *e;
898 int attrib_name_size;
899 struct tokenval tokval;
900 char *exp;
902 /* Skip whitespace. */
903 while (**line && nasm_isspace(**line))
904 (*line)++;
905 if (!**line)
906 return 0;
908 /* Figure out what attribute we're reading. */
909 if (!nasm_strnicmp(*line, "align=", 6)) {
910 *attribute = ATTRIB_ALIGN;
911 attrib_name_size = 6;
912 } else if (format_mode) {
913 if (!nasm_strnicmp(*line, "start=", 6)) {
914 *attribute = ATTRIB_START;
915 attrib_name_size = 6;
916 } else if (!nasm_strnicmp(*line, "follows=", 8)) {
917 *attribute = ATTRIB_FOLLOWS;
918 *line += 8;
919 return 1;
920 } else if (!nasm_strnicmp(*line, "vstart=", 7)) {
921 *attribute = ATTRIB_VSTART;
922 attrib_name_size = 7;
923 } else if (!nasm_strnicmp(*line, "valign=", 7)) {
924 *attribute = ATTRIB_VALIGN;
925 attrib_name_size = 7;
926 } else if (!nasm_strnicmp(*line, "vfollows=", 9)) {
927 *attribute = ATTRIB_VFOLLOWS;
928 *line += 9;
929 return 1;
930 } else if (!nasm_strnicmp(*line, "nobits", 6) &&
931 (nasm_isspace((*line)[6]) || ((*line)[6] == '\0'))) {
932 *attribute = ATTRIB_NOBITS;
933 *line += 6;
934 return 1;
935 } else if (!nasm_strnicmp(*line, "progbits", 8) &&
936 (nasm_isspace((*line)[8]) || ((*line)[8] == '\0'))) {
937 *attribute = ATTRIB_PROGBITS;
938 *line += 8;
939 return 1;
940 } else
941 return 0;
942 } else
943 return 0;
945 /* Find the end of the expression. */
946 if ((*line)[attrib_name_size] != '(') {
947 /* Single term (no parenthesis). */
948 exp = *line += attrib_name_size;
949 while (**line && !nasm_isspace(**line))
950 (*line)++;
951 if (**line) {
952 **line = '\0';
953 (*line)++;
955 } else {
956 char c;
957 int pcount = 1;
959 /* Full expression (delimited by parenthesis) */
960 exp = *line += attrib_name_size + 1;
961 while (1) {
962 (*line) += strcspn(*line, "()'\"");
963 if (**line == '(') {
964 ++(*line);
965 ++pcount;
967 if (**line == ')') {
968 ++(*line);
969 --pcount;
970 if (!pcount)
971 break;
973 if ((**line == '"') || (**line == '\'')) {
974 c = **line;
975 while (**line) {
976 ++(*line);
977 if (**line == c)
978 break;
980 if (!**line) {
981 error(ERR_NONFATAL,
982 "invalid syntax in `section' directive");
983 return -1;
985 ++(*line);
987 if (!**line) {
988 error(ERR_NONFATAL, "expecting `)'");
989 return -1;
992 *(*line - 1) = '\0'; /* Terminate the expression. */
995 /* Check for no value given. */
996 if (!*exp) {
997 error(ERR_WARNING, "No value given to attribute in"
998 " `section' directive");
999 return -1;
1002 /* Read and evaluate the expression. */
1003 stdscan_reset();
1004 stdscan_bufptr = exp;
1005 tokval.t_type = TOKEN_INVALID;
1006 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
1007 if (e) {
1008 if (!is_really_simple(e)) {
1009 error(ERR_NONFATAL, "section attribute value must be"
1010 " a critical expression");
1011 return -1;
1013 } else {
1014 error(ERR_NONFATAL, "Invalid attribute value"
1015 " specified in `section' directive.");
1016 return -1;
1018 *value = (uint64_t)reloc_value(e);
1019 return 1;
1022 static void bin_assign_attributes(struct Section *sec, char *astring)
1024 int attribute, check;
1025 uint64_t value;
1026 char *p;
1028 while (1) { /* Get the next attribute. */
1029 check = bin_read_attribute(&astring, &attribute, &value);
1030 /* Skip bad attribute. */
1031 if (check == -1)
1032 continue;
1033 /* Unknown section attribute, so skip it and warn the user. */
1034 if (!check) {
1035 if (!*astring)
1036 break; /* End of line. */
1037 else {
1038 p = astring;
1039 while (*astring && !nasm_isspace(*astring))
1040 astring++;
1041 if (*astring) {
1042 *astring = '\0';
1043 astring++;
1045 error(ERR_WARNING, "ignoring unknown section attribute:"
1046 " \"%s\"", p);
1048 continue;
1051 switch (attribute) { /* Handle nobits attribute. */
1052 case ATTRIB_NOBITS:
1053 if ((sec->flags & TYPE_DEFINED)
1054 && (sec->flags & TYPE_PROGBITS))
1055 error(ERR_NONFATAL,
1056 "attempt to change section type"
1057 " from progbits to nobits");
1058 else
1059 sec->flags |= TYPE_DEFINED | TYPE_NOBITS;
1060 continue;
1062 /* Handle progbits attribute. */
1063 case ATTRIB_PROGBITS:
1064 if ((sec->flags & TYPE_DEFINED) && (sec->flags & TYPE_NOBITS))
1065 error(ERR_NONFATAL, "attempt to change section type"
1066 " from nobits to progbits");
1067 else
1068 sec->flags |= TYPE_DEFINED | TYPE_PROGBITS;
1069 continue;
1071 /* Handle align attribute. */
1072 case ATTRIB_ALIGN:
1073 if (!format_mode && (!strcmp(sec->name, ".text")))
1074 error(ERR_NONFATAL, "cannot specify an alignment"
1075 " to the .text section");
1076 else {
1077 if (!value || ((value - 1) & value))
1078 error(ERR_NONFATAL, "argument to `align' is not a"
1079 " power of two");
1080 else { /* Alignment is already satisfied if the previous
1081 * align value is greater. */
1082 if ((sec->flags & ALIGN_DEFINED)
1083 && (value < sec->align))
1084 value = sec->align;
1086 /* Don't allow a conflicting align value. */
1087 if ((sec->flags & START_DEFINED)
1088 && (sec->start & (value - 1)))
1089 error(ERR_NONFATAL,
1090 "`align' value conflicts "
1091 "with section start address");
1092 else {
1093 sec->align = value;
1094 sec->flags |= ALIGN_DEFINED;
1098 continue;
1100 /* Handle valign attribute. */
1101 case ATTRIB_VALIGN:
1102 if (!value || ((value - 1) & value))
1103 error(ERR_NONFATAL, "argument to `valign' is not a"
1104 " power of two");
1105 else { /* Alignment is already satisfied if the previous
1106 * align value is greater. */
1107 if ((sec->flags & VALIGN_DEFINED) && (value < sec->valign))
1108 value = sec->valign;
1110 /* Don't allow a conflicting valign value. */
1111 if ((sec->flags & VSTART_DEFINED)
1112 && (sec->vstart & (value - 1)))
1113 error(ERR_NONFATAL,
1114 "`valign' value conflicts "
1115 "with `vstart' address");
1116 else {
1117 sec->valign = value;
1118 sec->flags |= VALIGN_DEFINED;
1121 continue;
1123 /* Handle start attribute. */
1124 case ATTRIB_START:
1125 if (sec->flags & FOLLOWS_DEFINED)
1126 error(ERR_NONFATAL, "cannot combine `start' and `follows'"
1127 " section attributes");
1128 else if ((sec->flags & START_DEFINED) && (value != sec->start))
1129 error(ERR_NONFATAL, "section start address redefined");
1130 else {
1131 sec->start = value;
1132 sec->flags |= START_DEFINED;
1133 if (sec->flags & ALIGN_DEFINED) {
1134 if (sec->start & (sec->align - 1))
1135 error(ERR_NONFATAL, "`start' address conflicts"
1136 " with section alignment");
1137 sec->flags ^= ALIGN_DEFINED;
1140 continue;
1142 /* Handle vstart attribute. */
1143 case ATTRIB_VSTART:
1144 if (sec->flags & VFOLLOWS_DEFINED)
1145 error(ERR_NONFATAL,
1146 "cannot combine `vstart' and `vfollows'"
1147 " section attributes");
1148 else if ((sec->flags & VSTART_DEFINED)
1149 && (value != sec->vstart))
1150 error(ERR_NONFATAL,
1151 "section virtual start address"
1152 " (vstart) redefined");
1153 else {
1154 sec->vstart = value;
1155 sec->flags |= VSTART_DEFINED;
1156 if (sec->flags & VALIGN_DEFINED) {
1157 if (sec->vstart & (sec->valign - 1))
1158 error(ERR_NONFATAL, "`vstart' address conflicts"
1159 " with `valign' value");
1160 sec->flags ^= VALIGN_DEFINED;
1163 continue;
1165 /* Handle follows attribute. */
1166 case ATTRIB_FOLLOWS:
1167 p = astring;
1168 astring += strcspn(astring, " \t");
1169 if (astring == p)
1170 error(ERR_NONFATAL, "expecting section name for `follows'"
1171 " attribute");
1172 else {
1173 *(astring++) = '\0';
1174 if (sec->flags & START_DEFINED)
1175 error(ERR_NONFATAL,
1176 "cannot combine `start' and `follows'"
1177 " section attributes");
1178 sec->follows = nasm_strdup(p);
1179 sec->flags |= FOLLOWS_DEFINED;
1181 continue;
1183 /* Handle vfollows attribute. */
1184 case ATTRIB_VFOLLOWS:
1185 if (sec->flags & VSTART_DEFINED)
1186 error(ERR_NONFATAL,
1187 "cannot combine `vstart' and `vfollows'"
1188 " section attributes");
1189 else {
1190 p = astring;
1191 astring += strcspn(astring, " \t");
1192 if (astring == p)
1193 error(ERR_NONFATAL,
1194 "expecting section name for `vfollows'"
1195 " attribute");
1196 else {
1197 *(astring++) = '\0';
1198 sec->vfollows = nasm_strdup(p);
1199 sec->flags |= VFOLLOWS_DEFINED;
1202 continue;
1207 static void bin_define_section_labels(void)
1209 static int labels_defined = 0;
1210 struct Section *sec;
1211 char *label_name;
1212 size_t base_len;
1214 if (labels_defined)
1215 return;
1216 for (sec = sections; sec; sec = sec->next) {
1217 base_len = strlen(sec->name) + 8;
1218 label_name = nasm_malloc(base_len + 8);
1219 strcpy(label_name, "section.");
1220 strcpy(label_name + 8, sec->name);
1222 /* section.<name>.start */
1223 strcpy(label_name + base_len, ".start");
1224 define_label(label_name, sec->start_index, 0L,
1225 NULL, 0, 0, bin_get_ofmt(), error);
1227 /* section.<name>.vstart */
1228 strcpy(label_name + base_len, ".vstart");
1229 define_label(label_name, sec->vstart_index, 0L,
1230 NULL, 0, 0, bin_get_ofmt(), error);
1232 nasm_free(label_name);
1234 labels_defined = 1;
1237 static int32_t bin_secname(char *name, int pass, int *bits)
1239 char *p;
1240 struct Section *sec;
1242 /* bin_secname is called with *name = NULL at the start of each
1243 * pass. Use this opportunity to establish the default section
1244 * (default is BITS-16 ".text" segment).
1246 if (!name) { /* Reset ORG and section attributes at the start of each pass. */
1247 origin_defined = 0;
1248 for (sec = sections; sec; sec = sec->next)
1249 sec->flags &= ~(START_DEFINED | VSTART_DEFINED |
1250 ALIGN_DEFINED | VALIGN_DEFINED);
1252 /* Define section start and vstart labels. */
1253 if (format_mode && (pass != 1))
1254 bin_define_section_labels();
1256 /* Establish the default (.text) section. */
1257 *bits = 16;
1258 sec = find_section_by_name(".text");
1259 sec->flags |= TYPE_DEFINED | TYPE_PROGBITS;
1260 current_section = sec->vstart_index;
1261 return current_section;
1264 /* Attempt to find the requested section. If it does not
1265 * exist, create it. */
1266 p = name;
1267 while (*p && !nasm_isspace(*p))
1268 p++;
1269 if (*p)
1270 *p++ = '\0';
1271 sec = find_section_by_name(name);
1272 if (!sec) {
1273 sec = create_section(name);
1274 if (!strcmp(name, ".data"))
1275 sec->flags |= TYPE_DEFINED | TYPE_PROGBITS;
1276 else if (!strcmp(name, ".bss")) {
1277 sec->flags |= TYPE_DEFINED | TYPE_NOBITS;
1278 sec->ifollows = NULL;
1279 } else if (!format_mode) {
1280 error(ERR_NONFATAL, "section name must be "
1281 ".text, .data, or .bss");
1282 return current_section;
1286 /* Handle attribute assignments. */
1287 if (pass != 1)
1288 bin_assign_attributes(sec, p);
1290 #ifndef ABIN_SMART_ADAPT
1291 /* The following line disables smart adaptation of
1292 * PROGBITS/NOBITS section types (it forces sections to
1293 * default to PROGBITS). */
1294 if ((pass != 1) && !(sec->flags & TYPE_DEFINED))
1295 sec->flags |= TYPE_DEFINED | TYPE_PROGBITS;
1296 #endif
1298 /* Set the current section and return. */
1299 current_section = sec->vstart_index;
1300 return current_section;
1303 static int bin_directive(char *directive, char *args, int pass)
1305 /* Handle ORG directive */
1306 if (!nasm_stricmp(directive, "org")) {
1307 struct tokenval tokval;
1308 uint64_t value;
1309 expr *e;
1311 stdscan_reset();
1312 stdscan_bufptr = args;
1313 tokval.t_type = TOKEN_INVALID;
1314 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
1315 if (e) {
1316 if (!is_really_simple(e))
1317 error(ERR_NONFATAL, "org value must be a critical"
1318 " expression");
1319 else {
1320 value = reloc_value(e);
1321 /* Check for ORG redefinition. */
1322 if (origin_defined && (value != origin))
1323 error(ERR_NONFATAL, "program origin redefined");
1324 else {
1325 origin = value;
1326 origin_defined = 1;
1329 } else
1330 error(ERR_NONFATAL, "No or invalid offset specified"
1331 " in ORG directive.");
1332 return 1;
1335 /* The 'map' directive allows the user to generate section
1336 * and symbol information to stdout, stderr, or to a file. */
1337 else if (format_mode && !nasm_stricmp(directive, "map")) {
1338 char *p;
1340 if (pass != 1)
1341 return 1;
1342 args += strspn(args, " \t");
1343 while (*args) {
1344 p = args;
1345 args += strcspn(args, " \t");
1346 if (*args != '\0')
1347 *(args++) = '\0';
1348 if (!nasm_stricmp(p, "all"))
1349 map_control |=
1350 MAP_ORIGIN | MAP_SUMMARY | MAP_SECTIONS | MAP_SYMBOLS;
1351 else if (!nasm_stricmp(p, "brief"))
1352 map_control |= MAP_ORIGIN | MAP_SUMMARY;
1353 else if (!nasm_stricmp(p, "sections"))
1354 map_control |= MAP_ORIGIN | MAP_SUMMARY | MAP_SECTIONS;
1355 else if (!nasm_stricmp(p, "segments"))
1356 map_control |= MAP_ORIGIN | MAP_SUMMARY | MAP_SECTIONS;
1357 else if (!nasm_stricmp(p, "symbols"))
1358 map_control |= MAP_SYMBOLS;
1359 else if (!rf) {
1360 if (!nasm_stricmp(p, "stdout"))
1361 rf = stdout;
1362 else if (!nasm_stricmp(p, "stderr"))
1363 rf = stderr;
1364 else { /* Must be a filename. */
1365 rf = fopen(p, "wt");
1366 if (!rf) {
1367 error(ERR_WARNING, "unable to open map file `%s'",
1369 map_control = 0;
1370 return 1;
1373 } else
1374 error(ERR_WARNING, "map file already specified");
1376 if (map_control == 0)
1377 map_control |= MAP_ORIGIN | MAP_SUMMARY;
1378 if (!rf)
1379 rf = stdout;
1380 return 1;
1382 return 0;
1385 static void bin_filename(char *inname, char *outname, efunc error)
1387 standard_extension(inname, outname, "", error);
1388 infile = inname;
1389 outfile = outname;
1392 static int32_t bin_segbase(int32_t segment)
1394 return segment;
1397 static int bin_set_info(enum geninfo type, char **val)
1399 (void)type;
1400 (void)val;
1401 return 0;
1404 static void bin_init(FILE * afp, efunc errfunc, ldfunc ldef, evalfunc eval)
1406 fp = afp;
1407 error = errfunc;
1409 (void)eval; /* Don't warn that this parameter is unused. */
1410 (void)ldef; /* Placate optimizers. */
1412 maxbits = 64; /* Support 64-bit Segments */
1413 relocs = NULL;
1414 reloctail = &relocs;
1415 origin_defined = 0;
1416 no_seg_labels = NULL;
1417 nsl_tail = &no_seg_labels;
1418 format_mode = 1; /* Extended bin format
1419 * (set this to zero for old bin format). */
1421 /* Create default section (.text). */
1422 sections = last_section = nasm_malloc(sizeof(struct Section));
1423 last_section->next = NULL;
1424 last_section->name = nasm_strdup(".text");
1425 last_section->contents = saa_init(1L);
1426 last_section->follows = last_section->vfollows = 0;
1427 last_section->ifollows = NULL;
1428 last_section->length = 0;
1429 last_section->flags = TYPE_DEFINED | TYPE_PROGBITS;
1430 last_section->labels = NULL;
1431 last_section->labels_end = &(last_section->labels);
1432 last_section->start_index = seg_alloc();
1433 last_section->vstart_index = current_section = seg_alloc();
1436 struct ofmt of_bin = {
1437 "flat-form binary files (e.g. DOS .COM, .SYS)",
1438 "bin",
1439 NULL,
1440 null_debug_arr,
1441 &null_debug_form,
1442 bin_stdmac,
1443 bin_init,
1444 bin_set_info,
1445 bin_out,
1446 bin_deflabel,
1447 bin_secname,
1448 bin_segbase,
1449 bin_directive,
1450 bin_filename,
1451 bin_cleanup
1454 /* This is needed for bin_define_section_labels() */
1455 struct ofmt *bin_get_ofmt(void)
1457 return &of_bin;
1460 #endif /* #ifdef OF_BIN */