2002-05-01 04:51:32 +08:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
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>
|
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"
|
2007-08-31 06:35:34 +08:00
|
|
|
#include "stdscan.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "outform.h"
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
#ifdef OF_ELF32
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Relocation types.
|
|
|
|
*/
|
2002-05-01 05:09:12 +08:00
|
|
|
enum reloc_type {
|
2005-01-16 06:15:51 +08:00
|
|
|
R_386_32 = 1, /* ordinary absolute relocation */
|
|
|
|
R_386_PC32 = 2, /* PC-relative relocation */
|
|
|
|
R_386_GOT32 = 3, /* an offset into GOT */
|
|
|
|
R_386_PLT32 = 4, /* a PC-relative offset into PLT */
|
|
|
|
R_386_COPY = 5, /* ??? */
|
|
|
|
R_386_GLOB_DAT = 6, /* ??? */
|
|
|
|
R_386_JUMP_SLOT = 7, /* ??? */
|
|
|
|
R_386_RELATIVE = 8, /* ??? */
|
|
|
|
R_386_GOTOFF = 9, /* an offset from GOT base */
|
|
|
|
R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
|
|
|
|
/* These are GNU extensions, but useful */
|
|
|
|
R_386_16 = 20, /* A 16-bit absolute relocation */
|
|
|
|
R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
|
|
|
|
R_386_8 = 22, /* An 8-bit absolute relocation */
|
|
|
|
R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
|
2002-05-01 05:09:12 +08:00
|
|
|
};
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc {
|
|
|
|
struct Reloc *next;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t address; /* relative to _start_ of section */
|
2007-10-22 05:21:43 +08:00
|
|
|
int32_t symbol; /* symbol index */
|
2005-01-16 06:15:51 +08:00
|
|
|
int type; /* type of relocation */
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Symbol {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t strpos; /* string table position of name */
|
|
|
|
int32_t section; /* section ID of the symbol */
|
2005-01-16 06:15:51 +08:00
|
|
|
int type; /* symbol type */
|
2007-08-26 13:10:24 +08:00
|
|
|
int other; /* symbol visibility */
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t value; /* address, or COMMON variable align */
|
|
|
|
int32_t size; /* size of symbol */
|
|
|
|
int32_t globnum; /* symbol table offset if global */
|
2005-01-16 06:15:51 +08:00
|
|
|
struct Symbol *next; /* list of globals in each section */
|
|
|
|
struct Symbol *nextfwd; /* list of unresolved-size symbols */
|
2007-04-14 00:47:53 +08:00
|
|
|
char *name; /* used temporarily if in above list */
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
#define SHT_PROGBITS 1
|
|
|
|
#define SHT_NOBITS 8
|
|
|
|
|
|
|
|
#define SHF_WRITE 1
|
|
|
|
#define SHF_ALLOC 2
|
|
|
|
#define SHF_EXECINSTR 4
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Section {
|
|
|
|
struct SAA *data;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t len, size, nrelocs;
|
|
|
|
int32_t index;
|
2005-01-16 06:15:51 +08:00
|
|
|
int type; /* SHT_PROGBITS or SHT_NOBITS */
|
|
|
|
int align; /* alignment: power of two */
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t flags; /* section flags */
|
2007-04-14 00:47:53 +08:00
|
|
|
char *name;
|
2002-05-01 04:51:53 +08:00
|
|
|
struct SAA *rel;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t rellen;
|
2002-05-01 04:51:32 +08:00
|
|
|
struct Reloc *head, **tail;
|
2005-01-16 06:15:51 +08:00
|
|
|
struct Symbol *gsyms; /* global symbols in section */
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
#define SECT_DELTA 32
|
|
|
|
static struct Section **sects;
|
|
|
|
static int nsects, sectlen;
|
|
|
|
|
|
|
|
#define SHSTR_DELTA 256
|
2007-04-14 00:47:53 +08:00
|
|
|
static char *shstrtab;
|
2002-05-01 04:51:53 +08:00
|
|
|
static int shstrtablen, shstrtabsize;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
static struct SAA *syms;
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t nlocals, nglobs;
|
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
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
static struct RAA *bsym;
|
|
|
|
|
|
|
|
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 FILE *elffp;
|
|
|
|
static efunc error;
|
2002-05-01 04:52:49 +08:00
|
|
|
static evalfunc evaluate;
|
|
|
|
|
|
|
|
static struct Symbol *fwds;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static char elf_module[FILENAME_MAX];
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
extern struct ofmt of_elf32;
|
2007-04-29 08:28:24 +08:00
|
|
|
extern struct ofmt of_elf;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#define SHN_ABS 0xFFF1
|
|
|
|
#define SHN_COMMON 0xFFF2
|
|
|
|
#define SHN_UNDEF 0
|
|
|
|
|
|
|
|
#define SYM_GLOBAL 0x10
|
2007-10-29 06:29:54 +08:00
|
|
|
|
|
|
|
#define STT_NOTYPE 0 /* Symbol type is unspecified */
|
|
|
|
#define STT_OBJECT 1 /* Symbol is a data object */
|
|
|
|
#define STT_FUNC 2 /* Symbol is a code object */
|
|
|
|
#define STT_SECTION 3 /* Symbol associated with a section */
|
|
|
|
#define STT_FILE 4 /* Symbol's name is file name */
|
|
|
|
#define STT_COMMON 5 /* Symbol is a common data object */
|
|
|
|
#define STT_TLS 6 /* Symbol is thread-local data object*/
|
|
|
|
#define STT_NUM 7 /* Number of defined types. */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-08-26 13:10:24 +08:00
|
|
|
#define STV_DEFAULT 0
|
|
|
|
#define STV_INTERNAL 1
|
|
|
|
#define STV_HIDDEN 2
|
|
|
|
#define STV_PROTECTED 3
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
#define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
#define SEG_ALIGN 16 /* alignment of sections in file */
|
2002-05-01 04:51:32 +08:00
|
|
|
#define SEG_ALIGN_1 (SEG_ALIGN-1)
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
#define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
|
2002-05-01 04:51:32 +08:00
|
|
|
static struct ELF_SECTDATA {
|
|
|
|
void *data;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t len;
|
2007-10-11 15:05:31 +08:00
|
|
|
bool is_saa;
|
2002-05-01 04:53:55 +08:00
|
|
|
} *elf_sects;
|
2002-05-01 04:51:32 +08:00
|
|
|
static int elf_nsect;
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t elf_foffs;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
static void elf_write(void);
|
2007-04-12 10:40:54 +08:00
|
|
|
static void elf_sect_write(struct Section *, const uint8_t *,
|
|
|
|
uint32_t);
|
2007-10-11 05:58:45 +08:00
|
|
|
static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
|
2005-01-16 06:15:51 +08:00
|
|
|
int, int);
|
|
|
|
static void elf_write_sections(void);
|
2007-04-12 10:40:54 +08:00
|
|
|
static struct SAA *elf_build_symtab(int32_t *, int32_t *);
|
|
|
|
static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
|
2007-04-14 00:47:53 +08:00
|
|
|
static void add_sectname(char *, char *);
|
2003-07-10 03:11:10 +08:00
|
|
|
|
|
|
|
/* this stuff is needed for the stabs debugging format */
|
2005-01-16 06:15:51 +08:00
|
|
|
#define N_SO 0x64 /* ID for main source file */
|
|
|
|
#define N_SOL 0x84 /* ID for sub-source file */
|
2003-07-10 03:11:10 +08:00
|
|
|
#define N_BINCL 0x82
|
|
|
|
#define N_EINCL 0xA2
|
|
|
|
#define N_SLINE 0x44
|
2005-01-16 06:15:51 +08:00
|
|
|
#define TY_STABSSYMLIN 0x40 /* ouch */
|
2003-07-10 03:11:10 +08:00
|
|
|
|
|
|
|
struct stabentry {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t n_strx;
|
|
|
|
uint8_t n_type;
|
|
|
|
uint8_t n_other;
|
|
|
|
uint16_t n_desc;
|
|
|
|
uint32_t n_value;
|
2003-07-10 03:11:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct erel {
|
2005-01-16 06:15:51 +08:00
|
|
|
int offset, info;
|
2003-07-10 03:11:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct symlininfo {
|
2005-01-16 06:15:51 +08:00
|
|
|
int offset;
|
|
|
|
int section; /* section index */
|
2007-04-14 00:47:53 +08:00
|
|
|
char *name; /* shallow-copied pointer of section name */
|
2003-07-10 03:11:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct linelist {
|
2005-01-16 06:15:51 +08:00
|
|
|
struct symlininfo info;
|
|
|
|
int line;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *filename;
|
2005-01-16 06:15:51 +08:00
|
|
|
struct linelist *next;
|
|
|
|
struct linelist *last;
|
2003-07-10 03:11:10 +08:00
|
|
|
};
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static struct linelist *stabslines = 0;
|
|
|
|
static int stabs_immcall = 0;
|
|
|
|
static int currentline = 0;
|
|
|
|
static int numlinestabs = 0;
|
2007-04-14 00:47:53 +08:00
|
|
|
static char *stabs_filename = 0;
|
2003-07-10 03:11:10 +08:00
|
|
|
static int symtabsection;
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
static int stablen, stabstrlen, stabrellen;
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2003-09-11 07:34:23 +08:00
|
|
|
static struct dfmt df_stabs;
|
2007-10-29 06:29:54 +08:00
|
|
|
static struct Symbol *lastsym;
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_init(struct ofmt *, void *, FILE *, efunc);
|
|
|
|
void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
|
2007-11-06 09:19:32 +08:00
|
|
|
void stabs32_deflabel(char *, int32_t, int64_t, int, char *);
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_directive(const char *, const char *);
|
|
|
|
void stabs32_typevalue(int32_t);
|
|
|
|
void stabs32_output(int, void *);
|
2007-09-17 13:14:50 +08:00
|
|
|
void stabs32_generate(void);
|
|
|
|
void stabs32_cleanup(void);
|
2003-07-10 03:11:10 +08:00
|
|
|
|
|
|
|
/* end of stabs debugging stuff */
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Special section numbers which are used to define ELF special
|
|
|
|
* symbols, which can be used with WRT to provide PIC relocation
|
|
|
|
* types.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t elf_gotpc_sect, elf_gotoff_sect;
|
|
|
|
static int32_t elf_got_sect, elf_plt_sect;
|
|
|
|
static int32_t elf_sym_sect;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
elffp = fp;
|
|
|
|
error = errfunc;
|
2002-05-01 04:52:49 +08:00
|
|
|
evaluate = eval;
|
2005-01-16 06:15:51 +08:00
|
|
|
(void)ldef; /* placate optimisers */
|
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
|
|
|
nlocals = nglobs = 0;
|
|
|
|
bsym = raa_init();
|
|
|
|
strs = saa_init(1L);
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_wbytes(strs, "\0", 1L);
|
2007-04-12 10:40:54 +08:00
|
|
|
saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
|
2005-01-16 06:15:51 +08:00
|
|
|
strslen = 2 + strlen(elf_module);
|
2002-05-01 04:51:53 +08:00
|
|
|
shstrtab = NULL;
|
|
|
|
shstrtablen = shstrtabsize = 0;;
|
2005-01-16 06:15:51 +08:00
|
|
|
add_sectname("", "");
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
fwds = NULL;
|
|
|
|
|
|
|
|
elf_gotpc_sect = seg_alloc();
|
2007-10-11 05:58:45 +08:00
|
|
|
ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
|
2005-01-16 06:15:51 +08:00
|
|
|
error);
|
2002-05-01 04:52:49 +08:00
|
|
|
elf_gotoff_sect = seg_alloc();
|
2007-10-11 05:58:45 +08:00
|
|
|
ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
|
2005-01-16 06:15:51 +08:00
|
|
|
error);
|
2002-05-01 04:52:49 +08:00
|
|
|
elf_got_sect = seg_alloc();
|
2007-10-11 05:58:45 +08:00
|
|
|
ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
|
2005-01-16 06:15:51 +08:00
|
|
|
error);
|
2002-05-01 04:52:49 +08:00
|
|
|
elf_plt_sect = seg_alloc();
|
2007-10-11 05:58:45 +08:00
|
|
|
ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
|
2005-01-16 06:15:51 +08:00
|
|
|
error);
|
2002-05-01 04:52:49 +08:00
|
|
|
elf_sym_sect = seg_alloc();
|
2007-10-11 05:58:45 +08:00
|
|
|
ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
|
2005-01-16 06:15:51 +08:00
|
|
|
error);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
def_seg = seg_alloc();
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2003-07-10 03:11:10 +08:00
|
|
|
static void elf_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
|
|
|
elf_write();
|
2005-01-16 06:15:51 +08:00
|
|
|
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);
|
|
|
|
}
|
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);
|
|
|
|
saa_free(strs);
|
2007-04-28 14:18:48 +08:00
|
|
|
if (of_elf32.current_dfmt) {
|
|
|
|
of_elf32.current_dfmt->cleanup();
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void add_sectname(char *firsthalf, char *secondhalf)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
int len = strlen(firsthalf) + strlen(secondhalf);
|
2002-05-01 04:51:53 +08:00
|
|
|
while (shstrtablen + len + 1 > shstrtabsize)
|
2005-01-16 06:15:51 +08:00
|
|
|
shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
|
|
|
|
strcpy(shstrtab + shstrtablen, firsthalf);
|
|
|
|
strcat(shstrtab + shstrtablen, secondhalf);
|
|
|
|
shstrtablen += len + 1;
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static int elf_make_section(char *name, int type, int flags, int align)
|
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 (type != SHT_NOBITS)
|
2005-01-16 06:15:51 +08:00
|
|
|
s->data = saa_init(1L);
|
2002-05-01 04:51:53 +08:00
|
|
|
s->head = NULL;
|
|
|
|
s->tail = &s->head;
|
|
|
|
s->len = s->size = 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();
|
|
|
|
add_sectname("", name);
|
|
|
|
s->name = nasm_malloc(1 + strlen(name));
|
|
|
|
strcpy(s->name, name);
|
2002-05-01 04:51:53 +08:00
|
|
|
s->type = type;
|
|
|
|
s->flags = flags;
|
|
|
|
s->align = align;
|
2002-05-01 04:52:49 +08:00
|
|
|
s->gsyms = NULL;
|
2002-05-01 04:51:53 +08:00
|
|
|
|
|
|
|
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 elf_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-10-19 08:04:10 +08:00
|
|
|
unsigned flags_and, flags_or;
|
|
|
|
int type, align, i;
|
2002-05-01 04:51:53 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Default is 32 bits.
|
|
|
|
*/
|
2002-05-01 04:53:55 +08:00
|
|
|
if (!name) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*bits = 32;
|
|
|
|
return def_seg;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
2002-05-01 04:51:53 +08:00
|
|
|
|
|
|
|
p = name;
|
2005-01-16 06:15:51 +08:00
|
|
|
while (*p && !isspace(*p))
|
|
|
|
p++;
|
|
|
|
if (*p)
|
|
|
|
*p++ = '\0';
|
2002-05-01 04:51:53 +08:00
|
|
|
flags_and = flags_or = type = align = 0;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
while (*p && isspace(*p))
|
|
|
|
p++;
|
2002-05-01 04:51:53 +08:00
|
|
|
while (*p) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char *q = p;
|
2005-01-16 06:15:51 +08:00
|
|
|
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;
|
|
|
|
} else if (!nasm_stricmp(q, "nowrite")) {
|
|
|
|
flags_and |= SHF_WRITE;
|
|
|
|
flags_or &= ~SHF_WRITE;
|
|
|
|
} else if (!nasm_stricmp(q, "progbits")) {
|
|
|
|
type = SHT_PROGBITS;
|
|
|
|
} else if (!nasm_stricmp(q, "nobits")) {
|
|
|
|
type = SHT_NOBITS;
|
|
|
|
}
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
if (!strcmp(name, ".comment") ||
|
2005-01-16 06:15:51 +08:00
|
|
|
!strcmp(name, ".shstrtab") ||
|
|
|
|
!strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
|
|
|
|
error(ERR_NONFATAL, "attempt to redefine reserved section"
|
|
|
|
"name `%s'", name);
|
|
|
|
return NO_SEG;
|
2002-05-01 04:51:53 +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 (!strcmp(name, ".text"))
|
|
|
|
i = elf_make_section(name, SHT_PROGBITS,
|
|
|
|
SHF_ALLOC | SHF_EXECINSTR, 16);
|
|
|
|
else if (!strcmp(name, ".rodata"))
|
|
|
|
i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
|
|
|
|
else if (!strcmp(name, ".data"))
|
|
|
|
i = elf_make_section(name, SHT_PROGBITS,
|
|
|
|
SHF_ALLOC | SHF_WRITE, 4);
|
|
|
|
else if (!strcmp(name, ".bss"))
|
|
|
|
i = elf_make_section(name, SHT_NOBITS,
|
|
|
|
SHF_ALLOC | SHF_WRITE, 4);
|
|
|
|
else
|
|
|
|
i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
|
|
|
|
if (type)
|
|
|
|
sects[i]->type = type;
|
|
|
|
if (align)
|
|
|
|
sects[i]->align = align;
|
|
|
|
sects[i]->flags &= ~flags_and;
|
|
|
|
sects[i]->flags |= flags_or;
|
2002-05-01 04:51:53 +08:00
|
|
|
} else if (pass == 1) {
|
2007-10-19 08:04:10 +08:00
|
|
|
if ((type && sects[i]->type != type)
|
|
|
|
|| (align && sects[i]->align != align)
|
|
|
|
|| (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
|
2005-01-16 06:15:51 +08:00
|
|
|
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 elf_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
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
int pos = strslen;
|
|
|
|
struct Symbol *sym;
|
2007-10-11 05:58:45 +08:00
|
|
|
bool special_used = false;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 05:00:33 +08:00
|
|
|
#if defined(DEBUG) && DEBUG>2
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
" elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
|
|
|
|
name, segment, offset, is_global, special);
|
2002-05-01 05:00:33 +08:00
|
|
|
#endif
|
2002-05-01 04:52:26 +08:00
|
|
|
if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
if (is_global == 3) {
|
2005-01-16 06:15:51 +08:00
|
|
|
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;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p = special;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
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 */
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-10-29 06:29:54 +08:00
|
|
|
lastsym = sym = saa_wstruct(syms);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
sym->strpos = pos;
|
|
|
|
sym->type = is_global ? SYM_GLOBAL : 0;
|
2007-08-26 13:10:24 +08:00
|
|
|
sym->other = STV_DEFAULT;
|
2002-05-01 04:52:49 +08:00
|
|
|
sym->size = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
if (segment == NO_SEG)
|
2005-01-16 06:15:51 +08:00
|
|
|
sym->section = SHN_ABS;
|
2002-05-01 04:51:53 +08:00
|
|
|
else {
|
2005-01-16 06:15:51 +08:00
|
|
|
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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (is_global == 2) {
|
2005-01-16 06:15:51 +08:00
|
|
|
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) {
|
2007-10-11 15:05:31 +08:00
|
|
|
bool err;
|
2005-01-16 06:15:51 +08:00
|
|
|
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);
|
|
|
|
}
|
2007-10-11 05:58:45 +08:00
|
|
|
special_used = true;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (sym->type == SYM_GLOBAL) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* If sym->section == SHN_ABS, then the first line of the
|
2007-04-30 04:57:53 +08:00
|
|
|
* else section would cause a core dump, because its a reference
|
2005-01-16 06:15:51 +08:00
|
|
|
* beyond the end of the section array.
|
|
|
|
* This behaviour is exhibited by this code:
|
|
|
|
* GLOBAL crash_nasm
|
|
|
|
* crash_nasm equ 0
|
2007-04-30 04:57:53 +08:00
|
|
|
* To avoid such a crash, such requests are silently discarded.
|
|
|
|
* This may not be the best solution.
|
2005-01-16 06:15:51 +08:00
|
|
|
*/
|
|
|
|
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) {
|
2007-08-26 13:10:24 +08:00
|
|
|
int n = strcspn(special, " \t");
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
if (!nasm_strnicmp(special, "function", n))
|
2007-10-29 06:29:54 +08:00
|
|
|
sym->type |= STT_FUNC;
|
2005-01-16 06:15:51 +08:00
|
|
|
else if (!nasm_strnicmp(special, "data", n) ||
|
|
|
|
!nasm_strnicmp(special, "object", n))
|
2007-10-29 06:29:54 +08:00
|
|
|
sym->type |= STT_OBJECT;
|
2007-08-26 13:10:24 +08:00
|
|
|
else if (!nasm_strnicmp(special, "notype", n))
|
2007-10-29 06:29:54 +08:00
|
|
|
sym->type |= STT_NOTYPE;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
|
|
|
error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
|
|
|
|
n, special);
|
2007-08-26 13:10:24 +08:00
|
|
|
special += n;
|
|
|
|
|
|
|
|
while (isspace(*special))
|
|
|
|
++special;
|
|
|
|
if (*special) {
|
|
|
|
n = strcspn(special, " \t");
|
|
|
|
if (!nasm_strnicmp(special, "default", n))
|
|
|
|
sym->other = STV_DEFAULT;
|
|
|
|
else if (!nasm_strnicmp(special, "internal", n))
|
|
|
|
sym->other = STV_INTERNAL;
|
|
|
|
else if (!nasm_strnicmp(special, "hidden", n))
|
|
|
|
sym->other = STV_HIDDEN;
|
|
|
|
else if (!nasm_strnicmp(special, "protected", n))
|
|
|
|
sym->other = STV_PROTECTED;
|
|
|
|
else
|
|
|
|
n = 0;
|
|
|
|
special += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*special) {
|
2005-01-16 06:15:51 +08:00
|
|
|
struct tokenval tokval;
|
|
|
|
expr *e;
|
2007-10-11 05:58:45 +08:00
|
|
|
int fwd = 0;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
|
2005-01-16 06:15:51 +08: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 */
|
|
|
|
}
|
2007-10-11 05:58:45 +08:00
|
|
|
special_used = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sym->globnum = nglobs;
|
|
|
|
nglobs++;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
nlocals++;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
if (special && !special_used)
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "no special symbol features supported here");
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static void elf_add_reloc(struct Section *sect, int32_t segment, int 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)
|
2007-10-22 05:21:43 +08:00
|
|
|
r->symbol = 0;
|
2002-05-01 04:51:53 +08:00
|
|
|
else {
|
2005-01-16 06:15:51 +08:00
|
|
|
int i;
|
|
|
|
r->symbol = 0;
|
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (segment == sects[i]->index)
|
2007-10-22 05:21:43 +08:00
|
|
|
r->symbol = i + 2;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!r->symbol)
|
|
|
|
r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
|
2002-05-01 04:51:53 +08:00
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
r->type = type;
|
|
|
|
|
|
|
|
sect->nrelocs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine deals with ..got and ..sym relocations: the more
|
|
|
|
* complicated kinds. In shared-library writing, some relocations
|
|
|
|
* with respect to global symbols must refer to the precise symbol
|
|
|
|
* rather than referring to an offset from the base of the section
|
|
|
|
* _containing_ the symbol. Such relocations call to this routine,
|
|
|
|
* which searches the symbol list for the symbol in question.
|
|
|
|
*
|
|
|
|
* R_386_GOT32 references require the _exact_ symbol address to be
|
|
|
|
* used; R_386_32 references can be at an offset from the symbol.
|
|
|
|
* The boolean argument `exact' tells us this.
|
|
|
|
*
|
|
|
|
* Return value is the adjusted value of `addr', having become an
|
|
|
|
* offset from the symbol rather than the section. Should always be
|
|
|
|
* zero when returning from an exact call.
|
|
|
|
*
|
|
|
|
* Limitation: if you define two symbols at the same place,
|
|
|
|
* confusion will occur.
|
|
|
|
*
|
|
|
|
* Inefficiency: we search, currently, using a linked list which
|
|
|
|
* isn't even necessarily sorted.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t elf_add_gsym_reloc(struct Section *sect,
|
|
|
|
int32_t segment, int32_t offset,
|
2007-10-11 05:58:45 +08:00
|
|
|
int type, bool exact)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
struct Reloc *r;
|
|
|
|
struct Section *s;
|
|
|
|
struct Symbol *sym, *sm;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First look up the segment/offset pair and find a global
|
|
|
|
* symbol corresponding to it. If it's not one of our segments,
|
|
|
|
* then it must be an external symbol, in which case we're fine
|
|
|
|
* doing a normal elf_add_reloc after first sanity-checking
|
|
|
|
* that the offset from the symbol is zero.
|
|
|
|
*/
|
|
|
|
s = NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (segment == sects[i]->index) {
|
|
|
|
s = sects[i];
|
|
|
|
break;
|
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!s) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (exact && offset != 0)
|
|
|
|
error(ERR_NONFATAL, "unable to find a suitable global symbol"
|
|
|
|
" for this reference");
|
|
|
|
else
|
|
|
|
elf_add_reloc(sect, segment, type);
|
|
|
|
return offset;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (exact) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Find a symbol pointing _exactly_ at this one.
|
|
|
|
*/
|
|
|
|
for (sym = s->gsyms; sym; sym = sym->next)
|
|
|
|
if (sym->value == offset)
|
|
|
|
break;
|
2002-05-01 04:52:49 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Find the nearest symbol below this one.
|
|
|
|
*/
|
|
|
|
sym = NULL;
|
|
|
|
for (sm = s->gsyms; sm; sm = sm->next)
|
|
|
|
if (sm->value <= offset && (!sym || sm->value > sym->value))
|
|
|
|
sym = sm;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
if (!sym && exact) {
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "unable to find a suitable global symbol"
|
|
|
|
" for this reference");
|
|
|
|
return 0;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
|
|
|
|
sect->tail = &r->next;
|
|
|
|
r->next = NULL;
|
|
|
|
|
|
|
|
r->address = sect->len;
|
|
|
|
r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
|
|
|
|
r->type = type;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
sect->nrelocs++;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
return offset - sym->value;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
static void elf_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
|
|
|
int32_t addr;
|
|
|
|
uint8_t mydata[4], *p;
|
2002-05-01 04:51:53 +08:00
|
|
|
int i;
|
2003-07-10 03:11:10 +08:00
|
|
|
static struct symlininfo sinfo;
|
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 != elf_section_names(".text", 2, &tempint))
|
|
|
|
error(ERR_PANIC, "strange segment conditions in ELF driver");
|
|
|
|
else {
|
|
|
|
s = sects[nsects - 1];
|
|
|
|
i = nsects - 1;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2003-07-10 03:11:10 +08:00
|
|
|
/* again some stabs debugging stuff */
|
2007-04-28 14:18:48 +08:00
|
|
|
if (of_elf32.current_dfmt) {
|
2005-01-16 06:15:51 +08:00
|
|
|
sinfo.offset = s->len;
|
|
|
|
sinfo.section = i;
|
|
|
|
sinfo.name = s->name;
|
2007-04-28 14:18:48 +08:00
|
|
|
of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
/* end of debugging stuff */
|
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
if (s->type == SHT_NOBITS && 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->type == SHT_PROGBITS) {
|
2007-04-12 10:40:54 +08:00
|
|
|
error(ERR_WARNING, "uninitialized space declared in"
|
2005-01-16 06:15:51 +08:00
|
|
|
" non-BSS section `%s': zeroing", s->name);
|
2007-11-10 06:44:02 +08:00
|
|
|
elf_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
|
|
|
elf_sect_write(s, data, size);
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_ADDRESS) {
|
2007-10-11 15:05:31 +08:00
|
|
|
bool gnu16 = false;
|
2007-04-12 10:40:54 +08:00
|
|
|
addr = *(int32_t *)data;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segment != NO_SEG) {
|
|
|
|
if (segment % 2) {
|
|
|
|
error(ERR_NONFATAL, "ELF format does not support"
|
|
|
|
" segment base references");
|
|
|
|
} else {
|
|
|
|
if (wrt == NO_SEG) {
|
2007-11-10 06:44:02 +08:00
|
|
|
if (size == 2) {
|
2007-10-11 15:05:31 +08:00
|
|
|
gnu16 = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
elf_add_reloc(s, segment, R_386_16);
|
|
|
|
} else {
|
|
|
|
elf_add_reloc(s, segment, R_386_32);
|
|
|
|
}
|
|
|
|
} else if (wrt == elf_gotpc_sect + 1) {
|
|
|
|
/*
|
|
|
|
* The user will supply GOT relative to $$. ELF
|
|
|
|
* will let us have GOT relative to $. So we
|
|
|
|
* need to fix up the data item by $-$$.
|
|
|
|
*/
|
|
|
|
addr += s->len;
|
|
|
|
elf_add_reloc(s, segment, R_386_GOTPC);
|
|
|
|
} else if (wrt == elf_gotoff_sect + 1) {
|
|
|
|
elf_add_reloc(s, segment, R_386_GOTOFF);
|
|
|
|
} else if (wrt == elf_got_sect + 1) {
|
|
|
|
addr = elf_add_gsym_reloc(s, segment, addr,
|
2007-10-11 05:58:45 +08:00
|
|
|
R_386_GOT32, true);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else if (wrt == elf_sym_sect + 1) {
|
2007-11-10 06:44:02 +08:00
|
|
|
if (size == 2) {
|
2007-10-11 15:05:31 +08:00
|
|
|
gnu16 = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
addr = elf_add_gsym_reloc(s, segment, addr,
|
2007-10-11 05:58:45 +08:00
|
|
|
R_386_16, false);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
|
|
|
addr = elf_add_gsym_reloc(s, segment, addr,
|
2007-10-11 05:58:45 +08:00
|
|
|
R_386_32, false);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
} else if (wrt == elf_plt_sect + 1) {
|
|
|
|
error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
|
|
|
|
"relative PLT references");
|
|
|
|
} else {
|
|
|
|
error(ERR_NONFATAL, "ELF format does not support this"
|
|
|
|
" use of WRT");
|
|
|
|
wrt = NO_SEG; /* we can at least _try_ to continue */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = mydata;
|
|
|
|
if (gnu16) {
|
|
|
|
error(ERR_WARNING | ERR_WARN_GNUELF,
|
|
|
|
"16-bit relocations in ELF is a GNU extension");
|
|
|
|
WRITESHORT(p, addr);
|
|
|
|
} else {
|
2007-11-10 06:44:02 +08:00
|
|
|
if (size != 4 && segment != NO_SEG) {
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"Unsupported non-32-bit ELF relocation");
|
|
|
|
}
|
|
|
|
WRITELONG(p, addr);
|
|
|
|
}
|
2007-11-10 06:44:02 +08:00
|
|
|
elf_sect_write(s, mydata, size);
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_REL2ADR) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segment == segto)
|
|
|
|
error(ERR_PANIC, "intra-segment OUT_REL2ADR");
|
|
|
|
if (segment != NO_SEG && segment % 2) {
|
|
|
|
error(ERR_NONFATAL, "ELF format does not support"
|
|
|
|
" segment base references");
|
|
|
|
} else {
|
|
|
|
if (wrt == NO_SEG) {
|
|
|
|
error(ERR_WARNING | ERR_WARN_GNUELF,
|
|
|
|
"16-bit relocations in ELF is a GNU extension");
|
|
|
|
elf_add_reloc(s, segment, R_386_PC16);
|
|
|
|
} else {
|
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"Unsupported non-32-bit ELF relocation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = mydata;
|
2007-11-10 06:44:02 +08:00
|
|
|
WRITESHORT(p, *(int32_t *)data - size);
|
2005-01-16 06:15:51 +08:00
|
|
|
elf_sect_write(s, mydata, 2L);
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (type == OUT_REL4ADR) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segment == segto)
|
|
|
|
error(ERR_PANIC, "intra-segment OUT_REL4ADR");
|
|
|
|
if (segment != NO_SEG && segment % 2) {
|
|
|
|
error(ERR_NONFATAL, "ELF format does not support"
|
|
|
|
" segment base references");
|
|
|
|
} else {
|
|
|
|
if (wrt == NO_SEG) {
|
|
|
|
elf_add_reloc(s, segment, R_386_PC32);
|
|
|
|
} else if (wrt == elf_plt_sect + 1) {
|
|
|
|
elf_add_reloc(s, segment, R_386_PLT32);
|
|
|
|
} else if (wrt == elf_gotpc_sect + 1 ||
|
|
|
|
wrt == elf_gotoff_sect + 1 ||
|
|
|
|
wrt == elf_got_sect + 1) {
|
|
|
|
error(ERR_NONFATAL, "ELF format cannot produce PC-"
|
|
|
|
"relative GOT references");
|
|
|
|
} else {
|
|
|
|
error(ERR_NONFATAL, "ELF format does not support this"
|
|
|
|
" use of WRT");
|
|
|
|
wrt = NO_SEG; /* we can at least _try_ to continue */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = mydata;
|
2007-11-10 06:44:02 +08:00
|
|
|
WRITELONG(p, *(int32_t *)data - size);
|
2005-01-16 06:15:51 +08:00
|
|
|
elf_sect_write(s, mydata, 4L);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-10 03:11:10 +08:00
|
|
|
static void elf_write(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
int nsections, align;
|
2003-07-10 03:11:10 +08:00
|
|
|
int scount;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p;
|
2002-05-01 04:51:53 +08:00
|
|
|
int commlen;
|
2007-04-14 00:47:53 +08:00
|
|
|
char comment[64];
|
2002-05-01 04:51:53 +08:00
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:51:53 +08:00
|
|
|
struct SAA *symtab;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t symtablen, symtablocal;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
2002-05-01 04:51:53 +08:00
|
|
|
* Work out how many sections we will have. We have SHN_UNDEF,
|
|
|
|
* then the flexible user sections, then the four fixed
|
|
|
|
* sections `.comment', `.shstrtab', `.symtab' and `.strtab',
|
|
|
|
* then optionally relocation sections for the user sections.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-04-28 14:18:48 +08:00
|
|
|
if (of_elf32.current_dfmt == &df_stabs)
|
2005-01-16 06:15:51 +08:00
|
|
|
nsections = 8;
|
2003-07-10 03:11:10 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
nsections = 5; /* SHN_UNDEF and the fixed ones */
|
|
|
|
|
|
|
|
add_sectname("", ".comment");
|
|
|
|
add_sectname("", ".shstrtab");
|
|
|
|
add_sectname("", ".symtab");
|
|
|
|
add_sectname("", ".strtab");
|
|
|
|
for (i = 0; i < nsects; i++) {
|
|
|
|
nsections++; /* for the section itself */
|
|
|
|
if (sects[i]->head) {
|
|
|
|
nsections++; /* for its relocations */
|
|
|
|
add_sectname(".rel", sects[i]->name);
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
if (of_elf32.current_dfmt == &df_stabs) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/* in case the debug information is wanted, just add these three sections... */
|
|
|
|
add_sectname("", ".stab");
|
|
|
|
add_sectname("", ".stabstr");
|
|
|
|
add_sectname(".rel", ".stab");
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Do the comment.
|
|
|
|
*/
|
|
|
|
*comment = '\0';
|
2005-01-16 06:15:51 +08:00
|
|
|
commlen =
|
|
|
|
2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the ELF header.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint16_t(1, elffp); /* ET_REL relocatable file */
|
|
|
|
fwriteint16_t(3, elffp); /* EM_386 processor ID */
|
|
|
|
fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
|
|
|
|
fwriteint32_t(0L, elffp); /* no entry point */
|
|
|
|
fwriteint32_t(0L, elffp); /* no program header table */
|
|
|
|
fwriteint32_t(0x40L, elffp); /* section headers straight after
|
2005-01-16 06:15:51 +08:00
|
|
|
* ELF header plus alignment */
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(0L, elffp); /* 386 defines no special flags */
|
|
|
|
fwriteint16_t(0x34, elffp); /* size of ELF header */
|
|
|
|
fwriteint16_t(0, elffp); /* no program header table, again */
|
|
|
|
fwriteint16_t(0, elffp); /* still no program header table */
|
|
|
|
fwriteint16_t(0x28, elffp); /* size of section header */
|
|
|
|
fwriteint16_t(nsections, elffp); /* number of sections */
|
|
|
|
fwriteint16_t(nsects + 2, elffp); /* string table section index for
|
2005-01-16 06:15:51 +08:00
|
|
|
* section header table */
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
|
|
|
|
fwriteint32_t(0L, elffp);
|
|
|
|
fwriteint32_t(0L, elffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Build the symbol table and relocation tables.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
symtab = elf_build_symtab(&symtablen, &symtablocal);
|
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (sects[i]->head)
|
|
|
|
sects[i]->rel = elf_build_reltab(§s[i]->rellen,
|
|
|
|
sects[i]->head);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now output the section header table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
elf_foffs = 0x40 + 0x28 * nsections;
|
2005-01-16 06:15:51 +08:00
|
|
|
align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
|
2002-05-01 04:51:32 +08:00
|
|
|
elf_foffs += align;
|
|
|
|
elf_nsect = 0;
|
2002-05-01 04:53:55 +08:00
|
|
|
elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
|
2005-01-16 06:15:51 +08:00
|
|
|
scount = 1; /* needed for the stabs debugging to track the symtable section */
|
|
|
|
p = shstrtab + 1;
|
|
|
|
for (i = 0; i < nsects; i++) {
|
|
|
|
elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
|
|
|
|
(sects[i]->type == SHT_PROGBITS ?
|
2007-10-11 05:58:45 +08:00
|
|
|
sects[i]->data : NULL), true,
|
2005-01-16 06:15:51 +08:00
|
|
|
sects[i]->len, 0, 0, sects[i]->align, 0);
|
|
|
|
p += strlen(p) + 1;
|
|
|
|
scount++; /* dito */
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
|
2005-01-16 06:15:51 +08:00
|
|
|
scount++; /* dito */
|
|
|
|
p += strlen(p) + 1;
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
|
2005-01-16 06:15:51 +08:00
|
|
|
scount++; /* dito */
|
|
|
|
p += strlen(p) + 1;
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
|
2005-01-16 06:15:51 +08:00
|
|
|
symtabsection = scount; /* now we got the symtab section index in the ELF file */
|
|
|
|
p += strlen(p) + 1;
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < nsects; i++)
|
|
|
|
if (sects[i]->head) {
|
|
|
|
p += strlen(p) + 1;
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
|
2005-01-16 06:15:51 +08:00
|
|
|
sects[i]->rellen, nsects + 3, i + 1, 4, 8);
|
|
|
|
}
|
2007-04-28 14:18:48 +08:00
|
|
|
if (of_elf32.current_dfmt == &df_stabs) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/* for debugging information, create the last three sections
|
|
|
|
which are the .stab , .stabstr and .rel.stab sections respectively */
|
|
|
|
|
|
|
|
/* this function call creates the stab sections in memory */
|
2007-04-28 14:18:48 +08:00
|
|
|
stabs32_generate();
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
|
|
|
|
p += strlen(p) + 1;
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
|
2005-01-16 06:15:51 +08:00
|
|
|
nsections - 2, 0, 4, 12);
|
|
|
|
|
|
|
|
p += strlen(p) + 1;
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
|
2005-01-16 06:15:51 +08:00
|
|
|
stabstrlen, 0, 0, 4, 0);
|
|
|
|
|
|
|
|
p += strlen(p) + 1;
|
|
|
|
/* link -> symtable info -> section to refer to */
|
2007-10-11 05:58:45 +08:00
|
|
|
elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
|
2005-01-16 06:15:51 +08:00
|
|
|
stabrellen, symtabsection, nsections - 3, 4,
|
|
|
|
8);
|
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
fwrite(align_str, align, 1, elffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now output the sections.
|
|
|
|
*/
|
|
|
|
elf_write_sections();
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(elf_sects);
|
|
|
|
saa_free(symtab);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct SAA *s = saa_init(1L);
|
|
|
|
struct Symbol *sym;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t entry[16], *p;
|
2002-05-01 04:51:32 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
*len = *local = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, an all-zeros entry, required by the ELF spec.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_wbytes(s, NULL, 16L); /* null symbol table entry */
|
2002-05-01 04:51:32 +08:00
|
|
|
*len += 16;
|
|
|
|
(*local)++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, an entry for the file name.
|
|
|
|
*/
|
|
|
|
p = entry;
|
2007-10-22 05:21:43 +08:00
|
|
|
WRITELONG(p, 1); /* we know it's 1st entry in strtab */
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITELONG(p, 0); /* no value */
|
|
|
|
WRITELONG(p, 0); /* no size either */
|
2007-10-29 06:29:54 +08:00
|
|
|
WRITESHORT(p, STT_FILE); /* type FILE */
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITESHORT(p, SHN_ABS);
|
|
|
|
saa_wbytes(s, entry, 16L);
|
2002-05-01 04:51:32 +08:00
|
|
|
*len += 16;
|
|
|
|
(*local)++;
|
|
|
|
|
|
|
|
/*
|
2002-05-01 04:51:53 +08:00
|
|
|
* Now some standard symbols defining the segments, for relocation
|
2002-05-01 04:51:32 +08:00
|
|
|
* purposes.
|
|
|
|
*/
|
2007-10-22 05:21:43 +08:00
|
|
|
for (i = 1; i <= nsects; i++) {
|
2005-01-16 06:15:51 +08:00
|
|
|
p = entry;
|
|
|
|
WRITELONG(p, 0); /* no symbol name */
|
|
|
|
WRITELONG(p, 0); /* offset zero */
|
|
|
|
WRITELONG(p, 0); /* size zero */
|
2007-10-29 06:29:54 +08:00
|
|
|
WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
|
2007-10-22 05:21:43 +08:00
|
|
|
WRITESHORT(p, i); /* section id */
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_wbytes(s, entry, 16L);
|
|
|
|
*len += 16;
|
|
|
|
(*local)++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now the other local symbols.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_rewind(syms);
|
|
|
|
while ((sym = saa_rstruct(syms))) {
|
|
|
|
if (sym->type & SYM_GLOBAL)
|
|
|
|
continue;
|
|
|
|
p = entry;
|
|
|
|
WRITELONG(p, sym->strpos);
|
|
|
|
WRITELONG(p, sym->value);
|
|
|
|
WRITELONG(p, sym->size);
|
2007-10-22 05:21:43 +08:00
|
|
|
WRITECHAR(p, sym->type); /* type and binding */
|
|
|
|
WRITECHAR(p, sym->other); /* visibility */
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITESHORT(p, sym->section);
|
|
|
|
saa_wbytes(s, entry, 16L);
|
2002-05-01 04:51:32 +08:00
|
|
|
*len += 16;
|
2005-01-16 06:15:51 +08:00
|
|
|
(*local)++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now the global symbols.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_rewind(syms);
|
|
|
|
while ((sym = saa_rstruct(syms))) {
|
|
|
|
if (!(sym->type & SYM_GLOBAL))
|
|
|
|
continue;
|
|
|
|
p = entry;
|
|
|
|
WRITELONG(p, sym->strpos);
|
|
|
|
WRITELONG(p, sym->value);
|
|
|
|
WRITELONG(p, sym->size);
|
2007-10-22 05:21:43 +08:00
|
|
|
WRITECHAR(p, sym->type); /* type and binding */
|
|
|
|
WRITECHAR(p, sym->other); /* visibility */
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITESHORT(p, sym->section);
|
|
|
|
saa_wbytes(s, entry, 16L);
|
|
|
|
*len += 16;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct SAA *s;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t *p, entry[8];
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (!r)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
s = saa_init(1L);
|
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
while (r) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t sym = r->symbol;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (sym >= GLOBAL_TEMP_BASE)
|
2007-10-22 05:21:43 +08:00
|
|
|
sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
p = entry;
|
|
|
|
WRITELONG(p, r->address);
|
|
|
|
WRITELONG(p, (sym << 8) + r->type);
|
|
|
|
saa_wbytes(s, entry, 8L);
|
|
|
|
*len += 8;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
r = r->next;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void elf_section_header(int name, int type, int flags,
|
2007-10-11 05:58:45 +08:00
|
|
|
void *data, bool is_saa, int32_t datalen,
|
2005-01-16 06:15:51 +08:00
|
|
|
int link, int info, int align, int eltsize)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
elf_sects[elf_nsect].data = data;
|
|
|
|
elf_sects[elf_nsect].len = datalen;
|
|
|
|
elf_sects[elf_nsect].is_saa = is_saa;
|
|
|
|
elf_nsect++;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t((int32_t)name, elffp);
|
|
|
|
fwriteint32_t((int32_t)type, elffp);
|
|
|
|
fwriteint32_t((int32_t)flags, elffp);
|
|
|
|
fwriteint32_t(0L, elffp); /* no address, ever, in object files */
|
|
|
|
fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
|
|
|
|
fwriteint32_t(datalen, elffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
if (data)
|
2005-01-16 06:15:51 +08:00
|
|
|
elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
|
2007-04-12 10:40:54 +08:00
|
|
|
fwriteint32_t((int32_t)link, elffp);
|
|
|
|
fwriteint32_t((int32_t)info, elffp);
|
|
|
|
fwriteint32_t((int32_t)align, elffp);
|
|
|
|
fwriteint32_t((int32_t)eltsize, elffp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void elf_write_sections(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < elf_nsect; i++)
|
2005-01-16 06:15:51 +08:00
|
|
|
if (elf_sects[i].data) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t len = elf_sects[i].len;
|
|
|
|
int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
|
|
|
|
int32_t align = reallen - len;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (elf_sects[i].is_saa)
|
|
|
|
saa_fpwrite(elf_sects[i].data, elffp);
|
|
|
|
else
|
|
|
|
fwrite(elf_sects[i].data, len, 1, elffp);
|
|
|
|
fwrite(align_str, align, 1, elffp);
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void elf_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;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t elf_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 int elf_directive(char *directive, char *value, int pass)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-17 13:14:50 +08:00
|
|
|
(void)directive;
|
|
|
|
(void)value;
|
|
|
|
(void)pass;
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void elf_filename(char *inname, char *outname, efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
strcpy(elf_module, 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 const char *elf_stdmac[] = {
|
2002-05-01 04:52:49 +08:00
|
|
|
"%define __SECT__ [section .text]",
|
2002-05-01 04:53:55 +08:00
|
|
|
"%macro __NASM_CDecl__ 1",
|
|
|
|
"%define $_%1 $%1",
|
|
|
|
"%endmacro",
|
2002-05-01 04:52:49 +08:00
|
|
|
NULL
|
|
|
|
};
|
2007-04-14 00:47:53 +08:00
|
|
|
static int elf_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;
|
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2003-07-10 03:11:10 +08:00
|
|
|
static struct dfmt df_stabs = {
|
2005-01-16 06:15:51 +08:00
|
|
|
"ELF32 (i386) stabs debug format for Linux",
|
|
|
|
"stabs",
|
2007-04-28 14:18:48 +08:00
|
|
|
stabs32_init,
|
|
|
|
stabs32_linenum,
|
|
|
|
stabs32_deflabel,
|
|
|
|
stabs32_directive,
|
|
|
|
stabs32_typevalue,
|
|
|
|
stabs32_output,
|
|
|
|
stabs32_cleanup
|
2003-07-10 03:11:10 +08:00
|
|
|
};
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
struct ofmt of_elf32 = {
|
2002-05-01 04:51:32 +08:00
|
|
|
"ELF32 (i386) object files (e.g. Linux)",
|
2007-04-29 08:28:24 +08:00
|
|
|
"elf32",
|
2002-05-01 04:53:55 +08:00
|
|
|
NULL,
|
2007-04-28 14:18:48 +08:00
|
|
|
elf32_debugs_arr,
|
2002-05-01 04:53:55 +08:00
|
|
|
&null_debug_form,
|
2002-05-01 04:52:49 +08:00
|
|
|
elf_stdmac,
|
2002-05-01 04:51:32 +08:00
|
|
|
elf_init,
|
2002-05-01 04:53:55 +08:00
|
|
|
elf_set_info,
|
2002-05-01 04:51:32 +08:00
|
|
|
elf_out,
|
|
|
|
elf_deflabel,
|
|
|
|
elf_section_names,
|
|
|
|
elf_segbase,
|
|
|
|
elf_directive,
|
|
|
|
elf_filename,
|
|
|
|
elf_cleanup
|
|
|
|
};
|
|
|
|
|
2007-04-29 08:28:24 +08:00
|
|
|
struct ofmt of_elf = {
|
|
|
|
"ELF (short name for ELF32) ",
|
|
|
|
"elf",
|
|
|
|
NULL,
|
|
|
|
elf32_debugs_arr,
|
|
|
|
&null_debug_form,
|
|
|
|
elf_stdmac,
|
|
|
|
elf_init,
|
|
|
|
elf_set_info,
|
|
|
|
elf_out,
|
|
|
|
elf_deflabel,
|
|
|
|
elf_section_names,
|
|
|
|
elf_segbase,
|
|
|
|
elf_directive,
|
|
|
|
elf_filename,
|
|
|
|
elf_cleanup
|
|
|
|
};
|
2003-07-10 03:11:10 +08:00
|
|
|
/* again, the stabs debugging stuff (code) */
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)of;
|
|
|
|
(void)id;
|
|
|
|
(void)fp;
|
|
|
|
(void)error;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)segto;
|
2007-09-17 13:14:50 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!stabs_filename) {
|
2007-04-14 00:47:53 +08:00
|
|
|
stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(stabs_filename, filename);
|
|
|
|
} else {
|
|
|
|
if (strcmp(stabs_filename, filename)) {
|
|
|
|
/* yep, a memory leak...this program is one-shot anyway, so who cares...
|
|
|
|
in fact, this leak comes in quite handy to maintain a list of files
|
|
|
|
encountered so far in the symbol lines... */
|
|
|
|
|
|
|
|
/* why not nasm_free(stabs_filename); we're done with the old one */
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(stabs_filename, filename);
|
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
stabs_immcall = 1;
|
|
|
|
currentline = linenumber;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2007-11-06 09:19:32 +08:00
|
|
|
void stabs32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
|
2007-04-14 00:47:53 +08:00
|
|
|
char *special)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2007-04-28 14:18:48 +08:00
|
|
|
(void)name;
|
|
|
|
(void)segment;
|
|
|
|
(void)offset;
|
|
|
|
(void)is_global;
|
|
|
|
(void)special;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_directive(const char *directive, const char *params)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2007-04-28 14:18:48 +08:00
|
|
|
(void)directive;
|
|
|
|
(void)params;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_typevalue(int32_t type)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2007-10-29 06:29:54 +08:00
|
|
|
int32_t stype, ssize;
|
|
|
|
switch (TYM_TYPE(type)) {
|
|
|
|
case TY_LABEL:
|
|
|
|
ssize = 0;
|
|
|
|
stype = STT_NOTYPE;
|
|
|
|
break;
|
|
|
|
case TY_BYTE:
|
|
|
|
ssize = 1;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_WORD:
|
|
|
|
ssize = 2;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_DWORD:
|
|
|
|
ssize = 4;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
ssize = 4;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_QWORD:
|
|
|
|
ssize = 8;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_TBYTE:
|
|
|
|
ssize = 10;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_OWORD:
|
|
|
|
ssize = 8;
|
|
|
|
stype = STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case TY_COMMON:
|
|
|
|
ssize = 0;
|
|
|
|
stype = STT_COMMON;
|
|
|
|
break;
|
|
|
|
case TY_SEG:
|
|
|
|
ssize = 0;
|
|
|
|
stype = STT_SECTION;
|
|
|
|
break;
|
|
|
|
case TY_EXTERN:
|
|
|
|
ssize = 0;
|
|
|
|
stype = STT_NOTYPE;
|
|
|
|
break;
|
|
|
|
case TY_EQU:
|
|
|
|
ssize = 0;
|
|
|
|
stype = STT_NOTYPE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ssize = 0;
|
|
|
|
stype = STT_NOTYPE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (stype == STT_OBJECT && !lastsym->type) {
|
|
|
|
lastsym->size = ssize;
|
|
|
|
lastsym->type = stype;
|
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_output(int type, void *param)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
struct symlininfo *s;
|
|
|
|
struct linelist *el;
|
|
|
|
if (type == TY_STABSSYMLIN) {
|
|
|
|
if (stabs_immcall) {
|
|
|
|
s = (struct symlininfo *)param;
|
2007-10-18 08:55:45 +08:00
|
|
|
if (!(sects[s->section]->flags & SHF_EXECINSTR))
|
2005-01-16 06:15:51 +08:00
|
|
|
return; /* we are only interested in the text stuff */
|
|
|
|
numlinestabs++;
|
|
|
|
el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
|
|
|
|
el->info.offset = s->offset;
|
|
|
|
el->info.section = s->section;
|
|
|
|
el->info.name = s->name;
|
|
|
|
el->line = currentline;
|
|
|
|
el->filename = stabs_filename;
|
|
|
|
el->next = 0;
|
|
|
|
if (stabslines) {
|
|
|
|
stabslines->last->next = el;
|
|
|
|
stabslines->last = el;
|
|
|
|
} else {
|
|
|
|
stabslines = el;
|
|
|
|
stabslines->last = el;
|
|
|
|
}
|
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
stabs_immcall = 0;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 08:50:27 +08:00
|
|
|
#define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
|
|
|
|
do {\
|
|
|
|
WRITELONG(p,n_strx); \
|
|
|
|
WRITECHAR(p,n_type); \
|
|
|
|
WRITECHAR(p,n_other); \
|
|
|
|
WRITESHORT(p,n_desc); \
|
|
|
|
WRITELONG(p,n_value); \
|
|
|
|
} while (0)
|
2003-07-10 03:11:10 +08:00
|
|
|
|
|
|
|
/* for creating the .stab , .stabstr and .rel.stab sections in memory */
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
void stabs32_generate(void)
|
2003-09-06 08:50:27 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
|
2007-04-14 00:47:53 +08:00
|
|
|
char **allfiles;
|
2005-01-16 06:15:51 +08:00
|
|
|
int *fileidx;
|
|
|
|
|
|
|
|
struct linelist *ptr;
|
|
|
|
|
|
|
|
ptr = stabslines;
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < numlinestabs; i++)
|
|
|
|
allfiles[i] = 0;
|
|
|
|
numfiles = 0;
|
|
|
|
while (ptr) {
|
|
|
|
if (numfiles == 0) {
|
|
|
|
allfiles[0] = ptr->filename;
|
|
|
|
numfiles++;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < numfiles; i++) {
|
|
|
|
if (!strcmp(allfiles[i], ptr->filename))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= numfiles) {
|
|
|
|
allfiles[i] = ptr->filename;
|
|
|
|
numfiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
strsize = 1;
|
|
|
|
fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
|
|
|
|
for (i = 0; i < numfiles; i++) {
|
|
|
|
fileidx[i] = strsize;
|
|
|
|
strsize += strlen(allfiles[i]) + 1;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
mainfileindex = 0;
|
|
|
|
for (i = 0; i < numfiles; i++) {
|
|
|
|
if (!strcmp(allfiles[i], elf_module)) {
|
|
|
|
mainfileindex = i;
|
|
|
|
break;
|
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
2003-09-06 08:50:27 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
/* worst case size of the stab buffer would be:
|
|
|
|
the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
|
|
|
|
*/
|
|
|
|
sbuf =
|
2007-04-12 10:40:54 +08:00
|
|
|
(uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
|
2005-01-16 06:15:51 +08:00
|
|
|
sizeof(struct stabentry));
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
ssbuf = (uint8_t *)nasm_malloc(strsize);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
|
2005-01-16 06:15:51 +08:00
|
|
|
rptr = rbuf;
|
|
|
|
|
|
|
|
for (i = 0; i < numfiles; i++) {
|
2007-04-14 00:47:53 +08:00
|
|
|
strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
ssbuf[0] = 0;
|
|
|
|
|
|
|
|
stabstrlen = strsize; /* set global variable for length of stab strings */
|
|
|
|
|
|
|
|
sptr = sbuf;
|
|
|
|
ptr = stabslines;
|
|
|
|
numstabs = 0;
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ptr) {
|
2007-10-18 08:55:45 +08:00
|
|
|
/* this is the first stab, its strx points to the filename of the
|
|
|
|
the source-file, the n_desc field should be set to the number
|
|
|
|
of remaining stabs
|
|
|
|
*/
|
|
|
|
WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
/* this is the stab for the main source file */
|
|
|
|
WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2007-04-30 04:57:53 +08:00
|
|
|
/* relocation table entry */
|
|
|
|
|
2007-10-29 06:29:54 +08:00
|
|
|
/* Since the symbol table has two entries before */
|
|
|
|
/* the section symbols, the index in the info.section */
|
|
|
|
/* member must be adjusted by adding 2 */
|
2007-04-30 04:57:53 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITELONG(rptr, (sptr - sbuf) - 4);
|
2007-10-29 06:29:54 +08:00
|
|
|
WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
numstabs++;
|
|
|
|
currfile = mainfileindex;
|
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
while (ptr) {
|
|
|
|
if (strcmp(allfiles[currfile], ptr->filename)) {
|
|
|
|
/* oops file has changed... */
|
|
|
|
for (i = 0; i < numfiles; i++)
|
|
|
|
if (!strcmp(allfiles[i], ptr->filename))
|
|
|
|
break;
|
|
|
|
currfile = i;
|
|
|
|
WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
|
|
|
|
ptr->info.offset);
|
|
|
|
numstabs++;
|
|
|
|
|
2007-04-30 04:57:53 +08:00
|
|
|
/* relocation table entry */
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITELONG(rptr, (sptr - sbuf) - 4);
|
2007-10-29 06:29:54 +08:00
|
|
|
WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
|
|
|
|
numstabs++;
|
|
|
|
|
2007-04-30 04:57:53 +08:00
|
|
|
/* relocation table entry */
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
WRITELONG(rptr, (sptr - sbuf) - 4);
|
2007-10-29 06:29:54 +08:00
|
|
|
WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
ptr = ptr->next;
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
((struct stabentry *)sbuf)->n_desc = numstabs;
|
2003-07-10 03:11:10 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(allfiles);
|
|
|
|
nasm_free(fileidx);
|
|
|
|
|
|
|
|
stablen = (sptr - sbuf);
|
|
|
|
stabrellen = (rptr - rbuf);
|
|
|
|
stabrelbuf = rbuf;
|
|
|
|
stabbuf = sbuf;
|
|
|
|
stabstrbuf = ssbuf;
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2007-09-17 13:14:50 +08:00
|
|
|
void stabs32_cleanup(void)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
|
|
|
struct linelist *ptr, *del;
|
|
|
|
if (!stabslines)
|
|
|
|
return;
|
|
|
|
ptr = stabslines;
|
|
|
|
while (ptr) {
|
|
|
|
del = ptr;
|
|
|
|
ptr = ptr->next;
|
|
|
|
nasm_free(del);
|
|
|
|
}
|
|
|
|
if (stabbuf)
|
|
|
|
nasm_free(stabbuf);
|
|
|
|
if (stabrelbuf)
|
|
|
|
nasm_free(stabrelbuf);
|
|
|
|
if (stabstrbuf)
|
|
|
|
nasm_free(stabstrbuf);
|
2003-07-10 03:11:10 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
#endif /* OF_ELF */
|