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

📄 outaout.c

📁 nasm早期的源代码,比较简单是学习汇编和编译原理的好例子
💻 C
📖 第 1 页 / 共 3 页
字号:
                    is_pic = 0x40;
                    addr = aout_add_gotoff_reloc(s, segment,
                                                 addr, realbytes);
                } else if (wrt == aout_got_sect + 1) {
                    is_pic = 0x40;
                    addr =
                        aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
                                            realbytes, true);
                } else if (wrt == aout_sym_sect + 1) {
                    addr = aout_add_gsym_reloc(s, segment, addr,
                                               RELTYPE_ABSOLUTE, realbytes,
                                               false);
                } else if (wrt == aout_plt_sect + 1) {
                    is_pic = 0x40;
                    error(ERR_NONFATAL,
                          "a.out format cannot produce non-PC-"
                          "relative PLT references");
                } else {
                    error(ERR_NONFATAL,
                          "a.out format does not support this"
                          " use of WRT");
                    wrt = NO_SEG;       /* we can at least _try_ to continue */
                }
            }
        }
        p = mydata;
        if (realbytes == 2)
            WRITESHORT(p, addr);
        else
            WRITELONG(p, addr);
        aout_sect_write(s, mydata, realbytes);
    } else if (type == OUT_REL2ADR) {
        if (segment == segto)
            error(ERR_PANIC, "intra-segment OUT_REL2ADR");
        if (segment != NO_SEG && segment % 2) {
            error(ERR_NONFATAL, "a.out format does not support"
                  " segment base references");
        } else {
            if (wrt == NO_SEG) {
                aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
            } else if (!bsd) {
                error(ERR_NONFATAL, "Linux a.out format does not support"
                      " any use of WRT");
                wrt = NO_SEG;   /* we can at least _try_ to continue */
            } else if (wrt == aout_plt_sect + 1) {
                is_pic = 0x40;
                aout_add_reloc(s, segment, RELTYPE_PLT, 2);
            } else if (wrt == aout_gotpc_sect + 1 ||
                       wrt == aout_gotoff_sect + 1 ||
                       wrt == aout_got_sect + 1) {
                error(ERR_NONFATAL, "a.out format cannot produce PC-"
                      "relative GOT references");
            } else {
                error(ERR_NONFATAL, "a.out format does not support this"
                      " use of WRT");
                wrt = NO_SEG;   /* we can at least _try_ to continue */
            }
        }
        p = mydata;
        WRITESHORT(p, *(int32_t *)data - (realbytes + s->len));
        aout_sect_write(s, mydata, 2L);
    } else if (type == OUT_REL4ADR) {
        if (segment == segto)
            error(ERR_PANIC, "intra-segment OUT_REL4ADR");
        if (segment != NO_SEG && segment % 2) {
            error(ERR_NONFATAL, "a.out format does not support"
                  " segment base references");
        } else {
            if (wrt == NO_SEG) {
                aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
            } else if (!bsd) {
                error(ERR_NONFATAL, "Linux a.out format does not support"
                      " any use of WRT");
                wrt = NO_SEG;   /* we can at least _try_ to continue */
            } else if (wrt == aout_plt_sect + 1) {
                is_pic = 0x40;
                aout_add_reloc(s, segment, RELTYPE_PLT, 4);
            } else if (wrt == aout_gotpc_sect + 1 ||
                       wrt == aout_gotoff_sect + 1 ||
                       wrt == aout_got_sect + 1) {
                error(ERR_NONFATAL, "a.out format cannot produce PC-"
                      "relative GOT references");
            } else {
                error(ERR_NONFATAL, "a.out format does not support this"
                      " use of WRT");
                wrt = NO_SEG;   /* we can at least _try_ to continue */
            }
        }
        p = mydata;
        WRITELONG(p, *(int32_t *)data - (realbytes + s->len));
        aout_sect_write(s, mydata, 4L);
    }
}

static void aout_pad_sections(void)
{
    static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
    /*
     * Pad each of the text and data sections with NOPs until their
     * length is a multiple of four. (NOP == 0x90.) Also increase
     * the length of the BSS section similarly.
     */
    aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
    aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
    sbss.len = (sbss.len + 3) & ~3;
}

/*
 * a.out files have the curious property that all references to
 * things in the data or bss sections are done by addresses which
 * are actually relative to the start of the _text_ section, in the
 * _file_. (No relation to what happens after linking. No idea why
 * this should be so. It's very strange.) So we have to go through
 * the relocation table, _after_ the final size of each section is
 * known, and fix up the relocations pointed to.
 */
static void aout_fixup_relocs(struct Section *sect)
{
    struct Reloc *r;

    saa_rewind(sect->data);
    for (r = sect->head; r; r = r->next) {
        uint8_t *p, *q, blk[4];
        int32_t l;

        saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
        p = q = blk;
        l = *p++;
        if (r->bytes > 1) {
            l += ((int32_t)*p++) << 8;
            if (r->bytes == 4) {
                l += ((int32_t)*p++) << 16;
                l += ((int32_t)*p++) << 24;
            }
        }
        if (r->symbol == -SECT_DATA)
            l += stext.len;
        else if (r->symbol == -SECT_BSS)
            l += stext.len + sdata.len;
        if (r->bytes == 4)
            WRITELONG(q, l);
        else if (r->bytes == 2)
            WRITESHORT(q, l);
        else
            *q++ = l & 0xFF;
        saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
    }
}

static void aout_write(void)
{
    /*
     * Emit the a.out header.
     */
    /* OMAGIC, M_386 or MID_I386, no flags */
    fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
    fwriteint32_t(stext.len, aoutfp);
    fwriteint32_t(sdata.len, aoutfp);
    fwriteint32_t(sbss.len, aoutfp);
    fwriteint32_t(nsyms * 12, aoutfp);     /* length of symbol table */
    fwriteint32_t(0L, aoutfp);     /* object files have no entry point */
    fwriteint32_t(stext.nrelocs * 8, aoutfp);      /* size of text relocs */
    fwriteint32_t(sdata.nrelocs * 8, aoutfp);      /* size of data relocs */

    /*
     * Write out the code section and the data section.
     */
    saa_fpwrite(stext.data, aoutfp);
    saa_fpwrite(sdata.data, aoutfp);

    /*
     * Write out the relocations.
     */
    aout_write_relocs(stext.head);
    aout_write_relocs(sdata.head);

    /*
     * Write the symbol table.
     */
    aout_write_syms();

    /*
     * And the string table.
     */
    fwriteint32_t(strslen + 4, aoutfp);    /* length includes length count */
    saa_fpwrite(strs, aoutfp);
}

static void aout_write_relocs(struct Reloc *r)
{
    while (r) {
        uint32_t word2;

        fwriteint32_t(r->address, aoutfp);

        if (r->symbol >= 0)
            word2 = r->symbol;
        else
            word2 = -r->symbol;
        word2 |= r->reltype << 24;
        word2 |= (r->bytes == 1 ? 0 :
                  r->bytes == 2 ? 0x2000000L : 0x4000000L);
        fwriteint32_t(word2, aoutfp);

        r = r->next;
    }
}

static void aout_write_syms(void)
{
    uint32_t i;

    saa_rewind(syms);
    for (i = 0; i < nsyms; i++) {
        struct Symbol *sym = saa_rstruct(syms);
        fwriteint32_t(sym->strpos, aoutfp);
        fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
        /*
         * Fix up the symbol value now we know the final section
         * sizes.
         */
        if ((sym->type & SECT_MASK) == SECT_DATA)
            sym->value += stext.len;
        if ((sym->type & SECT_MASK) == SECT_BSS)
            sym->value += stext.len + sdata.len;
        fwriteint32_t(sym->value, aoutfp);
        /*
         * Output a size record if necessary.
         */
        if (sym->type & SYM_WITH_SIZE) {
            fwriteint32_t(sym->strpos, aoutfp);
            fwriteint32_t(0x0DL, aoutfp);  /* special value: means size */
            fwriteint32_t(sym->size, aoutfp);
            i++;                /* use up another of `nsyms' */
        }
    }
}

static void aout_sect_write(struct Section *sect,
                            const uint8_t *data, uint32_t len)
{
    saa_wbytes(sect->data, data, len);
    sect->len += len;
}

static int32_t aout_segbase(int32_t segment)
{
    return segment;
}

static int aout_directive(char *directive, char *value, int pass)
{
    (void)directive;
    (void)value;
    (void)pass;
    return 0;
}

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

static const char *aout_stdmac[] = {
    "%define __SECT__ [section .text]",
    "%macro __NASM_CDecl__ 1",
    "%endmacro",
    NULL
};

static int aout_set_info(enum geninfo type, char **val)
{
    (void)type;
    (void)val;
    return 0;
}
#endif                          /* OF_AOUT || OF_AOUTB */

#ifdef OF_AOUT

struct ofmt of_aout = {
    "Linux a.out object files",
    "aout",
    NULL,
    null_debug_arr,
    &null_debug_form,
    aout_stdmac,
    aout_init,
    aout_set_info,
    aout_out,
    aout_deflabel,
    aout_section_names,
    aout_segbase,
    aout_directive,
    aout_filename,
    aout_cleanup
};

#endif

#ifdef OF_AOUTB

struct ofmt of_aoutb = {
    "NetBSD/FreeBSD a.out object files",
    "aoutb",
    NULL,
    null_debug_arr,
    &null_debug_form,
    aout_stdmac,
    aoutb_init,
    aout_set_info,
    aout_out,
    aout_deflabel,
    aout_section_names,
    aout_segbase,
    aout_directive,
    aout_filename,
    aout_cleanup
};

#endif

⌨️ 快捷键说明

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