attempt to make static makefiles aware of outelf32/outelf64
[nasm/nasm.git] / output / outelf32.c
blob6c16645aa0f6f812d5259d5134e8ed16ed719ba8
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>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "outform.h"
20 #ifdef OF_ELF32
23 * Relocation types.
25 enum reloc_type {
26 R_386_32 = 1, /* ordinary absolute relocation */
27 R_386_PC32 = 2, /* PC-relative relocation */
28 R_386_GOT32 = 3, /* an offset into GOT */
29 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
30 R_386_COPY = 5, /* ??? */
31 R_386_GLOB_DAT = 6, /* ??? */
32 R_386_JUMP_SLOT = 7, /* ??? */
33 R_386_RELATIVE = 8, /* ??? */
34 R_386_GOTOFF = 9, /* an offset from GOT base */
35 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
36 /* These are GNU extensions, but useful */
37 R_386_16 = 20, /* A 16-bit absolute relocation */
38 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
39 R_386_8 = 22, /* An 8-bit absolute relocation */
40 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
43 struct Reloc {
44 struct Reloc *next;
45 int32_t address; /* relative to _start_ of section */
46 int32_t symbol; /* ELF symbol info thingy */
47 int type; /* type of relocation */
50 struct Symbol {
51 int32_t strpos; /* string table position of name */
52 int32_t section; /* section ID of the symbol */
53 int type; /* symbol type */
54 int other; /* symbol visibility */
55 int32_t value; /* address, or COMMON variable align */
56 int32_t size; /* size of symbol */
57 int32_t globnum; /* symbol table offset if global */
58 struct Symbol *next; /* list of globals in each section */
59 struct Symbol *nextfwd; /* list of unresolved-size symbols */
60 char *name; /* used temporarily if in above list */
63 #define SHT_PROGBITS 1
64 #define SHT_NOBITS 8
66 #define SHF_WRITE 1
67 #define SHF_ALLOC 2
68 #define SHF_EXECINSTR 4
70 struct Section {
71 struct SAA *data;
72 uint32_t len, size, nrelocs;
73 int32_t index;
74 int type; /* SHT_PROGBITS or SHT_NOBITS */
75 int align; /* alignment: power of two */
76 uint32_t flags; /* section flags */
77 char *name;
78 struct SAA *rel;
79 int32_t rellen;
80 struct Reloc *head, **tail;
81 struct Symbol *gsyms; /* global symbols in section */
84 #define SECT_DELTA 32
85 static struct Section **sects;
86 static int nsects, sectlen;
88 #define SHSTR_DELTA 256
89 static char *shstrtab;
90 static int shstrtablen, shstrtabsize;
92 static struct SAA *syms;
93 static uint32_t nlocals, nglobs;
95 static int32_t def_seg;
97 static struct RAA *bsym;
99 static struct SAA *strs;
100 static uint32_t strslen;
102 static FILE *elffp;
103 static efunc error;
104 static evalfunc evaluate;
106 static struct Symbol *fwds;
108 static char elf_module[FILENAME_MAX];
110 extern struct ofmt of_elf32;
111 extern struct ofmt of_elf;
113 #define SHN_ABS 0xFFF1
114 #define SHN_COMMON 0xFFF2
115 #define SHN_UNDEF 0
117 #define SYM_SECTION 0x04
118 #define SYM_GLOBAL 0x10
119 #define SYM_NOTYPE 0x00
120 #define SYM_DATA 0x01
121 #define SYM_FUNCTION 0x02
123 #define STV_DEFAULT 0
124 #define STV_INTERNAL 1
125 #define STV_HIDDEN 2
126 #define STV_PROTECTED 3
128 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
130 #define SEG_ALIGN 16 /* alignment of sections in file */
131 #define SEG_ALIGN_1 (SEG_ALIGN-1)
133 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
135 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
136 static struct ELF_SECTDATA {
137 void *data;
138 int32_t len;
139 int is_saa;
140 } *elf_sects;
141 static int elf_nsect;
142 static int32_t elf_foffs;
144 static void elf_write(void);
145 static void elf_sect_write(struct Section *, const uint8_t *,
146 uint32_t);
147 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
148 int, int);
149 static void elf_write_sections(void);
150 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
151 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
152 static void add_sectname(char *, char *);
154 /* this stuff is needed for the stabs debugging format */
155 #define N_SO 0x64 /* ID for main source file */
156 #define N_SOL 0x84 /* ID for sub-source file */
157 #define N_BINCL 0x82
158 #define N_EINCL 0xA2
159 #define N_SLINE 0x44
160 #define TY_STABSSYMLIN 0x40 /* ouch */
162 struct stabentry {
163 uint32_t n_strx;
164 uint8_t n_type;
165 uint8_t n_other;
166 uint16_t n_desc;
167 uint32_t n_value;
170 struct erel {
171 int offset, info;
174 struct symlininfo {
175 int offset;
176 int section; /* section index */
177 char *name; /* shallow-copied pointer of section name */
180 struct linelist {
181 struct symlininfo info;
182 int line;
183 char *filename;
184 struct linelist *next;
185 struct linelist *last;
188 static struct linelist *stabslines = 0;
189 static int stabs_immcall = 0;
190 static int currentline = 0;
191 static int numlinestabs = 0;
192 static char *stabs_filename = 0;
193 static int symtabsection;
194 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
195 static int stablen, stabstrlen, stabrellen;
197 static struct dfmt df_stabs;
199 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
200 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
201 void stabs32_deflabel(char *, int32_t, int32_t, int, char *);
202 void stabs32_directive(const char *, const char *);
203 void stabs32_typevalue(int32_t);
204 void stabs32_output(int, void *);
205 void stabs32_generate();
206 void stabs32_cleanup();
208 /* end of stabs debugging stuff */
211 * Special section numbers which are used to define ELF special
212 * symbols, which can be used with WRT to provide PIC relocation
213 * types.
215 static int32_t elf_gotpc_sect, elf_gotoff_sect;
216 static int32_t elf_got_sect, elf_plt_sect;
217 static int32_t elf_sym_sect;
219 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
221 elffp = fp;
222 error = errfunc;
223 evaluate = eval;
224 (void)ldef; /* placate optimisers */
225 sects = NULL;
226 nsects = sectlen = 0;
227 syms = saa_init((int32_t)sizeof(struct Symbol));
228 nlocals = nglobs = 0;
229 bsym = raa_init();
230 strs = saa_init(1L);
231 saa_wbytes(strs, "\0", 1L);
232 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
233 strslen = 2 + strlen(elf_module);
234 shstrtab = NULL;
235 shstrtablen = shstrtabsize = 0;;
236 add_sectname("", "");
238 fwds = NULL;
240 elf_gotpc_sect = seg_alloc();
241 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
242 error);
243 elf_gotoff_sect = seg_alloc();
244 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
245 error);
246 elf_got_sect = seg_alloc();
247 ldef("..got", elf_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
248 error);
249 elf_plt_sect = seg_alloc();
250 ldef("..plt", elf_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
251 error);
252 elf_sym_sect = seg_alloc();
253 ldef("..sym", elf_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
254 error);
256 def_seg = seg_alloc();
259 static void elf_cleanup(int debuginfo)
261 struct Reloc *r;
262 int i;
264 (void)debuginfo;
266 elf_write();
267 fclose(elffp);
268 for (i = 0; i < nsects; i++) {
269 if (sects[i]->type != SHT_NOBITS)
270 saa_free(sects[i]->data);
271 if (sects[i]->head)
272 saa_free(sects[i]->rel);
273 while (sects[i]->head) {
274 r = sects[i]->head;
275 sects[i]->head = sects[i]->head->next;
276 nasm_free(r);
279 nasm_free(sects);
280 saa_free(syms);
281 raa_free(bsym);
282 saa_free(strs);
283 if (of_elf32.current_dfmt) {
284 of_elf32.current_dfmt->cleanup();
288 static void add_sectname(char *firsthalf, char *secondhalf)
290 int len = strlen(firsthalf) + strlen(secondhalf);
291 while (shstrtablen + len + 1 > shstrtabsize)
292 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
293 strcpy(shstrtab + shstrtablen, firsthalf);
294 strcat(shstrtab + shstrtablen, secondhalf);
295 shstrtablen += len + 1;
298 static int elf_make_section(char *name, int type, int flags, int align)
300 struct Section *s;
302 s = nasm_malloc(sizeof(*s));
304 if (type != SHT_NOBITS)
305 s->data = saa_init(1L);
306 s->head = NULL;
307 s->tail = &s->head;
308 s->len = s->size = 0;
309 s->nrelocs = 0;
310 if (!strcmp(name, ".text"))
311 s->index = def_seg;
312 else
313 s->index = seg_alloc();
314 add_sectname("", name);
315 s->name = nasm_malloc(1 + strlen(name));
316 strcpy(s->name, name);
317 s->type = type;
318 s->flags = flags;
319 s->align = align;
320 s->gsyms = NULL;
322 if (nsects >= sectlen)
323 sects =
324 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
325 sects[nsects++] = s;
327 return nsects - 1;
330 static int32_t elf_section_names(char *name, int pass, int *bits)
332 char *p;
333 int flags_and, flags_or, type, align, i;
336 * Default is 32 bits.
338 if (!name) {
339 *bits = 32;
340 return def_seg;
343 p = name;
344 while (*p && !isspace(*p))
345 p++;
346 if (*p)
347 *p++ = '\0';
348 flags_and = flags_or = type = align = 0;
350 while (*p && isspace(*p))
351 p++;
352 while (*p) {
353 char *q = p;
354 while (*p && !isspace(*p))
355 p++;
356 if (*p)
357 *p++ = '\0';
358 while (*p && isspace(*p))
359 p++;
361 if (!nasm_strnicmp(q, "align=", 6)) {
362 align = atoi(q + 6);
363 if (align == 0)
364 align = 1;
365 if ((align - 1) & align) { /* means it's not a power of two */
366 error(ERR_NONFATAL, "section alignment %d is not"
367 " a power of two", align);
368 align = 1;
370 } else if (!nasm_stricmp(q, "alloc")) {
371 flags_and |= SHF_ALLOC;
372 flags_or |= SHF_ALLOC;
373 } else if (!nasm_stricmp(q, "noalloc")) {
374 flags_and |= SHF_ALLOC;
375 flags_or &= ~SHF_ALLOC;
376 } else if (!nasm_stricmp(q, "exec")) {
377 flags_and |= SHF_EXECINSTR;
378 flags_or |= SHF_EXECINSTR;
379 } else if (!nasm_stricmp(q, "noexec")) {
380 flags_and |= SHF_EXECINSTR;
381 flags_or &= ~SHF_EXECINSTR;
382 } else if (!nasm_stricmp(q, "write")) {
383 flags_and |= SHF_WRITE;
384 flags_or |= SHF_WRITE;
385 } else if (!nasm_stricmp(q, "nowrite")) {
386 flags_and |= SHF_WRITE;
387 flags_or &= ~SHF_WRITE;
388 } else if (!nasm_stricmp(q, "progbits")) {
389 type = SHT_PROGBITS;
390 } else if (!nasm_stricmp(q, "nobits")) {
391 type = SHT_NOBITS;
395 if (!strcmp(name, ".comment") ||
396 !strcmp(name, ".shstrtab") ||
397 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
398 error(ERR_NONFATAL, "attempt to redefine reserved section"
399 "name `%s'", name);
400 return NO_SEG;
403 for (i = 0; i < nsects; i++)
404 if (!strcmp(name, sects[i]->name))
405 break;
406 if (i == nsects) {
407 if (!strcmp(name, ".text"))
408 i = elf_make_section(name, SHT_PROGBITS,
409 SHF_ALLOC | SHF_EXECINSTR, 16);
410 else if (!strcmp(name, ".rodata"))
411 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
412 else if (!strcmp(name, ".data"))
413 i = elf_make_section(name, SHT_PROGBITS,
414 SHF_ALLOC | SHF_WRITE, 4);
415 else if (!strcmp(name, ".bss"))
416 i = elf_make_section(name, SHT_NOBITS,
417 SHF_ALLOC | SHF_WRITE, 4);
418 else
419 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
420 if (type)
421 sects[i]->type = type;
422 if (align)
423 sects[i]->align = align;
424 sects[i]->flags &= ~flags_and;
425 sects[i]->flags |= flags_or;
426 } else if (pass == 1) {
427 if (type || align || flags_and)
428 error(ERR_WARNING, "section attributes ignored on"
429 " redeclaration of section `%s'", name);
432 return sects[i]->index;
435 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
436 int is_global, char *special)
438 int pos = strslen;
439 struct Symbol *sym;
440 int special_used = FALSE;
442 #if defined(DEBUG) && DEBUG>2
443 fprintf(stderr,
444 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
445 name, segment, offset, is_global, special);
446 #endif
447 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
449 * This is a NASM special symbol. We never allow it into
450 * the ELF symbol table, even if it's a valid one. If it
451 * _isn't_ a valid one, we should barf immediately.
453 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
454 strcmp(name, "..got") && strcmp(name, "..plt") &&
455 strcmp(name, "..sym"))
456 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
457 return;
460 if (is_global == 3) {
461 struct Symbol **s;
463 * Fix up a forward-reference symbol size from the first
464 * pass.
466 for (s = &fwds; *s; s = &(*s)->nextfwd)
467 if (!strcmp((*s)->name, name)) {
468 struct tokenval tokval;
469 expr *e;
470 char *p = special;
472 while (*p && !isspace(*p))
473 p++;
474 while (*p && isspace(*p))
475 p++;
476 stdscan_reset();
477 stdscan_bufptr = p;
478 tokval.t_type = TOKEN_INVALID;
479 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
480 if (e) {
481 if (!is_simple(e))
482 error(ERR_NONFATAL, "cannot use relocatable"
483 " expression as symbol size");
484 else
485 (*s)->size = reloc_value(e);
489 * Remove it from the list of unresolved sizes.
491 nasm_free((*s)->name);
492 *s = (*s)->nextfwd;
493 return;
495 return; /* it wasn't an important one */
498 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
499 strslen += 1 + strlen(name);
501 sym = saa_wstruct(syms);
503 sym->strpos = pos;
504 sym->type = is_global ? SYM_GLOBAL : 0;
505 sym->other = STV_DEFAULT;
506 sym->size = 0;
507 if (segment == NO_SEG)
508 sym->section = SHN_ABS;
509 else {
510 int i;
511 sym->section = SHN_UNDEF;
512 if (nsects == 0 && segment == def_seg) {
513 int tempint;
514 if (segment != elf_section_names(".text", 2, &tempint))
515 error(ERR_PANIC,
516 "strange segment conditions in ELF driver");
517 sym->section = nsects;
518 } else {
519 for (i = 0; i < nsects; i++)
520 if (segment == sects[i]->index) {
521 sym->section = i + 1;
522 break;
527 if (is_global == 2) {
528 sym->size = offset;
529 sym->value = 0;
530 sym->section = SHN_COMMON;
532 * We have a common variable. Check the special text to see
533 * if it's a valid number and power of two; if so, store it
534 * as the alignment for the common variable.
536 if (special) {
537 int err;
538 sym->value = readnum(special, &err);
539 if (err)
540 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
541 " valid number", special);
542 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
543 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
544 " power of two", special);
546 special_used = TRUE;
547 } else
548 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
550 if (sym->type == SYM_GLOBAL) {
552 * If sym->section == SHN_ABS, then the first line of the
553 * else section would cause a core dump, because its a reference
554 * beyond the end of the section array.
555 * This behaviour is exhibited by this code:
556 * GLOBAL crash_nasm
557 * crash_nasm equ 0
558 * To avoid such a crash, such requests are silently discarded.
559 * This may not be the best solution.
561 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
562 bsym = raa_write(bsym, segment, nglobs);
563 } else if (sym->section != SHN_ABS) {
565 * This is a global symbol; so we must add it to the linked
566 * list of global symbols in its section. We'll push it on
567 * the beginning of the list, because it doesn't matter
568 * much which end we put it on and it's easier like this.
570 * In addition, we check the special text for symbol
571 * type and size information.
573 sym->next = sects[sym->section - 1]->gsyms;
574 sects[sym->section - 1]->gsyms = sym;
576 if (special) {
577 int n = strcspn(special, " \t");
579 if (!nasm_strnicmp(special, "function", n))
580 sym->type |= SYM_FUNCTION;
581 else if (!nasm_strnicmp(special, "data", n) ||
582 !nasm_strnicmp(special, "object", n))
583 sym->type |= SYM_DATA;
584 else if (!nasm_strnicmp(special, "notype", n))
585 sym->type |= SYM_NOTYPE;
586 else
587 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
588 n, special);
589 special += n;
591 while (isspace(*special))
592 ++special;
593 if (*special) {
594 n = strcspn(special, " \t");
595 if (!nasm_strnicmp(special, "default", n))
596 sym->other = STV_DEFAULT;
597 else if (!nasm_strnicmp(special, "internal", n))
598 sym->other = STV_INTERNAL;
599 else if (!nasm_strnicmp(special, "hidden", n))
600 sym->other = STV_HIDDEN;
601 else if (!nasm_strnicmp(special, "protected", n))
602 sym->other = STV_PROTECTED;
603 else
604 n = 0;
605 special += n;
608 if (*special) {
609 struct tokenval tokval;
610 expr *e;
611 int fwd = FALSE;
612 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
614 while (special[n] && isspace(special[n]))
615 n++;
617 * We have a size expression; attempt to
618 * evaluate it.
620 stdscan_reset();
621 stdscan_bufptr = special + n;
622 tokval.t_type = TOKEN_INVALID;
623 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
624 NULL);
625 if (fwd) {
626 sym->nextfwd = fwds;
627 fwds = sym;
628 sym->name = nasm_strdup(name);
629 } else if (e) {
630 if (!is_simple(e))
631 error(ERR_NONFATAL, "cannot use relocatable"
632 " expression as symbol size");
633 else
634 sym->size = reloc_value(e);
636 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
638 special_used = TRUE;
641 sym->globnum = nglobs;
642 nglobs++;
643 } else
644 nlocals++;
646 if (special && !special_used)
647 error(ERR_NONFATAL, "no special symbol features supported here");
650 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
652 struct Reloc *r;
654 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
655 sect->tail = &r->next;
656 r->next = NULL;
658 r->address = sect->len;
659 if (segment == NO_SEG)
660 r->symbol = 2;
661 else {
662 int i;
663 r->symbol = 0;
664 for (i = 0; i < nsects; i++)
665 if (segment == sects[i]->index)
666 r->symbol = i + 3;
667 if (!r->symbol)
668 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
670 r->type = type;
672 sect->nrelocs++;
676 * This routine deals with ..got and ..sym relocations: the more
677 * complicated kinds. In shared-library writing, some relocations
678 * with respect to global symbols must refer to the precise symbol
679 * rather than referring to an offset from the base of the section
680 * _containing_ the symbol. Such relocations call to this routine,
681 * which searches the symbol list for the symbol in question.
683 * R_386_GOT32 references require the _exact_ symbol address to be
684 * used; R_386_32 references can be at an offset from the symbol.
685 * The boolean argument `exact' tells us this.
687 * Return value is the adjusted value of `addr', having become an
688 * offset from the symbol rather than the section. Should always be
689 * zero when returning from an exact call.
691 * Limitation: if you define two symbols at the same place,
692 * confusion will occur.
694 * Inefficiency: we search, currently, using a linked list which
695 * isn't even necessarily sorted.
697 static int32_t elf_add_gsym_reloc(struct Section *sect,
698 int32_t segment, int32_t offset,
699 int type, int exact)
701 struct Reloc *r;
702 struct Section *s;
703 struct Symbol *sym, *sm;
704 int i;
707 * First look up the segment/offset pair and find a global
708 * symbol corresponding to it. If it's not one of our segments,
709 * then it must be an external symbol, in which case we're fine
710 * doing a normal elf_add_reloc after first sanity-checking
711 * that the offset from the symbol is zero.
713 s = NULL;
714 for (i = 0; i < nsects; i++)
715 if (segment == sects[i]->index) {
716 s = sects[i];
717 break;
719 if (!s) {
720 if (exact && offset != 0)
721 error(ERR_NONFATAL, "unable to find a suitable global symbol"
722 " for this reference");
723 else
724 elf_add_reloc(sect, segment, type);
725 return offset;
728 if (exact) {
730 * Find a symbol pointing _exactly_ at this one.
732 for (sym = s->gsyms; sym; sym = sym->next)
733 if (sym->value == offset)
734 break;
735 } else {
737 * Find the nearest symbol below this one.
739 sym = NULL;
740 for (sm = s->gsyms; sm; sm = sm->next)
741 if (sm->value <= offset && (!sym || sm->value > sym->value))
742 sym = sm;
744 if (!sym && exact) {
745 error(ERR_NONFATAL, "unable to find a suitable global symbol"
746 " for this reference");
747 return 0;
750 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
751 sect->tail = &r->next;
752 r->next = NULL;
754 r->address = sect->len;
755 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
756 r->type = type;
758 sect->nrelocs++;
760 return offset - sym->value;
763 static void elf_out(int32_t segto, const void *data, uint32_t type,
764 int32_t segment, int32_t wrt)
766 struct Section *s;
767 int32_t realbytes = type & OUT_SIZMASK;
768 int32_t addr;
769 uint8_t mydata[4], *p;
770 int i;
771 static struct symlininfo sinfo;
773 type &= OUT_TYPMASK;
776 * handle absolute-assembly (structure definitions)
778 if (segto == NO_SEG) {
779 if (type != OUT_RESERVE)
780 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
781 " space");
782 return;
785 s = NULL;
786 for (i = 0; i < nsects; i++)
787 if (segto == sects[i]->index) {
788 s = sects[i];
789 break;
791 if (!s) {
792 int tempint; /* ignored */
793 if (segto != elf_section_names(".text", 2, &tempint))
794 error(ERR_PANIC, "strange segment conditions in ELF driver");
795 else {
796 s = sects[nsects - 1];
797 i = nsects - 1;
801 /* again some stabs debugging stuff */
802 if (of_elf32.current_dfmt) {
803 sinfo.offset = s->len;
804 sinfo.section = i;
805 sinfo.name = s->name;
806 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
808 /* end of debugging stuff */
810 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
811 error(ERR_WARNING, "attempt to initialize memory in"
812 " BSS section `%s': ignored", s->name);
813 if (type == OUT_REL2ADR)
814 realbytes = 2;
815 else if (type == OUT_REL4ADR)
816 realbytes = 4;
817 s->len += realbytes;
818 return;
821 if (type == OUT_RESERVE) {
822 if (s->type == SHT_PROGBITS) {
823 error(ERR_WARNING, "uninitialized space declared in"
824 " non-BSS section `%s': zeroing", s->name);
825 elf_sect_write(s, NULL, realbytes);
826 } else
827 s->len += realbytes;
828 } else if (type == OUT_RAWDATA) {
829 if (segment != NO_SEG)
830 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
831 elf_sect_write(s, data, realbytes);
832 } else if (type == OUT_ADDRESS) {
833 int gnu16 = 0;
834 addr = *(int32_t *)data;
835 if (segment != NO_SEG) {
836 if (segment % 2) {
837 error(ERR_NONFATAL, "ELF format does not support"
838 " segment base references");
839 } else {
840 if (wrt == NO_SEG) {
841 if (realbytes == 2) {
842 gnu16 = 1;
843 elf_add_reloc(s, segment, R_386_16);
844 } else {
845 elf_add_reloc(s, segment, R_386_32);
847 } else if (wrt == elf_gotpc_sect + 1) {
849 * The user will supply GOT relative to $$. ELF
850 * will let us have GOT relative to $. So we
851 * need to fix up the data item by $-$$.
853 addr += s->len;
854 elf_add_reloc(s, segment, R_386_GOTPC);
855 } else if (wrt == elf_gotoff_sect + 1) {
856 elf_add_reloc(s, segment, R_386_GOTOFF);
857 } else if (wrt == elf_got_sect + 1) {
858 addr = elf_add_gsym_reloc(s, segment, addr,
859 R_386_GOT32, TRUE);
860 } else if (wrt == elf_sym_sect + 1) {
861 if (realbytes == 2) {
862 gnu16 = 1;
863 addr = elf_add_gsym_reloc(s, segment, addr,
864 R_386_16, FALSE);
865 } else {
866 addr = elf_add_gsym_reloc(s, segment, addr,
867 R_386_32, FALSE);
869 } else if (wrt == elf_plt_sect + 1) {
870 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
871 "relative PLT references");
872 } else {
873 error(ERR_NONFATAL, "ELF format does not support this"
874 " use of WRT");
875 wrt = NO_SEG; /* we can at least _try_ to continue */
879 p = mydata;
880 if (gnu16) {
881 error(ERR_WARNING | ERR_WARN_GNUELF,
882 "16-bit relocations in ELF is a GNU extension");
883 WRITESHORT(p, addr);
884 } else {
885 if (realbytes != 4 && segment != NO_SEG) {
886 error(ERR_NONFATAL,
887 "Unsupported non-32-bit ELF relocation");
889 WRITELONG(p, addr);
891 elf_sect_write(s, mydata, realbytes);
892 } else if (type == OUT_REL2ADR) {
893 if (segment == segto)
894 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
895 if (segment != NO_SEG && segment % 2) {
896 error(ERR_NONFATAL, "ELF format does not support"
897 " segment base references");
898 } else {
899 if (wrt == NO_SEG) {
900 error(ERR_WARNING | ERR_WARN_GNUELF,
901 "16-bit relocations in ELF is a GNU extension");
902 elf_add_reloc(s, segment, R_386_PC16);
903 } else {
904 error(ERR_NONFATAL,
905 "Unsupported non-32-bit ELF relocation");
908 p = mydata;
909 WRITESHORT(p, *(int32_t *)data - realbytes);
910 elf_sect_write(s, mydata, 2L);
911 } else if (type == OUT_REL4ADR) {
912 if (segment == segto)
913 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
914 if (segment != NO_SEG && segment % 2) {
915 error(ERR_NONFATAL, "ELF format does not support"
916 " segment base references");
917 } else {
918 if (wrt == NO_SEG) {
919 elf_add_reloc(s, segment, R_386_PC32);
920 } else if (wrt == elf_plt_sect + 1) {
921 elf_add_reloc(s, segment, R_386_PLT32);
922 } else if (wrt == elf_gotpc_sect + 1 ||
923 wrt == elf_gotoff_sect + 1 ||
924 wrt == elf_got_sect + 1) {
925 error(ERR_NONFATAL, "ELF format cannot produce PC-"
926 "relative GOT references");
927 } else {
928 error(ERR_NONFATAL, "ELF format does not support this"
929 " use of WRT");
930 wrt = NO_SEG; /* we can at least _try_ to continue */
933 p = mydata;
934 WRITELONG(p, *(int32_t *)data - realbytes);
935 elf_sect_write(s, mydata, 4L);
939 static void elf_write(void)
941 int nsections, align;
942 int scount;
943 char *p;
944 int commlen;
945 char comment[64];
946 int i;
948 struct SAA *symtab;
949 int32_t symtablen, symtablocal;
952 * Work out how many sections we will have. We have SHN_UNDEF,
953 * then the flexible user sections, then the four fixed
954 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
955 * then optionally relocation sections for the user sections.
957 if (of_elf32.current_dfmt == &df_stabs)
958 nsections = 8;
959 else
960 nsections = 5; /* SHN_UNDEF and the fixed ones */
962 add_sectname("", ".comment");
963 add_sectname("", ".shstrtab");
964 add_sectname("", ".symtab");
965 add_sectname("", ".strtab");
966 for (i = 0; i < nsects; i++) {
967 nsections++; /* for the section itself */
968 if (sects[i]->head) {
969 nsections++; /* for its relocations */
970 add_sectname(".rel", sects[i]->name);
974 if (of_elf32.current_dfmt == &df_stabs) {
975 /* in case the debug information is wanted, just add these three sections... */
976 add_sectname("", ".stab");
977 add_sectname("", ".stabstr");
978 add_sectname(".rel", ".stab");
982 * Do the comment.
984 *comment = '\0';
985 commlen =
986 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
989 * Output the ELF header.
991 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
992 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
993 fwriteint16_t(3, elffp); /* EM_386 processor ID */
994 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
995 fwriteint32_t(0L, elffp); /* no entry point */
996 fwriteint32_t(0L, elffp); /* no program header table */
997 fwriteint32_t(0x40L, elffp); /* section headers straight after
998 * ELF header plus alignment */
999 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1000 fwriteint16_t(0x34, elffp); /* size of ELF header */
1001 fwriteint16_t(0, elffp); /* no program header table, again */
1002 fwriteint16_t(0, elffp); /* still no program header table */
1003 fwriteint16_t(0x28, elffp); /* size of section header */
1004 fwriteint16_t(nsections, elffp); /* number of sections */
1005 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1006 * section header table */
1007 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1008 fwriteint32_t(0L, elffp);
1009 fwriteint32_t(0L, elffp);
1012 * Build the symbol table and relocation tables.
1014 symtab = elf_build_symtab(&symtablen, &symtablocal);
1015 for (i = 0; i < nsects; i++)
1016 if (sects[i]->head)
1017 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1018 sects[i]->head);
1021 * Now output the section header table.
1024 elf_foffs = 0x40 + 0x28 * nsections;
1025 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1026 elf_foffs += align;
1027 elf_nsect = 0;
1028 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1030 elf_section_header(0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1031 scount = 1; /* needed for the stabs debugging to track the symtable section */
1032 p = shstrtab + 1;
1033 for (i = 0; i < nsects; i++) {
1034 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1035 (sects[i]->type == SHT_PROGBITS ?
1036 sects[i]->data : NULL), TRUE,
1037 sects[i]->len, 0, 0, sects[i]->align, 0);
1038 p += strlen(p) + 1;
1039 scount++; /* dito */
1041 elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1042 scount++; /* dito */
1043 p += strlen(p) + 1;
1044 elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1045 scount++; /* dito */
1046 p += strlen(p) + 1;
1047 elf_section_header(p - shstrtab, 2, 0, symtab, TRUE, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1048 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1049 p += strlen(p) + 1;
1050 elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
1051 for (i = 0; i < nsects; i++)
1052 if (sects[i]->head) {
1053 p += strlen(p) + 1;
1054 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, TRUE,
1055 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1057 if (of_elf32.current_dfmt == &df_stabs) {
1058 /* for debugging information, create the last three sections
1059 which are the .stab , .stabstr and .rel.stab sections respectively */
1061 /* this function call creates the stab sections in memory */
1062 stabs32_generate();
1064 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1065 p += strlen(p) + 1;
1066 elf_section_header(p - shstrtab, 1, 0, stabbuf, 0, stablen,
1067 nsections - 2, 0, 4, 12);
1069 p += strlen(p) + 1;
1070 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, 0,
1071 stabstrlen, 0, 0, 4, 0);
1073 p += strlen(p) + 1;
1074 /* link -> symtable info -> section to refer to */
1075 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, 0,
1076 stabrellen, symtabsection, nsections - 3, 4,
1080 fwrite(align_str, align, 1, elffp);
1083 * Now output the sections.
1085 elf_write_sections();
1087 nasm_free(elf_sects);
1088 saa_free(symtab);
1091 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1093 struct SAA *s = saa_init(1L);
1094 struct Symbol *sym;
1095 uint8_t entry[16], *p;
1096 int i;
1098 *len = *local = 0;
1101 * First, an all-zeros entry, required by the ELF spec.
1103 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1104 *len += 16;
1105 (*local)++;
1108 * Next, an entry for the file name.
1110 p = entry;
1111 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1112 WRITELONG(p, 0); /* no value */
1113 WRITELONG(p, 0); /* no size either */
1114 WRITESHORT(p, 4); /* type FILE */
1115 WRITESHORT(p, SHN_ABS);
1116 saa_wbytes(s, entry, 16L);
1117 *len += 16;
1118 (*local)++;
1121 * Now some standard symbols defining the segments, for relocation
1122 * purposes.
1124 for (i = 1; i <= nsects + 1; i++) {
1125 p = entry;
1126 WRITELONG(p, 0); /* no symbol name */
1127 WRITELONG(p, 0); /* offset zero */
1128 WRITELONG(p, 0); /* size zero */
1129 WRITESHORT(p, 3); /* local section-type thing */
1130 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1131 saa_wbytes(s, entry, 16L);
1132 *len += 16;
1133 (*local)++;
1137 * Now the other local symbols.
1139 saa_rewind(syms);
1140 while ((sym = saa_rstruct(syms))) {
1141 if (sym->type & SYM_GLOBAL)
1142 continue;
1143 p = entry;
1144 WRITELONG(p, sym->strpos);
1145 WRITELONG(p, sym->value);
1146 WRITELONG(p, sym->size);
1147 WRITECHAR(p, sym->type); /* local non-typed thing */
1148 WRITECHAR(p, sym->other);
1149 WRITESHORT(p, sym->section);
1150 saa_wbytes(s, entry, 16L);
1151 *len += 16;
1152 (*local)++;
1156 * Now the global symbols.
1158 saa_rewind(syms);
1159 while ((sym = saa_rstruct(syms))) {
1160 if (!(sym->type & SYM_GLOBAL))
1161 continue;
1162 p = entry;
1163 WRITELONG(p, sym->strpos);
1164 WRITELONG(p, sym->value);
1165 WRITELONG(p, sym->size);
1166 WRITECHAR(p, sym->type); /* global non-typed thing */
1167 WRITECHAR(p, sym->other);
1168 WRITESHORT(p, sym->section);
1169 saa_wbytes(s, entry, 16L);
1170 *len += 16;
1173 return s;
1176 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1178 struct SAA *s;
1179 uint8_t *p, entry[8];
1181 if (!r)
1182 return NULL;
1184 s = saa_init(1L);
1185 *len = 0;
1187 while (r) {
1188 int32_t sym = r->symbol;
1190 if (sym >= GLOBAL_TEMP_BASE)
1191 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1193 p = entry;
1194 WRITELONG(p, r->address);
1195 WRITELONG(p, (sym << 8) + r->type);
1196 saa_wbytes(s, entry, 8L);
1197 *len += 8;
1199 r = r->next;
1202 return s;
1205 static void elf_section_header(int name, int type, int flags,
1206 void *data, int is_saa, int32_t datalen,
1207 int link, int info, int align, int eltsize)
1209 elf_sects[elf_nsect].data = data;
1210 elf_sects[elf_nsect].len = datalen;
1211 elf_sects[elf_nsect].is_saa = is_saa;
1212 elf_nsect++;
1214 fwriteint32_t((int32_t)name, elffp);
1215 fwriteint32_t((int32_t)type, elffp);
1216 fwriteint32_t((int32_t)flags, elffp);
1217 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1218 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1219 fwriteint32_t(datalen, elffp);
1220 if (data)
1221 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1222 fwriteint32_t((int32_t)link, elffp);
1223 fwriteint32_t((int32_t)info, elffp);
1224 fwriteint32_t((int32_t)align, elffp);
1225 fwriteint32_t((int32_t)eltsize, elffp);
1228 static void elf_write_sections(void)
1230 int i;
1231 for (i = 0; i < elf_nsect; i++)
1232 if (elf_sects[i].data) {
1233 int32_t len = elf_sects[i].len;
1234 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1235 int32_t align = reallen - len;
1236 if (elf_sects[i].is_saa)
1237 saa_fpwrite(elf_sects[i].data, elffp);
1238 else
1239 fwrite(elf_sects[i].data, len, 1, elffp);
1240 fwrite(align_str, align, 1, elffp);
1244 static void elf_sect_write(struct Section *sect,
1245 const uint8_t *data, uint32_t len)
1247 saa_wbytes(sect->data, data, len);
1248 sect->len += len;
1251 static int32_t elf_segbase(int32_t segment)
1253 return segment;
1256 static int elf_directive(char *directive, char *value, int pass)
1258 (void)directive;
1259 (void)value;
1260 (void)pass;
1261 return 0;
1264 static void elf_filename(char *inname, char *outname, efunc error)
1266 strcpy(elf_module, inname);
1267 standard_extension(inname, outname, ".o", error);
1270 static const char *elf_stdmac[] = {
1271 "%define __SECT__ [section .text]",
1272 "%macro __NASM_CDecl__ 1",
1273 "%define $_%1 $%1",
1274 "%endmacro",
1275 NULL
1277 static int elf_set_info(enum geninfo type, char **val)
1279 (void)type;
1280 (void)val;
1281 return 0;
1284 static struct dfmt df_stabs = {
1285 "ELF32 (i386) stabs debug format for Linux",
1286 "stabs",
1287 stabs32_init,
1288 stabs32_linenum,
1289 stabs32_deflabel,
1290 stabs32_directive,
1291 stabs32_typevalue,
1292 stabs32_output,
1293 stabs32_cleanup
1296 struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
1298 struct ofmt of_elf32 = {
1299 "ELF32 (i386) object files (e.g. Linux)",
1300 "elf32",
1301 NULL,
1302 elf32_debugs_arr,
1303 &null_debug_form,
1304 elf_stdmac,
1305 elf_init,
1306 elf_set_info,
1307 elf_out,
1308 elf_deflabel,
1309 elf_section_names,
1310 elf_segbase,
1311 elf_directive,
1312 elf_filename,
1313 elf_cleanup
1316 struct ofmt of_elf = {
1317 "ELF (short name for ELF32) ",
1318 "elf",
1319 NULL,
1320 elf32_debugs_arr,
1321 &null_debug_form,
1322 elf_stdmac,
1323 elf_init,
1324 elf_set_info,
1325 elf_out,
1326 elf_deflabel,
1327 elf_section_names,
1328 elf_segbase,
1329 elf_directive,
1330 elf_filename,
1331 elf_cleanup
1333 /* again, the stabs debugging stuff (code) */
1335 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1337 (void)of;
1338 (void)id;
1339 (void)fp;
1340 (void)error;
1343 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1345 (void)segto;
1346 if (!stabs_filename) {
1347 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1348 strcpy(stabs_filename, filename);
1349 } else {
1350 if (strcmp(stabs_filename, filename)) {
1351 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1352 in fact, this leak comes in quite handy to maintain a list of files
1353 encountered so far in the symbol lines... */
1355 /* why not nasm_free(stabs_filename); we're done with the old one */
1357 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1358 strcpy(stabs_filename, filename);
1361 stabs_immcall = 1;
1362 currentline = linenumber;
1365 void stabs32_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1366 char *special)
1368 (void)name;
1369 (void)segment;
1370 (void)offset;
1371 (void)is_global;
1372 (void)special;
1375 void stabs32_directive(const char *directive, const char *params)
1377 (void)directive;
1378 (void)params;
1381 void stabs32_typevalue(int32_t type)
1383 (void)type;
1386 void stabs32_output(int type, void *param)
1388 struct symlininfo *s;
1389 struct linelist *el;
1390 if (type == TY_STABSSYMLIN) {
1391 if (stabs_immcall) {
1392 s = (struct symlininfo *)param;
1393 if (strcmp(s->name, ".text"))
1394 return; /* we are only interested in the text stuff */
1395 numlinestabs++;
1396 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1397 el->info.offset = s->offset;
1398 el->info.section = s->section;
1399 el->info.name = s->name;
1400 el->line = currentline;
1401 el->filename = stabs_filename;
1402 el->next = 0;
1403 if (stabslines) {
1404 stabslines->last->next = el;
1405 stabslines->last = el;
1406 } else {
1407 stabslines = el;
1408 stabslines->last = el;
1412 stabs_immcall = 0;
1415 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1416 do {\
1417 WRITELONG(p,n_strx); \
1418 WRITECHAR(p,n_type); \
1419 WRITECHAR(p,n_other); \
1420 WRITESHORT(p,n_desc); \
1421 WRITELONG(p,n_value); \
1422 } while (0)
1424 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1426 void stabs32_generate(void)
1428 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1429 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1430 char **allfiles;
1431 int *fileidx;
1433 struct linelist *ptr;
1435 ptr = stabslines;
1437 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1438 for (i = 0; i < numlinestabs; i++)
1439 allfiles[i] = 0;
1440 numfiles = 0;
1441 while (ptr) {
1442 if (numfiles == 0) {
1443 allfiles[0] = ptr->filename;
1444 numfiles++;
1445 } else {
1446 for (i = 0; i < numfiles; i++) {
1447 if (!strcmp(allfiles[i], ptr->filename))
1448 break;
1450 if (i >= numfiles) {
1451 allfiles[i] = ptr->filename;
1452 numfiles++;
1455 ptr = ptr->next;
1457 strsize = 1;
1458 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1459 for (i = 0; i < numfiles; i++) {
1460 fileidx[i] = strsize;
1461 strsize += strlen(allfiles[i]) + 1;
1463 mainfileindex = 0;
1464 for (i = 0; i < numfiles; i++) {
1465 if (!strcmp(allfiles[i], elf_module)) {
1466 mainfileindex = i;
1467 break;
1471 /* worst case size of the stab buffer would be:
1472 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1474 sbuf =
1475 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1476 sizeof(struct stabentry));
1478 ssbuf = (uint8_t *)nasm_malloc(strsize);
1480 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1481 rptr = rbuf;
1483 for (i = 0; i < numfiles; i++) {
1484 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1486 ssbuf[0] = 0;
1488 stabstrlen = strsize; /* set global variable for length of stab strings */
1490 sptr = sbuf;
1491 /* this is the first stab, its strx points to the filename of the
1492 the source-file, the n_desc field should be set to the number
1493 of remaining stabs
1495 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1497 ptr = stabslines;
1498 numstabs = 0;
1500 if (ptr) {
1501 /* this is the stab for the main source file */
1502 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1504 /* relocation table entry */
1506 /* Since the above WRITE_STAB calls have already */
1507 /* created two entries, the index in the info.section */
1508 /* member must be adjusted by adding 3 */
1510 WRITELONG(rptr, (sptr - sbuf) - 4);
1511 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1513 numstabs++;
1514 currfile = mainfileindex;
1517 while (ptr) {
1518 if (strcmp(allfiles[currfile], ptr->filename)) {
1519 /* oops file has changed... */
1520 for (i = 0; i < numfiles; i++)
1521 if (!strcmp(allfiles[i], ptr->filename))
1522 break;
1523 currfile = i;
1524 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1525 ptr->info.offset);
1526 numstabs++;
1528 /* relocation table entry */
1529 WRITELONG(rptr, (sptr - sbuf) - 4);
1530 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1533 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1534 numstabs++;
1536 /* relocation table entry */
1538 WRITELONG(rptr, (sptr - sbuf) - 4);
1539 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1541 ptr = ptr->next;
1545 ((struct stabentry *)sbuf)->n_desc = numstabs;
1547 nasm_free(allfiles);
1548 nasm_free(fileidx);
1550 stablen = (sptr - sbuf);
1551 stabrellen = (rptr - rbuf);
1552 stabrelbuf = rbuf;
1553 stabbuf = sbuf;
1554 stabstrbuf = ssbuf;
1557 void stabs32_cleanup()
1559 struct linelist *ptr, *del;
1560 if (!stabslines)
1561 return;
1562 ptr = stabslines;
1563 while (ptr) {
1564 del = ptr;
1565 ptr = ptr->next;
1566 nasm_free(del);
1568 if (stabbuf)
1569 nasm_free(stabbuf);
1570 if (stabrelbuf)
1571 nasm_free(stabrelbuf);
1572 if (stabstrbuf)
1573 nasm_free(stabstrbuf);
1576 #endif /* OF_ELF */