Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1138933
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: LINUX

2009-03-22 12:30:12

static void elf_deflabel(char *name, long segment, long offset, int is_global, char *special)
{
    int pos = strslen;
    struct Symbol *sym;
    int special_used = FALSE;

#if defined(DEBUG) && DEBUG>2
    fprintf(stderr, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n", name, segment, offset, is_global, special);
#endif
    if (name[0] == '.' && name[1] == '.' && name[2] != '@') { /* label1: ; a non-local label
                                                                      * .local: ; this is really label1.local
                                                                      * ..@@foo: ; this is a special symbol
                                                                      * label2: ; another non-local label
                                                                      * .local: ; this is really label2.local
                                                                      * jmp ..@@foo ; this will jump three lines up
                                                                     */

        /*
         * This is a NASM special symbol. We never allow it into
         * the ELF symbol table, even if it's a valid one. If it
         * _isn't_ a valid one, we should barf immediately.
         */

        /* 要么是..@,要么是下面5个特殊符号之一,要么出错 */
        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) { /* 已经定义的全局变量,在 nasm.c 中被调用 */
        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); /* 计算该全局变量的 size */
                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, (long)(1 + strlen(name)));
    strslen += 1 + strlen(name);

    sym = saa_wstruct(syms);

    sym->strpos = pos;
    sym->type = is_global ? SYM_GLOBAL : 0;
    sym->size = 0;
    if (segment == NO_SEG)
        sym->section = SHN_ABS;
    else {
        int i;
        sym->section = SHN_UNDEF;
        if (nsects == 0 && segment == def_seg) {
            int tempint;
            if (segment != elf_section_names(".text", 2, &tempint))
                error(ERR_PANIC, "strange segment conditions in ELF driver");
            sym->section = nsects;
        } else {
            for (i = 0; i < nsects; i++)
                if (segment == sects[i]->index) {
                    sym->section = i + 1; /* ELF 中, section ID 从 1 开始计算,0 reserved */
                    break;
                }
        }
    }

    if (is_global == 2) { /* 在 define_common 中传递 2 作为参数 */
        sym->size = offset;
        sym->value = 0;
        sym->section = SHN_COMMON;
        /*
         * We have a common variable. Check the special text to see
         * if it's a valid number and power of two; if so, store it
         * as the alignment for the common variable.
         */

        if (special) {
            int err;
            sym->value = readnum(special, &err);
            if (err)
                error(ERR_NONFATAL, "alignment constraint `%s' is not a valid number", special);
            else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
                error(ERR_NONFATAL, "alignment constraint `%s' is not a power of two", special);
        }
        special_used = TRUE;
    } else
        sym->value = (sym->section == SHN_UNDEF ? 0 : offset);

    if (sym->type == SYM_GLOBAL) {
        /*
         * There's a problem here that needs fixing.
         * If sym->section == SHN_ABS, then the first line of the
         * else section causes a core dump, because its a reference
         * beyond the end of the section array.
         * This behaviour is exhibited by this code:
         * GLOBAL crash_nasm
         * crash_nasm equ 0
         *
         * I'm not sure how to procede, because I haven't got the
         * first clue about how ELF works, so I don't know what to
         * do with it. Furthermore, I'm not sure what the rest of this
         * section of code does. Help?
         *
         * For now, I'll see if doing absolutely nothing with it will
         * work...
         */

        if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
            bsym = raa_write(bsym, segment, nglobs);
        } else if (sym->section != SHN_ABS) {
            /*
             * This is a global symbol; so we must add it to the linked
             * list of global symbols in its section. We'll push it on
             * the beginning of the list, because it doesn't matter
             * much which end we put it on and it's easier like this.
             *
             * In addition, we check the special text for symbol
             * type and size information.
             */

            sym->next = sects[sym->section - 1]->gsyms;
            sects[sym->section - 1]->gsyms = sym;

            if (special) { /* special 的类型只能是 function 或者 data, object */
                int n = strcspn(special, " ");

                if (!nasm_strnicmp(special, "function", n))
                    sym->type |= SYM_FUNCTION;
                else if (!nasm_strnicmp(special, "data", n) || !nasm_strnicmp(special, "object", n))
                    sym->type |= SYM_DATA;
                else
                    error(ERR_NONFATAL, "unrecognised symbol type `%.*s'", n, special);
                if (special[n]) { /* global hashtable:data (hashtable.end - hashtable) */
                    struct tokenval tokval;
                    expr *e;
                    int fwd = FALSE;
                    char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */

                    while (special[n] && isspace(special[n]))
                        n++;
                    /*
                     * We have a size expression; attempt to
                     * evaluate it.
                     */

                    stdscan_reset();
                    stdscan_bufptr = special + n;
                    tokval.t_type = TOKEN_INVALID;
                    e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
                    if (fwd) {
                        sym->nextfwd = fwds;
                        fwds = sym;
                        sym->name = nasm_strdup(name);
                    } else if (e) {
                        if (!is_simple(e))
                            error(ERR_NONFATAL, "cannot use relocatable expression as symbol size");
                        else
                            sym->size = reloc_value(e);
                    }
                    stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
                }
                special_used = TRUE;
            }
        }
        sym->globnum = nglobs;
        nglobs++;
    } else
        nlocals++;

    if (special && !special_used)
        error(ERR_NONFATAL, "no special symbol features supported here"); /* extern temp:dword 显示此出错信息 */
}

阅读(2198) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~