📄 outbin.c
字号:
/* outbin.c output routines for the Netwide Assembler to produce
* flat-form binary files
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the licence given in the file "Licence"
* distributed in the NASM archive.
*/
/* This is the extended version of NASM's original binary output
* format. It is backward compatible with the original BIN format,
* and contains support for multiple sections and advanced section
* ordering.
*
* Feature summary:
*
* - Users can create an arbitrary number of sections; they are not
* limited to just ".text", ".data", and ".bss".
*
* - Sections can be either progbits or nobits type.
*
* - You can specify that they be aligned at a certian boundary
* following the previous section ("align="), or positioned at an
* arbitrary byte-granular location ("start=").
*
* - You can specify a "virtual" start address for a section, which
* will be used for the calculation for all address references
* with respect to that section ("vstart=").
*
* - The ORG directive, as well as the section/segment directive
* arguments ("align=", "start=", "vstart="), can take a critical
* expression as their value. For example: "align=(1 << 12)".
*
* - You can generate map files using the 'map' directive.
*
*/
/* Uncomment the following define if you want sections to adapt
* their progbits/nobits state depending on what type of
* instructions are issued, rather than defaulting to progbits.
* Note that this behavior violates the specification.
#define ABIN_SMART_ADAPT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "nasm.h"
#include "nasmlib.h"
#include "labels.h"
#include "eval.h"
#include "outform.h"
#ifdef OF_BIN
struct ofmt *bin_get_ofmt(); /* Prototype goes here since no header file. */
static FILE *fp, *rf = NULL;
static efunc error;
/* Section flags keep track of which attributes the user has defined. */
#define START_DEFINED 0x001
#define ALIGN_DEFINED 0x002
#define FOLLOWS_DEFINED 0x004
#define VSTART_DEFINED 0x008
#define VALIGN_DEFINED 0x010
#define VFOLLOWS_DEFINED 0x020
#define TYPE_DEFINED 0x040
#define TYPE_PROGBITS 0x080
#define TYPE_NOBITS 0x100
/* This struct is used to keep track of symbols for map-file generation. */
static struct bin_label
{ char * name;
struct bin_label *next;
} *no_seg_labels, **nsl_tail;
static struct Section
{ char *name;
struct SAA * contents;
long length; /* section length in bytes */
/* Section attributes */
int flags; /* see flag definitions above */
unsigned long align; /* section alignment */
unsigned long valign; /* notional section alignment */
unsigned long start; /* section start address */
unsigned long vstart; /* section virtual start address */
char *follows; /* the section that this one will follow */
char *vfollows; /* the section that this one will notionally follow */
long start_index; /* NASM section id for non-relocated version */
long vstart_index; /* the NASM section id */
struct bin_label *labels; /* linked-list of label handles for map output. */
struct bin_label **labels_end; /* Holds address of end of labels list. */
struct Section *ifollows; /* Points to previous section (implicit follows). */
struct Section *next; /* This links sections with a defined start address. */
/* The extended bin format allows for sections to have a "virtual"
* start address. This is accomplished by creating two sections:
* one beginning at the Load Memory Address and the other beginning
* at the Virtual Memory Address. The LMA section is only used to
* define the section.<section_name>.start label, but there isn't
* any other good way for us to handle that label.
*/
} *sections, *last_section;
static struct Reloc {
struct Reloc *next;
long posn;
long bytes;
long secref;
long secrel;
struct Section *target;
} *relocs, **reloctail;
extern char *stdscan_bufptr;
extern int lookup_label (char *label, long *segment, long *offset);
static unsigned char format_mode; /* 0 = original bin, 1 = extended bin */
static long current_section; /* only really needed if format_mode = 0 */
static unsigned long origin;
static int origin_defined;
/* Stuff we need for map-file generation. */
#define MAP_ORIGIN 1
#define MAP_SUMMARY 2
#define MAP_SECTIONS 4
#define MAP_SYMBOLS 8
static int map_control = 0;
static char *infile, *outfile;
static const char *bin_stdmac[] = {
"%define __SECT__ [section .text]",
"%imacro org 1+.nolist",
"[org %1]",
"%endmacro",
"%macro __NASM_CDecl__ 1",
"%endmacro",
NULL
};
static void add_reloc(struct Section *s, long bytes, long secref, long secrel)
{ struct Reloc *r;
r = *reloctail = nasm_malloc(sizeof(struct Reloc));
reloctail = &r->next;
r->next = NULL;
r->posn = s->length;
r->bytes = bytes;
r->secref = secref;
r->secrel = secrel;
r->target = s;
}
static struct Section *find_section_by_name(const char *name)
{ struct Section *s;
for (s = sections; s; s = s->next)
if (!strcmp(s->name,name)) break;
return s;
}
static struct Section *find_section_by_index(long index)
{ struct Section *s;
for (s = sections; s; s = s->next)
if ((index == s->vstart_index) || (index == s->start_index))
break;
return s;
}
static struct Section * create_section(char *name)
{ /* Create a new section. */
last_section->next = nasm_malloc(sizeof(struct Section));
last_section->next->ifollows = last_section;
last_section = last_section->next;
last_section->labels = NULL;
last_section->labels_end = &(last_section->labels);
/* Initialize section attributes. */
last_section->name = nasm_strdup(name);
last_section->contents = saa_init(1L);
last_section->follows = last_section->vfollows = 0;
last_section->length = 0;
last_section->flags = 0;
last_section->next = NULL;
/* Register our sections with NASM. */
last_section->vstart_index = seg_alloc();
last_section->start_index = seg_alloc();
return last_section;
}
static void bin_cleanup (int debuginfo)
{ struct Section *g, **gp;
struct Section *gs = NULL, **gsp;
struct Section *s, **sp;
struct Section *nobits = NULL, **nt;
struct Section * last_progbits;
struct bin_label *l;
struct Reloc *r;
unsigned long pend;
int h;
#ifdef DEBUG
fprintf(stdout, "bin_cleanup: Sections were initially referenced in this order:\n");
for (h = 0, s = sections; s; h++, s = s->next)
fprintf(stdout, "%i. %s\n", h, s->name);
#endif
/* Assembly has completed, so now we need to generate the output file.
* Step 1: Separate progbits and nobits sections into separate lists.
* Step 2: Sort the progbits sections into their output order.
* Step 3: Compute start addresses for all progbits sections.
* Step 4: Compute vstart addresses for all sections.
* Step 5: Apply relocations.
* Step 6: Write the sections' data to the output file.
* Step 7: Generate the map file.
* Step 8: Release all allocated memory.
*/
/* To do: Smart section-type adaptation could leave some empty sections
* without a defined type (progbits/nobits). Won't fix now since this
* feature will be disabled. */
/* Step 1: Split progbits and nobits sections into separate lists. */
nt = &nobits;
/* Move nobits sections into a separate list. Also pre-process nobits
* sections' attributes. */
for (sp = §ions->next, s = sections->next; s; s = *sp)
{ /* Skip progbits sections. */
if (s->flags & TYPE_PROGBITS)
{ sp = &s->next; continue;
}
/* Do some special pre-processing on nobits sections' attributes. */
if (s->flags & (START_DEFINED | ALIGN_DEFINED | FOLLOWS_DEFINED))
{ /* Check for a mixture of real and virtual section attributes. */
if (s->flags & (VSTART_DEFINED | VALIGN_DEFINED | VFOLLOWS_DEFINED))
error(ERR_FATAL, "cannot mix real and virtual attributes"
" in nobits section (%s)", s->name);
/* Real and virtual attributes mean the same thing for nobits sections. */
if (s->flags & START_DEFINED)
{ s->vstart = s->start; s->flags |= VSTART_DEFINED;
}
if (s->flags & ALIGN_DEFINED)
{ s->valign = s->align; s->flags |= VALIGN_DEFINED;
}
if (s->flags & FOLLOWS_DEFINED)
{ s->vfollows = s->follows; s->flags |= VFOLLOWS_DEFINED;
s->flags &= ~FOLLOWS_DEFINED;
}
}
/* Every section must have a start address. */
if (s->flags & VSTART_DEFINED)
{ s->start = s->vstart; s->flags |= START_DEFINED;
}
/* Move the section into the nobits list. */
*sp = s->next; s->next = NULL;
*nt = s; nt = &s->next;
}
/* Step 2: Sort the progbits sections into their output order. */
/* In Step 2 we move around sections in groups. A group
* begins with a section (group leader) that has a user-
* defined start address or follows section. The remainder
* of the group is made up of the sections that implicitly
* follow the group leader (i.e., they were defined after
* the group leader and were not given an explicit start
* address or follows section by the user). */
/* For anyone attempting to read this code:
* g (group) points to a group of sections, the first one of which has
* a user-defined start address or follows section.
* gp (g previous) holds the location of the pointer to g.
* gs (g scan) is a temp variable that we use to scan to the end of the group.
* gsp (gs previous) holds the location of the pointer to gs.
* nt (nobits tail) points to the nobits section-list tail.
*/
/* Link all 'follows' groups to their proper position. To do
* this we need to know three things: the start of the group
* to relocate (g), the section it is following (s), and the
* end of the group we're relocating (gs). */
for (gp = §ions, g = sections; g; g = gs)
{ /* Find the next follows group that is out of place (g). */
if (!(g->flags & FOLLOWS_DEFINED))
{ while (g->next)
{ if ((g->next->flags & FOLLOWS_DEFINED) &&
strcmp(g->name, g->next->follows)) break;
g = g->next;
}
if (!g->next) break;
gp = &g->next; g = g->next;
}
/* Find the section that this group follows (s). */
for (sp = §ions, s = sections;
s && strcmp(s->name, g->follows);
sp = &s->next, s = s->next);
if (!s) error(ERR_FATAL, "section %s follows an invalid or"
" unknown section (%s)", g->name, g->follows);
if (s->next && (s->next->flags & FOLLOWS_DEFINED) &&
!strcmp(s->name, s->next->follows))
error(ERR_FATAL, "sections %s and %s can't both follow"
" section %s", g->name, s->next->name, s->name);
/* Find the end of the current follows group (gs). */
for (gsp = &g->next, gs = g->next;
gs && (gs != s) && !(gs->flags & START_DEFINED);
gsp = &gs->next, gs = gs->next)
{ if (gs->next && (gs->next->flags & FOLLOWS_DEFINED) &&
strcmp(gs->name, gs->next->follows))
{ gsp = &gs->next; gs = gs->next; break;
}
}
/* Re-link the group after its follows section. */
*gsp = s->next; s->next = g;
*gp = gs;
}
/* Link all 'start' groups to their proper position. Once
* again we need to know g, s, and gs (see above). The main
* difference is we already know g since we sort by moving
* groups from the 'unsorted' list into a 'sorted' list (g
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -