Update for newer RH releases
[nasm/avx512.git] / output / outelf.c
blobd53c8a5e75a2cd0a0adb17ca0695e1947188fd60
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 *, unsigned long);
137 static void elf_section_header (int, int, int, void *, int, long,
138 int, int, int, int);
139 static void elf_write_sections (void);
140 static struct SAA *elf_build_symtab (long *, long *);
141 static struct SAA *elf_build_reltab (long *, struct Reloc *);
142 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;
189 void stabs_init(struct ofmt *,void *,FILE *,efunc );
190 void stabs_linenum(const char *filename,long linenumber,long);
191 void stabs_deflabel(char *,long ,long ,int ,char *);
192 void stabs_directive(const char *,const char *);
193 void stabs_typevalue(long );
194 void stabs_output(int ,void *);
195 void stabs_generate();
196 void stabs_cleanup();
198 /* 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, error);
233 elf_gotoff_sect = seg_alloc();
234 ldef("..gotoff", elf_gotoff_sect+1, 0L, NULL, FALSE, FALSE,&of_elf,error);
235 elf_got_sect = seg_alloc();
236 ldef("..got", elf_got_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
237 elf_plt_sect = seg_alloc();
238 ldef("..plt", elf_plt_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
239 elf_sym_sect = seg_alloc();
240 ldef("..sym", elf_sym_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
242 def_seg = seg_alloc();
245 static void elf_cleanup(int debuginfo)
247 struct Reloc *r;
248 int i;
250 (void) debuginfo;
252 elf_write();
253 fclose (elffp);
254 for (i=0; i<nsects; i++) {
255 if (sects[i]->type != SHT_NOBITS)
256 saa_free (sects[i]->data);
257 if (sects[i]->head)
258 saa_free (sects[i]->rel);
259 while (sects[i]->head) {
260 r = sects[i]->head;
261 sects[i]->head = sects[i]->head->next;
262 nasm_free (r);
265 nasm_free (sects);
266 saa_free (syms);
267 raa_free (bsym);
268 saa_free (strs);
269 if (of_elf.current_dfmt) {
270 of_elf.current_dfmt->cleanup();
274 static void add_sectname (char *firsthalf, char *secondhalf)
276 int len = strlen(firsthalf)+strlen(secondhalf);
277 while (shstrtablen + len + 1 > shstrtabsize)
278 shstrtab = nasm_realloc (shstrtab, (shstrtabsize += SHSTR_DELTA));
279 strcpy (shstrtab+shstrtablen, firsthalf);
280 strcat (shstrtab+shstrtablen, secondhalf);
281 shstrtablen += len+1;
284 static int elf_make_section (char *name, int type, int flags, int align)
286 struct Section *s;
288 s = nasm_malloc (sizeof(*s));
290 if (type != SHT_NOBITS)
291 s->data = saa_init (1L);
292 s->head = NULL;
293 s->tail = &s->head;
294 s->len = s->size = 0;
295 s->nrelocs = 0;
296 if (!strcmp(name, ".text"))
297 s->index = def_seg;
298 else
299 s->index = seg_alloc();
300 add_sectname ("", name);
301 s->name = nasm_malloc (1+strlen(name));
302 strcpy (s->name, name);
303 s->type = type;
304 s->flags = flags;
305 s->align = align;
306 s->gsyms = NULL;
308 if (nsects >= sectlen)
309 sects = nasm_realloc (sects, (sectlen += SECT_DELTA)*sizeof(*sects));
310 sects[nsects++] = s;
312 return nsects-1;
315 static long elf_section_names (char *name, int pass, int *bits)
317 char *p;
318 int flags_and, flags_or, type, align, i;
321 * Default is 32 bits.
323 if (!name) {
324 *bits = 32;
325 return def_seg;
328 p = name;
329 while (*p && !isspace(*p)) p++;
330 if (*p) *p++ = '\0';
331 flags_and = flags_or = type = align = 0;
333 while (*p && isspace(*p)) p++;
334 while (*p) {
335 char *q = p;
336 while (*p && !isspace(*p)) p++;
337 if (*p) *p++ = '\0';
338 while (*p && isspace(*p)) p++;
340 if (!nasm_strnicmp(q, "align=", 6)) {
341 align = atoi(q+6);
342 if (align == 0)
343 align = 1;
344 if ( (align-1) & align ) { /* means it's not a power of two */
345 error (ERR_NONFATAL, "section alignment %d is not"
346 " a power of two", align);
347 align = 1;
349 } else if (!nasm_stricmp(q, "alloc")) {
350 flags_and |= SHF_ALLOC;
351 flags_or |= SHF_ALLOC;
352 } else if (!nasm_stricmp(q, "noalloc")) {
353 flags_and |= SHF_ALLOC;
354 flags_or &= ~SHF_ALLOC;
355 } else if (!nasm_stricmp(q, "exec")) {
356 flags_and |= SHF_EXECINSTR;
357 flags_or |= SHF_EXECINSTR;
358 } else if (!nasm_stricmp(q, "noexec")) {
359 flags_and |= SHF_EXECINSTR;
360 flags_or &= ~SHF_EXECINSTR;
361 } else if (!nasm_stricmp(q, "write")) {
362 flags_and |= SHF_WRITE;
363 flags_or |= SHF_WRITE;
364 } else if (!nasm_stricmp(q, "nowrite")) {
365 flags_and |= SHF_WRITE;
366 flags_or &= ~SHF_WRITE;
367 } else if (!nasm_stricmp(q, "progbits")) {
368 type = SHT_PROGBITS;
369 } else if (!nasm_stricmp(q, "nobits")) {
370 type = SHT_NOBITS;
374 if (!strcmp(name, ".comment") ||
375 !strcmp(name, ".shstrtab") ||
376 !strcmp(name, ".symtab") ||
377 !strcmp(name, ".strtab")) {
378 error (ERR_NONFATAL, "attempt to redefine reserved section"
379 "name `%s'", name);
380 return NO_SEG;
383 for (i=0; i<nsects; i++)
384 if (!strcmp(name, sects[i]->name))
385 break;
386 if (i == nsects) {
387 if (!strcmp(name, ".text"))
388 i = elf_make_section (name, SHT_PROGBITS,
389 SHF_ALLOC | SHF_EXECINSTR, 16);
390 else if (!strcmp(name, ".rodata"))
391 i = elf_make_section (name, SHT_PROGBITS,
392 SHF_ALLOC, 4);
393 else if (!strcmp(name, ".data"))
394 i = elf_make_section (name, SHT_PROGBITS,
395 SHF_ALLOC | SHF_WRITE, 4);
396 else if (!strcmp(name, ".bss"))
397 i = elf_make_section (name, SHT_NOBITS,
398 SHF_ALLOC | SHF_WRITE, 4);
399 else
400 i = elf_make_section (name, SHT_PROGBITS, SHF_ALLOC, 1);
401 if (type)
402 sects[i]->type = type;
403 if (align)
404 sects[i]->align = align;
405 sects[i]->flags &= ~flags_and;
406 sects[i]->flags |= flags_or;
407 } else if (pass == 1) {
408 if (type || align || flags_and)
409 error (ERR_WARNING, "section attributes ignored on"
410 " redeclaration of section `%s'", name);
413 return sects[i]->index;
416 static void elf_deflabel (char *name, long segment, long offset,
417 int is_global, char *special)
419 int pos = strslen;
420 struct Symbol *sym;
421 int special_used = FALSE;
423 #if defined(DEBUG) && DEBUG>2
424 fprintf(stderr, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
425 name, segment, offset, is_global, special);
426 #endif
427 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
429 * This is a NASM special symbol. We never allow it into
430 * the ELF symbol table, even if it's a valid one. If it
431 * _isn't_ a valid one, we should barf immediately.
433 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
434 strcmp(name, "..got") && strcmp(name, "..plt") &&
435 strcmp(name, "..sym"))
436 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
437 return;
440 if (is_global == 3) {
441 struct Symbol **s;
443 * Fix up a forward-reference symbol size from the first
444 * pass.
446 for (s = &fwds; *s; s = &(*s)->nextfwd)
447 if (!strcmp((*s)->name, name)) {
448 struct tokenval tokval;
449 expr *e;
450 char *p = special;
452 while (*p && !isspace(*p)) p++;
453 while (*p && isspace(*p)) p++;
454 stdscan_reset();
455 stdscan_bufptr = p;
456 tokval.t_type = TOKEN_INVALID;
457 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
458 if (e) {
459 if (!is_simple(e))
460 error (ERR_NONFATAL, "cannot use relocatable"
461 " expression as symbol size");
462 else
463 (*s)->size = reloc_value(e);
467 * Remove it from the list of unresolved sizes.
469 nasm_free ((*s)->name);
470 *s = (*s)->nextfwd;
471 return;
473 return; /* it wasn't an important one */
476 saa_wbytes (strs, name, (long)(1+strlen(name)));
477 strslen += 1+strlen(name);
479 sym = saa_wstruct (syms);
481 sym->strpos = pos;
482 sym->type = is_global ? SYM_GLOBAL : 0;
483 sym->size = 0;
484 if (segment == NO_SEG)
485 sym->section = SHN_ABS;
486 else {
487 int i;
488 sym->section = SHN_UNDEF;
489 if (nsects == 0 && segment == def_seg) {
490 int tempint;
491 if (segment != elf_section_names (".text", 2, &tempint))
492 error (ERR_PANIC, "strange segment conditions in ELF driver");
493 sym->section = nsects;
494 } else {
495 for (i=0; i<nsects; i++)
496 if (segment == sects[i]->index) {
497 sym->section = i+1;
498 break;
503 if (is_global == 2) {
504 sym->size = offset;
505 sym->value = 0;
506 sym->section = SHN_COMMON;
508 * We have a common variable. Check the special text to see
509 * if it's a valid number and power of two; if so, store it
510 * as the alignment for the common variable.
512 if (special) {
513 int err;
514 sym->value = readnum (special, &err);
515 if (err)
516 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
517 " valid number", special);
518 else if ( (sym->value | (sym->value-1)) != 2*sym->value - 1)
519 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
520 " power of two", special);
522 special_used = TRUE;
523 } else
524 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
526 if (sym->type == SYM_GLOBAL) {
528 * There's a problem here that needs fixing.
529 * If sym->section == SHN_ABS, then the first line of the
530 * else section causes a core dump, because its a reference
531 * beyond the end of the section array.
532 * This behaviour is exhibited by this code:
533 * GLOBAL crash_nasm
534 * crash_nasm equ 0
536 * I'm not sure how to procede, because I haven't got the
537 * first clue about how ELF works, so I don't know what to
538 * do with it. Furthermore, I'm not sure what the rest of this
539 * section of code does. Help?
541 * For now, I'll see if doing absolutely nothing with it will
542 * work...
544 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON)
546 bsym = raa_write (bsym, segment, nglobs);
548 else if (sym->section != SHN_ABS)
551 * This is a global symbol; so we must add it to the linked
552 * list of global symbols in its section. We'll push it on
553 * the beginning of the list, because it doesn't matter
554 * much which end we put it on and it's easier like this.
556 * In addition, we check the special text for symbol
557 * type and size information.
559 sym->next = sects[sym->section-1]->gsyms;
560 sects[sym->section-1]->gsyms = sym;
562 if (special) {
563 int n = strcspn(special, " ");
565 if (!nasm_strnicmp(special, "function", n))
566 sym->type |= SYM_FUNCTION;
567 else if (!nasm_strnicmp(special, "data", n) ||
568 !nasm_strnicmp(special, "object", n))
569 sym->type |= SYM_DATA;
570 else
571 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
572 n, special);
573 if (special[n]) {
574 struct tokenval tokval;
575 expr *e;
576 int fwd = FALSE;
577 char *saveme=stdscan_bufptr; /* bugfix? fbk 8/10/00 */
579 while (special[n] && isspace(special[n]))
580 n++;
582 * We have a size expression; attempt to
583 * evaluate it.
585 stdscan_reset();
586 stdscan_bufptr = special+n;
587 tokval.t_type = TOKEN_INVALID;
588 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
589 if (fwd) {
590 sym->nextfwd = fwds;
591 fwds = sym;
592 sym->name = nasm_strdup(name);
593 } else if (e) {
594 if (!is_simple(e))
595 error (ERR_NONFATAL, "cannot use relocatable"
596 " expression as symbol size");
597 else
598 sym->size = reloc_value(e);
600 stdscan_bufptr=saveme; /* bugfix? fbk 8/10/00 */
602 special_used = TRUE;
605 sym->globnum = nglobs;
606 nglobs++;
607 } else
608 nlocals++;
610 if (special && !special_used)
611 error(ERR_NONFATAL, "no special symbol features supported here");
614 static void elf_add_reloc (struct Section *sect, long segment,
615 int type)
617 struct Reloc *r;
619 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
620 sect->tail = &r->next;
621 r->next = NULL;
623 r->address = sect->len;
624 if (segment == NO_SEG)
625 r->symbol = 2;
626 else {
627 int i;
628 r->symbol = 0;
629 for (i=0; i<nsects; i++)
630 if (segment == sects[i]->index)
631 r->symbol = i+3;
632 if (!r->symbol)
633 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
635 r->type = type;
637 sect->nrelocs++;
641 * This routine deals with ..got and ..sym relocations: the more
642 * complicated kinds. In shared-library writing, some relocations
643 * with respect to global symbols must refer to the precise symbol
644 * rather than referring to an offset from the base of the section
645 * _containing_ the symbol. Such relocations call to this routine,
646 * which searches the symbol list for the symbol in question.
648 * R_386_GOT32 references require the _exact_ symbol address to be
649 * used; R_386_32 references can be at an offset from the symbol.
650 * The boolean argument `exact' tells us this.
652 * Return value is the adjusted value of `addr', having become an
653 * offset from the symbol rather than the section. Should always be
654 * zero when returning from an exact call.
656 * Limitation: if you define two symbols at the same place,
657 * confusion will occur.
659 * Inefficiency: we search, currently, using a linked list which
660 * isn't even necessarily sorted.
662 static long elf_add_gsym_reloc (struct Section *sect,
663 long segment, long offset,
664 int type, int exact)
666 struct Reloc *r;
667 struct Section *s;
668 struct Symbol *sym, *sm;
669 int i;
672 * First look up the segment/offset pair and find a global
673 * symbol corresponding to it. If it's not one of our segments,
674 * then it must be an external symbol, in which case we're fine
675 * doing a normal elf_add_reloc after first sanity-checking
676 * that the offset from the symbol is zero.
678 s = NULL;
679 for (i=0; i<nsects; i++)
680 if (segment == sects[i]->index) {
681 s = sects[i];
682 break;
684 if (!s) {
685 if (exact && offset != 0)
686 error (ERR_NONFATAL, "unable to find a suitable global symbol"
687 " for this reference");
688 else
689 elf_add_reloc (sect, segment, type);
690 return offset;
693 if (exact) {
695 * Find a symbol pointing _exactly_ at this one.
697 for (sym = s->gsyms; sym; sym = sym->next)
698 if (sym->value == offset)
699 break;
700 } else {
702 * Find the nearest symbol below this one.
704 sym = NULL;
705 for (sm = s->gsyms; sm; sm = sm->next)
706 if (sm->value <= offset && (!sym || sm->value > sym->value))
707 sym = sm;
709 if (!sym && exact) {
710 error (ERR_NONFATAL, "unable to find a suitable global symbol"
711 " for this reference");
712 return 0;
715 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
716 sect->tail = &r->next;
717 r->next = NULL;
719 r->address = sect->len;
720 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
721 r->type = type;
723 sect->nrelocs++;
725 return offset - sym->value;
728 static void elf_out (long segto, const void *data, unsigned long type,
729 long segment, long wrt)
731 struct Section *s;
732 long realbytes = type & OUT_SIZMASK;
733 long addr;
734 unsigned char mydata[4], *p;
735 int i;
736 static struct symlininfo sinfo;
738 type &= OUT_TYPMASK;
741 * handle absolute-assembly (structure definitions)
743 if (segto == NO_SEG) {
744 if (type != OUT_RESERVE)
745 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
746 " space");
747 return;
750 s = NULL;
751 for (i=0; i<nsects; i++)
752 if (segto == sects[i]->index) {
753 s = sects[i];
754 break;
756 if (!s) {
757 int tempint; /* ignored */
758 if (segto != elf_section_names (".text", 2, &tempint))
759 error (ERR_PANIC, "strange segment conditions in ELF driver");
760 else {
761 s = sects[nsects-1];
762 i=nsects-1;
766 /* again some stabs debugging stuff */
767 if (of_elf.current_dfmt) {
768 sinfo.offset=s->len;
769 sinfo.section=i;
770 sinfo.name=s->name;
771 of_elf.current_dfmt->debug_output(TY_STABSSYMLIN,&sinfo);
773 /* end of debugging stuff */
775 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
776 error(ERR_WARNING, "attempt to initialise memory in"
777 " BSS section `%s': ignored", s->name);
778 if (type == OUT_REL2ADR)
779 realbytes = 2;
780 else if (type == OUT_REL4ADR)
781 realbytes = 4;
782 s->len += realbytes;
783 return;
786 if (type == OUT_RESERVE) {
787 if (s->type == SHT_PROGBITS) {
788 error(ERR_WARNING, "uninitialised space declared in"
789 " non-BSS section `%s': zeroing", s->name);
790 elf_sect_write (s, NULL, realbytes);
791 } else
792 s->len += realbytes;
793 } else if (type == OUT_RAWDATA) {
794 if (segment != NO_SEG)
795 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
796 elf_sect_write (s, data, realbytes);
797 } else if (type == OUT_ADDRESS) {
798 int gnu16 = 0;
799 addr = *(long *)data;
800 if (segment != NO_SEG) {
801 if (segment % 2) {
802 error(ERR_NONFATAL, "ELF format does not support"
803 " segment base references");
804 } else {
805 if (wrt == NO_SEG) {
806 if ( realbytes == 2 ) {
807 gnu16 = 1;
808 elf_add_reloc (s, segment, R_386_16);
809 } else {
810 elf_add_reloc (s, segment, R_386_32);
812 } else if (wrt == elf_gotpc_sect+1) {
814 * The user will supply GOT relative to $$. ELF
815 * will let us have GOT relative to $. So we
816 * need to fix up the data item by $-$$.
818 addr += s->len;
819 elf_add_reloc (s, segment, R_386_GOTPC);
820 } else if (wrt == elf_gotoff_sect+1) {
821 elf_add_reloc (s, segment, R_386_GOTOFF);
822 } else if (wrt == elf_got_sect+1) {
823 addr = elf_add_gsym_reloc (s, segment, addr,
824 R_386_GOT32, TRUE);
825 } else if (wrt == elf_sym_sect+1) {
826 if ( realbytes == 2 ) {
827 gnu16 = 1;
828 addr = elf_add_gsym_reloc (s, segment, addr,
829 R_386_16, FALSE);
830 } else {
831 addr = elf_add_gsym_reloc (s, segment, addr,
832 R_386_32, FALSE);
834 } else if (wrt == elf_plt_sect+1) {
835 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
836 "relative PLT references");
837 } else {
838 error (ERR_NONFATAL, "ELF format does not support this"
839 " use of WRT");
840 wrt = NO_SEG; /* we can at least _try_ to continue */
844 p = mydata;
845 if (gnu16) {
846 error(ERR_WARNING|ERR_WARN_GNUELF,
847 "16-bit relocations in ELF is a GNU extension");
848 WRITESHORT (p, addr);
849 } else {
850 if (realbytes != 4 && segment != NO_SEG) {
851 error (ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
853 WRITELONG (p, addr);
855 elf_sect_write (s, mydata, realbytes);
856 } else if (type == OUT_REL2ADR) {
857 if (segment == segto)
858 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
859 if (segment != NO_SEG && segment % 2) {
860 error(ERR_NONFATAL, "ELF format does not support"
861 " segment base references");
862 } else {
863 if (wrt == NO_SEG) {
864 error (ERR_WARNING|ERR_WARN_GNUELF,
865 "16-bit relocations in ELF is a GNU extension");
866 elf_add_reloc (s, segment, R_386_PC16);
867 } else {
868 error (ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
871 p = mydata;
872 WRITESHORT (p, *(long*)data - realbytes);
873 elf_sect_write (s, mydata, 2L);
874 } else if (type == OUT_REL4ADR) {
875 if (segment == segto)
876 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
877 if (segment != NO_SEG && segment % 2) {
878 error(ERR_NONFATAL, "ELF format does not support"
879 " segment base references");
880 } else {
881 if (wrt == NO_SEG) {
882 elf_add_reloc (s, segment, R_386_PC32);
883 } else if (wrt == elf_plt_sect+1) {
884 elf_add_reloc (s, segment, R_386_PLT32);
885 } else if (wrt == elf_gotpc_sect+1 ||
886 wrt == elf_gotoff_sect+1 ||
887 wrt == elf_got_sect+1) {
888 error(ERR_NONFATAL, "ELF format cannot produce PC-"
889 "relative GOT references");
890 } else {
891 error (ERR_NONFATAL, "ELF format does not support this"
892 " use of WRT");
893 wrt = NO_SEG; /* we can at least _try_ to continue */
896 p = mydata;
897 WRITELONG (p, *(long*)data - realbytes);
898 elf_sect_write (s, mydata, 4L);
902 static void elf_write(void)
904 int nsections, align;
905 int scount;
906 char *p;
907 int commlen;
908 char comment[64];
909 int i;
911 struct SAA *symtab;
912 long symtablen, symtablocal;
915 * Work out how many sections we will have. We have SHN_UNDEF,
916 * then the flexible user sections, then the four fixed
917 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
918 * then optionally relocation sections for the user sections.
920 if (of_elf.current_dfmt)
921 nsections=8;
922 else
923 nsections = 5; /* SHN_UNDEF and the fixed ones */
925 add_sectname ("", ".comment");
926 add_sectname ("", ".shstrtab");
927 add_sectname ("", ".symtab");
928 add_sectname ("", ".strtab");
929 for (i=0; i<nsects; i++) {
930 nsections++; /* for the section itself */
931 if (sects[i]->head) {
932 nsections++; /* for its relocations */
933 add_sectname (".rel", sects[i]->name);
937 if (of_elf.current_dfmt) {
938 /* in case the debug information is wanted, just add these three sections... */
939 add_sectname("",".stab");
940 add_sectname("",".stabstr");
941 add_sectname(".rel",".stab");
945 * Do the comment.
947 *comment = '\0';
948 commlen = 2+sprintf(comment+1, "The Netwide Assembler %s", NASM_VER);
951 * Output the ELF header.
953 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
954 fwriteshort (1, elffp); /* ET_REL relocatable file */
955 fwriteshort (3, elffp); /* EM_386 processor ID */
956 fwritelong (1L, elffp); /* EV_CURRENT file format version */
957 fwritelong (0L, elffp); /* no entry point */
958 fwritelong (0L, elffp); /* no program header table */
959 fwritelong (0x40L, elffp); /* section headers straight after
960 * ELF header plus alignment */
961 fwritelong (0L, elffp); /* 386 defines no special flags */
962 fwriteshort (0x34, elffp); /* size of ELF header */
963 fwriteshort (0, elffp); /* no program header table, again */
964 fwriteshort (0, elffp); /* still no program header table */
965 fwriteshort (0x28, elffp); /* size of section header */
966 fwriteshort (nsections, elffp); /* number of sections */
967 fwriteshort (nsects+2, elffp); /* string table section index for
968 * section header table */
969 fwritelong (0L, elffp); /* align to 0x40 bytes */
970 fwritelong (0L, elffp);
971 fwritelong (0L, elffp);
974 * Build the symbol table and relocation tables.
976 symtab = elf_build_symtab (&symtablen, &symtablocal);
977 for (i=0; i<nsects; i++)
978 if (sects[i]->head)
979 sects[i]->rel = elf_build_reltab (&sects[i]->rellen,
980 sects[i]->head);
983 * Now output the section header table.
986 elf_foffs = 0x40 + 0x28 * nsections;
987 align = ((elf_foffs+SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
988 elf_foffs += align;
989 elf_nsect = 0;
990 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
992 elf_section_header (0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
993 scount=1; /* needed for the stabs debugging to track the symtable section */
994 p = shstrtab+1;
995 for (i=0; i<nsects; i++) {
996 elf_section_header (p - shstrtab, sects[i]->type, sects[i]->flags,
997 (sects[i]->type == SHT_PROGBITS ?
998 sects[i]->data : NULL), TRUE,
999 sects[i]->len, 0, 0, sects[i]->align, 0);
1000 p += strlen(p)+1;
1001 scount++; /* dito */
1003 elf_section_header (p - shstrtab, 1, 0, comment, FALSE,
1004 (long)commlen, 0, 0, 1, 0);/* .comment */
1005 scount++; /* dito */
1006 p += strlen(p)+1;
1007 elf_section_header (p - shstrtab, 3, 0, shstrtab, FALSE,
1008 (long)shstrtablen, 0, 0, 1, 0);/* .shstrtab */
1009 scount++; /* dito */
1010 p += strlen(p)+1;
1011 elf_section_header (p - shstrtab, 2, 0, symtab, TRUE,
1012 symtablen, nsects+4, symtablocal, 4, 16);/* .symtab */
1013 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1014 p += strlen(p)+1;
1015 elf_section_header (p - shstrtab, 3, 0, strs, TRUE,
1016 strslen, 0, 0, 1, 0); /* .strtab */
1017 for (i=0; i<nsects; i++) if (sects[i]->head) {
1018 p += strlen(p)+1;
1019 elf_section_header (p - shstrtab, 9, 0, sects[i]->rel, TRUE,
1020 sects[i]->rellen, nsects+3, i+1, 4, 8);
1022 if (of_elf.current_dfmt) {
1023 /* for debugging information, create the last three sections
1024 which are the .stab , .stabstr and .rel.stab sections respectively */
1026 /* this function call creates the stab sections in memory */
1027 stabs_generate();
1029 if ((stabbuf)&&(stabstrbuf)&&(stabrelbuf)) {
1030 p += strlen(p)+1;
1031 elf_section_header(p-shstrtab,1,0,stabbuf,0,stablen,nsections-2,0,4,12);
1033 p += strlen(p)+1;
1034 elf_section_header(p-shstrtab,3,0,stabstrbuf,0,stabstrlen,0,0,4,0);
1036 p += strlen(p)+1;
1037 /* link -> symtable info -> section to refer to */
1038 elf_section_header(p-shstrtab,9,0,stabrelbuf,0,stabrellen,symtabsection,nsections-3,4,8);
1041 fwrite (align_str, align, 1, elffp);
1044 * Now output the sections.
1046 elf_write_sections();
1048 nasm_free (elf_sects);
1049 saa_free (symtab);
1052 static struct SAA *elf_build_symtab (long *len, long *local)
1054 struct SAA *s = saa_init(1L);
1055 struct Symbol *sym;
1056 unsigned char entry[16], *p;
1057 int i;
1059 *len = *local = 0;
1062 * First, an all-zeros entry, required by the ELF spec.
1064 saa_wbytes (s, NULL, 16L); /* null symbol table entry */
1065 *len += 16;
1066 (*local)++;
1069 * Next, an entry for the file name.
1071 p = entry;
1072 WRITELONG (p, 1); /* we know it's 1st thing in strtab */
1073 WRITELONG (p, 0); /* no value */
1074 WRITELONG (p, 0); /* no size either */
1075 WRITESHORT (p, 4); /* type FILE */
1076 WRITESHORT (p, SHN_ABS);
1077 saa_wbytes (s, entry, 16L);
1078 *len += 16;
1079 (*local)++;
1082 * Now some standard symbols defining the segments, for relocation
1083 * purposes.
1085 for (i = 1; i <= nsects+1; i++) {
1086 p = entry;
1087 WRITELONG (p, 0); /* no symbol name */
1088 WRITELONG (p, 0); /* offset zero */
1089 WRITELONG (p, 0); /* size zero */
1090 WRITESHORT (p, 3); /* local section-type thing */
1091 WRITESHORT (p, (i==1 ? SHN_ABS : i-1)); /* the section id */
1092 saa_wbytes (s, entry, 16L);
1093 *len += 16;
1094 (*local)++;
1098 * Now the other local symbols.
1100 saa_rewind (syms);
1101 while ( (sym = saa_rstruct (syms)) ) {
1102 if (sym->type & SYM_GLOBAL)
1103 continue;
1104 p = entry;
1105 WRITELONG (p, sym->strpos);
1106 WRITELONG (p, sym->value);
1107 WRITELONG (p, sym->size);
1108 WRITESHORT (p, sym->type); /* local non-typed thing */
1109 WRITESHORT (p, sym->section);
1110 saa_wbytes (s, entry, 16L);
1111 *len += 16;
1112 (*local)++;
1116 * Now the global symbols.
1118 saa_rewind (syms);
1119 while ( (sym = saa_rstruct (syms)) ) {
1120 if (!(sym->type & SYM_GLOBAL))
1121 continue;
1122 p = entry;
1123 WRITELONG (p, sym->strpos);
1124 WRITELONG (p, sym->value);
1125 WRITELONG (p, sym->size);
1126 WRITESHORT (p, sym->type); /* global non-typed thing */
1127 WRITESHORT (p, sym->section);
1128 saa_wbytes (s, entry, 16L);
1129 *len += 16;
1132 return s;
1135 static struct SAA *elf_build_reltab (long *len, struct Reloc *r) {
1136 struct SAA *s;
1137 unsigned char *p, entry[8];
1139 if (!r)
1140 return NULL;
1142 s = saa_init(1L);
1143 *len = 0;
1145 while (r) {
1146 long sym = r->symbol;
1148 if (sym >= GLOBAL_TEMP_BASE)
1149 sym += -GLOBAL_TEMP_BASE + (nsects+3) + nlocals;
1151 p = entry;
1152 WRITELONG (p, r->address);
1153 WRITELONG (p, (sym << 8) + r->type);
1154 saa_wbytes (s, entry, 8L);
1155 *len += 8;
1157 r = r->next;
1160 return s;
1163 static void elf_section_header (int name, int type, int flags,
1164 void *data, int is_saa, long datalen,
1165 int link, int info, int align, int eltsize)
1167 elf_sects[elf_nsect].data = data;
1168 elf_sects[elf_nsect].len = datalen;
1169 elf_sects[elf_nsect].is_saa = is_saa;
1170 elf_nsect++;
1172 fwritelong ((long)name, elffp);
1173 fwritelong ((long)type, elffp);
1174 fwritelong ((long)flags, elffp);
1175 fwritelong (0L, elffp); /* no address, ever, in object files */
1176 fwritelong (type == 0 ? 0L : elf_foffs, elffp);
1177 fwritelong (datalen, elffp);
1178 if (data)
1179 elf_foffs += (datalen+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1180 fwritelong ((long)link, elffp);
1181 fwritelong ((long)info, elffp);
1182 fwritelong ((long)align, elffp);
1183 fwritelong ((long)eltsize, elffp);
1186 static void elf_write_sections (void)
1188 int i;
1189 for (i = 0; i < elf_nsect; i++)
1190 if (elf_sects[i].data) {
1191 long len = elf_sects[i].len;
1192 long reallen = (len+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1193 long align = reallen - len;
1194 if (elf_sects[i].is_saa)
1195 saa_fpwrite (elf_sects[i].data, elffp);
1196 else
1197 fwrite (elf_sects[i].data, len, 1, elffp);
1198 fwrite (align_str, align, 1, elffp);
1202 static void elf_sect_write (struct Section *sect,
1203 const unsigned char *data, unsigned long len)
1205 saa_wbytes (sect->data, data, len);
1206 sect->len += len;
1209 static long elf_segbase (long segment)
1211 return segment;
1214 static int elf_directive (char *directive, char *value, int pass)
1216 return 0;
1219 static void elf_filename (char *inname, char *outname, efunc error)
1221 strcpy(elf_module, inname);
1222 standard_extension (inname, outname, ".o", error);
1225 static const char *elf_stdmac[] = {
1226 "%define __SECT__ [section .text]",
1227 "%macro __NASM_CDecl__ 1",
1228 "%define $_%1 $%1",
1229 "%endmacro",
1230 NULL
1232 static int elf_set_info(enum geninfo type, char **val)
1234 return 0;
1237 static struct dfmt df_stabs = {
1238 "ELF32 (i386) stabs debug format for Linux",
1239 "stabs",
1240 stabs_init,
1241 stabs_linenum,
1242 stabs_deflabel,
1243 stabs_directive,
1244 stabs_typevalue,
1245 stabs_output,
1246 stabs_cleanup
1249 struct dfmt *elf_debugs_arr[2]={&df_stabs,NULL};
1251 struct ofmt of_elf = {
1252 "ELF32 (i386) object files (e.g. Linux)",
1253 "elf",
1254 NULL,
1255 elf_debugs_arr,
1256 &null_debug_form,
1257 elf_stdmac,
1258 elf_init,
1259 elf_set_info,
1260 elf_out,
1261 elf_deflabel,
1262 elf_section_names,
1263 elf_segbase,
1264 elf_directive,
1265 elf_filename,
1266 elf_cleanup
1269 /* again, the stabs debugging stuff (code) */
1271 void stabs_init(struct ofmt *of,void *id,FILE *fp,efunc error) {
1274 void stabs_linenum(const char *filename,long linenumber,long segto) {
1275 if (!stabs_filename) {
1276 stabs_filename = (char *)malloc(strlen(filename)+1);
1277 strcpy(stabs_filename,filename);
1278 } else {
1279 if (strcmp(stabs_filename,filename)) {
1280 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1281 in fact, this leak comes in quite handy to maintain a list of files
1282 encountered so far in the symbol lines... */
1283 stabs_filename = (char *)malloc(strlen(filename)+1);
1284 strcpy(stabs_filename,filename);
1287 stabs_immcall=1;
1288 currentline=linenumber;
1291 void stabs_deflabel(char *name,long segment,long offset,int is_global,char *special) {
1294 void stabs_directive(const char *directive,const char *params) {
1297 void stabs_typevalue(long type) {
1300 void stabs_output(int type,void *param) {
1301 struct symlininfo *s;
1302 struct linelist *el;
1303 if (type==TY_STABSSYMLIN) {
1304 if (stabs_immcall) {
1305 s=(struct symlininfo *)param;
1306 if (strcmp( s->name,".text")) return; /* we are only interested in the text stuff */
1307 numlinestabs++;
1308 el=(struct linelist *)malloc(sizeof(struct linelist));
1309 el->info.offset=s->offset;
1310 el->info.section=s->section;
1311 el->info.name=s->name;
1312 el->line=currentline;
1313 el->filename=stabs_filename;
1314 el->next=0;
1315 if (stabslines) {
1316 stabslines->last->next=el;
1317 stabslines->last=el;
1318 } else {
1319 stabslines=el;
1320 stabslines->last=el;
1324 stabs_immcall=0;
1328 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1330 void stabs_generate() {
1331 int i,numfiles,strsize,numstabs=0,currfile,mainfileindex;
1332 struct erel rentry;
1333 unsigned char *sbuf,*ssbuf,*rbuf,*sptr,*rptr;
1334 char **allfiles;
1335 int *fileidx;
1337 struct linelist *ptr;
1338 struct stabentry stab;
1340 ptr=stabslines;
1342 allfiles = (char **)malloc(numlinestabs*sizeof(char *));
1343 for (i=0;i<numlinestabs;i++) allfiles[i]=0;
1344 numfiles=0;
1345 while (ptr) {
1346 if (numfiles==0) {
1347 allfiles[0]=ptr->filename;
1348 numfiles++;
1349 } else {
1350 for (i=0;i<numfiles;i++) {
1351 if (!strcmp(allfiles[i],ptr->filename)) break;
1353 if (i>=numfiles) {
1354 allfiles[i]=ptr->filename;
1355 numfiles++;
1358 ptr=ptr->next;
1360 strsize=1;
1361 fileidx = (int *)malloc(numfiles*sizeof(int));
1362 for (i=0;i<numfiles;i++) {
1363 fileidx[i]=strsize;
1364 strsize+=strlen(allfiles[i])+1;
1366 mainfileindex=0;
1367 for (i=0;i<numfiles;i++) {
1368 if (!strcmp(allfiles[i],elf_module)) {
1369 mainfileindex=i;
1370 break;
1375 /* worst case size of the stab buffer would be:
1376 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1378 sbuf = (unsigned char *)malloc((numlinestabs*2+3)*sizeof(struct stabentry));
1380 ssbuf = (unsigned char *)malloc(strsize);
1382 rbuf = (unsigned char *)malloc(numlinestabs*8*(2+3));
1383 rptr=rbuf;
1385 for (i=0;i<numfiles;i++) {
1386 strcpy(ssbuf+fileidx[i],allfiles[i]);
1388 ssbuf[0]=0;
1390 stabstrlen = strsize; /* set global variable for length of stab strings */
1392 sptr=sbuf;
1393 /* this is the first stab, its strx points to the filename of the
1394 the source-file, the n_desc field should be set to the number
1395 of remaining stabs
1397 stab.n_strx = fileidx[0];
1398 stab.n_type=0;
1399 stab.n_other=0;
1400 stab.n_desc=0; /* # of remaining stabs (to be determined later) */
1401 stab.n_value = strlen(allfiles[0])+1;
1402 memcpy(sptr,&stab,sizeof(struct stabentry));
1403 sptr+=sizeof(struct stabentry);
1405 ptr=stabslines;
1406 for (i=0;i<numfiles;i++) if (!strcmp(allfiles[i],ptr->filename)) break;
1407 currfile=i;
1409 /* this is the stab for the main source file */
1410 stab.n_strx = fileidx[mainfileindex];
1411 stab.n_type=N_SO;
1412 stab.n_other=0;
1413 stab.n_desc=0;
1414 stab.n_value = 0; /* strlen(allfiles[mainfileindex])+1; */
1415 memcpy(sptr,&stab,sizeof(struct stabentry));
1416 /* relocation stuff */
1417 rentry.offset=(sptr-sbuf)+8;
1418 /* the following section+3 assumption relies on the following order of output sections:
1419 0 -> dummy
1420 1 -> .text
1421 2 -> .data
1422 3 -> .comment
1423 4 -> .strtab
1424 ... this is an ugly hack and should be improved in the near future
1426 rentry.info=((ptr->info.section+3)<<8)|R_386_32;
1427 memcpy(rptr,&rentry,8);
1428 rptr+=8;
1429 sptr+=sizeof(struct stabentry);
1431 numstabs=1;
1432 currfile=mainfileindex;
1434 while (ptr) {
1435 if (strcmp(allfiles[currfile],ptr->filename)) {
1436 /* oops file has changed... */
1437 for (i=0;i<numfiles;i++) if (!strcmp(allfiles[i],ptr->filename)) break;
1438 currfile=i;
1439 stab.n_strx = fileidx[currfile];
1440 stab.n_type=N_SOL;
1441 stab.n_other=0;
1442 stab.n_desc=0;
1443 /* stab.n_value = strlen(allfiles[currfile])+1; */
1444 stab.n_value = ptr->info.offset;
1445 memcpy(sptr,&stab,sizeof(struct stabentry));
1446 /* relocation stuff */
1447 rentry.offset=(sptr-sbuf)+8;
1448 rentry.info=((ptr->info.section+3)<<8)|R_386_32;
1449 memcpy(rptr,&rentry,8);
1450 rptr+=8;
1451 sptr+=sizeof(struct stabentry);
1452 numstabs++;
1454 stab.n_strx=0;
1455 stab.n_type=N_SLINE;
1456 stab.n_other=0;
1457 stab.n_desc=ptr->line;
1458 stab.n_value=ptr->info.offset;
1459 memcpy(sptr,&stab,sizeof(struct stabentry));
1460 numstabs++;
1462 /* relocation stuff */
1463 rentry.offset=(sptr-sbuf)+8;
1464 rentry.info=((ptr->info.section+3)<<8)|R_386_32;
1465 memcpy(rptr,&rentry,8);
1466 rptr+=8;
1468 sptr+=sizeof(struct stabentry);
1469 ptr=ptr->next;
1474 ((struct stabentry *)sbuf)->n_desc=numstabs;
1476 free(allfiles);
1477 free(fileidx);
1479 stablen = (sptr-sbuf);
1480 stabrellen=(rptr-rbuf);
1481 stabrelbuf= rbuf;
1482 stabbuf = sbuf;
1483 stabstrbuf = ssbuf;
1486 void stabs_cleanup() {
1487 struct linelist *ptr,*del;
1488 if (!stabslines) return;
1489 ptr=stabslines;
1490 while (ptr) {
1491 del=ptr;
1492 ptr=ptr->next;
1493 free(del);
1495 if (stabbuf) free(stabbuf);
1496 if (stabrelbuf) free(stabrelbuf);
1497 if (stabstrbuf) free(stabstrbuf);
1500 #endif /* OF_ELF */