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