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
65 struct ofmt
*bin_get_ofmt(); /* Prototype goes here since no header file. */
67 static FILE *fp
, *rf
= NULL
;
70 /* Section flags keep track of which attributes the user has defined. */
71 #define START_DEFINED 0x001
72 #define ALIGN_DEFINED 0x002
73 #define FOLLOWS_DEFINED 0x004
74 #define VSTART_DEFINED 0x008
75 #define VALIGN_DEFINED 0x010
76 #define VFOLLOWS_DEFINED 0x020
77 #define TYPE_DEFINED 0x040
78 #define TYPE_PROGBITS 0x080
79 #define TYPE_NOBITS 0x100
81 /* This struct is used to keep track of symbols for map-file generation. */
82 static struct bin_label
{
84 struct bin_label
*next
;
85 } *no_seg_labels
, **nsl_tail
;
87 static struct Section
{
90 int64_t length
; /* section length in bytes */
92 /* Section attributes */
93 int flags
; /* see flag definitions above */
94 uint64_t align
; /* section alignment */
95 uint64_t valign
; /* notional section alignment */
96 uint64_t start
; /* section start address */
97 uint64_t vstart
; /* section virtual start address */
98 char *follows
; /* the section that this one will follow */
99 char *vfollows
; /* the section that this one will notionally follow */
100 int32_t start_index
; /* NASM section id for non-relocated version */
101 int32_t vstart_index
; /* the NASM section id */
103 struct bin_label
*labels
; /* linked-list of label handles for map output. */
104 struct bin_label
**labels_end
; /* Holds address of end of labels list. */
105 struct Section
*ifollows
; /* Points to previous section (implicit follows). */
106 struct Section
*next
; /* This links sections with a defined start address. */
108 /* The extended bin format allows for sections to have a "virtual"
109 * start address. This is accomplished by creating two sections:
110 * one beginning at the Load Memory Address and the other beginning
111 * at the Virtual Memory Address. The LMA section is only used to
112 * define the section.<section_name>.start label, but there isn't
113 * any other good way for us to handle that label.
116 } *sections
, *last_section
;
118 static struct Reloc
{
124 struct Section
*target
;
125 } *relocs
, **reloctail
;
127 extern char *stdscan_bufptr
;
129 static uint8_t format_mode
; /* 0 = original bin, 1 = extended bin */
130 static int32_t current_section
; /* only really needed if format_mode = 0 */
131 static uint64_t origin
;
132 static int origin_defined
;
134 /* Stuff we need for map-file generation. */
136 #define MAP_SUMMARY 2
137 #define MAP_SECTIONS 4
138 #define MAP_SYMBOLS 8
139 static int map_control
= 0;
140 static char *infile
, *outfile
;
142 static const char *bin_stdmac
[] = {
143 "%define __SECT__ [section .text]",
144 "%imacro org 1+.nolist",
147 "%macro __NASM_CDecl__ 1",
152 static void add_reloc(struct Section
*s
, int32_t bytes
, int32_t secref
,
157 r
= *reloctail
= nasm_malloc(sizeof(struct Reloc
));
158 reloctail
= &r
->next
;
167 static struct Section
*find_section_by_name(const char *name
)
171 for (s
= sections
; s
; s
= s
->next
)
172 if (!strcmp(s
->name
, name
))
177 static struct Section
*find_section_by_index(int32_t index
)
181 for (s
= sections
; s
; s
= s
->next
)
182 if ((index
== s
->vstart_index
) || (index
== s
->start_index
))
187 static struct Section
*create_section(char *name
)
188 { /* Create a new section. */
189 last_section
->next
= nasm_malloc(sizeof(struct Section
));
190 last_section
->next
->ifollows
= last_section
;
191 last_section
= last_section
->next
;
192 last_section
->labels
= NULL
;
193 last_section
->labels_end
= &(last_section
->labels
);
195 /* Initialize section attributes. */
196 last_section
->name
= nasm_strdup(name
);
197 last_section
->contents
= saa_init(1L);
198 last_section
->follows
= last_section
->vfollows
= 0;
199 last_section
->length
= 0;
200 last_section
->flags
= 0;
201 last_section
->next
= NULL
;
203 /* Register our sections with NASM. */
204 last_section
->vstart_index
= seg_alloc();
205 last_section
->start_index
= seg_alloc();
209 static void bin_cleanup(int debuginfo
)
211 struct Section
*g
, **gp
;
212 struct Section
*gs
= NULL
, **gsp
;
213 struct Section
*s
, **sp
;
214 struct Section
*nobits
= NULL
, **nt
;
215 struct Section
*last_progbits
;
221 (void)debuginfo
; /* placate optimizers */
225 "bin_cleanup: Sections were initially referenced in this order:\n");
226 for (h
= 0, s
= sections
; s
; h
++, s
= s
->next
)
227 fprintf(stdout
, "%i. %s\n", h
, s
->name
);
230 /* Assembly has completed, so now we need to generate the output file.
231 * Step 1: Separate progbits and nobits sections into separate lists.
232 * Step 2: Sort the progbits sections into their output order.
233 * Step 3: Compute start addresses for all progbits sections.
234 * Step 4: Compute vstart addresses for all sections.
235 * Step 5: Apply relocations.
236 * Step 6: Write the sections' data to the output file.
237 * Step 7: Generate the map file.
238 * Step 8: Release all allocated memory.
241 /* To do: Smart section-type adaptation could leave some empty sections
242 * without a defined type (progbits/nobits). Won't fix now since this
243 * feature will be disabled. */
245 /* Step 1: Split progbits and nobits sections into separate lists. */
248 /* Move nobits sections into a separate list. Also pre-process nobits
249 * sections' attributes. */
250 for (sp
= §ions
->next
, s
= sections
->next
; s
; s
= *sp
) { /* Skip progbits sections. */
251 if (s
->flags
& TYPE_PROGBITS
) {
255 /* Do some special pre-processing on nobits sections' attributes. */
256 if (s
->flags
& (START_DEFINED
| ALIGN_DEFINED
| FOLLOWS_DEFINED
)) { /* Check for a mixture of real and virtual section attributes. */
258 flags
& (VSTART_DEFINED
| VALIGN_DEFINED
|
261 "cannot mix real and virtual attributes"
262 " in nobits section (%s)", s
->name
);
263 /* Real and virtual attributes mean the same thing for nobits sections. */
264 if (s
->flags
& START_DEFINED
) {
265 s
->vstart
= s
->start
;
266 s
->flags
|= VSTART_DEFINED
;
268 if (s
->flags
& ALIGN_DEFINED
) {
269 s
->valign
= s
->align
;
270 s
->flags
|= VALIGN_DEFINED
;
272 if (s
->flags
& FOLLOWS_DEFINED
) {
273 s
->vfollows
= s
->follows
;
274 s
->flags
|= VFOLLOWS_DEFINED
;
275 s
->flags
&= ~FOLLOWS_DEFINED
;
278 /* Every section must have a start address. */
279 if (s
->flags
& VSTART_DEFINED
) {
280 s
->start
= s
->vstart
;
281 s
->flags
|= START_DEFINED
;
283 /* Move the section into the nobits list. */
290 /* Step 2: Sort the progbits sections into their output order. */
292 /* In Step 2 we move around sections in groups. A group
293 * begins with a section (group leader) that has a user-
294 * defined start address or follows section. The remainder
295 * of the group is made up of the sections that implicitly
296 * follow the group leader (i.e., they were defined after
297 * the group leader and were not given an explicit start
298 * address or follows section by the user). */
300 /* For anyone attempting to read this code:
301 * g (group) points to a group of sections, the first one of which has
302 * a user-defined start address or follows section.
303 * gp (g previous) holds the location of the pointer to g.
304 * gs (g scan) is a temp variable that we use to scan to the end of the group.
305 * gsp (gs previous) holds the location of the pointer to gs.
306 * nt (nobits tail) points to the nobits section-list tail.
309 /* Link all 'follows' groups to their proper position. To do
310 * this we need to know three things: the start of the group
311 * to relocate (g), the section it is following (s), and the
312 * end of the group we're relocating (gs). */
313 for (gp
= §ions
, g
= sections
; g
; g
= gs
) { /* Find the next follows group that is out of place (g). */
314 if (!(g
->flags
& FOLLOWS_DEFINED
)) {
316 if ((g
->next
->flags
& FOLLOWS_DEFINED
) &&
317 strcmp(g
->name
, g
->next
->follows
))
326 /* Find the section that this group follows (s). */
327 for (sp
= §ions
, s
= sections
;
328 s
&& strcmp(s
->name
, g
->follows
);
329 sp
= &s
->next
, s
= s
->next
) ;
331 error(ERR_FATAL
, "section %s follows an invalid or"
332 " unknown section (%s)", g
->name
, g
->follows
);
333 if (s
->next
&& (s
->next
->flags
& FOLLOWS_DEFINED
) &&
334 !strcmp(s
->name
, s
->next
->follows
))
335 error(ERR_FATAL
, "sections %s and %s can't both follow"
336 " section %s", g
->name
, s
->next
->name
, s
->name
);
337 /* Find the end of the current follows group (gs). */
338 for (gsp
= &g
->next
, gs
= g
->next
;
339 gs
&& (gs
!= s
) && !(gs
->flags
& START_DEFINED
);
340 gsp
= &gs
->next
, gs
= gs
->next
) {
341 if (gs
->next
&& (gs
->next
->flags
& FOLLOWS_DEFINED
) &&
342 strcmp(gs
->name
, gs
->next
->follows
)) {
348 /* Re-link the group after its follows section. */
354 /* Link all 'start' groups to their proper position. Once
355 * again we need to know g, s, and gs (see above). The main
356 * difference is we already know g since we sort by moving
357 * groups from the 'unsorted' list into a 'sorted' list (g
358 * will always be the first section in the unsorted list). */
359 for (g
= sections
, sections
= NULL
; g
; g
= gs
) { /* Find the section that we will insert this group before (s). */
360 for (sp
= §ions
, s
= sections
; s
; sp
= &s
->next
, s
= s
->next
)
361 if ((s
->flags
& START_DEFINED
) && (g
->start
< s
->start
))
363 /* Find the end of the group (gs). */
364 for (gs
= g
->next
, gsp
= &g
->next
;
365 gs
&& !(gs
->flags
& START_DEFINED
);
366 gsp
= &gs
->next
, gs
= gs
->next
) ;
367 /* Re-link the group before the target section. */
372 /* Step 3: Compute start addresses for all progbits sections. */
374 /* Make sure we have an origin and a start address for the first section. */
376 switch (sections
->flags
& (START_DEFINED
| ALIGN_DEFINED
)) {
377 case START_DEFINED
| ALIGN_DEFINED
:
379 /* Make sure this section doesn't begin before the origin. */
380 if (sections
->start
< origin
)
381 error(ERR_FATAL
, "section %s begins"
382 " before program origin", sections
->name
);
385 sections
->start
= ((origin
+ sections
->align
- 1) &
386 ~(sections
->align
- 1));
389 sections
->start
= origin
;
391 if (!(sections
->flags
& START_DEFINED
))
393 origin
= sections
->start
;
395 sections
->flags
|= START_DEFINED
;
397 /* Make sure each section has an explicit start address. If it
398 * doesn't, then compute one based its alignment and the end of
399 * the previous section. */
400 for (pend
= sections
->start
, g
= s
= sections
; g
; g
= g
->next
) { /* Find the next section that could cause an overlap situation
401 * (has a defined start address, and is not zero length). */
404 s
&& ((s
->length
== 0) || !(s
->flags
& START_DEFINED
));
406 /* Compute the start address of this section, if necessary. */
407 if (!(g
->flags
& START_DEFINED
)) { /* Default to an alignment of 4 if unspecified. */
408 if (!(g
->flags
& ALIGN_DEFINED
)) {
410 g
->flags
|= ALIGN_DEFINED
;
412 /* Set the section start address. */
413 g
->start
= (pend
+ g
->align
- 1) & ~(g
->align
- 1);
414 g
->flags
|= START_DEFINED
;
416 /* Ugly special case for progbits sections' virtual attributes:
417 * If there is a defined valign, but no vstart and no vfollows, then
418 * we valign after the previous progbits section. This case doesn't
419 * really make much sense for progbits sections with a defined start
420 * address, but it is possible and we must do *something*.
421 * Not-so-ugly special case:
422 * If a progbits section has no virtual attributes, we set the
423 * vstart equal to the start address. */
424 if (!(g
->flags
& (VSTART_DEFINED
| VFOLLOWS_DEFINED
))) {
425 if (g
->flags
& VALIGN_DEFINED
)
426 g
->vstart
= (pend
+ g
->valign
- 1) & ~(g
->valign
- 1);
428 g
->vstart
= g
->start
;
429 g
->flags
|= VSTART_DEFINED
;
431 /* Ignore zero-length sections. */
434 /* Compute the span of this section. */
435 pend
= g
->start
+ g
->length
;
436 /* Check for section overlap. */
438 if (g
->start
> s
->start
)
439 error(ERR_FATAL
, "sections %s ~ %s and %s overlap!",
440 gs
->name
, g
->name
, s
->name
);
442 error(ERR_FATAL
, "sections %s and %s overlap!",
445 /* Remember this section as the latest >0 length section. */
449 /* Step 4: Compute vstart addresses for all sections. */
451 /* Attach the nobits sections to the end of the progbits sections. */
452 for (s
= sections
; s
->next
; s
= s
->next
) ;
455 /* Scan for sections that don't have a vstart address. If we find one we'll
456 * attempt to compute its vstart. If we can't compute the vstart, we leave
457 * it alone and come back to it in a subsequent scan. We continue scanning
458 * and re-scanning until we've gone one full cycle without computing any
460 do { /* Do one full scan of the sections list. */
461 for (h
= 0, g
= sections
; g
; g
= g
->next
) {
462 if (g
->flags
& VSTART_DEFINED
)
464 /* Find the section that this one virtually follows. */
465 if (g
->flags
& VFOLLOWS_DEFINED
) {
466 for (s
= sections
; s
&& strcmp(g
->vfollows
, s
->name
);
470 "section %s vfollows unknown section (%s)",
471 g
->name
, g
->vfollows
);
472 } else if (g
->ifollows
!= NULL
)
473 for (s
= sections
; s
&& (s
!= g
->ifollows
); s
= s
->next
) ;
474 /* The .bss section is the only one with ifollows = NULL. In this case we
475 * implicitly follow the last progbits section. */
479 /* If the section we're following has a vstart, we can proceed. */
480 if (s
->flags
& VSTART_DEFINED
) { /* Default to virtual alignment of four. */
481 if (!(g
->flags
& VALIGN_DEFINED
)) {
483 g
->flags
|= VALIGN_DEFINED
;
485 /* Compute the vstart address. */
487 (s
->vstart
+ s
->length
+ g
->valign
- 1) & ~(g
->valign
-
489 g
->flags
|= VSTART_DEFINED
;
491 /* Start and vstart mean the same thing for nobits sections. */
492 if (g
->flags
& TYPE_NOBITS
)
493 g
->start
= g
->vstart
;
498 /* Now check for any circular vfollows references, which will manifest
499 * themselves as sections without a defined vstart. */
500 for (h
= 0, s
= sections
; s
; s
= s
->next
) {
501 if (!(s
->flags
& VSTART_DEFINED
)) { /* Non-fatal errors after assembly has completed are generally a
502 * no-no, but we'll throw a fatal one eventually so it's ok. */
503 error(ERR_NONFATAL
, "cannot compute vstart for section %s",
509 error(ERR_FATAL
, "circular vfollows path detected");
513 "bin_cleanup: Confirm final section order for output file:\n");
514 for (h
= 0, s
= sections
; s
&& (s
->flags
& TYPE_PROGBITS
);
516 fprintf(stdout
, "%i. %s\n", h
, s
->name
);
519 /* Step 5: Apply relocations. */
521 /* Prepare the sections for relocating. */
522 for (s
= sections
; s
; s
= s
->next
)
523 saa_rewind(s
->contents
);
524 /* Apply relocations. */
525 for (r
= relocs
; r
; r
= r
->next
) {
526 uint8_t *p
, *q
, mydata
[8];
529 saa_fread(r
->target
->contents
, r
->posn
, mydata
, r
->bytes
);
534 l
+= ((int64_t)*p
++) << 8;
536 l
+= ((int64_t)*p
++) << 16;
537 l
+= ((int64_t)*p
++) << 24;
540 l
+= ((int64_t)*p
++) << 32;
541 l
+= ((int64_t)*p
++) << 40;
542 l
+= ((int64_t)*p
++) << 48;
543 l
+= ((int64_t)*p
++) << 56;
547 s
= find_section_by_index(r
->secref
);
549 if (r
->secref
== s
->start_index
)
554 s
= find_section_by_index(r
->secrel
);
556 if (r
->secrel
== s
->start_index
)
564 else if (r
->bytes
== 2)
567 *q
++ = (uint8_t)(l
& 0xFF);
568 saa_fwrite(r
->target
->contents
, r
->posn
, mydata
, r
->bytes
);
571 /* Step 6: Write the section data to the output file. */
573 /* Write the progbits sections to the output file. */
574 for (pend
= origin
, s
= sections
; s
&& (s
->flags
& TYPE_PROGBITS
); s
= s
->next
) { /* Skip zero-length sections. */
577 /* Pad the space between sections. */
578 for (h
= s
->start
- pend
; h
; h
--)
580 /* Write the section to the output file. */
582 saa_fpwrite(s
->contents
, fp
);
583 pend
= s
->start
+ s
->length
;
585 /* Done writing the file, so close it. */
588 /* Step 7: Generate the map file. */
591 const char *not_defined
= { "not defined" };
593 /* Display input and output file names. */
594 fprintf(rf
, "\n- NASM Map file ");
597 fprintf(rf
, "\n\nSource file: %s\nOutput file: %s\n\n",
600 if (map_control
& MAP_ORIGIN
) { /* Display program origin. */
601 fprintf(rf
, "-- Program origin ");
604 fprintf(rf
, "\n\n%08"PRIX64
"\n\n", origin
);
606 /* Display sections summary. */
607 if (map_control
& MAP_SUMMARY
) {
608 fprintf(rf
, "-- Sections (summary) ");
611 fprintf(rf
, "\n\nVstart Start Stop "
612 "Length Class Name\n");
613 for (s
= sections
; s
; s
= s
->next
) {
614 fprintf(rf
, "%16"PRIX64
" %16"PRIX64
" %16"PRIX64
" %08"PRIX64
" ",
615 s
->vstart
, s
->start
, s
->start
+ s
->length
,
617 if (s
->flags
& TYPE_PROGBITS
)
618 fprintf(rf
, "progbits ");
620 fprintf(rf
, "nobits ");
621 fprintf(rf
, "%s\n", s
->name
);
625 /* Display detailed section information. */
626 if (map_control
& MAP_SECTIONS
) {
627 fprintf(rf
, "-- Sections (detailed) ");
631 for (s
= sections
; s
; s
= s
->next
) {
632 fprintf(rf
, "---- Section %s ", s
->name
);
633 for (h
= 65 - strlen(s
->name
); h
; h
--)
635 fprintf(rf
, "\n\nclass: ");
636 if (s
->flags
& TYPE_PROGBITS
)
637 fprintf(rf
, "progbits");
639 fprintf(rf
, "nobits");
640 fprintf(rf
, "\nlength: %16"PRIX64
"\nstart: %16"PRIX64
""
641 "\nalign: ", s
->length
, s
->start
);
642 if (s
->flags
& ALIGN_DEFINED
)
643 fprintf(rf
, "%16"PRIX64
"", s
->align
);
645 fprintf(rf
, not_defined
);
646 fprintf(rf
, "\nfollows: ");
647 if (s
->flags
& FOLLOWS_DEFINED
)
648 fprintf(rf
, "%s", s
->follows
);
650 fprintf(rf
, not_defined
);
651 fprintf(rf
, "\nvstart: %16"PRIX64
"\nvalign: ", s
->vstart
);
652 if (s
->flags
& VALIGN_DEFINED
)
653 fprintf(rf
, "%16"PRIX64
"", s
->valign
);
655 fprintf(rf
, not_defined
);
656 fprintf(rf
, "\nvfollows: ");
657 if (s
->flags
& VFOLLOWS_DEFINED
)
658 fprintf(rf
, "%s", s
->vfollows
);
660 fprintf(rf
, not_defined
);
664 /* Display symbols information. */
665 if (map_control
& MAP_SYMBOLS
) {
669 fprintf(rf
, "-- Symbols ");
674 fprintf(rf
, "---- No Section ");
677 fprintf(rf
, "\n\nValue Name\n");
678 for (l
= no_seg_labels
; l
; l
= l
->next
) {
679 lookup_label(l
->name
, &segment
, &offset
);
680 fprintf(rf
, "%08"PRIX64
" %s\n", offset
, l
->name
);
684 for (s
= sections
; s
; s
= s
->next
) {
686 fprintf(rf
, "---- Section %s ", s
->name
);
687 for (h
= 65 - strlen(s
->name
); h
; h
--)
689 fprintf(rf
, "\n\nReal Virtual Name\n");
690 for (l
= s
->labels
; l
; l
= l
->next
) {
691 lookup_label(l
->name
, &segment
, &offset
);
692 fprintf(rf
, "%16"PRIX64
" %16"PRIX64
" %s\n",
693 s
->start
+ offset
, s
->vstart
+ offset
,
702 /* Close the report file. */
703 if (map_control
&& (rf
!= stdout
) && (rf
!= stderr
))
706 /* Step 8: Release all allocated memory. */
708 /* Free sections, label pointer structs, etc.. */
712 saa_free(s
->contents
);
714 if (s
->flags
& FOLLOWS_DEFINED
)
715 nasm_free(s
->follows
);
716 if (s
->flags
& VFOLLOWS_DEFINED
)
717 nasm_free(s
->vfollows
);
726 /* Free no-section labels. */
727 while (no_seg_labels
) {
729 no_seg_labels
= l
->next
;
733 /* Free relocation structures. */
741 static void bin_out(int32_t segto
, const void *data
,
742 enum out_type type
, uint64_t size
,
743 int32_t segment
, int32_t wrt
)
745 uint8_t *p
, mydata
[8];
749 wrt
= NO_SEG
; /* continue to do _something_ */
750 error(ERR_NONFATAL
, "WRT not supported by binary output format");
753 /* Handle absolute-assembly (structure definitions). */
754 if (segto
== NO_SEG
) {
755 if (type
!= OUT_RESERVE
)
756 error(ERR_NONFATAL
, "attempt to assemble code in"
757 " [ABSOLUTE] space");
761 /* Find the segment we are targeting. */
762 s
= find_section_by_index(segto
);
764 error(ERR_PANIC
, "code directed to nonexistent segment?");
766 /* "Smart" section-type adaptation code. */
767 if (!(s
->flags
& TYPE_DEFINED
)) {
768 if (type
== OUT_RESERVE
)
769 s
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
771 s
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
774 if ((s
->flags
& TYPE_NOBITS
) && (type
!= OUT_RESERVE
))
775 error(ERR_WARNING
, "attempt to initialize memory in a"
776 " nobits section: ignored");
778 if (type
== OUT_ADDRESS
) {
779 if (segment
!= NO_SEG
&& !find_section_by_index(segment
)) {
781 error(ERR_NONFATAL
, "binary output format does not support"
782 " segment base references");
784 error(ERR_NONFATAL
, "binary output format does not support"
785 " external references");
788 if (s
->flags
& TYPE_PROGBITS
) {
789 if (segment
!= NO_SEG
)
790 add_reloc(s
, size
, segment
, -1L);
792 WRITEADDR(p
, *(int64_t *)data
, size
);
793 saa_wbytes(s
->contents
, mydata
, size
);
796 } else if (type
== OUT_RAWDATA
) {
797 if (s
->flags
& TYPE_PROGBITS
)
798 saa_wbytes(s
->contents
, data
, size
);
800 } else if (type
== OUT_RESERVE
) {
801 if (s
->flags
& TYPE_PROGBITS
) {
802 error(ERR_WARNING
, "uninitialized space declared in"
803 " %s section: zeroing", s
->name
);
804 saa_wbytes(s
->contents
, NULL
, size
);
807 } else if (type
== OUT_REL2ADR
|| type
== OUT_REL4ADR
||
808 type
== OUT_REL8ADR
) {
820 size
= 0; /* Shut up warning */
823 if (segment
!= NO_SEG
&& !find_section_by_index(segment
)) {
825 error(ERR_NONFATAL
, "binary output format does not support"
826 " segment base references");
828 error(ERR_NONFATAL
, "binary output format does not support"
829 " external references");
832 if (s
->flags
& TYPE_PROGBITS
) {
833 add_reloc(s
, size
, segment
, segto
);
835 WRITEADDR(p
, *(int64_t *)data
- size
- s
->length
, size
);
836 saa_wbytes(s
->contents
, mydata
, size
);
842 static void bin_deflabel(char *name
, int32_t segment
, int64_t offset
,
843 int is_global
, char *special
)
845 (void)segment
; /* Don't warn that this parameter is unused */
846 (void)offset
; /* Don't warn that this parameter is unused */
849 error(ERR_NONFATAL
, "binary format does not support any"
850 " special symbol types");
851 else if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@')
852 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
853 else if (is_global
== 2)
854 error(ERR_NONFATAL
, "binary output format does not support common"
858 struct bin_label
***ltp
;
860 /* Remember label definition so we can look it up later when
861 * creating the map file. */
862 s
= find_section_by_index(segment
);
864 ltp
= &(s
->labels_end
);
867 (**ltp
) = nasm_malloc(sizeof(struct bin_label
));
868 (**ltp
)->name
= name
;
869 (**ltp
)->next
= NULL
;
870 *ltp
= &((**ltp
)->next
);
875 /* These constants and the following function are used
876 * by bin_secname() to parse attribute assignments. */
878 enum { ATTRIB_START
, ATTRIB_ALIGN
, ATTRIB_FOLLOWS
,
879 ATTRIB_VSTART
, ATTRIB_VALIGN
, ATTRIB_VFOLLOWS
,
880 ATTRIB_NOBITS
, ATTRIB_PROGBITS
883 static int bin_read_attribute(char **line
, int *attribute
,
887 int attrib_name_size
;
888 struct tokenval tokval
;
891 /* Skip whitespace. */
892 while (**line
&& isspace(**line
))
897 /* Figure out what attribute we're reading. */
898 if (!nasm_strnicmp(*line
, "align=", 6)) {
899 *attribute
= ATTRIB_ALIGN
;
900 attrib_name_size
= 6;
901 } else if (format_mode
) {
902 if (!nasm_strnicmp(*line
, "start=", 6)) {
903 *attribute
= ATTRIB_START
;
904 attrib_name_size
= 6;
905 } else if (!nasm_strnicmp(*line
, "follows=", 8)) {
906 *attribute
= ATTRIB_FOLLOWS
;
909 } else if (!nasm_strnicmp(*line
, "vstart=", 7)) {
910 *attribute
= ATTRIB_VSTART
;
911 attrib_name_size
= 7;
912 } else if (!nasm_strnicmp(*line
, "valign=", 7)) {
913 *attribute
= ATTRIB_VALIGN
;
914 attrib_name_size
= 7;
915 } else if (!nasm_strnicmp(*line
, "vfollows=", 9)) {
916 *attribute
= ATTRIB_VFOLLOWS
;
919 } else if (!nasm_strnicmp(*line
, "nobits", 6) &&
920 (isspace((*line
)[6]) || ((*line
)[6] == '\0'))) {
921 *attribute
= ATTRIB_NOBITS
;
924 } else if (!nasm_strnicmp(*line
, "progbits", 8) &&
925 (isspace((*line
)[8]) || ((*line
)[8] == '\0'))) {
926 *attribute
= ATTRIB_PROGBITS
;
934 /* Find the end of the expression. */
935 if ((*line
)[attrib_name_size
] != '(') {
936 /* Single term (no parenthesis). */
937 exp
= *line
+= attrib_name_size
;
938 while (**line
&& !isspace(**line
))
948 /* Full expression (delimited by parenthesis) */
949 exp
= *line
+= attrib_name_size
+ 1;
951 (*line
) += strcspn(*line
, "()'\"");
962 if ((**line
== '"') || (**line
== '\'')) {
971 "invalid syntax in `section' directive");
977 error(ERR_NONFATAL
, "expecting `)'");
981 *(*line
- 1) = '\0'; /* Terminate the expression. */
984 /* Check for no value given. */
986 error(ERR_WARNING
, "No value given to attribute in"
987 " `section' directive");
991 /* Read and evaluate the expression. */
993 stdscan_bufptr
= exp
;
994 tokval
.t_type
= TOKEN_INVALID
;
995 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
997 if (!is_really_simple(e
)) {
998 error(ERR_NONFATAL
, "section attribute value must be"
999 " a critical expression");
1003 error(ERR_NONFATAL
, "Invalid attribute value"
1004 " specified in `section' directive.");
1007 *value
= (uint64_t)reloc_value(e
);
1011 static void bin_assign_attributes(struct Section
*sec
, char *astring
)
1013 int attribute
, check
;
1017 while (1) { /* Get the next attribute. */
1018 check
= bin_read_attribute(&astring
, &attribute
, &value
);
1019 /* Skip bad attribute. */
1022 /* Unknown section attribute, so skip it and warn the user. */
1025 break; /* End of line. */
1028 while (*astring
&& !isspace(*astring
))
1034 error(ERR_WARNING
, "ignoring unknown section attribute:"
1040 switch (attribute
) { /* Handle nobits attribute. */
1042 if ((sec
->flags
& TYPE_DEFINED
)
1043 && (sec
->flags
& TYPE_PROGBITS
))
1045 "attempt to change section type"
1046 " from progbits to nobits");
1048 sec
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
1051 /* Handle progbits attribute. */
1052 case ATTRIB_PROGBITS
:
1053 if ((sec
->flags
& TYPE_DEFINED
) && (sec
->flags
& TYPE_NOBITS
))
1054 error(ERR_NONFATAL
, "attempt to change section type"
1055 " from nobits to progbits");
1057 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1060 /* Handle align attribute. */
1062 if (!format_mode
&& (!strcmp(sec
->name
, ".text")))
1063 error(ERR_NONFATAL
, "cannot specify an alignment"
1064 " to the .text section");
1066 if (!value
|| ((value
- 1) & value
))
1067 error(ERR_NONFATAL
, "argument to `align' is not a"
1069 else { /* Alignment is already satisfied if the previous
1070 * align value is greater. */
1071 if ((sec
->flags
& ALIGN_DEFINED
)
1072 && (value
< sec
->align
))
1075 /* Don't allow a conflicting align value. */
1076 if ((sec
->flags
& START_DEFINED
)
1077 && (sec
->start
& (value
- 1)))
1079 "`align' value conflicts "
1080 "with section start address");
1083 sec
->flags
|= ALIGN_DEFINED
;
1089 /* Handle valign attribute. */
1091 if (!value
|| ((value
- 1) & value
))
1092 error(ERR_NONFATAL
, "argument to `valign' is not a"
1094 else { /* Alignment is already satisfied if the previous
1095 * align value is greater. */
1096 if ((sec
->flags
& VALIGN_DEFINED
) && (value
< sec
->valign
))
1097 value
= sec
->valign
;
1099 /* Don't allow a conflicting valign value. */
1100 if ((sec
->flags
& VSTART_DEFINED
)
1101 && (sec
->vstart
& (value
- 1)))
1103 "`valign' value conflicts "
1104 "with `vstart' address");
1106 sec
->valign
= value
;
1107 sec
->flags
|= VALIGN_DEFINED
;
1112 /* Handle start attribute. */
1114 if (sec
->flags
& FOLLOWS_DEFINED
)
1115 error(ERR_NONFATAL
, "cannot combine `start' and `follows'"
1116 " section attributes");
1117 else if ((sec
->flags
& START_DEFINED
) && (value
!= sec
->start
))
1118 error(ERR_NONFATAL
, "section start address redefined");
1121 sec
->flags
|= START_DEFINED
;
1122 if (sec
->flags
& ALIGN_DEFINED
) {
1123 if (sec
->start
& (sec
->align
- 1))
1124 error(ERR_NONFATAL
, "`start' address conflicts"
1125 " with section alignment");
1126 sec
->flags
^= ALIGN_DEFINED
;
1131 /* Handle vstart attribute. */
1133 if (sec
->flags
& VFOLLOWS_DEFINED
)
1135 "cannot combine `vstart' and `vfollows'"
1136 " section attributes");
1137 else if ((sec
->flags
& VSTART_DEFINED
)
1138 && (value
!= sec
->vstart
))
1140 "section virtual start address"
1141 " (vstart) redefined");
1143 sec
->vstart
= value
;
1144 sec
->flags
|= VSTART_DEFINED
;
1145 if (sec
->flags
& VALIGN_DEFINED
) {
1146 if (sec
->vstart
& (sec
->valign
- 1))
1147 error(ERR_NONFATAL
, "`vstart' address conflicts"
1148 " with `valign' value");
1149 sec
->flags
^= VALIGN_DEFINED
;
1154 /* Handle follows attribute. */
1155 case ATTRIB_FOLLOWS
:
1157 astring
+= strcspn(astring
, " \t");
1159 error(ERR_NONFATAL
, "expecting section name for `follows'"
1162 *(astring
++) = '\0';
1163 if (sec
->flags
& START_DEFINED
)
1165 "cannot combine `start' and `follows'"
1166 " section attributes");
1167 sec
->follows
= nasm_strdup(p
);
1168 sec
->flags
|= FOLLOWS_DEFINED
;
1172 /* Handle vfollows attribute. */
1173 case ATTRIB_VFOLLOWS
:
1174 if (sec
->flags
& VSTART_DEFINED
)
1176 "cannot combine `vstart' and `vfollows'"
1177 " section attributes");
1180 astring
+= strcspn(astring
, " \t");
1183 "expecting section name for `vfollows'"
1186 *(astring
++) = '\0';
1187 sec
->vfollows
= nasm_strdup(p
);
1188 sec
->flags
|= VFOLLOWS_DEFINED
;
1196 static void bin_define_section_labels(void)
1198 static int labels_defined
= 0;
1199 struct Section
*sec
;
1205 for (sec
= sections
; sec
; sec
= sec
->next
) {
1206 base_len
= strlen(sec
->name
) + 8;
1207 label_name
= nasm_malloc(base_len
+ 8);
1208 strcpy(label_name
, "section.");
1209 strcpy(label_name
+ 8, sec
->name
);
1211 /* section.<name>.start */
1212 strcpy(label_name
+ base_len
, ".start");
1213 define_label(label_name
, sec
->start_index
, 0L,
1214 NULL
, 0, 0, bin_get_ofmt(), error
);
1216 /* section.<name>.vstart */
1217 strcpy(label_name
+ base_len
, ".vstart");
1218 define_label(label_name
, sec
->vstart_index
, 0L,
1219 NULL
, 0, 0, bin_get_ofmt(), error
);
1221 nasm_free(label_name
);
1226 static int32_t bin_secname(char *name
, int pass
, int *bits
)
1229 struct Section
*sec
;
1231 /* bin_secname is called with *name = NULL at the start of each
1232 * pass. Use this opportunity to establish the default section
1233 * (default is BITS-16 ".text" segment).
1235 if (!name
) { /* Reset ORG and section attributes at the start of each pass. */
1237 for (sec
= sections
; sec
; sec
= sec
->next
)
1238 sec
->flags
&= ~(START_DEFINED
| VSTART_DEFINED
|
1239 ALIGN_DEFINED
| VALIGN_DEFINED
);
1241 /* Define section start and vstart labels. */
1242 if (format_mode
&& (pass
!= 1))
1243 bin_define_section_labels();
1245 /* Establish the default (.text) section. */
1247 sec
= find_section_by_name(".text");
1248 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1249 current_section
= sec
->vstart_index
;
1250 return current_section
;
1253 /* Attempt to find the requested section. If it does not
1254 * exist, create it. */
1256 while (*p
&& !isspace(*p
))
1260 sec
= find_section_by_name(name
);
1262 sec
= create_section(name
);
1263 if (!strcmp(name
, ".data"))
1264 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1265 else if (!strcmp(name
, ".bss")) {
1266 sec
->flags
|= TYPE_DEFINED
| TYPE_NOBITS
;
1267 sec
->ifollows
= NULL
;
1268 } else if (!format_mode
) {
1269 error(ERR_NONFATAL
, "section name must be "
1270 ".text, .data, or .bss");
1271 return current_section
;
1275 /* Handle attribute assignments. */
1277 bin_assign_attributes(sec
, p
);
1279 #ifndef ABIN_SMART_ADAPT
1280 /* The following line disables smart adaptation of
1281 * PROGBITS/NOBITS section types (it forces sections to
1282 * default to PROGBITS). */
1283 if ((pass
!= 1) && !(sec
->flags
& TYPE_DEFINED
))
1284 sec
->flags
|= TYPE_DEFINED
| TYPE_PROGBITS
;
1287 /* Set the current section and return. */
1288 current_section
= sec
->vstart_index
;
1289 return current_section
;
1292 static int bin_directive(char *directive
, char *args
, int pass
)
1294 /* Handle ORG directive */
1295 if (!nasm_stricmp(directive
, "org")) {
1296 struct tokenval tokval
;
1301 stdscan_bufptr
= args
;
1302 tokval
.t_type
= TOKEN_INVALID
;
1303 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
1305 if (!is_really_simple(e
))
1306 error(ERR_NONFATAL
, "org value must be a critical"
1309 value
= reloc_value(e
);
1310 /* Check for ORG redefinition. */
1311 if (origin_defined
&& (value
!= origin
))
1312 error(ERR_NONFATAL
, "program origin redefined");
1319 error(ERR_NONFATAL
, "No or invalid offset specified"
1320 " in ORG directive.");
1324 /* The 'map' directive allows the user to generate section
1325 * and symbol information to stdout, stderr, or to a file. */
1326 else if (format_mode
&& !nasm_stricmp(directive
, "map")) {
1331 args
+= strspn(args
, " \t");
1334 args
+= strcspn(args
, " \t");
1337 if (!nasm_stricmp(p
, "all"))
1339 MAP_ORIGIN
| MAP_SUMMARY
| MAP_SECTIONS
| MAP_SYMBOLS
;
1340 else if (!nasm_stricmp(p
, "brief"))
1341 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
;
1342 else if (!nasm_stricmp(p
, "sections"))
1343 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
| MAP_SECTIONS
;
1344 else if (!nasm_stricmp(p
, "segments"))
1345 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
| MAP_SECTIONS
;
1346 else if (!nasm_stricmp(p
, "symbols"))
1347 map_control
|= MAP_SYMBOLS
;
1349 if (!nasm_stricmp(p
, "stdout"))
1351 else if (!nasm_stricmp(p
, "stderr"))
1353 else { /* Must be a filename. */
1354 rf
= fopen(p
, "wt");
1356 error(ERR_WARNING
, "unable to open map file `%s'",
1363 error(ERR_WARNING
, "map file already specified");
1365 if (map_control
== 0)
1366 map_control
|= MAP_ORIGIN
| MAP_SUMMARY
;
1374 static void bin_filename(char *inname
, char *outname
, efunc error
)
1376 standard_extension(inname
, outname
, "", error
);
1381 static int32_t bin_segbase(int32_t segment
)
1386 static int bin_set_info(enum geninfo type
, char **val
)
1393 static void bin_init(FILE * afp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
1398 (void)eval
; /* Don't warn that this parameter is unused. */
1399 (void)ldef
; /* Placate optimizers. */
1401 maxbits
= 64; /* Support 64-bit Segments */
1403 reloctail
= &relocs
;
1405 no_seg_labels
= NULL
;
1406 nsl_tail
= &no_seg_labels
;
1407 format_mode
= 1; /* Extended bin format
1408 * (set this to zero for old bin format). */
1410 /* Create default section (.text). */
1411 sections
= last_section
= nasm_malloc(sizeof(struct Section
));
1412 last_section
->next
= NULL
;
1413 last_section
->name
= nasm_strdup(".text");
1414 last_section
->contents
= saa_init(1L);
1415 last_section
->follows
= last_section
->vfollows
= 0;
1416 last_section
->ifollows
= NULL
;
1417 last_section
->length
= 0;
1418 last_section
->flags
= TYPE_DEFINED
| TYPE_PROGBITS
;
1419 last_section
->labels
= NULL
;
1420 last_section
->labels_end
= &(last_section
->labels
);
1421 last_section
->start_index
= seg_alloc();
1422 last_section
->vstart_index
= current_section
= seg_alloc();
1425 struct ofmt of_bin
= {
1426 "flat-form binary files (e.g. DOS .COM, .SYS)",
1443 /* This is needed for bin_define_section_labels() */
1444 struct ofmt
*bin_get_ofmt(void)
1449 #endif /* #ifdef OF_BIN */