Apply Nindent to all .c and .h files
[nasm/avx512.git] / output / outelf.c
blob35d0e343e02e485cfd56d77d2da600b386cc899f
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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "outform.h"
19 #ifdef OF_ELF
22 * Relocation types.
24 enum reloc_type {
25 R_386_32 = 1, /* ordinary absolute relocation */
26 R_386_PC32 = 2, /* PC-relative relocation */
27 R_386_GOT32 = 3, /* an offset into GOT */
28 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
29 R_386_COPY = 5, /* ??? */
30 R_386_GLOB_DAT = 6, /* ??? */
31 R_386_JUMP_SLOT = 7, /* ??? */
32 R_386_RELATIVE = 8, /* ??? */
33 R_386_GOTOFF = 9, /* an offset from GOT base */
34 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
35 /* These are GNU extensions, but useful */
36 R_386_16 = 20, /* A 16-bit absolute relocation */
37 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
38 R_386_8 = 22, /* An 8-bit absolute relocation */
39 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
42 struct Reloc {
43 struct Reloc *next;
44 long address; /* relative to _start_ of section */
45 long symbol; /* ELF symbol info thingy */
46 int type; /* type of relocation */
49 struct Symbol {
50 long strpos; /* string table position of name */
51 long section; /* section ID of the symbol */
52 int type; /* symbol type */
53 long value; /* address, or COMMON variable align */
54 long size; /* size of symbol */
55 long globnum; /* symbol table offset if global */
56 struct Symbol *next; /* list of globals in each section */
57 struct Symbol *nextfwd; /* list of unresolved-size symbols */
58 char *name; /* used temporarily if in above list */
61 #define SHT_PROGBITS 1
62 #define SHT_NOBITS 8
64 #define SHF_WRITE 1
65 #define SHF_ALLOC 2
66 #define SHF_EXECINSTR 4
68 struct Section {
69 struct SAA *data;
70 unsigned long len, size, nrelocs;
71 long index;
72 int type; /* SHT_PROGBITS or SHT_NOBITS */
73 int align; /* alignment: power of two */
74 unsigned long flags; /* section flags */
75 char *name;
76 struct SAA *rel;
77 long rellen;
78 struct Reloc *head, **tail;
79 struct Symbol *gsyms; /* global symbols in section */
82 #define SECT_DELTA 32
83 static struct Section **sects;
84 static int nsects, sectlen;
86 #define SHSTR_DELTA 256
87 static char *shstrtab;
88 static int shstrtablen, shstrtabsize;
90 static struct SAA *syms;
91 static unsigned long nlocals, nglobs;
93 static long def_seg;
95 static struct RAA *bsym;
97 static struct SAA *strs;
98 static unsigned long strslen;
100 static FILE *elffp;
101 static efunc error;
102 static evalfunc evaluate;
104 static struct Symbol *fwds;
106 static char elf_module[FILENAME_MAX];
108 extern struct ofmt of_elf;
110 #define SHN_ABS 0xFFF1
111 #define SHN_COMMON 0xFFF2
112 #define SHN_UNDEF 0
114 #define SYM_SECTION 0x04
115 #define SYM_GLOBAL 0x10
116 #define SYM_DATA 0x01
117 #define SYM_FUNCTION 0x02
119 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
121 #define SEG_ALIGN 16 /* alignment of sections in file */
122 #define SEG_ALIGN_1 (SEG_ALIGN-1)
124 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
126 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
127 static struct ELF_SECTDATA {
128 void *data;
129 long len;
130 int is_saa;
131 } *elf_sects;
132 static int elf_nsect;
133 static long elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const unsigned char *,
137 unsigned long);
138 static void elf_section_header(int, int, int, void *, int, long, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(long *, long *);
142 static struct SAA *elf_build_reltab(long *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 /* this stuff is needed for the stabs debugging format */
146 #define N_SO 0x64 /* ID for main source file */
147 #define N_SOL 0x84 /* ID for sub-source file */
148 #define N_BINCL 0x82
149 #define N_EINCL 0xA2
150 #define N_SLINE 0x44
151 #define TY_STABSSYMLIN 0x40 /* ouch */
153 struct stabentry {
154 unsigned long n_strx;
155 unsigned char n_type;
156 unsigned char n_other;
157 unsigned short n_desc;
158 unsigned long n_value;
161 struct erel {
162 int offset, info;
165 struct symlininfo {
166 int offset;
167 int section; /* section index */
168 char *name; /* shallow-copied pointer of section name */
171 struct linelist {
172 struct symlininfo info;
173 int line;
174 char *filename;
175 struct linelist *next;
176 struct linelist *last;
179 static struct linelist *stabslines = 0;
180 static int stabs_immcall = 0;
181 static int currentline = 0;
182 static int numlinestabs = 0;
183 static char *stabs_filename = 0;
184 static int symtabsection;
185 static unsigned char *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
186 static int stablen, stabstrlen, stabrellen;
188 static struct dfmt df_stabs;
190 void stabs_init(struct ofmt *, void *, FILE *, efunc);
191 void stabs_linenum(const char *filename, long linenumber, long);
192 void stabs_deflabel(char *, long, long, int, char *);
193 void stabs_directive(const char *, const char *);
194 void stabs_typevalue(long);
195 void stabs_output(int, void *);
196 void stabs_generate();
197 void stabs_cleanup();
199 /* end of stabs debugging stuff */
202 * Special section numbers which are used to define ELF special
203 * symbols, which can be used with WRT to provide PIC relocation
204 * types.
206 static long elf_gotpc_sect, elf_gotoff_sect;
207 static long elf_got_sect, elf_plt_sect;
208 static long elf_sym_sect;
210 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
212 elffp = fp;
213 error = errfunc;
214 evaluate = eval;
215 (void)ldef; /* placate optimisers */
216 sects = NULL;
217 nsects = sectlen = 0;
218 syms = saa_init((long)sizeof(struct Symbol));
219 nlocals = nglobs = 0;
220 bsym = raa_init();
221 strs = saa_init(1L);
222 saa_wbytes(strs, "\0", 1L);
223 saa_wbytes(strs, elf_module, (long)(strlen(elf_module) + 1));
224 strslen = 2 + strlen(elf_module);
225 shstrtab = NULL;
226 shstrtablen = shstrtabsize = 0;;
227 add_sectname("", "");
229 fwds = NULL;
231 elf_gotpc_sect = seg_alloc();
232 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf,
233 error);
234 elf_gotoff_sect = seg_alloc();
235 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf,
236 error);
237 elf_got_sect = seg_alloc();
238 ldef("..got", elf_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf,
239 error);
240 elf_plt_sect = seg_alloc();
241 ldef("..plt", elf_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf,
242 error);
243 elf_sym_sect = seg_alloc();
244 ldef("..sym", elf_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf,
245 error);
247 def_seg = seg_alloc();
250 static void elf_cleanup(int debuginfo)
252 struct Reloc *r;
253 int i;
255 (void)debuginfo;
257 elf_write();
258 fclose(elffp);
259 for (i = 0; i < nsects; i++) {
260 if (sects[i]->type != SHT_NOBITS)
261 saa_free(sects[i]->data);
262 if (sects[i]->head)
263 saa_free(sects[i]->rel);
264 while (sects[i]->head) {
265 r = sects[i]->head;
266 sects[i]->head = sects[i]->head->next;
267 nasm_free(r);
270 nasm_free(sects);
271 saa_free(syms);
272 raa_free(bsym);
273 saa_free(strs);
274 if (of_elf.current_dfmt) {
275 of_elf.current_dfmt->cleanup();
279 static void add_sectname(char *firsthalf, char *secondhalf)
281 int len = strlen(firsthalf) + strlen(secondhalf);
282 while (shstrtablen + len + 1 > shstrtabsize)
283 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
284 strcpy(shstrtab + shstrtablen, firsthalf);
285 strcat(shstrtab + shstrtablen, secondhalf);
286 shstrtablen += len + 1;
289 static int elf_make_section(char *name, int type, int flags, int align)
291 struct Section *s;
293 s = nasm_malloc(sizeof(*s));
295 if (type != SHT_NOBITS)
296 s->data = saa_init(1L);
297 s->head = NULL;
298 s->tail = &s->head;
299 s->len = s->size = 0;
300 s->nrelocs = 0;
301 if (!strcmp(name, ".text"))
302 s->index = def_seg;
303 else
304 s->index = seg_alloc();
305 add_sectname("", name);
306 s->name = nasm_malloc(1 + strlen(name));
307 strcpy(s->name, name);
308 s->type = type;
309 s->flags = flags;
310 s->align = align;
311 s->gsyms = NULL;
313 if (nsects >= sectlen)
314 sects =
315 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
316 sects[nsects++] = s;
318 return nsects - 1;
321 static long elf_section_names(char *name, int pass, int *bits)
323 char *p;
324 int flags_and, flags_or, type, align, i;
327 * Default is 32 bits.
329 if (!name) {
330 *bits = 32;
331 return def_seg;
334 p = name;
335 while (*p && !isspace(*p))
336 p++;
337 if (*p)
338 *p++ = '\0';
339 flags_and = flags_or = type = align = 0;
341 while (*p && isspace(*p))
342 p++;
343 while (*p) {
344 char *q = p;
345 while (*p && !isspace(*p))
346 p++;
347 if (*p)
348 *p++ = '\0';
349 while (*p && isspace(*p))
350 p++;
352 if (!nasm_strnicmp(q, "align=", 6)) {
353 align = atoi(q + 6);
354 if (align == 0)
355 align = 1;
356 if ((align - 1) & align) { /* means it's not a power of two */
357 error(ERR_NONFATAL, "section alignment %d is not"
358 " a power of two", align);
359 align = 1;
361 } else if (!nasm_stricmp(q, "alloc")) {
362 flags_and |= SHF_ALLOC;
363 flags_or |= SHF_ALLOC;
364 } else if (!nasm_stricmp(q, "noalloc")) {
365 flags_and |= SHF_ALLOC;
366 flags_or &= ~SHF_ALLOC;
367 } else if (!nasm_stricmp(q, "exec")) {
368 flags_and |= SHF_EXECINSTR;
369 flags_or |= SHF_EXECINSTR;
370 } else if (!nasm_stricmp(q, "noexec")) {
371 flags_and |= SHF_EXECINSTR;
372 flags_or &= ~SHF_EXECINSTR;
373 } else if (!nasm_stricmp(q, "write")) {
374 flags_and |= SHF_WRITE;
375 flags_or |= SHF_WRITE;
376 } else if (!nasm_stricmp(q, "nowrite")) {
377 flags_and |= SHF_WRITE;
378 flags_or &= ~SHF_WRITE;
379 } else if (!nasm_stricmp(q, "progbits")) {
380 type = SHT_PROGBITS;
381 } else if (!nasm_stricmp(q, "nobits")) {
382 type = SHT_NOBITS;
386 if (!strcmp(name, ".comment") ||
387 !strcmp(name, ".shstrtab") ||
388 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
389 error(ERR_NONFATAL, "attempt to redefine reserved section"
390 "name `%s'", name);
391 return NO_SEG;
394 for (i = 0; i < nsects; i++)
395 if (!strcmp(name, sects[i]->name))
396 break;
397 if (i == nsects) {
398 if (!strcmp(name, ".text"))
399 i = elf_make_section(name, SHT_PROGBITS,
400 SHF_ALLOC | SHF_EXECINSTR, 16);
401 else if (!strcmp(name, ".rodata"))
402 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
403 else if (!strcmp(name, ".data"))
404 i = elf_make_section(name, SHT_PROGBITS,
405 SHF_ALLOC | SHF_WRITE, 4);
406 else if (!strcmp(name, ".bss"))
407 i = elf_make_section(name, SHT_NOBITS,
408 SHF_ALLOC | SHF_WRITE, 4);
409 else
410 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
411 if (type)
412 sects[i]->type = type;
413 if (align)
414 sects[i]->align = align;
415 sects[i]->flags &= ~flags_and;
416 sects[i]->flags |= flags_or;
417 } else if (pass == 1) {
418 if (type || align || flags_and)
419 error(ERR_WARNING, "section attributes ignored on"
420 " redeclaration of section `%s'", name);
423 return sects[i]->index;
426 static void elf_deflabel(char *name, long segment, long offset,
427 int is_global, char *special)
429 int pos = strslen;
430 struct Symbol *sym;
431 int special_used = FALSE;
433 #if defined(DEBUG) && DEBUG>2
434 fprintf(stderr,
435 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
436 name, segment, offset, is_global, special);
437 #endif
438 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
440 * This is a NASM special symbol. We never allow it into
441 * the ELF symbol table, even if it's a valid one. If it
442 * _isn't_ a valid one, we should barf immediately.
444 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
445 strcmp(name, "..got") && strcmp(name, "..plt") &&
446 strcmp(name, "..sym"))
447 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
448 return;
451 if (is_global == 3) {
452 struct Symbol **s;
454 * Fix up a forward-reference symbol size from the first
455 * pass.
457 for (s = &fwds; *s; s = &(*s)->nextfwd)
458 if (!strcmp((*s)->name, name)) {
459 struct tokenval tokval;
460 expr *e;
461 char *p = special;
463 while (*p && !isspace(*p))
464 p++;
465 while (*p && isspace(*p))
466 p++;
467 stdscan_reset();
468 stdscan_bufptr = p;
469 tokval.t_type = TOKEN_INVALID;
470 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
471 if (e) {
472 if (!is_simple(e))
473 error(ERR_NONFATAL, "cannot use relocatable"
474 " expression as symbol size");
475 else
476 (*s)->size = reloc_value(e);
480 * Remove it from the list of unresolved sizes.
482 nasm_free((*s)->name);
483 *s = (*s)->nextfwd;
484 return;
486 return; /* it wasn't an important one */
489 saa_wbytes(strs, name, (long)(1 + strlen(name)));
490 strslen += 1 + strlen(name);
492 sym = saa_wstruct(syms);
494 sym->strpos = pos;
495 sym->type = is_global ? SYM_GLOBAL : 0;
496 sym->size = 0;
497 if (segment == NO_SEG)
498 sym->section = SHN_ABS;
499 else {
500 int i;
501 sym->section = SHN_UNDEF;
502 if (nsects == 0 && segment == def_seg) {
503 int tempint;
504 if (segment != elf_section_names(".text", 2, &tempint))
505 error(ERR_PANIC,
506 "strange segment conditions in ELF driver");
507 sym->section = nsects;
508 } else {
509 for (i = 0; i < nsects; i++)
510 if (segment == sects[i]->index) {
511 sym->section = i + 1;
512 break;
517 if (is_global == 2) {
518 sym->size = offset;
519 sym->value = 0;
520 sym->section = SHN_COMMON;
522 * We have a common variable. Check the special text to see
523 * if it's a valid number and power of two; if so, store it
524 * as the alignment for the common variable.
526 if (special) {
527 int err;
528 sym->value = readnum(special, &err);
529 if (err)
530 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
531 " valid number", special);
532 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
533 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
534 " power of two", special);
536 special_used = TRUE;
537 } else
538 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
540 if (sym->type == SYM_GLOBAL) {
542 * There's a problem here that needs fixing.
543 * If sym->section == SHN_ABS, then the first line of the
544 * else section causes a core dump, because its a reference
545 * beyond the end of the section array.
546 * This behaviour is exhibited by this code:
547 * GLOBAL crash_nasm
548 * crash_nasm equ 0
550 * I'm not sure how to procede, because I haven't got the
551 * first clue about how ELF works, so I don't know what to
552 * do with it. Furthermore, I'm not sure what the rest of this
553 * section of code does. Help?
555 * For now, I'll see if doing absolutely nothing with it will
556 * work...
558 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
559 bsym = raa_write(bsym, segment, nglobs);
560 } else if (sym->section != SHN_ABS) {
562 * This is a global symbol; so we must add it to the linked
563 * list of global symbols in its section. We'll push it on
564 * the beginning of the list, because it doesn't matter
565 * much which end we put it on and it's easier like this.
567 * In addition, we check the special text for symbol
568 * type and size information.
570 sym->next = sects[sym->section - 1]->gsyms;
571 sects[sym->section - 1]->gsyms = sym;
573 if (special) {
574 int n = strcspn(special, " ");
576 if (!nasm_strnicmp(special, "function", n))
577 sym->type |= SYM_FUNCTION;
578 else if (!nasm_strnicmp(special, "data", n) ||
579 !nasm_strnicmp(special, "object", n))
580 sym->type |= SYM_DATA;
581 else
582 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
583 n, special);
584 if (special[n]) {
585 struct tokenval tokval;
586 expr *e;
587 int fwd = FALSE;
588 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
590 while (special[n] && isspace(special[n]))
591 n++;
593 * We have a size expression; attempt to
594 * evaluate it.
596 stdscan_reset();
597 stdscan_bufptr = special + n;
598 tokval.t_type = TOKEN_INVALID;
599 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
600 NULL);
601 if (fwd) {
602 sym->nextfwd = fwds;
603 fwds = sym;
604 sym->name = nasm_strdup(name);
605 } else if (e) {
606 if (!is_simple(e))
607 error(ERR_NONFATAL, "cannot use relocatable"
608 " expression as symbol size");
609 else
610 sym->size = reloc_value(e);
612 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
614 special_used = TRUE;
617 sym->globnum = nglobs;
618 nglobs++;
619 } else
620 nlocals++;
622 if (special && !special_used)
623 error(ERR_NONFATAL, "no special symbol features supported here");
626 static void elf_add_reloc(struct Section *sect, long segment, int type)
628 struct Reloc *r;
630 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
631 sect->tail = &r->next;
632 r->next = NULL;
634 r->address = sect->len;
635 if (segment == NO_SEG)
636 r->symbol = 2;
637 else {
638 int i;
639 r->symbol = 0;
640 for (i = 0; i < nsects; i++)
641 if (segment == sects[i]->index)
642 r->symbol = i + 3;
643 if (!r->symbol)
644 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
646 r->type = type;
648 sect->nrelocs++;
652 * This routine deals with ..got and ..sym relocations: the more
653 * complicated kinds. In shared-library writing, some relocations
654 * with respect to global symbols must refer to the precise symbol
655 * rather than referring to an offset from the base of the section
656 * _containing_ the symbol. Such relocations call to this routine,
657 * which searches the symbol list for the symbol in question.
659 * R_386_GOT32 references require the _exact_ symbol address to be
660 * used; R_386_32 references can be at an offset from the symbol.
661 * The boolean argument `exact' tells us this.
663 * Return value is the adjusted value of `addr', having become an
664 * offset from the symbol rather than the section. Should always be
665 * zero when returning from an exact call.
667 * Limitation: if you define two symbols at the same place,
668 * confusion will occur.
670 * Inefficiency: we search, currently, using a linked list which
671 * isn't even necessarily sorted.
673 static long elf_add_gsym_reloc(struct Section *sect,
674 long segment, long offset,
675 int type, int exact)
677 struct Reloc *r;
678 struct Section *s;
679 struct Symbol *sym, *sm;
680 int i;
683 * First look up the segment/offset pair and find a global
684 * symbol corresponding to it. If it's not one of our segments,
685 * then it must be an external symbol, in which case we're fine
686 * doing a normal elf_add_reloc after first sanity-checking
687 * that the offset from the symbol is zero.
689 s = NULL;
690 for (i = 0; i < nsects; i++)
691 if (segment == sects[i]->index) {
692 s = sects[i];
693 break;
695 if (!s) {
696 if (exact && offset != 0)
697 error(ERR_NONFATAL, "unable to find a suitable global symbol"
698 " for this reference");
699 else
700 elf_add_reloc(sect, segment, type);
701 return offset;
704 if (exact) {
706 * Find a symbol pointing _exactly_ at this one.
708 for (sym = s->gsyms; sym; sym = sym->next)
709 if (sym->value == offset)
710 break;
711 } else {
713 * Find the nearest symbol below this one.
715 sym = NULL;
716 for (sm = s->gsyms; sm; sm = sm->next)
717 if (sm->value <= offset && (!sym || sm->value > sym->value))
718 sym = sm;
720 if (!sym && exact) {
721 error(ERR_NONFATAL, "unable to find a suitable global symbol"
722 " for this reference");
723 return 0;
726 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
727 sect->tail = &r->next;
728 r->next = NULL;
730 r->address = sect->len;
731 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
732 r->type = type;
734 sect->nrelocs++;
736 return offset - sym->value;
739 static void elf_out(long segto, const void *data, unsigned long type,
740 long segment, long wrt)
742 struct Section *s;
743 long realbytes = type & OUT_SIZMASK;
744 long addr;
745 unsigned char mydata[4], *p;
746 int i;
747 static struct symlininfo sinfo;
749 type &= OUT_TYPMASK;
752 * handle absolute-assembly (structure definitions)
754 if (segto == NO_SEG) {
755 if (type != OUT_RESERVE)
756 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
757 " space");
758 return;
761 s = NULL;
762 for (i = 0; i < nsects; i++)
763 if (segto == sects[i]->index) {
764 s = sects[i];
765 break;
767 if (!s) {
768 int tempint; /* ignored */
769 if (segto != elf_section_names(".text", 2, &tempint))
770 error(ERR_PANIC, "strange segment conditions in ELF driver");
771 else {
772 s = sects[nsects - 1];
773 i = nsects - 1;
777 /* again some stabs debugging stuff */
778 if (of_elf.current_dfmt) {
779 sinfo.offset = s->len;
780 sinfo.section = i;
781 sinfo.name = s->name;
782 of_elf.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
784 /* end of debugging stuff */
786 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
787 error(ERR_WARNING, "attempt to initialise memory in"
788 " BSS section `%s': ignored", s->name);
789 if (type == OUT_REL2ADR)
790 realbytes = 2;
791 else if (type == OUT_REL4ADR)
792 realbytes = 4;
793 s->len += realbytes;
794 return;
797 if (type == OUT_RESERVE) {
798 if (s->type == SHT_PROGBITS) {
799 error(ERR_WARNING, "uninitialised space declared in"
800 " non-BSS section `%s': zeroing", s->name);
801 elf_sect_write(s, NULL, realbytes);
802 } else
803 s->len += realbytes;
804 } else if (type == OUT_RAWDATA) {
805 if (segment != NO_SEG)
806 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
807 elf_sect_write(s, data, realbytes);
808 } else if (type == OUT_ADDRESS) {
809 int gnu16 = 0;
810 addr = *(long *)data;
811 if (segment != NO_SEG) {
812 if (segment % 2) {
813 error(ERR_NONFATAL, "ELF format does not support"
814 " segment base references");
815 } else {
816 if (wrt == NO_SEG) {
817 if (realbytes == 2) {
818 gnu16 = 1;
819 elf_add_reloc(s, segment, R_386_16);
820 } else {
821 elf_add_reloc(s, segment, R_386_32);
823 } else if (wrt == elf_gotpc_sect + 1) {
825 * The user will supply GOT relative to $$. ELF
826 * will let us have GOT relative to $. So we
827 * need to fix up the data item by $-$$.
829 addr += s->len;
830 elf_add_reloc(s, segment, R_386_GOTPC);
831 } else if (wrt == elf_gotoff_sect + 1) {
832 elf_add_reloc(s, segment, R_386_GOTOFF);
833 } else if (wrt == elf_got_sect + 1) {
834 addr = elf_add_gsym_reloc(s, segment, addr,
835 R_386_GOT32, TRUE);
836 } else if (wrt == elf_sym_sect + 1) {
837 if (realbytes == 2) {
838 gnu16 = 1;
839 addr = elf_add_gsym_reloc(s, segment, addr,
840 R_386_16, FALSE);
841 } else {
842 addr = elf_add_gsym_reloc(s, segment, addr,
843 R_386_32, FALSE);
845 } else if (wrt == elf_plt_sect + 1) {
846 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
847 "relative PLT references");
848 } else {
849 error(ERR_NONFATAL, "ELF format does not support this"
850 " use of WRT");
851 wrt = NO_SEG; /* we can at least _try_ to continue */
855 p = mydata;
856 if (gnu16) {
857 error(ERR_WARNING | ERR_WARN_GNUELF,
858 "16-bit relocations in ELF is a GNU extension");
859 WRITESHORT(p, addr);
860 } else {
861 if (realbytes != 4 && segment != NO_SEG) {
862 error(ERR_NONFATAL,
863 "Unsupported non-32-bit ELF relocation");
865 WRITELONG(p, addr);
867 elf_sect_write(s, mydata, realbytes);
868 } else if (type == OUT_REL2ADR) {
869 if (segment == segto)
870 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
871 if (segment != NO_SEG && segment % 2) {
872 error(ERR_NONFATAL, "ELF format does not support"
873 " segment base references");
874 } else {
875 if (wrt == NO_SEG) {
876 error(ERR_WARNING | ERR_WARN_GNUELF,
877 "16-bit relocations in ELF is a GNU extension");
878 elf_add_reloc(s, segment, R_386_PC16);
879 } else {
880 error(ERR_NONFATAL,
881 "Unsupported non-32-bit ELF relocation");
884 p = mydata;
885 WRITESHORT(p, *(long *)data - realbytes);
886 elf_sect_write(s, mydata, 2L);
887 } else if (type == OUT_REL4ADR) {
888 if (segment == segto)
889 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
890 if (segment != NO_SEG && segment % 2) {
891 error(ERR_NONFATAL, "ELF format does not support"
892 " segment base references");
893 } else {
894 if (wrt == NO_SEG) {
895 elf_add_reloc(s, segment, R_386_PC32);
896 } else if (wrt == elf_plt_sect + 1) {
897 elf_add_reloc(s, segment, R_386_PLT32);
898 } else if (wrt == elf_gotpc_sect + 1 ||
899 wrt == elf_gotoff_sect + 1 ||
900 wrt == elf_got_sect + 1) {
901 error(ERR_NONFATAL, "ELF format cannot produce PC-"
902 "relative GOT references");
903 } else {
904 error(ERR_NONFATAL, "ELF format does not support this"
905 " use of WRT");
906 wrt = NO_SEG; /* we can at least _try_ to continue */
909 p = mydata;
910 WRITELONG(p, *(long *)data - realbytes);
911 elf_sect_write(s, mydata, 4L);
915 static void elf_write(void)
917 int nsections, align;
918 int scount;
919 char *p;
920 int commlen;
921 char comment[64];
922 int i;
924 struct SAA *symtab;
925 long symtablen, symtablocal;
928 * Work out how many sections we will have. We have SHN_UNDEF,
929 * then the flexible user sections, then the four fixed
930 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
931 * then optionally relocation sections for the user sections.
933 if (of_elf.current_dfmt == &df_stabs)
934 nsections = 8;
935 else
936 nsections = 5; /* SHN_UNDEF and the fixed ones */
938 add_sectname("", ".comment");
939 add_sectname("", ".shstrtab");
940 add_sectname("", ".symtab");
941 add_sectname("", ".strtab");
942 for (i = 0; i < nsects; i++) {
943 nsections++; /* for the section itself */
944 if (sects[i]->head) {
945 nsections++; /* for its relocations */
946 add_sectname(".rel", sects[i]->name);
950 if (of_elf.current_dfmt == &df_stabs) {
951 /* in case the debug information is wanted, just add these three sections... */
952 add_sectname("", ".stab");
953 add_sectname("", ".stabstr");
954 add_sectname(".rel", ".stab");
958 * Do the comment.
960 *comment = '\0';
961 commlen =
962 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
965 * Output the ELF header.
967 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
968 fwriteshort(1, elffp); /* ET_REL relocatable file */
969 fwriteshort(3, elffp); /* EM_386 processor ID */
970 fwritelong(1L, elffp); /* EV_CURRENT file format version */
971 fwritelong(0L, elffp); /* no entry point */
972 fwritelong(0L, elffp); /* no program header table */
973 fwritelong(0x40L, elffp); /* section headers straight after
974 * ELF header plus alignment */
975 fwritelong(0L, elffp); /* 386 defines no special flags */
976 fwriteshort(0x34, elffp); /* size of ELF header */
977 fwriteshort(0, elffp); /* no program header table, again */
978 fwriteshort(0, elffp); /* still no program header table */
979 fwriteshort(0x28, elffp); /* size of section header */
980 fwriteshort(nsections, elffp); /* number of sections */
981 fwriteshort(nsects + 2, elffp); /* string table section index for
982 * section header table */
983 fwritelong(0L, elffp); /* align to 0x40 bytes */
984 fwritelong(0L, elffp);
985 fwritelong(0L, elffp);
988 * Build the symbol table and relocation tables.
990 symtab = elf_build_symtab(&symtablen, &symtablocal);
991 for (i = 0; i < nsects; i++)
992 if (sects[i]->head)
993 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
994 sects[i]->head);
997 * Now output the section header table.
1000 elf_foffs = 0x40 + 0x28 * nsections;
1001 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1002 elf_foffs += align;
1003 elf_nsect = 0;
1004 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1006 elf_section_header(0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1007 scount = 1; /* needed for the stabs debugging to track the symtable section */
1008 p = shstrtab + 1;
1009 for (i = 0; i < nsects; i++) {
1010 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1011 (sects[i]->type == SHT_PROGBITS ?
1012 sects[i]->data : NULL), TRUE,
1013 sects[i]->len, 0, 0, sects[i]->align, 0);
1014 p += strlen(p) + 1;
1015 scount++; /* dito */
1017 elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (long)commlen, 0, 0, 1, 0); /* .comment */
1018 scount++; /* dito */
1019 p += strlen(p) + 1;
1020 elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (long)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1021 scount++; /* dito */
1022 p += strlen(p) + 1;
1023 elf_section_header(p - shstrtab, 2, 0, symtab, TRUE, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1024 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1025 p += strlen(p) + 1;
1026 elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
1027 for (i = 0; i < nsects; i++)
1028 if (sects[i]->head) {
1029 p += strlen(p) + 1;
1030 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, TRUE,
1031 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1033 if (of_elf.current_dfmt == &df_stabs) {
1034 /* for debugging information, create the last three sections
1035 which are the .stab , .stabstr and .rel.stab sections respectively */
1037 /* this function call creates the stab sections in memory */
1038 stabs_generate();
1040 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1041 p += strlen(p) + 1;
1042 elf_section_header(p - shstrtab, 1, 0, stabbuf, 0, stablen,
1043 nsections - 2, 0, 4, 12);
1045 p += strlen(p) + 1;
1046 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, 0,
1047 stabstrlen, 0, 0, 4, 0);
1049 p += strlen(p) + 1;
1050 /* link -> symtable info -> section to refer to */
1051 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, 0,
1052 stabrellen, symtabsection, nsections - 3, 4,
1056 fwrite(align_str, align, 1, elffp);
1059 * Now output the sections.
1061 elf_write_sections();
1063 nasm_free(elf_sects);
1064 saa_free(symtab);
1067 static struct SAA *elf_build_symtab(long *len, long *local)
1069 struct SAA *s = saa_init(1L);
1070 struct Symbol *sym;
1071 unsigned char entry[16], *p;
1072 int i;
1074 *len = *local = 0;
1077 * First, an all-zeros entry, required by the ELF spec.
1079 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1080 *len += 16;
1081 (*local)++;
1084 * Next, an entry for the file name.
1086 p = entry;
1087 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1088 WRITELONG(p, 0); /* no value */
1089 WRITELONG(p, 0); /* no size either */
1090 WRITESHORT(p, 4); /* type FILE */
1091 WRITESHORT(p, SHN_ABS);
1092 saa_wbytes(s, entry, 16L);
1093 *len += 16;
1094 (*local)++;
1097 * Now some standard symbols defining the segments, for relocation
1098 * purposes.
1100 for (i = 1; i <= nsects + 1; i++) {
1101 p = entry;
1102 WRITELONG(p, 0); /* no symbol name */
1103 WRITELONG(p, 0); /* offset zero */
1104 WRITELONG(p, 0); /* size zero */
1105 WRITESHORT(p, 3); /* local section-type thing */
1106 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1107 saa_wbytes(s, entry, 16L);
1108 *len += 16;
1109 (*local)++;
1113 * Now the other local symbols.
1115 saa_rewind(syms);
1116 while ((sym = saa_rstruct(syms))) {
1117 if (sym->type & SYM_GLOBAL)
1118 continue;
1119 p = entry;
1120 WRITELONG(p, sym->strpos);
1121 WRITELONG(p, sym->value);
1122 WRITELONG(p, sym->size);
1123 WRITESHORT(p, sym->type); /* local non-typed thing */
1124 WRITESHORT(p, sym->section);
1125 saa_wbytes(s, entry, 16L);
1126 *len += 16;
1127 (*local)++;
1131 * Now the global symbols.
1133 saa_rewind(syms);
1134 while ((sym = saa_rstruct(syms))) {
1135 if (!(sym->type & SYM_GLOBAL))
1136 continue;
1137 p = entry;
1138 WRITELONG(p, sym->strpos);
1139 WRITELONG(p, sym->value);
1140 WRITELONG(p, sym->size);
1141 WRITESHORT(p, sym->type); /* global non-typed thing */
1142 WRITESHORT(p, sym->section);
1143 saa_wbytes(s, entry, 16L);
1144 *len += 16;
1147 return s;
1150 static struct SAA *elf_build_reltab(long *len, struct Reloc *r)
1152 struct SAA *s;
1153 unsigned char *p, entry[8];
1155 if (!r)
1156 return NULL;
1158 s = saa_init(1L);
1159 *len = 0;
1161 while (r) {
1162 long sym = r->symbol;
1164 if (sym >= GLOBAL_TEMP_BASE)
1165 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1167 p = entry;
1168 WRITELONG(p, r->address);
1169 WRITELONG(p, (sym << 8) + r->type);
1170 saa_wbytes(s, entry, 8L);
1171 *len += 8;
1173 r = r->next;
1176 return s;
1179 static void elf_section_header(int name, int type, int flags,
1180 void *data, int is_saa, long datalen,
1181 int link, int info, int align, int eltsize)
1183 elf_sects[elf_nsect].data = data;
1184 elf_sects[elf_nsect].len = datalen;
1185 elf_sects[elf_nsect].is_saa = is_saa;
1186 elf_nsect++;
1188 fwritelong((long)name, elffp);
1189 fwritelong((long)type, elffp);
1190 fwritelong((long)flags, elffp);
1191 fwritelong(0L, elffp); /* no address, ever, in object files */
1192 fwritelong(type == 0 ? 0L : elf_foffs, elffp);
1193 fwritelong(datalen, elffp);
1194 if (data)
1195 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1196 fwritelong((long)link, elffp);
1197 fwritelong((long)info, elffp);
1198 fwritelong((long)align, elffp);
1199 fwritelong((long)eltsize, elffp);
1202 static void elf_write_sections(void)
1204 int i;
1205 for (i = 0; i < elf_nsect; i++)
1206 if (elf_sects[i].data) {
1207 long len = elf_sects[i].len;
1208 long reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1209 long align = reallen - len;
1210 if (elf_sects[i].is_saa)
1211 saa_fpwrite(elf_sects[i].data, elffp);
1212 else
1213 fwrite(elf_sects[i].data, len, 1, elffp);
1214 fwrite(align_str, align, 1, elffp);
1218 static void elf_sect_write(struct Section *sect,
1219 const unsigned char *data, unsigned long len)
1221 saa_wbytes(sect->data, data, len);
1222 sect->len += len;
1225 static long elf_segbase(long segment)
1227 return segment;
1230 static int elf_directive(char *directive, char *value, int pass)
1232 return 0;
1235 static void elf_filename(char *inname, char *outname, efunc error)
1237 strcpy(elf_module, inname);
1238 standard_extension(inname, outname, ".o", error);
1241 static const char *elf_stdmac[] = {
1242 "%define __SECT__ [section .text]",
1243 "%macro __NASM_CDecl__ 1",
1244 "%define $_%1 $%1",
1245 "%endmacro",
1246 NULL
1248 static int elf_set_info(enum geninfo type, char **val)
1250 return 0;
1253 static struct dfmt df_stabs = {
1254 "ELF32 (i386) stabs debug format for Linux",
1255 "stabs",
1256 stabs_init,
1257 stabs_linenum,
1258 stabs_deflabel,
1259 stabs_directive,
1260 stabs_typevalue,
1261 stabs_output,
1262 stabs_cleanup
1265 struct dfmt *elf_debugs_arr[2] = { &df_stabs, NULL };
1267 struct ofmt of_elf = {
1268 "ELF32 (i386) object files (e.g. Linux)",
1269 "elf",
1270 NULL,
1271 elf_debugs_arr,
1272 &null_debug_form,
1273 elf_stdmac,
1274 elf_init,
1275 elf_set_info,
1276 elf_out,
1277 elf_deflabel,
1278 elf_section_names,
1279 elf_segbase,
1280 elf_directive,
1281 elf_filename,
1282 elf_cleanup
1285 /* again, the stabs debugging stuff (code) */
1287 void stabs_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1291 void stabs_linenum(const char *filename, long linenumber, long segto)
1293 if (!stabs_filename) {
1294 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1295 strcpy(stabs_filename, filename);
1296 } else {
1297 if (strcmp(stabs_filename, filename)) {
1298 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1299 in fact, this leak comes in quite handy to maintain a list of files
1300 encountered so far in the symbol lines... */
1302 /* why not nasm_free(stabs_filename); we're done with the old one */
1304 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1305 strcpy(stabs_filename, filename);
1308 stabs_immcall = 1;
1309 currentline = linenumber;
1312 void stabs_deflabel(char *name, long segment, long offset, int is_global,
1313 char *special)
1317 void stabs_directive(const char *directive, const char *params)
1321 void stabs_typevalue(long type)
1325 void stabs_output(int type, void *param)
1327 struct symlininfo *s;
1328 struct linelist *el;
1329 if (type == TY_STABSSYMLIN) {
1330 if (stabs_immcall) {
1331 s = (struct symlininfo *)param;
1332 if (strcmp(s->name, ".text"))
1333 return; /* we are only interested in the text stuff */
1334 numlinestabs++;
1335 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1336 el->info.offset = s->offset;
1337 el->info.section = s->section;
1338 el->info.name = s->name;
1339 el->line = currentline;
1340 el->filename = stabs_filename;
1341 el->next = 0;
1342 if (stabslines) {
1343 stabslines->last->next = el;
1344 stabslines->last = el;
1345 } else {
1346 stabslines = el;
1347 stabslines->last = el;
1351 stabs_immcall = 0;
1354 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1355 do {\
1356 WRITELONG(p,n_strx); \
1357 WRITECHAR(p,n_type); \
1358 WRITECHAR(p,n_other); \
1359 WRITESHORT(p,n_desc); \
1360 WRITELONG(p,n_value); \
1361 } while (0)
1363 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1365 void stabs_generate(void)
1367 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1368 unsigned char *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1369 char **allfiles;
1370 int *fileidx;
1372 struct linelist *ptr;
1374 ptr = stabslines;
1376 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1377 for (i = 0; i < numlinestabs; i++)
1378 allfiles[i] = 0;
1379 numfiles = 0;
1380 while (ptr) {
1381 if (numfiles == 0) {
1382 allfiles[0] = ptr->filename;
1383 numfiles++;
1384 } else {
1385 for (i = 0; i < numfiles; i++) {
1386 if (!strcmp(allfiles[i], ptr->filename))
1387 break;
1389 if (i >= numfiles) {
1390 allfiles[i] = ptr->filename;
1391 numfiles++;
1394 ptr = ptr->next;
1396 strsize = 1;
1397 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1398 for (i = 0; i < numfiles; i++) {
1399 fileidx[i] = strsize;
1400 strsize += strlen(allfiles[i]) + 1;
1402 mainfileindex = 0;
1403 for (i = 0; i < numfiles; i++) {
1404 if (!strcmp(allfiles[i], elf_module)) {
1405 mainfileindex = i;
1406 break;
1410 /* worst case size of the stab buffer would be:
1411 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1413 sbuf =
1414 (unsigned char *)nasm_malloc((numlinestabs * 2 + 3) *
1415 sizeof(struct stabentry));
1417 ssbuf = (unsigned char *)nasm_malloc(strsize);
1419 rbuf = (unsigned char *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1420 rptr = rbuf;
1422 for (i = 0; i < numfiles; i++) {
1423 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1425 ssbuf[0] = 0;
1427 stabstrlen = strsize; /* set global variable for length of stab strings */
1429 sptr = sbuf;
1430 /* this is the first stab, its strx points to the filename of the
1431 the source-file, the n_desc field should be set to the number
1432 of remaining stabs
1434 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1436 ptr = stabslines;
1437 numstabs = 0;
1439 if (ptr) {
1440 /* this is the stab for the main source file */
1441 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1443 /* relocation stuff */
1444 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1445 WRITELONG(rptr, (sptr - sbuf) - 4);
1446 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1448 numstabs++;
1449 currfile = mainfileindex;
1452 while (ptr) {
1453 if (strcmp(allfiles[currfile], ptr->filename)) {
1454 /* oops file has changed... */
1455 for (i = 0; i < numfiles; i++)
1456 if (!strcmp(allfiles[i], ptr->filename))
1457 break;
1458 currfile = i;
1459 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1460 ptr->info.offset);
1461 numstabs++;
1463 /* relocation stuff */
1464 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1465 WRITELONG(rptr, (sptr - sbuf) - 4);
1466 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1469 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1470 numstabs++;
1472 /* relocation stuff */
1473 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1474 WRITELONG(rptr, (sptr - sbuf) - 4);
1475 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1477 ptr = ptr->next;
1481 ((struct stabentry *)sbuf)->n_desc = numstabs;
1483 nasm_free(allfiles);
1484 nasm_free(fileidx);
1486 stablen = (sptr - sbuf);
1487 stabrellen = (rptr - rbuf);
1488 stabrelbuf = rbuf;
1489 stabbuf = sbuf;
1490 stabstrbuf = ssbuf;
1493 void stabs_cleanup()
1495 struct linelist *ptr, *del;
1496 if (!stabslines)
1497 return;
1498 ptr = stabslines;
1499 while (ptr) {
1500 del = ptr;
1501 ptr = ptr->next;
1502 nasm_free(del);
1504 if (stabbuf)
1505 nasm_free(stabbuf);
1506 if (stabrelbuf)
1507 nasm_free(stabrelbuf);
1508 if (stabstrbuf)
1509 nasm_free(stabstrbuf);
1512 #endif /* OF_ELF */