📄 outcoff.c
字号:
/* outcoff.c output routines for the Netwide Assembler to produce
* COFF object files (for DJGPP and Win32)
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the license given in the file "LICENSE"
* distributed in the NASM archive.
*/
#include "compiler.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <inttypes.h>
#include "nasm.h"
#include "nasmlib.h"
#include "saa.h"
#include "raa.h"
#include "outform.h"
#if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
/*
* Notes on COFF:
*
* (0) When I say `standard COFF' below, I mean `COFF as output and
* used by DJGPP'. I assume DJGPP gets it right.
*
* (1) Win32 appears to interpret the term `relative relocation'
* differently from standard COFF. Standard COFF understands a
* relative relocation to mean that during relocation you add the
* address of the symbol you're referencing, and subtract the base
* address of the section you're in. Win32 COFF, by contrast, seems
* to add the address of the symbol and then subtract the address
* of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
* subtly incompatible.
*
* (2) Win32 doesn't bother putting any flags in the header flags
* field (at offset 0x12 into the file).
*
* (3) Win32 uses some extra flags into the section header table:
* it defines flags 0x80000000 (writable), 0x40000000 (readable)
* and 0x20000000 (executable), and uses them in the expected
* combinations. It also defines 0x00100000 through 0x00700000 for
* section alignments of 1 through 64 bytes.
*
* (4) Both standard COFF and Win32 COFF seem to use the DWORD
* field directly after the section name in the section header
* table for something strange: they store what the address of the
* section start point _would_ be, if you laid all the sections end
* to end starting at zero. Dunno why. Microsoft's documentation
* lists this field as "Virtual Size of Section", which doesn't
* seem to fit at all. In fact, Win32 even includes non-linked
* sections such as .drectve in this calculation.
*
* Newer versions of MASM seem to have changed this to be zero, and
* that apparently matches the COFF spec, so go with that.
*
* (5) Standard COFF does something very strange to common
* variables: the relocation point for a common variable is as far
* _before_ the variable as its size stretches out _after_ it. So
* we must fix up common variable references. Win32 seems to be
* sensible on this one.
*/
/* Flag which version of COFF we are currently outputting. */
static bool win32, win64;
static int32_t imagebase_sect;
#define WRT_IMAGEBASE "..imagebase"
struct Reloc {
struct Reloc *next;
int32_t address; /* relative to _start_ of section */
int32_t symbol; /* symbol number */
enum {
SECT_SYMBOLS,
ABS_SYMBOL,
REAL_SYMBOLS
} symbase; /* relocation for symbol number :) */
int16_t type;
};
/* possible values for Reloc->type */
#define IMAGE_REL_AMD64_ADDR64 0x0001
#define IMAGE_REL_AMD64_ADDR32 0x0002
#define IMAGE_REL_AMD64_ADDR32NB 0x0003
#define IMAGE_REL_AMD64_REL32 0x0004
#define IMAGE_REL_I386_DIR32 0x0006
#define IMAGE_REL_I386_DIR32NB 0x0007
#define IMAGE_REL_I386_REL32 0x0014
struct Symbol {
char name[9];
int32_t strpos; /* string table position of name */
int32_t value; /* address, or COMMON variable size */
int section; /* section number where it's defined
* - in COFF codes, not NASM codes */
bool is_global; /* is it a global symbol or not? */
int16_t type; /* 0 - notype, 0x20 - function */
int32_t namlen; /* full name length */
};
static FILE *coffp;
static efunc error;
static char coff_infile[FILENAME_MAX];
struct Section {
struct SAA *data;
uint32_t len;
int nrelocs;
int32_t index;
struct Reloc *head, **tail;
uint32_t flags; /* section flags */
char name[9];
int32_t pos, relpos;
};
#define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
#define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
#define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
#define INFO_FLAGS 0x00100A00L
#define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
#define SECT_DELTA 32
static struct Section **sects;
static int nsects, sectlen;
static struct SAA *syms;
static uint32_t nsyms;
static int32_t def_seg;
static int initsym;
static struct RAA *bsym, *symval;
static struct SAA *strs;
static uint32_t strslen;
static void coff_gen_init(FILE *, efunc);
static void coff_sect_write(struct Section *, const uint8_t *,
uint32_t);
static void coff_write(void);
static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
static void coff_write_relocs(struct Section *);
static void coff_write_symbols(void);
static void coff_win32_init(FILE * fp, efunc errfunc,
ldfunc ldef, evalfunc eval)
{
win32 = true; win64 = false;
(void)ldef; /* placate optimizers */
(void)eval;
coff_gen_init(fp, errfunc);
}
static void coff_win64_init(FILE * fp, efunc errfunc,
ldfunc ldef, evalfunc eval)
{
extern struct ofmt of_win64;
maxbits = 64;
win32 = false; win64 = true;
(void)ldef; /* placate optimizers */
(void)eval;
coff_gen_init(fp, errfunc);
imagebase_sect = seg_alloc()+1;
ldef(WRT_IMAGEBASE,imagebase_sect,0,NULL,false,false,&of_win64,errfunc);
}
static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
evalfunc eval)
{
win32 = win64 = false;
(void)ldef; /* placate optimizers */
(void)eval;
coff_gen_init(fp, errfunc);
}
static void coff_gen_init(FILE * fp, efunc errfunc)
{
coffp = fp;
error = errfunc;
sects = NULL;
nsects = sectlen = 0;
syms = saa_init(sizeof(struct Symbol));
nsyms = 0;
bsym = raa_init();
symval = raa_init();
strs = saa_init(1);
strslen = 0;
def_seg = seg_alloc();
}
static void coff_cleanup(int debuginfo)
{
struct Reloc *r;
int i;
(void)debuginfo;
coff_write();
fclose(coffp);
for (i = 0; i < nsects; i++) {
if (sects[i]->data)
saa_free(sects[i]->data);
while (sects[i]->head) {
r = sects[i]->head;
sects[i]->head = sects[i]->head->next;
nasm_free(r);
}
nasm_free(sects[i]);
}
nasm_free(sects);
saa_free(syms);
raa_free(bsym);
raa_free(symval);
saa_free(strs);
}
static int coff_make_section(char *name, uint32_t flags)
{
struct Section *s;
s = nasm_malloc(sizeof(*s));
if (flags != BSS_FLAGS)
s->data = saa_init(1);
else
s->data = NULL;
s->head = NULL;
s->tail = &s->head;
s->len = 0;
s->nrelocs = 0;
if (!strcmp(name, ".text"))
s->index = def_seg;
else
s->index = seg_alloc();
strncpy(s->name, name, 8);
s->name[8] = '\0';
s->flags = flags;
if (nsects >= sectlen) {
sectlen += SECT_DELTA;
sects = nasm_realloc(sects, sectlen * sizeof(*sects));
}
sects[nsects++] = s;
return nsects - 1;
}
static int32_t coff_section_names(char *name, int pass, int *bits)
{
char *p;
uint32_t flags, align_and = ~0L, align_or = 0L;
int i;
/*
* Set default bits.
*/
if (!name) {
if(win64)
*bits = 64;
else
*bits = 32;
}
if (!name)
return def_seg;
p = name;
while (*p && !nasm_isspace(*p))
p++;
if (*p)
*p++ = '\0';
if (strlen(name) > 8) {
error(ERR_WARNING, "COFF section names limited to 8 characters:"
" truncating");
name[8] = '\0';
}
flags = 0;
while (*p && nasm_isspace(*p))
p++;
while (*p) {
char *q = p;
while (*p && !nasm_isspace(*p))
p++;
if (*p)
*p++ = '\0';
while (*p && nasm_isspace(*p))
p++;
if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
flags = TEXT_FLAGS;
} else if (!nasm_stricmp(q, "data")) {
flags = DATA_FLAGS;
} else if (!nasm_stricmp(q, "rdata")) {
if (win32 | win64)
flags = RDATA_FLAGS;
else {
flags = DATA_FLAGS; /* gotta do something */
error(ERR_NONFATAL, "standard COFF does not support"
" read-only data sections");
}
} else if (!nasm_stricmp(q, "bss")) {
flags = BSS_FLAGS;
} else if (!nasm_stricmp(q, "info")) {
if (win32 | win64)
flags = INFO_FLAGS;
else {
flags = DATA_FLAGS; /* gotta do something */
error(ERR_NONFATAL, "standard COFF does not support"
" informational sections");
}
} else if (!nasm_strnicmp(q, "align=", 6)) {
if (!(win32 | win64))
error(ERR_NONFATAL, "standard COFF does not support"
" section alignment specification");
else {
if (q[6 + strspn(q + 6, "0123456789")])
error(ERR_NONFATAL,
"argument to `align' is not numeric");
else {
unsigned int align = atoi(q + 6);
if (!align || ((align - 1) & align))
error(ERR_NONFATAL, "argument to `align' is not a"
" power of two");
else if (align > 64)
error(ERR_NONFATAL, "Win32 cannot align sections"
" to better than 64-byte boundaries");
else {
align_and = ~0x00F00000L;
align_or = (align == 1 ? 0x00100000L :
align == 2 ? 0x00200000L :
align == 4 ? 0x00300000L :
align == 8 ? 0x00400000L :
align == 16 ? 0x00500000L :
align ==
32 ? 0x00600000L : 0x00700000L);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -