outelf.c
资源名称:nasm-0.98.zip [点击查看]
上传用户:yuppie_zhu
上传日期:2007-01-08
资源大小:535k
文件大小:31k
源码类别:
编译器/解释器
开发平台:
C/C++
- /* outelf.c output routines for the Netwide Assembler to produce
- * ELF32 (i386 of course) object file format
- *
- * 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_ELF
- /*
- * Relocation types.
- */
- #define R_386_32 1 /* ordinary absolute relocation */
- #define R_386_PC32 2 /* PC-relative relocation */
- #define R_386_GOT32 3 /* an offset into GOT */
- #define R_386_PLT32 4 /* a PC-relative offset into PLT */
- #define R_386_GOTOFF 9 /* an offset from GOT base */
- #define R_386_GOTPC 10 /* a PC-relative offset _to_ GOT */
- struct Reloc {
- struct Reloc *next;
- long address; /* relative to _start_ of section */
- long symbol; /* ELF symbol info thingy */
- int type; /* type of relocation */
- };
- struct Symbol {
- long strpos; /* string table position of name */
- long section; /* section ID of the symbol */
- int type; /* symbol type */
- long value; /* address, or COMMON variable align */
- long size; /* size of symbol */
- long globnum; /* symbol table offset if global */
- struct Symbol *next; /* list of globals in each section */
- struct Symbol *nextfwd; /* list of unresolved-size symbols */
- char *name; /* used temporarily if in above list */
- };
- #define SHT_PROGBITS 1
- #define SHT_NOBITS 8
- #define SHF_WRITE 1
- #define SHF_ALLOC 2
- #define SHF_EXECINSTR 4
- struct Section {
- struct SAA *data;
- unsigned long len, size, nrelocs;
- long index;
- int type; /* SHT_PROGBITS or SHT_NOBITS */
- int align; /* alignment: power of two */
- unsigned long flags; /* section flags */
- char *name;
- struct SAA *rel;
- long rellen;
- struct Reloc *head, **tail;
- struct Symbol *gsyms; /* global symbols in section */
- };
- #define SECT_DELTA 32
- static struct Section **sects;
- static int nsects, sectlen;
- #define SHSTR_DELTA 256
- static char *shstrtab;
- static int shstrtablen, shstrtabsize;
- static struct SAA *syms;
- static unsigned long nlocals, nglobs;
- static long def_seg;
- static struct RAA *bsym;
- static struct SAA *strs;
- static unsigned long strslen;
- static FILE *elffp;
- static efunc error;
- static evalfunc evaluate;
- static struct Symbol *fwds;
- static char elf_module[FILENAME_MAX];
- extern struct ofmt of_elf;
- #define SHN_ABS 0xFFF1
- #define SHN_COMMON 0xFFF2
- #define SHN_UNDEF 0
- #define SYM_SECTION 0x04
- #define SYM_GLOBAL 0x10
- #define SYM_DATA 0x01
- #define SYM_FUNCTION 0x02
- #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
- #define SEG_ALIGN 16 /* alignment of sections in file */
- #define SEG_ALIGN_1 (SEG_ALIGN-1)
- static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
- #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
- static struct ELF_SECTDATA {
- void *data;
- long len;
- int is_saa;
- } *elf_sects;
- static int elf_nsect;
- static long elf_foffs;
- static void elf_write(void);
- static void elf_sect_write(struct Section *, unsigned char *, unsigned long);
- static void elf_section_header (int, int, int, void *, int, long,
- int, int, int, int);
- static void elf_write_sections (void);
- static struct SAA *elf_build_symtab (long *, long *);
- static struct SAA *elf_build_reltab (long *, struct Reloc *);
- static void add_sectname (char *, char *);
- /*
- * Special section numbers which are used to define ELF special
- * symbols, which can be used with WRT to provide PIC relocation
- * types.
- */
- static long elf_gotpc_sect, elf_gotoff_sect;
- static long elf_got_sect, elf_plt_sect;
- static long elf_sym_sect;
- static void elf_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
- {
- elffp = fp;
- error = errfunc;
- evaluate = eval;
- (void) ldef; /* placate optimisers */
- sects = NULL;
- nsects = sectlen = 0;
- syms = saa_init((long)sizeof(struct Symbol));
- nlocals = nglobs = 0;
- bsym = raa_init();
- strs = saa_init(1L);
- saa_wbytes (strs, "