2002-05-01 04:51:32 +08:00
|
|
|
/* 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
|
2007-12-29 22:44:23 +08:00
|
|
|
* redistributable under the license given in the file "LICENSE"
|
2002-05-01 04:51:32 +08:00
|
|
|
* distributed in the NASM archive.
|
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
2007-04-12 10:40:54 +08:00
|
|
|
#include <inttypes.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2008-06-10 08:32:43 +08:00
|
|
|
#include "saa.h"
|
2008-06-10 08:40:16 +08:00
|
|
|
#include "raa.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "outform.h"
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
#if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*
|
2002-05-01 04:52:49 +08:00
|
|
|
* (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.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
|
|
|
* (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
|
2002-05-01 04:52:49 +08:00
|
|
|
* sections such as .drectve in this calculation.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2007-09-25 08:02:07 +08:00
|
|
|
* Newer versions of MASM seem to have changed this to be zero, and
|
|
|
|
* that apparently matches the COFF spec, so go with that.
|
|
|
|
*
|
2002-05-01 04:51:32 +08:00
|
|
|
* (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. */
|
2007-10-11 05:58:45 +08:00
|
|
|
static bool win32, win64;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-05-24 10:45:39 +08:00
|
|
|
static int32_t imagebase_sect;
|
|
|
|
#define WRT_IMAGEBASE "..imagebase"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc {
|
|
|
|
struct Reloc *next;
|
2007-10-11 05:58:45 +08:00
|
|
|
int32_t address; /* relative to _start_ of section */
|
|
|
|
int32_t symbol; /* symbol number */
|
2002-05-01 04:51:53 +08:00
|
|
|
enum {
|
2005-01-16 06:15:51 +08:00
|
|
|
SECT_SYMBOLS,
|
|
|
|
ABS_SYMBOL,
|
|
|
|
REAL_SYMBOLS
|
|
|
|
} symbase; /* relocation for symbol number :) */
|
2008-05-24 10:45:39 +08:00
|
|
|
int16_t type;
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
|
|
|
|
2008-05-24 10:45:39 +08:00
|
|
|
/* 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
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Symbol {
|
2007-04-14 00:47:53 +08:00
|
|
|
char name[9];
|
2007-10-11 05:58:45 +08:00
|
|
|
int32_t strpos; /* string table position of name */
|
|
|
|
int32_t value; /* address, or COMMON variable size */
|
2005-01-16 06:15:51 +08:00
|
|
|
int section; /* section number where it's defined
|
|
|
|
* - in COFF codes, not NASM codes */
|
2007-10-11 05:58:45 +08:00
|
|
|
bool is_global; /* is it a global symbol or not? */
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
int16_t type; /* 0 - notype, 0x20 - function */
|
|
|
|
int32_t namlen; /* full name length */
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static FILE *coffp;
|
|
|
|
static efunc error;
|
2007-04-14 00:47:53 +08:00
|
|
|
static char coff_infile[FILENAME_MAX];
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
struct Section {
|
|
|
|
struct SAA *data;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t len;
|
2002-05-01 04:51:32 +08:00
|
|
|
int nrelocs;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t index;
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc *head, **tail;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t flags; /* section flags */
|
2007-04-14 00:47:53 +08:00
|
|
|
char name[9];
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t pos, relpos;
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
#define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
|
|
|
|
#define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
|
|
|
|
#define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
|
2002-05-01 04:51:53 +08:00
|
|
|
#define INFO_FLAGS 0x00100A00L
|
2007-04-12 10:40:54 +08:00
|
|
|
#define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
|
2002-05-01 04:51:53 +08:00
|
|
|
|
|
|
|
#define SECT_DELTA 32
|
|
|
|
static struct Section **sects;
|
|
|
|
static int nsects, sectlen;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
static struct SAA *syms;
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t nsyms;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t def_seg;
|
2002-05-01 04:51:53 +08:00
|
|
|
|
|
|
|
static int initsym;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
static struct RAA *bsym, *symval;
|
|
|
|
|
|
|
|
static struct SAA *strs;
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t strslen;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
static void coff_gen_init(FILE *, efunc);
|
2007-04-12 10:40:54 +08:00
|
|
|
static void coff_sect_write(struct Section *, const uint8_t *,
|
|
|
|
uint32_t);
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_write(void);
|
2007-04-14 00:47:53 +08:00
|
|
|
static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
|
2005-01-16 06:15:51 +08:00
|
|
|
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)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-11 05:58:45 +08:00
|
|
|
win32 = true; win64 = false;
|
2007-04-12 10:40:54 +08:00
|
|
|
(void)ldef; /* placate optimizers */
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)eval;
|
2007-04-12 10:40:54 +08:00
|
|
|
coff_gen_init(fp, errfunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void coff_win64_init(FILE * fp, efunc errfunc,
|
|
|
|
ldfunc ldef, evalfunc eval)
|
|
|
|
{
|
2008-05-24 10:45:39 +08:00
|
|
|
extern struct ofmt of_win64;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
maxbits = 64;
|
2007-10-11 05:58:45 +08:00
|
|
|
win32 = false; win64 = true;
|
2007-04-12 10:40:54 +08:00
|
|
|
(void)ldef; /* placate optimizers */
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)eval;
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_gen_init(fp, errfunc);
|
2008-05-24 10:45:39 +08:00
|
|
|
imagebase_sect = seg_alloc()+1;
|
|
|
|
ldef(WRT_IMAGEBASE,imagebase_sect,0,NULL,false,false,&of_win64,errfunc);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
|
|
|
|
evalfunc eval)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-11 05:58:45 +08:00
|
|
|
win32 = win64 = false;
|
2007-04-12 10:40:54 +08:00
|
|
|
(void)ldef; /* placate optimizers */
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)eval;
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_gen_init(fp, errfunc);
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_gen_init(FILE * fp, efunc errfunc)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
coffp = fp;
|
|
|
|
error = errfunc;
|
2002-05-01 04:51:53 +08:00
|
|
|
sects = NULL;
|
|
|
|
nsects = sectlen = 0;
|
2007-04-12 10:40:54 +08:00
|
|
|
syms = saa_init((int32_t)sizeof(struct Symbol));
|
2002-05-01 04:51:32 +08:00
|
|
|
nsyms = 0;
|
|
|
|
bsym = raa_init();
|
|
|
|
symval = raa_init();
|
|
|
|
strs = saa_init(1L);
|
|
|
|
strslen = 0;
|
2002-05-01 04:51:53 +08:00
|
|
|
def_seg = seg_alloc();
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_cleanup(int debuginfo)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc *r;
|
2002-05-01 04:51:53 +08:00
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
(void)debuginfo;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_write();
|
2005-01-16 06:15:51 +08:00
|
|
|
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]);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(sects);
|
|
|
|
saa_free(syms);
|
|
|
|
raa_free(bsym);
|
|
|
|
raa_free(symval);
|
|
|
|
saa_free(strs);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static int coff_make_section(char *name, uint32_t flags)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:53 +08:00
|
|
|
struct Section *s;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
s = nasm_malloc(sizeof(*s));
|
2002-05-01 04:51:53 +08:00
|
|
|
|
|
|
|
if (flags != BSS_FLAGS)
|
2005-01-16 06:15:51 +08:00
|
|
|
s->data = saa_init(1L);
|
2002-05-01 04:51:53 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
s->data = NULL;
|
2002-05-01 04:51:53 +08:00
|
|
|
s->head = NULL;
|
|
|
|
s->tail = &s->head;
|
|
|
|
s->len = 0;
|
|
|
|
s->nrelocs = 0;
|
|
|
|
if (!strcmp(name, ".text"))
|
2005-01-16 06:15:51 +08:00
|
|
|
s->index = def_seg;
|
2002-05-01 04:51:53 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
s->index = seg_alloc();
|
|
|
|
strncpy(s->name, name, 8);
|
2002-05-01 04:51:53 +08:00
|
|
|
s->name[8] = '\0';
|
|
|
|
s->flags = flags;
|
|
|
|
|
|
|
|
if (nsects >= sectlen)
|
2005-01-16 06:15:51 +08:00
|
|
|
sects =
|
|
|
|
nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
|
2002-05-01 04:51:53 +08:00
|
|
|
sects[nsects++] = s;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
return nsects - 1;
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static int32_t coff_section_names(char *name, int pass, int *bits)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t flags, align_and = ~0L, align_or = 0L;
|
2002-05-01 04:51:53 +08:00
|
|
|
int i;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
2007-04-12 10:40:54 +08:00
|
|
|
* Set default bits.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if (!name) {
|
|
|
|
if(win64)
|
|
|
|
*bits = 64;
|
|
|
|
else
|
|
|
|
*bits = 32;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (!name)
|
2005-01-16 06:15:51 +08:00
|
|
|
return def_seg;
|
2002-05-01 04:51:53 +08:00
|
|
|
|
|
|
|
p = name;
|
2008-06-22 01:23:17 +08:00
|
|
|
while (*p && !nasm_isspace(*p))
|
2005-01-16 06:15:51 +08:00
|
|
|
p++;
|
|
|
|
if (*p)
|
|
|
|
*p++ = '\0';
|
2002-05-01 04:52:49 +08:00
|
|
|
if (strlen(name) > 8) {
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_WARNING, "COFF section names limited to 8 characters:"
|
|
|
|
" truncating");
|
|
|
|
name[8] = '\0';
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
|
|
|
flags = 0;
|
|
|
|
|
2008-06-22 01:23:17 +08:00
|
|
|
while (*p && nasm_isspace(*p))
|
2005-01-16 06:15:51 +08:00
|
|
|
p++;
|
2002-05-01 04:51:53 +08:00
|
|
|
while (*p) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char *q = p;
|
2008-06-22 01:23:17 +08:00
|
|
|
while (*p && !nasm_isspace(*p))
|
2005-01-16 06:15:51 +08:00
|
|
|
p++;
|
|
|
|
if (*p)
|
|
|
|
*p++ = '\0';
|
2008-06-22 01:23:17 +08:00
|
|
|
while (*p && nasm_isspace(*p))
|
2005-01-16 06:15:51 +08:00
|
|
|
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")) {
|
2007-04-12 10:40:54 +08:00
|
|
|
if (win32 | win64)
|
2005-01-16 06:15:51 +08:00
|
|
|
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")) {
|
2007-04-12 10:40:54 +08:00
|
|
|
if (win32 | win64)
|
2005-01-16 06:15:51 +08:00
|
|
|
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)) {
|
2007-04-12 10:40:54 +08:00
|
|
|
if (!(win32 | win64))
|
2005-01-16 06:15:51 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (!strcmp(name, sects[i]->name))
|
|
|
|
break;
|
2002-05-01 04:51:53 +08:00
|
|
|
if (i == nsects) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!flags) {
|
|
|
|
if (!strcmp(name, ".data"))
|
|
|
|
flags = DATA_FLAGS;
|
|
|
|
else if (!strcmp(name, ".rdata"))
|
|
|
|
flags = RDATA_FLAGS;
|
|
|
|
else if (!strcmp(name, ".bss"))
|
|
|
|
flags = BSS_FLAGS;
|
2008-05-24 10:45:39 +08:00
|
|
|
else if (win64 && !strcmp(name, ".pdata"))
|
|
|
|
flags = 0x40300040; /* rdata align=4 */
|
|
|
|
else if (win64 && !strcmp(name, ".xdata"))
|
|
|
|
flags = 0x40400040; /* rdate align=8 */
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
|
|
|
flags = TEXT_FLAGS;
|
|
|
|
}
|
|
|
|
i = coff_make_section(name, flags);
|
|
|
|
if (flags)
|
|
|
|
sects[i]->flags = flags;
|
|
|
|
sects[i]->flags &= align_and;
|
|
|
|
sects[i]->flags |= align_or;
|
2002-05-01 04:51:53 +08:00
|
|
|
} else if (pass == 1) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (flags)
|
|
|
|
error(ERR_WARNING, "section attributes ignored on"
|
|
|
|
" redeclaration of section `%s'", name);
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sects[i]->index;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-11-06 09:19:32 +08:00
|
|
|
static void coff_deflabel(char *name, int32_t segment, int64_t offset,
|
2007-04-14 00:47:53 +08:00
|
|
|
int is_global, char *special)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
int pos = strslen + 4;
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Symbol *sym;
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
if (special)
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "binary format does not support any"
|
|
|
|
" special symbol types");
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:52:26 +08:00
|
|
|
if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
|
2008-05-24 10:45:39 +08:00
|
|
|
if (strcmp(name,WRT_IMAGEBASE))
|
|
|
|
error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
|
2005-01-16 06:15:51 +08:00
|
|
|
return;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(name) > 8) {
|
2007-04-12 10:40:54 +08:00
|
|
|
saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
|
2005-01-16 06:15:51 +08:00
|
|
|
strslen += 1 + strlen(name);
|
2002-05-01 04:51:32 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
pos = -1;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
sym = saa_wstruct(syms);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
sym->strpos = pos;
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
sym->namlen = strlen(name);
|
2002-05-01 04:51:32 +08:00
|
|
|
if (pos == -1)
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(sym->name, name);
|
2002-05-01 04:51:32 +08:00
|
|
|
sym->is_global = !!is_global;
|
2008-09-03 03:55:35 +08:00
|
|
|
sym->type = 0; /* Default to T_NULL (no type) */
|
2002-05-01 04:51:32 +08:00
|
|
|
if (segment == NO_SEG)
|
2005-01-16 06:15:51 +08:00
|
|
|
sym->section = -1; /* absolute symbol */
|
2002-05-01 04:51:32 +08:00
|
|
|
else {
|
2005-01-16 06:15:51 +08:00
|
|
|
int i;
|
|
|
|
sym->section = 0;
|
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (segment == sects[i]->index) {
|
|
|
|
sym->section = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!sym->section)
|
2007-10-11 05:58:45 +08:00
|
|
|
sym->is_global = true;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
if (is_global == 2)
|
2005-01-16 06:15:51 +08:00
|
|
|
sym->value = offset;
|
2002-05-01 04:51:32 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
sym->value = (sym->section == 0 ? 0 : offset);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* define the references from external-symbol segment numbers
|
|
|
|
* to these symbol records.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if (sym->section == 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
bsym = raa_write(bsym, segment, nsyms);
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (segment != NO_SEG)
|
2005-01-16 06:15:51 +08:00
|
|
|
symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
nsyms++;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
|
2008-05-24 10:45:39 +08:00
|
|
|
int16_t type)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc *r;
|
|
|
|
|
|
|
|
r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
|
|
|
|
sect->tail = &r->next;
|
|
|
|
r->next = NULL;
|
|
|
|
|
|
|
|
r->address = sect->len;
|
2002-05-01 04:51:53 +08:00
|
|
|
if (segment == NO_SEG)
|
2005-01-16 06:15:51 +08:00
|
|
|
r->symbol = 0, r->symbase = ABS_SYMBOL;
|
2002-05-01 04:51:53 +08:00
|
|
|
else {
|
2005-01-16 06:15:51 +08:00
|
|
|
int i;
|
|
|
|
r->symbase = REAL_SYMBOLS;
|
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (segment == sects[i]->index) {
|
|
|
|
r->symbol = i * 2;
|
|
|
|
r->symbase = SECT_SYMBOLS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r->symbase == REAL_SYMBOLS)
|
|
|
|
r->symbol = raa_read(bsym, segment);
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2008-05-24 10:45:39 +08:00
|
|
|
r->type = type;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
sect->nrelocs++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the fixup for standard COFF common variables.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
|
2005-01-16 06:15:51 +08:00
|
|
|
return raa_read(symval, segment);
|
2002-05-01 04:51:32 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
static void coff_out(int32_t segto, const void *data,
|
|
|
|
enum out_type type, uint64_t size,
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t segment, int32_t wrt)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Section *s;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t mydata[8], *p;
|
2002-05-01 04:51:53 +08:00
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-05-24 10:45:39 +08:00
|
|
|
if (wrt != NO_SEG && !win64) {
|
2005-01-16 06:15:51 +08:00
|
|
|
wrt = NO_SEG; /* continue to do _something_ */
|
|
|
|
error(ERR_NONFATAL, "WRT not supported by COFF output formats");
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle absolute-assembly (structure definitions)
|
|
|
|
*/
|
|
|
|
if (segto == NO_SEG) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (type != OUT_RESERVE)
|
|
|
|
error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
|
|
|
|
" space");
|
|
|
|
return;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
s = NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (segto == sects[i]->index) {
|
|
|
|
s = sects[i];
|
|
|
|
break;
|
|
|
|
}
|
2002-05-01 04:51:53 +08:00
|
|
|
if (!s) {
|
2005-01-16 06:15:51 +08:00
|
|
|
int tempint; /* ignored */
|
|
|
|
if (segto != coff_section_names(".text", 2, &tempint))
|
|
|
|
error(ERR_PANIC, "strange segment conditions in COFF driver");
|
|
|
|
else
|
|
|
|
s = sects[nsects - 1];
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2008-05-24 10:45:39 +08:00
|
|
|
/* magically default to 'wrt ..imagebase' in .pdata and .xdata */
|
|
|
|
if (win64 && wrt == NO_SEG &&
|
|
|
|
(!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
|
|
|
|
wrt = imagebase_sect;
|
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
if (!s->data && type != OUT_RESERVE) {
|
2007-04-12 10:40:54 +08:00
|
|
|
error(ERR_WARNING, "attempt to initialize memory in"
|
2005-01-16 06:15:51 +08:00
|
|
|
" BSS section `%s': ignored", s->name);
|
|
|
|
if (type == OUT_REL2ADR)
|
2007-11-10 06:44:02 +08:00
|
|
|
size = 2;
|
2005-01-16 06:15:51 +08:00
|
|
|
else if (type == OUT_REL4ADR)
|
2007-11-10 06:44:02 +08:00
|
|
|
size = 4;
|
|
|
|
s->len += size;
|
2005-01-16 06:15:51 +08:00
|
|
|
return;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == OUT_RESERVE) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (s->data) {
|
|
|
|
error(ERR_WARNING, "uninitialised space declared in"
|
|
|
|
" non-BSS section `%s': zeroing", s->name);
|
2007-11-10 06:44:02 +08:00
|
|
|
coff_sect_write(s, NULL, size);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else
|
2007-11-10 06:44:02 +08:00
|
|
|
s->len += size;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_RAWDATA) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segment != NO_SEG)
|
|
|
|
error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
|
2007-11-10 06:44:02 +08:00
|
|
|
coff_sect_write(s, data, size);
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_ADDRESS) {
|
2007-04-12 10:40:54 +08:00
|
|
|
if (!(win64)) {
|
2007-11-10 06:44:02 +08:00
|
|
|
if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
|
2007-04-12 10:40:54 +08:00
|
|
|
error(ERR_NONFATAL, "COFF format does not support non-32-bit"
|
|
|
|
" relocations");
|
|
|
|
else {
|
|
|
|
int32_t fix = 0;
|
|
|
|
if (segment != NO_SEG || wrt != NO_SEG) {
|
|
|
|
if (wrt != NO_SEG) {
|
|
|
|
error(ERR_NONFATAL, "COFF format does not support"
|
|
|
|
" WRT types");
|
|
|
|
} else if (segment % 2) {
|
|
|
|
error(ERR_NONFATAL, "COFF format does not support"
|
|
|
|
" segment base references");
|
|
|
|
} else
|
2008-05-24 10:45:39 +08:00
|
|
|
fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
|
|
|
p = mydata;
|
2007-11-14 01:37:59 +08:00
|
|
|
WRITELONG(p, *(int64_t *)data + fix);
|
2007-11-10 06:44:02 +08:00
|
|
|
coff_sect_write(s, mydata, size);
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int32_t fix = 0;
|
|
|
|
p = mydata;
|
2007-11-10 06:44:02 +08:00
|
|
|
if (size == 8) {
|
2008-05-28 05:01:25 +08:00
|
|
|
if (wrt == imagebase_sect) {
|
|
|
|
error(ERR_NONFATAL, "operand size mismatch: 'wrt "
|
|
|
|
WRT_IMAGEBASE "' is a 32-bit operand");
|
|
|
|
}
|
2008-05-24 10:45:39 +08:00
|
|
|
fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
|
2007-04-12 10:40:54 +08:00
|
|
|
WRITEDLONG(p, *(int64_t *)data + fix);
|
2007-11-10 06:44:02 +08:00
|
|
|
coff_sect_write(s, mydata, size);
|
2007-04-12 10:40:54 +08:00
|
|
|
} else {
|
2008-05-24 10:45:39 +08:00
|
|
|
fix = coff_add_reloc(s, segment,
|
|
|
|
wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
|
|
|
|
IMAGE_REL_AMD64_ADDR32);
|
2007-11-14 01:37:59 +08:00
|
|
|
WRITELONG(p, *(int64_t *)data + fix);
|
2007-11-10 06:44:02 +08:00
|
|
|
coff_sect_write(s, mydata, size);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_REL2ADR) {
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "COFF format does not support 16-bit"
|
|
|
|
" relocations");
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_REL4ADR) {
|
2007-04-12 10:40:54 +08:00
|
|
|
if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_PANIC, "intra-segment OUT_REL4ADR");
|
|
|
|
else if (segment == NO_SEG && win32)
|
|
|
|
error(ERR_NONFATAL, "Win32 COFF does not correctly support"
|
|
|
|
" relative references to absolute addresses");
|
|
|
|
else {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t fix = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segment != NO_SEG && segment % 2) {
|
|
|
|
error(ERR_NONFATAL, "COFF format does not support"
|
|
|
|
" segment base references");
|
|
|
|
} else
|
2008-05-24 10:45:39 +08:00
|
|
|
fix = coff_add_reloc(s, segment,
|
|
|
|
win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
|
2005-01-16 06:15:51 +08:00
|
|
|
p = mydata;
|
2007-04-12 10:40:54 +08:00
|
|
|
if (win32 | win64) {
|
2007-11-14 01:37:59 +08:00
|
|
|
WRITELONG(p, *(int64_t *)data + 4 - size + fix);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
2007-11-14 01:37:59 +08:00
|
|
|
WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
coff_sect_write(s, mydata, 4L);
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_sect_write(struct Section *sect,
|
2007-04-12 10:40:54 +08:00
|
|
|
const uint8_t *data, uint32_t len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_wbytes(sect->data, data, len);
|
2002-05-01 04:51:32 +08:00
|
|
|
sect->len += len;
|
|
|
|
}
|
|
|
|
|
2002-06-08 04:42:22 +08:00
|
|
|
typedef struct tagString {
|
2005-01-16 06:15:51 +08:00
|
|
|
struct tagString *Next;
|
|
|
|
int len;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *String;
|
2002-06-08 04:42:22 +08:00
|
|
|
} STRING;
|
|
|
|
|
|
|
|
#define EXPORT_SECTION_NAME ".drectve"
|
2005-01-16 06:15:51 +08:00
|
|
|
#define EXPORT_SECTION_FLAGS INFO_FLAGS
|
2007-10-20 05:42:29 +08:00
|
|
|
/*
|
2002-06-08 04:42:22 +08:00
|
|
|
#define EXPORT_SECTION_NAME ".text"
|
|
|
|
#define EXPORT_SECTION_FLAGS TEXT_FLAGS
|
|
|
|
*/
|
|
|
|
|
|
|
|
static STRING *Exports = NULL;
|
|
|
|
static struct Section *directive_sec;
|
2007-04-14 00:47:53 +08:00
|
|
|
void AddExport(char *name)
|
2002-06-08 04:42:22 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
STRING *rvp = Exports, *newS;
|
|
|
|
|
|
|
|
newS = (STRING *) nasm_malloc(sizeof(STRING));
|
|
|
|
newS->len = strlen(name);
|
|
|
|
newS->Next = NULL;
|
2007-04-14 00:47:53 +08:00
|
|
|
newS->String = (char *)nasm_malloc(newS->len + 1);
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(newS->String, name);
|
|
|
|
if (rvp == NULL) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
|
|
|
|
if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
|
|
|
|
break;
|
|
|
|
if (i == nsects)
|
|
|
|
directive_sec =
|
|
|
|
sects[coff_make_section
|
|
|
|
(EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
|
|
|
|
else
|
|
|
|
directive_sec = sects[i];
|
|
|
|
Exports = newS;
|
|
|
|
} else {
|
|
|
|
while (rvp->Next) {
|
|
|
|
if (!strcmp(rvp->String, name))
|
|
|
|
return;
|
|
|
|
rvp = rvp->Next;
|
|
|
|
}
|
|
|
|
rvp->Next = newS;
|
|
|
|
}
|
2002-06-08 04:42:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BuildExportTable(void)
|
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
STRING *rvp = Exports, *next;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t buf[256];
|
2005-01-16 06:15:51 +08:00
|
|
|
int len;
|
|
|
|
if (rvp == NULL)
|
|
|
|
return;
|
|
|
|
while (rvp) {
|
2007-04-14 00:47:53 +08:00
|
|
|
len = sprintf((char *)buf, "-export:%s ", rvp->String);
|
2005-01-16 06:15:51 +08:00
|
|
|
coff_sect_write(directive_sec, buf, len);
|
|
|
|
rvp = rvp->Next;
|
|
|
|
}
|
2002-06-08 04:42:22 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
next = Exports;
|
|
|
|
while ((rvp = next)) {
|
|
|
|
next = rvp->Next;
|
|
|
|
nasm_free(rvp->String);
|
|
|
|
nasm_free(rvp);
|
|
|
|
}
|
|
|
|
Exports = NULL;
|
|
|
|
}
|
2002-06-08 04:42:22 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static int coff_directives(char *directive, char *value, int pass)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-06-08 04:42:22 +08:00
|
|
|
if (!strcmp(directive, "export")) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char *q, *name;
|
2002-06-08 04:42:22 +08:00
|
|
|
|
|
|
|
if (pass == 2)
|
2005-01-16 06:15:51 +08:00
|
|
|
return 1; /* ignore in pass two */
|
2002-06-08 04:42:22 +08:00
|
|
|
name = q = value;
|
2008-06-22 01:23:17 +08:00
|
|
|
while (*q && !nasm_isspace(*q))
|
2002-06-08 04:42:22 +08:00
|
|
|
q++;
|
2008-06-22 01:23:17 +08:00
|
|
|
if (nasm_isspace(*q)) {
|
2002-06-08 04:42:22 +08:00
|
|
|
*q++ = '\0';
|
2008-06-22 01:23:17 +08:00
|
|
|
while (*q && nasm_isspace(*q))
|
2002-06-08 04:42:22 +08:00
|
|
|
q++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*name) {
|
|
|
|
error(ERR_NONFATAL, "`export' directive requires export name");
|
|
|
|
return 1;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*q) {
|
2007-04-12 10:40:54 +08:00
|
|
|
error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
|
2002-06-08 04:42:22 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
AddExport(name);
|
2002-06-08 04:42:22 +08:00
|
|
|
return 1;
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
} else if (win32 && !strcmp(directive,"safeseh")) {
|
|
|
|
static int sxseg=-1;
|
|
|
|
int i;
|
|
|
|
if (sxseg==-1)
|
|
|
|
{ for (i = 0; i < nsects; i++)
|
|
|
|
if (!strcmp(".sxdata",sects[i]->name))
|
|
|
|
break;
|
|
|
|
if (i == nsects)
|
|
|
|
sxseg = coff_make_section(".sxdata",0x200);
|
|
|
|
else
|
|
|
|
sxseg = i;
|
|
|
|
}
|
|
|
|
if (pass==2) {
|
|
|
|
uint32_t n;
|
|
|
|
saa_rewind(syms);
|
|
|
|
for (n = 0; n < nsyms; n++) {
|
|
|
|
struct Symbol *sym = saa_rstruct(syms);
|
|
|
|
bool equals;
|
|
|
|
|
|
|
|
/* sym->strpos is biased by 4, because symbol
|
|
|
|
* table is prefixed with table length */
|
|
|
|
if (sym->strpos >=4) {
|
|
|
|
char *name = nasm_malloc(sym->namlen+1);
|
|
|
|
saa_fread(strs,sym->strpos-4,name,sym->namlen);
|
|
|
|
name[sym->namlen]='\0';
|
|
|
|
equals = !strcmp(value,name);
|
|
|
|
nasm_free(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
equals = !strcmp(value,sym->name);
|
|
|
|
|
|
|
|
if (equals) {
|
|
|
|
/* this value arithmetics effectively reflects
|
|
|
|
* initsym in coff_write(): 2 for file, 1 for
|
|
|
|
* .absolute and two per each section */
|
|
|
|
unsigned char value[4],*p=value;
|
|
|
|
WRITELONG(p,n + 2 + 1 + nsects*2);
|
|
|
|
coff_sect_write(sects[sxseg],value,4);
|
|
|
|
sym->type = 0x20;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n == nsyms) {
|
2008-05-24 09:38:51 +08:00
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"`safeseh' directive requires valid symbol");
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2002-06-08 04:42:22 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_write(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t pos, sympos, vsize;
|
2002-05-01 04:51:53 +08:00
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
BuildExportTable(); /* fill in the .drectve section with -export's */
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
|
|
|
|
if (win32) {
|
|
|
|
/* add default value for @feat.00, this allows to 'link /safeseh' */
|
|
|
|
uint32_t n;
|
|
|
|
|
|
|
|
saa_rewind(syms);
|
|
|
|
for (n = 0; n < nsyms; n++) {
|
|
|
|
struct Symbol *sym = saa_rstruct(syms);
|
|
|
|
if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n == nsyms)
|
|
|
|
coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
2002-05-01 04:51:53 +08:00
|
|
|
* Work out how big the file will get. Calculate the start of
|
|
|
|
* the `real' symbols at the same time.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2002-05-01 04:52:08 +08:00
|
|
|
pos = 0x14 + 0x28 * nsects;
|
2005-01-16 06:15:51 +08:00
|
|
|
initsym = 3; /* two for the file, one absolute */
|
|
|
|
for (i = 0; i < nsects; i++) {
|
|
|
|
if (sects[i]->data) {
|
|
|
|
sects[i]->pos = pos;
|
|
|
|
pos += sects[i]->len;
|
|
|
|
sects[i]->relpos = pos;
|
|
|
|
pos += 10 * sects[i]->nrelocs;
|
|
|
|
} else
|
|
|
|
sects[i]->pos = sects[i]->relpos = 0L;
|
|
|
|
initsym += 2; /* two for each section */
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
|
|
|
sympos = pos;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the COFF header.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if (win64)
|
|
|
|
fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
|
|
|
|
else
|
|
|
|
fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
|
|
|
|
fwriteint16_t(nsects, coffp); /* number of sections */
|
|
|
|
fwriteint32_t(time(NULL), coffp); /* time stamp */
|
|
|
|
fwriteint32_t(sympos, coffp);
|
|
|
|
fwriteint32_t(nsyms + initsym, coffp);
|
|
|
|
fwriteint16_t(0, coffp); /* no optional header */
|
2002-05-01 04:51:32 +08:00
|
|
|
/* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the section headers.
|
|
|
|
*/
|
2002-05-01 04:51:53 +08:00
|
|
|
vsize = 0L;
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < nsects; i++) {
|
|
|
|
coff_section_header(sects[i]->name, vsize, sects[i]->len,
|
|
|
|
sects[i]->pos, sects[i]->relpos,
|
|
|
|
sects[i]->nrelocs, sects[i]->flags);
|
|
|
|
vsize += sects[i]->len;
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
2002-05-01 04:51:53 +08:00
|
|
|
* Output the sections and their relocations.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (sects[i]->data) {
|
|
|
|
saa_fpwrite(sects[i]->data, coffp);
|
|
|
|
coff_write_relocs(sects[i]);
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the symbol and string tables.
|
|
|
|
*/
|
|
|
|
coff_write_symbols();
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(strslen + 4, coffp); /* length includes length count */
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_fpwrite(strs, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void coff_section_header(char *name, int32_t vsize,
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t datalen, int32_t datapos,
|
|
|
|
int32_t relpos, int nrelocs, int32_t flags)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char padname[8];
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-25 08:02:07 +08:00
|
|
|
(void)vsize;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
memset(padname, 0, 8);
|
|
|
|
strncpy(padname, name, 8);
|
|
|
|
fwrite(padname, 8, 1, coffp);
|
2007-09-25 08:02:07 +08:00
|
|
|
fwriteint32_t(0, coffp); /* Virtual size field - set to 0 or vsize */
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
|
|
|
|
fwriteint32_t(datalen, coffp);
|
|
|
|
fwriteint32_t(datapos, coffp);
|
|
|
|
fwriteint32_t(relpos, coffp);
|
|
|
|
fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
|
|
|
|
fwriteint16_t(nrelocs, coffp);
|
|
|
|
fwriteint16_t(0, coffp); /* again, no line numbers */
|
|
|
|
fwriteint32_t(flags, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_write_relocs(struct Section *s)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc *r;
|
|
|
|
|
|
|
|
for (r = s->head; r; r = r->next) {
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(r->address, coffp);
|
|
|
|
fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
|
2005-01-16 06:15:51 +08:00
|
|
|
r->symbase == ABS_SYMBOL ? initsym - 1 :
|
|
|
|
r->symbase == SECT_SYMBOLS ? 2 : 0),
|
|
|
|
coffp);
|
2008-05-24 10:45:39 +08:00
|
|
|
fwriteint16_t(r->type, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void coff_symbol(char *name, int32_t strpos, int32_t value,
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
int section, int type, int storageclass, int aux)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char padname[8];
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (name) {
|
2005-01-16 06:15:51 +08:00
|
|
|
memset(padname, 0, 8);
|
|
|
|
strncpy(padname, name, 8);
|
|
|
|
fwrite(padname, 8, 1, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(0L, coffp);
|
|
|
|
fwriteint32_t(strpos, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(value, coffp);
|
|
|
|
fwriteint16_t(section, coffp);
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
fwriteint16_t(type, coffp);
|
|
|
|
fputc(storageclass, coffp);
|
2005-01-16 06:15:51 +08:00
|
|
|
fputc(aux, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void coff_write_symbols(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char filename[18];
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The `.file' record, and the file name auxiliary record.
|
|
|
|
*/
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
|
2005-01-16 06:15:51 +08:00
|
|
|
memset(filename, 0, 18);
|
|
|
|
strncpy(filename, coff_infile, 18);
|
|
|
|
fwrite(filename, 18, 1, coffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The section records, with their auxiliaries.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
memset(filename, 0, 18); /* useful zeroed buffer */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-11-06 09:19:32 +08:00
|
|
|
for (i = 0; i < (uint32_t) nsects; i++) {
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(sects[i]->len, coffp);
|
|
|
|
fwriteint16_t(sects[i]->nrelocs, coffp);
|
2005-01-16 06:15:51 +08:00
|
|
|
fwrite(filename, 12, 1, coffp);
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The absolute symbol, for relative-to-absolute relocations.
|
|
|
|
*/
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The real symbols.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_rewind(syms);
|
2002-05-01 05:01:08 +08:00
|
|
|
for (i = 0; i < nsyms; i++) {
|
2005-01-16 06:15:51 +08:00
|
|
|
struct Symbol *sym = saa_rstruct(syms);
|
|
|
|
coff_symbol(sym->strpos == -1 ? sym->name : NULL,
|
|
|
|
sym->strpos, sym->value, sym->section,
|
Support for Win32 safeseh (BR 195417)
This is basically not a bug report, but a feature request.
It's desired to be able to link .obj modules compiled with 'nasm -f
win32' with Microsoft 'link /safeseh'. As well as to register symbols
(commonly subroutine's entry points or even external symbols) as "safe
handlers." In order to achieve this, several points are required.
First of all, object module has to have absolute symbol named @feat.00
with value of 1. This can actually be achived by adding 'absolute 1'
and '@feat.00:' to source code, but it's desirable that it's
autogenerated for win32 modules.
Handler registration is essentially symbol's *index* in current
module's symbol table in .sxdata, segment with 0x200 segment flags, an
"info" segment. It's also essential that symbol has type 0x20 (see
below). All this is depicted in following framgents of 'objdump -xD'
output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .sxdata 00000004 00000000 00000000 0000003c 2**2
CONTENTS, READONLY, DEBUGGING
SYMBOL TABLE:
...
[ 5](sec -1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000001 @feat.00
[ 6](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _handler
...
<.sxdata>
0: 06 00 00 00
Note [6] and (ty 20) in _handle line in SYMBOL TABLE. "06 00 00 00" in
.sxdata is little-endian 6, _handler's index. This is what makes up
"registration." It's impossible to achieve this with current nasm
facilities and it's probably appropriate to introduce a directive for
it, 'safeseh _handler' is probably most natural choice.
2008-05-24 09:37:52 +08:00
|
|
|
sym->type, sym->is_global ? 2 : 3, 0);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t coff_segbase(int32_t segment)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void coff_std_filename(char *inname, char *outname, efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
strcpy(coff_infile, inname);
|
2005-01-16 06:15:51 +08:00
|
|
|
standard_extension(inname, outname, ".o", error);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void coff_win32_filename(char *inname, char *outname, efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
strcpy(coff_infile, inname);
|
2005-01-16 06:15:51 +08:00
|
|
|
standard_extension(inname, outname, ".obj", error);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2008-06-21 06:20:16 +08:00
|
|
|
extern macros_t coff_stdmac[];
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static int coff_set_info(enum geninfo type, char **val)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)type;
|
|
|
|
(void)val;
|
2002-05-01 04:53:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
#endif /* defined(OF_COFF) || defined(OF_WIN32) */
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#ifdef OF_COFF
|
|
|
|
|
|
|
|
struct ofmt of_coff = {
|
|
|
|
"COFF (i386) object files (e.g. DJGPP for DOS)",
|
|
|
|
"coff",
|
2002-05-01 04:53:55 +08:00
|
|
|
NULL,
|
|
|
|
null_debug_arr,
|
|
|
|
&null_debug_form,
|
2002-05-01 04:52:49 +08:00
|
|
|
coff_stdmac,
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_std_init,
|
2002-05-01 04:53:55 +08:00
|
|
|
coff_set_info,
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_out,
|
|
|
|
coff_deflabel,
|
|
|
|
coff_section_names,
|
|
|
|
coff_segbase,
|
|
|
|
coff_directives,
|
|
|
|
coff_std_filename,
|
|
|
|
coff_cleanup
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OF_WIN32
|
|
|
|
|
|
|
|
struct ofmt of_win32 = {
|
|
|
|
"Microsoft Win32 (i386) object files",
|
|
|
|
"win32",
|
2002-05-01 04:53:55 +08:00
|
|
|
NULL,
|
|
|
|
null_debug_arr,
|
|
|
|
&null_debug_form,
|
2002-05-01 04:52:49 +08:00
|
|
|
coff_stdmac,
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_win32_init,
|
2002-05-01 04:53:55 +08:00
|
|
|
coff_set_info,
|
2002-05-01 04:51:32 +08:00
|
|
|
coff_out,
|
|
|
|
coff_deflabel,
|
|
|
|
coff_section_names,
|
|
|
|
coff_segbase,
|
|
|
|
coff_directives,
|
|
|
|
coff_win32_filename,
|
|
|
|
coff_cleanup
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
#ifdef OF_WIN64
|
|
|
|
|
|
|
|
struct ofmt of_win64 = {
|
|
|
|
"Microsoft Win64 (x86-64) object files",
|
|
|
|
"win64",
|
|
|
|
NULL,
|
|
|
|
null_debug_arr,
|
|
|
|
&null_debug_form,
|
|
|
|
coff_stdmac,
|
|
|
|
coff_win64_init,
|
|
|
|
coff_set_info,
|
|
|
|
coff_out,
|
|
|
|
coff_deflabel,
|
|
|
|
coff_section_names,
|
|
|
|
coff_segbase,
|
|
|
|
coff_directives,
|
|
|
|
coff_win32_filename,
|
|
|
|
coff_cleanup
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|