⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outas86.c

📁 32位汇编编译器nasm源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* outas86.c	output routines for the Netwide Assembler to produce
 *		Linux as86 (bin86-0.3) object 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "nasm.h"
#include "nasmlib.h"
#include "outform.h"

#ifdef OF_AS86

struct Piece {
    struct Piece *next;
    int type;                   /* 0 = absolute, 1 = seg, 2 = sym */
    long offset;                /* relative offset */
    int number;                 /* symbol/segment number (4=bss) */
    long bytes;                 /* size of reloc or of absolute data */
    int relative;               /* TRUE or FALSE */
};

struct Symbol {
    long strpos;                /* string table position of name */
    int flags;                  /* symbol flags */
    int segment;                /* 4=bss at this point */
    long value;                 /* address, or COMMON variable size */
};

/*
 * Section IDs - used in Piece.number and Symbol.segment.
 */
#define SECT_TEXT 0             /* text section */
#define SECT_DATA 3             /* data section */
#define SECT_BSS 4              /* bss section */

/*
 * Flags used in Symbol.flags.
 */
#define SYM_ENTRY (1<<8)
#define SYM_EXPORT (1<<7)
#define SYM_IMPORT (1<<6)
#define SYM_ABSOLUTE (1<<4)

struct Section {
    struct SAA *data;
    unsigned long datalen, size, len;
    long index;
    struct Piece *head, *last, **tail;
};

static char as86_module[FILENAME_MAX];

static struct Section stext, sdata;
static unsigned long bsslen;
static long bssindex;

static struct SAA *syms;
static unsigned long nsyms;

static struct RAA *bsym;

static struct SAA *strs;
static unsigned long strslen;

static int as86_reloc_size;

static FILE *as86fp;
static efunc error;

static void as86_write(void);
static void as86_write_section(struct Section *, int);
static int as86_add_string(char *name);
static void as86_sect_write(struct Section *, const unsigned char *,
                            unsigned long);

static void as86_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
{
    as86fp = fp;
    error = errfunc;
    (void)ldef;                 /* placate optimisers */
    stext.data = saa_init(1L);
    stext.datalen = 0L;
    stext.head = stext.last = NULL;
    stext.tail = &stext.head;
    sdata.data = saa_init(1L);
    sdata.datalen = 0L;
    sdata.head = sdata.last = NULL;
    sdata.tail = &sdata.head;
    bsslen =
        stext.len = stext.datalen = stext.size =
        sdata.len = sdata.datalen = sdata.size = 0;
    stext.index = seg_alloc();
    sdata.index = seg_alloc();
    bssindex = seg_alloc();
    syms = saa_init((long)sizeof(struct Symbol));
    nsyms = 0;
    bsym = raa_init();
    strs = saa_init(1L);
    strslen = 0;

    as86_add_string(as86_module);
}

static void as86_cleanup(int debuginfo)
{
    struct Piece *p;

    (void)debuginfo;

    as86_write();
    fclose(as86fp);
    saa_free(stext.data);
    while (stext.head) {
        p = stext.head;
        stext.head = stext.head->next;
        nasm_free(p);
    }
    saa_free(sdata.data);
    while (sdata.head) {
        p = sdata.head;
        sdata.head = sdata.head->next;
        nasm_free(p);
    }
    saa_free(syms);
    raa_free(bsym);
    saa_free(strs);
}

static long as86_section_names(char *name, int pass, int *bits)
{
    /*
     * Default is 16 bits.
     */
    if (!name)
        *bits = 16;

    if (!name)
        return stext.index;

    if (!strcmp(name, ".text"))
        return stext.index;
    else if (!strcmp(name, ".data"))
        return sdata.index;
    else if (!strcmp(name, ".bss"))
        return bssindex;
    else
        return NO_SEG;
}

static int as86_add_string(char *name)
{
    int pos = strslen;
    int length = strlen(name);

    saa_wbytes(strs, name, (long)(length + 1));
    strslen += 1 + length;

    return pos;
}

static void as86_deflabel(char *name, long segment, long offset,
                          int is_global, char *special)
{
    struct Symbol *sym;

    if (special)
        error(ERR_NONFATAL, "as86 format does not support any"
              " special symbol types");

    if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
        error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
        return;
    }

    sym = saa_wstruct(syms);

    sym->strpos = as86_add_string(name);
    sym->flags = 0;
    if (segment == NO_SEG)
        sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
    else if (segment == stext.index)
        sym->segment = SECT_TEXT;
    else if (segment == sdata.index)
        sym->segment = SECT_DATA;
    else if (segment == bssindex)
        sym->segment = SECT_BSS;
    else {
        sym->flags |= SYM_IMPORT;
        sym->segment = 15;
    }

    if (is_global == 2)
        sym->segment = 3;       /* already have IMPORT */

    if (is_global && !(sym->flags & SYM_IMPORT))
        sym->flags |= SYM_EXPORT;

    sym->value = offset;

    /*
     * define the references from external-symbol segment numbers
     * to these symbol records.
     */
    if (segment != NO_SEG && segment != stext.index &&
        segment != sdata.index && segment != bssindex)
        bsym = raa_write(bsym, segment, nsyms);

    nsyms++;
}

static void as86_add_piece(struct Section *sect, int type, long offset,
                           long segment, long bytes, int relative)
{
    struct Piece *p;

    sect->len += bytes;

    if (type == 0 && sect->last && sect->last->type == 0) {
        sect->last->bytes += bytes;
        return;
    }

    p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
    sect->tail = &p->next;
    p->next = NULL;

    p->type = type;
    p->offset = offset;
    p->bytes = bytes;
    p->relative = relative;

    if (type == 1 && segment == stext.index)
        p->number = SECT_TEXT;
    else if (type == 1 && segment == sdata.index)
        p->number = SECT_DATA;
    else if (type == 1 && segment == bssindex)
        p->number = SECT_BSS;
    else if (type == 1)
        p->number = raa_read(bsym, segment), p->type = 2;
}

static void as86_out(long segto, const void *data, unsigned long type,
                     long segment, long wrt)
{
    struct Section *s;
    long realbytes = type & OUT_SIZMASK;
    long offset;
    unsigned char mydata[4], *p;

    if (wrt != NO_SEG) {
        wrt = NO_SEG;           /* continue to do _something_ */
        error(ERR_NONFATAL, "WRT not supported by as86 output format");
    }

    type &= OUT_TYPMASK;

    /*
     * handle absolute-assembly (structure definitions)
     */
    if (segto == NO_SEG) {
        if (type != OUT_RESERVE)
            error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
                  " space");
        return;
    }

    if (segto == stext.index)
        s = &stext;
    else if (segto == sdata.index)
        s = &sdata;
    else if (segto == bssindex)
        s = NULL;
    else {
        error(ERR_WARNING, "attempt to assemble code in"
              " segment %d: defaulting to `.text'", segto);
        s = &stext;
    }

    if (!s && type != OUT_RESERVE) {
        error(ERR_WARNING, "attempt to initialise memory in the"
              " BSS section: ignored");
        if (type == OUT_REL2ADR)
            realbytes = 2;
        else if (type == OUT_REL4ADR)
            realbytes = 4;
        bsslen += realbytes;
        return;
    }

    if (type == OUT_RESERVE) {
        if (s) {
            error(ERR_WARNING, "uninitialised space declared in"
                  " %s section: zeroing",
                  (segto == stext.index ? "code" : "data"));
            as86_sect_write(s, NULL, realbytes);
            as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
        } else
            bsslen += realbytes;
    } else if (type == OUT_RAWDATA) {
        if (segment != NO_SEG)
            error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
        as86_sect_write(s, data, realbytes);
        as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
    } else if (type == OUT_ADDRESS) {
        if (segment != NO_SEG) {
            if (segment % 2) {
                error(ERR_NONFATAL, "as86 format does not support"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -