NASM 0.99.05
[nasm/avx512.git] / output / outelf32.c
blob6466b1eb675d5a01270a5f0661e0f3aa498e86db
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; /* ELF symbol info thingy */
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 extern struct ofmt of_elf32;
114 extern struct ofmt of_elf;
116 #define SHN_ABS 0xFFF1
117 #define SHN_COMMON 0xFFF2
118 #define SHN_UNDEF 0
120 #define SYM_SECTION 0x04
121 #define SYM_GLOBAL 0x10
122 #define SYM_NOTYPE 0x00
123 #define SYM_DATA 0x01
124 #define SYM_FUNCTION 0x02
126 #define STV_DEFAULT 0
127 #define STV_INTERNAL 1
128 #define STV_HIDDEN 2
129 #define STV_PROTECTED 3
131 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
133 #define SEG_ALIGN 16 /* alignment of sections in file */
134 #define SEG_ALIGN_1 (SEG_ALIGN-1)
136 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
138 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
139 static struct ELF_SECTDATA {
140 void *data;
141 int32_t len;
142 bool is_saa;
143 } *elf_sects;
144 static int elf_nsect;
145 static int32_t elf_foffs;
147 static void elf_write(void);
148 static void elf_sect_write(struct Section *, const uint8_t *,
149 uint32_t);
150 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
151 int, int);
152 static void elf_write_sections(void);
153 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
154 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
155 static void add_sectname(char *, char *);
157 /* this stuff is needed for the stabs debugging format */
158 #define N_SO 0x64 /* ID for main source file */
159 #define N_SOL 0x84 /* ID for sub-source file */
160 #define N_BINCL 0x82
161 #define N_EINCL 0xA2
162 #define N_SLINE 0x44
163 #define TY_STABSSYMLIN 0x40 /* ouch */
165 struct stabentry {
166 uint32_t n_strx;
167 uint8_t n_type;
168 uint8_t n_other;
169 uint16_t n_desc;
170 uint32_t n_value;
173 struct erel {
174 int offset, info;
177 struct symlininfo {
178 int offset;
179 int section; /* section index */
180 char *name; /* shallow-copied pointer of section name */
183 struct linelist {
184 struct symlininfo info;
185 int line;
186 char *filename;
187 struct linelist *next;
188 struct linelist *last;
191 static struct linelist *stabslines = 0;
192 static int stabs_immcall = 0;
193 static int currentline = 0;
194 static int numlinestabs = 0;
195 static char *stabs_filename = 0;
196 static int symtabsection;
197 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
198 static int stablen, stabstrlen, stabrellen;
200 static struct dfmt df_stabs;
202 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
203 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
204 void stabs32_deflabel(char *, int32_t, int32_t, int, char *);
205 void stabs32_directive(const char *, const char *);
206 void stabs32_typevalue(int32_t);
207 void stabs32_output(int, void *);
208 void stabs32_generate(void);
209 void stabs32_cleanup(void);
211 /* end of stabs debugging stuff */
214 * Special section numbers which are used to define ELF special
215 * symbols, which can be used with WRT to provide PIC relocation
216 * types.
218 static int32_t elf_gotpc_sect, elf_gotoff_sect;
219 static int32_t elf_got_sect, elf_plt_sect;
220 static int32_t elf_sym_sect;
222 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
224 elffp = fp;
225 error = errfunc;
226 evaluate = eval;
227 (void)ldef; /* placate optimisers */
228 sects = NULL;
229 nsects = sectlen = 0;
230 syms = saa_init((int32_t)sizeof(struct Symbol));
231 nlocals = nglobs = 0;
232 bsym = raa_init();
233 strs = saa_init(1L);
234 saa_wbytes(strs, "\0", 1L);
235 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
236 strslen = 2 + strlen(elf_module);
237 shstrtab = NULL;
238 shstrtablen = shstrtabsize = 0;;
239 add_sectname("", "");
241 fwds = NULL;
243 elf_gotpc_sect = seg_alloc();
244 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
245 error);
246 elf_gotoff_sect = seg_alloc();
247 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
248 error);
249 elf_got_sect = seg_alloc();
250 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
251 error);
252 elf_plt_sect = seg_alloc();
253 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
254 error);
255 elf_sym_sect = seg_alloc();
256 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
257 error);
259 def_seg = seg_alloc();
262 static void elf_cleanup(int debuginfo)
264 struct Reloc *r;
265 int i;
267 (void)debuginfo;
269 elf_write();
270 fclose(elffp);
271 for (i = 0; i < nsects; i++) {
272 if (sects[i]->type != SHT_NOBITS)
273 saa_free(sects[i]->data);
274 if (sects[i]->head)
275 saa_free(sects[i]->rel);
276 while (sects[i]->head) {
277 r = sects[i]->head;
278 sects[i]->head = sects[i]->head->next;
279 nasm_free(r);
282 nasm_free(sects);
283 saa_free(syms);
284 raa_free(bsym);
285 saa_free(strs);
286 if (of_elf32.current_dfmt) {
287 of_elf32.current_dfmt->cleanup();
291 static void add_sectname(char *firsthalf, char *secondhalf)
293 int len = strlen(firsthalf) + strlen(secondhalf);
294 while (shstrtablen + len + 1 > shstrtabsize)
295 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
296 strcpy(shstrtab + shstrtablen, firsthalf);
297 strcat(shstrtab + shstrtablen, secondhalf);
298 shstrtablen += len + 1;
301 static int elf_make_section(char *name, int type, int flags, int align)
303 struct Section *s;
305 s = nasm_malloc(sizeof(*s));
307 if (type != SHT_NOBITS)
308 s->data = saa_init(1L);
309 s->head = NULL;
310 s->tail = &s->head;
311 s->len = s->size = 0;
312 s->nrelocs = 0;
313 if (!strcmp(name, ".text"))
314 s->index = def_seg;
315 else
316 s->index = seg_alloc();
317 add_sectname("", name);
318 s->name = nasm_malloc(1 + strlen(name));
319 strcpy(s->name, name);
320 s->type = type;
321 s->flags = flags;
322 s->align = align;
323 s->gsyms = NULL;
325 if (nsects >= sectlen)
326 sects =
327 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
328 sects[nsects++] = s;
330 return nsects - 1;
333 static int32_t elf_section_names(char *name, int pass, int *bits)
335 char *p;
336 int flags_and, flags_or, type, align, i;
339 * Default is 32 bits.
341 if (!name) {
342 *bits = 32;
343 return def_seg;
346 p = name;
347 while (*p && !isspace(*p))
348 p++;
349 if (*p)
350 *p++ = '\0';
351 flags_and = flags_or = type = align = 0;
353 while (*p && isspace(*p))
354 p++;
355 while (*p) {
356 char *q = p;
357 while (*p && !isspace(*p))
358 p++;
359 if (*p)
360 *p++ = '\0';
361 while (*p && isspace(*p))
362 p++;
364 if (!nasm_strnicmp(q, "align=", 6)) {
365 align = atoi(q + 6);
366 if (align == 0)
367 align = 1;
368 if ((align - 1) & align) { /* means it's not a power of two */
369 error(ERR_NONFATAL, "section alignment %d is not"
370 " a power of two", align);
371 align = 1;
373 } else if (!nasm_stricmp(q, "alloc")) {
374 flags_and |= SHF_ALLOC;
375 flags_or |= SHF_ALLOC;
376 } else if (!nasm_stricmp(q, "noalloc")) {
377 flags_and |= SHF_ALLOC;
378 flags_or &= ~SHF_ALLOC;
379 } else if (!nasm_stricmp(q, "exec")) {
380 flags_and |= SHF_EXECINSTR;
381 flags_or |= SHF_EXECINSTR;
382 } else if (!nasm_stricmp(q, "noexec")) {
383 flags_and |= SHF_EXECINSTR;
384 flags_or &= ~SHF_EXECINSTR;
385 } else if (!nasm_stricmp(q, "write")) {
386 flags_and |= SHF_WRITE;
387 flags_or |= SHF_WRITE;
388 } else if (!nasm_stricmp(q, "nowrite")) {
389 flags_and |= SHF_WRITE;
390 flags_or &= ~SHF_WRITE;
391 } else if (!nasm_stricmp(q, "progbits")) {
392 type = SHT_PROGBITS;
393 } else if (!nasm_stricmp(q, "nobits")) {
394 type = SHT_NOBITS;
398 if (!strcmp(name, ".comment") ||
399 !strcmp(name, ".shstrtab") ||
400 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
401 error(ERR_NONFATAL, "attempt to redefine reserved section"
402 "name `%s'", name);
403 return NO_SEG;
406 for (i = 0; i < nsects; i++)
407 if (!strcmp(name, sects[i]->name))
408 break;
409 if (i == nsects) {
410 if (!strcmp(name, ".text"))
411 i = elf_make_section(name, SHT_PROGBITS,
412 SHF_ALLOC | SHF_EXECINSTR, 16);
413 else if (!strcmp(name, ".rodata"))
414 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
415 else if (!strcmp(name, ".data"))
416 i = elf_make_section(name, SHT_PROGBITS,
417 SHF_ALLOC | SHF_WRITE, 4);
418 else if (!strcmp(name, ".bss"))
419 i = elf_make_section(name, SHT_NOBITS,
420 SHF_ALLOC | SHF_WRITE, 4);
421 else
422 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
423 if (type)
424 sects[i]->type = type;
425 if (align)
426 sects[i]->align = align;
427 sects[i]->flags &= ~flags_and;
428 sects[i]->flags |= flags_or;
429 } else if (pass == 1) {
430 if (type || align || flags_and)
431 error(ERR_WARNING, "section attributes ignored on"
432 " redeclaration of section `%s'", name);
435 return sects[i]->index;
438 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
439 int is_global, char *special)
441 int pos = strslen;
442 struct Symbol *sym;
443 bool special_used = false;
445 #if defined(DEBUG) && DEBUG>2
446 fprintf(stderr,
447 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
448 name, segment, offset, is_global, special);
449 #endif
450 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
452 * This is a NASM special symbol. We never allow it into
453 * the ELF symbol table, even if it's a valid one. If it
454 * _isn't_ a valid one, we should barf immediately.
456 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
457 strcmp(name, "..got") && strcmp(name, "..plt") &&
458 strcmp(name, "..sym"))
459 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
460 return;
463 if (is_global == 3) {
464 struct Symbol **s;
466 * Fix up a forward-reference symbol size from the first
467 * pass.
469 for (s = &fwds; *s; s = &(*s)->nextfwd)
470 if (!strcmp((*s)->name, name)) {
471 struct tokenval tokval;
472 expr *e;
473 char *p = special;
475 while (*p && !isspace(*p))
476 p++;
477 while (*p && isspace(*p))
478 p++;
479 stdscan_reset();
480 stdscan_bufptr = p;
481 tokval.t_type = TOKEN_INVALID;
482 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
483 if (e) {
484 if (!is_simple(e))
485 error(ERR_NONFATAL, "cannot use relocatable"
486 " expression as symbol size");
487 else
488 (*s)->size = reloc_value(e);
492 * Remove it from the list of unresolved sizes.
494 nasm_free((*s)->name);
495 *s = (*s)->nextfwd;
496 return;
498 return; /* it wasn't an important one */
501 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
502 strslen += 1 + strlen(name);
504 sym = saa_wstruct(syms);
506 sym->strpos = pos;
507 sym->type = is_global ? SYM_GLOBAL : 0;
508 sym->other = STV_DEFAULT;
509 sym->size = 0;
510 if (segment == NO_SEG)
511 sym->section = SHN_ABS;
512 else {
513 int i;
514 sym->section = SHN_UNDEF;
515 if (nsects == 0 && segment == def_seg) {
516 int tempint;
517 if (segment != elf_section_names(".text", 2, &tempint))
518 error(ERR_PANIC,
519 "strange segment conditions in ELF driver");
520 sym->section = nsects;
521 } else {
522 for (i = 0; i < nsects; i++)
523 if (segment == sects[i]->index) {
524 sym->section = i + 1;
525 break;
530 if (is_global == 2) {
531 sym->size = offset;
532 sym->value = 0;
533 sym->section = SHN_COMMON;
535 * We have a common variable. Check the special text to see
536 * if it's a valid number and power of two; if so, store it
537 * as the alignment for the common variable.
539 if (special) {
540 bool err;
541 sym->value = readnum(special, &err);
542 if (err)
543 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
544 " valid number", special);
545 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
546 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
547 " power of two", special);
549 special_used = true;
550 } else
551 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
553 if (sym->type == SYM_GLOBAL) {
555 * If sym->section == SHN_ABS, then the first line of the
556 * else section would cause a core dump, because its a reference
557 * beyond the end of the section array.
558 * This behaviour is exhibited by this code:
559 * GLOBAL crash_nasm
560 * crash_nasm equ 0
561 * To avoid such a crash, such requests are silently discarded.
562 * This may not be the best solution.
564 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
565 bsym = raa_write(bsym, segment, nglobs);
566 } else if (sym->section != SHN_ABS) {
568 * This is a global symbol; so we must add it to the linked
569 * list of global symbols in its section. We'll push it on
570 * the beginning of the list, because it doesn't matter
571 * much which end we put it on and it's easier like this.
573 * In addition, we check the special text for symbol
574 * type and size information.
576 sym->next = sects[sym->section - 1]->gsyms;
577 sects[sym->section - 1]->gsyms = sym;
579 if (special) {
580 int n = strcspn(special, " \t");
582 if (!nasm_strnicmp(special, "function", n))
583 sym->type |= SYM_FUNCTION;
584 else if (!nasm_strnicmp(special, "data", n) ||
585 !nasm_strnicmp(special, "object", n))
586 sym->type |= SYM_DATA;
587 else if (!nasm_strnicmp(special, "notype", n))
588 sym->type |= SYM_NOTYPE;
589 else
590 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
591 n, special);
592 special += n;
594 while (isspace(*special))
595 ++special;
596 if (*special) {
597 n = strcspn(special, " \t");
598 if (!nasm_strnicmp(special, "default", n))
599 sym->other = STV_DEFAULT;
600 else if (!nasm_strnicmp(special, "internal", n))
601 sym->other = STV_INTERNAL;
602 else if (!nasm_strnicmp(special, "hidden", n))
603 sym->other = STV_HIDDEN;
604 else if (!nasm_strnicmp(special, "protected", n))
605 sym->other = STV_PROTECTED;
606 else
607 n = 0;
608 special += n;
611 if (*special) {
612 struct tokenval tokval;
613 expr *e;
614 int fwd = 0;
615 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
617 while (special[n] && isspace(special[n]))
618 n++;
620 * We have a size expression; attempt to
621 * evaluate it.
623 stdscan_reset();
624 stdscan_bufptr = special + n;
625 tokval.t_type = TOKEN_INVALID;
626 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
627 NULL);
628 if (fwd) {
629 sym->nextfwd = fwds;
630 fwds = sym;
631 sym->name = nasm_strdup(name);
632 } else if (e) {
633 if (!is_simple(e))
634 error(ERR_NONFATAL, "cannot use relocatable"
635 " expression as symbol size");
636 else
637 sym->size = reloc_value(e);
639 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
641 special_used = true;
644 sym->globnum = nglobs;
645 nglobs++;
646 } else
647 nlocals++;
649 if (special && !special_used)
650 error(ERR_NONFATAL, "no special symbol features supported here");
653 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
655 struct Reloc *r;
657 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
658 sect->tail = &r->next;
659 r->next = NULL;
661 r->address = sect->len;
662 if (segment == NO_SEG)
663 r->symbol = 2;
664 else {
665 int i;
666 r->symbol = 0;
667 for (i = 0; i < nsects; i++)
668 if (segment == sects[i]->index)
669 r->symbol = i + 3;
670 if (!r->symbol)
671 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
673 r->type = type;
675 sect->nrelocs++;
679 * This routine deals with ..got and ..sym relocations: the more
680 * complicated kinds. In shared-library writing, some relocations
681 * with respect to global symbols must refer to the precise symbol
682 * rather than referring to an offset from the base of the section
683 * _containing_ the symbol. Such relocations call to this routine,
684 * which searches the symbol list for the symbol in question.
686 * R_386_GOT32 references require the _exact_ symbol address to be
687 * used; R_386_32 references can be at an offset from the symbol.
688 * The boolean argument `exact' tells us this.
690 * Return value is the adjusted value of `addr', having become an
691 * offset from the symbol rather than the section. Should always be
692 * zero when returning from an exact call.
694 * Limitation: if you define two symbols at the same place,
695 * confusion will occur.
697 * Inefficiency: we search, currently, using a linked list which
698 * isn't even necessarily sorted.
700 static int32_t elf_add_gsym_reloc(struct Section *sect,
701 int32_t segment, int32_t offset,
702 int type, bool exact)
704 struct Reloc *r;
705 struct Section *s;
706 struct Symbol *sym, *sm;
707 int i;
710 * First look up the segment/offset pair and find a global
711 * symbol corresponding to it. If it's not one of our segments,
712 * then it must be an external symbol, in which case we're fine
713 * doing a normal elf_add_reloc after first sanity-checking
714 * that the offset from the symbol is zero.
716 s = NULL;
717 for (i = 0; i < nsects; i++)
718 if (segment == sects[i]->index) {
719 s = sects[i];
720 break;
722 if (!s) {
723 if (exact && offset != 0)
724 error(ERR_NONFATAL, "unable to find a suitable global symbol"
725 " for this reference");
726 else
727 elf_add_reloc(sect, segment, type);
728 return offset;
731 if (exact) {
733 * Find a symbol pointing _exactly_ at this one.
735 for (sym = s->gsyms; sym; sym = sym->next)
736 if (sym->value == offset)
737 break;
738 } else {
740 * Find the nearest symbol below this one.
742 sym = NULL;
743 for (sm = s->gsyms; sm; sm = sm->next)
744 if (sm->value <= offset && (!sym || sm->value > sym->value))
745 sym = sm;
747 if (!sym && exact) {
748 error(ERR_NONFATAL, "unable to find a suitable global symbol"
749 " for this reference");
750 return 0;
753 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
754 sect->tail = &r->next;
755 r->next = NULL;
757 r->address = sect->len;
758 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
759 r->type = type;
761 sect->nrelocs++;
763 return offset - sym->value;
766 static void elf_out(int32_t segto, const void *data, uint32_t type,
767 int32_t segment, int32_t wrt)
769 struct Section *s;
770 int32_t realbytes = type & OUT_SIZMASK;
771 int32_t addr;
772 uint8_t mydata[4], *p;
773 int i;
774 static struct symlininfo sinfo;
776 type &= OUT_TYPMASK;
779 * handle absolute-assembly (structure definitions)
781 if (segto == NO_SEG) {
782 if (type != OUT_RESERVE)
783 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
784 " space");
785 return;
788 s = NULL;
789 for (i = 0; i < nsects; i++)
790 if (segto == sects[i]->index) {
791 s = sects[i];
792 break;
794 if (!s) {
795 int tempint; /* ignored */
796 if (segto != elf_section_names(".text", 2, &tempint))
797 error(ERR_PANIC, "strange segment conditions in ELF driver");
798 else {
799 s = sects[nsects - 1];
800 i = nsects - 1;
804 /* again some stabs debugging stuff */
805 if (of_elf32.current_dfmt) {
806 sinfo.offset = s->len;
807 sinfo.section = i;
808 sinfo.name = s->name;
809 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
811 /* end of debugging stuff */
813 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
814 error(ERR_WARNING, "attempt to initialize memory in"
815 " BSS section `%s': ignored", s->name);
816 if (type == OUT_REL2ADR)
817 realbytes = 2;
818 else if (type == OUT_REL4ADR)
819 realbytes = 4;
820 s->len += realbytes;
821 return;
824 if (type == OUT_RESERVE) {
825 if (s->type == SHT_PROGBITS) {
826 error(ERR_WARNING, "uninitialized space declared in"
827 " non-BSS section `%s': zeroing", s->name);
828 elf_sect_write(s, NULL, realbytes);
829 } else
830 s->len += realbytes;
831 } else if (type == OUT_RAWDATA) {
832 if (segment != NO_SEG)
833 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
834 elf_sect_write(s, data, realbytes);
835 } else if (type == OUT_ADDRESS) {
836 bool gnu16 = false;
837 addr = *(int32_t *)data;
838 if (segment != NO_SEG) {
839 if (segment % 2) {
840 error(ERR_NONFATAL, "ELF format does not support"
841 " segment base references");
842 } else {
843 if (wrt == NO_SEG) {
844 if (realbytes == 2) {
845 gnu16 = true;
846 elf_add_reloc(s, segment, R_386_16);
847 } else {
848 elf_add_reloc(s, segment, R_386_32);
850 } else if (wrt == elf_gotpc_sect + 1) {
852 * The user will supply GOT relative to $$. ELF
853 * will let us have GOT relative to $. So we
854 * need to fix up the data item by $-$$.
856 addr += s->len;
857 elf_add_reloc(s, segment, R_386_GOTPC);
858 } else if (wrt == elf_gotoff_sect + 1) {
859 elf_add_reloc(s, segment, R_386_GOTOFF);
860 } else if (wrt == elf_got_sect + 1) {
861 addr = elf_add_gsym_reloc(s, segment, addr,
862 R_386_GOT32, true);
863 } else if (wrt == elf_sym_sect + 1) {
864 if (realbytes == 2) {
865 gnu16 = true;
866 addr = elf_add_gsym_reloc(s, segment, addr,
867 R_386_16, false);
868 } else {
869 addr = elf_add_gsym_reloc(s, segment, addr,
870 R_386_32, false);
872 } else if (wrt == elf_plt_sect + 1) {
873 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
874 "relative PLT references");
875 } else {
876 error(ERR_NONFATAL, "ELF format does not support this"
877 " use of WRT");
878 wrt = NO_SEG; /* we can at least _try_ to continue */
882 p = mydata;
883 if (gnu16) {
884 error(ERR_WARNING | ERR_WARN_GNUELF,
885 "16-bit relocations in ELF is a GNU extension");
886 WRITESHORT(p, addr);
887 } else {
888 if (realbytes != 4 && segment != NO_SEG) {
889 error(ERR_NONFATAL,
890 "Unsupported non-32-bit ELF relocation");
892 WRITELONG(p, addr);
894 elf_sect_write(s, mydata, realbytes);
895 } else if (type == OUT_REL2ADR) {
896 if (segment == segto)
897 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
898 if (segment != NO_SEG && segment % 2) {
899 error(ERR_NONFATAL, "ELF format does not support"
900 " segment base references");
901 } else {
902 if (wrt == NO_SEG) {
903 error(ERR_WARNING | ERR_WARN_GNUELF,
904 "16-bit relocations in ELF is a GNU extension");
905 elf_add_reloc(s, segment, R_386_PC16);
906 } else {
907 error(ERR_NONFATAL,
908 "Unsupported non-32-bit ELF relocation");
911 p = mydata;
912 WRITESHORT(p, *(int32_t *)data - realbytes);
913 elf_sect_write(s, mydata, 2L);
914 } else if (type == OUT_REL4ADR) {
915 if (segment == segto)
916 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
917 if (segment != NO_SEG && segment % 2) {
918 error(ERR_NONFATAL, "ELF format does not support"
919 " segment base references");
920 } else {
921 if (wrt == NO_SEG) {
922 elf_add_reloc(s, segment, R_386_PC32);
923 } else if (wrt == elf_plt_sect + 1) {
924 elf_add_reloc(s, segment, R_386_PLT32);
925 } else if (wrt == elf_gotpc_sect + 1 ||
926 wrt == elf_gotoff_sect + 1 ||
927 wrt == elf_got_sect + 1) {
928 error(ERR_NONFATAL, "ELF format cannot produce PC-"
929 "relative GOT references");
930 } else {
931 error(ERR_NONFATAL, "ELF format does not support this"
932 " use of WRT");
933 wrt = NO_SEG; /* we can at least _try_ to continue */
936 p = mydata;
937 WRITELONG(p, *(int32_t *)data - realbytes);
938 elf_sect_write(s, mydata, 4L);
942 static void elf_write(void)
944 int nsections, align;
945 int scount;
946 char *p;
947 int commlen;
948 char comment[64];
949 int i;
951 struct SAA *symtab;
952 int32_t symtablen, symtablocal;
955 * Work out how many sections we will have. We have SHN_UNDEF,
956 * then the flexible user sections, then the four fixed
957 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
958 * then optionally relocation sections for the user sections.
960 if (of_elf32.current_dfmt == &df_stabs)
961 nsections = 8;
962 else
963 nsections = 5; /* SHN_UNDEF and the fixed ones */
965 add_sectname("", ".comment");
966 add_sectname("", ".shstrtab");
967 add_sectname("", ".symtab");
968 add_sectname("", ".strtab");
969 for (i = 0; i < nsects; i++) {
970 nsections++; /* for the section itself */
971 if (sects[i]->head) {
972 nsections++; /* for its relocations */
973 add_sectname(".rel", sects[i]->name);
977 if (of_elf32.current_dfmt == &df_stabs) {
978 /* in case the debug information is wanted, just add these three sections... */
979 add_sectname("", ".stab");
980 add_sectname("", ".stabstr");
981 add_sectname(".rel", ".stab");
985 * Do the comment.
987 *comment = '\0';
988 commlen =
989 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
992 * Output the ELF header.
994 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
995 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
996 fwriteint16_t(3, elffp); /* EM_386 processor ID */
997 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
998 fwriteint32_t(0L, elffp); /* no entry point */
999 fwriteint32_t(0L, elffp); /* no program header table */
1000 fwriteint32_t(0x40L, elffp); /* section headers straight after
1001 * ELF header plus alignment */
1002 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1003 fwriteint16_t(0x34, elffp); /* size of ELF header */
1004 fwriteint16_t(0, elffp); /* no program header table, again */
1005 fwriteint16_t(0, elffp); /* still no program header table */
1006 fwriteint16_t(0x28, elffp); /* size of section header */
1007 fwriteint16_t(nsections, elffp); /* number of sections */
1008 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1009 * section header table */
1010 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1011 fwriteint32_t(0L, elffp);
1012 fwriteint32_t(0L, elffp);
1015 * Build the symbol table and relocation tables.
1017 symtab = elf_build_symtab(&symtablen, &symtablocal);
1018 for (i = 0; i < nsects; i++)
1019 if (sects[i]->head)
1020 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1021 sects[i]->head);
1024 * Now output the section header table.
1027 elf_foffs = 0x40 + 0x28 * nsections;
1028 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1029 elf_foffs += align;
1030 elf_nsect = 0;
1031 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1033 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1034 scount = 1; /* needed for the stabs debugging to track the symtable section */
1035 p = shstrtab + 1;
1036 for (i = 0; i < nsects; i++) {
1037 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1038 (sects[i]->type == SHT_PROGBITS ?
1039 sects[i]->data : NULL), true,
1040 sects[i]->len, 0, 0, sects[i]->align, 0);
1041 p += strlen(p) + 1;
1042 scount++; /* dito */
1044 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1045 scount++; /* dito */
1046 p += strlen(p) + 1;
1047 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1048 scount++; /* dito */
1049 p += strlen(p) + 1;
1050 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1051 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1052 p += strlen(p) + 1;
1053 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1054 for (i = 0; i < nsects; i++)
1055 if (sects[i]->head) {
1056 p += strlen(p) + 1;
1057 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1058 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1060 if (of_elf32.current_dfmt == &df_stabs) {
1061 /* for debugging information, create the last three sections
1062 which are the .stab , .stabstr and .rel.stab sections respectively */
1064 /* this function call creates the stab sections in memory */
1065 stabs32_generate();
1067 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1068 p += strlen(p) + 1;
1069 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1070 nsections - 2, 0, 4, 12);
1072 p += strlen(p) + 1;
1073 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1074 stabstrlen, 0, 0, 4, 0);
1076 p += strlen(p) + 1;
1077 /* link -> symtable info -> section to refer to */
1078 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1079 stabrellen, symtabsection, nsections - 3, 4,
1083 fwrite(align_str, align, 1, elffp);
1086 * Now output the sections.
1088 elf_write_sections();
1090 nasm_free(elf_sects);
1091 saa_free(symtab);
1094 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1096 struct SAA *s = saa_init(1L);
1097 struct Symbol *sym;
1098 uint8_t entry[16], *p;
1099 int i;
1101 *len = *local = 0;
1104 * First, an all-zeros entry, required by the ELF spec.
1106 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1107 *len += 16;
1108 (*local)++;
1111 * Next, an entry for the file name.
1113 p = entry;
1114 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1115 WRITELONG(p, 0); /* no value */
1116 WRITELONG(p, 0); /* no size either */
1117 WRITESHORT(p, 4); /* type FILE */
1118 WRITESHORT(p, SHN_ABS);
1119 saa_wbytes(s, entry, 16L);
1120 *len += 16;
1121 (*local)++;
1124 * Now some standard symbols defining the segments, for relocation
1125 * purposes.
1127 for (i = 1; i <= nsects + 1; i++) {
1128 p = entry;
1129 WRITELONG(p, 0); /* no symbol name */
1130 WRITELONG(p, 0); /* offset zero */
1131 WRITELONG(p, 0); /* size zero */
1132 WRITESHORT(p, 3); /* local section-type thing */
1133 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1134 saa_wbytes(s, entry, 16L);
1135 *len += 16;
1136 (*local)++;
1140 * Now the other local symbols.
1142 saa_rewind(syms);
1143 while ((sym = saa_rstruct(syms))) {
1144 if (sym->type & SYM_GLOBAL)
1145 continue;
1146 p = entry;
1147 WRITELONG(p, sym->strpos);
1148 WRITELONG(p, sym->value);
1149 WRITELONG(p, sym->size);
1150 WRITECHAR(p, sym->type); /* local non-typed thing */
1151 WRITECHAR(p, sym->other);
1152 WRITESHORT(p, sym->section);
1153 saa_wbytes(s, entry, 16L);
1154 *len += 16;
1155 (*local)++;
1159 * Now the global symbols.
1161 saa_rewind(syms);
1162 while ((sym = saa_rstruct(syms))) {
1163 if (!(sym->type & SYM_GLOBAL))
1164 continue;
1165 p = entry;
1166 WRITELONG(p, sym->strpos);
1167 WRITELONG(p, sym->value);
1168 WRITELONG(p, sym->size);
1169 WRITECHAR(p, sym->type); /* global non-typed thing */
1170 WRITECHAR(p, sym->other);
1171 WRITESHORT(p, sym->section);
1172 saa_wbytes(s, entry, 16L);
1173 *len += 16;
1176 return s;
1179 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1181 struct SAA *s;
1182 uint8_t *p, entry[8];
1184 if (!r)
1185 return NULL;
1187 s = saa_init(1L);
1188 *len = 0;
1190 while (r) {
1191 int32_t sym = r->symbol;
1193 if (sym >= GLOBAL_TEMP_BASE)
1194 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1196 p = entry;
1197 WRITELONG(p, r->address);
1198 WRITELONG(p, (sym << 8) + r->type);
1199 saa_wbytes(s, entry, 8L);
1200 *len += 8;
1202 r = r->next;
1205 return s;
1208 static void elf_section_header(int name, int type, int flags,
1209 void *data, bool is_saa, int32_t datalen,
1210 int link, int info, int align, int eltsize)
1212 elf_sects[elf_nsect].data = data;
1213 elf_sects[elf_nsect].len = datalen;
1214 elf_sects[elf_nsect].is_saa = is_saa;
1215 elf_nsect++;
1217 fwriteint32_t((int32_t)name, elffp);
1218 fwriteint32_t((int32_t)type, elffp);
1219 fwriteint32_t((int32_t)flags, elffp);
1220 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1221 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1222 fwriteint32_t(datalen, elffp);
1223 if (data)
1224 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1225 fwriteint32_t((int32_t)link, elffp);
1226 fwriteint32_t((int32_t)info, elffp);
1227 fwriteint32_t((int32_t)align, elffp);
1228 fwriteint32_t((int32_t)eltsize, elffp);
1231 static void elf_write_sections(void)
1233 int i;
1234 for (i = 0; i < elf_nsect; i++)
1235 if (elf_sects[i].data) {
1236 int32_t len = elf_sects[i].len;
1237 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1238 int32_t align = reallen - len;
1239 if (elf_sects[i].is_saa)
1240 saa_fpwrite(elf_sects[i].data, elffp);
1241 else
1242 fwrite(elf_sects[i].data, len, 1, elffp);
1243 fwrite(align_str, align, 1, elffp);
1247 static void elf_sect_write(struct Section *sect,
1248 const uint8_t *data, uint32_t len)
1250 saa_wbytes(sect->data, data, len);
1251 sect->len += len;
1254 static int32_t elf_segbase(int32_t segment)
1256 return segment;
1259 static int elf_directive(char *directive, char *value, int pass)
1261 (void)directive;
1262 (void)value;
1263 (void)pass;
1264 return 0;
1267 static void elf_filename(char *inname, char *outname, efunc error)
1269 strcpy(elf_module, inname);
1270 standard_extension(inname, outname, ".o", error);
1273 static const char *elf_stdmac[] = {
1274 "%define __SECT__ [section .text]",
1275 "%macro __NASM_CDecl__ 1",
1276 "%define $_%1 $%1",
1277 "%endmacro",
1278 NULL
1280 static int elf_set_info(enum geninfo type, char **val)
1282 (void)type;
1283 (void)val;
1284 return 0;
1287 static struct dfmt df_stabs = {
1288 "ELF32 (i386) stabs debug format for Linux",
1289 "stabs",
1290 stabs32_init,
1291 stabs32_linenum,
1292 stabs32_deflabel,
1293 stabs32_directive,
1294 stabs32_typevalue,
1295 stabs32_output,
1296 stabs32_cleanup
1299 struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
1301 struct ofmt of_elf32 = {
1302 "ELF32 (i386) object files (e.g. Linux)",
1303 "elf32",
1304 NULL,
1305 elf32_debugs_arr,
1306 &null_debug_form,
1307 elf_stdmac,
1308 elf_init,
1309 elf_set_info,
1310 elf_out,
1311 elf_deflabel,
1312 elf_section_names,
1313 elf_segbase,
1314 elf_directive,
1315 elf_filename,
1316 elf_cleanup
1319 struct ofmt of_elf = {
1320 "ELF (short name for ELF32) ",
1321 "elf",
1322 NULL,
1323 elf32_debugs_arr,
1324 &null_debug_form,
1325 elf_stdmac,
1326 elf_init,
1327 elf_set_info,
1328 elf_out,
1329 elf_deflabel,
1330 elf_section_names,
1331 elf_segbase,
1332 elf_directive,
1333 elf_filename,
1334 elf_cleanup
1336 /* again, the stabs debugging stuff (code) */
1338 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1340 (void)of;
1341 (void)id;
1342 (void)fp;
1343 (void)error;
1346 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1348 (void)segto;
1350 if (!stabs_filename) {
1351 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1352 strcpy(stabs_filename, filename);
1353 } else {
1354 if (strcmp(stabs_filename, filename)) {
1355 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1356 in fact, this leak comes in quite handy to maintain a list of files
1357 encountered so far in the symbol lines... */
1359 /* why not nasm_free(stabs_filename); we're done with the old one */
1361 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1362 strcpy(stabs_filename, filename);
1365 stabs_immcall = 1;
1366 currentline = linenumber;
1369 void stabs32_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1370 char *special)
1372 (void)name;
1373 (void)segment;
1374 (void)offset;
1375 (void)is_global;
1376 (void)special;
1379 void stabs32_directive(const char *directive, const char *params)
1381 (void)directive;
1382 (void)params;
1385 void stabs32_typevalue(int32_t type)
1387 (void)type;
1390 void stabs32_output(int type, void *param)
1392 struct symlininfo *s;
1393 struct linelist *el;
1394 if (type == TY_STABSSYMLIN) {
1395 if (stabs_immcall) {
1396 s = (struct symlininfo *)param;
1397 if (strcmp(s->name, ".text"))
1398 return; /* we are only interested in the text stuff */
1399 numlinestabs++;
1400 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1401 el->info.offset = s->offset;
1402 el->info.section = s->section;
1403 el->info.name = s->name;
1404 el->line = currentline;
1405 el->filename = stabs_filename;
1406 el->next = 0;
1407 if (stabslines) {
1408 stabslines->last->next = el;
1409 stabslines->last = el;
1410 } else {
1411 stabslines = el;
1412 stabslines->last = el;
1416 stabs_immcall = 0;
1419 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1420 do {\
1421 WRITELONG(p,n_strx); \
1422 WRITECHAR(p,n_type); \
1423 WRITECHAR(p,n_other); \
1424 WRITESHORT(p,n_desc); \
1425 WRITELONG(p,n_value); \
1426 } while (0)
1428 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1430 void stabs32_generate(void)
1432 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1433 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1434 char **allfiles;
1435 int *fileidx;
1437 struct linelist *ptr;
1439 ptr = stabslines;
1441 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1442 for (i = 0; i < numlinestabs; i++)
1443 allfiles[i] = 0;
1444 numfiles = 0;
1445 while (ptr) {
1446 if (numfiles == 0) {
1447 allfiles[0] = ptr->filename;
1448 numfiles++;
1449 } else {
1450 for (i = 0; i < numfiles; i++) {
1451 if (!strcmp(allfiles[i], ptr->filename))
1452 break;
1454 if (i >= numfiles) {
1455 allfiles[i] = ptr->filename;
1456 numfiles++;
1459 ptr = ptr->next;
1461 strsize = 1;
1462 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1463 for (i = 0; i < numfiles; i++) {
1464 fileidx[i] = strsize;
1465 strsize += strlen(allfiles[i]) + 1;
1467 mainfileindex = 0;
1468 for (i = 0; i < numfiles; i++) {
1469 if (!strcmp(allfiles[i], elf_module)) {
1470 mainfileindex = i;
1471 break;
1475 /* worst case size of the stab buffer would be:
1476 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1478 sbuf =
1479 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1480 sizeof(struct stabentry));
1482 ssbuf = (uint8_t *)nasm_malloc(strsize);
1484 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1485 rptr = rbuf;
1487 for (i = 0; i < numfiles; i++) {
1488 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1490 ssbuf[0] = 0;
1492 stabstrlen = strsize; /* set global variable for length of stab strings */
1494 sptr = sbuf;
1495 /* this is the first stab, its strx points to the filename of the
1496 the source-file, the n_desc field should be set to the number
1497 of remaining stabs
1499 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1501 ptr = stabslines;
1502 numstabs = 0;
1504 if (ptr) {
1505 /* this is the stab for the main source file */
1506 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1508 /* relocation table entry */
1510 /* Since the above WRITE_STAB calls have already */
1511 /* created two entries, the index in the info.section */
1512 /* member must be adjusted by adding 3 */
1514 WRITELONG(rptr, (sptr - sbuf) - 4);
1515 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1517 numstabs++;
1518 currfile = mainfileindex;
1521 while (ptr) {
1522 if (strcmp(allfiles[currfile], ptr->filename)) {
1523 /* oops file has changed... */
1524 for (i = 0; i < numfiles; i++)
1525 if (!strcmp(allfiles[i], ptr->filename))
1526 break;
1527 currfile = i;
1528 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1529 ptr->info.offset);
1530 numstabs++;
1532 /* relocation table entry */
1533 WRITELONG(rptr, (sptr - sbuf) - 4);
1534 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1537 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1538 numstabs++;
1540 /* relocation table entry */
1542 WRITELONG(rptr, (sptr - sbuf) - 4);
1543 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1545 ptr = ptr->next;
1549 ((struct stabentry *)sbuf)->n_desc = numstabs;
1551 nasm_free(allfiles);
1552 nasm_free(fileidx);
1554 stablen = (sptr - sbuf);
1555 stabrellen = (rptr - rbuf);
1556 stabrelbuf = rbuf;
1557 stabbuf = sbuf;
1558 stabstrbuf = ssbuf;
1561 void stabs32_cleanup(void)
1563 struct linelist *ptr, *del;
1564 if (!stabslines)
1565 return;
1566 ptr = stabslines;
1567 while (ptr) {
1568 del = ptr;
1569 ptr = ptr->next;
1570 nasm_free(del);
1572 if (stabbuf)
1573 nasm_free(stabbuf);
1574 if (stabrelbuf)
1575 nasm_free(stabrelbuf);
1576 if (stabstrbuf)
1577 nasm_free(stabstrbuf);
1580 #endif /* OF_ELF */