NASM 2.00rc2
[nasm.git] / output / outelf32.c
blobab56eb77c36b2b67b62b935cf38a13f3f29c55ad
1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "stdscan.h"
21 #include "outform.h"
23 #ifdef OF_ELF32
26 * Relocation types.
28 enum reloc_type {
29 R_386_32 = 1, /* ordinary absolute relocation */
30 R_386_PC32 = 2, /* PC-relative relocation */
31 R_386_GOT32 = 3, /* an offset into GOT */
32 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
33 R_386_COPY = 5, /* ??? */
34 R_386_GLOB_DAT = 6, /* ??? */
35 R_386_JUMP_SLOT = 7, /* ??? */
36 R_386_RELATIVE = 8, /* ??? */
37 R_386_GOTOFF = 9, /* an offset from GOT base */
38 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
39 /* These are GNU extensions, but useful */
40 R_386_16 = 20, /* A 16-bit absolute relocation */
41 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
42 R_386_8 = 22, /* An 8-bit absolute relocation */
43 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
46 struct Reloc {
47 struct Reloc *next;
48 int32_t address; /* relative to _start_ of section */
49 int32_t symbol; /* symbol index */
50 int type; /* type of relocation */
53 struct Symbol {
54 int32_t strpos; /* string table position of name */
55 int32_t section; /* section ID of the symbol */
56 int type; /* symbol type */
57 int other; /* symbol visibility */
58 int32_t value; /* address, or COMMON variable align */
59 int32_t size; /* size of symbol */
60 int32_t globnum; /* symbol table offset if global */
61 struct Symbol *next; /* list of globals in each section */
62 struct Symbol *nextfwd; /* list of unresolved-size symbols */
63 char *name; /* used temporarily if in above list */
66 #define SHT_PROGBITS 1
67 #define SHT_NOBITS 8
69 #define SHF_WRITE 1
70 #define SHF_ALLOC 2
71 #define SHF_EXECINSTR 4
73 struct Section {
74 struct SAA *data;
75 uint32_t len, size, nrelocs;
76 int32_t index;
77 int type; /* SHT_PROGBITS or SHT_NOBITS */
78 int align; /* alignment: power of two */
79 uint32_t flags; /* section flags */
80 char *name;
81 struct SAA *rel;
82 int32_t rellen;
83 struct Reloc *head, **tail;
84 struct Symbol *gsyms; /* global symbols in section */
87 #define SECT_DELTA 32
88 static struct Section **sects;
89 static int nsects, sectlen;
91 #define SHSTR_DELTA 256
92 static char *shstrtab;
93 static int shstrtablen, shstrtabsize;
95 static struct SAA *syms;
96 static uint32_t nlocals, nglobs;
98 static int32_t def_seg;
100 static struct RAA *bsym;
102 static struct SAA *strs;
103 static uint32_t strslen;
105 static FILE *elffp;
106 static efunc error;
107 static evalfunc evaluate;
109 static struct Symbol *fwds;
111 static char elf_module[FILENAME_MAX];
113 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
114 static uint8_t elf_abiver = 0; /* Current ABI version */
116 extern struct ofmt of_elf32;
117 extern struct ofmt of_elf;
119 #define SHN_ABS 0xFFF1
120 #define SHN_COMMON 0xFFF2
121 #define SHN_UNDEF 0
123 #define SYM_GLOBAL 0x10
125 #define STT_NOTYPE 0 /* Symbol type is unspecified */
126 #define STT_OBJECT 1 /* Symbol is a data object */
127 #define STT_FUNC 2 /* Symbol is a code object */
128 #define STT_SECTION 3 /* Symbol associated with a section */
129 #define STT_FILE 4 /* Symbol's name is file name */
130 #define STT_COMMON 5 /* Symbol is a common data object */
131 #define STT_TLS 6 /* Symbol is thread-local data object*/
132 #define STT_NUM 7 /* Number of defined types. */
134 #define STV_DEFAULT 0
135 #define STV_INTERNAL 1
136 #define STV_HIDDEN 2
137 #define STV_PROTECTED 3
139 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
141 #define SEG_ALIGN 16 /* alignment of sections in file */
142 #define SEG_ALIGN_1 (SEG_ALIGN-1)
144 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
146 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
147 static struct ELF_SECTDATA {
148 void *data;
149 int32_t len;
150 bool is_saa;
151 } *elf_sects;
152 static int elf_nsect;
153 static int32_t elf_foffs;
155 static void elf_write(void);
156 static void elf_sect_write(struct Section *, const uint8_t *,
157 uint32_t);
158 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
159 int, int);
160 static void elf_write_sections(void);
161 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
162 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
163 static void add_sectname(char *, char *);
165 /* this stuff is needed for the stabs debugging format */
166 #define N_SO 0x64 /* ID for main source file */
167 #define N_SOL 0x84 /* ID for sub-source file */
168 #define N_BINCL 0x82
169 #define N_EINCL 0xA2
170 #define N_SLINE 0x44
171 #define TY_STABSSYMLIN 0x40 /* ouch */
173 struct stabentry {
174 uint32_t n_strx;
175 uint8_t n_type;
176 uint8_t n_other;
177 uint16_t n_desc;
178 uint32_t n_value;
181 struct erel {
182 int offset, info;
185 struct symlininfo {
186 int offset;
187 int section; /* section index */
188 char *name; /* shallow-copied pointer of section name */
191 struct linelist {
192 struct symlininfo info;
193 int line;
194 char *filename;
195 struct linelist *next;
196 struct linelist *last;
199 static struct linelist *stabslines = 0;
200 static int stabs_immcall = 0;
201 static int currentline = 0;
202 static int numlinestabs = 0;
203 static char *stabs_filename = 0;
204 static int symtabsection;
205 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
206 static int stablen, stabstrlen, stabrellen;
208 static struct dfmt df_stabs;
209 static struct Symbol *lastsym;
211 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
212 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
213 void stabs32_deflabel(char *, int32_t, int64_t, int, char *);
214 void stabs32_directive(const char *, const char *);
215 void stabs32_typevalue(int32_t);
216 void stabs32_output(int, void *);
217 void stabs32_generate(void);
218 void stabs32_cleanup(void);
220 /* end of stabs debugging stuff */
223 * Special section numbers which are used to define ELF special
224 * symbols, which can be used with WRT to provide PIC relocation
225 * types.
227 static int32_t elf_gotpc_sect, elf_gotoff_sect;
228 static int32_t elf_got_sect, elf_plt_sect;
229 static int32_t elf_sym_sect;
231 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
233 elffp = fp;
234 error = errfunc;
235 evaluate = eval;
236 (void)ldef; /* placate optimisers */
237 sects = NULL;
238 nsects = sectlen = 0;
239 syms = saa_init((int32_t)sizeof(struct Symbol));
240 nlocals = nglobs = 0;
241 bsym = raa_init();
242 strs = saa_init(1L);
243 saa_wbytes(strs, "\0", 1L);
244 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
245 strslen = 2 + strlen(elf_module);
246 shstrtab = NULL;
247 shstrtablen = shstrtabsize = 0;;
248 add_sectname("", "");
250 fwds = NULL;
252 elf_gotpc_sect = seg_alloc();
253 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
254 error);
255 elf_gotoff_sect = seg_alloc();
256 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
257 error);
258 elf_got_sect = seg_alloc();
259 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
260 error);
261 elf_plt_sect = seg_alloc();
262 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
263 error);
264 elf_sym_sect = seg_alloc();
265 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
266 error);
268 def_seg = seg_alloc();
271 static void elf_cleanup(int debuginfo)
273 struct Reloc *r;
274 int i;
276 (void)debuginfo;
278 elf_write();
279 fclose(elffp);
280 for (i = 0; i < nsects; i++) {
281 if (sects[i]->type != SHT_NOBITS)
282 saa_free(sects[i]->data);
283 if (sects[i]->head)
284 saa_free(sects[i]->rel);
285 while (sects[i]->head) {
286 r = sects[i]->head;
287 sects[i]->head = sects[i]->head->next;
288 nasm_free(r);
291 nasm_free(sects);
292 saa_free(syms);
293 raa_free(bsym);
294 saa_free(strs);
295 if (of_elf32.current_dfmt) {
296 of_elf32.current_dfmt->cleanup();
300 static void add_sectname(char *firsthalf, char *secondhalf)
302 int len = strlen(firsthalf) + strlen(secondhalf);
303 while (shstrtablen + len + 1 > shstrtabsize)
304 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
305 strcpy(shstrtab + shstrtablen, firsthalf);
306 strcat(shstrtab + shstrtablen, secondhalf);
307 shstrtablen += len + 1;
310 static int elf_make_section(char *name, int type, int flags, int align)
312 struct Section *s;
314 s = nasm_malloc(sizeof(*s));
316 if (type != SHT_NOBITS)
317 s->data = saa_init(1L);
318 s->head = NULL;
319 s->tail = &s->head;
320 s->len = s->size = 0;
321 s->nrelocs = 0;
322 if (!strcmp(name, ".text"))
323 s->index = def_seg;
324 else
325 s->index = seg_alloc();
326 add_sectname("", name);
327 s->name = nasm_malloc(1 + strlen(name));
328 strcpy(s->name, name);
329 s->type = type;
330 s->flags = flags;
331 s->align = align;
332 s->gsyms = NULL;
334 if (nsects >= sectlen)
335 sects =
336 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
337 sects[nsects++] = s;
339 return nsects - 1;
342 static int32_t elf_section_names(char *name, int pass, int *bits)
344 char *p;
345 unsigned flags_and, flags_or;
346 int type, align, i;
349 * Default is 32 bits.
351 if (!name) {
352 *bits = 32;
353 return def_seg;
356 p = name;
357 while (*p && !isspace(*p))
358 p++;
359 if (*p)
360 *p++ = '\0';
361 flags_and = flags_or = type = align = 0;
363 while (*p && isspace(*p))
364 p++;
365 while (*p) {
366 char *q = p;
367 while (*p && !isspace(*p))
368 p++;
369 if (*p)
370 *p++ = '\0';
371 while (*p && isspace(*p))
372 p++;
374 if (!nasm_strnicmp(q, "align=", 6)) {
375 align = atoi(q + 6);
376 if (align == 0)
377 align = 1;
378 if ((align - 1) & align) { /* means it's not a power of two */
379 error(ERR_NONFATAL, "section alignment %d is not"
380 " a power of two", align);
381 align = 1;
383 } else if (!nasm_stricmp(q, "alloc")) {
384 flags_and |= SHF_ALLOC;
385 flags_or |= SHF_ALLOC;
386 } else if (!nasm_stricmp(q, "noalloc")) {
387 flags_and |= SHF_ALLOC;
388 flags_or &= ~SHF_ALLOC;
389 } else if (!nasm_stricmp(q, "exec")) {
390 flags_and |= SHF_EXECINSTR;
391 flags_or |= SHF_EXECINSTR;
392 } else if (!nasm_stricmp(q, "noexec")) {
393 flags_and |= SHF_EXECINSTR;
394 flags_or &= ~SHF_EXECINSTR;
395 } else if (!nasm_stricmp(q, "write")) {
396 flags_and |= SHF_WRITE;
397 flags_or |= SHF_WRITE;
398 } else if (!nasm_stricmp(q, "nowrite")) {
399 flags_and |= SHF_WRITE;
400 flags_or &= ~SHF_WRITE;
401 } else if (!nasm_stricmp(q, "progbits")) {
402 type = SHT_PROGBITS;
403 } else if (!nasm_stricmp(q, "nobits")) {
404 type = SHT_NOBITS;
408 if (!strcmp(name, ".comment") ||
409 !strcmp(name, ".shstrtab") ||
410 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
411 error(ERR_NONFATAL, "attempt to redefine reserved section"
412 "name `%s'", name);
413 return NO_SEG;
416 for (i = 0; i < nsects; i++)
417 if (!strcmp(name, sects[i]->name))
418 break;
419 if (i == nsects) {
420 if (!strcmp(name, ".text"))
421 i = elf_make_section(name, SHT_PROGBITS,
422 SHF_ALLOC | SHF_EXECINSTR, 16);
423 else if (!strcmp(name, ".rodata"))
424 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
425 else if (!strcmp(name, ".data"))
426 i = elf_make_section(name, SHT_PROGBITS,
427 SHF_ALLOC | SHF_WRITE, 4);
428 else if (!strcmp(name, ".bss"))
429 i = elf_make_section(name, SHT_NOBITS,
430 SHF_ALLOC | SHF_WRITE, 4);
431 else
432 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
433 if (type)
434 sects[i]->type = type;
435 if (align)
436 sects[i]->align = align;
437 sects[i]->flags &= ~flags_and;
438 sects[i]->flags |= flags_or;
439 } else if (pass == 1) {
440 if ((type && sects[i]->type != type)
441 || (align && sects[i]->align != align)
442 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
443 error(ERR_WARNING, "section attributes ignored on"
444 " redeclaration of section `%s'", name);
447 return sects[i]->index;
450 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
451 int is_global, char *special)
453 int pos = strslen;
454 struct Symbol *sym;
455 bool special_used = false;
457 #if defined(DEBUG) && DEBUG>2
458 fprintf(stderr,
459 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
460 name, segment, offset, is_global, special);
461 #endif
462 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
464 * This is a NASM special symbol. We never allow it into
465 * the ELF symbol table, even if it's a valid one. If it
466 * _isn't_ a valid one, we should barf immediately.
468 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
469 strcmp(name, "..got") && strcmp(name, "..plt") &&
470 strcmp(name, "..sym"))
471 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
472 return;
475 if (is_global == 3) {
476 struct Symbol **s;
478 * Fix up a forward-reference symbol size from the first
479 * pass.
481 for (s = &fwds; *s; s = &(*s)->nextfwd)
482 if (!strcmp((*s)->name, name)) {
483 struct tokenval tokval;
484 expr *e;
485 char *p = special;
487 while (*p && !isspace(*p))
488 p++;
489 while (*p && isspace(*p))
490 p++;
491 stdscan_reset();
492 stdscan_bufptr = p;
493 tokval.t_type = TOKEN_INVALID;
494 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
495 if (e) {
496 if (!is_simple(e))
497 error(ERR_NONFATAL, "cannot use relocatable"
498 " expression as symbol size");
499 else
500 (*s)->size = reloc_value(e);
504 * Remove it from the list of unresolved sizes.
506 nasm_free((*s)->name);
507 *s = (*s)->nextfwd;
508 return;
510 return; /* it wasn't an important one */
513 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
514 strslen += 1 + strlen(name);
516 lastsym = sym = saa_wstruct(syms);
518 sym->strpos = pos;
519 sym->type = is_global ? SYM_GLOBAL : 0;
520 sym->other = STV_DEFAULT;
521 sym->size = 0;
522 if (segment == NO_SEG)
523 sym->section = SHN_ABS;
524 else {
525 int i;
526 sym->section = SHN_UNDEF;
527 if (nsects == 0 && segment == def_seg) {
528 int tempint;
529 if (segment != elf_section_names(".text", 2, &tempint))
530 error(ERR_PANIC,
531 "strange segment conditions in ELF driver");
532 sym->section = nsects;
533 } else {
534 for (i = 0; i < nsects; i++)
535 if (segment == sects[i]->index) {
536 sym->section = i + 1;
537 break;
542 if (is_global == 2) {
543 sym->size = offset;
544 sym->value = 0;
545 sym->section = SHN_COMMON;
547 * We have a common variable. Check the special text to see
548 * if it's a valid number and power of two; if so, store it
549 * as the alignment for the common variable.
551 if (special) {
552 bool err;
553 sym->value = readnum(special, &err);
554 if (err)
555 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
556 " valid number", special);
557 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
558 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
559 " power of two", special);
561 special_used = true;
562 } else
563 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
565 if (sym->type == SYM_GLOBAL) {
567 * If sym->section == SHN_ABS, then the first line of the
568 * else section would cause a core dump, because its a reference
569 * beyond the end of the section array.
570 * This behaviour is exhibited by this code:
571 * GLOBAL crash_nasm
572 * crash_nasm equ 0
573 * To avoid such a crash, such requests are silently discarded.
574 * This may not be the best solution.
576 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
577 bsym = raa_write(bsym, segment, nglobs);
578 } else if (sym->section != SHN_ABS) {
580 * This is a global symbol; so we must add it to the linked
581 * list of global symbols in its section. We'll push it on
582 * the beginning of the list, because it doesn't matter
583 * much which end we put it on and it's easier like this.
585 * In addition, we check the special text for symbol
586 * type and size information.
588 sym->next = sects[sym->section - 1]->gsyms;
589 sects[sym->section - 1]->gsyms = sym;
591 if (special) {
592 int n = strcspn(special, " \t");
594 if (!nasm_strnicmp(special, "function", n))
595 sym->type |= STT_FUNC;
596 else if (!nasm_strnicmp(special, "data", n) ||
597 !nasm_strnicmp(special, "object", n))
598 sym->type |= STT_OBJECT;
599 else if (!nasm_strnicmp(special, "notype", n))
600 sym->type |= STT_NOTYPE;
601 else
602 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
603 n, special);
604 special += n;
606 while (isspace(*special))
607 ++special;
608 if (*special) {
609 n = strcspn(special, " \t");
610 if (!nasm_strnicmp(special, "default", n))
611 sym->other = STV_DEFAULT;
612 else if (!nasm_strnicmp(special, "internal", n))
613 sym->other = STV_INTERNAL;
614 else if (!nasm_strnicmp(special, "hidden", n))
615 sym->other = STV_HIDDEN;
616 else if (!nasm_strnicmp(special, "protected", n))
617 sym->other = STV_PROTECTED;
618 else
619 n = 0;
620 special += n;
623 if (*special) {
624 struct tokenval tokval;
625 expr *e;
626 int fwd = 0;
627 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
629 while (special[n] && isspace(special[n]))
630 n++;
632 * We have a size expression; attempt to
633 * evaluate it.
635 stdscan_reset();
636 stdscan_bufptr = special + n;
637 tokval.t_type = TOKEN_INVALID;
638 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
639 NULL);
640 if (fwd) {
641 sym->nextfwd = fwds;
642 fwds = sym;
643 sym->name = nasm_strdup(name);
644 } else if (e) {
645 if (!is_simple(e))
646 error(ERR_NONFATAL, "cannot use relocatable"
647 " expression as symbol size");
648 else
649 sym->size = reloc_value(e);
651 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
653 special_used = true;
656 sym->globnum = nglobs;
657 nglobs++;
658 } else
659 nlocals++;
661 if (special && !special_used)
662 error(ERR_NONFATAL, "no special symbol features supported here");
665 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
667 struct Reloc *r;
669 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
670 sect->tail = &r->next;
671 r->next = NULL;
673 r->address = sect->len;
674 if (segment == NO_SEG)
675 r->symbol = 0;
676 else {
677 int i;
678 r->symbol = 0;
679 for (i = 0; i < nsects; i++)
680 if (segment == sects[i]->index)
681 r->symbol = i + 2;
682 if (!r->symbol)
683 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
685 r->type = type;
687 sect->nrelocs++;
691 * This routine deals with ..got and ..sym relocations: the more
692 * complicated kinds. In shared-library writing, some relocations
693 * with respect to global symbols must refer to the precise symbol
694 * rather than referring to an offset from the base of the section
695 * _containing_ the symbol. Such relocations call to this routine,
696 * which searches the symbol list for the symbol in question.
698 * R_386_GOT32 references require the _exact_ symbol address to be
699 * used; R_386_32 references can be at an offset from the symbol.
700 * The boolean argument `exact' tells us this.
702 * Return value is the adjusted value of `addr', having become an
703 * offset from the symbol rather than the section. Should always be
704 * zero when returning from an exact call.
706 * Limitation: if you define two symbols at the same place,
707 * confusion will occur.
709 * Inefficiency: we search, currently, using a linked list which
710 * isn't even necessarily sorted.
712 static int32_t elf_add_gsym_reloc(struct Section *sect,
713 int32_t segment, int32_t offset,
714 int type, bool exact)
716 struct Reloc *r;
717 struct Section *s;
718 struct Symbol *sym, *sm;
719 int i;
722 * First look up the segment/offset pair and find a global
723 * symbol corresponding to it. If it's not one of our segments,
724 * then it must be an external symbol, in which case we're fine
725 * doing a normal elf_add_reloc after first sanity-checking
726 * that the offset from the symbol is zero.
728 s = NULL;
729 for (i = 0; i < nsects; i++)
730 if (segment == sects[i]->index) {
731 s = sects[i];
732 break;
734 if (!s) {
735 if (exact && offset != 0)
736 error(ERR_NONFATAL, "unable to find a suitable global symbol"
737 " for this reference");
738 else
739 elf_add_reloc(sect, segment, type);
740 return offset;
743 if (exact) {
745 * Find a symbol pointing _exactly_ at this one.
747 for (sym = s->gsyms; sym; sym = sym->next)
748 if (sym->value == offset)
749 break;
750 } else {
752 * Find the nearest symbol below this one.
754 sym = NULL;
755 for (sm = s->gsyms; sm; sm = sm->next)
756 if (sm->value <= offset && (!sym || sm->value > sym->value))
757 sym = sm;
759 if (!sym && exact) {
760 error(ERR_NONFATAL, "unable to find a suitable global symbol"
761 " for this reference");
762 return 0;
765 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
766 sect->tail = &r->next;
767 r->next = NULL;
769 r->address = sect->len;
770 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
771 r->type = type;
773 sect->nrelocs++;
775 return offset - sym->value;
778 static void elf_out(int32_t segto, const void *data,
779 enum out_type type, uint64_t size,
780 int32_t segment, int32_t wrt)
782 struct Section *s;
783 int32_t addr;
784 uint8_t mydata[4], *p;
785 int i;
786 static struct symlininfo sinfo;
789 * handle absolute-assembly (structure definitions)
791 if (segto == NO_SEG) {
792 if (type != OUT_RESERVE)
793 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
794 " space");
795 return;
798 s = NULL;
799 for (i = 0; i < nsects; i++)
800 if (segto == sects[i]->index) {
801 s = sects[i];
802 break;
804 if (!s) {
805 int tempint; /* ignored */
806 if (segto != elf_section_names(".text", 2, &tempint))
807 error(ERR_PANIC, "strange segment conditions in ELF driver");
808 else {
809 s = sects[nsects - 1];
810 i = nsects - 1;
814 /* again some stabs debugging stuff */
815 if (of_elf32.current_dfmt) {
816 sinfo.offset = s->len;
817 sinfo.section = i;
818 sinfo.name = s->name;
819 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
821 /* end of debugging stuff */
823 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
824 error(ERR_WARNING, "attempt to initialize memory in"
825 " BSS section `%s': ignored", s->name);
826 if (type == OUT_REL2ADR)
827 size = 2;
828 else if (type == OUT_REL4ADR)
829 size = 4;
830 s->len += size;
831 return;
834 if (type == OUT_RESERVE) {
835 if (s->type == SHT_PROGBITS) {
836 error(ERR_WARNING, "uninitialized space declared in"
837 " non-BSS section `%s': zeroing", s->name);
838 elf_sect_write(s, NULL, size);
839 } else
840 s->len += size;
841 } else if (type == OUT_RAWDATA) {
842 if (segment != NO_SEG)
843 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
844 elf_sect_write(s, data, size);
845 } else if (type == OUT_ADDRESS) {
846 bool gnu16 = false;
847 addr = *(int64_t *)data;
848 if (segment != NO_SEG) {
849 if (segment % 2) {
850 error(ERR_NONFATAL, "ELF format does not support"
851 " segment base references");
852 } else {
853 if (wrt == NO_SEG) {
854 if (size == 2) {
855 gnu16 = true;
856 elf_add_reloc(s, segment, R_386_16);
857 } else {
858 elf_add_reloc(s, segment, R_386_32);
860 } else if (wrt == elf_gotpc_sect + 1) {
862 * The user will supply GOT relative to $$. ELF
863 * will let us have GOT relative to $. So we
864 * need to fix up the data item by $-$$.
866 addr += s->len;
867 elf_add_reloc(s, segment, R_386_GOTPC);
868 } else if (wrt == elf_gotoff_sect + 1) {
869 elf_add_reloc(s, segment, R_386_GOTOFF);
870 } else if (wrt == elf_got_sect + 1) {
871 addr = elf_add_gsym_reloc(s, segment, addr,
872 R_386_GOT32, true);
873 } else if (wrt == elf_sym_sect + 1) {
874 if (size == 2) {
875 gnu16 = true;
876 addr = elf_add_gsym_reloc(s, segment, addr,
877 R_386_16, false);
878 } else {
879 addr = elf_add_gsym_reloc(s, segment, addr,
880 R_386_32, false);
882 } else if (wrt == elf_plt_sect + 1) {
883 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
884 "relative PLT references");
885 } else {
886 error(ERR_NONFATAL, "ELF format does not support this"
887 " use of WRT");
888 wrt = NO_SEG; /* we can at least _try_ to continue */
892 p = mydata;
893 if (gnu16) {
894 error(ERR_WARNING | ERR_WARN_GNUELF,
895 "16-bit relocations in ELF is a GNU extension");
896 WRITESHORT(p, addr);
897 } else {
898 if (size != 4 && segment != NO_SEG) {
899 error(ERR_NONFATAL,
900 "Unsupported non-32-bit ELF relocation");
902 WRITELONG(p, addr);
904 elf_sect_write(s, mydata, size);
905 } else if (type == OUT_REL2ADR) {
906 if (segment == segto)
907 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
908 if (segment != NO_SEG && segment % 2) {
909 error(ERR_NONFATAL, "ELF format does not support"
910 " segment base references");
911 } else {
912 if (wrt == NO_SEG) {
913 error(ERR_WARNING | ERR_WARN_GNUELF,
914 "16-bit relocations in ELF is a GNU extension");
915 elf_add_reloc(s, segment, R_386_PC16);
916 } else {
917 error(ERR_NONFATAL,
918 "Unsupported non-32-bit ELF relocation");
921 p = mydata;
922 WRITESHORT(p, *(int64_t *)data - size);
923 elf_sect_write(s, mydata, 2L);
924 } else if (type == OUT_REL4ADR) {
925 if (segment == segto)
926 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
927 if (segment != NO_SEG && segment % 2) {
928 error(ERR_NONFATAL, "ELF format does not support"
929 " segment base references");
930 } else {
931 if (wrt == NO_SEG) {
932 elf_add_reloc(s, segment, R_386_PC32);
933 } else if (wrt == elf_plt_sect + 1) {
934 elf_add_reloc(s, segment, R_386_PLT32);
935 } else if (wrt == elf_gotpc_sect + 1 ||
936 wrt == elf_gotoff_sect + 1 ||
937 wrt == elf_got_sect + 1) {
938 error(ERR_NONFATAL, "ELF format cannot produce PC-"
939 "relative GOT references");
940 } else {
941 error(ERR_NONFATAL, "ELF format does not support this"
942 " use of WRT");
943 wrt = NO_SEG; /* we can at least _try_ to continue */
946 p = mydata;
947 WRITELONG(p, *(int64_t *)data - size);
948 elf_sect_write(s, mydata, 4L);
952 static void elf_write(void)
954 int nsections, align;
955 int scount;
956 char *p;
957 int commlen;
958 char comment[64];
959 int i;
961 struct SAA *symtab;
962 int32_t symtablen, symtablocal;
965 * Work out how many sections we will have. We have SHN_UNDEF,
966 * then the flexible user sections, then the four fixed
967 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
968 * then optionally relocation sections for the user sections.
970 if (of_elf32.current_dfmt == &df_stabs)
971 nsections = 8;
972 else
973 nsections = 5; /* SHN_UNDEF and the fixed ones */
975 add_sectname("", ".comment");
976 add_sectname("", ".shstrtab");
977 add_sectname("", ".symtab");
978 add_sectname("", ".strtab");
979 for (i = 0; i < nsects; i++) {
980 nsections++; /* for the section itself */
981 if (sects[i]->head) {
982 nsections++; /* for its relocations */
983 add_sectname(".rel", sects[i]->name);
987 if (of_elf32.current_dfmt == &df_stabs) {
988 /* in case the debug information is wanted, just add these three sections... */
989 add_sectname("", ".stab");
990 add_sectname("", ".stabstr");
991 add_sectname(".rel", ".stab");
995 * Do the comment.
997 *comment = '\0';
998 commlen =
999 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1002 * Output the ELF header.
1004 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1005 fputc(elf_osabi, elffp);
1006 fputc(elf_abiver, elffp);
1007 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1008 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1009 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1010 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1011 fwriteint32_t(0L, elffp); /* no entry point */
1012 fwriteint32_t(0L, elffp); /* no program header table */
1013 fwriteint32_t(0x40L, elffp); /* section headers straight after
1014 * ELF header plus alignment */
1015 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1016 fwriteint16_t(0x34, elffp); /* size of ELF header */
1017 fwriteint16_t(0, elffp); /* no program header table, again */
1018 fwriteint16_t(0, elffp); /* still no program header table */
1019 fwriteint16_t(0x28, elffp); /* size of section header */
1020 fwriteint16_t(nsections, elffp); /* number of sections */
1021 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1022 * section header table */
1023 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1024 fwriteint32_t(0L, elffp);
1025 fwriteint32_t(0L, elffp);
1028 * Build the symbol table and relocation tables.
1030 symtab = elf_build_symtab(&symtablen, &symtablocal);
1031 for (i = 0; i < nsects; i++)
1032 if (sects[i]->head)
1033 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1034 sects[i]->head);
1037 * Now output the section header table.
1040 elf_foffs = 0x40 + 0x28 * nsections;
1041 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1042 elf_foffs += align;
1043 elf_nsect = 0;
1044 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1046 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1047 scount = 1; /* needed for the stabs debugging to track the symtable section */
1048 p = shstrtab + 1;
1049 for (i = 0; i < nsects; i++) {
1050 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1051 (sects[i]->type == SHT_PROGBITS ?
1052 sects[i]->data : NULL), true,
1053 sects[i]->len, 0, 0, sects[i]->align, 0);
1054 p += strlen(p) + 1;
1055 scount++; /* dito */
1057 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1058 scount++; /* dito */
1059 p += strlen(p) + 1;
1060 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1061 scount++; /* dito */
1062 p += strlen(p) + 1;
1063 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1064 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1065 p += strlen(p) + 1;
1066 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1067 for (i = 0; i < nsects; i++)
1068 if (sects[i]->head) {
1069 p += strlen(p) + 1;
1070 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1071 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1073 if (of_elf32.current_dfmt == &df_stabs) {
1074 /* for debugging information, create the last three sections
1075 which are the .stab , .stabstr and .rel.stab sections respectively */
1077 /* this function call creates the stab sections in memory */
1078 stabs32_generate();
1080 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1081 p += strlen(p) + 1;
1082 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1083 nsections - 2, 0, 4, 12);
1085 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1087 stabstrlen, 0, 0, 4, 0);
1089 p += strlen(p) + 1;
1090 /* link -> symtable info -> section to refer to */
1091 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1092 stabrellen, symtabsection, nsections - 3, 4,
1096 fwrite(align_str, align, 1, elffp);
1099 * Now output the sections.
1101 elf_write_sections();
1103 nasm_free(elf_sects);
1104 saa_free(symtab);
1107 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1109 struct SAA *s = saa_init(1L);
1110 struct Symbol *sym;
1111 uint8_t entry[16], *p;
1112 int i;
1114 *len = *local = 0;
1117 * First, an all-zeros entry, required by the ELF spec.
1119 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1120 *len += 16;
1121 (*local)++;
1124 * Next, an entry for the file name.
1126 p = entry;
1127 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1128 WRITELONG(p, 0); /* no value */
1129 WRITELONG(p, 0); /* no size either */
1130 WRITESHORT(p, STT_FILE); /* type FILE */
1131 WRITESHORT(p, SHN_ABS);
1132 saa_wbytes(s, entry, 16L);
1133 *len += 16;
1134 (*local)++;
1137 * Now some standard symbols defining the segments, for relocation
1138 * purposes.
1140 for (i = 1; i <= nsects; i++) {
1141 p = entry;
1142 WRITELONG(p, 0); /* no symbol name */
1143 WRITELONG(p, 0); /* offset zero */
1144 WRITELONG(p, 0); /* size zero */
1145 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1146 WRITESHORT(p, i); /* section id */
1147 saa_wbytes(s, entry, 16L);
1148 *len += 16;
1149 (*local)++;
1153 * Now the other local symbols.
1155 saa_rewind(syms);
1156 while ((sym = saa_rstruct(syms))) {
1157 if (sym->type & SYM_GLOBAL)
1158 continue;
1159 p = entry;
1160 WRITELONG(p, sym->strpos);
1161 WRITELONG(p, sym->value);
1162 WRITELONG(p, sym->size);
1163 WRITECHAR(p, sym->type); /* type and binding */
1164 WRITECHAR(p, sym->other); /* visibility */
1165 WRITESHORT(p, sym->section);
1166 saa_wbytes(s, entry, 16L);
1167 *len += 16;
1168 (*local)++;
1172 * Now the global symbols.
1174 saa_rewind(syms);
1175 while ((sym = saa_rstruct(syms))) {
1176 if (!(sym->type & SYM_GLOBAL))
1177 continue;
1178 p = entry;
1179 WRITELONG(p, sym->strpos);
1180 WRITELONG(p, sym->value);
1181 WRITELONG(p, sym->size);
1182 WRITECHAR(p, sym->type); /* type and binding */
1183 WRITECHAR(p, sym->other); /* visibility */
1184 WRITESHORT(p, sym->section);
1185 saa_wbytes(s, entry, 16L);
1186 *len += 16;
1189 return s;
1192 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1194 struct SAA *s;
1195 uint8_t *p, entry[8];
1197 if (!r)
1198 return NULL;
1200 s = saa_init(1L);
1201 *len = 0;
1203 while (r) {
1204 int32_t sym = r->symbol;
1206 if (sym >= GLOBAL_TEMP_BASE)
1207 sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1209 p = entry;
1210 WRITELONG(p, r->address);
1211 WRITELONG(p, (sym << 8) + r->type);
1212 saa_wbytes(s, entry, 8L);
1213 *len += 8;
1215 r = r->next;
1218 return s;
1221 static void elf_section_header(int name, int type, int flags,
1222 void *data, bool is_saa, int32_t datalen,
1223 int link, int info, int align, int eltsize)
1225 elf_sects[elf_nsect].data = data;
1226 elf_sects[elf_nsect].len = datalen;
1227 elf_sects[elf_nsect].is_saa = is_saa;
1228 elf_nsect++;
1230 fwriteint32_t((int32_t)name, elffp);
1231 fwriteint32_t((int32_t)type, elffp);
1232 fwriteint32_t((int32_t)flags, elffp);
1233 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1234 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1235 fwriteint32_t(datalen, elffp);
1236 if (data)
1237 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1238 fwriteint32_t((int32_t)link, elffp);
1239 fwriteint32_t((int32_t)info, elffp);
1240 fwriteint32_t((int32_t)align, elffp);
1241 fwriteint32_t((int32_t)eltsize, elffp);
1244 static void elf_write_sections(void)
1246 int i;
1247 for (i = 0; i < elf_nsect; i++)
1248 if (elf_sects[i].data) {
1249 int32_t len = elf_sects[i].len;
1250 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1251 int32_t align = reallen - len;
1252 if (elf_sects[i].is_saa)
1253 saa_fpwrite(elf_sects[i].data, elffp);
1254 else
1255 fwrite(elf_sects[i].data, len, 1, elffp);
1256 fwrite(align_str, align, 1, elffp);
1260 static void elf_sect_write(struct Section *sect,
1261 const uint8_t *data, uint32_t len)
1263 saa_wbytes(sect->data, data, len);
1264 sect->len += len;
1267 static int32_t elf_segbase(int32_t segment)
1269 return segment;
1272 static int elf_directive(char *directive, char *value, int pass)
1274 bool err;
1275 int64_t n;
1276 char *p;
1278 if (!strcmp(directive, "osabi")) {
1279 if (pass == 2)
1280 return 1; /* ignore in pass 2 */
1282 n = readnum(value, &err);
1283 if (err) {
1284 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1285 return 1;
1287 if (n < 0 || n > 255) {
1288 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1289 return 1;
1291 elf_osabi = n;
1292 elf_abiver = 0;
1294 if ((p = strchr(value,',')) == NULL)
1295 return 1;
1297 n = readnum(p+1, &err);
1298 if (err || n < 0 || n > 255) {
1299 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1300 return 1;
1303 elf_abiver = n;
1304 return 1;
1307 return 0;
1310 static void elf_filename(char *inname, char *outname, efunc error)
1312 strcpy(elf_module, inname);
1313 standard_extension(inname, outname, ".o", error);
1316 static const char *elf_stdmac[] = {
1317 "%define __SECT__ [section .text]",
1318 "%macro __NASM_CDecl__ 1",
1319 "%define $_%1 $%1",
1320 "%endmacro",
1321 "%macro osabi 1+.nolist",
1322 "[osabi %1]",
1323 "%endmacro",
1324 NULL
1326 static int elf_set_info(enum geninfo type, char **val)
1328 (void)type;
1329 (void)val;
1330 return 0;
1333 static struct dfmt df_stabs = {
1334 "ELF32 (i386) stabs debug format for Linux",
1335 "stabs",
1336 stabs32_init,
1337 stabs32_linenum,
1338 stabs32_deflabel,
1339 stabs32_directive,
1340 stabs32_typevalue,
1341 stabs32_output,
1342 stabs32_cleanup
1345 struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
1347 struct ofmt of_elf32 = {
1348 "ELF32 (i386) object files (e.g. Linux)",
1349 "elf32",
1350 NULL,
1351 elf32_debugs_arr,
1352 &null_debug_form,
1353 elf_stdmac,
1354 elf_init,
1355 elf_set_info,
1356 elf_out,
1357 elf_deflabel,
1358 elf_section_names,
1359 elf_segbase,
1360 elf_directive,
1361 elf_filename,
1362 elf_cleanup
1365 struct ofmt of_elf = {
1366 "ELF (short name for ELF32) ",
1367 "elf",
1368 NULL,
1369 elf32_debugs_arr,
1370 &null_debug_form,
1371 elf_stdmac,
1372 elf_init,
1373 elf_set_info,
1374 elf_out,
1375 elf_deflabel,
1376 elf_section_names,
1377 elf_segbase,
1378 elf_directive,
1379 elf_filename,
1380 elf_cleanup
1382 /* again, the stabs debugging stuff (code) */
1384 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1386 (void)of;
1387 (void)id;
1388 (void)fp;
1389 (void)error;
1392 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1394 (void)segto;
1396 if (!stabs_filename) {
1397 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1398 strcpy(stabs_filename, filename);
1399 } else {
1400 if (strcmp(stabs_filename, filename)) {
1401 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1402 in fact, this leak comes in quite handy to maintain a list of files
1403 encountered so far in the symbol lines... */
1405 /* why not nasm_free(stabs_filename); we're done with the old one */
1407 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1408 strcpy(stabs_filename, filename);
1411 stabs_immcall = 1;
1412 currentline = linenumber;
1415 void stabs32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1416 char *special)
1418 (void)name;
1419 (void)segment;
1420 (void)offset;
1421 (void)is_global;
1422 (void)special;
1425 void stabs32_directive(const char *directive, const char *params)
1427 (void)directive;
1428 (void)params;
1431 void stabs32_typevalue(int32_t type)
1433 int32_t stype, ssize;
1434 switch (TYM_TYPE(type)) {
1435 case TY_LABEL:
1436 ssize = 0;
1437 stype = STT_NOTYPE;
1438 break;
1439 case TY_BYTE:
1440 ssize = 1;
1441 stype = STT_OBJECT;
1442 break;
1443 case TY_WORD:
1444 ssize = 2;
1445 stype = STT_OBJECT;
1446 break;
1447 case TY_DWORD:
1448 ssize = 4;
1449 stype = STT_OBJECT;
1450 break;
1451 case TY_FLOAT:
1452 ssize = 4;
1453 stype = STT_OBJECT;
1454 break;
1455 case TY_QWORD:
1456 ssize = 8;
1457 stype = STT_OBJECT;
1458 break;
1459 case TY_TBYTE:
1460 ssize = 10;
1461 stype = STT_OBJECT;
1462 break;
1463 case TY_OWORD:
1464 ssize = 8;
1465 stype = STT_OBJECT;
1466 break;
1467 case TY_COMMON:
1468 ssize = 0;
1469 stype = STT_COMMON;
1470 break;
1471 case TY_SEG:
1472 ssize = 0;
1473 stype = STT_SECTION;
1474 break;
1475 case TY_EXTERN:
1476 ssize = 0;
1477 stype = STT_NOTYPE;
1478 break;
1479 case TY_EQU:
1480 ssize = 0;
1481 stype = STT_NOTYPE;
1482 break;
1483 default:
1484 ssize = 0;
1485 stype = STT_NOTYPE;
1486 break;
1488 if (stype == STT_OBJECT && !lastsym->type) {
1489 lastsym->size = ssize;
1490 lastsym->type = stype;
1494 void stabs32_output(int type, void *param)
1496 struct symlininfo *s;
1497 struct linelist *el;
1498 if (type == TY_STABSSYMLIN) {
1499 if (stabs_immcall) {
1500 s = (struct symlininfo *)param;
1501 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1502 return; /* we are only interested in the text stuff */
1503 numlinestabs++;
1504 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1505 el->info.offset = s->offset;
1506 el->info.section = s->section;
1507 el->info.name = s->name;
1508 el->line = currentline;
1509 el->filename = stabs_filename;
1510 el->next = 0;
1511 if (stabslines) {
1512 stabslines->last->next = el;
1513 stabslines->last = el;
1514 } else {
1515 stabslines = el;
1516 stabslines->last = el;
1520 stabs_immcall = 0;
1523 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1524 do {\
1525 WRITELONG(p,n_strx); \
1526 WRITECHAR(p,n_type); \
1527 WRITECHAR(p,n_other); \
1528 WRITESHORT(p,n_desc); \
1529 WRITELONG(p,n_value); \
1530 } while (0)
1532 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1534 void stabs32_generate(void)
1536 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1537 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1538 char **allfiles;
1539 int *fileidx;
1541 struct linelist *ptr;
1543 ptr = stabslines;
1545 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1546 for (i = 0; i < numlinestabs; i++)
1547 allfiles[i] = 0;
1548 numfiles = 0;
1549 while (ptr) {
1550 if (numfiles == 0) {
1551 allfiles[0] = ptr->filename;
1552 numfiles++;
1553 } else {
1554 for (i = 0; i < numfiles; i++) {
1555 if (!strcmp(allfiles[i], ptr->filename))
1556 break;
1558 if (i >= numfiles) {
1559 allfiles[i] = ptr->filename;
1560 numfiles++;
1563 ptr = ptr->next;
1565 strsize = 1;
1566 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1567 for (i = 0; i < numfiles; i++) {
1568 fileidx[i] = strsize;
1569 strsize += strlen(allfiles[i]) + 1;
1571 mainfileindex = 0;
1572 for (i = 0; i < numfiles; i++) {
1573 if (!strcmp(allfiles[i], elf_module)) {
1574 mainfileindex = i;
1575 break;
1579 /* worst case size of the stab buffer would be:
1580 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1582 sbuf =
1583 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1584 sizeof(struct stabentry));
1586 ssbuf = (uint8_t *)nasm_malloc(strsize);
1588 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1589 rptr = rbuf;
1591 for (i = 0; i < numfiles; i++) {
1592 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1594 ssbuf[0] = 0;
1596 stabstrlen = strsize; /* set global variable for length of stab strings */
1598 sptr = sbuf;
1599 ptr = stabslines;
1600 numstabs = 0;
1602 if (ptr) {
1603 /* this is the first stab, its strx points to the filename of the
1604 the source-file, the n_desc field should be set to the number
1605 of remaining stabs
1607 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1609 /* this is the stab for the main source file */
1610 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1612 /* relocation table entry */
1614 /* Since the symbol table has two entries before */
1615 /* the section symbols, the index in the info.section */
1616 /* member must be adjusted by adding 2 */
1618 WRITELONG(rptr, (sptr - sbuf) - 4);
1619 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1621 numstabs++;
1622 currfile = mainfileindex;
1625 while (ptr) {
1626 if (strcmp(allfiles[currfile], ptr->filename)) {
1627 /* oops file has changed... */
1628 for (i = 0; i < numfiles; i++)
1629 if (!strcmp(allfiles[i], ptr->filename))
1630 break;
1631 currfile = i;
1632 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1633 ptr->info.offset);
1634 numstabs++;
1636 /* relocation table entry */
1637 WRITELONG(rptr, (sptr - sbuf) - 4);
1638 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1641 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1642 numstabs++;
1644 /* relocation table entry */
1646 WRITELONG(rptr, (sptr - sbuf) - 4);
1647 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1649 ptr = ptr->next;
1653 ((struct stabentry *)sbuf)->n_desc = numstabs;
1655 nasm_free(allfiles);
1656 nasm_free(fileidx);
1658 stablen = (sptr - sbuf);
1659 stabrellen = (rptr - rbuf);
1660 stabrelbuf = rbuf;
1661 stabbuf = sbuf;
1662 stabstrbuf = ssbuf;
1665 void stabs32_cleanup(void)
1667 struct linelist *ptr, *del;
1668 if (!stabslines)
1669 return;
1670 ptr = stabslines;
1671 while (ptr) {
1672 del = ptr;
1673 ptr = ptr->next;
1674 nasm_free(del);
1676 if (stabbuf)
1677 nasm_free(stabbuf);
1678 if (stabrelbuf)
1679 nasm_free(stabrelbuf);
1680 if (stabstrbuf)
1681 nasm_free(stabstrbuf);
1684 #endif /* OF_ELF */