📄 outelf64.c
字号:
/* outelf.c output routines for the Netwide Assembler to produce
* ELF64 (x86_64 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 "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"
/* Definitions in lieu of elf.h */
#define SHT_PROGBITS 1
#define SHT_RELA 4 /* Relocation entries with addends */
#define SHT_NOBITS 8
#define SHF_WRITE (1 << 0) /* Writable */
#define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
#define SHF_EXECINSTR (1 << 2) /* Executable */
#define SHN_ABS 0xfff1 /* Associated symbol is absolute */
#define SHN_COMMON 0xfff2 /* Associated symbol is common */
#define R_X86_64_NONE 0 /* No reloc */
#define R_X86_64_64 1 /* Direct 64 bit address */
#define R_X86_64_PC32 2 /* PC relative 32 bit signed */
#define R_X86_64_GOT32 3 /* 32 bit GOT entry */
#define R_X86_64_PLT32 4 /* 32 bit PLT address */
#define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
#define R_X86_64_32 10 /* Direct 32 bit zero extended */
#define R_X86_64_16 12 /* Direct 16 bit zero extended */
#define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
#define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
#define ET_REL 1 /* Relocatable file */
#define EM_X86_64 62 /* AMD x86-64 architecture */
typedef uint32_t Elf64_Word;
typedef uint64_t Elf64_Xword;
typedef uint64_t Elf64_Addr;
typedef uint64_t Elf64_Off;
typedef struct
{
Elf64_Word sh_name; /* Section name (string tbl index) */
Elf64_Word sh_type; /* Section type */
Elf64_Xword sh_flags; /* Section flags */
Elf64_Addr sh_addr; /* Section virtual addr at execution */
Elf64_Off sh_offset; /* Section file offset */
Elf64_Xword sh_size; /* Section size in bytes */
Elf64_Word sh_link; /* Link to another section */
Elf64_Word sh_info; /* Additional section information */
Elf64_Xword sh_addralign; /* Section alignment */
Elf64_Xword sh_entsize; /* Entry size if section holds table */
} Elf64_Shdr;
#ifdef OF_ELF64
struct Reloc {
struct Reloc *next;
int64_t address; /* relative to _start_ of section */
int64_t symbol; /* ELF symbol info thingy */
int type; /* type of relocation */
};
struct Symbol {
int32_t strpos; /* string table position of name */
int32_t section; /* section ID of the symbol */
int type; /* symbol type */
int other; /* symbol visibility */
int64_t value; /* address, or COMMON variable align */
int32_t size; /* size of symbol */
int32_t 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 */
};
struct Section {
struct SAA *data;
uint32_t len, size, nrelocs;
int32_t index;
int type; /* SHT_PROGBITS or SHT_NOBITS */
int align; /* alignment: power of two */
uint32_t flags; /* section flags */
char *name;
struct SAA *rel;
int32_t 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 uint32_t nlocals, nglobs;
static int32_t def_seg;
static struct RAA *bsym;
static struct SAA *strs;
static uint32_t strslen;
static FILE *elffp;
static efunc error;
static evalfunc evaluate;
static struct Symbol *fwds;
static char elf_module[FILENAME_MAX];
extern struct ofmt of_elf64;
#define SHN_UNDEF 0
#define SYM_SECTION 0x04
#define SYM_GLOBAL 0x10
#define SYM_NOTYPE 0x00
#define SYM_DATA 0x01
#define SYM_FUNCTION 0x02
#define STV_DEFAULT 0
#define STV_INTERNAL 1
#define STV_HIDDEN 2
#define STV_PROTECTED 3
#define GLOBAL_TEMP_BASE 16 /* 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;
int32_t len;
bool is_saa;
} *elf_sects;
static int elf_nsect;
static int32_t elf_foffs;
static void elf_write(void);
static void elf_sect_write(struct Section *, const uint8_t *,
uint32_t);
static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
int, int);
static void elf_write_sections(void);
static struct SAA *elf_build_symtab(int32_t *, int32_t *);
static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
static void add_sectname(char *, char *);
/* this stuff is needed for the stabs debugging format */
#define N_SO 0x64 /* ID for main source file */
#define N_SOL 0x84 /* ID for sub-source file */
#define N_BINCL 0x82
#define N_EINCL 0xA2
#define N_SLINE 0x44
#define TY_STABSSYMLIN 0x40 /* ouch */
struct stabentry {
uint32_t n_strx;
uint8_t n_type;
uint8_t n_other;
uint16_t n_desc;
uint32_t n_value;
};
struct erel {
int offset, info;
};
struct symlininfo {
int offset;
int section; /* section index */
char *name; /* shallow-copied pointer of section name */
};
struct linelist {
struct symlininfo info;
int line;
char *filename;
struct linelist *next;
struct linelist *last;
};
static struct linelist *stabslines = 0;
static int stabs_immcall = 0;
static int currentline = 0;
static int numlinestabs = 0;
static char *stabs_filename = 0;
static int symtabsection;
static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
static int stablen, stabstrlen, stabrellen;
static struct dfmt df_stabs;
void stabs64_init(struct ofmt *, void *, FILE *, efunc);
void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
void stabs64_directive(const char *, const char *);
void stabs64_typevalue(int32_t);
void stabs64_output(int, void *);
void stabs64_generate(void);
void stabs64_cleanup(void);
/* end of stabs debugging stuff */
/*
* Special section numbers which are used to define ELF special
* symbols, which can be used with WRT to provide PIC relocation
* types.
*/
static int32_t elf_gotpc_sect, elf_gotoff_sect;
static int32_t elf_got_sect, elf_plt_sect;
static int32_t elf_sym_sect;
static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
{
maxbits = 64;
elffp = fp;
error = errfunc;
evaluate = eval;
(void)ldef; /* placate optimisers */
sects = NULL;
nsects = sectlen = 0;
syms = saa_init((int32_t)sizeof(struct Symbol));
nlocals = nglobs = 0;
bsym = raa_init();
strs = saa_init(1L);
saa_wbytes(strs, "\0", 1L);
saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
strslen = 2 + strlen(elf_module);
shstrtab = NULL;
shstrtablen = shstrtabsize = 0;;
add_sectname("", "");
fwds = NULL;
elf_gotpc_sect = seg_alloc();
ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
error);
elf_gotoff_sect = seg_alloc();
ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
error);
elf_got_sect = seg_alloc();
ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
error);
elf_plt_sect = seg_alloc();
ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
error);
elf_sym_sect = seg_alloc();
ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
error);
def_seg = seg_alloc();
}
static void elf_cleanup(int debuginfo)
{
struct Reloc *r;
int i;
(void)debuginfo;
elf_write();
fclose(elffp);
for (i = 0; i < nsects; i++) {
if (sects[i]->type != SHT_NOBITS)
saa_free(sects[i]->data);
if (sects[i]->head)
saa_free(sects[i]->rel);
while (sects[i]->head) {
r = sects[i]->head;
sects[i]->head = sects[i]->head->next;
nasm_free(r);
}
}
nasm_free(sects);
saa_free(syms);
raa_free(bsym);
saa_free(strs);
if (of_elf64.current_dfmt) {
of_elf64.current_dfmt->cleanup();
}
}
static void add_sectname(char *firsthalf, char *secondhalf)
{
int len = strlen(firsthalf) + strlen(secondhalf);
while (shstrtablen + len + 1 > shstrtabsize)
shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
strcpy(shstrtab + shstrtablen, firsthalf);
strcat(shstrtab + shstrtablen, secondhalf);
shstrtablen += len + 1;
}
static int elf_make_section(char *name, int type, int flags, int align)
{
struct Section *s;
s = nasm_malloc(sizeof(*s));
if (type != SHT_NOBITS)
s->data = saa_init(1L);
s->head = NULL;
s->tail = &s->head;
s->len = s->size = 0;
s->nrelocs = 0;
if (!strcmp(name, ".text"))
s->index = def_seg;
else
s->index = seg_alloc();
add_sectname("", name);
s->name = nasm_malloc(1 + strlen(name));
strcpy(s->name, name);
s->type = type;
s->flags = flags;
s->align = align;
s->gsyms = NULL;
if (nsects >= sectlen)
sects =
nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
sects[nsects++] = s;
return nsects - 1;
}
static int32_t elf_section_names(char *name, int pass, int *bits)
{
char *p;
int flags_and, flags_or, type, align, i;
/*
* Default is 64 bits.
*/
if (!name) {
*bits = 64;
return def_seg;
}
p = name;
while (*p && !isspace(*p))
p++;
if (*p)
*p++ = '\0';
flags_and = flags_or = type = align = 0;
while (*p && isspace(*p))
p++;
while (*p) {
char *q = p;
while (*p && !isspace(*p))
p++;
if (*p)
*p++ = '\0';
while (*p && isspace(*p))
p++;
if (!nasm_strnicmp(q, "align=", 6)) {
align = atoi(q + 6);
if (align == 0)
align = 1;
if ((align - 1) & align) { /* means it's not a power of two */
error(ERR_NONFATAL, "section alignment %d is not"
" a power of two", align);
align = 1;
}
} else if (!nasm_stricmp(q, "alloc")) {
flags_and |= SHF_ALLOC;
flags_or |= SHF_ALLOC;
} else if (!nasm_stricmp(q, "noalloc")) {
flags_and |= SHF_ALLOC;
flags_or &= ~SHF_ALLOC;
} else if (!nasm_stricmp(q, "exec")) {
flags_and |= SHF_EXECINSTR;
flags_or |= SHF_EXECINSTR;
} else if (!nasm_stricmp(q, "noexec")) {
flags_and |= SHF_EXECINSTR;
flags_or &= ~SHF_EXECINSTR;
} else if (!nasm_stricmp(q, "write")) {
flags_and |= SHF_WRITE;
flags_or |= SHF_WRITE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -