1 /* outbin.c output routines for the Netwide Assembler to produce
2 * flat-form binary files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
10 /* This is the extended version of NASM's original binary output
11 * format. It is backward compatible with the original BIN format,
12 * and contains support for multiple sections and advanced section
17 * - Users can create an arbitrary number of sections; they are not
18 * limited to just ".text", ".data", and ".bss".
20 * - Sections can be either progbits or nobits type.
22 * - You can specify that they be aligned at a certian boundary
23 * following the previous section ("align="), or positioned at an
24 * arbitrary byte-granular location ("start=").
26 * - You can specify a "virtual" start address for a section, which
27 * will be used for the calculation for all address references
28 * with respect to that section ("vstart=").
30 * - The ORG directive, as well as the section/segment directive
31 * arguments ("align=", "start=", "vstart="), can take a critical
32 * expression as their value. For example: "align=(1 << 12)".
34 * - You can generate map files using the 'map' directive.
38 /* Uncomment the following define if you want sections to adapt
39 * their progbits/nobits state depending on what type of
40 * instructions are issued, rather than defaulting to progbits.
41 * Note that this behavior violates the specification.
43 #define ABIN_SMART_ADAPT
61 struct ofmt
*bin_get_ofmt(); /* Prototype goes here since no header file. */
63 static FILE *fp
, *rf
= NULL
;
66 /* Section flags keep track of which attributes the user has defined. */
67 #define START_DEFINED 0x001
68 #define ALIGN_DEFINED 0x002
69 #define FOLLOWS_DEFINED 0x004
70 #define VSTART_DEFINED 0x008
71 #define VALIGN_DEFINED 0x010
72 #define VFOLLOWS_DEFINED 0x020
73 #define TYPE_DEFINED 0x040
74 #define TYPE_PROGBITS 0x080
75 #define TYPE_NOBITS 0x100
77 /* This struct is used to keep track of symbols for map-file generation. */
78 static struct bin_label
{
80 struct bin_label
*next
;
81 } *no_seg_labels
, **nsl_tail
;
83 static struct Section
{
86 int32_t length
; /* section length in bytes */
88 /* Section attributes */
89 int flags
; /* see flag definitions above */
90 uint32_t align
; /* section alignment */
91 uint32_t valign
; /* notional section alignment */
92 uint32_t start
; /* section start address */
93 uint32_t vstart
; /* section virtual start address */
94 int8_t *follows
; /* the section that this one will follow */
95 int8_t *vfollows
; /* the section that this one will notionally follow */
96 int32_t start_index
; /* NASM section id for non-relocated version */
97 int32_t vstart_index
; /* the NASM section id */
99 struct bin_label
*labels
; /* linked-list of label handles for map output. */
100 struct bin_label
**labels_end
; /* Holds address of end of labels list. */
101 struct Section
*ifollows
; /* Points to previous section (implicit follows). */
102 struct Section
*next
; /* This links sections with a defined start address. */
104 /* The extended bin format allows for sections to have a "virtual"
105 * start address. This is accomplished by creating two sections:
106 * one beginning at the Load Memory Address and the other beginning
107 * at the Virtual Memory Address. The LMA section is only used to
108 * define the section.<section_name>.start label, but there isn't
109 * any other good way for us to handle that label.
112 } *sections
, *last_section
;
114 static struct Reloc
{
120 struct Section
*target
;
121 } *relocs
, **reloctail
;
123 extern int8_t *stdscan_bufptr
;
124 extern int lookup_label(int8_t *label
, int32_t *segment
, int32_t *offset
);
126 static uint8_t format_mode
; /* 0 = original bin, 1 = extended bin */
127 static int32_t current_section
; /* only really needed if format_mode = 0 */
128 static uint32_t origin
;
129 static int origin_defined
;
131 /* Stuff we need for map-file generation. */
133 #define MAP_SUMMARY 2
134 #define MAP_SECTIONS 4
135 #define MAP_SYMBOLS 8
136 static int map_control
= 0;
137 static int8_t *infile
, *outfile
;
139 static const int8_t *bin_stdmac
[] = {
140 "%define __SECT__ [section .text]",
141 "%imacro org 1+.nolist",
144 "%macro __NASM_CDecl__ 1",
149 static void add_reloc(struct Section
*s
, int32_t bytes
, int32_t secref
,
154 r
= *reloctail
= nasm_malloc(sizeof(struct Reloc
));
155 reloctail
= &r
->next
;
164 static struct Section
*find_section_by_name(const int8_t *name
)
168 for (s
= sections
; s
; s
= s
->next
)
169 if (!strcmp(s
->name
, name
))
174 static struct Section
*find_section_by_index(int32_t index
)
178 for (s
= sections
; s
; s
= s
->next
)
179 if ((index
== s
->vstart_index
) || (index
== s
->start_index
))
184 static struct Section
*create_section(int8_t *name
)
185 { /* Create a new section. */
186 last_section
->next
= nasm_malloc(sizeof(struct Section
));
187 last_section
->next
->ifollows
= last_section
;
188 last_section
= last_section
->next
;
189 last_section
->labels
= NULL
;
190 last_section
->labels_end
= &(last_section
->labels
);
192 /* Initialize section attributes. */
193 last_section
->name
= nasm_strdup(name
);
194 last_section
->contents
= saa_init(1L);
195 last_section
->follows
= last_section
->vfollows
= 0;
196 last_section
->length
= 0;
197 last_section
->flags
= 0;
198 last_section
->next
= NULL
;
200 /* Register our sections with NASM. */
201 last_section
->vstart_index
= seg_alloc();
202 last_section
->start_index
= seg_alloc();
206 static void bin_cleanup(int debuginfo
)
208 struct Section
*g
, **gp
;
209 struct Section
*gs
= NULL
, **gsp
;
210 struct Section
*s
, **sp
;
211 struct Section
*nobits
= NULL
, **nt
;
212 struct Section
*last_progbits
;
220 "bin_cleanup: Sections were initially referenced in this order:\n");
221 for (h
= 0, s
= sections
; s
; h
++, s
= s
->next
)
222 fprintf(stdout
, "%i. %s\n", h
, s
->name
);
225 /* Assembly has completed, so now we need to generate the output file.
226 * Step 1: Separate progbits and nobits sections into separate lists.
227 * Step 2: Sort the progbits sections into their output order.
228 * Step 3: Compute start addresses for all progbits sections.
229 * Step 4: Compute vstart addresses for all sections.
230 * Step 5: Apply relocations.
231 * Step 6: Write the sections' data to the output file.
232 * Step 7: Generate the map file.
233 * Step 8: Release all allocated memory.
236 /* To do: Smart section-type adaptation could leave some empty sections
237 * without a defined type (progbits/nobits). Won't fix now since this
238 * feature will be disabled. */
240 /* Step 1: Split progbits and nobits sections into separate lists. */
243 /* Move nobits sections into a separate list. Also pre-process nobits
244 * sections' attributes. */
245 for (sp
= §ions
->next
, s
= sections
->next
; s
; s
= *sp
) { /* Skip progbits sections. */
246 if (s
->flags
& TYPE_PROGBITS
) {
250 /* Do some special pre-processing on nobits sections' attributes. */
251 if (s
->flags
& (START_DEFINED
| ALIGN_DEFINED
| FOLLOWS_DEFINED
)) { /* Check for a mixture of real and virtual section attributes. */
253 flags
& (VSTART_DEFINED
| VALIGN_DEFINED
|
256 "cannot mix real and virtual attributes"
257 " in nobits section (%s)", s
->name
);
258 /* Real and virtual attributes mean the same thing for nobits sections. */
259 if (s
->flags
& START_DEFINED
) {
260 s
->vstart
= s
->start
;
261 s
->flags
|= VSTART_DEFINED
;
263 if (s
->flags
& ALIGN_DEFINED
) {
264 s
->valign
= s
->align
;
265 s
->flags
|= VALIGN_DEFINED
;
267 if (s
->flags
& FOLLOWS_DEFINED
) {
268 s
->vfollows
= s
->follows
;
269 s
->flags
|= VFOLLOWS_DEFINED
;
270 s
->flags
&= ~FOLLOWS_DEFINED
;
273 /* Every section must have a start address. */
274 if (s
->flags
& VSTART_DEFINED
) {
275 s
->start
= s
->vstart
;
276 s
->flags
|= START_DEFINED
;
278 /* Move the section into the nobits list. */
285 /* Step 2: Sort the progbits sections into their output order. */
287 /* In Step 2 we move around sections in groups. A group
288 * begins with a section (group leader) that has a user-
289 * defined start address or follows section. The remainder
290 * of the group is made up of the sections that implicitly
291 * follow the group leader (i.e., they were defined after
292 * the group leader and were not given an explicit start
293 * address or follows section by the user). */
295 /* For anyone attempting to read this code:
296 * g (group) points to a group of sections, the first one of which has
297 * a user-defined start address or follows section.
298 * gp (g previous) holds the location of the pointer to g.
299 * gs (g scan) is a temp variable that we use to scan to the end of the group.
300 * gsp (gs previous) holds the location of the pointer to gs.
301 * nt (nobits tail) points to the nobits section-list tail.
304 /* Link all 'follows' groups to their proper position. To do
305 * this we need to know three things: the start of the group
306 * to relocate (g), the section it is following (s), and the
307 * end of the group we're relocating (gs). */
308 for (gp
= §ions
, g
= sections
; g
; g
= gs
) { /* Find the next follows group that is out of place (g). */
309 if (!(g
->flags
& FOLLOWS_DEFINED
)) {
311 if ((g
->next
->flags
& FOLLOWS_DEFINED
) &&
312 strcmp(g
->name
, g
->next
->follows
))
321 /* Find the section that this group follows (s). */
322 for (sp
= §ions
, s
= sections
;
323 s
&& strcmp(s
->name
, g
->follows
);
324 sp
= &s
->next
, s
= s
->next
) ;
326 error(ERR_FATAL
, "section %s follows an invalid or"
327 " unknown section (%s)", g
->name
, g
->follows
);
328 if (s
->next
&& (s
->next
->flags
& FOLLOWS_DEFINED
) &&
329 !strcmp(s
->name
, s
->next
->follows
))
330 error(ERR_FATAL
, "sections %s and %s can't both follow"
331 " section %s", g
->name
, s
->next
->name
, s
->name
);
332 /* Find the end of the current follows group (gs). */
333 for (gsp
= &g
->next
, gs
= g
->next
;
334 gs
&& (gs
!= s
) && !(gs
->flags
& START_DEFINED
);
335 gsp
= &gs
->next
, gs
= gs
->next
) {
336 if (gs
->next
&& (gs
->next
->flags
& FOLLOWS_DEFINED
) &&
337 strcmp(gs
->name
, gs
->next
->follows
)) {
343 /* Re-link the group after its follows section. */
349 /* Link all 'start' groups to their proper position. Once
350 * again we need to know g, s, and gs (see above). The main
351 * difference is we already know g since we sort by moving
352 * groups from the 'unsorted' list into a 'sorted' list (g
353 * will always be the first section in the unsorted list). */
354 for (g
= sections
, sections
= NULL
; g
; g
= gs
) { /* Find the section that we will insert this group before (s). */
355 for (sp
= §ions
, s
= sections
; s
; sp
= &s
->next
, s
= s
->next
)
356 if ((s
->flags
& START_DEFINED
) && (g
->start
< s
->start
))
358 /* Find the end of the group (gs). */
359 for (gs
= g
->next
, gsp
= &g
->next
;
360 gs
&& !(gs
->flags
& START_DEFINED
);
361 gsp
= &gs
->next
, gs
= gs
->next
) ;
362 /* Re-link the group before the target section. */
367 /* Step 3: Compute start addresses for all progbits sections. */
369 /* Make sure we have an origin and a start address for the first section. */
371 switch (sections
->flags
& (START_DEFINED
| ALIGN_DEFINED
)) {
372 case START_DEFINED
| ALIGN_DEFINED
:
374 /* Make sure this section doesn't begin before the origin. */
375 if (sections
->start
< origin
)
376 error(ERR_FATAL
, "section %s begins"
377 " before program origin", sections
->name
);
380 sections
->start
= ((origin
+ sections
->align
- 1) &
381 ~(sections
->align
- 1));
384 sections
->start
= origin
;
386 if (!(sections
->flags
& START_DEFINED
))
388 origin
= sections
->start
;
390 sections
->flags
|= START_DEFINED
;
392 /* Make sure each section has an explicit start address. If it
393 * doesn't, then compute one based its alignment and the end of
394 * the previous section. */
395 for (pend
= sections
->start
, g
= s
= sections
; g
; g
= g
->next
) { /* Find the next section that could cause an overlap situation
396 * (has a defined start address, and is not zero length). */
399 s
&& ((s
->length
== 0) || !(s
->flags
& START_DEFINED
));
401 /* Compute the start address of this section, if necessary. */
402 if (!(g
->flags
& START_DEFINED
)) { /* Default to an alignment of 4 if unspecified. */
403 if (!(g
->flags
& ALIGN_DEFINED
)) {
405 g
->flags
|= ALIGN_DEFINED
;
407 /* Set the section start address. */
408 g
->start
= (pend
+ g
->align
- 1) & ~(g
->align
- 1);
409 g
->flags
|= START_DEFINED
;
411 /* Ugly special case for progbits sections' virtual attributes:
412 * If there is a defined valign, but no vstart and no vfollows, then
413 * we valign after the previous progbits section. This case doesn't
414 * really make much sense for progbits sections with a defined start
415 * address, but it is possible and we must do *something*.
416 * Not-so-ugly special case:
417 * If a progbits section has no virtual attributes, we set the
418 * vstart equal to the start address. */
419 if (!(g
->flags
& (VSTART_DEFINED
| VFOLLOWS_DEFINED
))) {
420 if (g
->flags
& VALIGN_DEFINED
)
421 g
->vstart
= (pend
+ g
->valign
- 1) & ~(g
->valign
- 1);
423 g
->vstart
= g
->start
;
424 g
->flags
|= VSTART_DEFINED
;
426 /* Ignore zero-length sections. */
429 /* Compute the span of this section. */
430 pend
= g
->start
+ g
->length
;
431 /* Check for section overlap. */
433 if (g
->start
> s
->start
)
434 error(ERR_FATAL
, "sections %s ~ %s and %s overlap!",
435 gs
->name
, g
->name
, s
->name
);
437 error(ERR_FATAL
, "sections %s and %s overlap!",
440 /* Remember this section as the latest >0 length section. */
444 /* Step 4: Compute vstart addresses for all sections. */
446 /* Attach the nobits sections to the end of the progbits sections. */
447 for (s
= sections
; s
->next
; s
= s
->next
) ;
450 /* Scan for sections that don't have a vstart address. If we find one we'll
451 * attempt to compute its vstart. If we can't compute the vstart, we leave
452 * it alone and come back to it in a subsequent scan. We continue scanning
453 * and re-scanning until we've gone one full cycle without computing any
455 do { /* Do one full scan of the sections list. */
456 for (h
= 0, g
= sections
; g
; g
= g
->next
) {
457 if (g
->flags
& VSTART_DEFINED
)
459 /* Find the section that this one virtually follows. */
460 if (g
->flags
& VFOLLOWS_DEFINED
) {
461 for (s
= sections
; s
&& strcmp(g
->vfollows
, s
->name
);
465 "section %s vfollows unknown section (%s)",
466 g
->name
, g
->vfollows
);
467 } else if (g
->ifollows
!= NULL
)
468 for (s
= sections
; s
&& (s
!= g
->ifollows
); s
= s
->next
) ;
469 /* The .bss section is the only one with ifollows = NULL. In this case we
470 * implicitly follow the last progbits section. */
474 /* If the section we're following has a vstart, we can proceed. */
475 if (s
->flags
& VSTART_DEFINED
) { /* Default to virtual alignment of four. */
476 if (!(g
->flags
& VALIGN_DEFINED
)) {
478 g
->flags
|= VALIGN_DEFINED
;
480 /* Compute the vstart address. */
482 (s
->vstart
+ s
->length
+ g
->valign
- 1) & ~(g
->valign
-
484 g
->flags
|= VSTART_DEFINED
;
486 /* Start and vstart mean the same thing for nobits sections. */
487 if (g
->flags
& TYPE_NOBITS
)
488 g
->start
= g
->vstart
;
493 /* Now check for any circular vfollows references, which will manifest
494 * themselves as sections without a defined vstart. */
495 for (h
= 0, s
= sections
; s
; s
= s
->next
) {
496 if (!(s
->flags
& VSTART_DEFINED
)) { /* Non-fatal errors after assembly has completed are generally a
497 * no-no, but we'll throw a fatal one eventually so it's ok. */
498 error(ERR_NONFATAL
, "cannot compute vstart for section %s",
504 error(ERR_FATAL
, "circular vfollows path detected");
508 "bin_cleanup: Confirm final section order for output file:\n");
509 for (h
= 0, s
= sections
; s
&& (s
->flags
& TYPE_PROGBITS
);
511 fprintf(stdout
, "%i. %s\n", h
, s
->name
);
514 /* Step 5: Apply relocations. */
516 /* Prepare the sections for relocating. */
517 for (s
= sections
; s
; s
= s
->next
)
518 saa_rewind(s
->contents
);
519 /* Apply relocations. */
520 for (r
= relocs
; r
; r
= r
->next
) {
521 uint8_t *p
, *q
, mydata
[8];
524 saa_fread(r
->target
->contents
, r
->posn
, mydata
, r
->bytes
);
529 l
+= ((int64_t)*p
++) << 8;
531 l
+= ((int64_t)*p
++) << 16;
532 l
+= ((int64_t)*p
++) << 24;
535 l
+= ((int64_t)*p
++) << 32;
536 l
+= ((int64_t)*p
++) << 40;
537 l
+= ((int64_t)*p
++) << 48;
538 l
+= ((int64_t)*p
++) << 56;
542 s
= find_section_by_index(r
->secref
);
544 if (r
->secref
== s
->start_index
)
549 s
= find_section_by_index(r
->secrel
);
551 if (r
->secrel
== s
->start_index
)
559 else if (r
->bytes
== 2)
562 *q
++ = (uint8_t)(l
& 0xFF);
563 saa_fwrite(r
->target
->contents
, r
->posn
, mydata
, r
->bytes
);
566 /* Step 6: Write the section data to the output file. */
568 /* Write the progbits sections to the output file. */
569 for (pend
= origin
, s
= sections
; s
&& (s
->flags
& TYPE_PROGBITS
); s
= s
->next
) { /* Skip zero-length sections. */
572 /* Pad the space between sections. */
573 for (h
= s
->start
- pend
; h
; h
--)
575 /* Write the section to the output file. */
577 saa_fpwrite(s
->contents
, fp
);
578 pend
= s
->start
+ s
->length
;
580 /* Done writing the file, so close it. */
583 /* Step 7: Generate the map file. */
586 const int8_t *not_defined
= { "not defined" };
588 /* Display input and output file names. */
589 fprintf(rf
, "\n- NASM Map file ");
592 fprintf(rf
, "\n\nSource file: %s\nOutput file: %s\n\n",
595 if (map_control
& MAP_ORIGIN
) { /* Display program origin. */
596 fprintf(rf
, "-- Program origin ");
599 fprintf(rf
, "\n\n%08lX\n\n", origin
);
601 /* Display sections summary. */
602 if (map_control
& MAP_SUMMARY
) {
603 fprintf(rf
, "-- Sections (summary) ");
606 fprintf(rf
, "\n\nVstart Start Stop "
607 "Length Class Name\n");
608 for (s
= sections
; s
; s
= s
->next
) {
609 fprintf(rf
, "%08lX %08lX %08lX %08lX ",
610 s
->vstart
, s
->start
, s
->start
+ s
->length
,
612 if (s
->flags
& TYPE_PROGBITS
)
613 fprintf(rf
, "progbits ");
615 fprintf(rf
, "nobits ");
616 fprintf(rf
, "%s\n", s
->name
);
620 /* Display detailed section information. */
621 if (map_control
& MAP_SECTIONS
) {
622 fprintf(rf
, "-- Sections (detailed) ");
626 for (s
= sections
; s
; s
= s
->next
) {
627 fprintf(rf
, "---- Section %s ", s
->name
);
628 for (h
= 65 - strlen(s
->name
); h
; h
--)
630 fprintf(rf
, "\n\nclass: ");
631 if (s
->flags
& TYPE_PROGBITS
)
632 fprintf(rf
, "progbits");
634 fprintf(rf
, "nobits");
635 fprintf(rf
, "\nlength: %08lX\nstart: %08lX"
636 "\nalign: ", s
->length
, s
->start
);
637 if (s
->flags
& ALIGN_DEFINED
)
638 fprintf(rf
, "%08lX", s
->align
);
640 fprintf(rf
, not_defined
);
641 fprintf(rf
, "\nfollows: ");
642 if (s
->flags
& FOLLOWS_DEFINED
)
643 fprintf(rf
, "%s", s
->follows
);
645 fprintf(rf
, not_defined
);
646 fprintf(rf
, "\nvstart: %08lX\nvalign: ", s
->vstart
);
647 if (s
->flags
& VALIGN_DEFINED
)
648 fprintf(rf
, "%08lX", s
->valign
);
650 fprintf(rf
, not_defined
);
651 fprintf(rf
, "\nvfollows: ");
652 if (s
->flags
& VFOLLOWS_DEFINED
)
653 fprintf(rf
, "%s", s
->vfollows
);
655 fprintf(rf
, not_defined
);
659 /* Display symbols information. */
660 if (map_control
& MAP_SYMBOLS
) {
661 int32_t segment
, offset
;
663 fprintf(rf
, "-- Symbols ");
668 fprintf(rf
, "---- No Section ");
671 fprintf(rf
, "\n\nValue Name\n");
672 for (l
= no_seg_labels
; l
; l
= l
->next
) {
673 lookup_label(l
->name
, &segment
, &offset
);
674 fprintf(rf
, "%08lX %s\n", offset
, l
->name
);
678 for (s
= sections
; s
; s
= s
->next
) {
680 fprintf(rf
, "---- Section %s ", s
->name
);
681 for (h
= 65 - strlen(s
->name
); h
; h
--)
683 fprintf(rf
, "\n\nReal Virtual Name\n");
684 for (l
= s
->labels
; l
; l
= l
->next
) {
685 lookup_label(l
->name
, &segment
, &offset
);
686 fprintf(rf
, "%08lX %08lX %s\n",
687 s
->start
+ offset
, s
->vstart
+ offset
,
696 /* Close the report file. */
697 if (map_control
&& (rf
!= stdout
) && (rf
!= stderr
))
700 /* Step 8: Release all allocated memory. */
702 /* Free sections, label pointer structs, etc.. */
706 saa_free(s
->contents
);
708 if (s
->flags
& FOLLOWS_DEFINED
)
709 nasm_free(s
->follows
);
710 if (s
->flags
& VFOLLOWS_DEFINED
)
711 nasm_free(s
->vfollows
);
720 /* Free no-section labels. */
721 while (no_seg_labels
) {
723 no_seg_labels
= l
->next
;
727 /* Free relocation structures. */
735 static void bin_out(int32_t segto
, const void *data
, uint32_t type
,
736 int32_t segment
, int32_t wrt
)
738 uint8_t *p
, mydata
[8];
744 wrt
= NO_SEG
; /* continue to do _something_ */
745 error(ERR_NONFATAL
, "WRT not supported by binary output format");
748 /* Handle absolute-assembly (structure definitions). */
749 if (segto
== NO_SEG
) {
750 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
751 error(ERR_NONFATAL
, "attempt to assemble code in"
752 " [ABSOLUTE] space");
756 /* Find the segment we are targeting. */
757 s
= find_section_by_index(segto
);
759 error(ERR_PANIC
, "code directed to nonexistent segment?");
761 /* "Smart" section-type adaptation code. */
762 if (!(s
->flags
& TYPE_DEFINED
)) {
763 if ((type
& OUT_TYPMASK
) == OUT_RESERVE
)
764 s
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
766 s
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
769 if ((s
->flags
& TYPE_NOBITS
) && ((type
& OUT_TYPMASK
) != OUT_RESERVE
))
770 error(ERR_WARNING
, "attempt to initialize memory in a"
771 " nobits section: ignored");
773 if ((type
& OUT_TYPMASK
) == OUT_ADDRESS
) {
774 if (segment
!= NO_SEG
&& !find_section_by_index(segment
)) {
776 error(ERR_NONFATAL
, "binary output format does not support"
777 " segment base references");
779 error(ERR_NONFATAL
, "binary output format does not support"
780 " external references");
783 if (s
->flags
& TYPE_PROGBITS
) {
784 if (segment
!= NO_SEG
)
785 add_reloc(s
, type
& OUT_SIZMASK
, segment
, -1L);
787 if ((type
& OUT_SIZMASK
) == 4)
788 WRITELONG(p
, *(int32_t *)data
);
789 else if ((type
& OUT_SIZMASK
) == 8)
790 WRITEDLONG(p
, *(int64_t *)data
);
792 WRITESHORT(p
, *(int32_t *)data
);
793 saa_wbytes(s
->contents
, mydata
, type
& OUT_SIZMASK
);
795 s
->length
+= type
& OUT_SIZMASK
;
796 } else if ((type
& OUT_TYPMASK
) == OUT_RAWDATA
) {
798 if (s
->flags
& TYPE_PROGBITS
)
799 saa_wbytes(s
->contents
, data
, type
);
801 } else if ((type
& OUT_TYPMASK
) == OUT_RESERVE
) {
803 if (s
->flags
& TYPE_PROGBITS
) {
804 error(ERR_WARNING
, "uninitialized space declared in"
805 " %s section: zeroing", s
->name
);
806 saa_wbytes(s
->contents
, NULL
, type
);
809 } else if ((type
& OUT_TYPMASK
) == OUT_REL2ADR
||
810 (type
& OUT_TYPMASK
) == OUT_REL4ADR
) {
811 realbytes
= (type
& OUT_TYPMASK
);
812 if (realbytes
== OUT_REL2ADR
)
816 if (segment
!= NO_SEG
&& !find_section_by_index(segment
)) {
818 error(ERR_NONFATAL
, "binary output format does not support"
819 " segment base references");
821 error(ERR_NONFATAL
, "binary output format does not support"
822 " external references");
825 if (s
->flags
& TYPE_PROGBITS
) {
826 add_reloc(s
, realbytes
, segment
, segto
);
829 WRITELONG(p
, *(int32_t *)data
- realbytes
- s
->length
);
831 WRITESHORT(p
, *(int32_t *)data
- realbytes
- s
->length
);
832 saa_wbytes(s
->contents
, mydata
, realbytes
);
834 s
->length
+= realbytes
;
838 static void bin_deflabel(int8_t *name
, int32_t segment
, int32_t offset
,
839 int is_global
, int8_t *special
)
841 (void)segment
; /* Don't warn that this parameter is unused */
842 (void)offset
; /* Don't warn that this parameter is unused */
845 error(ERR_NONFATAL
, "binary format does not support any"
846 " special symbol types");
847 else if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@')
848 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
849 else if (is_global
== 2)
850 error(ERR_NONFATAL
, "binary output format does not support common"
854 struct bin_label
***ltp
;
856 /* Remember label definition so we can look it up later when
857 * creating the map file. */
858 s
= find_section_by_index(segment
);
860 ltp
= &(s
->labels_end
);
863 (**ltp
) = nasm_malloc(sizeof(struct bin_label
));
864 (**ltp
)->name
= name
;
865 (**ltp
)->next
= NULL
;
866 *ltp
= &((**ltp
)->next
);
871 /* These constants and the following function are used
872 * by bin_secname() to parse attribute assignments. */
874 enum { ATTRIB_START
, ATTRIB_ALIGN
, ATTRIB_FOLLOWS
,
875 ATTRIB_VSTART
, ATTRIB_VALIGN
, ATTRIB_VFOLLOWS
,
876 ATTRIB_NOBITS
, ATTRIB_PROGBITS
879 static int bin_read_attribute(int8_t **line
, int *attribute
,
883 int attrib_name_size
;
884 struct tokenval tokval
;
887 /* Skip whitespace. */
888 while (**line
&& isspace(**line
))
893 /* Figure out what attribute we're reading. */
894 if (!nasm_strnicmp(*line
, "align=", 6)) {
895 *attribute
= ATTRIB_ALIGN
;
896 attrib_name_size
= 6;
897 } else if (format_mode
) {
898 if (!nasm_strnicmp(*line
, "start=", 6)) {
899 *attribute
= ATTRIB_START
;
900 attrib_name_size
= 6;
901 } else if (!nasm_strnicmp(*line
, "follows=", 8)) {
902 *attribute
= ATTRIB_FOLLOWS
;
905 } else if (!nasm_strnicmp(*line
, "vstart=", 7)) {
906 *attribute
= ATTRIB_VSTART
;
907 attrib_name_size
= 7;
908 } else if (!nasm_strnicmp(*line
, "valign=", 7)) {
909 *attribute
= ATTRIB_VALIGN
;
910 attrib_name_size
= 7;
911 } else if (!nasm_strnicmp(*line
, "vfollows=", 9)) {
912 *attribute
= ATTRIB_VFOLLOWS
;
915 } else if (!nasm_strnicmp(*line
, "nobits", 6) &&
916 (isspace((*line
)[6]) || ((*line
)[6] == '\0'))) {
917 *attribute
= ATTRIB_NOBITS
;
920 } else if (!nasm_strnicmp(*line
, "progbits", 8) &&
921 (isspace((*line
)[8]) || ((*line
)[8] == '\0'))) {
922 *attribute
= ATTRIB_PROGBITS
;
930 /* Find the end of the expression. */
931 if ((*line
)[attrib_name_size
] != '(') {
932 /* Single term (no parenthesis). */
933 exp
= *line
+= attrib_name_size
;
934 while (**line
&& !isspace(**line
))
944 /* Full expression (delimited by parenthesis) */
945 exp
= *line
+= attrib_name_size
+ 1;
947 (*line
) += strcspn(*line
, "()'\"");
958 if ((**line
== '"') || (**line
== '\'')) {
967 "invalid syntax in `section' directive");
973 error(ERR_NONFATAL
, "expecting `)'");
977 *(*line
- 1) = '\0'; /* Terminate the expression. */
980 /* Check for no value given. */
982 error(ERR_WARNING
, "No value given to attribute in"
983 " `section' directive");
987 /* Read and evaluate the expression. */
989 stdscan_bufptr
= exp
;
990 tokval
.t_type
= TOKEN_INVALID
;
991 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
993 if (!is_really_simple(e
)) {
994 error(ERR_NONFATAL
, "section attribute value must be"
995 " a critical expression");
999 error(ERR_NONFATAL
, "Invalid attribute value"
1000 " specified in `section' directive.");
1003 *value
= (uint32_t)reloc_value(e
);
1007 static void bin_assign_attributes(struct Section
*sec
, int8_t *astring
)
1009 int attribute
, check
;
1013 while (1) { /* Get the next attribute. */
1014 check
= bin_read_attribute(&astring
, &attribute
, &value
);
1015 /* Skip bad attribute. */
1018 /* Unknown section attribute, so skip it and warn the user. */
1021 break; /* End of line. */
1024 while (*astring
&& !isspace(*astring
))
1030 error(ERR_WARNING
, "ignoring unknown section attribute:"
1036 switch (attribute
) { /* Handle nobits attribute. */
1038 if ((sec
->flags
& TYPE_DEFINED
)
1039 && (sec
->flags
& TYPE_PROGBITS
))
1041 "attempt to change section type"
1042 " from progbits to nobits");
1044 sec
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
1047 /* Handle progbits attribute. */
1048 case ATTRIB_PROGBITS
:
1049 if ((sec
->flags
& TYPE_DEFINED
) && (sec
->flags
& TYPE_NOBITS
))
1050 error(ERR_NONFATAL
, "attempt to change section type"
1051 " from nobits to progbits");
1053 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1056 /* Handle align attribute. */
1058 if (!format_mode
&& (!strcmp(sec
->name
, ".text")))
1059 error(ERR_NONFATAL
, "cannot specify an alignment"
1060 " to the .text section");
1062 if (!value
|| ((value
- 1) & value
))
1063 error(ERR_NONFATAL
, "argument to `align' is not a"
1065 else { /* Alignment is already satisfied if the previous
1066 * align value is greater. */
1067 if ((sec
->flags
& ALIGN_DEFINED
)
1068 && (value
< sec
->align
))
1071 /* Don't allow a conflicting align value. */
1072 if ((sec
->flags
& START_DEFINED
)
1073 && (sec
->start
& (value
- 1)))
1075 "`align' value conflicts "
1076 "with section start address");
1079 sec
->flags
|= ALIGN_DEFINED
;
1085 /* Handle valign attribute. */
1087 if (!value
|| ((value
- 1) & value
))
1088 error(ERR_NONFATAL
, "argument to `valign' is not a"
1090 else { /* Alignment is already satisfied if the previous
1091 * align value is greater. */
1092 if ((sec
->flags
& VALIGN_DEFINED
) && (value
< sec
->valign
))
1093 value
= sec
->valign
;
1095 /* Don't allow a conflicting valign value. */
1096 if ((sec
->flags
& VSTART_DEFINED
)
1097 && (sec
->vstart
& (value
- 1)))
1099 "`valign' value conflicts "
1100 "with `vstart' address");
1102 sec
->valign
= value
;
1103 sec
->flags
|= VALIGN_DEFINED
;
1108 /* Handle start attribute. */
1110 if (sec
->flags
& FOLLOWS_DEFINED
)
1111 error(ERR_NONFATAL
, "cannot combine `start' and `follows'"
1112 " section attributes");
1114 error(ERR_NONFATAL
, "attempt to specify a negative"
1115 " section start address");
1116 else if ((sec
->flags
& START_DEFINED
) && (value
!= sec
->start
))
1117 error(ERR_NONFATAL
, "section start address redefined");
1120 sec
->flags
|= START_DEFINED
;
1121 if (sec
->flags
& ALIGN_DEFINED
) {
1122 if (sec
->start
& (sec
->align
- 1))
1123 error(ERR_NONFATAL
, "`start' address conflicts"
1124 " with section alignment");
1125 sec
->flags
^= ALIGN_DEFINED
;
1130 /* Handle vstart attribute. */
1132 if (sec
->flags
& VFOLLOWS_DEFINED
)
1134 "cannot combine `vstart' and `vfollows'"
1135 " section attributes");
1136 else if ((sec
->flags
& VSTART_DEFINED
)
1137 && (value
!= sec
->vstart
))
1139 "section virtual start address"
1140 " (vstart) redefined");
1142 sec
->vstart
= value
;
1143 sec
->flags
|= VSTART_DEFINED
;
1144 if (sec
->flags
& VALIGN_DEFINED
) {
1145 if (sec
->vstart
& (sec
->valign
- 1))
1146 error(ERR_NONFATAL
, "`vstart' address conflicts"
1147 " with `valign' value");
1148 sec
->flags
^= VALIGN_DEFINED
;
1153 /* Handle follows attribute. */
1154 case ATTRIB_FOLLOWS
:
1156 astring
+= strcspn(astring
, " \t");
1158 error(ERR_NONFATAL
, "expecting section name for `follows'"
1161 *(astring
++) = '\0';
1162 if (sec
->flags
& START_DEFINED
)
1164 "cannot combine `start' and `follows'"
1165 " section attributes");
1166 sec
->follows
= nasm_strdup(p
);
1167 sec
->flags
|= FOLLOWS_DEFINED
;
1171 /* Handle vfollows attribute. */
1172 case ATTRIB_VFOLLOWS
:
1173 if (sec
->flags
& VSTART_DEFINED
)
1175 "cannot combine `vstart' and `vfollows'"
1176 " section attributes");
1179 astring
+= strcspn(astring
, " \t");
1182 "expecting section name for `vfollows'"
1185 *(astring
++) = '\0';
1186 sec
->vfollows
= nasm_strdup(p
);
1187 sec
->flags
|= VFOLLOWS_DEFINED
;
1195 static void bin_define_section_labels()
1197 static int labels_defined
= 0;
1198 struct Section
*sec
;
1204 for (sec
= sections
; sec
; sec
= sec
->next
) {
1205 base_len
= strlen(sec
->name
) + 8;
1206 label_name
= nasm_malloc(base_len
+ 8);
1207 strcpy(label_name
, "section.");
1208 strcpy(label_name
+ 8, sec
->name
);
1210 /* section.<name>.start */
1211 strcpy(label_name
+ base_len
, ".start");
1212 define_label(label_name
, sec
->start_index
, 0L,
1213 NULL
, 0, 0, bin_get_ofmt(), error
);
1215 /* section.<name>.vstart */
1216 strcpy(label_name
+ base_len
, ".vstart");
1217 define_label(label_name
, sec
->vstart_index
, 0L,
1218 NULL
, 0, 0, bin_get_ofmt(), error
);
1220 nasm_free(label_name
);
1225 static int32_t bin_secname(int8_t *name
, int pass
, int *bits
)
1228 struct Section
*sec
;
1230 /* bin_secname is called with *name = NULL at the start of each
1231 * pass. Use this opportunity to establish the default section
1232 * (default is BITS-16 ".text" segment).
1234 if (!name
) { /* Reset ORG and section attributes at the start of each pass. */
1236 for (sec
= sections
; sec
; sec
= sec
->next
)
1237 sec
->flags
&= ~(START_DEFINED
| VSTART_DEFINED
|
1238 ALIGN_DEFINED
| VALIGN_DEFINED
);
1240 /* Define section start and vstart labels. */
1241 if (format_mode
&& (pass
!= 1))
1242 bin_define_section_labels();
1244 /* Establish the default (.text) section. */
1246 sec
= find_section_by_name(".text");
1247 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1248 current_section
= sec
->vstart_index
;
1249 return current_section
;
1252 /* Attempt to find the requested section. If it does not
1253 * exist, create it. */
1255 while (*p
&& !isspace(*p
))
1259 sec
= find_section_by_name(name
);
1261 sec
= create_section(name
);
1262 if (!strcmp(name
, ".data"))
1263 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1264 else if (!strcmp(name
, ".bss")) {
1265 sec
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
1266 sec
->ifollows
= NULL
;
1267 } else if (!format_mode
) {
1268 error(ERR_NONFATAL
, "section name must be "
1269 ".text, .data, or .bss");
1270 return current_section
;
1274 /* Handle attribute assignments. */
1276 bin_assign_attributes(sec
, p
);
1278 #ifndef ABIN_SMART_ADAPT
1279 /* The following line disables smart adaptation of
1280 * PROGBITS/NOBITS section types (it forces sections to
1281 * default to PROGBITS). */
1282 if ((pass
!= 1) && !(sec
->flags
& TYPE_DEFINED
))
1283 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1286 /* Set the current section and return. */
1287 current_section
= sec
->vstart_index
;
1288 return current_section
;
1291 static int bin_directive(int8_t *directive
, int8_t *args
, int pass
)
1293 /* Handle ORG directive */
1294 if (!nasm_stricmp(directive
, "org")) {
1295 struct tokenval tokval
;
1300 stdscan_bufptr
= args
;
1301 tokval
.t_type
= TOKEN_INVALID
;
1302 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
1304 if (!is_really_simple(e
))
1305 error(ERR_NONFATAL
, "org value must be a critical"
1308 value
= reloc_value(e
);
1309 /* Check for ORG redefinition. */
1310 if (origin_defined
&& (value
!= origin
))
1311 error(ERR_NONFATAL
, "program origin redefined");
1318 error(ERR_NONFATAL
, "No or invalid offset specified"
1319 " in ORG directive.");
1323 /* The 'map' directive allows the user to generate section
1324 * and symbol information to stdout, stderr, or to a file. */
1325 else if (format_mode
&& !nasm_stricmp(directive
, "map")) {
1330 args
+= strspn(args
, " \t");
1333 args
+= strcspn(args
, " \t");
1336 if (!nasm_stricmp(p
, "all"))
1338 MAP_ORIGIN
| MAP_SUMMARY
| MAP_SECTIONS
| MAP_SYMBOLS
;
1339 else if (!nasm_stricmp(p
, "brief"))
1340 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
;
1341 else if (!nasm_stricmp(p
, "sections"))
1342 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
| MAP_SECTIONS
;
1343 else if (!nasm_stricmp(p
, "segments"))
1344 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
| MAP_SECTIONS
;
1345 else if (!nasm_stricmp(p
, "symbols"))
1346 map_control
|= MAP_SYMBOLS
;
1348 if (!nasm_stricmp(p
, "stdout"))
1350 else if (!nasm_stricmp(p
, "stderr"))
1352 else { /* Must be a filename. */
1353 rf
= fopen(p
, "wt");
1355 error(ERR_WARNING
, "unable to open map file `%s'",
1362 error(ERR_WARNING
, "map file already specified");
1364 if (map_control
== 0)
1365 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
;
1373 static void bin_filename(int8_t *inname
, int8_t *outname
, efunc error
)
1375 standard_extension(inname
, outname
, "", error
);
1380 static int32_t bin_segbase(int32_t segment
)
1385 static int bin_set_info(enum geninfo type
, int8_t **val
)
1390 static void bin_init(FILE * afp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
1395 (void)eval
; /* Don't warn that this parameter is unused. */
1396 (void)ldef
; /* Placate optimizers. */
1398 maxbits
= 64; /* Support 64-bit Segments */
1400 reloctail
= &relocs
;
1402 no_seg_labels
= NULL
;
1403 nsl_tail
= &no_seg_labels
;
1404 format_mode
= 1; /* Extended bin format
1405 * (set this to zero for old bin format). */
1407 /* Create default section (.text). */
1408 sections
= last_section
= nasm_malloc(sizeof(struct Section
));
1409 last_section
->next
= NULL
;
1410 last_section
->name
= nasm_strdup(".text");
1411 last_section
->contents
= saa_init(1L);
1412 last_section
->follows
= last_section
->vfollows
= 0;
1413 last_section
->ifollows
= NULL
;
1414 last_section
->length
= 0;
1415 last_section
->flags
= TYPE_DEFINED
| TYPE_PROGBITS
;
1416 last_section
->labels
= NULL
;
1417 last_section
->labels_end
= &(last_section
->labels
);
1418 last_section
->start_index
= seg_alloc();
1419 last_section
->vstart_index
= current_section
= seg_alloc();
1422 struct ofmt of_bin
= {
1423 "flat-form binary files (e.g. DOS .COM, .SYS)",
1440 /* This is needed for bin_define_section_labels() */
1441 struct ofmt
*bin_get_ofmt()
1446 #endif /* #ifdef OF_BIN */