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

📄 outrdf.c

📁 nasm的全套源代码,有些我做了些修改,以方便您更方便更容易调试成功,方便学习做编译器
💻 C
📖 第 1 页 / 共 2 页
字号:

static void rdf_deflabel(char *name, long segment, long offset,
                         int is_global, char *special)
{
    struct ExportRec r;
    struct ImportRec ri;
#ifdef VERBOSE_WARNINGS
    static int warned_common = 0;
#endif

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

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

    if (is_global == 2) {
#ifdef VERBOSE_WARNINGS
        if (!warned_common) {
            error(ERR_WARNING,
                  "common declarations not supported: using extern");
            warned_common = 1;
        }
#endif
        is_global = 1;
    }

    if (segment > 4) {          /* EXTERN declaration */
        ri.type = 2;
        ri.segment = segment;
        strncpy(ri.label, name, 32);
        ri.label[32] = 0;
        write_import_rec(&ri);
    } else if (is_global) {
        r.type = 3;
        r.segment = segment;
        r.offset = offset;
        strncpy(r.label, name, 32);
        r.label[32] = 0;
        write_export_rec(&r);
    }
}

static void rdf_out(long segto, void *data, unsigned long type,
                    long segment, long wrt)
{
    long bytes = type & OUT_SIZMASK;
    struct RelocRec rr;
    unsigned char databuf[4], *pd;

    if (segto == NO_SEG) {
        if ((type & OUT_TYPMASK) != OUT_RESERVE)
            error(ERR_NONFATAL,
                  "attempt to assemble code in ABSOLUTE space");
        return;
    }

    segto >>= 1;                /* convert NASM segment no to RDF number */

    if (segto != 0 && segto != 1 && segto != 2) {
        error(ERR_NONFATAL,
              "specified segment not supported by rdf output format");
        return;
    }

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

    type &= OUT_TYPMASK;

    if (segto == 2 && type != OUT_RESERVE) {
        error(ERR_NONFATAL, "BSS segments may not be initialised");

        /* just reserve the space for now... */

        if (type == OUT_REL2ADR)
            bytes = 2;
        else
            bytes = 4;
        type = OUT_RESERVE;
    }

    if (type == OUT_RESERVE) {
        if (segto == 2)         /* BSS segment space reserverd */
            bsslength += bytes;
        else
            while (bytes--)
                membufwrite(seg[segto], databuf, 1);
    } else if (type == OUT_RAWDATA) {
        if (segment != NO_SEG)
            error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
        membufwrite(seg[segto], data, bytes);
    } else if (type == OUT_ADDRESS) {

        /* if segment == NO_SEG then we are writing an address of an
           object within the same segment - do not produce reloc rec. */

        if (segment != NO_SEG) {

            /* it's an address, so we must write a relocation record */

            rr.type = 1;        /* type signature */
            rr.segment = segto; /* segment we're currently in */
            rr.offset = membuflength(seg[segto]);       /* current offset */
            rr.length = bytes;  /* length of reference */
            rr.refseg = segment;        /* segment referred to */
            write_reloc_rec(&rr);
        }

        pd = databuf;           /* convert address to little-endian */
        if (bytes == 2)
            WRITESHORT(pd, *(long *)data);
        else
            WRITELONG(pd, *(long *)data);

        membufwrite(seg[segto], databuf, bytes);

    } else if (type == OUT_REL2ADR) {
        if (segment == segto)
            error(ERR_PANIC, "intra-segment OUT_REL2ADR");
        if (segment != NO_SEG && segment % 2) {
            error(ERR_NONFATAL,
                  "rdf format does not support segment base refs");
        }

        rr.type = 1;            /* type signature */
        rr.segment = segto + 64;        /* segment we're currently in + rel flag */
        rr.offset = membuflength(seg[segto]);   /* current offset */
        rr.length = 2;          /* length of reference */
        rr.refseg = segment;    /* segment referred to */
        write_reloc_rec(&rr);

        /* work out what to put in the code: offset of the end of this operand,
         * subtracted from any data specified, so that loader can just add
         * address of imported symbol onto it to get address relative to end of
         * instruction: import_address + data(offset) - end_of_instrn */

        rr.offset = *(long *)data - (rr.offset + bytes);

        membufwrite(seg[segto], &rr.offset, -2);
    } else if (type == OUT_REL4ADR) {
        if (segment == segto)
            error(ERR_PANIC, "intra-segment OUT_REL4ADR");
        if (segment != NO_SEG && segment % 2) {
            error(ERR_NONFATAL,
                  "rdf format does not support segment base refs");
        }

        rr.type = 1;            /* type signature */
        rr.segment = segto + 64;        /* segment we're currently in + rel tag */
        rr.offset = membuflength(seg[segto]);   /* current offset */
        rr.length = 4;          /* length of reference */
        rr.refseg = segment;    /* segment referred to */
        write_reloc_rec(&rr);

        rr.offset = *(long *)data - (rr.offset + bytes);
        membufwrite(seg[segto], &rr.offset, -4);
    }
}

static void rdf_cleanup(int debuginfo)
{
    long l;
    unsigned char b[4], *d;
    struct BSSRec bs;

    (void)debuginfo;

    /* should write imported & exported symbol declarations to header here */

    /* generate the output file... */
    fwrite(RDOFFId, 6, 1, ofile);       /* file type magic number */

    if (bsslength != 0) {       /* reserve BSS */
        bs.type = 5;
        bs.amount = bsslength;
        write_bss_rec(&bs);
    }

    l = membuflength(header);
    d = b;
    WRITELONG(d, l);

    fwrite(b, 4, 1, ofile);     /* write length of header */
    membufdump(header, ofile);  /* dump header */

    l = membuflength(seg[0]);
    d = b;                      /* code segment */
    WRITELONG(d, l);

    fwrite(b, 4, 1, ofile);
    membufdump(seg[0], ofile);

    l = membuflength(seg[1]);
    d = b;                      /* data segment */
    WRITELONG(d, l);

    fwrite(b, 4, 1, ofile);
    membufdump(seg[1], ofile);

    freemembuf(header);
    freemembuf(seg[0]);
    freemembuf(seg[1]);
    fclose(ofile);
}

static long rdf_segbase(long segment)
{
    return segment;
}

static int rdf_directive(char *directive, char *value, int pass)
{
    struct DLLRec r;

    if (!strcmp(directive, "library")) {
        if (pass == 1) {
            r.type = 4;
            strcpy(r.libname, value);
            write_dll_rec(&r);
        }
        return 1;
    }

    return 0;
}

static void rdf_filename(char *inname, char *outname, efunc error)
{
    standard_extension(inname, outname, ".rdf", error);
}

static char *rdf_stdmac[] = {
    "%define __SECT__ [section .text]",
    "%imacro library 1+.nolist",
    "[library %1]",
    "%endmacro",
    "%macro __NASM_CDecl__ 1",
    "%endmacro",
    NULL
};

static int rdf_set_info(enum geninfo type, char **val)
{
    return 0;
}

struct ofmt of_rdf = {
    "Relocatable Dynamic Object File Format v1.1",
#ifdef OF_RDF2
    "oldrdf",
#else
    "rdf",
#endif
    NULL,
    null_debug_arr,
    &null_debug_form,
    rdf_stdmac,
    rdf_init,
    rdf_set_info,
    rdf_out,
    rdf_deflabel,
    rdf_section_names,
    rdf_segbase,
    rdf_directive,
    rdf_filename,
    rdf_cleanup
};

#endif                          /* OF_RDF */

⌨️ 快捷键说明

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