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 license given in the file "LICENSE"
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
64 struct ofmt
*bin_get_ofmt(); /* Prototype goes here since no header file. */
66 static FILE *fp
, *rf
= NULL
;
69 /* Section flags keep track of which attributes the user has defined. */
70 #define START_DEFINED 0x001
71 #define ALIGN_DEFINED 0x002
72 #define FOLLOWS_DEFINED 0x004
73 #define VSTART_DEFINED 0x008
74 #define VALIGN_DEFINED 0x010
75 #define VFOLLOWS_DEFINED 0x020
76 #define TYPE_DEFINED 0x040
77 #define TYPE_PROGBITS 0x080
78 #define TYPE_NOBITS 0x100
80 /* This struct is used to keep track of symbols for map-file generation. */
81 static struct bin_label
{
83 struct bin_label
*next
;
84 } *no_seg_labels
, **nsl_tail
;
86 static struct Section
{
89 int64_t length
; /* section length in bytes */
91 /* Section attributes */
92 int flags
; /* see flag definitions above */
93 uint64_t align
; /* section alignment */
94 uint64_t valign
; /* notional section alignment */
95 uint64_t start
; /* section start address */
96 uint64_t vstart
; /* section virtual start address */
97 char *follows
; /* the section that this one will follow */
98 char *vfollows
; /* the section that this one will notionally follow */
99 int32_t start_index
; /* NASM section id for non-relocated version */
100 int32_t vstart_index
; /* the NASM section id */
102 struct bin_label
*labels
; /* linked-list of label handles for map output. */
103 struct bin_label
**labels_end
; /* Holds address of end of labels list. */
104 struct Section
*ifollows
; /* Points to previous section (implicit follows). */
105 struct Section
*next
; /* This links sections with a defined start address. */
107 /* The extended bin format allows for sections to have a "virtual"
108 * start address. This is accomplished by creating two sections:
109 * one beginning at the Load Memory Address and the other beginning
110 * at the Virtual Memory Address. The LMA section is only used to
111 * define the section.<section_name>.start label, but there isn't
112 * any other good way for us to handle that label.
115 } *sections
, *last_section
;
117 static struct Reloc
{
123 struct Section
*target
;
124 } *relocs
, **reloctail
;
126 extern char *stdscan_bufptr
;
128 static uint8_t format_mode
; /* 0 = original bin, 1 = extended bin */
129 static int32_t current_section
; /* only really needed if format_mode = 0 */
130 static uint64_t origin
;
131 static int origin_defined
;
133 /* Stuff we need for map-file generation. */
135 #define MAP_SUMMARY 2
136 #define MAP_SECTIONS 4
137 #define MAP_SYMBOLS 8
138 static int map_control
= 0;
139 static char *infile
, *outfile
;
141 static const char *bin_stdmac
[] = {
142 "%define __SECT__ [section .text]",
143 "%imacro org 1+.nolist",
146 "%macro __NASM_CDecl__ 1",
151 static void add_reloc(struct Section
*s
, int32_t bytes
, int32_t secref
,
156 r
= *reloctail
= nasm_malloc(sizeof(struct Reloc
));
157 reloctail
= &r
->next
;
166 static struct Section
*find_section_by_name(const char *name
)
170 for (s
= sections
; s
; s
= s
->next
)
171 if (!strcmp(s
->name
, name
))
176 static struct Section
*find_section_by_index(int32_t index
)
180 for (s
= sections
; s
; s
= s
->next
)
181 if ((index
== s
->vstart_index
) || (index
== s
->start_index
))
186 static struct Section
*create_section(char *name
)
187 { /* Create a new section. */
188 last_section
->next
= nasm_malloc(sizeof(struct Section
));
189 last_section
->next
->ifollows
= last_section
;
190 last_section
= last_section
->next
;
191 last_section
->labels
= NULL
;
192 last_section
->labels_end
= &(last_section
->labels
);
194 /* Initialize section attributes. */
195 last_section
->name
= nasm_strdup(name
);
196 last_section
->contents
= saa_init(1L);
197 last_section
->follows
= last_section
->vfollows
= 0;
198 last_section
->length
= 0;
199 last_section
->flags
= 0;
200 last_section
->next
= NULL
;
202 /* Register our sections with NASM. */
203 last_section
->vstart_index
= seg_alloc();
204 last_section
->start_index
= seg_alloc();
208 static void bin_cleanup(int debuginfo
)
210 struct Section
*g
, **gp
;
211 struct Section
*gs
= NULL
, **gsp
;
212 struct Section
*s
, **sp
;
213 struct Section
*nobits
= NULL
, **nt
;
214 struct Section
*last_progbits
;
220 (void)debuginfo
; /* placate optimizers */
224 "bin_cleanup: Sections were initially referenced in this order:\n");
225 for (h
= 0, s
= sections
; s
; h
++, s
= s
->next
)
226 fprintf(stdout
, "%i. %s\n", h
, s
->name
);
229 /* Assembly has completed, so now we need to generate the output file.
230 * Step 1: Separate progbits and nobits sections into separate lists.
231 * Step 2: Sort the progbits sections into their output order.
232 * Step 3: Compute start addresses for all progbits sections.
233 * Step 4: Compute vstart addresses for all sections.
234 * Step 5: Apply relocations.
235 * Step 6: Write the sections' data to the output file.
236 * Step 7: Generate the map file.
237 * Step 8: Release all allocated memory.
240 /* To do: Smart section-type adaptation could leave some empty sections
241 * without a defined type (progbits/nobits). Won't fix now since this
242 * feature will be disabled. */
244 /* Step 1: Split progbits and nobits sections into separate lists. */
247 /* Move nobits sections into a separate list. Also pre-process nobits
248 * sections' attributes. */
249 for (sp
= §ions
->next
, s
= sections
->next
; s
; s
= *sp
) { /* Skip progbits sections. */
250 if (s
->flags
& TYPE_PROGBITS
) {
254 /* Do some special pre-processing on nobits sections' attributes. */
255 if (s
->flags
& (START_DEFINED
| ALIGN_DEFINED
| FOLLOWS_DEFINED
)) { /* Check for a mixture of real and virtual section attributes. */
257 flags
& (VSTART_DEFINED
| VALIGN_DEFINED
|
260 "cannot mix real and virtual attributes"
261 " in nobits section (%s)", s
->name
);
262 /* Real and virtual attributes mean the same thing for nobits sections. */
263 if (s
->flags
& START_DEFINED
) {
264 s
->vstart
= s
->start
;
265 s
->flags
|= VSTART_DEFINED
;
267 if (s
->flags
& ALIGN_DEFINED
) {
268 s
->valign
= s
->align
;
269 s
->flags
|= VALIGN_DEFINED
;
271 if (s
->flags
& FOLLOWS_DEFINED
) {
272 s
->vfollows
= s
->follows
;
273 s
->flags
|= VFOLLOWS_DEFINED
;
274 s
->flags
&= ~FOLLOWS_DEFINED
;
277 /* Every section must have a start address. */
278 if (s
->flags
& VSTART_DEFINED
) {
279 s
->start
= s
->vstart
;
280 s
->flags
|= START_DEFINED
;
282 /* Move the section into the nobits list. */
289 /* Step 2: Sort the progbits sections into their output order. */
291 /* In Step 2 we move around sections in groups. A group
292 * begins with a section (group leader) that has a user-
293 * defined start address or follows section. The remainder
294 * of the group is made up of the sections that implicitly
295 * follow the group leader (i.e., they were defined after
296 * the group leader and were not given an explicit start
297 * address or follows section by the user). */
299 /* For anyone attempting to read this code:
300 * g (group) points to a group of sections, the first one of which has
301 * a user-defined start address or follows section.
302 * gp (g previous) holds the location of the pointer to g.
303 * gs (g scan) is a temp variable that we use to scan to the end of the group.
304 * gsp (gs previous) holds the location of the pointer to gs.
305 * nt (nobits tail) points to the nobits section-list tail.
308 /* Link all 'follows' groups to their proper position. To do
309 * this we need to know three things: the start of the group
310 * to relocate (g), the section it is following (s), and the
311 * end of the group we're relocating (gs). */
312 for (gp
= §ions
, g
= sections
; g
; g
= gs
) { /* Find the next follows group that is out of place (g). */
313 if (!(g
->flags
& FOLLOWS_DEFINED
)) {
315 if ((g
->next
->flags
& FOLLOWS_DEFINED
) &&
316 strcmp(g
->name
, g
->next
->follows
))
325 /* Find the section that this group follows (s). */
326 for (sp
= §ions
, s
= sections
;
327 s
&& strcmp(s
->name
, g
->follows
);
328 sp
= &s
->next
, s
= s
->next
) ;
330 error(ERR_FATAL
, "section %s follows an invalid or"
331 " unknown section (%s)", g
->name
, g
->follows
);
332 if (s
->next
&& (s
->next
->flags
& FOLLOWS_DEFINED
) &&
333 !strcmp(s
->name
, s
->next
->follows
))
334 error(ERR_FATAL
, "sections %s and %s can't both follow"
335 " section %s", g
->name
, s
->next
->name
, s
->name
);
336 /* Find the end of the current follows group (gs). */
337 for (gsp
= &g
->next
, gs
= g
->next
;
338 gs
&& (gs
!= s
) && !(gs
->flags
& START_DEFINED
);
339 gsp
= &gs
->next
, gs
= gs
->next
) {
340 if (gs
->next
&& (gs
->next
->flags
& FOLLOWS_DEFINED
) &&
341 strcmp(gs
->name
, gs
->next
->follows
)) {
347 /* Re-link the group after its follows section. */
353 /* Link all 'start' groups to their proper position. Once
354 * again we need to know g, s, and gs (see above). The main
355 * difference is we already know g since we sort by moving
356 * groups from the 'unsorted' list into a 'sorted' list (g
357 * will always be the first section in the unsorted list). */
358 for (g
= sections
, sections
= NULL
; g
; g
= gs
) { /* Find the section that we will insert this group before (s). */
359 for (sp
= §ions
, s
= sections
; s
; sp
= &s
->next
, s
= s
->next
)
360 if ((s
->flags
& START_DEFINED
) && (g
->start
< s
->start
))
362 /* Find the end of the group (gs). */
363 for (gs
= g
->next
, gsp
= &g
->next
;
364 gs
&& !(gs
->flags
& START_DEFINED
);
365 gsp
= &gs
->next
, gs
= gs
->next
) ;
366 /* Re-link the group before the target section. */
371 /* Step 3: Compute start addresses for all progbits sections. */
373 /* Make sure we have an origin and a start address for the first section. */
375 switch (sections
->flags
& (START_DEFINED
| ALIGN_DEFINED
)) {
376 case START_DEFINED
| ALIGN_DEFINED
:
378 /* Make sure this section doesn't begin before the origin. */
379 if (sections
->start
< origin
)
380 error(ERR_FATAL
, "section %s begins"
381 " before program origin", sections
->name
);
384 sections
->start
= ((origin
+ sections
->align
- 1) &
385 ~(sections
->align
- 1));
388 sections
->start
= origin
;
390 if (!(sections
->flags
& START_DEFINED
))
392 origin
= sections
->start
;
394 sections
->flags
|= START_DEFINED
;
396 /* Make sure each section has an explicit start address. If it
397 * doesn't, then compute one based its alignment and the end of
398 * the previous section. */
399 for (pend
= sections
->start
, g
= s
= sections
; g
; g
= g
->next
) { /* Find the next section that could cause an overlap situation
400 * (has a defined start address, and is not zero length). */
403 s
&& ((s
->length
== 0) || !(s
->flags
& START_DEFINED
));
405 /* Compute the start address of this section, if necessary. */
406 if (!(g
->flags
& START_DEFINED
)) { /* Default to an alignment of 4 if unspecified. */
407 if (!(g
->flags
& ALIGN_DEFINED
)) {
409 g
->flags
|= ALIGN_DEFINED
;
411 /* Set the section start address. */
412 g
->start
= (pend
+ g
->align
- 1) & ~(g
->align
- 1);
413 g
->flags
|= START_DEFINED
;
415 /* Ugly special case for progbits sections' virtual attributes:
416 * If there is a defined valign, but no vstart and no vfollows, then
417 * we valign after the previous progbits section. This case doesn't
418 * really make much sense for progbits sections with a defined start
419 * address, but it is possible and we must do *something*.
420 * Not-so-ugly special case:
421 * If a progbits section has no virtual attributes, we set the
422 * vstart equal to the start address. */
423 if (!(g
->flags
& (VSTART_DEFINED
| VFOLLOWS_DEFINED
))) {
424 if (g
->flags
& VALIGN_DEFINED
)
425 g
->vstart
= (pend
+ g
->valign
- 1) & ~(g
->valign
- 1);
427 g
->vstart
= g
->start
;
428 g
->flags
|= VSTART_DEFINED
;
430 /* Ignore zero-length sections. */
433 /* Compute the span of this section. */
434 pend
= g
->start
+ g
->length
;
435 /* Check for section overlap. */
437 if (g
->start
> s
->start
)
438 error(ERR_FATAL
, "sections %s ~ %s and %s overlap!",
439 gs
->name
, g
->name
, s
->name
);
441 error(ERR_FATAL
, "sections %s and %s overlap!",
444 /* Remember this section as the latest >0 length section. */
448 /* Step 4: Compute vstart addresses for all sections. */
450 /* Attach the nobits sections to the end of the progbits sections. */
451 for (s
= sections
; s
->next
; s
= s
->next
) ;
454 /* Scan for sections that don't have a vstart address. If we find one we'll
455 * attempt to compute its vstart. If we can't compute the vstart, we leave
456 * it alone and come back to it in a subsequent scan. We continue scanning
457 * and re-scanning until we've gone one full cycle without computing any
459 do { /* Do one full scan of the sections list. */
460 for (h
= 0, g
= sections
; g
; g
= g
->next
) {
461 if (g
->flags
& VSTART_DEFINED
)
463 /* Find the section that this one virtually follows. */
464 if (g
->flags
& VFOLLOWS_DEFINED
) {
465 for (s
= sections
; s
&& strcmp(g
->vfollows
, s
->name
);
469 "section %s vfollows unknown section (%s)",
470 g
->name
, g
->vfollows
);
471 } else if (g
->ifollows
!= NULL
)
472 for (s
= sections
; s
&& (s
!= g
->ifollows
); s
= s
->next
) ;
473 /* The .bss section is the only one with ifollows = NULL. In this case we
474 * implicitly follow the last progbits section. */
478 /* If the section we're following has a vstart, we can proceed. */
479 if (s
->flags
& VSTART_DEFINED
) { /* Default to virtual alignment of four. */
480 if (!(g
->flags
& VALIGN_DEFINED
)) {
482 g
->flags
|= VALIGN_DEFINED
;
484 /* Compute the vstart address. */
486 (s
->vstart
+ s
->length
+ g
->valign
- 1) & ~(g
->valign
-
488 g
->flags
|= VSTART_DEFINED
;
490 /* Start and vstart mean the same thing for nobits sections. */
491 if (g
->flags
& TYPE_NOBITS
)
492 g
->start
= g
->vstart
;
497 /* Now check for any circular vfollows references, which will manifest
498 * themselves as sections without a defined vstart. */
499 for (h
= 0, s
= sections
; s
; s
= s
->next
) {
500 if (!(s
->flags
& VSTART_DEFINED
)) { /* Non-fatal errors after assembly has completed are generally a
501 * no-no, but we'll throw a fatal one eventually so it's ok. */
502 error(ERR_NONFATAL
, "cannot compute vstart for section %s",
508 error(ERR_FATAL
, "circular vfollows path detected");
512 "bin_cleanup: Confirm final section order for output file:\n");
513 for (h
= 0, s
= sections
; s
&& (s
->flags
& TYPE_PROGBITS
);
515 fprintf(stdout
, "%i. %s\n", h
, s
->name
);
518 /* Step 5: Apply relocations. */
520 /* Prepare the sections for relocating. */
521 for (s
= sections
; s
; s
= s
->next
)
522 saa_rewind(s
->contents
);
523 /* Apply relocations. */
524 for (r
= relocs
; r
; r
= r
->next
) {
525 uint8_t *p
, *q
, mydata
[8];
528 saa_fread(r
->target
->contents
, r
->posn
, mydata
, r
->bytes
);
533 l
+= ((int64_t)*p
++) << 8;
535 l
+= ((int64_t)*p
++) << 16;
536 l
+= ((int64_t)*p
++) << 24;
539 l
+= ((int64_t)*p
++) << 32;
540 l
+= ((int64_t)*p
++) << 40;
541 l
+= ((int64_t)*p
++) << 48;
542 l
+= ((int64_t)*p
++) << 56;
546 s
= find_section_by_index(r
->secref
);
548 if (r
->secref
== s
->start_index
)
553 s
= find_section_by_index(r
->secrel
);
555 if (r
->secrel
== s
->start_index
)
563 else if (r
->bytes
== 2)
566 *q
++ = (uint8_t)(l
& 0xFF);
567 saa_fwrite(r
->target
->contents
, r
->posn
, mydata
, r
->bytes
);
570 /* Step 6: Write the section data to the output file. */
572 /* Write the progbits sections to the output file. */
573 for (pend
= origin
, s
= sections
; s
&& (s
->flags
& TYPE_PROGBITS
); s
= s
->next
) { /* Skip zero-length sections. */
576 /* Pad the space between sections. */
577 for (h
= s
->start
- pend
; h
; h
--)
579 /* Write the section to the output file. */
581 saa_fpwrite(s
->contents
, fp
);
582 pend
= s
->start
+ s
->length
;
584 /* Done writing the file, so close it. */
587 /* Step 7: Generate the map file. */
590 const char *not_defined
= { "not defined" };
592 /* Display input and output file names. */
593 fprintf(rf
, "\n- NASM Map file ");
596 fprintf(rf
, "\n\nSource file: %s\nOutput file: %s\n\n",
599 if (map_control
& MAP_ORIGIN
) { /* Display program origin. */
600 fprintf(rf
, "-- Program origin ");
603 fprintf(rf
, "\n\n%08"PRIX64
"\n\n", origin
);
605 /* Display sections summary. */
606 if (map_control
& MAP_SUMMARY
) {
607 fprintf(rf
, "-- Sections (summary) ");
610 fprintf(rf
, "\n\nVstart Start Stop "
611 "Length Class Name\n");
612 for (s
= sections
; s
; s
= s
->next
) {
613 fprintf(rf
, "%16"PRIX64
" %16"PRIX64
" %16"PRIX64
" %08"PRIX64
" ",
614 s
->vstart
, s
->start
, s
->start
+ s
->length
,
616 if (s
->flags
& TYPE_PROGBITS
)
617 fprintf(rf
, "progbits ");
619 fprintf(rf
, "nobits ");
620 fprintf(rf
, "%s\n", s
->name
);
624 /* Display detailed section information. */
625 if (map_control
& MAP_SECTIONS
) {
626 fprintf(rf
, "-- Sections (detailed) ");
630 for (s
= sections
; s
; s
= s
->next
) {
631 fprintf(rf
, "---- Section %s ", s
->name
);
632 for (h
= 65 - strlen(s
->name
); h
; h
--)
634 fprintf(rf
, "\n\nclass: ");
635 if (s
->flags
& TYPE_PROGBITS
)
636 fprintf(rf
, "progbits");
638 fprintf(rf
, "nobits");
639 fprintf(rf
, "\nlength: %16"PRIX64
"\nstart: %16"PRIX64
""
640 "\nalign: ", s
->length
, s
->start
);
641 if (s
->flags
& ALIGN_DEFINED
)
642 fprintf(rf
, "%16"PRIX64
"", s
->align
);
644 fprintf(rf
, not_defined
);
645 fprintf(rf
, "\nfollows: ");
646 if (s
->flags
& FOLLOWS_DEFINED
)
647 fprintf(rf
, "%s", s
->follows
);
649 fprintf(rf
, not_defined
);
650 fprintf(rf
, "\nvstart: %16"PRIX64
"\nvalign: ", s
->vstart
);
651 if (s
->flags
& VALIGN_DEFINED
)
652 fprintf(rf
, "%16"PRIX64
"", s
->valign
);
654 fprintf(rf
, not_defined
);
655 fprintf(rf
, "\nvfollows: ");
656 if (s
->flags
& VFOLLOWS_DEFINED
)
657 fprintf(rf
, "%s", s
->vfollows
);
659 fprintf(rf
, not_defined
);
663 /* Display symbols information. */
664 if (map_control
& MAP_SYMBOLS
) {
668 fprintf(rf
, "-- Symbols ");
673 fprintf(rf
, "---- No Section ");
676 fprintf(rf
, "\n\nValue Name\n");
677 for (l
= no_seg_labels
; l
; l
= l
->next
) {
678 lookup_label(l
->name
, &segment
, &offset
);
679 fprintf(rf
, "%08"PRIX64
" %s\n", offset
, l
->name
);
683 for (s
= sections
; s
; s
= s
->next
) {
685 fprintf(rf
, "---- Section %s ", s
->name
);
686 for (h
= 65 - strlen(s
->name
); h
; h
--)
688 fprintf(rf
, "\n\nReal Virtual Name\n");
689 for (l
= s
->labels
; l
; l
= l
->next
) {
690 lookup_label(l
->name
, &segment
, &offset
);
691 fprintf(rf
, "%16"PRIX64
" %16"PRIX64
" %s\n",
692 s
->start
+ offset
, s
->vstart
+ offset
,
701 /* Close the report file. */
702 if (map_control
&& (rf
!= stdout
) && (rf
!= stderr
))
705 /* Step 8: Release all allocated memory. */
707 /* Free sections, label pointer structs, etc.. */
711 saa_free(s
->contents
);
713 if (s
->flags
& FOLLOWS_DEFINED
)
714 nasm_free(s
->follows
);
715 if (s
->flags
& VFOLLOWS_DEFINED
)
716 nasm_free(s
->vfollows
);
725 /* Free no-section labels. */
726 while (no_seg_labels
) {
728 no_seg_labels
= l
->next
;
732 /* Free relocation structures. */
740 static void bin_out(int32_t segto
, const void *data
,
741 enum out_type type
, uint64_t size
,
742 int32_t segment
, int32_t wrt
)
744 uint8_t *p
, mydata
[8];
748 wrt
= NO_SEG
; /* continue to do _something_ */
749 error(ERR_NONFATAL
, "WRT not supported by binary output format");
752 /* Handle absolute-assembly (structure definitions). */
753 if (segto
== NO_SEG
) {
754 if (type
!= OUT_RESERVE
)
755 error(ERR_NONFATAL
, "attempt to assemble code in"
756 " [ABSOLUTE] space");
760 /* Find the segment we are targeting. */
761 s
= find_section_by_index(segto
);
763 error(ERR_PANIC
, "code directed to nonexistent segment?");
765 /* "Smart" section-type adaptation code. */
766 if (!(s
->flags
& TYPE_DEFINED
)) {
767 if (type
== OUT_RESERVE
)
768 s
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
770 s
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
773 if ((s
->flags
& TYPE_NOBITS
) && (type
!= OUT_RESERVE
))
774 error(ERR_WARNING
, "attempt to initialize memory in a"
775 " nobits section: ignored");
777 if (type
== OUT_ADDRESS
) {
778 if (segment
!= NO_SEG
&& !find_section_by_index(segment
)) {
780 error(ERR_NONFATAL
, "binary output format does not support"
781 " segment base references");
783 error(ERR_NONFATAL
, "binary output format does not support"
784 " external references");
787 if (s
->flags
& TYPE_PROGBITS
) {
788 if (segment
!= NO_SEG
)
789 add_reloc(s
, size
, segment
, -1L);
791 WRITEADDR(p
, *(int64_t *)data
, size
);
792 saa_wbytes(s
->contents
, mydata
, size
);
795 } else if (type
== OUT_RAWDATA
) {
796 if (s
->flags
& TYPE_PROGBITS
)
797 saa_wbytes(s
->contents
, data
, size
);
799 } else if (type
== OUT_RESERVE
) {
800 if (s
->flags
& TYPE_PROGBITS
) {
801 error(ERR_WARNING
, "uninitialized space declared in"
802 " %s section: zeroing", s
->name
);
803 saa_wbytes(s
->contents
, NULL
, size
);
806 } else if (type
== OUT_REL2ADR
|| type
== OUT_REL4ADR
||
807 type
== OUT_REL8ADR
) {
819 size
= 0; /* Shut up warning */
822 if (segment
!= NO_SEG
&& !find_section_by_index(segment
)) {
824 error(ERR_NONFATAL
, "binary output format does not support"
825 " segment base references");
827 error(ERR_NONFATAL
, "binary output format does not support"
828 " external references");
831 if (s
->flags
& TYPE_PROGBITS
) {
832 add_reloc(s
, size
, segment
, segto
);
834 WRITEADDR(p
, *(int64_t *)data
- size
- s
->length
, size
);
835 saa_wbytes(s
->contents
, mydata
, size
);
841 static void bin_deflabel(char *name
, int32_t segment
, int64_t offset
,
842 int is_global
, char *special
)
844 (void)segment
; /* Don't warn that this parameter is unused */
845 (void)offset
; /* Don't warn that this parameter is unused */
848 error(ERR_NONFATAL
, "binary format does not support any"
849 " special symbol types");
850 else if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@')
851 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
852 else if (is_global
== 2)
853 error(ERR_NONFATAL
, "binary output format does not support common"
857 struct bin_label
***ltp
;
859 /* Remember label definition so we can look it up later when
860 * creating the map file. */
861 s
= find_section_by_index(segment
);
863 ltp
= &(s
->labels_end
);
866 (**ltp
) = nasm_malloc(sizeof(struct bin_label
));
867 (**ltp
)->name
= name
;
868 (**ltp
)->next
= NULL
;
869 *ltp
= &((**ltp
)->next
);
874 /* These constants and the following function are used
875 * by bin_secname() to parse attribute assignments. */
877 enum { ATTRIB_START
, ATTRIB_ALIGN
, ATTRIB_FOLLOWS
,
878 ATTRIB_VSTART
, ATTRIB_VALIGN
, ATTRIB_VFOLLOWS
,
879 ATTRIB_NOBITS
, ATTRIB_PROGBITS
882 static int bin_read_attribute(char **line
, int *attribute
,
886 int attrib_name_size
;
887 struct tokenval tokval
;
890 /* Skip whitespace. */
891 while (**line
&& isspace(**line
))
896 /* Figure out what attribute we're reading. */
897 if (!nasm_strnicmp(*line
, "align=", 6)) {
898 *attribute
= ATTRIB_ALIGN
;
899 attrib_name_size
= 6;
900 } else if (format_mode
) {
901 if (!nasm_strnicmp(*line
, "start=", 6)) {
902 *attribute
= ATTRIB_START
;
903 attrib_name_size
= 6;
904 } else if (!nasm_strnicmp(*line
, "follows=", 8)) {
905 *attribute
= ATTRIB_FOLLOWS
;
908 } else if (!nasm_strnicmp(*line
, "vstart=", 7)) {
909 *attribute
= ATTRIB_VSTART
;
910 attrib_name_size
= 7;
911 } else if (!nasm_strnicmp(*line
, "valign=", 7)) {
912 *attribute
= ATTRIB_VALIGN
;
913 attrib_name_size
= 7;
914 } else if (!nasm_strnicmp(*line
, "vfollows=", 9)) {
915 *attribute
= ATTRIB_VFOLLOWS
;
918 } else if (!nasm_strnicmp(*line
, "nobits", 6) &&
919 (isspace((*line
)[6]) || ((*line
)[6] == '\0'))) {
920 *attribute
= ATTRIB_NOBITS
;
923 } else if (!nasm_strnicmp(*line
, "progbits", 8) &&
924 (isspace((*line
)[8]) || ((*line
)[8] == '\0'))) {
925 *attribute
= ATTRIB_PROGBITS
;
933 /* Find the end of the expression. */
934 if ((*line
)[attrib_name_size
] != '(') {
935 /* Single term (no parenthesis). */
936 exp
= *line
+= attrib_name_size
;
937 while (**line
&& !isspace(**line
))
947 /* Full expression (delimited by parenthesis) */
948 exp
= *line
+= attrib_name_size
+ 1;
950 (*line
) += strcspn(*line
, "()'\"");
961 if ((**line
== '"') || (**line
== '\'')) {
970 "invalid syntax in `section' directive");
976 error(ERR_NONFATAL
, "expecting `)'");
980 *(*line
- 1) = '\0'; /* Terminate the expression. */
983 /* Check for no value given. */
985 error(ERR_WARNING
, "No value given to attribute in"
986 " `section' directive");
990 /* Read and evaluate the expression. */
992 stdscan_bufptr
= exp
;
993 tokval
.t_type
= TOKEN_INVALID
;
994 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
996 if (!is_really_simple(e
)) {
997 error(ERR_NONFATAL
, "section attribute value must be"
998 " a critical expression");
1002 error(ERR_NONFATAL
, "Invalid attribute value"
1003 " specified in `section' directive.");
1006 *value
= (uint64_t)reloc_value(e
);
1010 static void bin_assign_attributes(struct Section
*sec
, char *astring
)
1012 int attribute
, check
;
1016 while (1) { /* Get the next attribute. */
1017 check
= bin_read_attribute(&astring
, &attribute
, &value
);
1018 /* Skip bad attribute. */
1021 /* Unknown section attribute, so skip it and warn the user. */
1024 break; /* End of line. */
1027 while (*astring
&& !isspace(*astring
))
1033 error(ERR_WARNING
, "ignoring unknown section attribute:"
1039 switch (attribute
) { /* Handle nobits attribute. */
1041 if ((sec
->flags
& TYPE_DEFINED
)
1042 && (sec
->flags
& TYPE_PROGBITS
))
1044 "attempt to change section type"
1045 " from progbits to nobits");
1047 sec
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
1050 /* Handle progbits attribute. */
1051 case ATTRIB_PROGBITS
:
1052 if ((sec
->flags
& TYPE_DEFINED
) && (sec
->flags
& TYPE_NOBITS
))
1053 error(ERR_NONFATAL
, "attempt to change section type"
1054 " from nobits to progbits");
1056 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1059 /* Handle align attribute. */
1061 if (!format_mode
&& (!strcmp(sec
->name
, ".text")))
1062 error(ERR_NONFATAL
, "cannot specify an alignment"
1063 " to the .text section");
1065 if (!value
|| ((value
- 1) & value
))
1066 error(ERR_NONFATAL
, "argument to `align' is not a"
1068 else { /* Alignment is already satisfied if the previous
1069 * align value is greater. */
1070 if ((sec
->flags
& ALIGN_DEFINED
)
1071 && (value
< sec
->align
))
1074 /* Don't allow a conflicting align value. */
1075 if ((sec
->flags
& START_DEFINED
)
1076 && (sec
->start
& (value
- 1)))
1078 "`align' value conflicts "
1079 "with section start address");
1082 sec
->flags
|= ALIGN_DEFINED
;
1088 /* Handle valign attribute. */
1090 if (!value
|| ((value
- 1) & value
))
1091 error(ERR_NONFATAL
, "argument to `valign' is not a"
1093 else { /* Alignment is already satisfied if the previous
1094 * align value is greater. */
1095 if ((sec
->flags
& VALIGN_DEFINED
) && (value
< sec
->valign
))
1096 value
= sec
->valign
;
1098 /* Don't allow a conflicting valign value. */
1099 if ((sec
->flags
& VSTART_DEFINED
)
1100 && (sec
->vstart
& (value
- 1)))
1102 "`valign' value conflicts "
1103 "with `vstart' address");
1105 sec
->valign
= value
;
1106 sec
->flags
|= VALIGN_DEFINED
;
1111 /* Handle start attribute. */
1113 if (sec
->flags
& FOLLOWS_DEFINED
)
1114 error(ERR_NONFATAL
, "cannot combine `start' and `follows'"
1115 " section attributes");
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(void)
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(char *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(char *directive
, char *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(char *inname
, char *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
, char **val
)
1392 static void bin_init(FILE * afp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
1397 (void)eval
; /* Don't warn that this parameter is unused. */
1398 (void)ldef
; /* Placate optimizers. */
1400 maxbits
= 64; /* Support 64-bit Segments */
1402 reloctail
= &relocs
;
1404 no_seg_labels
= NULL
;
1405 nsl_tail
= &no_seg_labels
;
1406 format_mode
= 1; /* Extended bin format
1407 * (set this to zero for old bin format). */
1409 /* Create default section (.text). */
1410 sections
= last_section
= nasm_malloc(sizeof(struct Section
));
1411 last_section
->next
= NULL
;
1412 last_section
->name
= nasm_strdup(".text");
1413 last_section
->contents
= saa_init(1L);
1414 last_section
->follows
= last_section
->vfollows
= 0;
1415 last_section
->ifollows
= NULL
;
1416 last_section
->length
= 0;
1417 last_section
->flags
= TYPE_DEFINED
| TYPE_PROGBITS
;
1418 last_section
->labels
= NULL
;
1419 last_section
->labels_end
= &(last_section
->labels
);
1420 last_section
->start_index
= seg_alloc();
1421 last_section
->vstart_index
= current_section
= seg_alloc();
1424 struct ofmt of_bin
= {
1425 "flat-form binary files (e.g. DOS .COM, .SYS)",
1442 /* This is needed for bin_define_section_labels() */
1443 struct ofmt
*bin_get_ofmt(void)
1448 #endif /* #ifdef OF_BIN */