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

📄 outaout.c

📁 nasm早期的源代码,比较简单是学习汇编和编译原理的好例子
💻 C
📖 第 1 页 / 共 3 页
字号:
/* outaout.c	output routines for the Netwide Assembler to produce
 *		Linux a.out 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 "compiler.h"

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

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

#if defined OF_AOUT || defined OF_AOUTB

#define RELTYPE_ABSOLUTE 0x00
#define RELTYPE_RELATIVE 0x01
#define RELTYPE_GOTPC    0x01   /* no explicit GOTPC in a.out */
#define RELTYPE_GOTOFF   0x10
#define RELTYPE_GOT      0x10   /* distinct from GOTOFF bcos sym not sect */
#define RELTYPE_PLT      0x21
#define RELTYPE_SYMFLAG  0x08

struct Reloc {
    struct Reloc *next;
    int32_t address;               /* relative to _start_ of section */
    int32_t symbol;                /* symbol number or -ve section id */
    int bytes;                  /* 2 or 4 */
    int reltype;                /* see above */
};

struct Symbol {
    int32_t strpos;                /* string table position of name */
    int type;                   /* symbol type - see flags below */
    int32_t value;                 /* address, or COMMON variable size */
    int32_t size;                  /* size for data or function exports */
    int32_t segment;               /* back-reference used by gsym_reloc */
    struct Symbol *next;        /* list of globals in each section */
    struct Symbol *nextfwd;     /* list of unresolved-size symbols */
    char *name;                 /* for unresolved-size symbols */
    int32_t symnum;                /* index into symbol table */
};

/*
 * Section IDs - used in Reloc.symbol when negative, and in
 * Symbol.type when positive.
 */
#define SECT_ABS 2              /* absolute value */
#define SECT_TEXT 4             /* text section */
#define SECT_DATA 6             /* data section */
#define SECT_BSS 8              /* bss section */
#define SECT_MASK 0xE           /* mask out any of the above */

/*
 * More flags used in Symbol.type.
 */
#define SYM_GLOBAL 1            /* it's a global symbol */
#define SYM_DATA 0x100          /* used for shared libs */
#define SYM_FUNCTION 0x200      /* used for shared libs */
#define SYM_WITH_SIZE 0x4000    /* not output; internal only */

/*
 * Bit more explanation of symbol types: SECT_xxx denotes a local
 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
 * this module. Just SYM_GLOBAL, with zero value, denotes an
 * external symbol referenced in this module. And just SYM_GLOBAL,
 * but with a non-zero value, declares a C `common' variable, of
 * size `value'.
 */

struct Section {
    struct SAA *data;
    uint32_t len, size, nrelocs;
    int32_t index;
    struct Reloc *head, **tail;
    struct Symbol *gsyms, *asym;
};

static struct Section stext, sdata, sbss;

static struct SAA *syms;
static uint32_t nsyms;

static struct RAA *bsym;

static struct SAA *strs;
static uint32_t strslen;

static struct Symbol *fwds;

static FILE *aoutfp;
static efunc error;
static evalfunc evaluate;

static int bsd;
static int is_pic;

static void aout_write(void);
static void aout_write_relocs(struct Reloc *);
static void aout_write_syms(void);
static void aout_sect_write(struct Section *, const uint8_t *,
                            uint32_t);
static void aout_pad_sections(void);
static void aout_fixup_relocs(struct Section *);

/*
 * Special section numbers which are used to define special
 * symbols, which can be used with WRT to provide PIC relocation
 * types.
 */
static int32_t aout_gotpc_sect, aout_gotoff_sect;
static int32_t aout_got_sect, aout_plt_sect;
static int32_t aout_sym_sect;

static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
                       evalfunc eval)
{
    aoutfp = fp;
    error = errfunc;
    evaluate = eval;
    (void)ldef;                 /* placate optimisers */
    stext.data = saa_init(1L);
    stext.head = NULL;
    stext.tail = &stext.head;
    sdata.data = saa_init(1L);
    sdata.head = NULL;
    sdata.tail = &sdata.head;
    stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
    stext.nrelocs = sdata.nrelocs = 0;
    stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
    stext.index = seg_alloc();
    sdata.index = seg_alloc();
    sbss.index = seg_alloc();
    stext.asym = sdata.asym = sbss.asym = NULL;
    syms = saa_init((int32_t)sizeof(struct Symbol));
    nsyms = 0;
    bsym = raa_init();
    strs = saa_init(1L);
    strslen = 0;
    fwds = NULL;
}

#ifdef OF_AOUT

static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
{
    bsd = false;
    aoutg_init(fp, errfunc, ldef, eval);

    aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
        aout_plt_sect = aout_sym_sect = NO_SEG;
}

#endif

#ifdef OF_AOUTB

extern struct ofmt of_aoutb;

static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
                       evalfunc eval)
{
    bsd = true;
    aoutg_init(fp, errfunc, ldef, eval);

    is_pic = 0x00;              /* may become 0x40 */

    aout_gotpc_sect = seg_alloc();
    ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false, &of_aoutb,
         error);
    aout_gotoff_sect = seg_alloc();
    ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false,
         &of_aoutb, error);
    aout_got_sect = seg_alloc();
    ldef("..got", aout_got_sect + 1, 0L, NULL, false, false, &of_aoutb,
         error);
    aout_plt_sect = seg_alloc();
    ldef("..plt", aout_plt_sect + 1, 0L, NULL, false, false, &of_aoutb,
         error);
    aout_sym_sect = seg_alloc();
    ldef("..sym", aout_sym_sect + 1, 0L, NULL, false, false, &of_aoutb,
         error);
}

#endif

static void aout_cleanup(int debuginfo)
{
    struct Reloc *r;

    (void)debuginfo;

    aout_pad_sections();
    aout_fixup_relocs(&stext);
    aout_fixup_relocs(&sdata);
    aout_write();
    fclose(aoutfp);
    saa_free(stext.data);
    while (stext.head) {
        r = stext.head;
        stext.head = stext.head->next;
        nasm_free(r);
    }
    saa_free(sdata.data);
    while (sdata.head) {
        r = sdata.head;
        sdata.head = sdata.head->next;
        nasm_free(r);
    }
    saa_free(syms);
    raa_free(bsym);
    saa_free(strs);
}

static int32_t aout_section_names(char *name, int pass, int *bits)
{
    
    (void)pass;

    /*
     * Default to 32 bits.
     */
    if (!name)
        *bits = 32;

    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 sbss.index;
    else
        return NO_SEG;
}

static void aout_deflabel(char *name, int32_t segment, int32_t offset,
                          int is_global, char *special)
{
    int pos = strslen + 4;
    struct Symbol *sym;
    int special_used = false;

    if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
        /*
         * This is a NASM special symbol. We never allow it into
         * the a.out symbol table, even if it's a valid one. If it
         * _isn't_ a valid one, we should barf immediately.
         */
        if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
            strcmp(name, "..got") && strcmp(name, "..plt") &&
            strcmp(name, "..sym"))
            error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
        return;
    }

    if (is_global == 3) {
        struct Symbol **s;
        /*
         * Fix up a forward-reference symbol size from the first
         * pass.
         */
        for (s = &fwds; *s; s = &(*s)->nextfwd)
            if (!strcmp((*s)->name, name)) {
                struct tokenval tokval;
                expr *e;
                char *p = special;

                while (*p && !isspace(*p))
                    p++;
                while (*p && isspace(*p))
                    p++;
                stdscan_reset();
                stdscan_bufptr = p;
                tokval.t_type = TOKEN_INVALID;
                e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
                if (e) {
                    if (!is_simple(e))
                        error(ERR_NONFATAL, "cannot use relocatable"
                              " expression as symbol size");
                    else
                        (*s)->size = reloc_value(e);
                }

                /*
                 * Remove it from the list of unresolved sizes.
                 */
                nasm_free((*s)->name);
                *s = (*s)->nextfwd;
                return;
            }
        return;                 /* it wasn't an important one */
    }

    saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
    strslen += 1 + strlen(name);

    sym = saa_wstruct(syms);

    sym->strpos = pos;
    sym->type = is_global ? SYM_GLOBAL : 0;
    sym->segment = segment;
    if (segment == NO_SEG)
        sym->type |= SECT_ABS;
    else if (segment == stext.index) {
        sym->type |= SECT_TEXT;
        if (is_global) {
            sym->next = stext.gsyms;
            stext.gsyms = sym;
        } else if (!stext.asym)
            stext.asym = sym;

⌨️ 快捷键说明

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